Skip to content

Commit

Permalink
merge: #2667
Browse files Browse the repository at this point in the history
2667: feat(si): Implement a Container Engine Trait r=stack72 a=stack72

This is a container engine trait that needs to be completed before any new containe engines are added to the SI Launcher

prerequisite for #2607 

Co-authored-by: stack72 <[email protected]>
  • Loading branch information
si-bors-ng[bot] and stack72 authored Aug 22, 2023
2 parents aa710d9 + dad5771 commit 6faf6e6
Show file tree
Hide file tree
Showing 23 changed files with 1,406 additions and 678 deletions.
172 changes: 136 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ paste = "1.0.12"
pathdiff = "0.2.1"
petgraph = { version = "0.6.3", features = ["serde-1"] }
pin-project-lite = "0.2.9"
podman-api = "0.10"
postgres-types = { version = "0.2.5", features = ["derive"] }
pretty_assertions_sorted = "1.2.1"
proc-macro2 = "1.0.56"
Expand Down
61 changes: 16 additions & 45 deletions bin/si/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::args::{Commands, Engine};
use color_eyre::{eyre::eyre, Result};
use si_cli::{state::AppState, DockerClient};
use color_eyre::Result;
use si_cli::engine::docker_engine::DockerEngine;
use si_cli::engine::podman_engine::PodmanEngine;
use si_cli::state::AppState;
use std::sync::Arc;
use telemetry_application::{prelude::*, TelemetryConfig};
use tokio::sync::oneshot::Sender;
Expand All @@ -23,10 +25,9 @@ async fn main() -> Result<()> {
let mode = args.mode();
let is_preview = args.is_preview;

let docker_sock = if let Some(sock) = args.docker_sock.clone() {
sock
} else {
"".to_string()
let engine = match args.engine() {
Engine::Docker => DockerEngine::new(args.docker_sock.clone()).await?,
Engine::Podman => PodmanEngine::new(args.docker_sock.clone()).await?,
};

let web_host = args.web_host.clone();
Expand All @@ -41,31 +42,6 @@ async fn main() -> Result<()> {

tokio::spawn(wait_for_posthog_flush(ph_done_sender, ph_sender));

let docker_socket_candidates = vec![
#[allow(clippy::disallowed_methods)] // Used to determine a path relative to users's home
std::path::Path::new(&std::env::var("HOME")?)
.join(".docker")
.join("run")
.join("docker.sock"),
std::path::Path::new("/var/run/docker.sock").to_path_buf(),
];

let docker: DockerClient;
if let "" = docker_sock.as_str() {
let socket = docker_socket_candidates
.iter()
.find(|candidate| candidate.exists())
.ok_or(eyre!(
"failed to determine Docker socket location. Set a custom location using `--docker-sock` \
or `SI_DOCKER_SOCK`; candidates={docker_socket_candidates:?}"
))?;
docker = DockerClient::unix(socket)
} else {
println!("Checking for user supplied docker.sock");
let path = std::path::Path::new(docker_sock.as_str()).to_path_buf();
docker = DockerClient::unix(path);
}

let state = AppState::new(
ph_client,
Arc::from(current_version),
Expand All @@ -74,6 +50,7 @@ async fn main() -> Result<()> {
web_host,
web_port,
args.with_function_debug_logs,
Arc::from(engine),
);

println!(
Expand All @@ -89,10 +66,7 @@ async fn main() -> Result<()> {
let auth_api_host = std::env::var("AUTH_API").ok();

if !matches!(args.command, Commands::Update(_)) {
match state
.find(&docker, current_version, auth_api_host.as_deref())
.await
{
match state.find(current_version, auth_api_host.as_deref()).await {
Ok(update) => {
if update.si.is_some() {
println!("Launcher update found, please run `si update` to install it");
Expand Down Expand Up @@ -120,33 +94,32 @@ async fn main() -> Result<()> {

match args.command {
Commands::Install(_args) => {
state.install(&docker).await?;
state.install().await?;
}
Commands::Check(_args) => {
state.check(&docker, false).await?;
state.check(false).await?;
}
Commands::Launch(args) => {
state.launch(args.metrics).await?;
}
Commands::Start(_args) => {
state.start(&docker).await?;
state.start().await?;
}
Commands::Configure(args) => {
state.configure(args.force_reconfigure).await?;
}
Commands::Delete(args) => {
state.delete(&docker, args.keep_images).await?;
state.delete(args.keep_images).await?;
}
Commands::Restart(_args) => {
state.restart(&docker).await?;
state.restart().await?;
}
Commands::Stop(_args) => {
state.stop(&docker).await?;
state.stop().await?;
}
Commands::Update(args) => {
state
.update(
&docker,
current_version,
auth_api_host.as_deref(),
args.skip_confirmation,
Expand All @@ -155,9 +128,7 @@ async fn main() -> Result<()> {
.await?;
}
Commands::Status(args) => {
state
.status(&docker, args.show_logs, args.log_lines)
.await?;
state.status(args.show_logs, args.log_lines).await?;
} // Commands::Report(_args) => {
// state.report().await?;
// }
Expand Down
2 changes: 2 additions & 0 deletions lib/si-cli/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rust_library(
"//lib/si-posthog-rs:si-posthog",
"//lib/telemetry-rs:telemetry",
"//third-party/rust:axum",
"//third-party/rust:async-trait",
"//third-party/rust:base64",
"//third-party/rust:color-eyre",
"//third-party/rust:colored",
Expand All @@ -18,6 +19,7 @@ rust_library(
"//third-party/rust:indicatif",
"//third-party/rust:inquire",
"//third-party/rust:open",
"//third-party/rust:podman-api",
"//third-party/rust:rand",
"//third-party/rust:remain",
"//third-party/rust:reqwest",
Expand Down
2 changes: 2 additions & 0 deletions lib/si-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false

[dependencies]
axum = { workspace = true }
async-trait = { workspace = true }
base64 = { workspace = true }
color-eyre = { workspace = true }
colored = { workspace = true }
Expand All @@ -19,6 +20,7 @@ futures = { workspace = true }
indicatif = { workspace = true }
inquire = { workspace = true }
open = { workspace = true }
podman-api = { workspace = true }
rand = { workspace = true }
remain = { workspace = true }
reqwest = { workspace = true }
Expand Down
9 changes: 4 additions & 5 deletions lib/si-cli/src/cmd/check.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use crate::containers::DockerClient;
use crate::key_management::get_user_email;
use crate::state::AppState;
use crate::{CliResult, SiCliError};
use comfy_table::presets::UTF8_FULL;
use comfy_table::*;

impl AppState {
pub async fn check(&self, docker: &DockerClient, silent: bool) -> CliResult<()> {
pub async fn check(&self, silent: bool) -> CliResult<()> {
self.track(
get_user_email().await?,
serde_json::json!({"command-name": "check-dependencies"}),
);
invoke(docker, silent, self.is_preview()).await?;
invoke(self, silent, self.is_preview()).await?;
Ok(())
}
}

async fn invoke(docker: &DockerClient, silent: bool, is_preview: bool) -> CliResult<()> {
async fn invoke(app: &AppState, silent: bool, is_preview: bool) -> CliResult<()> {
if !silent {
println!("Checking that the system is able to interact with the docker engine to control System Initiative...");
}
Expand All @@ -25,7 +24,7 @@ async fn invoke(docker: &DockerClient, silent: bool, is_preview: bool) -> CliRes
return Ok(());
}

if let Err(_e) = docker.ping().await {
if let Err(_e) = app.container_engine().ping().await {
return Err(SiCliError::DockerEngine);
}

Expand Down
28 changes: 14 additions & 14 deletions lib/si-cli/src/cmd/delete.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
use crate::containers::DockerClient;
use crate::key_management::get_user_email;
use crate::state::AppState;
use crate::{CliResult, CONTAINER_NAMES};

impl AppState {
pub async fn delete(&self, docker: &DockerClient, keep_images: bool) -> CliResult<()> {
pub async fn delete(&self, keep_images: bool) -> CliResult<()> {
self.track(
get_user_email().await?,
serde_json::json!({"command-name": "delete-system"}),
);
invoke(self, docker, self.is_preview(), keep_images).await?;
invoke(self, self.is_preview(), keep_images).await?;
Ok(())
}
}

async fn invoke(
app: &AppState,
docker: &DockerClient,
is_preview: bool,
keep_images: bool,
) -> CliResult<()> {
app.check(docker, true).await?;
async fn invoke(app: &AppState, is_preview: bool, keep_images: bool) -> CliResult<()> {
app.check(true).await?;

if is_preview {
println!("Deleted the following containers and associated images:");
Expand All @@ -32,16 +26,22 @@ async fn invoke(
println!("{}", container_name);
continue;
}
let container_summary = docker
let container_summary = app
.container_engine()
.get_existing_container(container_name.clone())
.await?;
if let Some(container_summary) = container_summary {
docker
.delete_container(container_summary, container_name.clone())
app.container_engine()
.delete_container(
container_summary.id.unwrap().to_string(),
container_name.clone(),
)
.await?;

if !keep_images {
docker.cleanup_image(name.to_string()).await?;
app.container_engine()
.cleanup_image(name.to_string())
.await?;
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions lib/si-cli/src/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use crate::containers::DockerClient;
use crate::key_management::get_user_email;
use crate::state::AppState;
use crate::CliResult;

impl AppState {
pub async fn install(&self, docker: &DockerClient) -> CliResult<()> {
pub async fn install(&self) -> CliResult<()> {
self.track(
get_user_email().await?,
serde_json::json!({"command-name": "install"}),
);
invoke(docker, self.is_preview()).await?;
invoke(self, self.is_preview()).await?;
Ok(())
}
}

async fn invoke(docker: &DockerClient, is_preview: bool) -> CliResult<()> {
let missing_containers = docker.missing_containers().await?;
async fn invoke(app: &AppState, is_preview: bool) -> CliResult<()> {
let missing_containers = app.container_engine().missing_containers().await?;
if missing_containers.is_empty() {
println!("All containers downloaded\n");
return Ok(());
Expand All @@ -30,7 +29,7 @@ async fn invoke(docker: &DockerClient, is_preview: bool) -> CliResult<()> {
}

println!("Downloading the containers required to run System Initiative");
docker
app.container_engine()
.download_missing_containers(missing_containers)
.await?;

Expand Down
11 changes: 5 additions & 6 deletions lib/si-cli/src/cmd/restart.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use crate::containers::DockerClient;
use crate::key_management::get_user_email;
use crate::state::AppState;
use crate::CliResult;

impl AppState {
pub async fn restart(&self, docker: &DockerClient) -> CliResult<()> {
pub async fn restart(&self) -> CliResult<()> {
self.track(
get_user_email().await?,
serde_json::json!({"command-name": "restart-system"}),
);
invoke(self, docker).await?;
invoke(self).await?;
Ok(())
}
}

async fn invoke(app: &AppState, docker: &DockerClient) -> CliResult<()> {
app.stop(docker).await?;
app.start(docker).await?;
async fn invoke(app: &AppState) -> CliResult<()> {
app.stop().await?;
app.start().await?;

Ok(())
}
Loading

0 comments on commit 6faf6e6

Please sign in to comment.