-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmodule.rs
More file actions
175 lines (150 loc) · 4.83 KB
/
module.rs
File metadata and controls
175 lines (150 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::collections::HashMap;
use eyre::{ContextCompat, Result};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use toml::Table;
use crate::{
commit::client::SignerClient,
config::{
constants::{CONFIG_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, SIGNER_URL_ENV},
load_env_var,
utils::load_file_from_env,
},
types::{Chain, Jwt, ModuleId},
};
#[derive(Debug, Deserialize, Serialize)]
pub enum ModuleKind {
#[serde(alias = "commit")]
Commit,
}
/// Static module config from config file
#[derive(Debug, Deserialize, Serialize)]
pub struct StaticModuleConfig {
/// Unique id of the module
pub id: ModuleId,
/// Docker image of the module
pub docker_image: String,
/// Environment variables for the module
pub env: Option<HashMap<String, String>>,
/// Environment file for the module
pub env_file: Option<String>,
/// Type of the module
#[serde(rename = "type")]
pub kind: ModuleKind,
}
/// Runtime config to start a module
#[derive(Debug)]
pub struct StartCommitModuleConfig<T = ()> {
/// Unique id of the module
pub id: ModuleId,
/// Chain spec
pub chain: Chain,
/// Signer client to call Signer API
pub signer_client: SignerClient,
/// Opaque module config
pub extra: T,
}
/// Loads a module config from the environment and config file:
/// - [MODULE_ID_ENV] - the id of the module to load
/// - [CB_CONFIG_ENV] - the path to the config file
/// - [MODULE_JWT_ENV] - the jwt token for the module
// TODO: add metrics url here
pub fn load_commit_module_config<T: DeserializeOwned>() -> Result<StartCommitModuleConfig<T>> {
let module_id = ModuleId(load_env_var(MODULE_ID_ENV)?);
let module_jwt = Jwt(load_env_var(MODULE_JWT_ENV)?);
let signer_server_url = load_env_var(SIGNER_URL_ENV)?.parse()?;
#[derive(Debug, Deserialize)]
struct ThisModuleConfig<U> {
#[serde(flatten)]
static_config: StaticModuleConfig,
#[serde(flatten)]
extra: U,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ThisModule<U> {
Target(ThisModuleConfig<U>),
#[allow(dead_code)]
Other(Table),
}
#[derive(Deserialize, Debug)]
struct StubConfig<U> {
chain: Chain,
modules: Vec<ThisModule<U>>,
}
// load module config including the extra data (if any)
let (cb_config, _): (StubConfig<T>, _) = load_file_from_env(CONFIG_ENV)?;
// find all matching modules config
let matches: Vec<ThisModuleConfig<T>> = cb_config
.modules
.into_iter()
.filter_map(|m| match m {
ThisModule::Target(config) => Some(config),
_ => None,
})
.collect();
eyre::ensure!(!matches.is_empty(), "Failed to find matching config type");
let module_config = matches
.into_iter()
.find(|m| m.static_config.id == module_id)
.wrap_err(format!("failed to find module for {module_id}"))?;
let signer_client = SignerClient::new(signer_server_url, module_jwt, module_id)?;
Ok(StartCommitModuleConfig {
id: module_config.static_config.id,
chain: cb_config.chain,
signer_client,
extra: module_config.extra,
})
}
#[derive(Debug)]
pub struct StartBuilderModuleConfig<T> {
/// Unique id of the module
pub id: ModuleId,
/// Chain spec
pub chain: Chain,
/// Opaque module config
pub extra: T,
}
pub fn load_builder_module_config<T: DeserializeOwned>() -> eyre::Result<StartBuilderModuleConfig<T>>
{
let module_id = ModuleId(load_env_var(MODULE_ID_ENV)?);
#[derive(Debug, Deserialize)]
struct ThisModuleConfig<U> {
#[serde(flatten)]
static_config: StaticModuleConfig,
#[serde(flatten)]
extra: U,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ThisModule<U> {
Target(ThisModuleConfig<U>),
#[allow(dead_code)]
Other(Table),
}
#[derive(Deserialize, Debug)]
struct StubConfig<U> {
chain: Chain,
modules: Vec<ThisModule<U>>,
}
// load module config including the extra data (if any)
let (cb_config, _): (StubConfig<T>, _) = load_file_from_env(CONFIG_ENV)?;
// find all matching modules config
let matches: Vec<ThisModuleConfig<T>> = cb_config
.modules
.into_iter()
.filter_map(|m| match m {
ThisModule::Target(config) => Some(config),
_ => None,
})
.collect();
eyre::ensure!(!matches.is_empty(), "Failed to find matching config type");
let module_config = matches
.into_iter()
.find(|m| m.static_config.id == module_id)
.wrap_err(format!("failed to find module for {module_id}"))?;
Ok(StartBuilderModuleConfig {
id: module_config.static_config.id,
chain: cb_config.chain,
extra: module_config.extra,
})
}