Massenlöschung von EC2-Snapshots aus Jenkins

Massenlöschung von EC2-Snapshots aus Jenkins

Anwendungsfall:
Exportieren Sie die Liste der Snapshots in der TXT-Datei (old_snapshots.txt) in den S3-Bucket. Verwenden Sie in Jenkins den Befehl „aws cp“, um die Datei in das Jenkins-Verzeichnis „/tmp/“ zu kopieren.

--dry-run did not show any error 

Wenn jedoch die folgende Bash-Zeile mit dem AWS-Delete-Befehl in Jenkins übergeben wird, wird „SUCCESS“ angezeigt, gleichzeitig wird angezeigt, dass ein Fehler aufgetreten ist und die Snapshots nicht gelöscht werden.

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

Ausgabe

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

Anfrage:Dieser Fehler An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operationscheint zu allgemein zu sein? Hat jemand dieses Problem schon erlebt? Außerdem habe ich einen manuellen Test durchgeführt, bei dem ich mit AWS CLI nur einen einzigen Snapshot gelöscht habe, indem ich die Snapshot-ID manuell in Jenkins hinzugefügt habe, und das funktioniert einwandfrei. Jeder Vorschlag wäre sehr willkommen.

Einschließlich AWS-Befehl

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

Antwort1

Sie müssen Ihr Skript ein wenig ändern.

$ 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

Ihr Skript ruft filedie Variable konstant auf, aber Sie müssen den Inhalt der Datei zeilenweise übergeben. Ersetzen Sie also unten

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

Mit

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

Antwort2

Zum Nutzen anderer Community-Benutzer habe ich die --debugFlagge verwendet, um festzustellen, dass zusätzliche Zeichen '\r'hinzugefügt wurdenSchnappschuss

Debug-Ausgabe

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'

So lösen Sie das Problem: Ich habe es tr -d '\r'an eine Variable übergeben, die den Wert enthält, um die Konversation zu ermöglichen.

Beispiel:

tr -d '\r' < input > output

Aktualisiertes Skript

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

verwandte Informationen