Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: the vsi-map key to use the subnet-name #610

Merged
merged 55 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
bf1dc54
fix: update the keys of the vsi_map
Aashiq-J Jan 10, 2024
aa3f8c4
changes to the volume name
Aashiq-J Jan 16, 2024
10f71f3
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Jan 17, 2024
090cbf3
add script and documentation
Aashiq-J Jan 18, 2024
561e8cf
precommit changes
Aashiq-J Jan 18, 2024
be10c1d
SKIP UPGRADE TEST
Aashiq-J Jan 18, 2024
bd544d1
uncomment terraform mv command
Aashiq-J Jan 18, 2024
67f31b9
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Jan 24, 2024
abf6459
update vsi and vol name
Aashiq-J Jan 24, 2024
2541809
update script for volume name change
Aashiq-J Jan 29, 2024
bf08d34
Merge branch 'main' into refactor
Aashiq-J Jan 30, 2024
c25020a
fix shellcheck
Aashiq-J Jan 30, 2024
7481346
update script
Aashiq-J Feb 16, 2024
18e3841
Merge branch 'main' into refactor
ocofaigh Feb 21, 2024
bb6da63
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Mar 22, 2024
7df0598
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Mar 26, 2024
2938dfd
add new script
Aashiq-J Mar 26, 2024
3cfdc4d
update script
Aashiq-J Mar 27, 2024
e602163
script updates for local state file
Aashiq-J Mar 27, 2024
3beb5b2
precommit changes
Aashiq-J Mar 27, 2024
0222e61
update dependency check logic
Aashiq-J Mar 28, 2024
1099eae
update documentation
Aashiq-J Mar 28, 2024
8f0ac0d
update the doc
Aashiq-J Mar 28, 2024
8237a3e
update doc
Aashiq-J Mar 28, 2024
4f11192
remove readarray
Aashiq-J Mar 28, 2024
0baaa18
add prefix
Aashiq-J Mar 28, 2024
3ebd41d
test volume
Aashiq-J Mar 28, 2024
3ed9687
revert vol name
Aashiq-J Mar 28, 2024
b0d889d
update script and doc
Aashiq-J Mar 29, 2024
7772ecb
revert the name
Aashiq-J Mar 29, 2024
929be1a
update volume name
Aashiq-J Mar 29, 2024
d408297
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Apr 3, 2024
69c0f5b
test commit
Aashiq-J Apr 3, 2024
9e65f19
revert test
Aashiq-J Apr 3, 2024
9f8c640
change count per subnet
Aashiq-J Apr 3, 2024
9712f04
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Apr 17, 2024
a9cc79f
Merge branch 'main' into refactor
Aashiq-J Apr 22, 2024
88ff5b5
Merge branch 'main' into refactor
Aashiq-J Apr 29, 2024
e435779
review comments
Aashiq-J Apr 29, 2024
4531f45
review changes
Aashiq-J Apr 30, 2024
8691bce
fix typo
Aashiq-J Apr 30, 2024
33aae6b
review comments
Aashiq-J May 2, 2024
b08d71c
update FIP
Aashiq-J May 2, 2024
68b71ca
update fip in schematics
Aashiq-J May 2, 2024
bcbdebe
update command to select region
Aashiq-J May 2, 2024
26fc626
review changes
Aashiq-J May 2, 2024
3a08a47
review comments
Aashiq-J May 3, 2024
19d5752
support space after comma in schematics script
Aashiq-J May 3, 2024
d65fcb4
Merge branch 'main' into refactor
Aashiq-J May 6, 2024
400320f
Edits to new instructions (#667)
SirSpidey May 10, 2024
9affd48
rename script
Aashiq-J May 10, 2024
b2b4a8a
Update update/schematics_update_v3_to_v4.sh
Aashiq-J May 11, 2024
3907489
Merge branch 'main' into refactor
Aashiq-J May 11, 2024
27dd615
Merge branch 'main' into refactor
Aashiq-J May 13, 2024
dbf53d3
Update update-version.md
ocofaigh May 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ Brewfile.lock.json

# Ignore .vscode files
.vscode/

# Ignore schematics json
/update/*.json
4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ locals {
# For each subnet
for subnet in range(length(var.subnets)) :
{
name = "${var.prefix}-${(count) * length(var.subnets) + subnet + 1}"
vsi_name = "${var.prefix}-${format("%03d", count * length(var.subnets) + subnet + 1)}"
name = "${var.subnets[subnet].name}-${count}"
vsi_name = "${var.prefix}-${substr(var.subnets[subnet].id, -4, 4)}-${format("%03d", count + 1)}"
subnet_id = var.subnets[subnet].id
zone = var.subnets[subnet].zone
subnet_name = var.subnets[subnet].name
Expand Down
6 changes: 3 additions & 3 deletions storage.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ locals {
# For each volume
for volume in var.block_storage_volumes :
{
name = "${var.prefix}-${(subnet) * (var.vsi_per_subnet) + count + 1}-${volume.name}"
vol_name = "${var.prefix}-${format("%03d", subnet * var.vsi_per_subnet + count + 1)}-${volume.name}"
name = "${var.subnets[subnet].name}-${count}-${volume.name}"
vol_name = "${var.prefix}-${substr(var.subnets[subnet].id, -4, 4)}-${format("%03d", count + 1)}-${volume.name}"
zone = var.subnets[subnet].zone
profile = volume.profile
capacity = volume.capacity
vsi_name = "${var.prefix}-${(count) * length(var.subnets) + subnet + 1}"
vsi_name = "${var.subnets[subnet].name}-${count}"
iops = volume.iops
encryption_key = var.kms_encryption_enabled ? var.boot_volume_encryption_key : volume.encryption_key
resource_group = volume.resource_group_id != null ? volume.resource_group_id : var.resource_group_id
Expand Down
300 changes: 300 additions & 0 deletions update/schematics_update_v3.x.x_to_v4.x.x.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
#!/usr/bin/env bash

PRG=$(basename -- "${0}")
USAGE="
usage: ./${PRG}
Required environment variables:
IBMCLOUD_API_KEY
WORKSPACE_ID
Aashiq-J marked this conversation as resolved.
Show resolved Hide resolved
Dependencies:
- IBM Cloud CLI
- IBM Cloud CLI 'is' plugin
- Terraform CLI
Aashiq-J marked this conversation as resolved.
Show resolved Hide resolved
- jq
"
VOL_RESOURCES=""
VOL_NAMES=""
REVERT=false
VPC_IBMCLOUD_API_KEY=""

helpFunction() {
echo ""
echo "Usage: $0 -v VPC_ID -r VPC_REGION [-k VPC_IBMCLOUD_API_KEY] [-z]"
echo -e "\t-v , seperated IDs or names of the VPC in which the VSIs are deployed which needs to be tracked by the newer version of the terraform module."
echo -e "\t-r Region of the VPC."
echo -e "\t-k [Optional] IBMCLOUD_API_KEY to access the VPCs, if the VPCs are deployed in a different account."
echo -e "\t-z [Optional] Flag to revert the changes done to the state file."
exit 1 # Exit script after printing help
}

while getopts "v:r:k:z" opt; do
case "$opt" in
v) VPC_ID="$OPTARG" ;;
r) VPC_REGION="$OPTARG" ;;
k) VPC_IBMCLOUD_API_KEY="$OPTARG" ;;
z) REVERT=true ;;
?) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done

# Print helpFunction in case parameters are empty
if [ "$REVERT" == false ]; then
if [ -z "$VPC_ID" ] || [ -z "$VPC_REGION" ]; then
echo "VPC_ID or REGION is empty"
helpFunction
fi
fi

function dependency_check() {
dependencies=("ibmcloud" "jq")
for dependency in "${dependencies[@]}"; do
if ! command -v "$dependency" >/dev/null 2>&1; then
echo "\"$dependency\" is not installed. Please install $dependency."
exit 1
fi
done
plugin_dependencies=("schematics" "vpc-infrastructure")
for plugin_dependency in "${plugin_dependencies[@]}"; do
if ! ibmcloud plugin show "$plugin_dependency" >/dev/null; then
echo "\"$plugin_dependency\" ibmcloud plugin is not installed. Please install $plugin_dependency."
exit 1
fi
done
echo "All dependencies are available!"
}

# check that env contains required vars
function verify_required_env_var() {
printf "\n#### VERIFYING ENV ####\n\n"
all_env_vars_exist=true
env_var_array=(IBMCLOUD_API_KEY WORKSPACE_ID)
set +u
for var in "${env_var_array[@]}"; do
[ -z "${!var}" ] && echo "${var} not defined." && all_env_vars_exist=false
done
set -u
if [ ${all_env_vars_exist} == false ]; then
echo "One or more required environment variables are not defined. Exiting."
echo "${USAGE}"
exit 1
fi
Aashiq-J marked this conversation as resolved.
Show resolved Hide resolved
}

# Login to IBM Cloud using IBMCLOUD_API_KEY env var value
function ibmcloud_login() {
printf "\n#### IBM CLOUD LOGIN ####\n\n"
WORKSPACE_REGION=$(echo "$WORKSPACE_ID" | cut -d "." -f 1)
attempts=1
until ibmcloud login --apikey "$IBMCLOUD_API_KEY" -r "$WORKSPACE_REGION" || [ $attempts -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
printf "\n#### Verification complete ####\n\n"
Aashiq-J marked this conversation as resolved.
Show resolved Hide resolved
}

function get_workspace_details() {
template_id="$(ibmcloud schematics workspace get --id "$WORKSPACE_ID" -o json | jq -r .template_data[0].id)"
OUTPUT="$(ibmcloud schematics state pull --id "$WORKSPACE_ID" --template "$template_id")"
STATE=${OUTPUT//'OK'/}
}

function update_state() {
if [[ -z "$VPC_IBMCLOUD_API_KEY" ]]; then
until ibmcloud target -r "$VPC_REGION" || [ "$attempts" -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
else
until ibmcloud login --apikey "$VPC_IBMCLOUD_API_KEY" -r "$VPC_REGION" || [ $attempts -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
fi
VPC_LIST=()
IFS=',' read -r -d '' -a VPC_LIST <<<"$VPC_ID"
for vpc in "${!VPC_LIST[@]}"; do
VPC_DATA=$(ibmcloud is vpc "${VPC_LIST[$vpc]//$'\n'/}" --output JSON --show-attached -q)
SUBNET_LIST=()
while IFS='' read -r line; do SUBNET_LIST+=("$line"); done < <(echo "$VPC_DATA" | jq -r '.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 "$VPC_DATA" | jq -r --arg subnet_id "${SUBNET_LIST[$i]}" '.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}" ]; then
MOVED_PARAMS+=("$SOURCE, $DESTINATION")
REVERT_PARAMS+=("$DESTINATION, $SOURCE")
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}" ]; then
MOVED_PARAMS+=("$FIP_SOURCE, $FIP_DESTINATION")
REVERT_PARAMS+=("$FIP_DESTINATION, $FIP_SOURCE")
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}" ]; then
MOVED_PARAMS+=("$VOL_SOURCE, $VOL_DESTINATION")
REVERT_PARAMS+=("$VOL_DESTINATION, $VOL_SOURCE")
fi
fi
done
done
done
fi
done
done
done
done

}

function update_schematics() {
if [[ -z "$VPC_IBMCLOUD_API_KEY" ]]; then
until ibmcloud target -r "$WORKSPACE_REGION" || [ "$attempts" -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
else
until ibmcloud login --apikey "$IBMCLOUD_API_KEY" -r "$WORKSPACE_REGION" || [ $attempts -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
fi

ibmcloud schematics workspace commands --id "$WORKSPACE_ID" --file ./moved.json
}

function revert_schematics() {
if ! [ -f "./revert.json" ]; then
echo "Revert.json does not exist."
else
if [ -s "./revert.json" ]; then
if [[ -z "$VPC_IBMCLOUD_API_KEY" ]]; then
until ibmcloud target -r "$WORKSPACE_REGION" || [ "$attempts" -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
else
until ibmcloud login --apikey "$IBMCLOUD_API_KEY" -r "$WORKSPACE_REGION" || [ $attempts -ge 3 ]; do
attempts=$((attempts + 1))
echo "Error logging in to IBM Cloud CLI..."
sleep 3
done
fi
ibmcloud schematics workspace commands --id "$WORKSPACE_ID" --file ./revert.json
else
echo "Revert.json is empty."
fi
fi

}
create_json() {
for movedparam in "${!MOVED_PARAMS[@]}"; do
jq --arg command_params "${MOVED_PARAMS[$movedparam]}" --arg command_name "Move$movedparam" '.commands += [{"command": "state mv","command_params": $command_params, "command_name": $command_name, "command_onerror": "abort"}]' moved.json >temp.json && mv temp.json moved.json
done
for revertparam in "${!REVERT_PARAMS[@]}"; do
jq --arg command_params "${REVERT_PARAMS[$revertparam]}" --arg command_name "Revert$revertparam" '.commands += [{"command": "state mv","command_params": $command_params, "command_name": $command_name, "command_onerror": "continue"}]' revert.json >temp.json && mv temp.json revert.json
done
jq '.commands += [{"command": "state list","command_params": "", "command_name": "Test", "command_onerror": "continue"}]' revert.json >temp.json && mv temp.json revert.json
}

create_json_files() {
# Define the file path and content
MOVED_JSON="./moved.json"
REVERT_JSON="./revert.json"

# Check if the file exists
if [ -f "$MOVED_JSON" ] || [ -f "$REVERT_JSON" ]; then
# If the file exists, empty it
echo "" >"$MOVED_JSON"
echo "" >"$REVERT_JSON"
else
# If the file does not exist, create it
touch "$MOVED_JSON"
touch "$REVERT_JSON"
fi

# Add new content to the file
echo '{
"commands": [],
"operation_name": "workspace Command",
"description": "Executing command"
}
' >>$MOVED_JSON

echo '{
"commands": [],
"operation_name": "workspace Command",
"description": "Executing command"
}
' >>$REVERT_JSON
}

# run
function main() {
if [ "$REVERT" == false ]; then
dependency_check
create_json_files
verify_required_env_var
ibmcloud_login
get_workspace_details
update_state
create_json
update_schematics
else
verify_required_env_var
ibmcloud_login
revert_schematics
fi
}

main
Loading