Skip to content

Commit 7a55e0f

Browse files
authored
Merge pull request #30 from jiangliu/globalotps
RFC: Refactor ConfigBuilder as GlobalOpts
2 parents a243ddd + d170d62 commit 7a55e0f

File tree

3 files changed

+279
-223
lines changed

3 files changed

+279
-223
lines changed

crates/runc/src/lib.rs

Lines changed: 9 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
use std::fmt::{self, Display};
3939
use std::path::{Path, PathBuf};
4040
use std::process::ExitStatus;
41-
use std::time::Duration;
4241

4342
use oci_spec::runtime::{Linux, Process};
4443

@@ -58,7 +57,6 @@ use crate::error::Error;
5857
#[cfg(feature = "async")]
5958
use crate::monitor::{DefaultMonitor, Exit, ProcessMonitor};
6059
use crate::options::*;
61-
use crate::utils::{JSON, TEXT};
6260

6361
type Result<T> = std::result::Result<T, crate::error::Error>;
6462

@@ -98,177 +96,9 @@ impl Default for LogFormat {
9896
}
9997
}
10098

101-
/// Global options builder for the runc binary.
102-
///
103-
/// These options will be passed for all subsequent runc calls.
104-
/// See <https://github.com/opencontainers/runc/blob/main/man/runc.8.md#global-options>
105-
#[derive(Debug, Default)]
106-
pub struct ConfigBuilder {
107-
/// Override the name of the runc binary. If [`None`], `runc` is used.
108-
command: Option<PathBuf>,
109-
/// Path to root directory of container rootfs.
110-
root: Option<PathBuf>,
111-
/// Debug logging.
112-
///
113-
/// If true, debug level logs are emitted.
114-
debug: bool,
115-
/// Path to log file.
116-
log: Option<PathBuf>,
117-
/// Log format to use.
118-
log_format: LogFormat,
119-
/// Set process group ID (gpid).
120-
set_pgid: bool,
121-
/// Use systemd cgroup.
122-
systemd_cgroup: bool,
123-
/// Whether to use rootless mode.
124-
///
125-
/// If [`None`], `auto` settings is used.
126-
/// Note that "auto" is different from explicit "true" or "false".
127-
rootless: Option<bool>,
128-
/// Timeout settings for runc command.
129-
///
130-
/// Default is 5 seconds.
131-
/// This will be used only in AsyncClient.
132-
timeout: Duration,
133-
}
134-
13599
/// A shortcut to create `runc` global options builder.
136-
pub fn builder() -> ConfigBuilder {
137-
ConfigBuilder::default()
138-
}
139-
140-
impl ConfigBuilder {
141-
/// Create new config builder with no options.
142-
pub fn new() -> Self {
143-
Default::default()
144-
}
145-
146-
pub fn command(mut self, command: impl AsRef<Path>) -> Self {
147-
self.command = Some(command.as_ref().to_path_buf());
148-
self
149-
}
150-
151-
/// Set the root directory to store containers' state.
152-
///
153-
/// The path should be located on tmpfs.
154-
/// Default is `/run/runc`, or `$XDG_RUNTIME_DIR/runc` for rootless containers.
155-
pub fn root(mut self, root: impl AsRef<Path>) -> Self {
156-
self.root = Some(root.as_ref().to_path_buf());
157-
self
158-
}
159-
160-
/// Enable debug logging.
161-
pub fn debug(mut self, debug: bool) -> Self {
162-
self.debug = debug;
163-
self
164-
}
165-
166-
/// Set the log destination to path.
167-
///
168-
/// The default is to log to stderr.
169-
pub fn log(&mut self, log: impl AsRef<Path>) -> &mut Self {
170-
self.log = Some(log.as_ref().to_path_buf());
171-
self
172-
}
173-
174-
/// Set the log format (default is text).
175-
pub fn log_format(mut self, log_format: LogFormat) -> Self {
176-
self.log_format = log_format;
177-
self
178-
}
179-
180-
/// Set the log format to JSON.
181-
pub fn log_json(self) -> Self {
182-
self.log_format(LogFormat::Json)
183-
}
184-
185-
/// Set the log format to TEXT.
186-
pub fn log_text(self) -> Self {
187-
self.log_format(LogFormat::Text)
188-
}
189-
190-
/// Enable systemd cgroup support.
191-
///
192-
/// If this is set, the container spec (`config.json`) is expected to have `cgroupsPath` value in
193-
// the `slice:prefix:name` form (e.g. `system.slice:runc:434234`).
194-
pub fn systemd_cgroup(mut self, systemd_cgroup: bool) -> Self {
195-
self.systemd_cgroup = systemd_cgroup;
196-
self
197-
}
198-
199-
/// Enable or disable rootless mode.
200-
///
201-
// Default is auto, meaning to auto-detect whether rootless should be enabled.
202-
pub fn rootless(mut self, rootless: bool) -> Self {
203-
self.rootless = Some(rootless);
204-
self
205-
}
206-
207-
/// Set rootless mode to auto.
208-
pub fn rootless_auto(mut self) -> Self {
209-
self.rootless = None;
210-
self
211-
}
212-
213-
pub fn set_pgid(mut self, set_pgid: bool) -> Self {
214-
self.set_pgid = set_pgid;
215-
self
216-
}
217-
218-
pub fn timeout(&mut self, millis: u64) -> &mut Self {
219-
self.timeout = Duration::from_millis(millis);
220-
self
221-
}
222-
223-
fn build_runc(self) -> Result<Runc> {
224-
let path = self
225-
.command
226-
.clone()
227-
.unwrap_or_else(|| PathBuf::from("runc"));
228-
229-
let command = utils::binary_path(path).ok_or(Error::NotFound)?;
230-
231-
let mut args = Vec::new();
232-
233-
// --root path : Set the root directory to store containers' state.
234-
if let Some(root) = self.root {
235-
args.push("--root".into());
236-
args.push(utils::abs_string(root)?);
237-
}
238-
239-
// --debug : Enable debug logging.
240-
if self.debug {
241-
args.push("--debug".into());
242-
}
243-
244-
// --log path : Set the log destination to path. The default is to log to stderr.
245-
if let Some(log_path) = self.log {
246-
args.push("--log".into());
247-
args.push(utils::abs_string(log_path)?);
248-
}
249-
250-
// --log-format text|json : Set the log format (default is text).
251-
args.push("--log-format".into());
252-
args.push(self.log_format.to_string());
253-
254-
// --systemd-cgroup : Enable systemd cgroup support.
255-
if self.systemd_cgroup {
256-
args.push("--systemd-cgroup".into());
257-
}
258-
259-
// --rootless true|false|auto : Enable or disable rootless mode.
260-
if let Some(mode) = self.rootless {
261-
let arg = format!("--rootless={}", mode);
262-
args.push(arg);
263-
}
264-
265-
Ok(Runc { command, args })
266-
}
267-
268-
pub fn build(self) -> Result<Runc> {
269-
let runc = self.build_runc()?;
270-
Ok(runc)
271-
}
100+
pub fn builder() -> GlobalOpts {
101+
GlobalOpts::default()
272102
}
273103

274104
#[cfg(not(feature = "async"))]
@@ -304,6 +134,7 @@ impl Runc {
304134
}
305135
}
306136

137+
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
307138
#[cfg(not(feature = "async"))]
308139
impl Runc {
309140
pub fn checkpoint(&self) -> Result<()> {
@@ -601,7 +432,7 @@ impl Runc {
601432
}
602433

603434
/// Return an event stream of container notifications
604-
pub async fn events(&self, _id: &str, _interval: &Duration) -> Result<()> {
435+
pub async fn events(&self, _id: &str, _interval: &std::time::Duration) -> Result<()> {
605436
Err(Error::Unimplemented("events".to_string()))
606437
}
607438

@@ -728,21 +559,22 @@ impl Runc {
728559
Ok(())
729560
}
730561
}
562+
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
731563

732564
#[cfg(test)]
733565
#[cfg(all(target_os = "linux", not(feature = "async")))]
734566
mod tests {
735567
use super::*;
736568

737569
fn ok_client() -> Runc {
738-
ConfigBuilder::new()
570+
GlobalOpts::new()
739571
.command("/bin/true")
740572
.build()
741573
.expect("unable to create runc instance")
742574
}
743575

744576
fn fail_client() -> Runc {
745-
ConfigBuilder::new()
577+
GlobalOpts::new()
746578
.command("/bin/false")
747579
.build()
748580
.expect("unable to create runc instance")
@@ -875,14 +707,14 @@ mod tests {
875707
use super::*;
876708

877709
fn ok_client() -> Runc {
878-
ConfigBuilder::new()
710+
GlobalOpts::new()
879711
.command("/bin/true")
880712
.build()
881713
.expect("unable to create runc instance")
882714
}
883715

884716
fn fail_client() -> Runc {
885-
ConfigBuilder::new()
717+
GlobalOpts::new()
886718
.command("/bin/false")
887719
.build()
888720
.expect("unable to create runc instance")

0 commit comments

Comments
 (0)