From 632461a087d4e77d4aa603a114d2114652a44f97 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Wed, 24 Sep 2014 15:37:27 +0200 Subject: [PATCH] now i need to write the init script --- dev-vcs/gitlab-ci-runner/Manifest | 2 +- .../files/gitlab-ci-runner.init | 139 ++++++++++++++++++ .../files/gitlab-ci-unicorn.init | 67 +++++++++ .../gitlab-ci-runner-5.0.0.ebuild | 69 +-------- 4 files changed, 213 insertions(+), 64 deletions(-) create mode 100644 dev-vcs/gitlab-ci-runner/files/gitlab-ci-runner.init create mode 100755 dev-vcs/gitlab-ci-runner/files/gitlab-ci-unicorn.init diff --git a/dev-vcs/gitlab-ci-runner/Manifest b/dev-vcs/gitlab-ci-runner/Manifest index b1d4db6..d192b93 100644 --- a/dev-vcs/gitlab-ci-runner/Manifest +++ b/dev-vcs/gitlab-ci-runner/Manifest @@ -1,3 +1,3 @@ AUX gitlab-ci-runner-5.0.0-fix-gemfile.patch 1099 SHA256 80dddabf6c0abf0096a9ba54fe5d4f7408f47c624429ff5ac2bc19afe99b5461 SHA512 208db66cb21e467656a798f8954c474a614d3de406601806e906edbafbacafef7670f2913eae43d897be38bef1f26c70cf2a33901d0667dff4f1b1b89643f08d WHIRLPOOL 5b6e4eabc1d3a7e3320cf4616d9890c221ff306aa246aaefe8b87c9e6a631ebe7c658435225f7e546d34ce21ada063a016dad5080208765879211d2a1df7a26a DIST gitlab-ci-runner-5.0.0.tar.gz 11846 SHA256 7d7f97894ba5ffeb4f06aa8a62d1ec17c2a1cbf84efd20418d74ccfc0f18c1e5 SHA512 cee02d144f37840b215a9cf706a3ca8239fbd2f33561b783b1abfd9ebdfd2eb323ecb57b8545d08f17c50a24e237053c505257279940cef2dce60f160364875f WHIRLPOOL d79a53553221715d3a34a18bce88af5fdf02e162afb1c0ca7212707f2672e0e30f584513ac9a1b7a6b9c46b3695762fd04e2819903b05504b5f482e0047dc337 -EBUILD gitlab-ci-runner-5.0.0.ebuild 8450 SHA256 4f1e7758c89f702faa1fe1fd6ff37c359600bdcfb25b3c92501d787fdbc3b6eb SHA512 d3e725feefabfae6635aa91f98aec92872e37fc18db2bc6a4056e535fbfa32d5aba782f6f5364242db8b148a7380201eb1225bf66660589eac349659f1d26eb2 WHIRLPOOL f6a563173f44bc103ae35be3e106a76fa6f4addab3d25d6a534cad04942f7d1da12d1b31778a7688b678a241fbc3d677bcd373a362a3c14f65d5c63d629d11f3 +EBUILD gitlab-ci-runner-5.0.0.ebuild 6771 SHA256 76ced6451ffd4728351a7f9057fa38aafa57b2a20cd5901172107c23b178d554 SHA512 10436fa9efebbea36432b63051ec1871d794f16542d06054b72e5848df3ddb24f481c13e2e2caba6eec820df6868c7e2f6def1a9c5a126266ccc5bd69e05ec89 WHIRLPOOL cc429beb3fadf16ca69a9ded313e9fbb0dc22b37ae4cb95490d36570efa5d38835b33e5bd1ac815e7e26153a7bf9b898f3e51223a31b30d371f31bc80e37976d diff --git a/dev-vcs/gitlab-ci-runner/files/gitlab-ci-runner.init b/dev-vcs/gitlab-ci-runner/files/gitlab-ci-runner.init new file mode 100644 index 0000000..a8a2492 --- /dev/null +++ b/dev-vcs/gitlab-ci-runner/files/gitlab-ci-runner.init @@ -0,0 +1,139 @@ +#! /bin/bash + +### BEGIN INIT INFO +# Provides: gitlab-ci-runner +# Required-Start: $local_fs $remote_fs $network $syslog +# Required-Stop: $local_fs $remote_fs $network $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: GitLab CI Runner init.d script +# Description: Enables automatic start of runners at boot time in the background. +### END INIT INFO + +APP_ROOT="/home/gitlab_ci_runner/gitlab-ci-runner" +APP_USER="gitlab_ci_runner" +PID_PATH="$APP_ROOT/tmp/pids" +PROCESS_NAME="ruby ./bin/runner" +RUNNERS_PID="$PID_PATH/runners.pid" +RUNNERS_NUM=1 # number of runners to spawn +START_RUNNER="nohup bundle exec ./bin/runner" +NAME="gitlab-ci-runner" +DESC="GitLab CI runner" +RUNNERS_REGISTERED=0 +RUNNERS_RUNNING=0 +INIT_LOG="/var/log/gitlab_ci_runner.log" + +check_pid() { + # Number of registered runners in PID file + [ -f $RUNNERS_PID ] && RUNNERS_REGISTERED=`cat $RUNNERS_PID | wc -l` + + # Number of active runner processes + RUNNERS_RUNNING=`ps -ef | grep "$PROCESS_NAME" | grep -v grep | wc -l` + + + echo "Number of registered runners in PID file=$RUNNERS_REGISTERED" + echo "Number of running runners=$RUNNERS_RUNNING" +} + +execute() { + sudo -u $APP_USER -H bash -l -c "$1" + } + +start() { + cd $APP_ROOT + check_pid + if [ "$RUNNERS_REGISTERED" -ne 0 -o "$RUNNERS_RUNNING" -ne 0 ]; then + # Program is running, Exit with error code. + echo "Error! $DESC(s) ($NAME) appear to be running already! Try stopping them first. Exiting." + exit 1 + else + if [ `whoami` = root ]; then + [ ! -f $PID_PATH ] && execute "mkdir -p $PID_PATH" + [ -f $RUNNERS_PID ] && execute "rm -f $RUNNERS_PID" + + # Spawn runners + for (( i=1; i<=$RUNNERS_NUM; i++ )) + do + # Check log file + if [ ! -f $INIT_LOG ]; then + touch $INIT_LOG + chown $APP_USER $INIT_LOG + fi + echo "Starting runner #$i" + execute "$START_RUNNER >> $INIT_LOG 2>&1 & echo \$! >> $RUNNERS_PID" + done + echo "SUCCESS: Started $RUNNERS_NUM $DESC(s)." + fi + + fi +} + +stop() { + check_pid + # Exit if there are no runners + if [ $RUNNERS_REGISTERED -eq 0 -a $RUNNERS_RUNNING -eq 0 ]; then + echo "No runners have been found. Exiting." + fi + + # Runners found. Check if there are any ghost runners. + KILL_GHOSTS=0; + if [ $RUNNERS_REGISTERED -ne $RUNNERS_RUNNING ]; then + echo "WARNING: Numbers of registered runners don't match number of running runners. Will try to stop them all" + echo "Registered runners=$RUNNERS_REGISTERED" + echo "Running runners=$RUNNERS_RUNNING" + KILL_GHOSTS=1; + fi + + echo -n "Trying to stop registered runners..." + if [ $RUNNERS_REGISTERED -gt 0 ]; then + execute "cat $RUNNERS_PID | xargs kill -USR2" + rm -f $RUNNERS_PID + echo "OK" + else + echo "FAILED!" + echo "Couldn't stop registered runners as there is no record of such in $RUNNERS_PID file". + fi + + if [ $KILL_GHOSTS -eq 1 ]; then + echo -ne "Trying to kill ghost runners..." + ps -C "$PROCESS_NAME" -o "%p" h | xargs kill -USR2 + [ $? -eq 0 ] && echo "OK" + else + echo "No ghost runners have been found.This is good." + fi +} + +status() { + echo "Here is what we have at the moment:" + check_pid +} + +## Check to see if we are running as root first. +if [ "$(id -u)" != "0" ]; then + echo "This script must be run as root" + exit 1 +fi + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + status + ;; + *) + echo "Usage: sudo service gitlab_ci_runner {start|stop|restart|status}" >&2 + exit 1 + ;; +esac + +exit 0 + + diff --git a/dev-vcs/gitlab-ci-runner/files/gitlab-ci-unicorn.init b/dev-vcs/gitlab-ci-runner/files/gitlab-ci-unicorn.init new file mode 100755 index 0000000..bd89114 --- /dev/null +++ b/dev-vcs/gitlab-ci-runner/files/gitlab-ci-unicorn.init @@ -0,0 +1,67 @@ +#!/sbin/runscript + +name="GitLab CI" +description="GitLab CI on Unicorns" + +: ${gitlab_ci_user:=@USER@} +: ${gitlab_ci_base:="@GITLAB_CI_BASE@"} +: ${rails_env:=production} + +: ${server_pidfile:="@RUN_DIR@/unicorn.pid"} + +: ${sidekiq_pidfile:="@RUN_DIR@/sidekiq.pid"} +: ${sidekiq_logfile:="@LOGS_DIR@/sidekiq.log"} +: ${sidekiq_queues:="@QUEUES@"} + +server_command="/usr/bin/bundle" +server_command_args="exec unicorn_rails -c ${gitlab_ci_base}/config/unicorn.rb -E ${rails_env} -D" + +sidekiq_command="/usr/bin/bundle" +sidekiq_command_args="exec sidekiq -q ${sidekiq_queues} -P ${sidekiq_pidfile} -L ${sidekiq_logfile}" + +depend() { + provide gitlab-ci + need redis + use net +} + +start() { + ebegin "Starting ${name} - Unicorn servers" + + checkpath -d -o ${gitlab_ci_user} -m755 "$(dirname "${server_pidfile}")" + checkpath -d -o ${gitlab_ci_user} -m755 "$(dirname "${sidekiq_pidfile}")" + + start-stop-daemon --start \ + --chdir "${gitlab_ci_base}" \ + --user=${gitlab_ci_user} \ + --pidfile="${server_pidfile}" \ + --env RAILS_ENV=${rails_env} \ + --exec ${server_command} -- ${server_command_args} + eend $? + + ebegin "Starting ${name} - Sidekiq" + + start-stop-daemon --start \ + --background --quiet \ + --chdir "${gitlab_ci_base}" \ + --user=${gitlab_ci_user} \ + --pidfile="${sidekiq_pidfile}" \ + --env RAILS_ENV=${rails_env} \ + --exec ${sidekiq_command} -- ${sidekiq_command_args} + eend $? +} + +stop() { + ebegin "Stopping ${name} - Sidekiq" + start-stop-daemon --stop \ + --pidfile=${sidekiq_pidfile} \ + --exec ${sidekiq_command} + eend $? + + ebegin "Stopping ${name} - Unicorn servers" + start-stop-daemon --stop \ + --signal QUIT \ + --pidfile=${server_pidfile} \ + --exec ${server_command} + eend $? +} diff --git a/dev-vcs/gitlab-ci-runner/gitlab-ci-runner-5.0.0.ebuild b/dev-vcs/gitlab-ci-runner/gitlab-ci-runner-5.0.0.ebuild index eceb021..db5dd33 100644 --- a/dev-vcs/gitlab-ci-runner/gitlab-ci-runner-5.0.0.ebuild +++ b/dev-vcs/gitlab-ci-runner/gitlab-ci-runner-5.0.0.ebuild @@ -39,7 +39,6 @@ GEMS_DEPEND=" DEPEND="${GEMS_DEPEND} dev-vcs/git" RDEPEND="${DEPEND} - dev-db/redis virtual/mta" ruby_add_bdepend " virtual/rubygems @@ -54,10 +53,8 @@ MY_NAME="gitlab-ci-runner" MY_USER="gitlab_ci_runner" DEST_DIR="/opt/${MY_NAME}" -CONF_DIR="/etc/${MY_NAME}" LOGS_DIR="/var/log/${MY_NAME}" TEMP_DIR="/var/tmp/${MY_NAME}" -RUN_DIR="/run/${MY_NAME}" # When updating ebuild to newer version, check list of the queues in # https://gitlab.com/gitlab-org/gitlab-ci/blob/v${PV}/script/background_jobs @@ -69,77 +66,29 @@ pkg_setup() { } all_ruby_prepare() { - - # fix paths - sed -i -E \ - -e "s|redis://redis.example.com:6379|unix:/run/redis/redis.sock|" \ - config/resque.yml.example || die "failed to filter resque.yml.example" - sed -i -E \ - -e "s|/home/gitlab_ci/gitlab-ci/tmp/(pids\|sockets)|${RUN_DIR}|" \ - -e "s|/home/gitlab_ci/gitlab-ci/log|${LOGS_DIR}|" \ - -e "s|/home/gitlab_ci/gitlab-ci|${DEST_DIR}|" \ - config/unicorn.rb.example || die "failed to filter unicorn.rb.example" - - sed -i \ - -e "s|/home/gitlab_ci/gitlab-ci/tmp/sockets|${RUN_DIR}|" \ - -e "s|/home/gitlab_ci/gitlab-ci/public|${DEST_DIR}/public|" \ - lib/support/nginx/gitlab_ci || die "failed to filter nginx/gitlab_ci" - - # modify default database settings for PostgreSQL - sed -i -E \ - -e 's|(username:).*|\1 gitlab|' \ - -e 's|(password:).*|\1 gitlab|' \ - -e 's|(socket:).*|/run/postgresql/.s.PGSQL.5432|' \ - config/database.yml.postgresql \ - || die "failed to filter database.yml.postgresql" - # modify default database settings for MySQL - sed -i -E \ - -e "s|/tmp/mysql.sock|/run/mysqld/mysqld.sock|" \ - config/database.yml.mysql || die "failed to filter database.yml.mysql" - - # rename config files - mv config/application.yml.example config/application.yml - mv config/unicorn.rb.example config/unicorn.rb - - local dbconf=config/database.yml - if use postgres && ! use mysql; then - mv ${dbconf}.postgresql ${dbconf} - rm ${dbconf}.mysql - elif use mysql && ! use postgres; then - mv ${dbconf}.mysql ${dbconf} - rm ${dbconf}.postgresql - fi - # remove useless files - rm -r lib/support/init.d + rm -r lib/support/{init.d,logrotate.d} } all_ruby_install() { local dest=${DEST_DIR} - local conf=${CONF_DIR} local logs=${LOGS_DIR} local temp=${TEMP_DIR} - local runs=${RUN_DIR} # prepare directories diropts -m750 dodir ${logs} ${temp} diropts -m755 - dodir ${conf} ${runs} ${dest}/public/uploads + dodir ${dest} dosym ${temp} ${dest}/tmp dosym ${logs} ${dest}/log - # install configs - insinto ${conf} - doins -r config/* - dosym ${conf} ${dest}/config - echo 'export RAILS_ENV=production' > "${D}/${dest}/.profile" # remove needless dirs - rm -Rf config tmp log +# rm -Rf config tmp log # install the rest files # using cp 'cause doins is slow @@ -160,11 +109,7 @@ all_ruby_install() { cd "${D}/${dest}" - local without="development test aws" - local flag; for flag in mysql postgres; do - without+="$(use $flag || echo ' '$flag)" - done - local bundle_args="--deployment ${without:+--without ${without}}" + local bundle_args="--deployment" einfo "Running bundle install ${bundle_args} ..." ${RUBY} /usr/bin/bundle install ${bundle_args} || die "bundler failed" @@ -173,13 +118,11 @@ all_ruby_install() { rm -Rf vendor/bundle/ruby/*/cache # fix permissions - fowners -R ${MY_USER}:${MY_USER} ${dest} ${temp} ${logs} ${runs} - fowners ${MY_USER}:${MY_USER} ${conf}/database.yml - fperms 640 ${conf}/database.yml + fowners -R ${MY_USER}:${MY_USER} ${dest} ${temp} ${logs} ## RC script ## - local rcscript=gitlab-ci-unicorn.init + local rcscript=gitlab-ci-runner.init cp "${FILESDIR}/${rcscript}" "${T}" || die sed -i \