def test_ec2_client(self): ec2 = boto.ec2.connect_to_region("us-west-2") Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) ec2.get_all_instances() spans = self.pop_spans() assert spans self.assertEqual(len(spans), 1) span = spans[0] self.assertEqual(span.get_tag("aws.operation"), "DescribeInstances") assert_span_http_status_code(span, 200) self.assertEqual(span.get_tag(http.METHOD), "POST") self.assertEqual(span.get_tag("aws.region"), "us-west-2") self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY)) # Create an instance ec2.run_instances(21) spans = self.pop_spans() assert spans self.assertEqual(len(spans), 1) span = spans[0] assert_is_measured(span) self.assertEqual(span.get_tag("aws.operation"), "RunInstances") assert_span_http_status_code(span, 200) self.assertEqual(span.get_tag(http.METHOD), "POST") self.assertEqual(span.get_tag("aws.region"), "us-west-2") self.assertEqual(span.service, "test-boto-tracing.ec2") self.assertEqual(span.resource, "ec2.runinstances") self.assertEqual(span.name, "ec2.command") self.assertEqual(span.span_type, "http")
def test_ec2_client(self): ec2 = boto.ec2.connect_to_region("us-west-2") tracer = get_dummy_tracer() writer = tracer.writer Pin(service=self.TEST_SERVICE, tracer=tracer).onto(ec2) ec2.get_all_instances() spans = writer.pop() assert spans eq_(len(spans), 1) span = spans[0] eq_(span.get_tag('aws.operation'), "DescribeInstances") eq_(span.get_tag(http.STATUS_CODE), "200") eq_(span.get_tag(http.METHOD), "POST") eq_(span.get_tag('aws.region'), "us-west-2") # Create an instance ec2.run_instances(21) spans = writer.pop() assert spans eq_(len(spans), 1) span = spans[0] eq_(span.get_tag('aws.operation'), "RunInstances") eq_(span.get_tag(http.STATUS_CODE), "200") eq_(span.get_tag(http.METHOD), "POST") eq_(span.get_tag('aws.region'), "us-west-2") eq_(span.service, "test-boto-tracing.ec2") eq_(span.resource, "ec2.runinstances") eq_(span.name, "ec2.command")
def resolve_instances(arg, filters=None): inst = None if arg == 'latest': r = ec2.get_all_instances(filters=filters) li = sorted(list_instances(r), key=lambda i: i.launch_time) if li: return li[-1:] else: return [] m = re_inst_id.match(arg) if m: if len(arg) == 10: r = ec2.get_all_instances(instance_ids=[arg]) return list_instances(r) else: # partial id return [ i for i in iter_instances(ec2.get_all_instances()) if i.id.startswith(arg) ] m = re_tag.match(arg) if m: r = ec2.get_all_instances(filters={'tag:%s' % m.group(1): m.group(2)}) return list_instances(r) return []
def test_analytics_enabled_without_rate(self): ec2 = boto.ec2.connect_to_region("us-west-2") ec2.get_all_instances() spans = self.memory_exporter.get_finished_spans() assert spans
def test_ec2_client(self): ec2 = boto.ec2.connect_to_region("us-west-2") ec2.get_all_instances() spans = self.memory_exporter.get_finished_spans() assert spans self.assertEqual(len(spans), 1) span = spans[0] self.assertEqual(span.attributes["aws.operation"], "DescribeInstances") assert_span_http_status_code(span, 200) self.assertEqual(span.attributes["http.method"], "POST") self.assertEqual(span.attributes["aws.region"], "us-west-2") # Create an instance ec2.run_instances(21) spans = self.memory_exporter.get_finished_spans() assert spans self.assertEqual(len(spans), 2) span = spans[1] self.assertEqual(span.attributes["aws.operation"], "RunInstances") assert_span_http_status_code(span, 200) self.assertEqual( span.resource, Resource(attributes={ "endpoint": "ec2", "http_method": "runinstances" }), ) self.assertEqual(span.attributes["http.method"], "POST") self.assertEqual(span.attributes["aws.region"], "us-west-2") self.assertEqual(span.name, "ec2.command")
def resolve_instances(arg, filters=None): inst = None if arg == 'latest': r = ec2.get_all_instances(filters=filters) li = sorted(list_instances(r), key=lambda i:i.launch_time) if li: return li[-1:] else: return [] m = re_inst_id.match(arg) if m: if len(arg) == 10: r = ec2.get_all_instances(instance_ids=[arg]) return list_instances(r) else: # partial id return [ i for i in iter_instances(ec2.get_all_instances()) if i.id.startswith(arg) ] m = re_tag.match(arg) if m: r = ec2.get_all_instances(filters={'tag:%s' % m.group(1): m.group(2)}) return list_instances(r) return []
def test_ec2_client(self): ec2 = boto.ec2.connect_to_region('us-west-2') writer = self.tracer.writer Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) ec2.get_all_instances() spans = writer.pop() assert spans self.assertEqual(len(spans), 1) span = spans[0] self.assertEqual(span.get_tag('aws.operation'), 'DescribeInstances') self.assertEqual(span.get_tag(http.STATUS_CODE), '200') self.assertEqual(span.get_tag(http.METHOD), 'POST') self.assertEqual(span.get_tag('aws.region'), 'us-west-2') self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY)) # Create an instance ec2.run_instances(21) spans = writer.pop() assert spans self.assertEqual(len(spans), 1) span = spans[0] self.assertEqual(span.get_tag('aws.operation'), 'RunInstances') self.assertEqual(span.get_tag(http.STATUS_CODE), '200') self.assertEqual(span.get_tag(http.METHOD), 'POST') self.assertEqual(span.get_tag('aws.region'), 'us-west-2') self.assertEqual(span.service, 'test-boto-tracing.ec2') self.assertEqual(span.resource, 'ec2.runinstances') self.assertEqual(span.name, 'ec2.command') self.assertEqual(span.span_type, 'http')
def test_analytics_enabled_without_rate(self): with self.override_config("boto", dict(analytics_enabled=True)): ec2 = boto.ec2.connect_to_region("us-west-2") Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) ec2.get_all_instances() spans = self.pop_spans() assert spans span = spans[0] self.assertEqual(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY), 1.0)
def test_not_recording(self): mock_tracer = Mock() mock_span = Mock() mock_span.is_recording.return_value = False mock_tracer.start_span.return_value = mock_span with patch("opentelemetry.trace.get_tracer") as tracer: tracer.return_value = mock_tracer ec2 = boto.ec2.connect_to_region("us-west-2") ec2.get_all_instances() self.assertFalse(mock_span.is_recording()) self.assertTrue(mock_span.is_recording.called) self.assertFalse(mock_span.set_attribute.called) self.assertFalse(mock_span.set_status.called)
def test_analytics_enabled_with_rate(self): with self.override_config( 'boto', dict(analytics_enabled=True, analytics_sample_rate=0.5)): ec2 = boto.ec2.connect_to_region('us-west-2') writer = self.tracer.writer Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) ec2.get_all_instances() spans = writer.pop() assert spans span = spans[0] self.assertEqual(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY), 0.5)
def get_instances(kwargs): """Print list of instances in specific account and region.""" result = [] region = kwargs.get('region') profile = kwargs.get('profile') try: ec2 = boto.ec2.connect_to_region(region, profile_name=profile) log.info('Getting list of instances from %s: %s', profile, region) # Get only running instances reservations = ec2.get_all_instances(filters={'instance-state-name':'running'}) for reservation in reservations: for instance in reservation.instances: # This result will be returned for future use result.append(instance.tags.get('Name', instance.id)) print('{instance_id},{name},{private_ip},{public_ip},{instance_type},{region},{profile}'.format( instance_id = instance.id, name = instance.tags.get('Name'), private_ip = instance.private_ip_address, public_ip = instance.ip_address, instance_type = instance.instance_type, region = region, profile = profile, )) except boto.exception.EC2ResponseError as e: log.error('Error list instances in %s: %s (%s)', profile, region, e) finally: return result
def get_region_instances(auth, region, tables): """Get running instances""" table = PrettyTable([ "Name", "Key-Name", "Type", "Placement", "Public-DNS", "Public-IP", "Private-IP", "Instance-ID", "State", "Launch Time" ]) table.padding_width = 1 ec2 = boto.ec2.connect_to_region( region.name, aws_access_key_id=auth.aws_access_key_id, aws_secret_access_key=auth.aws_secret_access_key) reservations = ec2.get_all_instances() if reservations: for reservation in reservations: for i in reservation.instances: try: instance_name = i.tags['Name'] except KeyError: instance_name = "N/A" table.add_row([ instance_name, i.key_name, i.instance_type, i.placement, i.public_dns_name, i.ip_address, i.private_ip_address, i.id, i.state, i.launch_time ]) tables[region.name] = table return
def get_region_instances(region, tables, auth): """Collects all instances in given region, using given authentication and passes it back in given table.""" table = PrettyTable([ "Name", "Key-Name", "Type", "Placement", "Public-DNS", "Private-IP", "Instance-ID", "State", "Launch Time" ]) table.padding_width = 1 ec2 = boto.ec2.connect_to_region( region.name, aws_access_key_id=auth.aws_access_key_id, aws_secret_access_key=auth.aws_secret_access_key) if ec2: reservations = ec2.get_all_instances() if reservations: for reservation in reservations: for i in reservation.instances: try: instance_name = i.tags['Name'] except KeyError: instance_name = "N/A" if i.public_dns_name: accessname = i.public_dns_name elif i.ip_address: accessname = i.ip_address else: accessname = "n/a" table.add_row([ instance_name, i.key_name, i.instance_type, i.placement, accessname, i.private_ip_address, i.id, i.state, i.launch_time ]) tables[region.name] = table return
def all_instances(): reservations = ec2.get_all_instances(filters={'instance-state-code': '16'}) print '\nALL RUNNING EC2 INSTANCES' print '-------------------------------------------' allins = [] countins = [] i = 0 for a in reservations: for b in a.instances: allins1 = b.instance_type, b.tags.get("cluster") allins.append(allins1) i += 1 print "\nTOTAL NUMBER OF EC2 INSTANCES = ", i for a in reservations: for b in a.instances: countins1 = b.instance_type try: names = b.instance_type, b.tags['Name'] all_ins_names.append(names) except: break countins.append(countins1) disins = Counter(countins) disins2 = sorted(disins.keys()) print "\nEC2 INSTANCE TYPE BREAKDOWN ", i for x in disins2: print x, disins[x]
def get_ec2_instances(self, hosts): ec2 = self.connect_ec2() instance_ids = [i.instance_id for i in hosts] return [ i for r in ec2.get_all_instances(instance_ids) for i in r.instances ]
def _find_active_instances_for(args, ec2, user_name): return ec2.get_all_instances(filters={ 'tag:saved_for_user': user_name, 'instance-state-name': [ 'pending', 'running', 'stopping', 'stopped', ] })
def main(): argument_spec = ec2_argument_spec() argument_spec.update( dict( name=dict(), instance_ids=dict(), states=dict(), tags=dict(type='dict'), )) module = AnsibleModule(argument_spec=argument_spec) instance_ids = module.params.get('instance_ids') tags = module.params.get('tags') states = module.params.get('states') name = module.params.get('name') ec2 = ec2_connect(module) try: filters = {} if name: filters['tag:Name'] = name if states: filters['instance-state-name'] = states if tags: for key in tags.keys(): filters['tag:' + key] = tags[key] reservations = ec2.get_all_instances(instance_ids=instance_ids, filters=filters) except boto.exception.BotoServerError, e: module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
def f_stop(ec2, vpc, objects): # Create list of objects to print instance_list, instance_print, vpn_list, vpn_print = c_object_list(objects) if instance_print: # Stop instance print('Instance Stopping Process >>>') instances = ec2.get_all_instances() for inst in instances: if inst.instances[0].id in instance_list or len(instance_list[0]) == 0: if inst.instances[0].state == 'running': ec2.stop_instances(instance_ids=inst.instances[0].id) print('ID: ' + inst.instances[0].id + ' has been stopped.') elif inst.instances[0].state == 'stopped': print('ID: ' + inst.instances[0].id + ' is already stopped.') else: print('ID: ' + inst.instances[0].id + ' is under the ' + inst.instances[0].state + ' state.') if vpn_print: # Print vpn information print('\n' + 'Vpn Stopping Process >>>') vpns = vpc.get_all_vpn_connections() for v in vpns: if v.state == 'available': vpc.delete_vpn_connection(v.id, dry_run=False) print('ID: ' + v.id + ' has been deleted.') elif v.state == 'deleted': print('ID: ' + v.id + ' is already deleted.') else: print('ID: ' + v.id + ' is under the ' + v.state + ' state.') return
def handle_forward(id, event, qstate, qdata): global TTL qname = qstate.qinfo.qname_str msg = DNSMessage(qname, RR_TYPE_A, RR_CLASS_IN, PKT_QR | PKT_RA | PKT_AA) reservations = ec2.get_all_instances(filters={ "instance-state-name": "running", "tag:Name": qname.strip("."), }) instances = [ instance for reservation in reservations for instance in reservation.instances ] if len(instances) == 0: qstate.return_rcode = RCODE_NXDOMAIN else: qstate.return_rcode = RCODE_NOERROR random.shuffle(instances) for instance in instances: address = determine_address(instance) record = "%s %d IN A %s" % (qname, TTL, address) msg.answer.append(record) if not msg.set_return_msg(qstate): qstate.ext_state[id] = MODULE_ERROR return True qstate.return_msg.rep.security = 2 qstate.ext_state[id] = MODULE_FINISHED return True
def get_region_instances(auth, region, tables): """Get running instances""" table = PrettyTable(["Name", "Key-Name", "Type", "Placement", "Public-DNS", "Public-IP", "Private-IP", "Instance-ID", "State", "Launch Time"]) table.padding_width = 1 ec2 = boto.ec2.connect_to_region(region.name, aws_access_key_id=auth.aws_access_key_id, aws_secret_access_key= auth.aws_secret_access_key) reservations = ec2.get_all_instances() if reservations: for reservation in reservations: for i in reservation.instances: try: instance_name = i.tags['Name'] except KeyError: instance_name = "N/A" table.add_row([ instance_name, i.key_name, i.instance_type, i.placement, i.public_dns_name, i.ip_address, i.private_ip_address, i.id, i.state, i.launch_time ]) tables[region.name] = table return
def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict( name = dict(), instance_ids = dict(), states = dict(), tags = dict(type='dict'), ) ) module = AnsibleModule(argument_spec=argument_spec) instance_ids = module.params.get('instance_ids') tags = module.params.get('tags') states = module.params.get('states') name = module.params.get('name') ec2 = ec2_connect(module) try: filters = {} if name: filters['tag:Name'] = name if states: filters['instance-state-name'] = states if tags: for key in tags.keys(): filters['tag:' + key] = tags[key] reservations = ec2.get_all_instances(instance_ids=instance_ids, filters=filters) except boto.exception.BotoServerError, e: module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
def handle_forward(id, event, qstate, qdata): global TTL qname = qstate.qinfo.qname_str msg = DNSMessage(qname, RR_TYPE_A, RR_CLASS_IN, PKT_QR | PKT_RA | PKT_AA) reservations = ec2.get_all_instances(filters={ "instance-state-name": "running", "tag:Name": qname.strip("."), }) instances = [instance for reservation in reservations for instance in reservation.instances] if len(instances) == 0: qstate.return_rcode = RCODE_NXDOMAIN else: qstate.return_rcode = RCODE_NOERROR random.shuffle(instances) for instance in instances: address = determine_address(instance) record = "%s %d IN A %s" % (qname, TTL, address) msg.answer.append(record) if not msg.set_return_msg(qstate): qstate.ext_state[id] = MODULE_ERROR return True qstate.return_msg.rep.security = 2 qstate.ext_state[id] = MODULE_FINISHED return True
def get_ec2_instances(self, hosts): ec2 = self.connect_ec2() instance_ids = [i.instance_id for i in hosts] return [i for r in ec2.get_all_instances(instance_ids) for i in r.instances]
def start_vm_openstack(num_vms): """ Create a VM instance on OpenStack using the ec2 boto interface commands Args: Set the EC2_ACCESS_KEY and EC2_SECRET_KEY environment variables num_vms: Number of vms to spawn Returns: """ access_key = os.getenv("EC2_ACCESS_KEY") secret_key = os.getenv("EC2_SECRET_KEY") ec2=None try: region = boto.regioninfo.RegionInfo(name="openstack", endpoint="206.117.53.134") ec2 = boto.connect_ec2(aws_access_key_id=access_key, aws_secret_access_key=secret_key, is_secure=False, region=region, port=8773, path="/services/Cloud") except: print 'Error connection to openstack' return ## the machine image ami = "ami-00000001" ## start a bunch of vms for count in range(0,6): reservations=ec2.run_instances(ami,instance_type='m1.tiny',max_count=1) print "Launching vm ",count," of 256" ## create a chain (ie. Make in iterator that returns elements fromt the first ## iterable untils it is exhaused, then proceeds to the next iterable chain = itertools.chain.from_iterable existing_instances = list(chain([res.instances for res in ec2.get_all_instances()])) status = [e.update() for e in existing_instances] ## wait until all the instances have transitioned from 'pending' to 'running' status while 'pending' in status: existing_instances = list(chain([res.instances for res in ec2.get_all_instances()])) status = [e.update() for e in existing_instances] status_dict = dict(zip(existing_instances,status)) print [(i,status_dict[i]) for i in status_dict.keys() if status_dict[i] != 'terminated'] print 'Still pending' time.sleep(3) print 'Complete' return
def run(self): #def ec2_status(): global conn, globalconf, awsconf, BASE_URL, info, last if 'endpoint' in globalconf: BASE_URL = '%s/alerta/app/v1' % globalconf['endpoint'] url = '%s/alerts?%s' % (BASE_URL, awsconf.get('filter', 'tags=cloud:AWS/EC2')) if 'proxy' in globalconf: os.environ['http_proxy'] = globalconf['proxy']['http'] os.environ['https_proxy'] = globalconf['proxy']['https'] last = info.copy() info = dict() for account, keys in awsconf['accounts'].iteritems(): access_key = keys.get('aws_access_key_id', '') secret_key = keys.get('aws_secret_access_key', '') logging.debug('AWS Account=%s, AwsAccessKey=%s, AwsSecretKey=************************************%s', account, access_key, secret_key[-4:]) for region in awsconf['regions']: try: ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key) except boto.exception.EC2ResponseError, e: logging.warning('EC2 API call connect_to_region(region=%s) failed: %s', region, e) continue logging.info('Get all instances for account %s in %s', account, region) try: reservations = ec2.get_all_instances() except boto.exception.EC2ResponseError, e: logging.warning('EC2 API call get_all_instances() failed: %s', e) continue instances = [i for r in reservations for i in r.instances if i.tags] for i in instances: info[i.id] = dict() info[i.id]['state'] = i.state info[i.id]['stage'] = i.tags.get('Stage', 'unknown') info[i.id]['role'] = i.tags.get('Role', 'unknown') info[i.id]['tags'] = ['os:Linux', 'role:%s' % info[i.id]['role'], 'datacentre:%s' % region, 'virtual:xen', 'cloud:AWS/EC2', 'account:%s' % account] info[i.id]['tags'].append('cluster:%s_%s' % ( info[i.id]['role'], region)) # FIXME - replace match on cluster with match on role # FIXME - this is a hack until all EC2 instances are keyed off instance id logging.debug('%s -> %s', i.private_dns_name, i.id) lookup[i.private_dns_name.split('.')[0]] = i.id logging.info('Get system and instance status for account %s in %s', account, region) try: status = ec2.get_all_instance_status() except boto.exception.EC2ResponseError, e: logging.warning('EC2 API call get_all_instance_status() failed: %s', e) continue
def lookup_instance_by_name(qname): reservations = ec2.get_all_instances(filters={ "instance-state-name": "running", "tag:Name": qname.strip("."), }) return [instance for reservation in reservations for instance in reservation.instances]
def terminate_submaster(subMasterID): ec2 = boto.connect_ec2() reservations = ec2.get_all_instances(filters={"tag:Name":str(subMasterID)+'Submaster',"instance-state-name":'running'}) try: reservations[0].instances[0].terminate() except: print 'submaster is not running'
def get_tagged_ec2_instances(region, tag): ec2 = boto.ec2.connect_to_region(region) reservations = ec2.get_all_instances(filters={ "tag:Group": tag }) instances = [i for r in reservations for i in r.instances] return instances
def startstop_instances(module, ec2, instance_ids, state): """ Starts or stops a list of existing instances module: Ansible module object ec2: authenticated ec2 connection object instance_ids: The list of instances to start in the form of [ {id: <inst-id>}, ..] state: Intended state ("running" or "stopped") Returns a dictionary of instance information about the instances started/stopped. If the instance was not able to change state, "changed" will be set to False. """ wait = module.params.get('wait') wait_timeout = int(module.params.get('wait_timeout')) changed = False instance_dict_array = [] if not isinstance(instance_ids, list) or len(instance_ids) < 1: module.fail_json( msg='instance_ids should be a list of instances, aborting') # Check (and eventually change) instances attributes and instances state running_instances_array = [] for res in ec2.get_all_instances(instance_ids): for inst in res.instances: # Check "source_dest_check" attribute if inst.get_attribute( 'sourceDestCheck')['sourceDestCheck'] != source_dest_check: inst.modify_attribute('sourceDestCheck', source_dest_check) changed = True # Check "termination_protection" attribute if inst.get_attribute('disableApiTermination')[ 'disableApiTermination'] != termination_protection: inst.modify_attribute('disableApiTermination', termination_protection) changed = True # Check instance state if inst.state != state: instance_dict_array.append(get_instance_info(inst)) try: if state == 'running': inst.start() else: inst.stop() except EC2ResponseError, e: module.fail_json( msg='Unable to change state for instance {0}, error: {1}' .format(inst.id, e)) changed = True
def update_instance_state(instance_id, instance_state): """ update instance state """ ec2 = boto.ec2.connect_to_region("us-east-1", **auth) instance = ec2.get_all_instances(instance_ids=instance_id) if instance_state == AWS_INSTANCE_STATE_START: print("starting instance %s" % instance_id) instance[0].instances[0].start() elif instance_state == AWS_INSTANCE_STATE_STOP: instance[0].instances[0].stop()
def get_status(): ec2 = conn_region reservations = ec2.get_all_instances(filters={'instance-state-name': 'running'}) instances = [i for r in reservations for i in r.instances] for instance in instances: if instance.__dict__['tags'].__dict__['_current_key'] == tag_name: print instance
def ec2_get_all_instances(): ec2 = boto.ec2.connect_to_region( "us-west-2", aws_access_key_id="access-key", aws_secret_access_key="secret-key", ) ec2_instance_list = ec2.get_all_instances() return ec2_instance_list
def ec2_get_all_instances(): ec2 = boto.ec2.connect_to_region( "us-west-2", aws_access_key_id="ACCESS-KEY", aws_secret_access_key="SECRET-KEY", ) ec2_instance_list = ec2.get_all_instances() return ec2_instance_list
def get_instance_by_ip(ip): ec2 = boto.ec2.connect_to_region(env.ec2_region) for reservation in ec2.get_all_instances(): for instance in reservation.instances: if instance.state != 'terminated' and \ (instance.ip_address == ip or \ instance.private_ip_address == ip): return instance return None
def test_ec2_client_ot(self): """OpenTracing compatibility check of the test_ec2_client test.""" ec2 = boto.ec2.connect_to_region("us-west-2") tracer = get_dummy_tracer() ot_tracer = init_tracer('my_svc', tracer) writer = tracer.writer Pin(service=self.TEST_SERVICE, tracer=tracer).onto(ec2) with ot_tracer.start_active_span('ot_span'): ec2.get_all_instances() spans = writer.pop() assert spans eq_(len(spans), 2) ot_span, dd_span = spans # confirm the parenting eq_(ot_span.parent_id, None) eq_(dd_span.parent_id, ot_span.span_id) eq_(ot_span.resource, "ot_span") eq_(dd_span.get_tag('aws.operation'), "DescribeInstances") eq_(dd_span.get_tag(http.STATUS_CODE), "200") eq_(dd_span.get_tag(http.METHOD), "POST") eq_(dd_span.get_tag('aws.region'), "us-west-2") with ot_tracer.start_active_span('ot_span'): ec2.run_instances(21) spans = writer.pop() assert spans eq_(len(spans), 2) ot_span, dd_span = spans # confirm the parenting eq_(ot_span.parent_id, None) eq_(dd_span.parent_id, ot_span.span_id) eq_(dd_span.get_tag('aws.operation'), "RunInstances") eq_(dd_span.get_tag(http.STATUS_CODE), "200") eq_(dd_span.get_tag(http.METHOD), "POST") eq_(dd_span.get_tag('aws.region'), "us-west-2") eq_(dd_span.service, "test-boto-tracing.ec2") eq_(dd_span.resource, "ec2.runinstances") eq_(dd_span.name, "ec2.command")
def test_ec2_client_ot(self): """OpenTracing compatibility check of the test_ec2_client test.""" ec2 = boto.ec2.connect_to_region('us-west-2') ot_tracer = init_tracer('my_svc', self.tracer) writer = self.tracer.writer Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) with ot_tracer.start_active_span('ot_span'): ec2.get_all_instances() spans = writer.pop() assert spans self.assertEqual(len(spans), 2) ot_span, dd_span = spans # confirm the parenting self.assertIsNone(ot_span.parent_id) self.assertEqual(dd_span.parent_id, ot_span.span_id) self.assertEqual(ot_span.resource, 'ot_span') self.assertEqual(dd_span.get_tag('aws.operation'), 'DescribeInstances') self.assertEqual(dd_span.get_tag(http.STATUS_CODE), '200') self.assertEqual(dd_span.get_tag(http.METHOD), 'POST') self.assertEqual(dd_span.get_tag('aws.region'), 'us-west-2') with ot_tracer.start_active_span('ot_span'): ec2.run_instances(21) spans = writer.pop() assert spans self.assertEqual(len(spans), 2) ot_span, dd_span = spans # confirm the parenting self.assertIsNone(ot_span.parent_id) self.assertEqual(dd_span.parent_id, ot_span.span_id) self.assertEqual(dd_span.get_tag('aws.operation'), 'RunInstances') self.assertEqual(dd_span.get_tag(http.STATUS_CODE), '200') self.assertEqual(dd_span.get_tag(http.METHOD), 'POST') self.assertEqual(dd_span.get_tag('aws.region'), 'us-west-2') self.assertEqual(dd_span.service, 'test-boto-tracing.ec2') self.assertEqual(dd_span.resource, 'ec2.runinstances') self.assertEqual(dd_span.name, 'ec2.command')
def get_console_log(instance_id): """Print console log of a given EC2 instance""" config = Config() ec2 = boto.ec2.connect_to_region(config.get("region")) reservations = ec2.get_all_instances(filters={"instance-id": instance_id}) if reservations: instance = reservations[0].instances[0] return instance.get_console_output().output else: return "Instance with id %s not found" % instance_id
def instanceName(self): try: instance = ec2.get_all_instances(instance_ids=self.instanceID()) for res in instance: for i in res.instances: if 'Name' in i.tags: name = i.tags['Name'] except: logger.error('Failed to retrieve instance Name for %s' % (self.instanceID())) return name
def attachedvolumes(): print 'Attached Volume ID - Instance ID','-','Device Name' for volumes in vol: if volumes.attachment_state() == 'attached': filter = {'block-device-mapping.volume-id':volumes.id} volumesinstance = ec2.get_all_instances(filters=filter) ids = [z for k in volumesinstance for z in k.instances] for s in ids: print volumes.id,'-',s.id,'-',volumes.attach_data.device
def get_status(): ec2 = conn_region reservations = ec2.get_all_instances( filters={'instance-state-name': 'running'}) instances = [i for r in reservations for i in r.instances] for instance in instances: if instance.__dict__['tags'].__dict__['_current_key'] == tag_name: print instance
def ec2_terminate(ec2): #filters = {'instance-state-name' : 'running'} reservations = ec2.get_all_instances() instances = [i for r in reservations for i in r.instances] for i in instances: try: print "---Terminating instance: " + i.id ec2.terminate_instances(instance_ids=i.id) except Exception, e: print(e)
def get_instances(): conn = get_autoscale_connection() ec2 = boto.ec2.connect_to_region(REGION) conn.get_all_groups(names=[group_name])[0] instance_ids = [i.instance_id for i in group.instances] reservations = ec2.get_all_instances(instance_ids) instances = [i for r in reservations for i in r.instances] return instances
def get_hosts(ec2, fk, fv): hosts = [] print fv print fk f = {fk: fv} print f ec2.get_all_instances() #reservations = ec2.get_all_instances(filters={'tag:Name': 'awsecsec'}) reservations = ec2.get_all_instances(filters=f) for res in reservations: for inst in res.instances: if inst.ip_address: if inst.state == 'running': #print inst.private_ip_address print 'awsecsec :', inst.ip_address #hosts.append(inst.ip_address) hosts.append(inst.ip_address) return hosts
def _find_ec2_by_name(name, state='running'): ec2 = _get_ec2_connection() reservations = ec2.get_all_instances( filters={'tag:Name': name, 'instance-state-name': state}) if len(reservations) == 0: return None elif len(reservations) == 1: return reservations[0].instances[0] puts("Found more than one!") return None
def terminate_all_instances(self): for region, ami in self.available_amis_per_regions.iteritems(): print "terminating in {0}: ".format(region), ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=self.key, aws_secret_access_key=self.secret) reservations = ec2.get_all_instances() for r in reservations: for instance in r.instances: if instance.key_name == EC2_KEY_NAME: print "terminating: ", instance.key_name, instance.ip_address, "state now:", instance.state instance.terminate()
def get_latest_ip(instance_id): latest_ip = None reservations = ec2.get_all_instances() instances = [i for r in reservations for i in r.instances] for i in instances: attr = i.__dict__ if attr.get("id") == instance_id: latest_ip = attr.get('ip_address') break return latest_ip
def get_instances(region='us-east-1', filters={'instance-state-name': 'running'}, instance_ids=None): ec2 = boto.connect_ec2(region=boto.ec2.get_region(region)) reservations = ec2.get_all_instances(filters=filters, instance_ids=instance_ids) return [i for r in reservations for i in r.instances]
def generate_profile_config(region_name, profile_name): ec2 = boto.ec2.connect_to_region(region_name=region_name, profile_name=profile_name) # Get list of reservations. reservationList = ec2.get_all_instances() # Initialize instance data tuple instanceData = [] try: # Iterate over reservations for reservation in reservationList: # Iterate over instances for instance in reservation.instances: user = instance.tags.get('User', defaultUser) name = instance.tags.get('Name', instance.id) if instance.ip_address: instanceData.append((name.replace(' ', '_'), instance.ip_address, instance.key_name, user)) if 'MC Containers' in instance.tags: containers = instance.tags['MC Containers'].split() for container in containers: instanceData.append((container.replace(' ', '_'), instance.ip_address, instance.key_name, user)) # Generate .ssh/config output config_file_name = defaultKeyPath + '/' + profile_name + '_config' with open(config_file_name, 'w') as f: print("Generating " + config_file_name) f.write("#============ GENERATED DATA START ==================\n") f.write("UserKnownHostsFile=/dev/null\n") f.write("StrictHostKeyChecking=no\n\n") for data in instanceData: f.write("Host {0}\n".format(data[0])) f.write(" HostName {host_name}\n".format(host_name=data[1])) f.write(" User {user}\n".format(user=data[3])) if (data[2] != None): f.write(" IdentityFile {identity_file}\n".format( identity_file=os.path. join(defaultKeyPath, "{key_name}".format( key_name=data[2])))) f.write(" ControlPath ~/.ssh/ec2-{0}:%p.%r\n".format( data[0])) f.write("\n") f.write("#============ GENERATED DATA END ==================\n") f.write("# vim: ft=sshconfig\n") except Exception as inst: print(dir(inst)) print("Error..." + inst.message)
def get_instances(self, instance_ids=None, filters=None): """Get dictionary of instance info indexed by iid""" inst_by_id = {} ec2 = self.get_ec2() for reservation in ec2.get_all_instances( instance_ids=instance_ids, filters=filters, ): for instance in reservation.instances: inst_by_id[instance.id] = instance return inst_by_id
def get_tag(): global ec2, INSTANCE_ID try: reservations = ec2.get_all_instances(instance_ids=[INSTANCE_ID]) instance = reservations[0].instances[0] except boto.exception.EC2ResponseError: print_boto_error() if instance.tags.get('aws:autoscaling:groupName', False): return instance.tags['aws:autoscaling:groupName'].encode('ascii') return False
def get_status(): ec2 = conn_region reservations = ec2.get_all_instances( filters={'instance-state-name': 'running'}) instances = [i for r in reservations for i in r.instances] for instance in instances: if instance.__dict__['tags'].__dict__['_current_key'] == tag_name: instanceip = instance.__dict__['ip_address'] print("%s \tIP:%s" % (instance, instanceip))
def main(): for region in boto.ec2.regions(): ec2 = region.connect() if ec2 is not None: try: reservations = ec2.get_all_instances() except boto.exception.EC2ResponseError: reservations = [] for reservation in reservations: for i in reservation.instances: print(region.name, get_name(i), ",".join([r.name for r in i.groups]))
def listservers(): os.system('clear') print "Listing Servers:" print "==============================" reservations = ec2.get_all_instances() for res in reservations: for inst in res.instances: if 'Name' in inst.tags: print "%s (%s) [%s] %s" % (inst.tags['Name'], inst.private_ip_address, inst.state, inst.id) else: print "No instances in this region"
def test_ec2_client_ot(self): """OpenTracing compatibility check of the test_ec2_client test.""" ec2 = boto.ec2.connect_to_region("us-west-2") ot_tracer = init_tracer("my_svc", self.tracer) Pin(service=self.TEST_SERVICE, tracer=self.tracer).onto(ec2) with ot_tracer.start_active_span("ot_span"): ec2.get_all_instances() spans = self.pop_spans() assert spans self.assertEqual(len(spans), 2) ot_span, dd_span = spans # confirm the parenting self.assertIsNone(ot_span.parent_id) self.assertEqual(dd_span.parent_id, ot_span.span_id) self.assertEqual(ot_span.resource, "ot_span") self.assertEqual(dd_span.get_tag("aws.operation"), "DescribeInstances") assert_span_http_status_code(dd_span, 200) self.assertEqual(dd_span.get_tag(http.METHOD), "POST") self.assertEqual(dd_span.get_tag("aws.region"), "us-west-2") with ot_tracer.start_active_span("ot_span"): ec2.run_instances(21) spans = self.pop_spans() assert spans self.assertEqual(len(spans), 2) ot_span, dd_span = spans # confirm the parenting self.assertIsNone(ot_span.parent_id) self.assertEqual(dd_span.parent_id, ot_span.span_id) self.assertEqual(dd_span.get_tag("aws.operation"), "RunInstances") assert_span_http_status_code(dd_span, 200) self.assertEqual(dd_span.get_tag(http.METHOD), "POST") self.assertEqual(dd_span.get_tag("aws.region"), "us-west-2") self.assertEqual(dd_span.service, "test-boto-tracing.ec2") self.assertEqual(dd_span.resource, "ec2.runinstances") self.assertEqual(dd_span.name, "ec2.command")