AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1000670
Accepted
Sunny J
Sunny J
Asked: 2020-01-28 21:28:37 +0800 CST2020-01-28 21:28:37 +0800 CST 2020-01-28 21:28:37 +0800 CST

从 Jenkins 批量删除 ec2 快照

  • 772

用例:
将 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
snapshot amazon-web-services jenkins aws-cli deleting
  • 2 2 个回答
  • 763 Views

2 个回答

  • Voted
  1. asktyagi
    2020-01-28T22:14:27+08:002020-01-28T22:14:27+08:00

    你需要稍微改变你的脚本。

    $ 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
    
    • 1
  2. Best Answer
    Sunny J
    2020-01-29T10:59:16+08:002020-01-29T10:59:16+08:00

    为了其他社区用户的利益,我使用该--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
    
    • 0

相关问题

  • OpenBSD 和文件系统快照

  • LVM 快照是这样工作的吗?

  • 将 vmware 快照转换为 vdi

  • LVM 和快照

  • 加快 MSSQL 快照复制到 SQLExpress 副本的速度

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve