From ccad7ddbe12ffcd7ceb59ad81444d02d1a2d9cf1 Mon Sep 17 00:00:00 2001
From: "James E. Blair" <jeblair@linux.vnet.ibm.com>
Date: Tue, 26 Apr 2016 08:57:22 -0500
Subject: [PATCH] Add support for zuul launcher

Change-Id: I814a241e3934885dcd9020b5a73e69d1aa594b8f
---
 files/launcher-logging.conf |  49 +++++++++++++
 files/zuul-launcher.init    | 141 ++++++++++++++++++++++++++++++++++++
 manifests/init.pp           |  12 +++
 manifests/launcher.pp       |  61 ++++++++++++++++
 templates/zuul.conf.erb     |  20 +++++
 5 files changed, 283 insertions(+)
 create mode 100644 files/launcher-logging.conf
 create mode 100644 files/zuul-launcher.init
 create mode 100644 manifests/launcher.pp

diff --git a/files/launcher-logging.conf b/files/launcher-logging.conf
new file mode 100644
index 0000000..d44871c
--- /dev/null
+++ b/files/launcher-logging.conf
@@ -0,0 +1,49 @@
+[loggers]
+keys=root,zuul,gerrit,gear
+
+[handlers]
+keys=console,debug,normal
+
+[formatters]
+keys=simple
+
+[logger_root]
+level=WARNING
+handlers=console
+
+[logger_zuul]
+level=DEBUG
+handlers=debug,normal
+qualname=zuul
+
+[logger_gerrit]
+level=INFO
+handlers=debug,normal
+qualname=gerrit
+
+[logger_gear]
+level=WARNING
+handlers=debug,normal
+qualname=gear
+
+[handler_console]
+level=WARNING
+class=StreamHandler
+formatter=simple
+args=(sys.stdout,)
+
+[handler_debug]
+level=DEBUG
+class=logging.handlers.WatchedFileHandler
+formatter=simple
+args=('/var/log/zuul/launcher-debug.log',)
+
+[handler_normal]
+level=INFO
+class=logging.handlers.WatchedFileHandler
+formatter=simple
+args=('/var/log/zuul/launcher.log',)
+
+[formatter_simple]
+format=%(asctime)s %(levelname)s %(name)s: %(message)s
+datefmt=
diff --git a/files/zuul-launcher.init b/files/zuul-launcher.init
new file mode 100644
index 0000000..509af8e
--- /dev/null
+++ b/files/zuul-launcher.init
@@ -0,0 +1,141 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          zuul
+# Required-Start:    $remote_fs $syslog
+# Required-Stop:     $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Zuul
+# Description:       Trunk gating system launcher
+### END INIT INFO
+
+# Do NOT "set -e"
+
+# PATH should only include /usr/* if it runs after the mountnfs.sh script
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="Zuul Launcher"
+NAME=zuul-launcher
+DAEMON=/usr/local/bin/zuul-launcher
+PIDFILE=/var/run/$NAME/$NAME.pid
+SCRIPTNAME=/etc/init.d/$NAME
+USER=zuul
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    # Return
+    #   0 if daemon has been started
+    #   1 if daemon was already running
+    #   2 if daemon could not be started
+    #   3 if pid file already exist
+
+    mkdir -p /var/run/$NAME
+    chown $USER /var/run/$NAME
+    ulimit -n 8192
+    if [ -f $PIDFILE ]; then
+        return 3
+    fi
+    start-stop-daemon \
+        --start --quiet --pidfile $PIDFILE -c $USER \
+        --exec $DAEMON --test > /dev/null || return 1
+    start-stop-daemon \
+        --start --quiet --pidfile $PIDFILE -c $USER \
+        --exec $DAEMON -- $DAEMON_ARGS || return 2
+    # Add code here, if necessary, that waits for the process to be ready
+    # to handle requests from services started subsequently which depend
+    # on this one.  As a last resort, sleep for some time.
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    # Return
+    #   0 if daemon has been stopped
+    #   1 if daemon was already stopped
+    #   2 if daemon could not be stopped
+    #   other if a failure occurred
+    start-stop-daemon --stop --signal 9 --pidfile $PIDFILE
+    RETVAL="$?"
+    [ "$RETVAL" = 2 ] && return 2
+    rm -f /var/run/$NAME/*
+    return "$RETVAL"
+}
+ 
+#
+# Function that sends a SIGHUP to the daemon/service
+#
+do_reload() {
+    #
+    # If the daemon can reload its configuration without
+    # restarting (for example, when it is sent a SIGHUP),
+    # then implement that here.
+    #
+    start-stop-daemon \
+        --stop --signal 1 --quiet --pidfile $PIDFILE --name zuul-launcher
+    return 0
+}
+
+case "$1" in
+    start)
+        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+        do_start
+        case "$?" in
+            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+            3) echo "Pidfile at $PIDFILE already exists, run service zuul-launcher stop to clean up."
+        esac
+        ;;
+    stop)
+        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+        do_stop
+        case "$?" in
+            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+        esac
+        ;;
+    status)
+       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
+       ;;
+    reload|force-reload)
+        #
+        # If do_reload() is not implemented then leave this commented out
+        # and leave 'force-reload' as an alias for 'restart'.
+        #
+        log_daemon_msg "Reloading $DESC" "$NAME"
+        do_reload
+        log_end_msg $?
+        ;;
+    restart)
+        #
+        # If the "reload" option is implemented then remove the
+        # 'force-reload' alias
+        #
+        log_daemon_msg "Restarting $DESC" "$NAME"
+        do_stop
+        do_start
+        ;;
+    *)
+        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
+        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
+        exit 3
+        ;;
+esac
+
+:
diff --git a/manifests/init.pp b/manifests/init.pp
index 11b9a3b..01e68b2 100644
--- a/manifests/init.pp
+++ b/manifests/init.pp
@@ -53,6 +53,10 @@ class zuul (
   $proxy_ssl_key_file_contents = '',
   $proxy_ssl_chain_file_contents = '',
   $block_referers = [],
+  # Launcher config
+  $jenkins_jobs = '',
+  $workspace_root = '',
+  $sites = [],
 ) {
   include ::httpd
   include ::pip
@@ -346,6 +350,14 @@ class zuul (
     source => 'puppet:///modules/zuul/zuul-merger.init',
   }
 
+  file { '/etc/init.d/zuul-launcher':
+    ensure => present,
+    owner  => 'root',
+    group  => 'root',
+    mode   => '0555',
+    source => 'puppet:///modules/zuul/zuul-launcher.init',
+  }
+
   if $proxy_ssl_cert_file_contents == '' {
     $ssl = false
   } else {
diff --git a/manifests/launcher.pp b/manifests/launcher.pp
new file mode 100644
index 0000000..31ed806
--- /dev/null
+++ b/manifests/launcher.pp
@@ -0,0 +1,61 @@
+# Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
+# Copyright 2014 OpenStack Foundation
+# Copyright 2016 IBM Corp.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# == Class: zuul::launcher
+#
+class zuul::launcher (
+  $ensure = undef,
+  $manage_log_conf = true,
+) {
+  service { 'zuul-launcher':
+    ensure     => $ensure,
+    name       => 'zuul-launcher',
+    enable     => true,
+    hasrestart => true,
+    require    => File['/etc/init.d/zuul-launcher'],
+  }
+
+  if $manage_log_conf {
+    file { '/etc/zuul/launcher-logging.conf':
+      ensure => present,
+      source => 'puppet:///modules/zuul/launcher-logging.conf',
+    }
+  }
+
+  include ::logrotate
+  ::logrotate::file { 'launcher.log':
+    log     => '/var/log/zuul/launcher.log',
+    options => [
+      'compress',
+      'missingok',
+      'rotate 30',
+      'daily',
+      'notifempty',
+    ],
+    require => Service['zuul-launcher'],
+  }
+  ::logrotate::file { 'launcher-debug.log':
+    log     => '/var/log/zuul/launcher-debug.log',
+    options => [
+      'compress',
+      'missingok',
+      'rotate 30',
+      'daily',
+      'notifempty',
+    ],
+    require => Service['zuul-launcher'],
+  }
+}
diff --git a/templates/zuul.conf.erb b/templates/zuul.conf.erb
index ded5fdc..9677a53 100644
--- a/templates/zuul.conf.erb
+++ b/templates/zuul.conf.erb
@@ -57,3 +57,23 @@ port=<%= @smtp_port %>
 default_from=<%= @smtp_default_from %>
 default_to=<%= @smtp_default_to %>
 <% end -%>
+
+<% if @jenkins_jobs != "" -%>
+[launcher]
+jenkins_jobs=<%= @jenkins_jobs %>
+workspace_root=<%= @workspace_root %>
+<% end -%>
+
+<% @sites.each do |site| -%>
+[site "<%= site['name'] %>"]
+host=<%= site['host'] %>
+<% if site['user'] != "" -%>
+user="<%= site['user'] %>"
+<% end -%>
+<% if site['pass'] != "" -%>
+pass="<%= site['pass'] %>"
+<% end -%>
+<% if site['root'] != "" -%>
+root="<%= site['root'] %>"
+<% end -%>
+<% end -%>