Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time stamp to logs #40

Merged
merged 12 commits into from
Jun 8, 2020
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ FROM docker

ENV SLEEP_TIME='5m'
ENV FILTER_SERVICES=''
ENV TZ='US/Eastern'
ENV VERBOSE='true'

RUN apk add --update --no-cache bash curl
RUN apk add --update --no-cache bash curl tzdata

COPY shepherd /usr/local/bin/shepherd

Expand Down
6 changes: 5 additions & 1 deletion docker-compose.apprise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ services:
image: mazzolino/shepherd
environment:
APPRISE_SIDECAR_URL: notify:5000
TZ: 'US/Eastern'
SLEEP_TIME: '5m'
FILTER_SERVICES: ''
VERBOSE: 'true'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- notification
deploy:
placement:
constraints:
- node.role == manager
- node.role == manager

notify:
image: mazzolino/apprise-microservice:0.1
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ services:
placement:
constraints:
- node.role == manager
environment:
TZ: 'US/Eastern'
SLEEP_TIME: '5m'
FILTER_SERVICES: ''
VERBOSE: 'true'
39 changes: 26 additions & 13 deletions shepherd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ server_version() {
docker version -f "{{.Server.Version}}"
}

logger() {
local log="$1"
local is_verbose_log="${2:-false}"

if [[ "$is_verbose_log" == "true" && "$verbose" == "true" || "$is_verbose_log" == "false" ]]; then
echo "$(date) $log"
fi
}

update_services() {
local blacklist="$1"
local supports_detach_option=$2
Expand All @@ -30,20 +39,21 @@ update_services() {
image=$(echo "$image_with_digest" | cut -d@ -f1)

if ! DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $insecure_registry_flag $image > /dev/null; then
echo "Error updating service $name! Image $image does not exist or it is not available"
logger "Error updating service $name! Image $image does not exist or it is not available"
else
echo "Trying to update service $name with image $image"
logger "Trying to update service $name with image $image" "true"

docker service update "$name" $detach_option $registry_auth $no_resolve_image_flag --image="$image" > /dev/null

previousImage=$(docker service inspect "$name" -f '{{.PreviousSpec.TaskTemplate.ContainerSpec.Image}}')
currentImage=$(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')
if [ "$previousImage" == "$currentImage" ]; then
echo "No updates to service $name!"
logger "No updates to service $name!" "true"
else
echo "Service $name was updated!"
logger "Service $name was updated!"
if [[ "$apprise_sidecar_url" != "" ]]; then
title="[Shepherd] Service $name updated"
body="Service $name was updated from $previousImage to $currentImage"
body="$(date) Service $name was updated from $previousImage to $currentImage"
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"$title\", \"body\": \"$body\"}" "$apprise_sidecar_url"
fi
fi
Expand All @@ -53,39 +63,42 @@ update_services() {
}

main() {
local blacklist sleep_time supports_detach_option supports_registry_auth
local blacklist sleep_time supports_detach_option supports_registry_auth tz verbose
blacklist="${BLACKLIST_SERVICES:-}"
sleep_time="${SLEEP_TIME:-5m}"
verbose="${VERBOSE:-true}"

logger "Timezone set to $TZ"

supports_detach_option=false
if [[ "$(server_version)" > "17.05" ]]; then
supports_detach_option=true
echo "Enabling synchronous service updates"
logger "Enabling synchronous service updates"
fi

supports_registry_auth=false
if [[ ${WITH_REGISTRY_AUTH+x} ]]; then
supports_registry_auth=true
echo "Send registry authentication details to swarm agents"
logger "Send registry authentication details to swarm agents"
fi

supports_insecure_registry=false
if [[ ${WITH_INSECURE_REGISTRY+x} ]]; then
supports_insecure_registry=true
echo "Connection to insecure registry available"
logger "Connection to insecure registry available"
fi

supports_no_resolve_image=false
if [[ ${WITH_NO_RESOLVE_IMAGE+x} ]]; then
supports_no_resolve_image=true
echo "Deployment without resolving image"
logger "Deployment without resolving image"
fi

[[ "$blacklist" != "" ]] && echo "Excluding services: $blacklist"
[[ "$blacklist" != "" ]] && logger "Excluding services: $blacklist"

while true; do
update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth" "$supports_insecure_registry" "$supports_no_resolve_image"
echo "Sleeping $sleep_time before next update"
logger "Sleeping $sleep_time before next update" "true"
sleep "$sleep_time"
done
}
Expand Down