-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathpre_game.rs
More file actions
201 lines (178 loc) · 6.07 KB
/
Copy pathpre_game.rs
File metadata and controls
201 lines (178 loc) · 6.07 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::path::Path;
use clap::Args;
use color_eyre::{
Result,
eyre::{WrapErr, bail},
};
use argument_parsers::RobotAddress;
use indicatif::ProgressBar;
use repository::{Repository, upload::get_binary};
use robot::{Network, Robot, SystemctlAction};
use tempfile::tempdir;
use crate::{
cargo::{self, CargoCommand, build, cargo, environment::EnvironmentArguments},
deploy_config::DeployConfig,
progress_indicator::ProgressIndicator,
};
#[derive(Args)]
#[group(skip)]
pub struct Arguments {
#[command(flatten)]
pub pre_game: PreGameArguments,
#[command(flatten)]
pub environment: EnvironmentArguments,
#[command(flatten, next_help_heading = "Cargo Options")]
pub build: build::Arguments,
}
#[derive(Args)]
pub struct PreGameArguments {
/// Do not build before uploading
#[arg(long, visible_alias = "nich-bauen")]
pub no_build: bool,
/// Do not restart HULK service after uploading
#[arg(long, visible_alias = "nich-neustarten")]
pub no_restart: bool,
/// Do not remove existing remote files during uploading
#[arg(long, visible_alias = "nich-säubern")]
pub no_clean: bool,
/// Skip the OS version check
#[arg(long, visible_alias = "überspringe-bs-prüfung")]
pub skip_os_check: bool,
/// Prepare everything for the upload without performing the actual one
#[arg(long, visible_alias = "vorbereit")]
pub prepare: bool,
/// The Robots to apply the pregame to, queried from the deploy.toml if not specified
pub robots: Option<Vec<RobotAddress>>,
}
pub async fn pre_game(arguments: Arguments, repository: &Repository) -> Result<()> {
let config = DeployConfig::read_from_file(repository)
.await
.wrap_err("failed to read deploy config from file")?;
let playing_robots = config.playing_robots()?;
let robots = if let Some(robots) = &arguments.pre_game.robots {
for robot in robots {
if !playing_robots.contains(robot) {
bail!("Robot with IP {robot} is not one of the playing Robots in the deploy.toml");
}
}
robots
} else {
&playing_robots
};
let wifi = config.wifi;
config
.configure_repository(repository)
.await
.wrap_err("failed to configure repository")?;
let upload_directory = tempdir().wrap_err("failed to get temporary directory")?;
const BINARY_NAME: &str = "hulk_ros_z";
let hulk_binary = get_binary(arguments.build.profile(), BINARY_NAME);
let cargo_arguments = cargo::Arguments {
manifest: Some(
repository
.root
.join(format!("crates/{BINARY_NAME}/Cargo.toml"))
.into_os_string(),
),
environment: arguments.environment,
cargo: arguments.build,
};
if !arguments.pre_game.no_build {
cargo(cargo_arguments, repository, &[&hulk_binary])
.await
.wrap_err("failed to build")?;
}
if arguments.pre_game.prepare {
eprintln!("Preparation complete, skipping the rest");
return Ok(());
}
repository
.populate_upload_directory(&upload_directory, &[hulk_binary])
.await
.wrap_err("failed to populate upload directory")?;
let arguments = &arguments.pre_game;
let upload_directory = &upload_directory;
ProgressIndicator::new()
.map_tasks(
robots,
"Executing pregame tasks",
|robot_address, progress_bar| async move {
setup_robot(
robot_address,
upload_directory,
arguments,
wifi,
progress_bar,
repository,
)
.await
},
)
.await;
Ok(())
}
async fn setup_robot(
robot_address: &RobotAddress,
upload_directory: impl AsRef<Path>,
arguments: &PreGameArguments,
wifi: Network,
progress: ProgressBar,
repository: &Repository,
) -> Result<()> {
progress.set_message("Pinging Robot...");
let robot = Robot::ping_until_available(robot_address.ip).await;
if !arguments.skip_os_check {
progress.set_message("Checking OS version...");
let robot_os_version = robot
.get_os_version()
.await
.wrap_err_with(|| format!("failed to get OS version of {robot_address}"))?;
let expected_os_version = repository
.read_os_version()
.await
.wrap_err("failed to get configured OS version")?;
if robot_os_version != expected_os_version {
bail!(
"mismatched OS versions: Expected {expected_os_version}, found {robot_os_version}"
);
}
}
progress.set_message("Stopping HULK...");
robot
.execute_systemctl(SystemctlAction::Stop, "hulk")
.await
.wrap_err_with(|| format!("failed to stop HULK service on {robot_address}"))?;
progress.set_message("Uploading: ...");
robot
.upload(upload_directory, "hulk", !arguments.no_clean, |status| {
progress.set_message(format!("Uploading: {status}"))
})
.await
.wrap_err_with(|| format!("failed to upload binary to {robot_address}"))?;
if wifi != Network::None {
progress.set_message("Scanning for WiFi...");
robot
.scan_networks()
.await
.wrap_err_with(|| format!("failed to scan for networks on {robot_address}"))?;
}
progress.set_message("Setting WiFi...");
robot
.set_wifi(wifi)
.await
.wrap_err_with(|| format!("failed to set network on {robot_address}"))?;
if !arguments.no_restart {
progress.set_message("Restarting HULK...");
if let Err(error) = robot
.execute_systemctl(SystemctlAction::Start, "hulk")
.await
{
let logs = robot
.retrieve_logs()
.await
.wrap_err("failed to retrieve logs")?;
bail!("failed to restart hulk: {error:#?}\nLogs:\n{logs}")
};
}
Ok(())
}