|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +### BEGIN INIT INFO |
| 4 | +# Provides: docker-registry |
| 5 | +# Required-Start: $network $remote_fs $syslog |
| 6 | +# Required-Stop: $network $remote_fs $syslog |
| 7 | +# Default-Start: 2 3 4 5 |
| 8 | +# Default-Stop: 0 1 6 |
| 9 | +# Short-Description: Start Docker-Registry |
| 10 | +### END INIT INFO |
| 11 | + |
| 12 | +# Author: Anastas Semenov <anapsix@random.io> |
| 13 | +# Updated by: |
| 14 | + |
| 15 | +PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin |
| 16 | +. /lib/lsb/init-functions |
| 17 | + |
| 18 | +# check is we are running as root |
| 19 | +[ $(id -u) -ne 0 ] && echo "must run as root, exiting" && exit 1 |
| 20 | + |
| 21 | +# a way to figure out a service home directory, no matter where it's called from |
| 22 | +self_path="$(readlink -e $0)" |
| 23 | +self_dir="${self_path%%/${self_path##*/}}" |
| 24 | + |
| 25 | +# load config if present/readable |
| 26 | +[ -r /etc/default/docker-registry ] && . /etc/default/docker-registry |
| 27 | + |
| 28 | +# MAKE SURE THIS PATH IS CORRECT |
| 29 | +# it will default to repository location |
| 30 | +# which will work if you are calling directly from repo path or even symlink to it |
| 31 | +#DOCKER_REGISTRY_HOME="" # path to docker-registry codebase |
| 32 | + |
| 33 | +# set defaults if they are not set by config |
| 34 | +[[ -z "$RUNAS" ]] && RUNAS=$(stat --format "%U" $self_path) # defaults to user owning this init script |
| 35 | +[[ -z "$LOGFILE" ]] && LOGFILE=/var/log/docker-registry.log # will be chowned to $RUNAS |
| 36 | +[[ -z "$PIDFILE" ]] && PIDFILE=/var/run/docker-registry/docker-registry.pid # path will created and chowned to $RUNAS |
| 37 | +[[ -z "$LISTEN_IP" ]] && LISTEN_IP="0.0.0.0" |
| 38 | +[[ -z "$LISTEN_PORT" ]] && LISTEN_PORT=5000 |
| 39 | +[[ -z "$GUNICORN_WORKERS" ]] && GUNICORN_WORKERS=2 |
| 40 | +[[ -z "$DOCKER_REGISTRY_HOME" ]] && DOCKER_REGISTRY_HOME=${self_dir%/*} |
| 41 | + |
| 42 | +NAME="Docker Registry" |
| 43 | +DAEMON="/usr/local/bin/gunicorn" |
| 44 | +DAEMON_OPTS="-D --access-logfile ${LOGFILE} --pid ${PIDFILE} --max-requests 500 --graceful-timeout 3600 -t 3600 -k gevent -b ${LISTEN_IP}:${LISTEN_PORT} -w ${GUNICORN_WORKERS:-2} wsgi:application" |
| 45 | + |
| 46 | +RED='\e[0;31m' |
| 47 | +GREEN='\e[0;32m' |
| 48 | +YELLOW='\e[0;33m' |
| 49 | +PURPLE='\e[0;35m' |
| 50 | +NC='\e[0m' |
| 51 | + |
| 52 | +if [[ -z "${DOCKER_REGISTRY_HOME}" ]]; then |
| 53 | + echo "DOCKER_REGISTRY_HOME is not set, update this \"$(readlink -e $0)\" script or set it in /etc/default/docker-registry , exiting.." |
| 54 | + exit 1 |
| 55 | +else |
| 56 | + cd $DOCKER_REGISTRY_HOME |
| 57 | +fi |
| 58 | + |
| 59 | +start_server() { |
| 60 | + [ -d ${PIDFILE%/*} ] || mkdir -p ${PIDFILE%/*} || return 1 |
| 61 | + chown -R $RUNAS ${PIDFILE%/*} || return 1 |
| 62 | + touch $LOGFILE && chown $RUNAS $LOGFILE || return 1 |
| 63 | + start-stop-daemon --start --chuid $RUNAS --chdir $DOCKER_REGISTRY_HOME --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS |
| 64 | + return $? |
| 65 | +} |
| 66 | + |
| 67 | +stop_server() { |
| 68 | + start-stop-daemon --stop --oknodo --user $RUNAS --name ${DAEMON##*/} --pidfile $PIDFILE --retry 5 1>/dev/null |
| 69 | + return $? |
| 70 | +# kill $(cat $PIDFILE) |
| 71 | +# RETVAL=$? |
| 72 | +# rm -f $PIDFILE |
| 73 | +# return $RETVAL |
| 74 | +} |
| 75 | + |
| 76 | +reload_server() { |
| 77 | + kill -HUP $(cat $PIDFILE) |
| 78 | +} |
| 79 | + |
| 80 | +status_server() { |
| 81 | + if [ ! -r $PIDFILE ]; then |
| 82 | + return 1 |
| 83 | + elif ( kill -0 $(cat $PIDFILE) 2>/dev/null); then |
| 84 | + return 0 |
| 85 | + else |
| 86 | + rm -f $PIDFILE |
| 87 | + return 1 |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +case $1 in |
| 92 | + start) |
| 93 | + if status_server; then |
| 94 | + /bin/echo -e "${NAME} is ${GREEN}already running${NC}" >&2 |
| 95 | + exit 0 |
| 96 | + else |
| 97 | + log_daemon_msg "Starting ${NAME}" "on port ${LISTEN_IP}:${LISTEN_PORT}" |
| 98 | + start_server |
| 99 | + sleep 1 |
| 100 | + status_server |
| 101 | + log_end_msg $? |
| 102 | + fi |
| 103 | + ;; |
| 104 | + stop) |
| 105 | + if status_server; then |
| 106 | + log_daemon_msg "Stopping ${NAME}" # "on port ${LISTEN_IP}:${LISTEN_PORT}" |
| 107 | + stop_server |
| 108 | + log_end_msg $? |
| 109 | + else |
| 110 | + log_daemon_msg "${NAME}" "is not running" |
| 111 | + log_end_msg 0 |
| 112 | + fi |
| 113 | + ;; |
| 114 | + restart) |
| 115 | + log_daemon_msg "Restarting ${NAME}" # "on port ${LISTEN_IP}:${LISTEN_PORT}" |
| 116 | + if status_server; then |
| 117 | + stop_server && sleep 1 || log_end_msg $? |
| 118 | + fi |
| 119 | + start_server |
| 120 | + log_end_msg $? |
| 121 | + ;; |
| 122 | + reload) |
| 123 | + if status_server; then |
| 124 | + log_daemon_msg "Reloading ${NAME}" # "on port ${LISTEN_IP}:${LISTEN_PORT}" |
| 125 | + reload_server |
| 126 | + log_end_msg $? |
| 127 | + else |
| 128 | + log_daemon_msg "${NAME}" "is not running" |
| 129 | + log_end_msg 1 |
| 130 | + fi |
| 131 | + ;; |
| 132 | + status) |
| 133 | + /bin/echo -en "${NAME} is.. " >&2 |
| 134 | + if status_server; then |
| 135 | + /bin/echo -e "${GREEN}running${NC}" >&2 |
| 136 | + exit 0 |
| 137 | + else |
| 138 | + /bin/echo -e "${RED}not running${NC}" >&2 |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | + ;; |
| 142 | + *) |
| 143 | + echo "Usage: $0 {start|stop|restart|reload|status}" >&2 |
| 144 | + exit 2 |
| 145 | + ;; |
| 146 | +esac |
| 147 | + |
| 148 | +exit 0 |
0 commit comments