-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfig.rs
More file actions
375 lines (339 loc) · 9.72 KB
/
config.rs
File metadata and controls
375 lines (339 loc) · 9.72 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/// Configuration file format for floki
use crate::errors;
use crate::image;
use failure::Error;
use quicli::prelude::*;
use std::collections::BTreeMap;
use std::fs::File;
use std::path;
/// Specify the shell(s) for floki to run.
///
/// Floki runs the commands under [`init`][init] in an
/// "outer" shell, and then drops the user into an "inner"
/// shell.
///
/// [init]: ./struct.FlokiConfig.html#structfield.init
///
/// ---
///
/// Back to:
/// - [Floki Config](./struct.FlokiConfig.html#structfield.shell)
///
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub(crate) enum Shell {
/// Provide a string to specify both shells as the
/// same.
///
/// e.g.
///
/// ```yaml
/// bash
/// ```
///
Shell(String),
/// Specify both shells separately.
///
/// e.g.
///
/// ```yaml
/// inner: bash
/// outer: sh
/// ```
///
TwoShell { inner: String, outer: String },
}
impl Shell {
pub(crate) fn inner_shell(&self) -> &str {
match self {
Shell::Shell(s) => s,
Shell::TwoShell { inner: s, outer: _ } => s,
}
}
pub(crate) fn outer_shell(&self) -> &str {
match self {
Shell::Shell(s) => s,
Shell::TwoShell { inner: _, outer: s } => s,
}
}
}
/// Enable Docker-in-Docker inside the container that
/// floki runs.
///
/// ---
///
/// Back to:
/// - [Floki Config](./struct.FlokiConfig.html#structfield.dind)
///
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub(crate) enum DindConfig {
/// Provide a boolean.
///
/// If `true` is given, floki will enable Docker-in-Docker
/// using dind image: `docker:stable-dind`.
///
Toggle(bool),
/// Enable Docker-in-Docker and specify the dind image to use.
///
/// e.g.
///
/// ```yaml
/// image: docker:19.03-dind
/// ```
///
Image { image: String },
}
impl DindConfig {
pub fn deactivated() -> Self {
DindConfig::Toggle(false)
}
pub fn enabled(&self) -> bool {
match self {
DindConfig::Toggle(v) => *v,
_ => true,
}
}
pub fn image(&self) -> &str {
match self {
DindConfig::Image { image } => image,
_ => "docker:stable-dind",
}
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
/// # Floki Volumes
///
/// Floki has the ability to use volumes for caching build artifacts between
/// runs of the container (amongst other things).
///
/// Floki creates directories on the host to back these volumes
/// in `~/.floki/volumes`.
///
/// ---
///
/// Back to:
/// - [Floki Config](./struct.FlokiConfig.html#structfield.volumes)
///
pub(crate) struct Volume {
#[serde(default = "default_to_false")]
/// _Optional_
///
/// _Default:_ `false`
///
/// Share this volume with other containers instantiated from
/// different floki config files.
///
/// If `false`, this volume is only accessible to containers
/// instantiated using this config file.
///
pub(crate) shared: bool,
/// The path to which the volume is mounted inside the container.
///
pub(crate) mount: path::PathBuf,
}
/// # Floki Configuration Reference
///
/// By default floki looks for its configuration file in `floki.yaml`
/// (See `floki --help` for how to override this).
///
/// This document serves as a complete reference for all that can be
/// included in this configuration file. See the [user documentation][ud]
/// for installation instructions, usage guidance, and recipes for
/// a number of use cases.
///
/// [ud]: https://metaswitch.github.io/floki/
///
/// Floki config is defined as a [YAML][yaml] document with the structure
/// detailed below.
///
/// [yaml]: https://yaml.org/spec/1.2/spec.html
///
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct FlokiConfig {
/// Specify how floki sources or builds the image to host your
/// environment.
///
pub(crate) image: image::Image,
/// _Optional_
///
/// Commands to be run before dropping the user into an interactive
/// shell.
///
/// If "inner" and "outer" shells are defined under [`shell`](#structfield.shell),
/// these commands run in the outer shell.
///
#[serde(default = "Vec::new")]
pub(crate) init: Vec<String>,
/// _Optional_
///
/// _Default:_ `sh`
///
/// Specify the shell for floki to run.
///
#[serde(default = "default_shell")]
pub(crate) shell: Shell,
/// _Optional_
///
/// _Default:_ `/src`
///
/// Path inside the container that floki will mount the
/// current working directory to.
///
#[serde(default = "default_mount")]
pub(crate) mount: path::PathBuf,
/// _Optional_
///
/// Extra command line arguments to pass to docker.
///
/// NOTE: This is a back door and if you find you have repeated use
/// of the same invocation using `docker_switches`, consider raising
/// an issue to request the use case be covered with mainline
/// floki features.
///
#[serde(default = "Vec::new")]
pub(crate) docker_switches: Vec<String>,
/// _Optional_
///
/// _Default:_ `false`
///
/// Forward your ssh-agent into the container.
///
/// NOTE: You will need to have an ssh-agent running on the host
/// before launching floki.
///
#[serde(default = "default_to_false")]
pub(crate) forward_ssh_agent: bool,
/// _Optional_
///
/// _Default:_ `false`
///
/// Enable Docker-in-Docker inside the container.
///
#[serde(default = "DindConfig::deactivated")]
pub(crate) dind: DindConfig,
/// _Optional_
///
/// _Default:_ `false`
///
/// Run interactive shell in the container as the host user.
///
#[serde(default = "default_to_false")]
pub(crate) forward_user: bool,
/// _Optional_
///
/// Specify the volumes to mount in the container as a mapping
/// of a name to [volume config][vol].
///
/// [vol]: ./struct.Volume.html
///
#[serde(default = "BTreeMap::new")]
pub(crate) volumes: BTreeMap<String, Volume>,
}
impl FlokiConfig {
pub fn from_file(file: &path::Path) -> Result<FlokiConfig, Error> {
let f = File::open(file).map_err(|e| errors::FlokiError::ProblemOpeningConfigYaml {
name: file.display().to_string(),
error: e,
})?;
let mut config: FlokiConfig = serde_yaml::from_reader(f).map_err(|e| {
errors::FlokiError::ProblemParsingConfigYaml {
name: file.display().to_string(),
error: e,
}
})?;
// Ensure the path to an external yaml file is correct.
// If the image.yaml.path file is relative, then it should
// be relative to the floki config file. At this point we
// already have the path to the floki config file, so we
// just prepend that to image.yaml.path.
if let image::Image::Yaml { ref mut yaml } = config.image {
if yaml.file.is_relative() {
yaml.file = file
.parent()
.ok_or_else(|| errors::FlokiInternalError::InternalAssertionFailed {
description: format!(
"could not constuct path to external yaml file '{:?}'",
&yaml.file
),
})?
.join(yaml.file.clone());
}
}
debug!(
"Parsed '{}' into configuration: {:?}",
file.display(),
&config
);
Ok(config)
}
}
fn default_shell() -> Shell {
Shell::Shell("sh".into())
}
fn default_mount() -> path::PathBuf {
path::Path::new("/src").to_path_buf()
}
fn default_to_false() -> bool {
false
}
#[cfg(test)]
mod test {
use super::*;
use serde_yaml;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TestShellConfig {
shell: Shell,
}
#[test]
fn test_single_shell_config() {
let yaml = "shell: bash";
let expected = TestShellConfig {
shell: Shell::Shell("bash".into()),
};
let actual: TestShellConfig = serde_yaml::from_str(yaml).unwrap();
assert!(actual == expected);
}
#[test]
fn test_two_shell_config() {
let yaml = "shell:\n outer: sh\n inner: bash";
let expected_shell = Shell::TwoShell {
inner: "bash".into(),
outer: "sh".into(),
};
let expected = TestShellConfig {
shell: expected_shell,
};
let actual: TestShellConfig = serde_yaml::from_str(yaml).unwrap();
assert!(actual == expected);
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TestDindConfig {
dind: DindConfig,
}
#[test]
fn test_dind_enabled_config() {
let yaml = "dind: true";
let expected = TestDindConfig {
dind: DindConfig::Toggle(true),
};
let actual: TestDindConfig = serde_yaml::from_str(yaml).unwrap();
assert_eq!(actual, expected);
assert_eq!(actual.dind.enabled(), true);
assert_eq!(actual.dind.image(), "docker:stable-dind");
}
#[test]
fn test_dind_image_config() {
let yaml = "dind:\n image: dind:custom";
let expected = TestDindConfig {
dind: DindConfig::Image {
image: "dind:custom".into(),
},
};
let actual: TestDindConfig = serde_yaml::from_str(yaml).unwrap();
assert_eq!(actual, expected);
assert_eq!(actual.dind.enabled(), true);
assert_eq!(actual.dind.image(), "dind:custom");
}
}