- name: Create Worfkflow deployment directory {{ workflow_deployment_dir }}
  become: yes
  file: path={{ workflow_deployment_dir }}
    state=directory
    mode=0755
    owner={{ user }}
    group={{ group }}

- name: git checkout from Datalake github repo {{ datalake_repo }} branch {{ datalake_git_branch }}
  git: repo="{{ datalake_repo }}"
    dest="{{ datalake_source_dir }}"
    version="{{ datalake_git_branch }}"
  register: checkout
  tags: update
  become: yes
  become_user: "{{ user }}"

- name: open firewall ports for Workflow Engine
  firewalld:
    zone: public
    permanent: yes
    state: enabled
    immediate: yes
    rich_rule: rule family=ipv4 source address="{{ item }}" port port="{{ workflow_manager_grpc_port }}" protocol=tcp accept
  become: yes
  with_items:
    - "{{ sharing_subnets }}"

- name: Run Datalake maven build
  command: mvn clean install -Dmaven.test.skip=true chdir="{{ datalake_source_dir }}/"
  environment:
    MAVEN_OPTS: "-Xmx2048m"
  register: build
  tags: update
  become: yes
  become_user: "{{ user }}"

- name: Stop wf-controller
  command: systemctl daemon-reload
  notify: stop wf-controller
  become: yes

- name: Stop wf-manager
  command: systemctl daemon-reload
  notify: stop wf-manager
  become: yes

- name: Stop wf-worker
  command: systemctl daemon-reload
  notify: stop wf-worker
  become: yes


- name: Delete lib directories of the distributions
  file:
    state: absent
    path: "{{ workflow_deployment_dir }}/{{ item }}"
  with_items:
    - "WF-Manager-{{datalake_distribution_version}}/lib"
    - "WF-Controller-{{datalake_distribution_version}}/lib"
    - "WF-Worker-{{datalake_distribution_version}}/lib"
  become: yes
  become_user: "{{ user }}"
  ignore_errors: yes

- name: Copy Workflow distributions to Workflow deployment directory
  unarchive: "src={{ datalake_source_dir }}/{{ item }}
              dest={{ workflow_deployment_dir }}/ copy=no"
  with_items:
    - "data-orchestrator/workflow-engine/datasync-workflow-manager/target/WF-Manager-{{datalake_distribution_version}}-bin.zip"
    - "data-orchestrator/workflow-engine/workflow-engine-controller/target/WF-Controller-{{datalake_distribution_version}}-bin.zip"
    - "data-orchestrator/workflow-engine/workflow-engine-worker/target/WF-Worker-{{datalake_distribution_version}}-bin.zip"
  become: yes
  become_user: "{{ user }}"

- name: Copy Workflow Engine property files
  template: "src={{ item.name }}
            dest={{ workflow_deployment_dir }}/{{ item.dir }}/conf/{{ item.target }}
            owner={{ user }}
            group={{ group }}
            mode=\"u=rw,g=r,o=r\""
  with_items:
    - { name: wf-manager/application.properties.j2,
        dir: "WF-Manager-{{datalake_distribution_version}}",
        target: application.properties}
    - { name: wf-controller/application.properties.j2,
        dir: "WF-Controller-{{datalake_distribution_version}}",
        target: application.properties }
    - { name: wf-worker/application.properties.j2,
        dir: "WF-Worker-{{datalake_distribution_version}}",
        target: application.properties }

  become: yes
  become_user: "{{ user }}"

- name: Start wf-controller
  command: systemctl daemon-reload
  notify: start wf-controller
  become: yes

- name: Start wf-manager
  command: systemctl daemon-reload
  notify: start wf-manager
  become: yes

- name: Start wf-worker
  command: systemctl daemon-reload
  notify: start wf-worker
  become: yes