From a22888ff871199bb1259e706ceabd3d3eb767411 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Thu, 21 Aug 2014 16:41:23 +0200 Subject: [PATCH] A bit of RedHat and Debian slave initd script merging Add -mode flag to Debian slave start script Use a single /etc/defaults or /etc/sysconfig file for both OSes Do not customize /etc/init.d/jenkins-slave, do it in sysconfig or defaults Log output to file in RedHat Compose slave arguments in shell script --- files/{jenkins-slave => jenkins-slave.Debian} | 0 .../jenkins-slave.RedHat | 32 +++++-- manifests/slave.pp | 85 +++++-------------- spec/classes/jenkins_slave_spec.rb | 6 +- ...ults.Debian => jenkins-slave-defaults.erb} | 39 ++++++++- 5 files changed, 85 insertions(+), 77 deletions(-) rename files/{jenkins-slave => jenkins-slave.Debian} (100%) rename templates/jenkins-slave.erb => files/jenkins-slave.RedHat (50%) rename templates/{jenkins-slave-defaults.Debian => jenkins-slave-defaults.erb} (57%) diff --git a/files/jenkins-slave b/files/jenkins-slave.Debian similarity index 100% rename from files/jenkins-slave rename to files/jenkins-slave.Debian diff --git a/templates/jenkins-slave.erb b/files/jenkins-slave.RedHat similarity index 50% rename from templates/jenkins-slave.erb rename to files/jenkins-slave.RedHat index fbdfa5694..f8d2adcfd 100644 --- a/templates/jenkins-slave.erb +++ b/files/jenkins-slave.RedHat @@ -8,8 +8,15 @@ # RETVAL=0 -PID_FILE=/var/run/jenkins-slave.pid -LOCK_FILE=/var/lock/jenkins-slave +NAME=jenkins-slave +JENKINS_CONFIG=/etc/sysconfig/$NAME +LOCK_FILE=/var/lock/$NAME + +# Source function library. +. /etc/init.d/functions + +# Read config +[ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG" if [ -x /sbin/runuser ] ; then RUNUSER=runuser @@ -19,16 +26,27 @@ fi slave_start() { echo Starting Jenkins Slave... - $RUNUSER - <%= @slave_user -%> -c 'java -jar <%= @slave_home -%>/<%= @client_jar -%> <%= @ui_user_flag -%> <%= @ui_pass_flag -%> -mode <%= @slave_mode -%> -name <%= @fqdn || @hostname -%> -executors <%= @executors -%> <%= @masterurl_flag -%> <%= @labels_flag -%> <%= @disable_ssl_verification_flag -%> <%= @fsroot_flag -%> &' - pgrep -f -u <%= @slave_user -%> <%= @client_jar -%> > $PID_FILE + + # the default location is /var/run/jenkins/jenkins.pid but the parent directory needs to be created + mkdir `dirname $PIDFILE` > /dev/null 2>&1 || true + chown $JENKINS_SLAVE_USER `dirname $PIDFILE` + + # create log directory + mkdir -p `dirname $JENKINS_SLAVE_LOG` > /dev/null 2>&1 || true + chown $JENKINS_SLAVE_USER -R `dirname $JENKINS_SLAVE_LOG` + + # --user in daemon doesn't prepare environment variables like HOME, USER, LOGNAME or USERNAME, + # so we let su do so for us now + $RUNUSER - $JENKINS_SLAVE_USER -c "$JAVA $JAVA_ARGS -jar $JENKINS_SLAVE_JAR $JENKINS_SLAVE_ARGS &>> $JENKINS_SLAVE_LOG &" + pgrep -f -u $JENKINS_SLAVE_USER $JENKINS_SLAVE_JAR > $PIDFILE RETVAL=$? [ $RETVAL -eq 0 ] && touch $LOCK_FILE } slave_stop() { echo Stopping Jenkins Slave... - pid=`cat $PID_FILE` + pid=`cat $PIDFILE` - killproc -p $PID_FILE + killproc -p $PIDFILE # Wait until the monitor exits while (checkpid $pid) @@ -50,7 +68,7 @@ slave_restart() { } slave_status() { echo Jenkins Slave status: - status -p $PID_FILE + status -p $PIDFILE RETVAL=$? } case "$1" in diff --git a/manifests/slave.pp b/manifests/slave.pp index bb054e2e5..d9e348849 100644 --- a/manifests/slave.pp +++ b/manifests/slave.pp @@ -87,14 +87,6 @@ } } - #If disable_ssl_verification is set to true - if $disable_ssl_verification { - #disable SSL verification to the init script - $disable_ssl_verification_flag = '-disableSslVerification' - } else { - $disable_ssl_verification_flag = '' - } - #add jenkins slave user if necessary. if $manage_slave_user and $slave_uid { user { 'jenkins-slave_user': @@ -126,72 +118,39 @@ ## needs to be fixed if you create another version.. } - if $ui_user { - $ui_user_flag = "-username ${ui_user}" - } - else {$ui_user_flag = ''} - - if $ui_pass { - $ui_pass_flag = "-password ${ui_pass}" - } else { - $ui_pass_flag = '' - } - - if $masterurl { - $masterurl_flag = "-master ${masterurl}" - } else { - $masterurl_flag = '' - } - - if $labels { - $labels_flag = "-labels \'${labels}\'" - } else { - $labels_flag = '' - } - - if $slave_home { - $fsroot_flag = "-fsroot ${slave_home}" - } - - # choose the correct init functions + # customizations based on the OS family case $::osfamily { Debian: { - file { '/etc/init.d/jenkins-slave': - ensure => 'file', - mode => '0700', - owner => 'root', - group => 'root', - source => "puppet:///modules/${module_name}/jenkins-slave", - notify => Service['jenkins-slave'], - require => File['/etc/default/jenkins-slave'], - } - - file { '/etc/default/jenkins-slave': - ensure => 'file', - mode => '0600', - owner => 'root', - group => 'root', - content => template("${module_name}/jenkins-slave-defaults.${::osfamily}"), - notify => Service['jenkins-slave'], - require => Package['daemon'], - } + $defaults_location = '/etc/default' package {'daemon': ensure => present, + before => Service['jenkins-slave'], } } default: { - file { '/etc/init.d/jenkins-slave': - ensure => 'file', - mode => '0700', - owner => 'root', - group => 'root', - content => template("${module_name}/jenkins-slave.erb"), - notify => Service['jenkins-slave'], - } + $defaults_location = '/etc/sysconfig' } } + file { '/etc/init.d/jenkins-slave': + ensure => 'file', + mode => '0755', + owner => 'root', + group => 'root', + source => "puppet:///modules/${module_name}/jenkins-slave.${::osfamily}", + notify => Service['jenkins-slave'], + } + + file { "${defaults_location}/jenkins-slave": + ensure => 'file', + mode => '0600', + owner => 'root', + group => 'root', + content => template("${module_name}/jenkins-slave-defaults.erb"), + notify => Service['jenkins-slave'], + } + service { 'jenkins-slave': ensure => running, enable => $enable, diff --git a/spec/classes/jenkins_slave_spec.rb b/spec/classes/jenkins_slave_spec.rb index bd235096a..3f0bc56a5 100644 --- a/spec/classes/jenkins_slave_spec.rb +++ b/spec/classes/jenkins_slave_spec.rb @@ -8,7 +8,7 @@ it { should contain_service('jenkins-slave') } it { should contain_user('jenkins-slave_user').with_uid(nil) } # Let the different platform blocks define `slave_runtime_file` separately below - it { should contain_file(slave_runtime_file).with_content(/-fsroot \/home\/jenkins-slave/) } + it { should contain_file(slave_runtime_file).with_content(/^FSROOT="\/home\/jenkins-slave"$/) } describe 'with ssl verification disabled' do let(:params) { { :disable_ssl_verification => true } } @@ -23,13 +23,13 @@ describe 'with a non-default $slave_home' do let(:home) { '/home/rspec-runner' } let(:params) { {:slave_home => home } } - it { should contain_file(slave_runtime_file).with_content(/-fsroot #{home}/) } + it { should contain_file(slave_runtime_file).with_content(/^FSROOT="#{home}"$/) } end end describe 'RedHat' do let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'CentOS' } } - let(:slave_runtime_file) { '/etc/init.d/jenkins-slave' } + let(:slave_runtime_file) { '/etc/sysconfig/jenkins-slave' } it_behaves_like 'a jenkins::slave catalog' end diff --git a/templates/jenkins-slave-defaults.Debian b/templates/jenkins-slave-defaults.erb similarity index 57% rename from templates/jenkins-slave-defaults.Debian rename to templates/jenkins-slave-defaults.erb index 260558e72..d0cc0e527 100644 --- a/templates/jenkins-slave-defaults.Debian +++ b/templates/jenkins-slave-defaults.erb @@ -29,6 +29,10 @@ RUN_STANDALONE=true # log location. this may be a syslog facility.priority JENKINS_SLAVE_LOG=/var/log/$NAME/$NAME.log +# slave mode, can be either 'normal' (utilize this slave as much as possible) +# or 'exclusive' (leave this machine for tied jobs only). +JENKINS_SLAVE_MODE=<%= @slave_mode -%> + # OS LIMITS SETUP # comment this out to observe /etc/security/limits.conf # this is on by default because http://github.com/jenkinsci/jenkins/commit/2fb288474e980d0e7ff9c4a3b768874835a3e92e @@ -36,12 +40,39 @@ JENKINS_SLAVE_LOG=/var/log/$NAME/$NAME.log # descriptors are forced to 1024 regardless of /etc/security/limits.conf MAXOPENFILES=8192 -MASTER_URL="<%= @masterurl_flag -%> <%= @labels_flag -%>" +MASTER_URL="<%= @masterurl -%>" +LABELS="<%= @labels -%>" EXECUTORS=<%= @executors -%> -CLIENT_NAME=<%= @fqdn -%> +CLIENT_NAME="<%= @fqdn -%>" + +FSROOT="<%= @slave_home -%>" + +# credentials +JENKINS_USERNAME="<%= @ui_user -%>" +JENKINS_PASSWORD="<%= @ui_pass -%>" + +OTHER_ARGS="<%= '-disableSslVerification' if @disable_ssl_verification -%>" + +if [ -n "$JENKINS_USERNAME" ]; then + CREDENTIALS_ARG="-username $JENKINS_USERNAME -password $JENKINS_PASSWORD" +fi + +if [ -n "$CLIENT_NAME" ]; then + NAME_ARG="-name $CLIENT_NAME" +fi + +if [ -n "$MASTER_URL" ]; then + MASTER_URL_ARG="-master $MASTER_URL" +fi + +if [ -n "$LABELS" ]; then + LABELS_ARG="-labels '$LABELS'" +fi -MODE=<%= @slave_mode -%> +if [ -n "$FSROOT" ]; then + FSROOT_ARG="-fsroot '$FSROOT'" +fi -JENKINS_SLAVE_ARGS="<%= @ui_user_flag -%> <%= @ui_pass_flag -%> -name $CLIENT_NAME <%= @disable_ssl_verification_flag -%> -executors $EXECUTORS -mode $MODE $MASTER_URL <%= @fsroot_flag -%>" +JENKINS_SLAVE_ARGS="-mode $JENKINS_SLAVE_MODE -executors $EXECUTORS $CREDENTIALS_ARG $NAME_ARG $MASTER_URL_ARG $LABELS_ARG $FSROOT_ARG $OTHER_ARGS"