From db24cb31185c0de87bec171229af5967146564da Mon Sep 17 00:00:00 2001 From: "aashiq.jacob@ibm.com" Date: Wed, 19 Feb 2025 09:38:31 +0530 Subject: [PATCH 1/2] chore: add the scripts back due to a bug in projects --- .../migration-pre-ansible-playbook.yaml | 103 ++++++++++++ .../migration-pre-ansible-playbook.yaml | 103 ++++++++++++ .../migration-pre-ansible-playbook.yaml | 138 ++++++++++++++++ .../migration-pre-ansible-playbook.yaml | 147 ++++++++++++++++++ .../migration-pre-ansible-playbook.yaml | 147 ++++++++++++++++++ 5 files changed, 638 insertions(+) create mode 100644 patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml create mode 100644 patterns/roks/scripts/migration-pre-ansible-playbook.yaml create mode 100644 patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml create mode 100644 patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml create mode 100644 patterns/vsi/scripts/migration-pre-ansible-playbook.yaml diff --git a/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml b/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml new file mode 100644 index 000000000..514e4b9d6 --- /dev/null +++ b/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml @@ -0,0 +1,103 @@ +- name: migration pre playbook + hosts: localhost + tasks: + - name: get running ansible env variables + set_fact: + config_crn_token: "{{ lookup('env', 'config_crn_token') }}" + project_id: "{{ lookup('env', 'project_id') }}" + config_id: "{{ lookup('env', 'config_id') }}" + project_url: "{{ lookup('env', 'project_url') }}" + - name: Get Statefile + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" + method: GET + headers: + Authorization: "Bearer {{ config_crn_token }}" + register: result + - name: Set Statefile + set_fact: + statefile: "{{ result.json | string }}" + - name: Creating script + copy: + dest: "script.sh" + content: | + #!/bin/bash + STATE="$(cat terraform.tfstate.tmp)" + CLUSTER_LIST=() + while IFS='' read -r line; do CLUSTER_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | .index_key') + WORKER_POOLS=() + while IFS='' read -r line; do WORKER_POOLS+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_worker_pool") and (.mode == "managed") and (.name == "pool")) | .instances[] | .index_key') + ADDRESS_LIST="$(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .module')" + + for i in "${!CLUSTER_LIST[@]}"; do + SOURCE="${ADDRESS_LIST}.ibm_container_vpc_cluster.cluster[\"${CLUSTER_LIST[$i]}\"]" + DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_cluster.cluster[0]" + if [ -n "${CLUSTER_LIST[$i]}" ] && [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$SOURCE, $DESTINATION") + fi + + ADDONS_RESOURCE=$(echo "$STATE" | jq -r --arg CLUSTER "${CLUSTER_LIST[$i]}" '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | select(.index_key == $CLUSTER) | .index_key') + if [ -n "$ADDONS_RESOURCE" ]; then + ADDONS_SOURCE="${ADDRESS_LIST}.ibm_container_addons.addons[\"${CLUSTER_LIST[$i]}\"]" + ADDONS_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_addons.addons" + if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$ADDONS_SOURCE, $ADDONS_DESTINATION") + fi + fi + + for x in "${!WORKER_POOLS[@]}"; do + if [[ "${WORKER_POOLS[$x]}" == *"${CLUSTER_LIST[$i]}"* ]]; then + pool_name=${WORKER_POOLS[$x]//"${CLUSTER_LIST[$i]}-"/} + WORKER_POOL_SOURCE="${ADDRESS_LIST}.ibm_container_vpc_worker_pool.pool[\"${WORKER_POOLS[$x]}\"]" + WORKER_POOL_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_worker_pool.pool[\"$pool_name\"]" + if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$WORKER_POOL_SOURCE, $WORKER_POOL_DESTINATION") + fi + fi + done + done + for mv in "${!MOVED_PARAMS[@]}"; do + echo "${MOVED_PARAMS[$mv]}" + done + + - name: Create state file + copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" + - name: Run the script + ansible.builtin.script: + cmd: ./script.sh + register: move_list + - name: Initiate Commands List + set_fact: + commands: [] + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Add new JSON Objects to List + set_fact: + commands: "{{ commands + + [{ 'command': 'state mv', + 'command_params': item, + 'command_name': 'Move' + count|string, + 'command_onerror': 'abort'}] }}" + loop: "{{ move_list.stdout_lines }}" + loop_control: + index_var: count + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Create complete JSON body + set_fact: + commands: "{{ + { 'commands': commands , + 'operation_name': 'workspace Command', + 'description': 'Executing command'} + }}" + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Run Terraform commands + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" + method: POST + headers: + Authorization: "Bearer {{ config_crn_token }}" + Content-Type: application/json + body: "{{ commands }}" + body_format: json + status_code: [200, 202] + register: result + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' diff --git a/patterns/roks/scripts/migration-pre-ansible-playbook.yaml b/patterns/roks/scripts/migration-pre-ansible-playbook.yaml new file mode 100644 index 000000000..514e4b9d6 --- /dev/null +++ b/patterns/roks/scripts/migration-pre-ansible-playbook.yaml @@ -0,0 +1,103 @@ +- name: migration pre playbook + hosts: localhost + tasks: + - name: get running ansible env variables + set_fact: + config_crn_token: "{{ lookup('env', 'config_crn_token') }}" + project_id: "{{ lookup('env', 'project_id') }}" + config_id: "{{ lookup('env', 'config_id') }}" + project_url: "{{ lookup('env', 'project_url') }}" + - name: Get Statefile + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" + method: GET + headers: + Authorization: "Bearer {{ config_crn_token }}" + register: result + - name: Set Statefile + set_fact: + statefile: "{{ result.json | string }}" + - name: Creating script + copy: + dest: "script.sh" + content: | + #!/bin/bash + STATE="$(cat terraform.tfstate.tmp)" + CLUSTER_LIST=() + while IFS='' read -r line; do CLUSTER_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | .index_key') + WORKER_POOLS=() + while IFS='' read -r line; do WORKER_POOLS+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_worker_pool") and (.mode == "managed") and (.name == "pool")) | .instances[] | .index_key') + ADDRESS_LIST="$(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .module')" + + for i in "${!CLUSTER_LIST[@]}"; do + SOURCE="${ADDRESS_LIST}.ibm_container_vpc_cluster.cluster[\"${CLUSTER_LIST[$i]}\"]" + DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_cluster.cluster[0]" + if [ -n "${CLUSTER_LIST[$i]}" ] && [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$SOURCE, $DESTINATION") + fi + + ADDONS_RESOURCE=$(echo "$STATE" | jq -r --arg CLUSTER "${CLUSTER_LIST[$i]}" '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | select(.index_key == $CLUSTER) | .index_key') + if [ -n "$ADDONS_RESOURCE" ]; then + ADDONS_SOURCE="${ADDRESS_LIST}.ibm_container_addons.addons[\"${CLUSTER_LIST[$i]}\"]" + ADDONS_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_addons.addons" + if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$ADDONS_SOURCE, $ADDONS_DESTINATION") + fi + fi + + for x in "${!WORKER_POOLS[@]}"; do + if [[ "${WORKER_POOLS[$x]}" == *"${CLUSTER_LIST[$i]}"* ]]; then + pool_name=${WORKER_POOLS[$x]//"${CLUSTER_LIST[$i]}-"/} + WORKER_POOL_SOURCE="${ADDRESS_LIST}.ibm_container_vpc_worker_pool.pool[\"${WORKER_POOLS[$x]}\"]" + WORKER_POOL_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_worker_pool.pool[\"$pool_name\"]" + if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then + MOVED_PARAMS+=("$WORKER_POOL_SOURCE, $WORKER_POOL_DESTINATION") + fi + fi + done + done + for mv in "${!MOVED_PARAMS[@]}"; do + echo "${MOVED_PARAMS[$mv]}" + done + + - name: Create state file + copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" + - name: Run the script + ansible.builtin.script: + cmd: ./script.sh + register: move_list + - name: Initiate Commands List + set_fact: + commands: [] + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Add new JSON Objects to List + set_fact: + commands: "{{ commands + + [{ 'command': 'state mv', + 'command_params': item, + 'command_name': 'Move' + count|string, + 'command_onerror': 'abort'}] }}" + loop: "{{ move_list.stdout_lines }}" + loop_control: + index_var: count + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Create complete JSON body + set_fact: + commands: "{{ + { 'commands': commands , + 'operation_name': 'workspace Command', + 'description': 'Executing command'} + }}" + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Run Terraform commands + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" + method: POST + headers: + Authorization: "Bearer {{ config_crn_token }}" + Content-Type: application/json + body: "{{ commands }}" + body_format: json + status_code: [200, 202] + register: result + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' diff --git a/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml new file mode 100644 index 000000000..06c25372e --- /dev/null +++ b/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml @@ -0,0 +1,138 @@ +- name: migration pre playbook + hosts: localhost + tasks: + - name: get running ansible env variables + set_fact: + config_crn_token: "{{ lookup('env', 'config_crn_token') }}" + project_id: "{{ lookup('env', 'project_id') }}" + config_id: "{{ lookup('env', 'config_id') }}" + project_url: "{{ lookup('env', 'project_url') }}" + - name: Get Statefile + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" + method: GET + headers: + Authorization: "Bearer {{ config_crn_token }}" + register: result + - name: Set Statefile + set_fact: + statefile: "{{ result.json | string }}" + - name: Creating script + copy: + dest: "script.sh" + content: | + #!/bin/bash + STATE="$(cat terraform.tfstate.tmp)" + + SUBNET_LIST=() + while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "data") and (.name == "vpc_by_id")) | .instances[0] | .attributes | .subnets[] | .id') + ADDRESS_LIST=() + while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') + + for i in "${!SUBNET_LIST[@]}"; do + for j in "${!ADDRESS_LIST[@]}"; do + VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" + subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "data") and (.name == "vpc_by_id")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') + vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') + VSI_LIST=() + IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" + for x in "${!VSI_LIST[@]}"; do + SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" + DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$SOURCE, $DESTINATION") + fi + if [ -n "${VSI_LIST[$x]}" ]; then + VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') + fi + if [ -n "${VSI_LIST[$x]}" ]; then + FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" + fi + if [ -n "$FIP_RESOURCES" ]; then + FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" + FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") + fi + fi + str="${VSI_LIST[$x]}" + lastIndex=$(echo "$str" | awk '{print length}') + for ((l = lastIndex; l >= 0; l--)); do + if [[ "${str:$l:1}" == "-" ]]; then + str="${str::l}" + break + fi + done + if [ -n "$VOL_NAMES" ]; then + VOL_ADDRESS_LIST=() + while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') + VOL_NAME=() + IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" + for a in "${!VOL_NAME[@]}"; do + for b in "${!VOL_ADDRESS_LIST[@]}"; do + VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" + vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') + VOL_LIST=() + IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" + for c in "${!VOL_LIST[@]}"; do + if [ -n "${VOL_LIST[$c]}" ]; then + VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" + test="${VOL_LIST[$c]/$str/}" + vol=$(echo "$test" | cut -d"-" -f3-) + VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" + if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") + fi + fi + done + done + done + fi + done + done + done + for ab in "${!MOVED_PARAMS[@]}"; do + echo "${MOVED_PARAMS[$ab]}" + done + + - name: Create state file + copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" + - name: Run the script + ansible.builtin.script: + cmd: ./script.sh + register: move_list + - name: Initiate Commands List + set_fact: + commands: [] + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Add new JSON Objects to List + set_fact: + commands: "{{ commands + + [{ 'command': 'state mv', + 'command_params': item, + 'command_name': 'Move' + count|string, + 'command_onerror': 'abort'}] }}" + loop: "{{ move_list.stdout_lines }}" + loop_control: + index_var: count + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Create complete JSON body + set_fact: + commands: "{{ + { 'commands': commands , + 'operation_name': 'workspace Command', + 'description': 'Executing command'} + }}" + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Run Terraform commands + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" + method: POST + headers: + Authorization: "Bearer {{ config_crn_token }}" + Content-Type: application/json + body: "{{ commands }}" + body_format: json + status_code: [200, 202] + register: result + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' diff --git a/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml new file mode 100644 index 000000000..67931703b --- /dev/null +++ b/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml @@ -0,0 +1,147 @@ +- name: migration pre playbook + hosts: localhost + tasks: + - name: get running ansible env variables + set_fact: + config_crn_token: "{{ lookup('env', 'config_crn_token') }}" + project_id: "{{ lookup('env', 'project_id') }}" + config_id: "{{ lookup('env', 'config_id') }}" + project_url: "{{ lookup('env', 'project_url') }}" + - name: Get Statefile + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" + method: GET + headers: + Authorization: "Bearer {{ config_crn_token }}" + register: result + - name: Set Statefile + set_fact: + statefile: "{{ result.json | string }}" + - name: Creating script + copy: + dest: "script.sh" + content: | + #!/bin/bash + STATE="$(cat terraform.tfstate.tmp)" + + SUBNET_LIST=() + + ADDRESS_LIST=() + while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') + + VPC_ADDRESS_LIST=() + while IFS='' read -r line; do VPC_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "managed") and (.name == "vpc")) | .module') + + for abc in "${!VPC_ADDRESS_LIST[@]}"; do + while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | .id') + done + + for i in "${!SUBNET_LIST[@]}"; do + for j in "${!ADDRESS_LIST[@]}"; do + for abc in "${!VPC_ADDRESS_LIST[@]}"; do + VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" + subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') + vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') + VSI_LIST=() + IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" + for x in "${!VSI_LIST[@]}"; do + SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" + DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$SOURCE, $DESTINATION") + fi + if [ -n "${VSI_LIST[$x]}" ]; then + VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') + fi + if [ -n "${VSI_LIST[$x]}" ]; then + FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" + fi + if [ -n "$FIP_RESOURCES" ]; then + FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" + FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") + fi + fi + str="${VSI_LIST[$x]}" + lastIndex=$(echo "$str" | awk '{print length}') + for ((l = lastIndex; l >= 0; l--)); do + if [[ "${str:$l:1}" == "-" ]]; then + str="${str::l}" + break + fi + done + if [ -n "$VOL_NAMES" ]; then + VOL_ADDRESS_LIST=() + while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') + VOL_NAME=() + IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" + for a in "${!VOL_NAME[@]}"; do + for b in "${!VOL_ADDRESS_LIST[@]}"; do + VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" + vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') + VOL_LIST=() + IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" + for c in "${!VOL_LIST[@]}"; do + if [ -n "${VOL_LIST[$c]}" ]; then + VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" + test="${VOL_LIST[$c]/$str/}" + vol=$(echo "$test" | cut -d"-" -f3-) + VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" + if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") + fi + fi + done + done + done + fi + done + done + done + done + for ab in "${!MOVED_PARAMS[@]}"; do + echo "${MOVED_PARAMS[$ab]}" + done + + - name: Create state file + copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" + - name: Run the script + ansible.builtin.script: + cmd: ./script.sh + register: move_list + - name: Initiate Commands List + set_fact: + commands: [] + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Add new JSON Objects to List + set_fact: + commands: "{{ commands + + [{ 'command': 'state mv', + 'command_params': item, + 'command_name': 'Move' + count|string, + 'command_onerror': 'abort'}] }}" + loop: "{{ move_list.stdout_lines }}" + loop_control: + index_var: count + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Create complete JSON body + set_fact: + commands: "{{ + { 'commands': commands , + 'operation_name': 'workspace Command', + 'description': 'Executing command'} + }}" + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Run Terraform commands + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" + method: POST + headers: + Authorization: "Bearer {{ config_crn_token }}" + Content-Type: application/json + body: "{{ commands }}" + body_format: json + status_code: [200, 202] + register: result + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' diff --git a/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml new file mode 100644 index 000000000..67931703b --- /dev/null +++ b/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml @@ -0,0 +1,147 @@ +- name: migration pre playbook + hosts: localhost + tasks: + - name: get running ansible env variables + set_fact: + config_crn_token: "{{ lookup('env', 'config_crn_token') }}" + project_id: "{{ lookup('env', 'project_id') }}" + config_id: "{{ lookup('env', 'config_id') }}" + project_url: "{{ lookup('env', 'project_url') }}" + - name: Get Statefile + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" + method: GET + headers: + Authorization: "Bearer {{ config_crn_token }}" + register: result + - name: Set Statefile + set_fact: + statefile: "{{ result.json | string }}" + - name: Creating script + copy: + dest: "script.sh" + content: | + #!/bin/bash + STATE="$(cat terraform.tfstate.tmp)" + + SUBNET_LIST=() + + ADDRESS_LIST=() + while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') + + VPC_ADDRESS_LIST=() + while IFS='' read -r line; do VPC_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "managed") and (.name == "vpc")) | .module') + + for abc in "${!VPC_ADDRESS_LIST[@]}"; do + while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | .id') + done + + for i in "${!SUBNET_LIST[@]}"; do + for j in "${!ADDRESS_LIST[@]}"; do + for abc in "${!VPC_ADDRESS_LIST[@]}"; do + VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" + subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') + vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') + VSI_LIST=() + IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" + for x in "${!VSI_LIST[@]}"; do + SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" + DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$SOURCE, $DESTINATION") + fi + if [ -n "${VSI_LIST[$x]}" ]; then + VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') + fi + if [ -n "${VSI_LIST[$x]}" ]; then + FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" + fi + if [ -n "$FIP_RESOURCES" ]; then + FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" + FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" + if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") + fi + fi + str="${VSI_LIST[$x]}" + lastIndex=$(echo "$str" | awk '{print length}') + for ((l = lastIndex; l >= 0; l--)); do + if [[ "${str:$l:1}" == "-" ]]; then + str="${str::l}" + break + fi + done + if [ -n "$VOL_NAMES" ]; then + VOL_ADDRESS_LIST=() + while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') + VOL_NAME=() + IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" + for a in "${!VOL_NAME[@]}"; do + for b in "${!VOL_ADDRESS_LIST[@]}"; do + VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" + vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') + VOL_LIST=() + IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" + for c in "${!VOL_LIST[@]}"; do + if [ -n "${VOL_LIST[$c]}" ]; then + VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" + test="${VOL_LIST[$c]/$str/}" + vol=$(echo "$test" | cut -d"-" -f3-) + VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" + if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then + MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") + fi + fi + done + done + done + fi + done + done + done + done + for ab in "${!MOVED_PARAMS[@]}"; do + echo "${MOVED_PARAMS[$ab]}" + done + + - name: Create state file + copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" + - name: Run the script + ansible.builtin.script: + cmd: ./script.sh + register: move_list + - name: Initiate Commands List + set_fact: + commands: [] + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Add new JSON Objects to List + set_fact: + commands: "{{ commands + + [{ 'command': 'state mv', + 'command_params': item, + 'command_name': 'Move' + count|string, + 'command_onerror': 'abort'}] }}" + loop: "{{ move_list.stdout_lines }}" + loop_control: + index_var: count + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Create complete JSON body + set_fact: + commands: "{{ + { 'commands': commands , + 'operation_name': 'workspace Command', + 'description': 'Executing command'} + }}" + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Run Terraform commands + uri: + url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" + method: POST + headers: + Authorization: "Bearer {{ config_crn_token }}" + Content-Type: application/json + body: "{{ commands }}" + body_format: json + status_code: [200, 202] + register: result + when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' From c0f116ed02a07cd5a8e6012f6bf6070002df30a0 Mon Sep 17 00:00:00 2001 From: "aashiq.jacob@ibm.com" Date: Thu, 20 Feb 2025 15:30:36 +0530 Subject: [PATCH 2/2] review changes --- .catalog-onboard-pipeline.yaml | 10 +- .../migration-pre-ansible-playbook.yaml | 103 +----------- .../migration-pre-ansible-playbook.yaml | 103 +----------- .../migration-pre-ansible-playbook.yaml | 138 +--------------- .../migration-pre-ansible-playbook.yaml | 147 +----------------- .../migration-pre-ansible-playbook.yaml | 147 +----------------- 6 files changed, 20 insertions(+), 628 deletions(-) diff --git a/.catalog-onboard-pipeline.yaml b/.catalog-onboard-pipeline.yaml index ad1f531b2..3c6bd1f25 100644 --- a/.catalog-onboard-pipeline.yaml +++ b/.catalog-onboard-pipeline.yaml @@ -10,18 +10,18 @@ offerings: - name: quickstart mark_ready: false install_type: fullstack - validation_type: schematics + validation_type: projects # projects validation required when ansible migration script present in code - name: standard mark_ready: false install_type: fullstack - validation_type: schematics + validation_type: projects # projects validation required when ansible migration script present in code scc: instance_id: 1c7d5f78-9262-44c3-b779-b28fe4d88c37 region: us-south - name: existing-vpc mark_ready: false install_type: extension - validation_type: schematics + validation_type: projects # projects validation required when ansible migration script present in code pre_validation: "tests/scripts/pre-validation-deploy-slz-vpc.sh" post_validation: "tests/scripts/post-validation-destroy-slz-vpc.sh" scc: @@ -49,11 +49,11 @@ offerings: - name: standard mark_ready: false install_type: fullstack - validation_type: schematics + validation_type: projects # projects validation required when ansible migration script present in code scc: instance_id: 1c7d5f78-9262-44c3-b779-b28fe4d88c37 region: us-south - name: quickstart mark_ready: false install_type: fullstack - validation_type: schematics + validation_type: projects # projects validation required when ansible migration script present in code diff --git a/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml b/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml index 514e4b9d6..81d5b172a 100644 --- a/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml +++ b/patterns/roks-quickstart/scripts/migration-pre-ansible-playbook.yaml @@ -1,103 +1,6 @@ - name: migration pre playbook hosts: localhost tasks: - - name: get running ansible env variables - set_fact: - config_crn_token: "{{ lookup('env', 'config_crn_token') }}" - project_id: "{{ lookup('env', 'project_id') }}" - config_id: "{{ lookup('env', 'config_id') }}" - project_url: "{{ lookup('env', 'project_url') }}" - - name: Get Statefile - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" - method: GET - headers: - Authorization: "Bearer {{ config_crn_token }}" - register: result - - name: Set Statefile - set_fact: - statefile: "{{ result.json | string }}" - - name: Creating script - copy: - dest: "script.sh" - content: | - #!/bin/bash - STATE="$(cat terraform.tfstate.tmp)" - CLUSTER_LIST=() - while IFS='' read -r line; do CLUSTER_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | .index_key') - WORKER_POOLS=() - while IFS='' read -r line; do WORKER_POOLS+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_worker_pool") and (.mode == "managed") and (.name == "pool")) | .instances[] | .index_key') - ADDRESS_LIST="$(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .module')" - - for i in "${!CLUSTER_LIST[@]}"; do - SOURCE="${ADDRESS_LIST}.ibm_container_vpc_cluster.cluster[\"${CLUSTER_LIST[$i]}\"]" - DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_cluster.cluster[0]" - if [ -n "${CLUSTER_LIST[$i]}" ] && [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$SOURCE, $DESTINATION") - fi - - ADDONS_RESOURCE=$(echo "$STATE" | jq -r --arg CLUSTER "${CLUSTER_LIST[$i]}" '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | select(.index_key == $CLUSTER) | .index_key') - if [ -n "$ADDONS_RESOURCE" ]; then - ADDONS_SOURCE="${ADDRESS_LIST}.ibm_container_addons.addons[\"${CLUSTER_LIST[$i]}\"]" - ADDONS_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_addons.addons" - if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$ADDONS_SOURCE, $ADDONS_DESTINATION") - fi - fi - - for x in "${!WORKER_POOLS[@]}"; do - if [[ "${WORKER_POOLS[$x]}" == *"${CLUSTER_LIST[$i]}"* ]]; then - pool_name=${WORKER_POOLS[$x]//"${CLUSTER_LIST[$i]}-"/} - WORKER_POOL_SOURCE="${ADDRESS_LIST}.ibm_container_vpc_worker_pool.pool[\"${WORKER_POOLS[$x]}\"]" - WORKER_POOL_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_worker_pool.pool[\"$pool_name\"]" - if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$WORKER_POOL_SOURCE, $WORKER_POOL_DESTINATION") - fi - fi - done - done - for mv in "${!MOVED_PARAMS[@]}"; do - echo "${MOVED_PARAMS[$mv]}" - done - - - name: Create state file - copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" - - name: Run the script - ansible.builtin.script: - cmd: ./script.sh - register: move_list - - name: Initiate Commands List - set_fact: - commands: [] - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Add new JSON Objects to List - set_fact: - commands: "{{ commands + - [{ 'command': 'state mv', - 'command_params': item, - 'command_name': 'Move' + count|string, - 'command_onerror': 'abort'}] }}" - loop: "{{ move_list.stdout_lines }}" - loop_control: - index_var: count - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Create complete JSON body - set_fact: - commands: "{{ - { 'commands': commands , - 'operation_name': 'workspace Command', - 'description': 'Executing command'} - }}" - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Run Terraform commands - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" - method: POST - headers: - Authorization: "Bearer {{ config_crn_token }}" - Content-Type: application/json - body: "{{ commands }}" - body_format: json - status_code: [200, 202] - register: result - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Print message + debug: + msg: No migration needed. diff --git a/patterns/roks/scripts/migration-pre-ansible-playbook.yaml b/patterns/roks/scripts/migration-pre-ansible-playbook.yaml index 514e4b9d6..81d5b172a 100644 --- a/patterns/roks/scripts/migration-pre-ansible-playbook.yaml +++ b/patterns/roks/scripts/migration-pre-ansible-playbook.yaml @@ -1,103 +1,6 @@ - name: migration pre playbook hosts: localhost tasks: - - name: get running ansible env variables - set_fact: - config_crn_token: "{{ lookup('env', 'config_crn_token') }}" - project_id: "{{ lookup('env', 'project_id') }}" - config_id: "{{ lookup('env', 'config_id') }}" - project_url: "{{ lookup('env', 'project_url') }}" - - name: Get Statefile - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" - method: GET - headers: - Authorization: "Bearer {{ config_crn_token }}" - register: result - - name: Set Statefile - set_fact: - statefile: "{{ result.json | string }}" - - name: Creating script - copy: - dest: "script.sh" - content: | - #!/bin/bash - STATE="$(cat terraform.tfstate.tmp)" - CLUSTER_LIST=() - while IFS='' read -r line; do CLUSTER_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | .index_key') - WORKER_POOLS=() - while IFS='' read -r line; do WORKER_POOLS+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_worker_pool") and (.mode == "managed") and (.name == "pool")) | .instances[] | .index_key') - ADDRESS_LIST="$(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .module')" - - for i in "${!CLUSTER_LIST[@]}"; do - SOURCE="${ADDRESS_LIST}.ibm_container_vpc_cluster.cluster[\"${CLUSTER_LIST[$i]}\"]" - DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_cluster.cluster[0]" - if [ -n "${CLUSTER_LIST[$i]}" ] && [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$SOURCE, $DESTINATION") - fi - - ADDONS_RESOURCE=$(echo "$STATE" | jq -r --arg CLUSTER "${CLUSTER_LIST[$i]}" '.resources[] | select((.type == "ibm_container_vpc_cluster") and (.mode == "managed") and (.name == "cluster")) | .instances[] | select(.index_key == $CLUSTER) | .index_key') - if [ -n "$ADDONS_RESOURCE" ]; then - ADDONS_SOURCE="${ADDRESS_LIST}.ibm_container_addons.addons[\"${CLUSTER_LIST[$i]}\"]" - ADDONS_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_addons.addons" - if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$ADDONS_SOURCE, $ADDONS_DESTINATION") - fi - fi - - for x in "${!WORKER_POOLS[@]}"; do - if [[ "${WORKER_POOLS[$x]}" == *"${CLUSTER_LIST[$i]}"* ]]; then - pool_name=${WORKER_POOLS[$x]//"${CLUSTER_LIST[$i]}-"/} - WORKER_POOL_SOURCE="${ADDRESS_LIST}.ibm_container_vpc_worker_pool.pool[\"${WORKER_POOLS[$x]}\"]" - WORKER_POOL_DESTINATION="${ADDRESS_LIST}.module.cluster[\"${CLUSTER_LIST[$i]}\"].ibm_container_vpc_worker_pool.pool[\"$pool_name\"]" - if [ -n "$SOURCE" ] && [ -n "$DESTINATION" ] && [ "${CLUSTER_LIST[$i]}" != 0 ]; then - MOVED_PARAMS+=("$WORKER_POOL_SOURCE, $WORKER_POOL_DESTINATION") - fi - fi - done - done - for mv in "${!MOVED_PARAMS[@]}"; do - echo "${MOVED_PARAMS[$mv]}" - done - - - name: Create state file - copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" - - name: Run the script - ansible.builtin.script: - cmd: ./script.sh - register: move_list - - name: Initiate Commands List - set_fact: - commands: [] - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Add new JSON Objects to List - set_fact: - commands: "{{ commands + - [{ 'command': 'state mv', - 'command_params': item, - 'command_name': 'Move' + count|string, - 'command_onerror': 'abort'}] }}" - loop: "{{ move_list.stdout_lines }}" - loop_control: - index_var: count - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Create complete JSON body - set_fact: - commands: "{{ - { 'commands': commands , - 'operation_name': 'workspace Command', - 'description': 'Executing command'} - }}" - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Run Terraform commands - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" - method: POST - headers: - Authorization: "Bearer {{ config_crn_token }}" - Content-Type: application/json - body: "{{ commands }}" - body_format: json - status_code: [200, 202] - register: result - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Print message + debug: + msg: No migration needed. diff --git a/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml index 06c25372e..81d5b172a 100644 --- a/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml +++ b/patterns/vsi-extension/scripts/migration-pre-ansible-playbook.yaml @@ -1,138 +1,6 @@ - name: migration pre playbook hosts: localhost tasks: - - name: get running ansible env variables - set_fact: - config_crn_token: "{{ lookup('env', 'config_crn_token') }}" - project_id: "{{ lookup('env', 'project_id') }}" - config_id: "{{ lookup('env', 'config_id') }}" - project_url: "{{ lookup('env', 'project_url') }}" - - name: Get Statefile - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" - method: GET - headers: - Authorization: "Bearer {{ config_crn_token }}" - register: result - - name: Set Statefile - set_fact: - statefile: "{{ result.json | string }}" - - name: Creating script - copy: - dest: "script.sh" - content: | - #!/bin/bash - STATE="$(cat terraform.tfstate.tmp)" - - SUBNET_LIST=() - while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "data") and (.name == "vpc_by_id")) | .instances[0] | .attributes | .subnets[] | .id') - ADDRESS_LIST=() - while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') - - for i in "${!SUBNET_LIST[@]}"; do - for j in "${!ADDRESS_LIST[@]}"; do - VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" - subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "data") and (.name == "vpc_by_id")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') - vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') - VSI_LIST=() - IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" - for x in "${!VSI_LIST[@]}"; do - SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" - DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$SOURCE, $DESTINATION") - fi - if [ -n "${VSI_LIST[$x]}" ]; then - VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') - fi - if [ -n "${VSI_LIST[$x]}" ]; then - FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" - fi - if [ -n "$FIP_RESOURCES" ]; then - FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" - FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") - fi - fi - str="${VSI_LIST[$x]}" - lastIndex=$(echo "$str" | awk '{print length}') - for ((l = lastIndex; l >= 0; l--)); do - if [[ "${str:$l:1}" == "-" ]]; then - str="${str::l}" - break - fi - done - if [ -n "$VOL_NAMES" ]; then - VOL_ADDRESS_LIST=() - while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') - VOL_NAME=() - IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" - for a in "${!VOL_NAME[@]}"; do - for b in "${!VOL_ADDRESS_LIST[@]}"; do - VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" - vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') - VOL_LIST=() - IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" - for c in "${!VOL_LIST[@]}"; do - if [ -n "${VOL_LIST[$c]}" ]; then - VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" - test="${VOL_LIST[$c]/$str/}" - vol=$(echo "$test" | cut -d"-" -f3-) - VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" - if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") - fi - fi - done - done - done - fi - done - done - done - for ab in "${!MOVED_PARAMS[@]}"; do - echo "${MOVED_PARAMS[$ab]}" - done - - - name: Create state file - copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" - - name: Run the script - ansible.builtin.script: - cmd: ./script.sh - register: move_list - - name: Initiate Commands List - set_fact: - commands: [] - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Add new JSON Objects to List - set_fact: - commands: "{{ commands + - [{ 'command': 'state mv', - 'command_params': item, - 'command_name': 'Move' + count|string, - 'command_onerror': 'abort'}] }}" - loop: "{{ move_list.stdout_lines }}" - loop_control: - index_var: count - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Create complete JSON body - set_fact: - commands: "{{ - { 'commands': commands , - 'operation_name': 'workspace Command', - 'description': 'Executing command'} - }}" - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Run Terraform commands - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" - method: POST - headers: - Authorization: "Bearer {{ config_crn_token }}" - Content-Type: application/json - body: "{{ commands }}" - body_format: json - status_code: [200, 202] - register: result - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Print message + debug: + msg: No migration needed. diff --git a/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml index 67931703b..81d5b172a 100644 --- a/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml +++ b/patterns/vsi-quickstart/scripts/migration-pre-ansible-playbook.yaml @@ -1,147 +1,6 @@ - name: migration pre playbook hosts: localhost tasks: - - name: get running ansible env variables - set_fact: - config_crn_token: "{{ lookup('env', 'config_crn_token') }}" - project_id: "{{ lookup('env', 'project_id') }}" - config_id: "{{ lookup('env', 'config_id') }}" - project_url: "{{ lookup('env', 'project_url') }}" - - name: Get Statefile - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" - method: GET - headers: - Authorization: "Bearer {{ config_crn_token }}" - register: result - - name: Set Statefile - set_fact: - statefile: "{{ result.json | string }}" - - name: Creating script - copy: - dest: "script.sh" - content: | - #!/bin/bash - STATE="$(cat terraform.tfstate.tmp)" - - SUBNET_LIST=() - - ADDRESS_LIST=() - while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') - - VPC_ADDRESS_LIST=() - while IFS='' read -r line; do VPC_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "managed") and (.name == "vpc")) | .module') - - for abc in "${!VPC_ADDRESS_LIST[@]}"; do - while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | .id') - done - - for i in "${!SUBNET_LIST[@]}"; do - for j in "${!ADDRESS_LIST[@]}"; do - for abc in "${!VPC_ADDRESS_LIST[@]}"; do - VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" - subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') - vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') - VSI_LIST=() - IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" - for x in "${!VSI_LIST[@]}"; do - SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" - DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$SOURCE, $DESTINATION") - fi - if [ -n "${VSI_LIST[$x]}" ]; then - VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') - fi - if [ -n "${VSI_LIST[$x]}" ]; then - FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" - fi - if [ -n "$FIP_RESOURCES" ]; then - FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" - FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") - fi - fi - str="${VSI_LIST[$x]}" - lastIndex=$(echo "$str" | awk '{print length}') - for ((l = lastIndex; l >= 0; l--)); do - if [[ "${str:$l:1}" == "-" ]]; then - str="${str::l}" - break - fi - done - if [ -n "$VOL_NAMES" ]; then - VOL_ADDRESS_LIST=() - while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') - VOL_NAME=() - IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" - for a in "${!VOL_NAME[@]}"; do - for b in "${!VOL_ADDRESS_LIST[@]}"; do - VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" - vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') - VOL_LIST=() - IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" - for c in "${!VOL_LIST[@]}"; do - if [ -n "${VOL_LIST[$c]}" ]; then - VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" - test="${VOL_LIST[$c]/$str/}" - vol=$(echo "$test" | cut -d"-" -f3-) - VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" - if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") - fi - fi - done - done - done - fi - done - done - done - done - for ab in "${!MOVED_PARAMS[@]}"; do - echo "${MOVED_PARAMS[$ab]}" - done - - - name: Create state file - copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" - - name: Run the script - ansible.builtin.script: - cmd: ./script.sh - register: move_list - - name: Initiate Commands List - set_fact: - commands: [] - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Add new JSON Objects to List - set_fact: - commands: "{{ commands + - [{ 'command': 'state mv', - 'command_params': item, - 'command_name': 'Move' + count|string, - 'command_onerror': 'abort'}] }}" - loop: "{{ move_list.stdout_lines }}" - loop_control: - index_var: count - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Create complete JSON body - set_fact: - commands: "{{ - { 'commands': commands , - 'operation_name': 'workspace Command', - 'description': 'Executing command'} - }}" - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Run Terraform commands - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" - method: POST - headers: - Authorization: "Bearer {{ config_crn_token }}" - Content-Type: application/json - body: "{{ commands }}" - body_format: json - status_code: [200, 202] - register: result - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Print message + debug: + msg: No migration needed. diff --git a/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml b/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml index 67931703b..81d5b172a 100644 --- a/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml +++ b/patterns/vsi/scripts/migration-pre-ansible-playbook.yaml @@ -1,147 +1,6 @@ - name: migration pre playbook hosts: localhost tasks: - - name: get running ansible env variables - set_fact: - config_crn_token: "{{ lookup('env', 'config_crn_token') }}" - project_id: "{{ lookup('env', 'project_id') }}" - config_id: "{{ lookup('env', 'config_id') }}" - project_url: "{{ lookup('env', 'project_url') }}" - - name: Get Statefile - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/retrieve_schematics_statefile" - method: GET - headers: - Authorization: "Bearer {{ config_crn_token }}" - register: result - - name: Set Statefile - set_fact: - statefile: "{{ result.json | string }}" - - name: Creating script - copy: - dest: "script.sh" - content: | - #!/bin/bash - STATE="$(cat terraform.tfstate.tmp)" - - SUBNET_LIST=() - - ADDRESS_LIST=() - while IFS='' read -r line; do ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_instance") | .module') - - VPC_ADDRESS_LIST=() - while IFS='' read -r line; do VPC_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select((.type == "ibm_is_vpc") and (.mode == "managed") and (.name == "vpc")) | .module') - - for abc in "${!VPC_ADDRESS_LIST[@]}"; do - while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$STATE" | jq -r --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | .id') - done - - for i in "${!SUBNET_LIST[@]}"; do - for j in "${!ADDRESS_LIST[@]}"; do - for abc in "${!VPC_ADDRESS_LIST[@]}"; do - VSI_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_instance") and (.module == $address)) | .instances')" - subnet_name=$(echo "$STATE" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" --arg address "${VPC_ADDRESS_LIST[$abc]}" '.resources[] | select((.type == "ibm_is_vpc") and (.module == $address) and (.mode == "data") and (.name == "vpc")) | .instances[0] | .attributes | .subnets[] | select(.id == $subnet_id) | .name') - vsi_names=$(echo "$VSI_RESOURCES" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.[] | select(.attributes.primary_network_interface[0].subnet == $subnet_id) | .index_key') - VSI_LIST=() - IFS=$'\n' read -r -d '' -a VSI_LIST <<<"$vsi_names" - for x in "${!VSI_LIST[@]}"; do - SOURCE="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${VSI_LIST[$x]}\"]" - DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_instance.vsi[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$SOURCE, $DESTINATION") - fi - if [ -n "${VSI_LIST[$x]}" ]; then - VOL_NAMES=$(echo "$VSI_RESOURCES" | jq -r --arg vsi "${VSI_LIST[$x]}" '.[] | select(.index_key == $vsi) | .attributes.volume_attachments[].volume_name') - fi - if [ -n "${VSI_LIST[$x]}" ]; then - FIP_RESOURCES="$(echo "$STATE" | jq -r --arg address "${ADDRESS_LIST[$j]}" '.resources[] | select((.type == "ibm_is_floating_ip") and (.module == $address)) | .instances')" - fi - if [ -n "$FIP_RESOURCES" ]; then - FIP_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${VSI_LIST[$x]}\"]" - FIP_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_floating_ip.vsi_fip[\"${subnet_name}-${x}\"]" - if [ -n "${VSI_LIST[$x]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION") - fi - fi - str="${VSI_LIST[$x]}" - lastIndex=$(echo "$str" | awk '{print length}') - for ((l = lastIndex; l >= 0; l--)); do - if [[ "${str:$l:1}" == "-" ]]; then - str="${str::l}" - break - fi - done - if [ -n "$VOL_NAMES" ]; then - VOL_ADDRESS_LIST=() - while IFS='' read -r line; do VOL_ADDRESS_LIST+=("$line"); done < <(echo "$STATE" | jq -r '.resources[] | select(.type == "ibm_is_volume") | .module') - VOL_NAME=() - IFS=$'\n' read -r -d '' -a VOL_NAME <<<"$VOL_NAMES" - for a in "${!VOL_NAME[@]}"; do - for b in "${!VOL_ADDRESS_LIST[@]}"; do - VOL_RESOURCES="$(echo "$STATE" | jq -r --arg address "${VOL_ADDRESS_LIST[$b]}" '.resources[] | select((.type == "ibm_is_volume") and (.module == $address)) | .instances')" - vol_names=$(echo "$VOL_RESOURCES" | jq -r --arg vol1 "${VOL_NAME[$a]}" '.[] | select(.attributes.name == $vol1) | .index_key') - VOL_LIST=() - IFS=$'\n' read -r -d '' -a VOL_LIST <<<"$vol_names" - for c in "${!VOL_LIST[@]}"; do - if [ -n "${VOL_LIST[$c]}" ]; then - VOL_SOURCE="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${VOL_LIST[$c]}\"]" - test="${VOL_LIST[$c]/$str/}" - vol=$(echo "$test" | cut -d"-" -f3-) - VOL_DESTINATION="${ADDRESS_LIST[$j]}.ibm_is_volume.volume[\"${subnet_name}-${x}-${vol}\"]" - if [ -n "${VOL_LIST[$c]}" ] && [ -n "${subnet_name}" ] && [ "$SOURCE" != "$DESTINATION" ]; then - MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION") - fi - fi - done - done - done - fi - done - done - done - done - for ab in "${!MOVED_PARAMS[@]}"; do - echo "${MOVED_PARAMS[$ab]}" - done - - - name: Create state file - copy: content="{{ statefile }}" dest="terraform.tfstate.tmp" - - name: Run the script - ansible.builtin.script: - cmd: ./script.sh - register: move_list - - name: Initiate Commands List - set_fact: - commands: [] - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Add new JSON Objects to List - set_fact: - commands: "{{ commands + - [{ 'command': 'state mv', - 'command_params': item, - 'command_name': 'Move' + count|string, - 'command_onerror': 'abort'}] }}" - loop: "{{ move_list.stdout_lines }}" - loop_control: - index_var: count - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Create complete JSON body - set_fact: - commands: "{{ - { 'commands': commands , - 'operation_name': 'workspace Command', - 'description': 'Executing command'} - }}" - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' - - name: Run Terraform commands - uri: - url: "{{ project_url }}/v1/projects/{{ project_id }}/configs/{{ config_id }}/execute_schematics_commands" - method: POST - headers: - Authorization: "Bearer {{ config_crn_token }}" - Content-Type: application/json - body: "{{ commands }}" - body_format: json - status_code: [200, 202] - register: result - when: move_list is defined and move_list.stdout is defined and move_list.stdout != '' + - name: Print message + debug: + msg: No migration needed.