From 436c7d6829b585bdee9c23ec723ca0aa9bb9b2fa Mon Sep 17 00:00:00 2001
From: Andrea Dell'Amico <adellam@isti.cnr.it>
Date: Wed, 27 Sep 2017 19:10:37 +0200
Subject: [PATCH] smartgears distribution: Use maven_artifact to download the
 distribution file.

---
 smartgears/smartgears/defaults/main.yml       | 11 ++++++++--
 .../smartgears/tasks/smartgears-app.yml       | 22 ++++++++++++++-----
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/smartgears/smartgears/defaults/main.yml b/smartgears/smartgears/defaults/main.yml
index c114dd8..630c8d8 100644
--- a/smartgears/smartgears/defaults/main.yml
+++ b/smartgears/smartgears/defaults/main.yml
@@ -17,9 +17,16 @@ smartgears_downloads_dir: '{{ d4science_user_home }}/downloads'
 smartgears_instance_path: '{{ smartgears_user_home }}/tomcat'
 smartgears_install_path: '{{ smartgears_user_home }}/SmartGears'
 smartgears_distribution_version: 2.1.2-4.6.1-146408
-smartgears_file: 'smartgears-distribution-{{ smartgears_distribution_version }}.tar.gz'
+#smartgears_file: 'smartgears-distribution-{{ smartgears_distribution_version }}.tar.gz'
 smartgears_global_base_url: 'http://maven.research-infrastructures.eu/nexus/content/repositories/{{ gcube_repository }}'
-smartgears_url: 'http://maven.research-infrastructures.eu/nexus/content/repositories/{{ smartgears_gcube_repository }}/org/gcube/distribution/smartgears-distribution/{{ smartgears_distribution_version }}/{{ smartgears_file }}'
+#smartgears_url: 'http://maven.research-infrastructures.eu/nexus/content/repositories/{{ smartgears_gcube_repository }}/org/gcube/distribution/smartgears-distribution/{{ smartgears_distribution_version }}/{{ smartgears_file }}'
+smartgears_url: 'http://maven.research-infrastructures.eu/nexus/content/repositories/{{ smartgears_gcube_repository }}'
+
+smartgears_name: smartgears-distribution
+smartgears_group_id: org.gcube.distribution
+smartgears_extension: tar.gz
+smartgears_file: '{{ smartgears_name }}-{{ smartgears_distribution_version }}.{{ smartgears_extension }}'
+
 smartgears_mode: online
 smartgears_application_mode: online
 # Production infra
diff --git a/smartgears/smartgears/tasks/smartgears-app.yml b/smartgears/smartgears/tasks/smartgears-app.yml
index b3d6c6f..28efd9a 100644
--- a/smartgears/smartgears/tasks/smartgears-app.yml
+++ b/smartgears/smartgears/tasks/smartgears-app.yml
@@ -6,25 +6,35 @@
 - name: Remove the smartgears distribution archive to force un upgrade
   file: dest={{ item }} state=absent
   with_items:
-    - '{{ smartgears_user_home }}/{{ smartgears_file }}'
+    - '{{ smartgears_downloads_dir }}/{{ smartgears_file }}'
     - '{{ smartgears_user_home }}/smartgears-distribution-{{ smartgears_distribution_version }}'
   when: smartgears_force_install
   tags: [ 'smartgears', 'tomcat' ]
 
+- name: Get the smartgears distribution
+  become: True
+  become_user: '{{ smartgears_user }}'
+  maven_artifact: artifact_id={{ smartgears_name }} version={{ smartgears_distribution_version | default('latest') }} group_id={{ smartgears_group_id }} extension={{ smartgears_extension | default('tar.gz') }} repository_url={{ smartgears_url }} dest={{ smartgears_downloads_dir }}/{{ smartgears_file }}
+  with_items: '{{ tomcat_m_instances }}'
+  register: smartgears_download 
+  when: not item.skip_smartgears
+  tags: [ 'smartgears', 'tomcat' ]
+
 - name: Unarchive the smartgears distribution
   become: True
   become_user: '{{ smartgears_user }}'
-  unarchive: copy=no src={{ smartgears_url }} dest={{ smartgears_user_home }} creates={{ smartgears_user_home }}/smartgears-distribution-{{ smartgears_distribution_version }}
-  register: new_smartgears_distribution
+  unarchive: remote_src=yes src={{ smartgears_downloads_dir }}/{{ smartgears_file }} dest={{ smartgears_user_home }}
   with_items: '{{ tomcat_m_instances }}'
-  when: not item.skip_smartgears
+  when:
+    - not item.skip_smartgears
+    - ( smartgears_download | changed )
   tags: [ 'smartgears', 'tomcat' ]
 
 - name: Stop the smartgears container before launching the upgrade script
   become: True
   become_user: '{{ smartgears_user }}'
   shell: /home/{{ smartgears_user }}/stopContainer.sh
-  when: smartgears_upgrade
+  when: smartgears_upgrade or ( smartgears_download | changed )
   tags: [ 'smartgears', 'tomcat', 'stop_smartgears_service' ]
 
 - name: Install the additional tomcat default file
@@ -46,7 +56,7 @@
   shell: cd {{ item.user_home }}/smartgears-distribution-{{ smartgears_distribution_version }} ; . /{{ item.user_home }}/.bashrc  ; ./install -s tomcat ; chown -R gcube:gcube '{{ item.user_home }}/smartgears-distribution-{{ smartgears_distribution_version }}' '{{ item.user_home }}/SmartGears/' '{{ item.user_home }}/tomcat/lib/' '{{ item.user_home }}/tomcat/webapps/'
   with_items: '{{ tomcat_m_instances }}'
   when:
-    - ( new_smartgears_distribution | changed )
+    - ( smartgears_download | changed )
     - not item.skip_smartgears
   register: install_smartgears_app
   notify: Restart smartgears