From dcf8a7c6f0c00bbb5ddd3e21ecc9d9d29a9946b7 Mon Sep 17 00:00:00 2001 From: Aleksei Ivanov Date: Tue, 20 Apr 2021 05:22:59 +0300 Subject: [PATCH] Add upstream check TODO: Fix duplicate messages --- entrypoint.sh | 13 ++++++++--- includes/check-upstream.sh | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 includes/check-upstream.sh diff --git a/entrypoint.sh b/entrypoint.sh index 0cbbf9a..fe03b89 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -23,9 +23,12 @@ export DIR_NAME=.docker-blueprint export ROOT_DIR=~/.docker-blueprint export LOCAL_DIR="$PROJECT_DIR/$DIR_NAME" export TEMP_DIR="$LOCAL_DIR/tmp" +export CACHE_DIR="$ROOT_DIR/cache" export ENTRYPOINT="$ROOT_DIR/entrypoint.sh" export PROJECT_BLUEPRINT_FILE=docker-blueprint.yml +mkdir -p "$CACHE_DIR" + # Delete temporary files older than 5 minutes mkdir -p "$TEMP_DIR" find "$TEMP_DIR" -mindepth 1 -type f -mmin +5 -delete @@ -99,6 +102,7 @@ if [[ -f "$ROOT_DIR/commands/$1.sh" ]]; then fi source "$ROOT_DIR/commands/$1.sh" + source "$ROOT_DIR/includes/check-upstream.sh" exit fi @@ -106,6 +110,7 @@ MODE_NO_TTY=false if [[ $# -eq 0 ]]; then source "$ROOT_DIR/commands/help.sh" + source "$ROOT_DIR/includes/check-upstream.sh" exit fi @@ -113,18 +118,18 @@ while [[ "$#" -gt 0 ]]; do case $1 in start | stop | restart | down) $DOCKER_COMPOSE "$1" ${@:2} - exit + break ;; -h | --help) source "$ROOT_DIR/commands/help.sh" - exit + break ;; -v | --version) AS_FUNCTION=false source "$ROOT_DIR/commands/version.sh" - exit + break ;; -T) @@ -197,3 +202,5 @@ while [[ "$#" -gt 0 ]]; do shift done + +source "$ROOT_DIR/includes/check-upstream.sh" diff --git a/includes/check-upstream.sh b/includes/check-upstream.sh new file mode 100644 index 0000000..755cd7c --- /dev/null +++ b/includes/check-upstream.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +if [[ -z "$CHECK_UPSTREAM_WAS_CALLED" ]]; then + + export CHECK_UPSTREAM_WAS_CALLED=1 + + new_version_available_flag_file="$CACHE_DIR/.new-version-available" + update_check_timestamp_file="$CACHE_DIR/.update-check-timestamp" + update_check_interval=86400 # One day in seconds + + current_timestamp="$(date +%s)" + previous_timestamp=0 + + if [[ -f "$update_check_timestamp_file" ]]; then + previous_timestamp="$(cat "$update_check_timestamp_file")" + fi + + timestamp_delta=$(($current_timestamp - $previous_timestamp)) + + if [[ "$timestamp_delta" -gt "$update_check_interval" ]]; then + echo "$current_timestamp" >"$update_check_timestamp_file" + + PREVIOUS_DIR=$PWD + + cd $ROOT_DIR + git remote update >/dev/null + git status -uno | grep 'branch is behind' >/dev/null + cd $PREVIOUS_DIR + + if [[ $? > 0 ]]; then + rm -f "$new_version_available_flag_file" + exit # No new commits + else + touch "$new_version_available_flag_file" + fi + fi + + if [[ -f "$new_version_available_flag_file" ]]; then + printf "\n" + printf "New version is available!\n" + printf "Install with ${EXE_COL}docker-blueprint${RESET} ${CMD_COL}update${RESET}\n" + printf "\n" + fi + +fi