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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
//! Test manager configuration.
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
io,
ops::Deref,
path::{Path, PathBuf},
};
#[derive(err_derive::Error, Debug)]
pub enum Error {
#[error(display = "Failed to read config")]
Read(io::Error),
#[error(display = "Failed to parse config")]
InvalidConfig(serde_json::Error),
#[error(display = "Failed to write config")]
Write(io::Error),
}
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Config {
#[serde(skip)]
pub runtime_opts: RuntimeOptions,
pub vms: BTreeMap<String, VmConfig>,
pub mullvad_host: Option<String>,
}
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct RuntimeOptions {
pub display: Display,
pub keep_changes: bool,
}
#[derive(Default, Serialize, Deserialize, Clone)]
pub enum Display {
#[default]
None,
Local,
Vnc,
}
impl Config {
async fn load_or_default<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
Self::load(path).await.or_else(|error| match error {
Error::Read(ref io_err) if io_err.kind() == io::ErrorKind::NotFound => {
Ok(Self::default())
}
error => Err(error),
})
}
async fn load<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let data = tokio::fs::read(path).await.map_err(Error::Read)?;
serde_json::from_slice(&data).map_err(Error::InvalidConfig)
}
async fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let data = serde_json::to_vec_pretty(self).unwrap();
tokio::fs::write(path, &data).await.map_err(Error::Write)
}
pub fn get_vm(&self, name: &str) -> Option<&VmConfig> {
self.vms.get(name)
}
}
pub struct ConfigFile {
path: PathBuf,
config: Config,
}
impl ConfigFile {
/// Make config changes and save them to disk
pub async fn load_or_default<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
Ok(Self {
path: path.as_ref().to_path_buf(),
config: Config::load_or_default(path).await?,
})
}
/// Make config changes and save them to disk
pub async fn edit(&mut self, edit: impl FnOnce(&mut Config)) -> Result<(), Error> {
edit(&mut self.config);
self.config.save(&self.path).await
}
}
impl Deref for ConfigFile {
type Target = Config;
fn deref(&self) -> &Self::Target {
&self.config
}
}
#[derive(clap::Args, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub struct VmConfig {
/// Type of virtual machine to use
pub vm_type: VmType,
/// Path to a VM disk image
pub image_path: String,
/// Type of operating system.
pub os_type: OsType,
/// Package type to use, e.g. deb or rpm
#[arg(long, required_if_eq("os_type", "linux"))]
pub package_type: Option<PackageType>,
/// CPU architecture
#[arg(long, required_if_eq("os_type", "linux"))]
pub architecture: Option<Architecture>,
/// Tool to use for provisioning
#[arg(long, default_value = "noop")]
pub provisioner: Provisioner,
/// Username to use for SSH
#[arg(long, required_if_eq("provisioner", "ssh"))]
pub ssh_user: Option<String>,
/// Password to use for SSH
#[arg(long, required_if_eq("provisioner", "ssh"))]
pub ssh_password: Option<String>,
/// Additional disk images to mount/include
#[arg(long)]
pub disks: Vec<String>,
/// Where artifacts, such as app packages, are stored.
/// Usually /opt/testing on Linux.
#[arg(long)]
pub artifacts_dir: Option<String>,
/// Emulate a TPM. This also enables UEFI implicitly
#[serde(default)]
#[arg(long)]
pub tpm: bool,
}
impl VmConfig {
/// Combine authentication details, if all are present
pub fn get_ssh_options(&self) -> Option<(&str, &str)> {
Some((self.ssh_user.as_ref()?, self.ssh_password.as_ref()?))
}
pub fn get_runner_dir(&self) -> &Path {
match self.architecture {
None | Some(Architecture::X64) => self.get_x64_runner_dir(),
Some(Architecture::Aarch64) => self.get_aarch64_runner_dir(),
}
}
fn get_x64_runner_dir(&self) -> &Path {
pub const X64_LINUX_TARGET_DIR: &str = "./target/x86_64-unknown-linux-gnu/release";
pub const X64_WINDOWS_TARGET_DIR: &str = "./target/x86_64-pc-windows-gnu/release";
pub const X64_MACOS_TARGET_DIR: &str = "./target/x86_64-apple-darwin/release";
match self.os_type {
OsType::Linux => Path::new(X64_LINUX_TARGET_DIR),
OsType::Windows => Path::new(X64_WINDOWS_TARGET_DIR),
OsType::Macos => Path::new(X64_MACOS_TARGET_DIR),
}
}
fn get_aarch64_runner_dir(&self) -> &Path {
pub const AARCH64_LINUX_TARGET_DIR: &str = "./target/aarch64-unknown-linux-gnu/release";
pub const AARCH64_MACOS_TARGET_DIR: &str = "./target/aarch64-apple-darwin/release";
match self.os_type {
OsType::Linux => Path::new(AARCH64_LINUX_TARGET_DIR),
OsType::Macos => Path::new(AARCH64_MACOS_TARGET_DIR),
_ => unimplemented!(),
}
}
}
#[derive(clap::ValueEnum, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum VmType {
/// QEMU VM
Qemu,
/// Tart VM
Tart,
}
#[derive(clap::ValueEnum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum OsType {
Windows,
Linux,
Macos,
}
#[derive(clap::ValueEnum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PackageType {
Deb,
Rpm,
}
#[derive(clap::ValueEnum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Architecture {
X64,
Aarch64,
}
impl Architecture {
pub fn get_identifiers(&self) -> &[&'static str] {
match self {
Architecture::X64 => &["x86_64", "amd64"],
Architecture::Aarch64 => &["arm64", "aarch64"],
}
}
}
#[derive(clap::ValueEnum, Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Provisioner {
/// Do nothing: The image already includes a test runner service
#[default]
Noop,
/// Set up test runner over SSH.
Ssh,
}
|