diff --git a/.gitignore b/.gitignore index fedaa2b..de0a023 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ /target +/rpmbuild .env +deb/usr/local/bin/compose-watcher +rpm/usr/local/bin/compose-watcher +*.deb +*.rpm diff --git a/Cargo.lock b/Cargo.lock index 7048e39..0588719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,7 +134,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" [[package]] -name = "contpose" +name = "compose-watcher" version = "0.1.0" dependencies = [ "base64", diff --git a/Cargo.toml b/Cargo.toml index 8de2055..c80c334 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "contpose" +name = "compose-watcher" version = "0.1.0" edition = "2021" license = "MIT" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..737f569 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +.PHONY: build-deb build-rpm + +WORK_DIR=$(shell pwd) +TARGET_BIN=target/x86_64-unknown-linux-musl/release/compose-watcher +USR_BIN_DEB=deb/usr/local/bin/compose-watcher +USR_BIN_RPM=rpm/usr/local/bin/compose-watcher + +$(TARGET_BIN): + CARGO_TARGET_DIR="./target" cargo build --release --target "x86_64-unknown-linux-musl" + +$(USR_BIN_RPM): $(TARGET_BIN) + mkdir -p deb/usr/local/bin/ + mv $(TARGET_BIN) $(USR_BIN_RPM) + +$(USR_BIN_DEB): $(TARGET_BIN) + mkdir -p deb/usr/local/bin/ + mv $(TARGET_BIN) $(USR_BIN_DEB) + +build-deb: $(USR_BIN_DEB) + dpkg-deb --build deb + mv deb.deb compose-watcher.deb + +build-rpm: $(USR_BIN_RPM) + cp -r rpm/ rpmbuild + mkdir -p rpmbuild/opt/compose-watcher + rpmbuild --target=x86_64 --buildroot $(WORK_DIR)/rpmbuild \ + -bb rpmbuild/compose-watcher.spec diff --git a/deb/DEBIAN/control b/deb/DEBIAN/control new file mode 100644 index 0000000..891eda6 --- /dev/null +++ b/deb/DEBIAN/control @@ -0,0 +1,6 @@ +Package: compose-watcher +Version: 0.1 +Maintainer: ixpantia S.A. +Architecture: amd64 +Description: Continously Deploy services with Docker Compose +Depends: docker-ce, docker-ce-cli, containerd.io, docker-buildx-plugin, docker-compose-plugin diff --git a/deb/DEBIAN/install b/deb/DEBIAN/install new file mode 100644 index 0000000..e69de29 diff --git a/deb/DEBIAN/postinst b/deb/DEBIAN/postinst new file mode 100755 index 0000000..b11781d --- /dev/null +++ b/deb/DEBIAN/postinst @@ -0,0 +1,22 @@ +#!/bin/sh +set -e + +# Create the 'compose-watcher' user and home directory if it doesn't exist +if ! id -u compose-watcher > /dev/null 2>&1; then + useradd -r -d /opt/compose-watcher -s /bin/false compose-watcher + mkdir -p /opt/compose-watcher + chown compose-watcher:compose-watcher /opt/compose-watcher +fi + +# Create the compose-watcher.toml file if it doesn't exist +if [ ! -f /opt/compose-watcher/compose-watcher.toml ]; then + echo "delay=60 # Will watch for updates every 60 seconds" >> /opt/compose-watcher/compose-watcher.toml + chown compose-watcher:compose-watcher /opt/compose-watcher/compose-watcher.toml +fi + +# Restart the service on upgrade +if [ "$1" = "configure" ]; then + systemctl daemon-reload || true + systemctl enable compose-watcher.service || true + systemctl start compose-watcher.service || true +fi diff --git a/deb/DEBIAN/postrm b/deb/DEBIAN/postrm new file mode 100755 index 0000000..788d107 --- /dev/null +++ b/deb/DEBIAN/postrm @@ -0,0 +1,15 @@ +#!/bin/sh +set -e + +if [ "$1" = "purge" ]; then + systemctl daemon-reload || true + rm -f /lib/systemd/system/compose-watcher.service +fi + +rm -rf /usr/local/bin/compose-watcher + +if [ "$1" = "purge" ]; then + # Remove the compose-watcher user and its home directory + userdel -r compose-watcher || true + rm -rf /opt/compose-watcher +fi diff --git a/deb/DEBIAN/prerm b/deb/DEBIAN/prerm new file mode 100755 index 0000000..a097968 --- /dev/null +++ b/deb/DEBIAN/prerm @@ -0,0 +1,7 @@ +#!/bin/sh +set -e + +if [ "$1" = "remove" ]; then + systemctl stop compose-watcher.service || true + systemctl disable compose-watcher.service || true +fi diff --git a/deb/DEBIAN/rules b/deb/DEBIAN/rules new file mode 100755 index 0000000..65a7eea --- /dev/null +++ b/deb/DEBIAN/rules @@ -0,0 +1,9 @@ +#!/usr/bin/make -f + +%: + dh $@ + +override_dh_installinit: + dh_installinit --restart-after-upgrade + dh_systemd_enable + dh_systemd_start diff --git a/deb/lib/systemd/system/compose-watcher.service b/deb/lib/systemd/system/compose-watcher.service new file mode 100644 index 0000000..5b09e34 --- /dev/null +++ b/deb/lib/systemd/system/compose-watcher.service @@ -0,0 +1,17 @@ +[Unit] +Description=Compose Watcher +After=docker.service +BindsTo=docker.service +StartLimitIntervalSec=0 +[Service] +Type=simple +Restart=always +RestartSec=1 +User=compose-watcher +Environment="RUST_LOG=info" +ExecStart=/usr/local/bin/compose-watcher --config /opt/compose-watcher/compose-watcher.toml +ExecReload=/bin/kill -HUP $MAINPID +WorkingDirectory=/opt/compose-watcher + +[Install] +WantedBy=multi-user.target diff --git a/rpm/compose-watcher.spec b/rpm/compose-watcher.spec new file mode 100644 index 0000000..20d68ad --- /dev/null +++ b/rpm/compose-watcher.spec @@ -0,0 +1,65 @@ +Buildroot: /home/andres/projects/ixpantia/imasd/contpose/compose-watcher-0.1 +Name: compose-watcher +Version: 0.1 +Release: 0 +Summary: Continously Deploy services with Docker Compose +License: see /usr/share/doc/compose-watcher/copyright +Distribution: Debian +Group: Converted/unknown +Requires: docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + +%define _rpmdir ../ +%define _rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm +%define _unpackaged_files_terminate_build 0 + +%post +#!/bin/sh +set -e + +# Create the 'compose-watcher' user and home directory if it doesn't exist +if ! id -u compose-watcher > /dev/null 2>&1; then + useradd -r -d /opt/compose-watcher -s /bin/false compose-watcher + mkdir -p /opt/compose-watcher + chown compose-watcher:compose-watcher /opt/compose-watcher +fi + +# Create the compose-watcher.toml file if it doesn't exist +if [ ! -f /opt/compose-watcher/compose-watcher.toml ]; then + echo "delay=60 # Will watch for updates every 60 seconds" >> /opt/compose-watcher/compose-watcher.toml + chown compose-watcher:compose-watcher /opt/compose-watcher/compose-watcher.toml +fi + +# Restart the service on upgrade +systemctl daemon-reload || true +systemctl enable compose-watcher.service || true +systemctl start compose-watcher.service || true + + +%preun +#!/bin/sh +set -e + +systemctl stop compose-watcher.service || true +systemctl disable compose-watcher.service || true + + +%postun +#!/bin/sh +set -e + +systemctl daemon-reload || true +rm -f /lib/systemd/system/compose-watcher.service + +rm -rf /usr/local/bin/compose-watcher + +# Remove the compose-watcher user and its home directory +userdel -r compose-watcher || true +rm -rf /opt/compose-watcher + + +%description + +%files +%dir "/opt/compose-watcher" +"/lib/systemd/system/compose-watcher.service" +"/usr/local/bin/compose-watcher" diff --git a/rpm/lib/systemd/system/compose-watcher.service b/rpm/lib/systemd/system/compose-watcher.service new file mode 100644 index 0000000..5b09e34 --- /dev/null +++ b/rpm/lib/systemd/system/compose-watcher.service @@ -0,0 +1,17 @@ +[Unit] +Description=Compose Watcher +After=docker.service +BindsTo=docker.service +StartLimitIntervalSec=0 +[Service] +Type=simple +Restart=always +RestartSec=1 +User=compose-watcher +Environment="RUST_LOG=info" +ExecStart=/usr/local/bin/compose-watcher --config /opt/compose-watcher/compose-watcher.toml +ExecReload=/bin/kill -HUP $MAINPID +WorkingDirectory=/opt/compose-watcher + +[Install] +WantedBy=multi-user.target diff --git a/src/cli.rs b/src/cli.rs index 389f5c9..40907e1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,7 +7,7 @@ use clap::Parser; #[command(version, about, long_about = None)] pub struct Args { /// Path to the config file. - #[arg(short, long, default_value = "contpose.toml")] + #[arg(short, long, default_value = "compose-watcher.toml")] pub config: PathBuf, }