Amazon-Web-Services

從 Jenkins 批量刪除 ec2 快照

  • January 28, 2020

案例:

將 txt 文件 (old_snapshots.txt) 中的快照列表導出到 S3 儲存桶。從 Jenkins,使用 aws cp 命令將文件複製到 Jenkins /tmp/directory

--dry-run did not show any error 

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

## 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

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

調試輸出

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

你需要稍微改變你的腳本。

$ 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

引用自:https://serverfault.com/questions/1000670