aws 通过boto3 python脚本打pach的实现方法
作者:Zhiming's Blog 发布时间:2021-09-14 23:33:19
标签:aws,boto3,python,pach
脚本要实现的功能:输入instance id
1:将所有的volume take snapshot
2: 获取public ip 并登陆机器执行 ps 命令记录patch前进程状态已经端口状态
3:获取机器所在的elb
4: 从elb中移除当前机器
5:检查snapshots是否完成
6:snapshots完成后patching
7: patching完成后将instance加回到elb
#!/usr/bin/python
# vim: expandtab:tabstop=4:shiftwidth=4
''' script to get ecr info '''
# Reason: disable invalid-name because pylint does not like our naming convention
# pylint: disable=invalid-name
import time
import boto3
import sys
import argparse
def get_volume(ec2, instanceId):
result = []
instance = ec2.Instance(instanceId)
volumes = instance.volumes.all()
for volume in volumes:
print("Volume attached to this instance is :" + volume.id)
result.append(volume.id)
return result
def take_snapByInstance(client, instanceId):
response = client.create_snapshots(
Description='string',
InstanceSpecification={
'InstanceId': instanceId,
'ExcludeBootVolume': False
},
TagSpecifications=[
{
'ResourceType': 'snapshot',
'Tags': [
{
'Key': 'orginName',
'Value': 'patch backup'+ instanceId
},
]
},
],
DryRun=False,
CopyTagsFromSource='volume'
)
print("Creating new snapshots for instances:" + response['Snapshots'][0]['SnapshotId'])
return response['Snapshots'][0]['SnapshotId']
def get_publicIp(ec2, instanceId):
instance = ec2.Instance(instanceId)
publicIp = instance.public_ip_address
return publicIp
def take_screenshotOfProcess(public_ip):
print("Please run this command on your local machine")
print('ssh -t ' + public_ip + ' "sudo netstat -tnpl > disk.listen"')
print('ssh -t ' + public_ip + ' "sudo ps auxf > disk.ps"')
def get_elbInfo(client_elb, ec2, instanceId):
bals = client_elb.describe_load_balancers()
for elb in bals['LoadBalancerDescriptions']:
#print('ELB DNS Name : ' + elb['DNSName'])
#check if the elb is the elb of instance
if instanceId in elb['Instances']:
print("found elb " + elb['DNSName'])
else:
pass
def remove_fromElb(client_elb, elb, instanceId):
response = client_elb.deregister_instances_from_load_balancer(
LoadBalancerName='elb',
Instances=[
{
'InstanceId': instanceId
},
]
)
def add_backElb(client_elb, elb, instanceId):
response = client.register_instances_with_load_balancer(
LoadBalancerName= elb,
Instances=[
{
'InstanceId': instanceId
},
]
)
def check_snapStatus(ec2, snaps):
snapshot = ec2.Snapshot(snaps)
snapshot.load()
print(snapshot.state)
return snapshot.state
def main(ec2, client, instanceId, client_elb):
print("going to paching instanceid: " + instanceId)
#get volumes
volumes = get_volume(ec2, instanceId)
#get public ip
public_ip = get_publicIp(ec2, instanceId)
#take snapshot
snaps = take_snapByInstance(client, instanceId)
#take screenshot of procss and port
take_screenshotOfProcess(public_ip)
#get elb info
elb = False
#elb = get_elbInfo(client_elb, ec2, instanceId)
#remove from elb
if elb:
ans_remove = input("Are you sure to remove the instance from the elb now? Yes/No")
if ans_remove == 'Yes':
#remove from instance
remove_fromElb(client_elb, elb, instanceId)
#check snapshot status
snapshotStatus = ''
check_snapStatus(ec2, snaps)
print("checking staus of snapshots")
while True:
snapshotStatus = check_snapStatus(ec2, snaps)
print(snapshotStatus)
if snapshotStatus == 'completed':
break
else:
time.sleep(10)
#paching
paching_cmd = 'Your paching command'
print(paching_cmd)
#add to elb
if elb:
ans_add = input("please confirm the patching is over , input yes to continue")
if ans_add == 'Yes':
add_backElb(client_elb, elb, instanceId)
if __name__ == "__main__":
ec2 = boto3.resource('ec2', region_name='us-east-1')
client = boto3.client('ec2', region_name='us-east-1')
client_elb = boto3.client('elb', region_name='us-east-1')
main(ec2, client, 'i-abcasdfa111122', client_elb)
注意,本脚本并未包含链接机器并执行命令的部分,仅仅是打印出命令,需要手动执行 take_screenshotOfProcess
已经patch的命令,此部分也参考之前的文章,完全自动化,不需要手动执行
另外Patch命令脚本中并未给出
来源:https://www.503error.com/2020/aws-通过boto3-python脚本打pach/1761.html
0
投稿
猜你喜欢
- 前言Sphinx是一款支持多种编程语言的文档生成工具,在python项目开发过程中,可以帮助开发者根据需求生成相应的说明文档,拿今天我们就基
- 在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流!为了保证在业务高峰期,线上系统也能保证一定的弹性和稳定性,最有效的方案就是进行服
- 权重初始化对于训练神经网络至关重要,好的初始化权重可以有效的避免梯度消失等问题的发生。在pytorch的使用过程中有几种权重初始化的方法供大
- 简介这两天更新完Xcode8之后发现Xcode对图标的要求又有了变化,之前用的一个小应用“IconKit”还没赶上节奏,已经不能满足Xcod
- 1. 什么是phpDocumentor ? PHPDocumentor 是一个用PHP写的工具,对于有规范注释的php程序,它能够快速生成具
- write()方法把字符串str写入文件。没有返回值。由于缓冲,字符串可能不实际显示文件,直到flush()或close()方法
- 经常在网上冲浪的朋友是否曾注意到有些网站的鼠标不是规则的斜向上箭头的形状,而是"十"
- 编写兼容IE和FireFox的脚本确定的件很烦人的事,今日又经历了一次。一、正式表达式问题试图用以下表达式提取中括号“]”后面的内容,连接调
- Python(包括其包Numpy)中包含了了许多概率算法,包括基础的随机采样以及许多经典的概率分布生成。我们这个系列介绍几个在机
- 首先,先确认一下你的字段值是不是乱码,如果是,按照以下方法:我的字段值是来自于一个geojson字符串,我在对它解析时做了如下处理:prop
- 1、需求我们的代码已经变得无法阅读,到处都是硬编码的切片索引,我们想优化他们。2、解决方案代码中如果有很多硬编码的索引值,将导致可读性和维护
- function MakeUrl($arr){  
- 带农历的JavaScript日期时间,增加了农历使这款代码更具实用性,很不错,不但有农历,而且还可以显示民国计年,可以判断显示闰月、闰年等,
- 1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。最爽的是,PH
- 本文实例为大家分享了python实现简易学生信息管理系统的具体代码,供大家参考,具体内容如下一、系统功能1.录入学生信息2.查找学生信息3.
- 这篇文章主要介绍了Python基于内置库pytesseract实现图片验证码识别功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具
- 什么是数据库镜像?Robidoux:数据库镜像是将数据库事务处理从一个SQL Server数据库移动到不同SQL Server环境中的另一个
- 关于本文:本着互联网共享主义精神,特写此文献给建站新手,授人以鱼,不如授人以渔,本文所讲的只是方法和原理,希望大家看完此文,能够从中得到些帮
- 二分查找法(Binary Search)是一种在有序数组中查找某一特定元素的算法,它的思想是将数组从中间分成两部分,判断目标元素在哪一部分中
- 本文实例为大家分享了Python九宫格图片生成的具体代码,供大家参考,具体内容如下利用Image类将一张图片分割成9张,发朋友圈利器,打包成