從 Jenkins 批次刪除 ec2 快照

從 Jenkins 批次刪除 ec2 快照

使用案例:
將 txt 檔案 (old_snapshots.txt) 中的快照清單匯出到 S3 儲存桶。從 Jenkins,使用 aws cp 指令將檔案複製到 Jenkins /tmp/directory

--dry-run did not show any error 

但是,當在 Jenkins 中使用 aws delete 命令傳遞以下 bash 行時,它顯示成功,同時表示發生了錯誤且快照未刪除。

## actual deletion
        file="/tmp/old_snapshots.txt"
         while read delete_data
         do
        aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
        echo "Deleting snapshot $delete_data"
        done <"$file"

輸出

An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxx81xxxxxx7fxxx

An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxacc49xxxxxxc26


An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

要求:這個錯誤An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation似乎太籠統了?有人遇到過似乎問題嗎?此外,我還進行了手動測試,透過在 Jenkins 中手動新增快照 ID,使用 aws cli 刪除單一快照,效果很好。任何建議將不勝感激。

包括aws命令

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id snap-01x1618xxxxxxa51x
Found snapshotid: snap-01x1618xxxxxxa51x in the uploaded file: /tmp/old_snapshots.txt
Now deleting snapshot id snap-01x1618xxxxxxa51x

答案1

您需要稍微更改一下腳本。

$ sh abc.sh

What is in delete_data snap-xxxx81xxxxxx7fxxx
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxx81xxxxxx7fxxx
What is in delete_data snap-xxxacc49xxxxxxc26
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxacc49xxxxxxc26
What is in delete_data snap-04xxxxxxxx3fa3cxxxxxxf4
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4

$ cat abc.sh
## actual deletion

    file="/tmp/old_snapshots.txt"
    while read delete_data
    do
    #aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
    echo "What is in delete_data $delete_data"
    echo "What is in file $file"
    echo "Deleting snapshot $delete_data"
    done < $file

您的腳本呼叫file變數是常數,但您需要逐行傳遞檔案內容。所以,替換下面

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $delete_data

答案2

為了其他社區用戶的利益,我使用該--debug標誌發現附加字元'\r'被添加到折斷-#

偵錯輸出

2020-01-28 18:09:02,295 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.ec2.delete-snapshot.snapshot-id: calling handler <awscli.paramfile.URIArgumentHandler object at >
2020-01-28 18:09:02,296 - MainThread - botocore.hooks - DEBUG - Event process-cli-arg.ec2.delete-snapshot: calling handler <awscli.argprocess.ParamShorthandParser object at >
2020-01-28 18:09:02,296 - MainThread - awscli.arguments - DEBUG - Unpacked value of u'snap-0xxxxxxxxxxxxxxxx\r' for parameter "snapshot_id": u'snap-0xxxxxxxxxxxxxxxx\r'

為了解決這個問題:我傳遞tr -d '\r'給保存該值的變數來進行對話。

例子

tr -d '\r' < input > output

更新腳本

file="/tmp/old_snapshots.txt"
cat $file | tr -d '\r' | while read -r line; 
do 
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $line                        
done

相關內容