From a1472194d2ea552f06a4e2028bb8aa142fa55298 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 14 Aug 2022 21:06:07 +0200 Subject: [PATCH] fixed cuddle image --- cuddle.yaml | 18 +++++++++++++++ cuddle_cli/src/cli/mod.rs | 17 +++++++++++--- cuddle_cli/src/config.rs | 2 +- cuddle_cli/src/main.rs | 2 +- scripts/build_cuddle_image.sh | 16 +++++++++++++ templates/build_cuddle_image.Dockerfile | 23 +++++++++++++++++++ ...build_cuddle_image.Dockerfile.dockerignore | 2 ++ 7 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 cuddle.yaml create mode 100755 scripts/build_cuddle_image.sh create mode 100644 templates/build_cuddle_image.Dockerfile create mode 100644 templates/build_cuddle_image.Dockerfile.dockerignore diff --git a/cuddle.yaml b/cuddle.yaml new file mode 100644 index 0000000..78f6368 --- /dev/null +++ b/cuddle.yaml @@ -0,0 +1,18 @@ +# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json + +base: "git@git.front.kjuulh.io:kjuulh/cuddle-base.git" + +vars: + service: "cuddle-image" + registry: kasperhermansen + +scripts: + build_cuddle_image: + type: shell + args: + docker_username: + key: "DOCKER_USERNAME" + type: env + docker_password: + key: "DOCKER_PASSWORD" + type: env diff --git a/cuddle_cli/src/cli/mod.rs b/cuddle_cli/src/cli/mod.rs index a780de9..c5ed7a9 100644 --- a/cuddle_cli/src/cli/mod.rs +++ b/cuddle_cli/src/cli/mod.rs @@ -9,6 +9,7 @@ use clap::Command; use crate::{ actions::CuddleAction, + config::{CuddleConfig, CuddleFetchPolicy}, context::{CuddleContext, CuddleTreeType}, model::*, util::git::GitCommit, @@ -23,16 +24,21 @@ pub struct CuddleCli<'a> { context: Arc>>, command: Option>, tmp_dir: Option, + config: CuddleConfig, } impl<'a> CuddleCli<'a> { - pub fn new(context: Arc>>) -> anyhow::Result> { + pub fn new( + context: Arc>>, + config: CuddleConfig, + ) -> anyhow::Result> { let mut cli = CuddleCli { scripts: vec![], variables: vec![], context: context.clone(), command: None, tmp_dir: None, + config, }; cli = cli @@ -106,8 +112,13 @@ impl<'a> CuddleCli<'a> { .tmp_dir .clone() .ok_or(anyhow::anyhow!("tmp_dir does not exist aborting"))?; - if tmp_dir.exists() && tmp_dir.ends_with("tmp") { - std::fs::remove_dir_all(tmp_dir.clone())?; + match self.config.get_fetch_policy()? { + CuddleFetchPolicy::Always => { + if tmp_dir.exists() && tmp_dir.ends_with("tmp") { + std::fs::remove_dir_all(tmp_dir.clone())?; + } + } + _ => {} } std::fs::create_dir_all(tmp_dir.clone())?; // Handle all templating with variables and such. diff --git a/cuddle_cli/src/config.rs b/cuddle_cli/src/config.rs index 8a2325a..b67f56d 100644 --- a/cuddle_cli/src/config.rs +++ b/cuddle_cli/src/config.rs @@ -6,7 +6,7 @@ pub enum CuddleFetchPolicy { Never, } -#[derive(Envconfig, Clone)] +#[derive(Envconfig, Clone, Debug)] pub struct CuddleConfig { #[envconfig(from = "CUDDLE_FETCH_POLICY", default = "once")] fetch_policy: String, diff --git a/cuddle_cli/src/main.rs b/cuddle_cli/src/main.rs index e410620..394ec9e 100644 --- a/cuddle_cli/src/main.rs +++ b/cuddle_cli/src/main.rs @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> { let config = CuddleConfig::from_env()?; let context = context::extract_cuddle(config.clone())?; - _ = cli::CuddleCli::new(context)?.execute()?; + _ = cli::CuddleCli::new(context, config)?.execute()?; Ok(()) } diff --git a/scripts/build_cuddle_image.sh b/scripts/build_cuddle_image.sh new file mode 100755 index 0000000..4ee050b --- /dev/null +++ b/scripts/build_cuddle_image.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +tag="$REGISTRY/$SERVICE:${COMMIT_SHA:0:10}" +latest_tag="$REGISTRY/$SERVICE:latest" + +echo "logging in" +docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD + +echo "building image" +DOCKER_BUILDKIT=1 docker build -t "$tag" -t "$latest_tag" -f "$TMP/build_cuddle_image.Dockerfile" . + +echo "pushing image" +docker push "$tag" +docker push "$latest_tag" diff --git a/templates/build_cuddle_image.Dockerfile b/templates/build_cuddle_image.Dockerfile new file mode 100644 index 0000000..d43f04e --- /dev/null +++ b/templates/build_cuddle_image.Dockerfile @@ -0,0 +1,23 @@ +FROM rust:1.62.1-slim-bullseye as base + +RUN rustup target add x86_64-unknown-linux-musl + +RUN apt-get update -qq && \ + apt-get install -y \ + musl-tools \ + musl-dev + +RUN update-ca-certificates + +RUN apt-get update && apt-get upgrade -y +RUN apt-get install -y -q build-essential curl git + +WORKDIR /app/cuddle/ + +RUN cargo install --target x86_64-unknown-linux-musl --git https://git.front.kjuulh.io/kjuulh/cuddle.git cuddle_cli + +FROM docker:stable-dind + +COPY --from=base /usr/local/cargo/bin/ /usr/local/cargo/bin/ +ENV PATH="${PATH}:/usr/local/cargo/bin" + diff --git a/templates/build_cuddle_image.Dockerfile.dockerignore b/templates/build_cuddle_image.Dockerfile.dockerignore new file mode 100644 index 0000000..3852fe2 --- /dev/null +++ b/templates/build_cuddle_image.Dockerfile.dockerignore @@ -0,0 +1,2 @@ +.cuddle/ +.git/