aboutsummaryrefslogtreecommitdiffstats
path: root/roles/prepare
diff options
context:
space:
mode:
Diffstat (limited to 'roles/prepare')
-rw-r--r--roles/prepare/README.md28
-rw-r--r--roles/prepare/tasks/continue.yml15
-rw-r--r--roles/prepare/tasks/except.yml55
-rw-r--r--roles/prepare/tasks/exit.yml13
-rw-r--r--roles/prepare/tasks/main.yml93
-rw-r--r--roles/prepare/tasks/only.yml57
6 files changed, 261 insertions, 0 deletions
diff --git a/roles/prepare/README.md b/roles/prepare/README.md
new file mode 100644
index 0000000..2cdcd0e
--- /dev/null
+++ b/roles/prepare/README.md
@@ -0,0 +1,28 @@
+# Chained CI Prepare role
+
+This role prepare the settings before getting artifacts and run the playbook.
+It:
+ - Warn if log level is HIGH to avoid data leaking
+ - Check the step parameter is set
+ - prepare the `config` fact
+ - test `only` and `except` step parameters to limit when jobs are runned.
+ This will __SKIP__ this job if __ONE of__ the `except` condition is
+ successful __AND__ if __ALL__ the `only` conditions are failing. Those
+ conditions are testing environment variables like this:
+ - `VAR`: this test the presence of a variable that is not empty
+ - `VAR == value`: this test the exact value of a variable
+ - `VAR != value`: this test the exact difference of a variable.
+ - `VAR in [value1, value2]`: this test the exact value of a variable is a
+ set of possibilities
+
+## Example
+
+```
+except:
+ - "XXX in [aaa, aab]"
+ - "YYY"
+only:
+ - "AAA == yes"
+ - "BBB != no"
+ - "CCC in [pitet, possible]"
+```
diff --git a/roles/prepare/tasks/continue.yml b/roles/prepare/tasks/continue.yml
new file mode 100644
index 0000000..5d664c7
--- /dev/null
+++ b/roles/prepare/tasks/continue.yml
@@ -0,0 +1,15 @@
+---
+
+- name: we have to continue this role
+ debug:
+ msg: "{{ msg.split('\n') }}"
+ vars:
+ msg: |
+ **************************************************************************
+ ** We continue the play
+ ** REASON = '{{ condition }}'
+ **************************************************************************
+
+- name: Do not skip the run of the play
+ set_fact:
+ skip_run: false
diff --git a/roles/prepare/tasks/except.yml b/roles/prepare/tasks/except.yml
new file mode 100644
index 0000000..8d8abff
--- /dev/null
+++ b/roles/prepare/tasks/except.yml
@@ -0,0 +1,55 @@
+---
+# in this file, default variable value is '-666-', I hope no one will ever
+# test the number of the beast :)
+
+
+- name: Testing 'EXCEPT' condition
+ debug:
+ var: condition
+
+- name: if condition is only one word
+ block:
+ - name: check variable is present
+ include_tasks: exit.yml
+ when: lookup('env', condition)| default(False, true)
+ when: condition.split()| length == 1
+
+- name: if condition contains '=='
+ block:
+ - name: split condition with '=='
+ set_fact:
+ cond: "{{ (condition|replace(' == ', '==')).split('==') }}"
+ - debug: msg="{{ cond[1:]| join('==') }}"
+ - name: test condition
+ include_tasks: exit.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) == (
+ cond[1:]| join('=='))
+ when: condition is search('==')
+
+- name: if condition contains '!='
+ block:
+ - name: split condition with '!='
+ set_fact:
+ cond: "{{ (condition|replace(' != ', '!=')).split('!=') }}"
+ - name: test condition
+ include_tasks: exit.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) != (
+ cond[1:]| join('!='))
+ when: condition is search('!=')
+
+- name: if condition contains 'in'
+ block:
+ - name: split condition with ' in '
+ set_fact:
+ cond: "{{ condition.split(' in ') }}"
+ - name: split list
+ set_fact:
+ inlist: |
+ {{ (cond[1]|
+ replace(', ', ',')| replace(' ,', ',')|
+ replace(' ]', '') | replace(']', '')|
+ replace('[ ', '') | replace('[', '')).split(',') }}
+ - name: test condition
+ include_tasks: exit.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) in inlist
+ when: condition is search(' in ')
diff --git a/roles/prepare/tasks/exit.yml b/roles/prepare/tasks/exit.yml
new file mode 100644
index 0000000..58fb43d
--- /dev/null
+++ b/roles/prepare/tasks/exit.yml
@@ -0,0 +1,13 @@
+---
+
+- name: we have to end this role
+ debug:
+ msg: "{{ msg.split('\n') }}"
+ vars:
+ msg: |
+ **************************************************************************
+ ** We finish the play here
+ ** REASON = '{{ condition }}'
+ **************************************************************************
+
+- meta: end_play
diff --git a/roles/prepare/tasks/main.yml b/roles/prepare/tasks/main.yml
new file mode 100644
index 0000000..ce08540
--- /dev/null
+++ b/roles/prepare/tasks/main.yml
@@ -0,0 +1,93 @@
+---
+##
+# Warn if log level is high
+##
+- name: Echo running pipeline link
+ debug:
+ msg: "{{ msg.split('\n') }}"
+ verbosity: 3
+ vars:
+ msg: |
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ !! Log level is HIGH ! !!
+ !! Some sensitive data may be visible to everyone. !!
+ !! Don't forget to clean the task output ! !!
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+##
+# Check Step parameters
+##
+- name: check 'step' is set
+ fail:
+ msg: 'Step must be defined ! (use --extra-vars "step=test1")'
+ when: step is not defined
+
+##
+# Check the pod is not protected
+##
+- name: clean var
+ set_fact:
+ protected_pods: []
+ when: protected_pods|default() == None
+
+- name: check pod protection
+ fail:
+ msg: 'This pod is protected'
+ when:
+ inventory_hostname in protected_pods and
+ lookup( 'env', 'AREYOUSURE') != 'MAIS OUI !!!'
+
+##
+# Prepare the step config
+##
+- name: get default step parameters
+ set_fact:
+ config: >-
+ {{ gitlab.git_projects[
+ hostvars[inventory_hostname].scenario_steps[step].project] |
+ combine(hostvars[inventory_hostname].scenario_steps[step]) }}
+
+- name: merge step parameters
+ set_fact:
+ config: >-
+ {{ config| combine(
+ {'parameters': config.parameters|
+ combine(config.extra_parameters)}) }}
+ when: config.extra_parameters is defined
+
+##
+# Check if we must run this step - Must be run at the end of this role
+##
+
+- name: Set default skip_run value
+ set_fact:
+ skip_run: false
+
+- name: run except parameter
+ include_tasks: except.yml
+ loop: "{{ config.except }}"
+ loop_control:
+ loop_var: condition
+ label: "{{ condition }}"
+ when: config.except is defined
+
+- name: Set default skip_run value
+ set_fact:
+ skip_run: true
+ when: config.only is defined
+
+- name: run only parameter
+ include_tasks: only.yml
+ loop: "{{ config.only }}"
+ vars:
+ skip_all: false
+ loop_control:
+ loop_var: condition
+ label: "{{ condition }}"
+ when: config.only is defined
+
+- name: Skip if none of ONLY is successful
+ include_tasks: exit.yml
+ vars:
+ condition: "None of ONLY conditions are successful"
+ when: config.only is defined and skip_run
diff --git a/roles/prepare/tasks/only.yml b/roles/prepare/tasks/only.yml
new file mode 100644
index 0000000..893d32b
--- /dev/null
+++ b/roles/prepare/tasks/only.yml
@@ -0,0 +1,57 @@
+---
+# in this file, default variable value is '-666-', I hope no one will ever
+# test the number of the beast :)
+
+- name: test condition only if the previous failed
+ when: skip_run
+ block:
+ - name: Testing 'ONLY' condition
+ debug:
+ var: condition
+
+ - name: if condition is only one word
+ block:
+ - name: check variable is present
+ include_tasks: continue.yml
+ when: lookup('env', condition)| default(False, true)
+ when: condition.split()| length == 1
+
+ - name: if condition contains '=='
+ block:
+ - name: split condition with '=='
+ set_fact:
+ cond: "{{ (condition|replace(' == ', '==')).split('==') }}"
+ - debug: msg="{{ cond[1:]| join('==') }}"
+ - name: test condition
+ include_tasks: continue.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) == (
+ cond[1:]| join('=='))
+ when: condition is search('==')
+
+ - name: if condition contains '!='
+ block:
+ - name: split condition with '!='
+ set_fact:
+ cond: "{{ (condition|replace(' != ', '!=')).split('!=') }}"
+ - name: test condition
+ include_tasks: continue.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) != (
+ cond[1:]| join('!='))
+ when: condition is search('!=')
+
+ - name: if condition contains 'in'
+ block:
+ - name: split condition with ' in '
+ set_fact:
+ cond: "{{ condition.split(' in ') }}"
+ - name: split list
+ set_fact:
+ inlist: |
+ {{ (cond[1]|
+ replace(', ', ',')| replace(' ,', ',')|
+ replace(' ]', '') | replace(']', '')|
+ replace('[ ', '') | replace('[', '')).split(',') }}
+ - name: test condition
+ include_tasks: continue.yml
+ when: (lookup('env', cond[0])| default('-666-', true)) in inlist
+ when: condition is search(' in ')