add dockerfiles and add version tags to docker builds

This commit is contained in:
beckerinj
2022-11-20 22:24:59 -05:00
parent abc1ad4356
commit 56269b2fcf
9 changed files with 105 additions and 56 deletions

2
.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
/target
/config_example

100
Cargo.lock generated
View File

@@ -348,6 +348,35 @@ dependencies = [
"clap",
]
[[package]]
name = "core"
version = "0.1.0"
dependencies = [
"anyhow",
"async_timing_util",
"axum",
"axum-extra",
"bcrypt",
"db_client",
"dotenv",
"envy",
"helpers",
"hmac",
"jwt",
"mungos",
"oauth2",
"periphery_client",
"serde",
"serde_derive",
"serde_json",
"sha2",
"slack_client_rs",
"tokio",
"tower",
"tower-http",
"types",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@@ -1205,56 +1234,6 @@ dependencies = [
"zstd",
]
[[package]]
name = "monitor-core"
version = "0.1.0"
dependencies = [
"anyhow",
"async_timing_util",
"axum",
"axum-extra",
"bcrypt",
"db_client",
"dotenv",
"envy",
"helpers",
"hmac",
"jwt",
"mungos",
"oauth2",
"periphery_client",
"serde",
"serde_derive",
"serde_json",
"sha2",
"slack_client_rs",
"tokio",
"tower",
"tower-http",
"types",
]
[[package]]
name = "monitor-periphery"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"bollard",
"dotenv",
"envy",
"helpers",
"run_command",
"serde",
"serde_derive",
"serde_json",
"sysinfo",
"tokio",
"toml",
"tower",
"types",
]
[[package]]
name = "mungos"
version = "0.2.24"
@@ -1459,6 +1438,27 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "periphery"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"bollard",
"dotenv",
"envy",
"helpers",
"run_command",
"serde",
"serde_derive",
"serde_json",
"sysinfo",
"tokio",
"toml",
"tower",
"types",
]
[[package]]
name = "periphery_client"
version = "0.1.0"

11
Dockerfile.core Normal file
View File

@@ -0,0 +1,11 @@
FROM rust:latest as builder
WORKDIR /core
COPY . .
RUN cargo build -p core --release
FROM gcr.io/distroless/cc
COPY --from=builder /core/target/release/core /
CMD ["./core"]

13
Dockerfile.periphery Normal file
View File

@@ -0,0 +1,13 @@
FROM rust:latest as builder
WORKDIR /periphery
COPY . .
RUN cargo build -p periphery --release
FROM debian:stable-slim
# install git and docker
COPY --from=builder /periphery/target/release/periphery /usr/local/bin/periphery
CMD "periphery"

View File

@@ -1,5 +1,5 @@
[package]
name = "monitor-core"
name = "core"
version = "0.1.0"
edition = "2021"

View File

@@ -2,7 +2,7 @@ use std::{path::PathBuf, str::FromStr};
use anyhow::{anyhow, Context};
use run_command::async_run_command;
use types::{Build, DockerBuildArgs, Log};
use types::{Build, DockerBuildArgs, Log, Version};
use crate::{git, run_monitor_command, to_monitor_name};
@@ -52,13 +52,14 @@ pub async fn build(
None => "Dockerfile".to_owned(),
};
let image_name = get_image_name(docker_account, &name);
let image_tags = image_tags(&image_name, &version);
let docker_push = if docker_account_pw.is_some() {
format!(" && docker push {image_name}")
format!(" && docker image push --all-tags {image_name}")
} else {
String::new()
};
let command =
format!("cd {cd} && docker build -t {image_name} -f {dockerfile_path} .{docker_push}");
format!("cd {cd} && docker build {image_tags} -f {dockerfile_path} .{docker_push}");
let build_log = run_monitor_command("docker build", command).await;
Ok(vec![pull_log, build_log])
}
@@ -84,3 +85,19 @@ fn get_image_name(docker_account: &Option<String>, name: &str) -> String {
None => name.to_string(),
}
}
fn get_version_image_name(image_name: &str, version: &Version) -> String {
format!("{image_name}:{}", version.to_string())
}
fn get_latest_image_name(image_name: &str) -> String {
format!("{image_name}:latest")
}
fn image_tags(image_name: &str, version: &Version) -> String {
format!(
"-t {} -t {}",
get_version_image_name(image_name, version),
get_latest_image_name(image_name)
)
}

View File

@@ -46,7 +46,7 @@ pub async fn pull(path: &str, branch: &Option<String>) -> Log {
Some(branch) => branch.to_owned(),
None => "main".to_string(),
};
let command = format!("cd path && git pull origin {branch}");
let command = format!("cd {path} && git pull origin {branch}");
run_monitor_command("git pull", command).await
}

View File

@@ -222,6 +222,12 @@ pub struct Version {
pub minor: u64,
}
impl ToString for Version {
fn to_string(&self) -> String {
format!("{}.{}", self.major, self.minor)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Conversion {
pub local: String,

View File

@@ -1,5 +1,5 @@
[package]
name = "monitor-periphery"
name = "periphery"
version = "0.1.0"
edition = "2021"