我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用boto.connect_ec2()。
def do_start(self): ami_id = self.sd.get('ami_id') instance_type = self.sd.get('instance_type', 'm1.small') security_group = self.sd.get('security_group', 'default') if not ami_id: self.parser.error('ami_id option is required when starting the service') ec2 = boto.connect_ec2() if not self.sd.has_section('Credentials'): self.sd.add_section('Credentials') self.sd.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id) self.sd.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key) s = StringIO.StringIO() self.sd.write(s) rs = ec2.get_all_images([ami_id]) img = rs[0] r = img.run(user_data=s.getvalue(), key_name=self.options.keypair, max_count=self.options.num_instances, instance_type=instance_type, security_groups=[security_group]) print 'Starting AMI: %s' % ami_id print 'Reservation %s contains the following instances:' % r.id for i in r.instances: print '\t%s' % i.id
def ec2(self): if self._ec2 is None: self._ec2 = boto.connect_ec2() return self._ec2
def __init__(self, profile, queue, hook, dry_run, bin_directory=None): logging.basicConfig(level=logging.INFO) self.queue = queue self.hook = hook self.profile = profile if bin_directory: os.environ["PATH"] = bin_directory + os.pathsep + os.environ["PATH"] self.aws_bin = spawn.find_executable('aws') self.python_bin = spawn.find_executable('python') self.base_cli_command ="{python_bin} {aws_bin} --profile {profile} ".format( python_bin=self.python_bin, aws_bin=self.aws_bin, profile=self.profile) self.dry_run = dry_run self.ec2_con = boto.connect_ec2() self.sqs_con = boto.connect_sqs()
def services_for_instance(instance_id): """ Get the list of all services named by the services tag in this instance's tags. """ ec2 = boto.connect_ec2() reservations = ec2.get_all_instances(instance_ids=[instance_id]) for reservation in reservations: for instance in reservation.instances: if instance.id == instance_id: try: services = instance.tags['services'].split(',') except KeyError as ke: msg = "Tag named 'services' not found on this instance({})".format(instance_id) raise Exception(msg) for service in services: yield service
def start(self): self.stop() ec2 = boto.connect_ec2() ami = ec2.get_all_images(image_ids = [str(self.ami_id)])[0] groups = ec2.get_all_security_groups(groupnames=[str(self.security_group)]) if not self._config: self.load_config() if not self._config.has_section("Credentials"): self._config.add_section("Credentials") self._config.set("Credentials", "aws_access_key_id", ec2.aws_access_key_id) self._config.set("Credentials", "aws_secret_access_key", ec2.aws_secret_access_key) if not self._config.has_section("Pyami"): self._config.add_section("Pyami") if self._manager.domain: self._config.set('Pyami', 'server_sdb_domain', self._manager.domain.name) self._config.set("Pyami", 'server_sdb_name', self.name) cfg = StringIO() self._config.write(cfg) cfg = cfg.getvalue() r = ami.run(min_count=1, max_count=1, key_name=self.key_name, security_groups = groups, instance_type = self.instance_type, placement = self.zone, user_data = cfg) i = r.instances[0] self.instance_id = i.id self.put() if self.elastic_ip: ec2.associate_address(self.instance_id, self.elastic_ip)
def main(self): fp = StringIO.StringIO() boto.config.dump_safe(fp) self.notify('%s (%s) Starting' % (self.name, self.instance_id), fp.getvalue()) if self.src and self.dst: self.copy_keys() if self.dst: self.copy_log() self.notify('%s (%s) Stopping' % (self.name, self.instance_id), 'Copy Operation Complete') if boto.config.getbool(self.name, 'exit_on_completion', True): ec2 = boto.connect_ec2() ec2.terminate_instances([self.instance_id])
def attach(self): ec2 = boto.connect_ec2() if self.logical_volume_name: # if a logical volume was specified, override the specified volume_id # (if there was one) with the current AWS volume for the logical volume: logical_volume = next(Volume.find(name=self.logical_volume_name)) self.volume_id = logical_volume._volume_id volume = ec2.get_all_volumes([self.volume_id])[0] # wait for the volume to be available. The volume may still be being created # from a snapshot. while volume.update() != 'available': boto.log.info('Volume %s not yet available. Current status = %s.' % (volume.id, volume.status)) time.sleep(5) instance = ec2.get_only_instances([self.instance_id])[0] attempt_attach = True while attempt_attach: try: ec2.attach_volume(self.volume_id, self.instance_id, self.device) attempt_attach = False except EC2ResponseError as e: if e.error_code != 'IncorrectState': # if there's an EC2ResonseError with the code set to IncorrectState, delay a bit for ec2 # to realize the instance is running, then try again. Otherwise, raise the error: boto.log.info('Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.' % (self.volume_id, self.instance_id, e.errors)) time.sleep(2) else: raise e boto.log.info('Attached volume %s to instance %s as device %s' % (self.volume_id, self.instance_id, self.device)) # now wait for the volume device to appear while not os.path.exists(self.device): boto.log.info('%s still does not exist, waiting 2 seconds' % self.device) time.sleep(2)
def shutdown(self): on_completion = self.sd.get('on_completion', 'shutdown') if on_completion == 'shutdown': if self.instance_id: time.sleep(60) c = boto.connect_ec2() c.terminate_instances([self.instance_id])
def start(self): self.stop() ec2 = boto.connect_ec2() ami = ec2.get_all_images(image_ids = [str(self.ami_id)])[0] groups = ec2.get_all_security_groups(groupnames=[str(self.security_group)]) if not self._config: self.load_config() if not self._config.has_section("Credentials"): self._config.add_section("Credentials") self._config.set("Credentials", "aws_access_key_id", ec2.aws_access_key_id) self._config.set("Credentials", "aws_secret_access_key", ec2.aws_secret_access_key) if not self._config.has_section("Pyami"): self._config.add_section("Pyami") if self._manager.domain: self._config.set('Pyami', 'server_sdb_domain', self._manager.domain.name) self._config.set("Pyami", 'server_sdb_name', self.name) cfg = StringIO.StringIO() self._config.write(cfg) cfg = cfg.getvalue() r = ami.run(min_count=1, max_count=1, key_name=self.key_name, security_groups = groups, instance_type = self.instance_type, placement = self.zone, user_data = cfg) i = r.instances[0] self.instance_id = i.id self.put() if self.elastic_ip: ec2.associate_address(self.instance_id, self.elastic_ip)
def get_ec2_conn(conf): region = conf.get('EC2_REGION') if region: conn = boto.ec2.connect_to_region(region, **aws_creds(conf)) if not conn: raise ValueErrorRetry("Could not establish EC2 connection to region %r" % (region,)) else: conn = boto.connect_ec2(**aws_creds(conf)) return conn
def aws_connect(): if env.config['verbose']: logging.info("[aws_connect]") target_region = None if 'region' in env.config: target_region = boto.ec2.get_region(env.config['region']) conn = boto.connect_ec2(env.config['aws_key'], env.config['aws_secret'], region=target_region) return conn
def establish(): my_region = RegionInfo(name=config.region, endpoint=config.endpoint) conn = boto.connect_ec2( aws_access_key_id=config.aws_access_key_id, aws_secret_access_key=config.aws_secret_access_key, is_secure=True, region=my_region, port=config.port, path=config.endpoint_path, validate_certs=False) return conn
def run(self, **kwargs): ec2 = boto.connect_ec2(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET) sqs = boto.connect_sqs(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET) queue = sqs.create_queue(REQUEST_QUEUE) num = queue.count() launched = 0 icount = 0 reservations = ec2.get_all_instances() for reservation in reservations: for instance in reservation.instances: if instance.state == "running" and instance.image_id == AMI_ID: icount += 1 to_boot = min(num - icount, MAX_INSTANCES) if to_boot > 0: startup = BOOTSTRAP_SCRIPT % { 'KEY': settings.PDF_AWS_KEY, 'SECRET': settings.PDF_AWS_SECRET, 'RESPONSE_QUEUE': RESPONSE_QUEUE, 'REQUEST_QUEUE': REQUEST_QUEUE} r = ec2.run_instances( image_id=AMI_ID, min_count=to_boot, max_count=to_boot, key_name=KEYPAIR, security_groups=SECURITY_GROUPS, user_data=startup) launched = len(r.instances) return launched
def connect_method(self, *args, **kwargs): return boto.connect_ec2(*args, **kwargs)
def edp_for_instance(instance_id): ec2 = boto.connect_ec2() reservations = ec2.get_all_instances(instance_ids=[instance_id]) for reservation in reservations: for instance in reservation.instances: if instance.id == instance_id: try: environment = instance.tags['environment'] deployment = instance.tags['deployment'] play = instance.tags['play'] except KeyError as ke: msg = "{} tag not found on this instance({})".format(ke.message, instance_id) raise Exception(msg) return (environment, deployment, play)
def get_instance_dict(self): ec2 = boto.connect_ec2(profile_name=self.profile) reservations = ec2.get_all_instances() dict = {} for instance in [i for r in reservations for i in r.instances]: dict[instance.id] = instance return dict