def main(args): import datetime from amitools.dt import dateparse from amitools.util import ( calc_tags, tag_image, ) from amitools import ec2connect if args.create_date is None: when = datetime.datetime.utcfromtimestamp(args.create_timestamp) else: assert args.create_timestamp is None, args.create_timestamp when = dateparse(args.create_date) conn = ec2connect(args.region) tags = calc_tags( args.source_image, args.source_instance, args.source_region, when, ) tag_image( conn, args.image_id, tags, )
def main(args): from amitools import ( EC2ImageWatcher, ec2connect, ) from amitools.exceptions import TimeoutException conn = ec2connect(args.region) image_watcher = EC2ImageWatcher(args.image_id, conn) current_state = image_watcher.state() if current_state is None: if args.nowait_exists: return { 'msg': 'ERROR\tNo image ID {}'.format(args.image_id), 'exit_code': 1, } try: current_state = image_watcher.waiton_exists() except TimeoutException as ex: return { 'msg': 'FATAL\tTimed out waiting for image ID {} to exist'.format( args.image_id), 'exit_code': 2, } assert current_state is not None if 'exists' != args.state: if not can_reach(current_state, args.state): return { 'msg': 'FATAL\tCurrent state of "{}" can never transition to target of "{}"' .format(current_state, args.state), 'exit_code': 3, } if current_state != args.state: try: image_watcher.waiton(args.state) except TimeoutException as ex: return { 'msg': 'FATAL\tTimed out waiting for image ID {} to reach state {} - lastest was {}' .format(args.image_id, args.state, current_state), 'exit_code': 1, }
def main(args): from amitools import ( ec2connect, build_chain, ) conn = ec2connect(args.region) all_images = conn.get_all_images(filters={'tag-key':'source_image'}) try: chain = build_chain(args.image_id, all_images) exit_code = CHAIN_SUCCESS except AssertionError: chain = [] exit_code = CHAIN_NO_AMI return { 'exit_code' : exit_code, 'chain' : chain, }
def main(args): import datetime import time from amitools.watch import EC2ImageWatcher from amitools.util import ( get_instance, random_name, ) from amitools import ec2connect from amitools.util import ( calc_tags, tag_image, ) if args.name is None: args.name = random_name(args.random_name_prefix) conn = ec2connect(args.region) image_id = conn.create_image( args.instance_id, args.name, description=args.description, no_reboot = args.no_reboot, ) image_watcher = EC2ImageWatcher(image_id, conn) image_watcher.waiton_exists() # AMI image creation started. While that's cooking, set up tags image = image_watcher.resource source_instance = get_instance(conn, args.instance_id) source_region = source_instance.region.name tags = calc_tags( source_instance.image_id, args.instance_id, source_region, datetime.datetime.utcnow(), ) tag_image( conn, image.id, tags, ) return { 'image_id' : image_id, }
def main(args): import datetime import time from amitools.watch import EC2ImageWatcher from amitools.util import ( get_instance, random_name, ) from amitools import ec2connect from amitools.util import ( calc_tags, tag_image, ) if args.name is None: args.name = random_name(args.random_name_prefix) conn = ec2connect(args.region) image_id = conn.create_image( args.instance_id, args.name, description=args.description, no_reboot=args.no_reboot, ) image_watcher = EC2ImageWatcher(image_id, conn) image_watcher.waiton_exists() # AMI image creation started. While that's cooking, set up tags image = image_watcher.resource source_instance = get_instance(conn, args.instance_id) source_region = source_instance.region.name tags = calc_tags( source_instance.image_id, args.instance_id, source_region, datetime.datetime.utcnow(), ) tag_image( conn, image.id, tags, ) return { 'image_id': image_id, }
def _setupClass(cls): # silence boto's verbose debug logging, which can otherwise # produce hundreds of lines of output if a test fails cls.old_boto_loglevel = logging.getLogger('boto').level new_boto_loglevel = logging.CRITICAL cls.logc('Changing boto log level from "%s" to "%s" during tests' % (cls.old_boto_loglevel, new_boto_loglevel)) logging.getLogger('boto').setLevel(new_boto_loglevel) from amitools.watch import EC2InstanceWatcher cls.conn = ec2connect() params = { 'image_id' : DEV_AMI, } cls.logc('Running instance with params: ' + str(params)) reservation = cls.conn.run_instances(**params) assert len(reservation.instances) == 1, reservation.instances cls.instance = reservation.instances[0] cls.logc('Launched instance %s - blocking until it is running' % cls.instance.id) EC2InstanceWatcher(cls.instance.id, cls.conn).waiton('running') cls.logc('Dev instance %s in "running" state, ready for tests' % cls.instance.id)
def main(args): from amitools import ( EC2ImageWatcher, ec2connect, ) from amitools.exceptions import TimeoutException conn = ec2connect(args.region) image_watcher = EC2ImageWatcher(args.image_id, conn) current_state = image_watcher.state() if current_state is None: if args.nowait_exists: return { 'msg' : 'ERROR\tNo image ID {}'.format(args.image_id), 'exit_code' : 1, } try: current_state = image_watcher.waiton_exists() except TimeoutException as ex: return { 'msg' : 'FATAL\tTimed out waiting for image ID {} to exist'.format(args.image_id), 'exit_code' : 2, } assert current_state is not None if 'exists' != args.state: if not can_reach(current_state, args.state): return { 'msg' : 'FATAL\tCurrent state of "{}" can never transition to target of "{}"'.format(current_state, args.state), 'exit_code' : 3, } if current_state != args.state: try: image_watcher.waiton(args.state) except TimeoutException as ex: return { 'msg' : 'FATAL\tTimed out waiting for image ID {} to reach state {} - lastest was {}'.format( args.image_id, args.state, current_state), 'exit_code' : 1, }
def _setupClass(cls): # silence boto's verbose debug logging, which can otherwise # produce hundreds of lines of output if a test fails cls.old_boto_loglevel = logging.getLogger('boto').level new_boto_loglevel = logging.CRITICAL cls.logc('Changing boto log level from "%s" to "%s" during tests' % (cls.old_boto_loglevel, new_boto_loglevel)) logging.getLogger('boto').setLevel(new_boto_loglevel) from amitools.watch import EC2InstanceWatcher cls.conn = ec2connect() params = { 'image_id': DEV_AMI, } cls.logc('Running instance with params: ' + str(params)) reservation = cls.conn.run_instances(**params) assert len(reservation.instances) == 1, reservation.instances cls.instance = reservation.instances[0] cls.logc('Launched instance %s - blocking until it is running' % cls.instance.id) EC2InstanceWatcher(cls.instance.id, cls.conn).waiton('running') cls.logc('Dev instance %s in "running" state, ready for tests' % cls.instance.id)