-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.rs
More file actions
100 lines (81 loc) · 3.18 KB
/
admin.rs
File metadata and controls
100 lines (81 loc) · 3.18 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
use crate::admin::cli::AdminArgs;
pub mod cli;
/// Run the Admin command.
/// The only stop condition is currently either an error or Ctrl+C.
pub async fn run(
endpoint: iroh::Endpoint,
admin_args: AdminArgs,
) -> anyhow::Result<serde_json::Value> {
let AdminArgs {
timeout,
cmd,
node_id,
} = admin_args;
let timeout = std::time::Duration::from_secs_f64(timeout);
tracing::debug!("processing {cmd:?}");
let json_value = match cmd {
cli::AdminCmd::EchoHash { args } => {
serde_json::to_value(crate::protocols::echo_hash::send(endpoint, args).await?)?
}
cli::AdminCmd::Ping {} => {
let start = tokio::time::Instant::now();
let client =
crate::protocols::enrollment::enrollment_agent::EnrollmentAgentClient::connect(
endpoint, node_id,
)
.await?;
let time_to_connect = tokio::time::Instant::now() - start;
let result = client.ping().await?;
tracing::info!("time to connect: {time_to_connect:?}. ping time {result:#?}");
serde_json::to_value(())?
}
cli::AdminCmd::EnrollmentAgent { cmd } => {
let client =
crate::protocols::enrollment::enrollment_agent::EnrollmentAgentClient::connect(
endpoint, node_id,
)
.await?;
match cmd {
cli::EnrollmentAgentCmd::Ping => {
let duration = client.ping().await?;
serde_json::to_value(format!("ping to {node_id} took {duration:?}"))?
}
cli::EnrollmentAgentCmd::GetFacts => {
let result = client.get_facts().await?;
serde_json::to_value(result)?
}
}
}
cli::AdminCmd::EnrollmentService { cmd } => {
let client =
crate::protocols::enrollment::enrollment_service::EnrollmentServiceClient::connect(
endpoint.clone(),
node_id,
timeout,
)
.await?;
match cmd {
cli::EnrollmentServiceCmd::Ping => {
let duration = client.ping(timeout).await?;
serde_json::to_value(duration)?
}
cli::EnrollmentServiceCmd::ListAgents => {
let response = client.list_subscribers(timeout).await?;
serde_json::to_value(response)?
}
cli::EnrollmentServiceCmd::GetFacts { node_id } => {
let response = client.get_subscriber_facts(timeout, node_id).await?;
serde_json::to_value(response)?
}
cli::EnrollmentServiceCmd::AssignNixosClosure { node_id, path } => {
client
.upload_and_assign_nixos_closure(timeout, node_id, path)
.await?;
serde_json::to_value(())?
}
}
}
};
tracing::debug!("response: {json_value:#?}");
Ok(json_value)
}