我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用boto.ec2.get_all_instances()。
def create_from_instance_id(cls, instance_id, name, description=''): regions = boto.ec2.regions() for region in regions: ec2 = region.connect() try: rs = ec2.get_all_instances([instance_id]) except: rs = [] if len(rs) == 1: s = cls() s.ec2 = ec2 s.name = name s.description = description s.region_name = region.name s.instance_id = instance_id s._reservation = rs[0] for instance in s._reservation.instances: if instance.id == instance_id: s._instance = instance s.put() return s return None
def create_from_current_instances(cls): servers = [] regions = boto.ec2.regions() for region in regions: ec2 = region.connect() rs = ec2.get_all_instances() for reservation in rs: for instance in reservation.instances: try: Server.find(instance_id=instance.id).next() boto.log.info('Server for %s already exists' % instance.id) except StopIteration: s = cls() s.ec2 = ec2 s.name = instance.id s.region_name = region.name s.instance_id = instance.id s._reservation = reservation s.put() servers.append(s) return servers
def _setup_ec2(self): if self.ec2 and self._instance and self._reservation: return if self.id: if self.region_name: for region in boto.ec2.regions(): if region.name == self.region_name: self.ec2 = region.connect() if self.instance_id and not self._instance: try: rs = self.ec2.get_all_instances([self.instance_id]) if len(rs) >= 1: for instance in rs[0].instances: if instance.id == self.instance_id: self._reservation = rs[0] self._instance = instance except EC2ResponseError: pass
def calculate_sg_archdomains(): result = defaultdict(set) 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: archdomain = get_archdomain(i) for r in i.groups: result[r.name].add(archdomain) return result
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 get_reservations(module, ec2, tags=None, state=None, zone=None): # TODO: filters do not work with tags that have underscores filters = dict() if tags is not None: if type(tags) is str: try: tags = literal_eval(tags) except: pass # if string, we only care that a tag of that name exists if type(tags) is str: filters.update({"tag-key": tags}) # if list, append each item to filters if type(tags) is list: for x in tags: if type(x) is dict: x = _set_none_to_blank(x) filters.update(dict(("tag:"+tn, tv) for (tn,tv) in x.iteritems())) else: filters.update({"tag-key": x}) # if dict, add the key and value to the filter if type(tags) is dict: tags = _set_none_to_blank(tags) filters.update(dict(("tag:"+tn, tv) for (tn,tv) in tags.iteritems())) if state: # http://stackoverflow.com/questions/437511/what-are-the-valid-instancestates-for-the-amazon-ec2-api filters.update({'instance-state-name': state}) if zone: filters.update({'availability-zone': zone}) results = ec2.get_all_instances(filters=filters) return results
def safe_get_all_instances(ec2): try: return ec2.get_all_instances() except : return []
def get_all_instances(ec2): try: return ec2.get_all_instances() except: return []
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]))