From 1285e8ffef6f95be134db22ca88b235e1e5bf1dd Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Wed, 25 Oct 2017 16:58:41 -0400 Subject: [PATCH 1/4] Add blocks around the different SCMs, for clarity purposes. --- awx/playbooks/project_update.yml | 184 +++++++++++++++---------------- 1 file changed, 89 insertions(+), 95 deletions(-) diff --git a/awx/playbooks/project_update.yml b/awx/playbooks/project_update.yml index 20c84b6664..6e1ff0b947 100644 --- a/awx/playbooks/project_update.yml +++ b/awx/playbooks/project_update.yml @@ -23,88 +23,91 @@ state: absent when: scm_delete_on_update|default('') - - name: update project using git and accept hostkey - git: - dest: "{{project_path|quote}}" - repo: "{{scm_url}}" - version: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - accept_hostkey: "{{scm_accept_hostkey}}" - #clone: "{{ scm_full_checkout }}" - #update: "{{ scm_full_checkout }}" - when: scm_type == 'git' and scm_accept_hostkey is defined - register: scm_result + - block: + - name: update project using git and accept hostkey + git: + dest: "{{project_path|quote}}" + repo: "{{scm_url}}" + version: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + accept_hostkey: "{{scm_accept_hostkey}}" + when: scm_accept_hostkey is defined + register: scm_result - - name: Set the git repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" + - name: Set the git repository version + set_fact: + scm_version: "{{ scm_result['after'] }}" + when: "'after' in scm_result" - - name: update project using git - git: - dest: "{{project_path|quote}}" - repo: "{{scm_url}}" - version: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - #clone: "{{ scm_full_checkout }}" - #update: "{{ scm_full_checkout }}" - when: scm_type == 'git' and scm_accept_hostkey is not defined - register: scm_result + - name: update project using git + git: + dest: "{{project_path|quote}}" + repo: "{{scm_url}}" + version: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + when: scm_accept_hostkey is not defined + register: scm_result - - name: Set the git repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" + - name: Set the git repository version + set_fact: + scm_version: "{{ scm_result['after'] }}" + when: "'after' in scm_result" + when: scm_type == 'git' - - name: update project using hg - hg: - dest: "{{project_path|quote}}" - repo: "{{scm_url|quote}}" - revision: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - #clone: "{{ scm_full_checkout }}" - #update: "{{ scm_full_checkout }}" + - block: + - name: update project using hg + hg: + dest: "{{project_path|quote}}" + repo: "{{scm_url|quote}}" + revision: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + register: scm_result + + - name: Set the hg repository version + set_fact: + scm_version: "{{ scm_result['after'] }}" + when: "'after' in scm_result" + + - name: parse hg version string properly + set_fact: + scm_version: "{{scm_version|regex_replace('^([A-Za-z0-9]+).*$', '\\1')}}" when: scm_type == 'hg' - register: scm_result - - name: Set the hg repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" + - block: + - name: update project using svn + subversion: + dest: "{{project_path|quote}}" + repo: "{{scm_url|quote}}" + revision: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + when: not scm_username|default('') + register: scm_result - - name: update project using svn - subversion: - dest: "{{project_path|quote}}" - repo: "{{scm_url|quote}}" - revision: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - #checkout: "{{ scm_full_checkout }}" - #update: "{{ scm_full_checkout }}" - when: scm_type == 'svn' and not scm_username|default('') - register: scm_result + - name: Set the svn repository version + set_fact: + scm_version: "{{ scm_result['after'] }}" + when: "'after' in scm_result" - - name: Set the svn repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" + - name: update project using svn with auth + subversion: + dest: "{{project_path|quote}}" + repo: "{{scm_url|quote}}" + revision: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + username: "{{scm_username|quote}}" + password: "{{scm_password|quote}}" + when: scm_username|default('') + register: scm_result - - name: update project using svn with auth - subversion: - dest: "{{project_path|quote}}" - repo: "{{scm_url|quote}}" - revision: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - username: "{{scm_username|quote}}" - password: "{{scm_password|quote}}" - #checkout: "{{ scm_full_checkout }}" - #update: "{{ scm_full_checkout }}" - when: scm_type == 'svn' and scm_username|default('') - register: scm_result + - name: Set the svn repository version + set_fact: + scm_version: "{{ scm_result['after'] }}" + when: "'after' in scm_result" - - name: Set the svn repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" + - name: parse subversion version string properly + set_fact: + scm_version: "{{scm_version|regex_replace('^.*Revision: ([0-9]+).*$', '\\1')}}" + when: scm_type == 'svn' - block: - name: Ensure the project directory is present @@ -120,33 +123,24 @@ project_path: "{{project_path}}" register: results + - name: Save Insights Version + set_fact: + scm_version: "{{results.version}}" + when: results is defined when: scm_type == 'insights' - - name: Save Insights Version - set_fact: - scm_version: "{{results.version}}" - when: scm_type == 'insights' and results is defined - - name: detect requirements.yml - stat: path={{project_path|quote}}/roles/requirements.yml - register: doesRequirementsExist + - block: + - name: detect requirements.yml + stat: path={{project_path|quote}}/roles/requirements.yml + register: doesRequirementsExist - - name: fetch galaxy roles from requirements.yml - command: ansible-galaxy install -r requirements.yml -p {{project_path|quote}}/roles/ --force - args: - chdir: "{{project_path|quote}}/roles" - when: doesRequirementsExist.stat.exists and scm_full_checkout|bool - - # format provided by ansible is ["Revision: 12345", "URL: ..."] - - name: parse subversion version string properly - set_fact: - scm_version: "{{scm_version|regex_replace('^.*Revision: ([0-9]+).*$', '\\1')}}" - when: scm_type == 'svn' - - - name: parse hg version string properly - set_fact: - scm_version: "{{scm_version|regex_replace('^([A-Za-z0-9]+).*$', '\\1')}}" - when: scm_type == 'hg' + - name: fetch galaxy roles from requirements.yml + command: ansible-galaxy install -r requirements.yml -p {{project_path|quote}}/roles/ --force + args: + chdir: "{{project_path|quote}}/roles" + when: doesRequirementsExist.stat.exists + when: scm_full_checkout|bool - name: Repository Version debug: msg="Repository Version {{ scm_version }}" From 871dc81da3c897c0c1d990ae3a1bc3b89563e289 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Wed, 25 Oct 2017 21:30:46 -0400 Subject: [PATCH 2/4] Avoid task duplication by using default(omit). --- awx/playbooks/project_update.yml | 36 +++----------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/awx/playbooks/project_update.yml b/awx/playbooks/project_update.yml index 6e1ff0b947..5dd0302b6d 100644 --- a/awx/playbooks/project_update.yml +++ b/awx/playbooks/project_update.yml @@ -30,22 +30,7 @@ repo: "{{scm_url}}" version: "{{scm_branch|quote}}" force: "{{scm_clean}}" - accept_hostkey: "{{scm_accept_hostkey}}" - when: scm_accept_hostkey is defined - register: scm_result - - - name: Set the git repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" - - - name: update project using git - git: - dest: "{{project_path|quote}}" - repo: "{{scm_url}}" - version: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - when: scm_accept_hostkey is not defined + accept_hostkey: "{{scm_accept_hostkey|default(omit)}}" register: scm_result - name: Set the git repository version @@ -74,29 +59,14 @@ when: scm_type == 'hg' - block: - - name: update project using svn - subversion: - dest: "{{project_path|quote}}" - repo: "{{scm_url|quote}}" - revision: "{{scm_branch|quote}}" - force: "{{scm_clean}}" - when: not scm_username|default('') - register: scm_result - - - name: Set the svn repository version - set_fact: - scm_version: "{{ scm_result['after'] }}" - when: "'after' in scm_result" - - name: update project using svn with auth subversion: dest: "{{project_path|quote}}" repo: "{{scm_url|quote}}" revision: "{{scm_branch|quote}}" force: "{{scm_clean}}" - username: "{{scm_username|quote}}" - password: "{{scm_password|quote}}" - when: scm_username|default('') + username: "{{scm_username|default(omit)}}" + password: "{{scm_password|default(omit)}}" register: scm_result - name: Set the svn repository version From c827e73dac6917ef5d768b5eb6b15520265039e4 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Thu, 2 Nov 2017 14:11:48 -0400 Subject: [PATCH 3/4] Update comments and task names. --- awx/playbooks/project_update.yml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/awx/playbooks/project_update.yml b/awx/playbooks/project_update.yml index 5dd0302b6d..e0cc788ec7 100644 --- a/awx/playbooks/project_update.yml +++ b/awx/playbooks/project_update.yml @@ -1,16 +1,18 @@ --- - # The following variables will be set by the runner of this playbook: # project_path: PROJECTS_DIR/_local_path_ -# scm_type: git|hg|svn +# scm_type: git|hg|svn|insights # scm_url: https://server/repo -# scm_branch: HEAD +# insights_url: Insights service URL (from configuration) +# scm_branch: branch/tag/revision (HEAD if unset) # scm_clean: true/false # scm_delete_on_update: true/false -# scm_username: username (only for svn) -# scm_password: password (only for svn) -# scm_accept_hostkey: true/false (only for git) +# scm_full_checkout: true (if for a job template run), false (if retrieving revision) +# scm_username: username (only for svn/insights) +# scm_password: password (only for svn/insights) +# scm_accept_hostkey: true/false (only for git, set automatically) # scm_revision: current revision in tower +# scm_revision_output: where to store gathered revision (temporary file) - hosts: all connection: local @@ -24,7 +26,7 @@ when: scm_delete_on_update|default('') - block: - - name: update project using git and accept hostkey + - name: update project using git git: dest: "{{project_path|quote}}" repo: "{{scm_url}}" @@ -59,7 +61,7 @@ when: scm_type == 'hg' - block: - - name: update project using svn with auth + - name: update project using svn subversion: dest: "{{project_path|quote}}" repo: "{{scm_url|quote}}" @@ -99,7 +101,6 @@ when: results is defined when: scm_type == 'insights' - - block: - name: detect requirements.yml stat: path={{project_path|quote}}/roles/requirements.yml From a2b18a9f6eead388d77459c2183d65d7a474641e Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Wed, 8 Nov 2017 18:29:59 -0500 Subject: [PATCH 4/4] Add test to short-circuit checkout if revision is already checked out. Move role checkout to a separate play, to work with this. --- awx/playbooks/project_update.yml | 42 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/awx/playbooks/project_update.yml b/awx/playbooks/project_update.yml index e0cc788ec7..2564158bad 100644 --- a/awx/playbooks/project_update.yml +++ b/awx/playbooks/project_update.yml @@ -26,6 +26,22 @@ when: scm_delete_on_update|default('') - block: + - name: check repo using git + git: + dest: "{{project_path|quote}}" + repo: "{{scm_url}}" + version: "{{scm_branch|quote}}" + force: "{{scm_clean}}" + update: false + clone: false + register: repo_check + when: scm_full_checkout|default('') + ignore_errors: true + + - name: break if already checked out + meta: end_play + when: scm_full_checkout|default('') and repo_check|succeeded and repo_check.before == scm_branch + - name: update project using git git: dest: "{{project_path|quote}}" @@ -101,6 +117,22 @@ when: results is defined when: scm_type == 'insights' + + - name: Repository Version + debug: msg="Repository Version {{ scm_version }}" + when: scm_version is defined + + - name: Write Repository Version + copy: + dest: "{{ scm_revision_output }}" + content: "{{ scm_version }}" + when: scm_version is defined and scm_revision_output is defined + +- hosts: all + connection: local + gather_facts: false + tasks: + - block: - name: detect requirements.yml stat: path={{project_path|quote}}/roles/requirements.yml @@ -112,13 +144,3 @@ chdir: "{{project_path|quote}}/roles" when: doesRequirementsExist.stat.exists when: scm_full_checkout|bool - - - name: Repository Version - debug: msg="Repository Version {{ scm_version }}" - when: scm_version is defined - - - name: Write Repository Version - copy: - dest: "{{ scm_revision_output }}" - content: "{{ scm_version }}" - when: scm_version is defined and scm_revision_output is defined \ No newline at end of file