#!/usr/bin/bash # # This binary is made to be run by root, it expects a request from git server, deploy (install or update) # the related tpm package and send a response to git server. # Exit immediately if any command fails. set -e # Exit with the last non-zero fail code. set -o pipefail log() { echo -e "[DEPLOYER] $(date --rfc-3339=s) - $1" } fail () { if [ $# -eq 1 ]; then echo "$1" 1>&2 fi exit 1 } # Load config file. [ $# -eq 1 ] || fail "Expecting 1 argument: config file." source "$1" # Check variables in config file. [ -d "$REQUEST_DIR" ] || fail "Directory does not exist REQUEST_DIR=$REQUEST_DIR in config file $1." [ -d "$RESPONSE_DIR" ] || fail "Directory does not exist RESPONSE_DIR=$RESPONSE_DIR in config file $1." [ -d "$REPOS_DIR" ] || fail "Directory does not exist REPOS_DIR=$REPOS_DIR in config file $1." [ -d "$RPMS_DIR" ] || fail "Directory does not exist RPMS_DIR=$RPMS_DIR in config file $1." [ -z "$RPM_ARCH" ] && fail "Empty value RPM_ARCH in config file $1." [ -z "$RPM_RELEASE" ] && fail "Empty value RPM_RELEASE in config file $1." [ -z "$RPM_DIST" ] && fail "Empty value RPM_DIST in config file $1." # First remove eventual old existing tmp files. find "$REQUEST_DIR" -type f -delete find "$RESPONSE_DIR" -type f -delete # Loop on every created request. while read _ _ repo_name; do log "New request detected for repo $repo_name." # Read request file and remove it immediately. repo_version=$(cat "$REQUEST_DIR/$repo_name") rm "$REQUEST_DIR/$repo_name" # Check repo version not empty. if [ -z "$repo_version" ]; then echo -e "Content of $REQUEST_DIR/$repo_name must contain repo version but is empty\n1" > "$RESPONSE_DIR/$repo_name" continue fi # Check if repo does exist. if [ ! -d "$REPOS_DIR/$repo_name.git" ]; then echo -e "Repository $REPOS_DIR/$repo_name.git does not exist!\n1" > "$RESPONSE_DIR/$repo_name" continue fi # Check if repo package is already exisitng. rpm_path="$RPMS_DIR/$RPM_ARCH/$repo_name-$repo_version-$RPM_RELEASE.$RPM_DIST.$RPM_ARCH.rpm" log "Using rpm package at $rpm_path." if [ ! -f "$rpm_path" ]; then echo -e "RPM package $rpm_path does not exist!\n1" > "$RESPONSE_DIR/$repo_name" continue fi # Upgrade package if already installed. if rpm -q "$repo_name" 1>/dev/null 2>/dev/null; then log "Upgrade package $repo_name to v$repo_version" output=$(rpm --upgrade --verbose --hash "$rpm_path" 2>&1) || exit_code=$? echo -e "$output\n$exit_code" > "$RESPONSE_DIR/$repo_name" continue fi # Install package if not already installed. log "Install package $repo_name v$repo_version." output=$(rpm --install --verbose --hash "$rpm_path" 2>&1) || exit_code=$? echo -e "$output\n$exit_code" > "$RESPONSE_DIR/$repo_name" done < <(inotifywait --monitor --event create "$REQUEST_DIR")