Excluir em massa instantâneos ec2 do Jenkins

Excluir em massa instantâneos ec2 do Jenkins

Caso de uso:
Exporte a lista de snapshots em arquivo txt (old_snapshots.txt) para o bucket S3. No Jenkins, use o arquivo de cópia do comando aws cp para Jenkins /tmp/directory

--dry-run did not show any error 

No entanto, quando passada a seguinte linha bash com o comando aws delete no Jenkins, ele mostra SUCESSO, ao mesmo tempo que diz que ocorreu um erro e os instantâneos não foram excluídos.

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

Saída

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

Solicitar:Este erro An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operationparece muito genérico? Alguém já experimentou um problema de aparência? Além disso, fiz um teste manual de exclusão de apenas um único instantâneo com aws cli adicionando manualmente o ID do instantâneo no Jenkins e isso funciona bem. Qualquer sugestão seria muito apreciada.

Incluindo comando 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

Responder1

Você precisa mudar um pouco seu roteiro.

$ 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

Seu script está chamando filea variável é constante, mas você precisa passar o conteúdo do arquivo linha por linha. Então, substitua abaixo

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

Com

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

Responder2

Para o benefício de outros usuários da comunidade, usei o --debugsinalizador para descobrir que caracteres adicionais '\r'estavam sendo adicionados aofoto-#

Saída de depuração

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'

Para resolver o problema: passei tr -d '\r'para a variável que contém o valor, para fazer a conversa.

Exemplo:

tr -d '\r' < input > output

Roteiro atualizado

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

informação relacionada