From 35d3cf36fd8d32944f6169d2cd62d562378fbfe0 Mon Sep 17 00:00:00 2001 From: Nikolay Mahotkin Date: Thu, 23 Jun 2016 13:37:07 +0300 Subject: [PATCH] [Jenkins] Fixing upgrading of git plugin * Added a generic script for installing plugin with their dependencies Change-Id: I66239967f1bac1cfde1a3add81de2b70657e9f5d --- .../Jenkins/package/Classes/Jenkins.yaml | 6 +- .../package/Resources/InstallPlugins.template | 18 ++++++ .../Resources/scripts/install_plugins.sh | 62 +++++++++++++++++++ .../Jenkins/package/Resources/scripts/site.pp | 6 +- .../package/Resources/upgrade_git_plugin.sh | 26 -------- 5 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 murano-apps/Jenkins/package/Resources/InstallPlugins.template create mode 100755 murano-apps/Jenkins/package/Resources/scripts/install_plugins.sh delete mode 100644 murano-apps/Jenkins/package/Resources/upgrade_git_plugin.sh diff --git a/murano-apps/Jenkins/package/Classes/Jenkins.yaml b/murano-apps/Jenkins/package/Classes/Jenkins.yaml index ffc1239..e86b227 100644 --- a/murano-apps/Jenkins/package/Classes/Jenkins.yaml +++ b/murano-apps/Jenkins/package/Classes/Jenkins.yaml @@ -206,10 +206,10 @@ Methods: _upgradeGitPlugin: Body: - - $linux: new(conf:Linux) - $resource: new(sys:Resources) - - $script: $resource.string('upgrade_git_plugin.sh') - - $linux.runCommand($.instance.agent, $script) + - $._environment.reporter.report($this, 'Upgrading jenkins git plugin...') + - $template: $resource.yaml('InstallPlugins.template').bind({plugins => git}) + - $.instance.agent.call($template, $resource) configureCredentials: Body: diff --git a/murano-apps/Jenkins/package/Resources/InstallPlugins.template b/murano-apps/Jenkins/package/Resources/InstallPlugins.template new file mode 100644 index 0000000..893c13e --- /dev/null +++ b/murano-apps/Jenkins/package/Resources/InstallPlugins.template @@ -0,0 +1,18 @@ +FormatVersion: 2.1.0 +Version: 1.0.0 +Name: Install Jenkins plugins + +Parameters: + plugins: $plugins + +Body: | + return install_plugin(args.plugins).stdout + +Scripts: + install_plugin: + Type: Application + Version: 1.0.0 + EntryPoint: install_plugins.sh + Options: + captureStdout: true + captureStderr: true \ No newline at end of file diff --git a/murano-apps/Jenkins/package/Resources/scripts/install_plugins.sh b/murano-apps/Jenkins/package/Resources/scripts/install_plugins.sh new file mode 100755 index 0000000..17e41cd --- /dev/null +++ b/murano-apps/Jenkins/package/Resources/scripts/install_plugins.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash + +set -e + +if [ $# -eq 0 ]; then + echo "USAGE: $0 plugin1 plugin2 ..." + exit 1 +fi + +plugin_dir=/var/lib/jenkins/plugins +owner=jenkins:jenkins + +mkdir -p ${plugin_dir} + +installPlugin() { + plugin_name=$1 + if [ -f ${plugin_dir}/${plugin_name}.hpi -o -f ${plugin_dir}/${plugin_name}.jpi ]; then + if [ "$2" == "1" ]; then + return 1 + fi + fi + echo "Installing: $plugin_name" + curl -L --silent --output ${plugin_dir}/${plugin_name}.hpi https://updates.jenkins-ci.org/latest/${plugin_name}.hpi + return 0 +} + +# Install plugin. +for plugin in $* +do + installPlugin "$plugin" +done + +changed=1 +maxloops=5 + +# Install all dependencies. +while [ "$changed" == "1" ]; do + echo "Check for missing dependecies ..." + if [ $maxloops -lt 1 ] ; then + echo "Max loop count reached - probably a bug in this script: $0" + exit 1 + fi + ((maxloops--)) + changed=0 + for f in ${plugin_dir}/*.hpi ; do + # Without optionals. + # deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | awk -F ':' '{ print $1 }' | tr '\n' ' ' ) + # With optionals. + deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | awk -F ':' '{ print $1 }' | tr '\n' ' ' ) + for plugin in $deps; do + # if installPlugin returns 1 then 'changed' stays as is. (it means that the whole jenkins plugins state is not changed and in fact, nothing installed) + # if installPlugin returns 0 then changed=1 + installPlugin "$plugin" 1 && changed=1 + done + done +done + +# Fixing permissions. +chown -R ${owner} ${plugin_dir} + +# Restart Jenkins. +service jenkins restart diff --git a/murano-apps/Jenkins/package/Resources/scripts/site.pp b/murano-apps/Jenkins/package/Resources/scripts/site.pp index 2bc1525..fec4206 100644 --- a/murano-apps/Jenkins/package/Resources/scripts/site.pp +++ b/murano-apps/Jenkins/package/Resources/scripts/site.pp @@ -10,4 +10,8 @@ node default { ssl_key_file => '/etc/ssl/private/ssl-cert-snakeoil.key', manage_jenkins_jobs => false, } -} \ No newline at end of file + + package { 'unzip': + ensure => present + } +} diff --git a/murano-apps/Jenkins/package/Resources/upgrade_git_plugin.sh b/murano-apps/Jenkins/package/Resources/upgrade_git_plugin.sh deleted file mode 100644 index 15b6435..0000000 --- a/murano-apps/Jenkins/package/Resources/upgrade_git_plugin.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash - -# Plugin names are sorted in dependency order. -# The first ones doesn't have any dependencies while -# the next ones have the previous ones as the dependencies. -plugin_list="icon-shim - mailer - scm-api - token-macro - parameterized-trigger - junit - script-security - credentials - ssh-credentials - git-client - matrix-project - git" - -for plugin in $plugin_list -do - curl -L https://updates.jenkins-ci.org/latest/$plugin.hpi > /var/lib/jenkins/plugins/$plugin.hpi - chown jenkins:jenkins /var/lib/jenkins/plugins/$plugin.hpi - chmod 0644 /var/lib/jenkins/plugins/$plugin.hpi -done - -service jenkins restart