Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sonar plugin installer more idempotent #361

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion roles/add-sonar-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ This role uses the OpenShift command-line tool to upload a JAR based SonarQube p
# Testing
* Deploy a [Labs CI/CD environment](https://github.com/rht-labs/labs-ci-cd)
* Apply the role using the test playbook: `ansible-playbook -i inventory test.yml
* Verify deployment after SonarQube restarts
* Verify deployment after SonarQube restarts
* Apply the inventory a second time and ensure that SonarQube continues to start
* Multiple instances of the same plugin will cause SonarQube to fail on startup
39 changes: 29 additions & 10 deletions roles/add-sonar-plugin/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,46 @@
- name: Get JAR name
set_fact:
jar_name: "{{ plugin_url.split('/')[-1] }}"

- name: Download the SonarQube plugin
get_url:
url: "{{ plugin_url }}"
dest: "/tmp/{{ jar_name }}"
mode: 0440
plugin_name: "{{ plugin_url.split('/')[-1] | regex_replace('-([0-9]+\\.?)+\\.jar', '') }}"

## The Jinja2 template and the Go template in the following task conflict,
## so the weird double-curly-braces surrounded by double-curly-braces is required.
- name: Find sonarqube pod
command: >-
oc get pods --namespace={{ namespace }} -l="app==sonarqube"
oc get pods --namespace={{ namespace }} -l='app==sonarqube'
-o jsonpath="{.items[0].metadata.name}"
register: pod_name_lines
register: pod_name

- name: Extract pod name
set_fact:
pod_name: "{{ pod_name_lines.stdout }}"
pod_name: "{{ pod_name.stdout }}"

- name: Check for existing plugin install_location
command: >-
oc exec -i --namespace={{ namespace }} {{ pod_name }} -- find {{ install_location }}/ -type f -name '{{ plugin_name }}*jar'
register: existing_plugin

- name: Create tempfile for sonar plugin list
tempfile:
state: file
suffix: temp
register: plugin_list_tempfile

- name: Download the SonarQube plugin
get_url:
url: "{{ plugin_url }}"
dest: "{{ plugin_list_tempfile }}"
when: existing_plugin is not defined or not existing_plugin.stdout.endswith(jar_name)

- name: Delete any existing matching plugins
command: >-
oc exec --namespace={{ namespace }} {{ pod_name }} -- find {{ install_location }}/ -type f -name '{{ plugin_name }}*jar' -delete
when: existing_plugin is defined and not existing_plugin.stdout.endswith(jar_name)

- name: Copy plugin to SonarQube pod
command: "oc cp /tmp/{{ jar_name }} {{ namespace }}/{{ pod_name }}:{{ install_location }}/{{ jar_name }}"
when: existing_plugin is not defined or not existing_plugin.stdout.endswith(jar_name)

- name: Redeploy SonarQube
command: "oc rollout latest --namespace={{ namespace }} {{ pod_prefix }}"
command: "oc rollout latest --namespace={{ namespace }} {{ pod_prefix }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have seen this failing at random times. A few suggestions:

  1. Before starting the steps above, wait for the current deployment to be "stable" to ensure successful install of plugins
  2. Only do another "rollout" if the plugin was successfully installed above
  3. Consider using a different approach than "rollout" since you're not really changing the image - i.e.: scale-down/scale-up, check for successful scale down/up, etc. to avoid a failed "rollout"

when: existing_plugin is not defined or not existing_plugin.stdout.endswith(jar_name)
4 changes: 2 additions & 2 deletions roles/add-sonar-plugin/tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
vars:
plugin_url: https://github.com/SonarOpenCommunity/sonar-cxx/releases/download/cxx-1.2.2/sonar-cxx-plugin-1.2.2.1653.jar
install_location: /opt/sonarqube/extensions/plugins
namespace: deven-role-test
pod_prefix: sonarqube
namespace: test-ci-cd
pod_prefix: sonarqube