我试图让jenkins从之前的快照中自动构建一个新的ec2实例。基本上我希望Jenkins能够从我的快照创建一个新卷,然后将该卷附加到一个实例并启动它。 这可能吗 ??花了几天时间搜索所有CI页面,但不能把它拼凑起来做我想做的事情
答案 0 :(得分:0)
我一直在做的方法是首先从快照创建一个卷,然后创建一个实例,然后将该卷附加到该实例。像这样:
# Create a volume from a snapshot:
def createVolume(snapshot_id=None, volume_type=None, volume_size=None, region=None, az=None):
say('Creating volume...', banner="*")
snapshot_arg = ' --snapshot-id {}'.format(snapshot_id)
if snapshot_id is None:
snapshot_arg = ''
volume_size = '' if volume_size is None else ' --size ' + volume_size
cmd = 'aws ec2 create-volume --region ' + region + \
' --availability-zone ' + az + ' ' + \
snapshot_arg + \
' --volume-type ' + volume_type + volume_size
output, returncode = run(cmd, hide_command=g_hide_command, debug=g_args.debug)
volume_id = json.loads(output)['VolumeId']
size = json.loads(output)['Size']
while True:
cmd = 'aws ec2 describe-volumes --volume-ids ' + str(volume_id)
output, returncode = run(cmd, hide_command=g_hide_command, debug=g_args.debug)
state = json.loads(output)['Volumes'][0]['State']
if state == 'available':
say('Volume has been created: ' + str(volume_id))
break
else:
say('Current State: ' + str(state))
time.sleep(15)
cmd = 'aws ec2 create-tags --resources ' + str(volume_id) + ' --tags Key=Name,Value=jenkins-volume'
output, returncode = run(cmd, hide_command=g_hide_command, debug=g_args.debug)
return volume_id, size
创建新卷后,您可以创建实例并将该卷附加到实例:
new_instance = createInstance(ami_id=g_args.ami_id,
instance_type=g_args.instance_type,
target_env=g_args.target_env,
ssh_user=g_args.ssh_user,
id_rsa=g_args.id_rsa,
key_name='jenkins.cloud',
tags_dict=tags,
security_group_ids=g_env_map[g_args.target_env]['jenkins-sg'],
instance_profile=instance_profile,
subnet_id=subnet_id,
debug=g_args.debug)
# Attach volume to this instance (we reset the new_instance variable because the EBS vol has been attached):
new_instance = attacheVolume(volume_id=volume_id, instance_id=str(new_instance['InstanceId']), region=g_env_map[g_args.target_env]['region'])
我没有提供各种功能的内容,例如createInstance和attachVolume(和#34;说"和"运行"),但是你得到了要点。