From af89ef784234e7b89984d5b2995fc6d29b6d8c34 Mon Sep 17 00:00:00 2001 From: Tim Johnson Date: Mon, 4 Mar 2019 10:33:59 -0800 Subject: [PATCH] Cleanup Jenkins init scripts RELENG-1856 Cleanup Jenkins init scripts Make scripts portable so the ci-management can be installed anywhere. Update README comments. All changes should be backward compatible. Change-Id: I5ce6f76cfa0483b7a1b91b72ae1264c21e769dba Signed-off-by: Tim Johnson --- jenkins-init-scripts/README | 40 ++++++++++++++++------ jenkins-init-scripts/create-jenkins-user.sh | 11 +++--- jenkins-init-scripts/init.sh | 19 +++++----- jenkins-init-scripts/package-listing.sh | 12 ++++--- .../relocatable-init-scripts-3b1c5387fd723904.yaml | 5 +++ 5 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 releasenotes/notes/relocatable-init-scripts-3b1c5387fd723904.yaml diff --git a/jenkins-init-scripts/README b/jenkins-init-scripts/README index 2d85978c..286b620f 100644 --- a/jenkins-init-scripts/README +++ b/jenkins-init-scripts/README @@ -1,15 +1,33 @@ -The files in this directory are for initializing a Jenkins minion just before -the cloud plugin hands the minion over to Jenkins. +The files in this directory are for initializing a Jenkins minion just +before the cloud plugin hands the minion over to Jenkins. -init.sh is the entry point script which then calls all other scripts. +The Jenkins 'init.sh' script will check for a project specific init +script: '$ciman_root/jenkins-init-scripts/local-init.sh'. If the file +exits and is executable, it will be executed. Note the local init +script will be executed before the 'jenkins' user has been created. +You can also call a project specific script from the Jenkins Manged +File after the call to 'init.sh'. -init.sh also provides a local init script entry point by checking for the -existance of the file CIMAN_ROOT/jenkins-init-scripts/local-init.sh. If this -file exists it can be used to further customize the server node before handing -it to Jenkins for job use. - -To use this create a managed file in Jenkins with the contents: +To use this create a Jenkins Managed File with the contents: #!/bin/bash - git clone --recurse-submodules https://gerrit.example.org/r/ci-management.git /opt/ciman - /opt/ciman/jjb/global-jjb/jenkins-init-scripts/init.sh + + gerrit_host=gerrit.example.org + ciman_root=/opt/ciman + + until host $gerrit_host &>/dev/null ; do + echo "Waiting until $gerrit_host is resolvable..." + sleep 1 + done + + git clone --recurse-submodules https://$gerrit_host/r/ci-management $ciman_root + + # Specify swapspace on the minion + # SWAP_SIZE specifies the size of swapspace in GB If set to '0' no + # swapspace will be allocated If unset, 1GB swapspace will be + # allocated + #export SWAP_SIZE=0 + + $ciman_root/global-jjb/jenkins-init-scripts/init.sh + + rm -rf $ciman_root diff --git a/jenkins-init-scripts/create-jenkins-user.sh b/jenkins-init-scripts/create-jenkins-user.sh index 0983129f..e1e0b1ec 100755 --- a/jenkins-init-scripts/create-jenkins-user.sh +++ b/jenkins-init-scripts/create-jenkins-user.sh @@ -12,10 +12,8 @@ OS=$(facter operatingsystem | tr '[:upper:]' '[:lower:]') OS_RELEASE=$(facter lsbdistrelease | tr '[:upper:]' '[:lower:]') -if [[ "$OS_RELEASE" == "18.04" ]] && [[ "$OS" == 'ubuntu' ]] -then - echo 'PATH=$HOME/.local/bin:$PATH -export PATH' >> /etc/profile +if [[ "$OS_RELEASE" == "18.04" && "$OS" == 'ubuntu' ]]; then + echo 'export PATH=$HOME/.local/bin:$PATH' >> /etc/profile fi useradd -m -s /bin/bash jenkins @@ -30,12 +28,13 @@ if grep -q mock /etc/group; then fi mkdir /home/jenkins/.ssh -cp -r "/home/${OS}/.ssh/authorized_keys" /home/jenkins/.ssh/authorized_keys +cp "/home/${OS}/.ssh/authorized_keys" /home/jenkins/.ssh/authorized_keys +chmod 0600 /home/jenkins/.ssh/authorized_keys # Generate ssh key for use by Robot jobs echo -e 'y\n' | ssh-keygen -N "" -f /home/jenkins/.ssh/id_rsa -t rsa chown -R jenkins:jenkins /home/jenkins/.ssh -chmod 700 /home/jenkins/.ssh +chmod 0700 /home/jenkins/.ssh # The '/w' volume may already be part of image [[ ! -d '/w' ]] && mkdir /w diff --git a/jenkins-init-scripts/init.sh b/jenkins-init-scripts/init.sh index 1bd2229e..0ae7654e 100755 --- a/jenkins-init-scripts/init.sh +++ b/jenkins-init-scripts/init.sh @@ -9,19 +9,20 @@ # http://www.eclipse.org/legal/epl-v10.html ############################################################################## -INIT_SCRIPTS_DIR="/opt/ciman/global-jjb/jenkins-init-scripts" +ciman_init_scripts=$(readlink -f "$(dirname "$0")"/../../jenkins-init-scripts) +jjb_init_scripts=$(readlink -f "$(dirname "$0")") -"$INIT_SCRIPTS_DIR/package-listing.sh" -"$INIT_SCRIPTS_DIR/basic-settings.sh" -"$INIT_SCRIPTS_DIR/disable-firewall.sh" -"$INIT_SCRIPTS_DIR/create-swap-file.sh" +"$jjb_init_scripts/package-listing.sh" +"$jjb_init_scripts/basic-settings.sh" +"$jjb_init_scripts/disable-firewall.sh" +"$jjb_init_scripts/create-swap-file.sh" # Entry point for additional local minion customization -# Eg. OpenDaylight has additional bootstrap scripts depending on minion type. -if [ -f "/opt/ciman/jenkins-init-scripts/local-init.sh" ]; then - /opt/ciman/jenkins-init-scripts/local-init.sh +# Note this is called before the 'jenkins' account is created +if [ -e "$ciman_init_scripts/local-init.sh" ]; then + "$ciman_init_scripts/local-init.sh" fi # Create the jenkins user last so that hopefully we DO NOT have to deal with # guard files -"$INIT_SCRIPTS_DIR/create-jenkins-user.sh" +"$jjb_init_scripts/create-jenkins-user.sh" diff --git a/jenkins-init-scripts/package-listing.sh b/jenkins-init-scripts/package-listing.sh index 2598a224..93a2e747 100755 --- a/jenkins-init-scripts/package-listing.sh +++ b/jenkins-init-scripts/package-listing.sh @@ -9,9 +9,11 @@ # http://www.eclipse.org/legal/epl-v10.html ############################################################################## -# Share script with JJB jobs so we only have to maintain it in one place -JJB_SHELL_DIR="$(dirname $0)/../shell" +# The 'init' scripts are located in 'jenkins-init-scripts' directory. +# The 'global-jjb' scripts are located in 'shell' directory. The +# 'package-listing' script is used by both, this is a simple wrapper +# for 'shell/package-listing.sh'. Arguments would be quietly discarded. -# Make sure the script is executable and then run it -chmod +x "${JJB_SHELL_DIR}/package-listing.sh" -"${JJB_SHELL_DIR}/package-listing.sh" +jjb_init_scripts="$(dirname "$0")/../shell" + +"$jjb_init_scripts/package-listing.sh" diff --git a/releasenotes/notes/relocatable-init-scripts-3b1c5387fd723904.yaml b/releasenotes/notes/relocatable-init-scripts-3b1c5387fd723904.yaml new file mode 100644 index 00000000..386197d3 --- /dev/null +++ b/releasenotes/notes/relocatable-init-scripts-3b1c5387fd723904.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + **jenkiins-init-scripts** The 'ciman' repo is not longer required to be + located in '/opt/ciman'. -- 2.16.6