Improve application role testability

Remove different phases for application role as
those are supposed to run after each other anyway
from playbook and pre/post steps can be disabled
by other means.

Enable Helm override file generation customization by
delegating it to separate role.

Separate Helm file transfer to own module.

NOTE: install.yml with all Helm commands (shell/command)
is not idempotent and should be fixed with separate
commit.

Issue-ID: OOM-1655

Change-Id: Ib29773c4d64a0529c71c3f93c2af7265ae94059f
Signed-off-by: Samuli Silvius <s.silvius@partner.samsung.com>
diff --git a/ansible/roles/application/tasks/install.yml b/ansible/roles/application/tasks/install.yml
new file mode 100644
index 0000000..103ecc8
--- /dev/null
+++ b/ansible/roles/application/tasks/install.yml
@@ -0,0 +1,77 @@
+---
+- name: Helm init and upgrade
+  command: |
+     {{ helm_bin_dir }}/helm init
+     --upgrade
+     --skip-refresh
+  changed_when: true # init is always changed type of action
+
+# A correct way to implement this would be using --wait option in helm init invocation.
+# However, it does not work due to https://github.com/helm/helm/issues/4031 (fixed in newer helm release)
+- name: "Wait for helm upgrade to finish"
+  command: "{{ helm_bin_dir }}/helm version --tiller-connection-timeout 10"
+  register: result
+  until: result.rc == 0
+  delay: 10
+  retries: 12
+  changed_when: false # for idempotency
+
+- name: Get all helm repos
+  command: "{{ helm_bin_dir }}/helm repo list"
+  register: repos
+  changed_when: false # for idempotency
+
+- name: Remove stable repo
+  command: "{{ helm_bin_dir }}/helm repo remove stable"
+  changed_when: true # when executed its a changed type of action
+  when: "'stable' in repos.stdout"
+
+- name: Helm Serve
+  shell: "{{ helm_bin_dir }}/helm serve &"
+  async: 45
+  poll: 3 # wait 3sec to get a chance for some stderr
+  register: helm_serve
+  changed_when: "'address already in use' not in helm_serve.stderr"
+
+- name: List helm repos
+  command: "{{ helm_bin_dir }}/helm repo list"
+  register: helm_repo_list
+  changed_when: false # for idempotency
+  failed_when:
+    - helm_repo_list.rc > 0
+    - "'Error: no repositories to show' not in helm_repo_list.stderr"
+
+- name: Helm Add Repo
+  command: "{{ helm_bin_dir }}/helm repo add {{ helm_repository_name | mandatory }} {{ helm_repository_url | mandatory }}"
+  when: "'local' not in helm_repo_list.stdout"
+  changed_when: true # when executed its a changed type of action
+
+- name: Build local helm repository
+  make:
+    chdir: "{{ app_helm_charts_infra_directory }}"
+    target: "{{ item }}"
+  loop: "{{ app_helm_build_targets }}"
+  environment:
+    PATH: "{{ helm_bin_dir }}:{{ ansible_env.PATH }}"
+
+- name: Generate Helm application override file with custom role
+  include_role:
+    name: "{{ app_helm_override_role }}"
+  when: not app_skip_helm_override
+
+- name: Check for deploy plugin presence
+  stat:
+    path: '{{ helm_home_dir.stdout }}/plugins/deploy/deploy.sh'
+  register: deploy_plugin_presence
+
+- name: "Helm Install application {{ app_name }}"
+  command: >
+          {{ helm_bin_dir }}/helm
+          {{ 'deploy' if deploy_plugin_presence.stat.exists else 'install --name' }}
+          {{ app_helm_release_name }}
+          {{ helm_repository_name }}/{{ app_helm_chart_name }}
+          --namespace {{ app_kubernetes_namespace }}
+          {{ '' if app_skip_helm_override else '-f ' + app_helm_override_file }}
+  changed_when: true # when executed its a changed type of action
+  register: helm_install
+  failed_when: helm_install.stderr