---
version: '2.0'
name: tripleo.rename_ceph_ansible_fetch_directory.v1
description: TripleO rename ceph-ansible fetch directory backup in Swift Workflow v1

workflows:
  check_and_rename:
    description: >
      If the name of the tarball containing the ceph-ansible fetch directory
      matches the format found in Queens, then rename it to match the format
      expected in Rocky. I.e if temporary_dir-20180915-191756.tar.gz is found
      then rename it to temporary_dir.tar.gz. This workflow is idempotent and
      should be run before upgrading from Queens to Rocky.
    input:
      - container: overcloud
      - queue_name: tripleo
      - container_suffix: "_ceph_ansible_fetch_dir"
      - swift_tar: "temporary_dir.tar.gz"
    tags:
      - tripleo-common-managed
    tasks:
      swift_container:
        publish:
          swift_container: "<% $.container %><% $.container_suffix %>"
        on-complete: verify_container_exists

      verify_container_exists:
        action: swift.head_container container=<% $.swift_container %>
        on-success: get_object_list
        on-error: verify_container_exists_set_status_failed

      get_object_list:
        # the get_container result[1] will be a list of objects (even if list is empty)
        action: swift.get_container container=<% $.swift_container %>
        publish:
          object_list: <% task().result[1] %>
        on-success: set_need_rename
        on-error: get_object_list_set_status_failed

      set_need_rename:
        # if the list is empty, do not rename; otherwise check if name matches queens pattern
        publish:
          need_rename: <% switch($.object_list.len() = 0 => false, $.object_list.len() = 1 => $.object_list.first().get("name").matches("temporary_dir-[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])-(2[0-3]|[01][0-9])[0-5][0-9][0-5][0-9].tar.gz")) %>
        on-success: switch_on_need_rename
        on-error: set_need_rename_set_status_failed

      switch_on_need_rename:
        on-success:
          - copy_to_new_name: <% $.need_rename = true %>
          - set_status_success: <% $.need_rename != true %>

      copy_to_new_name:
        action: swift.copy_object container=<% $.swift_container %> obj=<% $.object_list.first().get("name") %> destination=<% concat($.swift_container, '/', $.swift_tar) %>
        on-success: remove_old_name
        on-error: copy_to_new_name_set_status_failed

      remove_old_name:
        action: swift.delete_object container=<% $.swift_container %> obj=<% $.object_list.first().get("name") %>
        on-success: set_status_success
        on-error: remove_old_name_set_status_failed

      set_status_success:
        on-success: notify_zaqar
        publish:
          status: SUCCESS
          message: <% switch($.need_rename = true => "The backup of the ceph-ansible fetch directory was renamed", $.need_rename != true => "The backup of the ceph-ansible fetch directory did not need to be renamed") %>

      verify_container_exists_set_status_failed:
        on-success: notify_zaqar
        publish:
          status: FAILED
          message: <% task(verify_container_exists).result %>

      get_object_list_set_status_failed:
        on-success: notify_zaqar
        publish:
          status: FAILED
          message: <% task(get_object_list).result %>

      set_need_rename_set_status_failed:
        on-success: notify_zaqar
        publish:
          status: FAILED
          message: <% task(set_need_rename).result %>

      copy_to_new_name_set_status_failed:
        on-success: notify_zaqar
        publish:
          status: FAILED
          message: <% task(copy_to_new_name).result %>

      remove_old_name_set_status_failed:
        on-success: notify_zaqar
        publish:
          status: FAILED
          message: <% task(remove_old_name).result %>

      notify_zaqar:
        workflow: tripleo.messaging.v1.send
        input:
          queue_name: <% $.queue_name %>
          type: <% execution().name %>
          status: <% $.get('status', 'SUCCESS') %>
          message: <% $.get('message', '') %>
          execution: <% execution() %>
