Created
May 22, 2017 20:17
-
-
Save ksylvan/9843d1bf41460f98ef75ce8a950c0659 to your computer and use it in GitHub Desktop.
Gitlab runner for VM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
#IMAGE=gitlab/gitlab-ce:previous | |
IMAGE=gitlab/gitlab-ce | |
NAME=gitlab | |
PORTS="-p 443:443 -p 80:80 -p 22:22" | |
G_CONFIG=/etc/gitlab | |
G_OPT=/var/opt/gitlab | |
G_LOG=/var/log/gitlab | |
POSTFIX_CONFIG=/etc/postfix | |
VOLUMES="-v $G_CONFIG:$G_CONFIG -v $G_OPT:$G_OPT -v $G_LOG:$G_LOG" | |
BACKUP_REMOTE=/root/box.net | |
MY_LOG=$G_LOG/docker.log | |
if [ -n "$CRON" ] | |
then | |
exec >> $MY_LOG 2>&1 | |
fi | |
kill_it() | |
{ | |
echo "$(date): Killing $NAME container" | |
docker rm -f $NAME > /dev/null 2>&1 | |
} | |
run_it() | |
{ | |
echo "$(date): Starting $NAME container" | |
docker run -d --privileged --name $NAME $VOLUMES $PORTS $IMAGE | |
} | |
back_it_up() | |
{ | |
echo "$(date): Backing up $NAME" | |
if [ -n "$CRON" ] | |
then | |
docker exec $NAME gitlab-rake gitlab:backup:create RAILS_ENV=production CRON=1 | |
else | |
docker exec -it $NAME gitlab-rake gitlab:backup:create RAILS_ENV=production | |
fi | |
gzip -v -9 $(find $G_OPT/backups -type f -a ! -name '*.gz') | |
} | |
remote_copy() | |
{ | |
echo "$(date): Backing up to $BACKUP_REMOTE" | |
if [ ! -r $BACKUP_REMOTE/gitlab/last_backup.txt ] | |
then | |
mount $BACKUP_REMOTE | |
if [ ! -r $BACKUP_REMOTE/gitlab/last_backup.txt ] | |
then | |
echo "ERROR: No $BACKUP_REMOTE/gitlab/last_backup.txt file present." | |
exit 1 | |
fi | |
fi | |
cp -v $(find $G_OPT/backups -type f -newer $BACKUP_REMOTE/gitlab/last_backup.txt) $BACKUP_REMOTE/gitlab | |
date > $BACKUP_REMOTE/gitlab/last_backup.txt | |
} | |
config_backup() | |
{ | |
echo "$(date): Backing up configs for $NAME" | |
tar -czf $G_OPT/backups/etc-gitlab.tar.gz $G_CONFIG $POSTFIX_CONFIG | |
} | |
upgrade() | |
{ | |
echo "$(date): Upgrade of GitLab docker image $IMAGE" | |
docker pull $IMAGE | |
} | |
case "$1" in | |
start|restart) | |
kill_it | |
run_it | |
;; | |
stop) | |
kill_it | |
;; | |
backup) | |
back_it_up | |
remote_copy | |
;; | |
config_backup) | |
config_backup | |
remote_copy | |
;; | |
upgrade) | |
upgrade | |
;; | |
sh) | |
docker exec -it $NAME bash | |
;; | |
logs) | |
docker logs -f $NAME | |
;; | |
renewed) | |
cp /etc/letsencrypt/live/gitlab.sylvan.com/fullchain.pem $G_CONFIG/ssl/gitlab.sylvan.com.crt | |
cp /etc/letsencrypt/live/gitlab.sylvan.com/privkey.pem $G_CONFIG/ssl/gitlab.sylvan.com.key | |
run_it | |
;; | |
certs) | |
/usr/local/bin/certbot-auto renew --pre-hook "$0 stop" \ | |
--post-hook "$0 renewed" --quiet --no-self-upgrade | |
;; | |
*) | |
echo "Usage: $0 [start | stop | restart | backup | config_backup | upgrade | sh | logs]" | |
echo " start .............. Start GitLab." | |
echo " stop .............. Stop GitLab." | |
echo " restart ............ Stop GitLab." | |
echo " backup ............. backup GitLab DB." | |
echo " config_backup ...... backup GitLab configs." | |
echo " upgrade ............ Upgrade to latest GitLab." | |
echo " sh ................. Interactive shell inside the container." | |
echo " logs ............... Logs from the container." | |
echo " certs .............. Renew certbot.eff.org certs if needed." | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment