From 834062b38b184367403b6a3f647bb2fe639a8acf Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 21 Mar 2017 18:17:47 +0100 Subject: [PATCH] library/roles/jenkins/master: Use the jenkins_plugin module to install the plugins. --- jenkins/master/defaults/main.yml | 71 ++++++++++++++------------ jenkins/master/tasks/main.yml | 87 ++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 64 deletions(-) diff --git a/jenkins/master/defaults/main.yml b/jenkins/master/defaults/main.yml index 29c071a..9d8a240 100644 --- a/jenkins/master/defaults/main.yml +++ b/jenkins/master/defaults/main.yml @@ -19,6 +19,7 @@ jenkins_sbt_launch_jars: jenkins_http_port: 8080 jenkins_http_host: 127.0.0.1 +jenkins_local_url: 'http://{{ jenkins_http_host }}:{{ jenkins_http_port }}' jenkins_dest: "/var/lib/jenkins" jenkins_webroot: /var/cache/jenkins/war jenkins_username: jenkins @@ -35,37 +36,41 @@ jenkins_cli_dest: "{{ jenkins_dest }}/jenkins-cli.jar" # Jenkins CLI destination jenkins_updates_dest: "{{ jenkins_dest }}/updates_jenkins.json" # Jenkins updates file jenkins_admin_user_pwd_file: "{{ jenkins_dest }}/.jenkins_admin_pwd" +jenkins_access_params: + url_username: '{{ jenkins_admin_user }}' + url_password: '{{ jenkins_admin_pwd }}' + url: '{{ jenkins_local_url }}' + jenkins_plugins: - - 'chucknorris' -# - 'configurationslicing' - - 'credentials' - - 'disk-usage' - - 'email-ext' - - 'embeddable-build-status' - - 'external-monitor-job' - - 'ldap' - - 'git' - - 'github' - - 'github-api' -# - 'global-build-stats' - - 'javadoc' - - 'job-dsl' -# - 'jobConfigHistory' - - 'mailer' - - 'maven-plugin' - - 'monitoring' - - 'extended-read-permission' - - 'dependency-queue-plugin' - - 'dependencyanalyzer' - - 'depgraph-view' - - 'progress-bar-column-plugin' - - 'publish-over-ftp-plugin' - - 'redmine-plugin' - - 'slave-utilization-plugin' - - 'sonar' - - 'ssh-credentials' - - 'ssh-slaves' - - 'subversion' -# - 'timestamper' - - 'translation' - - 'naginator' + chucknorris: + enabled: True + credentials: + enabled: True + disk-usage: + enabled: True + github: + enabled: True + github-api: + enabled: True + global-build-stats: + enabled: True + mailer: + enabled: True + maven-plugin: + enabled: True + monitoring: + enabled: True + extended-read-permission: + enabled: True + dependency-queue-plugin: + enabled: True + dependencyanalyzer: + enabled: True + depgraph-view: + enabled: True + progress-bar-column-plugin: + enabled: True + slave-utilization-plugin: + enabled: True + build-pipeline-plugin: + enabled: True diff --git a/jenkins/master/tasks/main.yml b/jenkins/master/tasks/main.yml index 34a4cfa..289fdc2 100644 --- a/jenkins/master/tasks/main.yml +++ b/jenkins/master/tasks/main.yml @@ -48,45 +48,70 @@ - name: Get Jenkins CLI get_url: url=http://localhost:{{ jenkins_http_port }}/jnlpJars/jenkins-cli.jar dest={{ jenkins_cli_dest }} mode=0440 - # Get latest Jenkins update file - - name: Get Jenkins updates - get_url: url=http://updates.jenkins-ci.org/update-center.json dest={{ jenkins_updates_dest }} thirsty=yes mode=0660 - register: jenkins_updates - - # Jenkins Update-center - - name: Update-center Jenkins - shell: "cat {{ jenkins_updates_dest }} | sed '1d;$d' | curl -X POST -H 'Accept: application/json' -d @- http://localhost:{{ jenkins_http_port }}/updateCenter/byId/default/postBack" - when: jenkins_updates | changed - # Create the Jenkins administrative user password file - name: Create the Jenkins administrative user password file copy: content={{ jenkins_admin_pwd }} dest={{ jenkins_admin_user_pwd_file }} mode=600 - # login as the Jenkins administrative user - - name: login as the Jenkins administrative user - command: java -jar {{ jenkins_cli_dest }} -s http://localhost:{{ jenkins_http_port }} login --username {{ jenkins_admin_user }} --password-file {{ jenkins_admin_user_pwd_file }} - when: (jenkins_plugins is defined) and (jenkins_updates | changed) + - name: Install plugins without a specific version + jenkins_plugin: name="{{ item.key }}" params='{{ jenkins_access_params }}' + register: my_jenkins_plugin_unversioned + when: 'version' not in item.value + with_dict: jenkins_plugins - # Install/update Jenkins plugins - - name: Install/update plugins - command: java -jar {{ jenkins_cli_dest }} -s http://localhost:{{ jenkins_http_port }} install-plugin {{ item }} - when: (jenkins_plugins is defined) and (jenkins_updates | changed) - with_items: '{{ jenkins_plugins }}' + - name: Install plugins with a specific version + jenkins_plugin: name="{{ item.key }}" version="{{ item.value['version'] }}" params='{{ jenkins_access_params }}' + register: my_jenkins_plugin_versioned + when: 'version' in item.value + with_dict: jenkins_plugins - # Wait for Jenkins to install plugins, assuming 10s should be sufficiant - - name: 120 seconds delay while installing plugins - wait_for: port={{ jenkins_http_port }} delay={{ jenkins_restart_delay }} - when: jenkins_updates | changed + - name: Initiate the jenkins_restart_required fact + set_fact: + jenkins_restart_required: no - # Safe-restart Jenkins - - name: Safe-restart Jenkins - command: java -jar {{ jenkins_cli_dest }} -s http://localhost:{{ jenkins_http_port }} safe-restart - when: jenkins_updates | changed + - name: Check if restart is required by any of the versioned plugins + set_fact: + jenkins_restart_required: yes + when: item.changed + with_items: my_jenkins_plugin_versioned.results - - name: logout the Jenkins administrative user - command: java -jar {{ jenkins_cli_dest }} -s http://localhost:{{ jenkins_http_port }} logout - when: (jenkins_plugins is defined) and (jenkins_updates | changed) - ignore_errors: True + - name: Check if restart is required by any of the unversioned plugins + set_fact: + jenkins_restart_required: yes + when: item.changed + with_items: my_jenkins_plugin_unversioned.results + + - name: Restart Jenkins if required + service: name=jenkins state= restarted + when: jenkins_restart_required + + - name: Wait for Jenkins to start up + uri: + url: '{{ jenkins_local_url }}' + status_code: 200 + timeout: 5 + register: jenkins_service_status + # Keep trying for 5 mins in 5 sec intervals + retries: 60 + delay: 5 + until: > + 'status' in jenkins_service_status and + jenkins_service_status['status'] == 200 + when: jenkins_restart_required + + - name: Reset the jenkins_restart_required fact + set_fact: + jenkins_restart_required: no + when: jenkins_restart_required + + - name: Plugin pinning + jenkins_plugin: name="{{ item.key }}" state="{{ 'pinned' if item.value['pinned'] else 'unpinned'}}" params='{{ jenkins_access_params }}' + when: 'pinned' in item.value + with_dict: jenkins_plugins + + - name: Plugin enabling + jenkins_plugin: name="{{ item.key }}" state="{{ 'enabled' if item.value['enabled'] else 'disabled'}}" params='{{ jenkins_access_params }}' + when: 'enabled' in item.value + with_dict: jenkins_plugins become: True become_user: '{{ jenkins_username }}'