リソースの一括破棄とスタックの削除

指定されたコンパートメントについて、次のスクリプトを使用してリソース・マネージャのすべてのスタックを削除し、対応するスタックに関連付けられたすべてのリソースを破棄します。

このスクリプトは、コンパートメント内のすべてのプロビジョニング済リソースでジョブを破棄し、関連するすべてのスタックを削除します。

  1. 次のスクリプトでコンパートメントOCIDを更新します。
    自動破棄ジョブおよびスタック削除のスクリプト
    #!/bin/bash
    echo ""
    echo ""
    echo ""
    echo "This script destroys all provisioned resources and deletes all associated stacks in a compartment. "
    echo "Update the compartment OCID before you run the script."
    echo " "
    echo "**************************************************"
    echo "STACK DELETIONS CANNOT BE UNDONE."
    echo "**************************************************"
    echo ""
    echo "Starting to delete stacks from compartment id " $1
    echo "Are you sure you want to continue ?"
    select yn in "Yes" "No"; do
        case $yn in
            Yes )  break;;
            No ) echo "Exiting...";exit;;
        esac
    done
    
    stacks_to_be_deleted=$(oci resource-manager stack  list --compartment-id $1  --lifecycle-state=ACTIVE | jq -r ".data[].id")
    
    #stacks_to_be_deleted=$(oci resource-manager stack list -c ocid1.tenancy.oc1..<unique_ID> --lifecycle-state=ACTIVE)
    
    echo "Destroying Stacks"
    echo
    failedStackArr=()
    for stack in $stacks_to_be_deleted
    do
            echo "Creating a job to destroy stack : $stack"
            #stack_destroyed=$(oci resource-manager job create-destroy-job --stack-id $stack --execution-plan-strategy AUTO_APPROVED)      
    
            #echo "oci resource-manager stack delete --force --stack-id $stack.id"
    
            job_id_destroy_stack=$(oci resource-manager job create-destroy-job --stack-id $stack  --execution-plan-strategy AUTO_APPROVED | jq -r ".data.id")
            #poll for the status
            
    	echo "Destroy job OCID : $job_id_destroy_stack"
    	echo "Destroying resources provisioned by stack : $stack"
            
    	#poll for destroy job
            destroy_job_status="STARTED"
            while [ $destroy_job_status != "SUCCEEDED" -a  $destroy_job_status != "FAILED" ]
            do
                    sleep 5s
                    destroy_job_status=$(oci resource-manager job get --job-id $job_id_destroy_stack  | jq -r '.data."lifecycle-state"')
    
            done
    
            if [ $destroy_job_status == "SUCCEEDED" ]; then
                echo "Stack resources destroyed. Now deleting the stack $stack"
    	    echo		
                stack_deleted=$(oci resource-manager stack delete --stack-id $stack --force   | jq -r ".data.id")
            fi
    
            if [ $destroy_job_status == "FAILED" ]; then
                echo "Not able to destroy resources from stack $stack"
                failedStackArr+=($stack)
            fi
    done
    
    if [ ${#failedStackArr[@]} -gt 0 ]; then
    	echo "Not able to destroy resources and delete these stacks. Please run destroy jobs and delete stacks manually."
    	echo ${failedStackArr[@]}
    else
    	echo "Success. All resources have been destroyed and associated stacks have been deleted."
    fi 
    
    echo "done "
  2. 次のコマンドを使用してスクリプトを実行します。
    ./destroy_delete_stacks_bulk_cloud_shell.sh <compartment_id>