Skip to content

Commit 15352c4

Browse files
committed
chore: use one-shot for init
1 parent 9ea4ef0 commit 15352c4

File tree

9 files changed

+21
-94
lines changed

9 files changed

+21
-94
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[package]
22
name = "rooz"
3-
version = "0.128.0"
3+
version = "0.129.0"
44
edition = "2021"
55

66
[dependencies]
77
age = {version = "0.11.1", features = ["armor"] }
88
base64 = "0.22.1"
99
bollard = { version = "0.19.2" }
10-
#bollard = { git = "https://github.com/fussybeaver/bollard", rev = "e785084893d6763dfa1a9c7e283c7ee9adde11eb"}
1110
bollard-stubs = "1.49.0-rc.28.3.3"
1211
clap = { version = "4.5.16", features = ["derive", "env"] }
1312
clap_complete = "4.5.24"

src/api/container.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{
1111
use base64::{engine::general_purpose, Engine as _};
1212

1313
use bollard::{
14-
container::LogOutput::{Console, StdErr, StdOut},
1514
errors::Error::{self, DockerResponseServerError},
1615
models::{
1716
ContainerCreateBody, ContainerCreateResponse, ContainerInspectResponse, ContainerState,
@@ -26,11 +25,7 @@ use bollard::{
2625
};
2726

2827
use futures::{future, StreamExt};
29-
use std::{
30-
collections::HashMap,
31-
io::{stderr, stdout, Write},
32-
time::Duration,
33-
};
28+
use std::{collections::HashMap, time::Duration};
3429
use tokio::time::{sleep, timeout};
3530

3631
pub fn inject2(script: &str, name: &str, post_sleep: bool) -> Vec<String> {
@@ -291,7 +286,6 @@ impl<'a> ContainerApi<'a> {
291286
RunMode::Git => (None, None, Some(true), Some(true)),
292287
RunMode::OneShot => (None, None, None, Some(true)),
293288
RunMode::Sidecar => (None, None, None, None),
294-
RunMode::Init => (None, None, None, None),
295289
};
296290

297291
let host_config = HostConfig {
@@ -556,25 +550,4 @@ impl<'a> ContainerApi<'a> {
556550
let _ = tokio::time::timeout(std::time::Duration::from_secs(2), log_task).await;
557551
Ok(exit_code)
558552
}
559-
560-
pub async fn logs_to_stdout(&self, container_name: &str) -> Result<(), AnyError> {
561-
let log_options = LogsOptions {
562-
stdout: true,
563-
follow: true,
564-
..Default::default()
565-
};
566-
567-
let mut stream = self.client.logs(&container_name, Some(log_options));
568-
569-
while let Some(l) = stream.next().await {
570-
match l {
571-
Ok(Console { message: m }) => stdout().write_all(&m)?,
572-
Ok(StdOut { message: m }) => stdout().write_all(&m)?,
573-
Ok(StdErr { message: m }) => stderr().write_all(&m)?,
574-
Ok(msg) => panic!("{}", msg),
575-
Err(e) => panic!("{}", e),
576-
};
577-
}
578-
Ok(())
579-
}
580553
}

src/api/crypt.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ impl CryptApi {
2626
}
2727
}
2828

29-
30-
3129
pub fn encrypt(
3230
&self,
3331
plaintext: String,

src/api/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub struct VolumeApi<'a> {
3131
pub container: &'a ContainerApi<'a>,
3232
}
3333

34-
pub struct CryptApi {
35-
}
34+
pub struct CryptApi {}
3635

3736
pub struct Api<'a> {
3837
pub exec: &'a ExecApi<'a>,

src/cmd/init.rs

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,19 @@
11
use std::str::FromStr;
22

33
use crate::{
4-
api::{container, Api},
4+
api::Api,
55
cli::InitParams,
66
config::config::SystemConfig,
77
constants,
88
model::{
9-
types::{AnyError, RunMode, RunSpec, VolumeResult},
9+
types::{AnyError, VolumeResult},
1010
volume::{RoozVolume, RoozVolumeRole},
1111
},
12-
util::{id, labels::Labels, ssh},
12+
util::ssh,
1313
};
1414
use age::secrecy::ExposeSecret;
15-
use bollard::models::MountTypeEnum::VOLUME;
16-
use bollard::service::Mount;
1715

1816
impl<'a> Api<'a> {
19-
async fn execute_init(
20-
&self,
21-
container_name: &str,
22-
entrypoint: &str,
23-
vol_name: &str,
24-
vol_mount_path: &str,
25-
image: &str,
26-
) -> Result<(), AnyError> {
27-
let workspace_key = id::random_suffix("init");
28-
let entrypoint = container::inject(entrypoint, "entrypoint.sh");
29-
let labels = Labels::default();
30-
let run_spec = RunSpec {
31-
reason: "init",
32-
image,
33-
uid: constants::ROOT_UID,
34-
work_dir: None,
35-
container_name,
36-
workspace_key: &workspace_key,
37-
mounts: Some(vec![Mount {
38-
typ: Some(VOLUME),
39-
read_only: Some(false),
40-
source: Some(vol_name.into()),
41-
target: Some(vol_mount_path.into()),
42-
..Default::default()
43-
}]),
44-
entrypoint: Some(entrypoint.iter().map(String::as_str).collect()),
45-
privileged: false,
46-
force_recreate: false,
47-
run_mode: RunMode::Init,
48-
labels,
49-
..Default::default()
50-
};
51-
52-
let result = self.container.create(run_spec).await?;
53-
self.container.start(result.id()).await?;
54-
self.container.logs_to_stdout(result.id()).await?;
55-
self.container.remove(result.id(), true).await?;
56-
Ok(())
57-
}
58-
5917
pub async fn init(&self, image: &str, uid: &str, spec: &InitParams) -> Result<(), AnyError> {
6018
let image_id = self.image.ensure(&image, false).await?.id;
6119

@@ -106,14 +64,15 @@ impl<'a> Api<'a> {
10664
&hostname, &uid,
10765
);
10866

109-
self.execute_init(
110-
"rooz-init-ssh",
111-
&init_ssh,
112-
ssh::VOLUME_NAME,
113-
"/tmp/.ssh",
114-
&image_id,
115-
)
116-
.await?;
67+
self.container
68+
.one_shot(
69+
"init",
70+
init_ssh,
71+
Some(vec![ssh::mount("/tmp/.ssh")]),
72+
None,
73+
Some(&image_id),
74+
)
75+
.await?;
11776
}
11877
VolumeResult::AlreadyExists => {
11978
println!("Rooz has been already initialized. Use --force to reinitialize.")

src/cmd/new.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ impl<'a> WorkspaceApi<'a> {
3333
cfg_builder.from_config(c);
3434
}
3535
cfg_builder.from_cli(cli_params, None);
36-
self.config.decrypt(cfg_builder, &self.api.system_config.age_identity()?).await?;
36+
self.config
37+
.decrypt(cfg_builder, &self.api.system_config.age_identity()?)
38+
.await?;
3739
cfg_builder.expand_vars()?;
3840

3941
let cfg = RuntimeConfig::from(&*cfg_builder);

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async fn main() -> Result<(), AnyError> {
115115
client: &docker,
116116
};
117117

118-
let crypt_api = CryptApi { };
118+
let crypt_api = CryptApi {};
119119

120120
let git_api = GitApi { api: &rooz };
121121

@@ -159,9 +159,7 @@ async fn main() -> Result<(), AnyError> {
159159
None => Ok(()),
160160
}?;
161161

162-
workspace
163-
.new(&name, &work, config_source, false,)
164-
.await?;
162+
workspace.new(&name, &work, config_source, false).await?;
165163
println!(
166164
"\nThe workspace is ready. Run 'rooz enter {}' to enter.",
167165
name

src/model/types.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ pub enum RunMode {
7878
Git,
7979
OneShot,
8080
Sidecar,
81-
Init,
8281
}
8382

8483
pub struct RunSpec<'a> {

0 commit comments

Comments
 (0)