Skip to content

Commit 77b49e7

Browse files
ghubertpaloDamien LACHAUME / PALO-IT
authored andcommitted
add import script
1 parent 139295f commit 77b49e7

File tree

5 files changed

+95
-23
lines changed

5 files changed

+95
-23
lines changed

Cargo.lock

Lines changed: 7 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
set +a -eu -o pipefail
3+
#set -v
4+
5+
error() {
6+
echo $1;
7+
exit 1;
8+
}
9+
10+
clean_directory() {
11+
echo "Cleaning data directory…"
12+
rm $DATA_DIR/*.json || true
13+
}
14+
15+
# $1=URL $2=artifact_name
16+
download_list() {
17+
local -r url=${1:?"No URL given to download_list function."};
18+
local -r artifact=${2:?"No artifact type given to download_list function."};
19+
20+
echo "Downloading ${artifact} list."
21+
wget -O $DATA_DIR/${artifact}.json --quiet "${url}"
22+
}
23+
24+
# $1=URL $2=artifact_name $3=JSON field
25+
download_artifacts() {
26+
local -r url=${1:?"No URL given to download_list function."};
27+
local -r artifact=${2:?"No artifact type given to download_list function."};
28+
local -r json_field=${3:?"No JSON field given to read from artifact list."};
29+
local -i nb=1
30+
31+
echo -n "Downloading ${artifact} data: "
32+
tput sc;
33+
34+
for digest in $(jq -r .[].${json_field} < $DATA_DIR/${artifact}s.json);
35+
do
36+
tput rc;
37+
wget -O $DATA_DIR/${artifact}-${digest}.json --quiet "${url}/${digest}"
38+
let "nb=nb+1"
39+
echo -n "$nb "
40+
done
41+
echo
42+
}
43+
44+
# MAIN execution
45+
46+
declare -r DATA_DIR=${1:?"No data directory given to download JSON files."};
47+
declare -r BASE_URL=${2:?"No Mithril Aggregator URL given."};
48+
49+
if [ ! -d "$DATA_DIR" ]; then error "Specified directory '${DATA_DIR}' is not a directory."; fi
50+
wget --quiet --server-response --spider $BASE_URL || error "Could not reach URL '${BASE_URL}'.";
51+
52+
export DATA_DIR URL;
53+
54+
clean_directory;
55+
56+
download_list $BASE_URL/certificates certificates
57+
download_artifacts $BASE_URL/certificate certificate "hash"
58+
59+
download_list $BASE_URL/artifact/snapshots snapshots
60+
download_artifacts $BASE_URL/artifact/snapshot snapshot digest
61+
62+
download_list $BASE_URL/artifact/mithril-stake-distributions mithril-stake-distributions
63+
download_artifacts $BASE_URL/artifact/mithril-stake-distribution mithril-stake-distribution "hash"
64+

mithril-fake-aggregator/src/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub async fn epoch_settings(State(state): State<SharedState>) -> Result<String,
1212
let app_state = state.read().await;
1313
let epoch_settings = app_state.get_epoch_settings().await?;
1414

15-
Ok(epoch_settings.into())
15+
Ok(epoch_settings)
1616
}
1717

1818
pub async fn snapshot(

mithril-fake-aggregator/src/main.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use tower_http::{
2121
trace::{DefaultMakeSpan, DefaultOnRequest, DefaultOnResponse, TraceLayer},
2222
LatencyUnit,
2323
};
24-
use tracing::{debug, error, info, trace, Level};
24+
use tracing::{debug, error, info, trace, warn, Level};
2525

2626
use crate::shared_state::{AppState, SharedState};
2727

@@ -105,10 +105,13 @@ impl OsSignalHandler {
105105
while let Some(signal) = signals.next().await {
106106
match signal {
107107
SIGTERM | SIGINT | SIGQUIT => {
108-
tracing::warn!("Signal caught: {signal}");
108+
warn!("Signal caught: {signal}, exiting.");
109109

110110
break;
111111
}
112+
SIGHUP => {
113+
info!("SIGHUP signal caught.");
114+
}
112115
_ => unreachable!(),
113116
}
114117
}
@@ -126,7 +129,7 @@ async fn main() -> StdResult<()> {
126129

127130
match &result {
128131
Err(e) => error!("{e}"),
129-
Ok(_) => trace!("terminated!"),
132+
Ok(_) => debug!("soft terminated"),
130133
};
131134

132135
result
@@ -142,15 +145,21 @@ impl Application {
142145

143146
trace!("setting up signal hook…");
144147
// supported signals
145-
let signals = Signals::new(&[SIGTERM, SIGINT, SIGQUIT])?;
148+
let signals = Signals::new([SIGTERM, SIGINT, SIGQUIT, SIGHUP])?;
146149

147150
// launch signal detector
148151
let signal_handler = signals.handle();
149152

150153
trace!("setting up shared state…");
151154
let shared_state: SharedState = match params.data_directory {
152-
Some(directory) => AppState::from_directory(&directory)?.into(),
153-
None => AppState::default().into(),
155+
Some(directory) => {
156+
info!("Read data files from directory '{}'.", directory.display());
157+
AppState::from_directory(&directory)?.into()
158+
}
159+
None => {
160+
debug!("Using default data set.");
161+
AppState::default().into()
162+
}
154163
};
155164

156165
trace!("configuring router…");
@@ -190,7 +199,7 @@ impl Application {
190199
);
191200
let listener = {
192201
let connection_string = format!("{}:{}", params.ip_address, params.tcp_port);
193-
debug!("binding on {connection_string}");
202+
info!("binding on {connection_string}");
194203
tokio::net::TcpListener::bind(&connection_string)
195204
.await
196205
.with_context(|| format!("Could not listen on '{}'.", connection_string))?

mithril-fake-aggregator/src/shared_state.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{collections::BTreeMap, path::Path, sync::Arc};
33
use anyhow::{anyhow, Context};
44
use serde_json::Value;
55
use tokio::sync::RwLock;
6+
use tracing::{debug, trace};
67

78
use crate::StdResult;
89

@@ -43,7 +44,7 @@ impl AppState {
4344
let epoch_settings = default_values::epoch_settings().to_owned();
4445
let (certificate_list, certificates) = read_files(data_dir, "certificate", "hash")?;
4546
let (snapshot_list, snapshots) = read_files(data_dir, "snapshot", "digest")?;
46-
let (msd_list, msds) = read_files(data_dir, "msd", "hash")?;
47+
let (msd_list, msds) = read_files(data_dir, "mithril-stake-distribution", "hash")?;
4748

4849
let instance = Self {
4950
epoch_settings,
@@ -92,6 +93,8 @@ fn read_files(
9293
entity: &str,
9394
field_id: &str,
9495
) -> StdResult<(String, BTreeMap<String, String>)> {
96+
debug!("Read data files, entity='{entity}', field='{field_id}'.");
97+
9598
if !data_dir.exists() {
9699
return Err(anyhow!(format!(
97100
"Path '{}' does not exist.",
@@ -108,8 +111,9 @@ fn read_files(
108111
let list_file = {
109112
let list_file_name = format!("{entity}s.json");
110113

111-
data_dir.to_owned().join(&list_file_name)
114+
data_dir.to_owned().join(list_file_name)
112115
};
116+
trace!("Reading JSON list file '{}'.", list_file.display());
113117
let list = std::fs::read_to_string(&list_file)
114118
.with_context(|| format!("Error while reading file '{}'.", list_file.display()))?;
115119
let list_json: Value = serde_json::from_str(&list)
@@ -137,6 +141,7 @@ fn read_files(
137141
for id in ids {
138142
let filename = format!("{entity}-{id}.json");
139143
let path = data_dir.to_owned().join(&filename);
144+
trace!("Reading {entity} JSON file '{}'.", path.display());
140145
let content = std::fs::read_to_string(&path)
141146
.with_context(|| format!("Could not read entity file '{}'.", path.display()))?;
142147
let _value: Value = serde_json::from_str(&content).with_context(|| {

0 commit comments

Comments
 (0)