Ejemplo n.º 1
0
 def shutdown(self):
     on_completion = self.sd.get('on_completion', 'shutdown')
     if on_completion == 'shutdown':
         if self.instance_id:
             time.sleep(60)
             c = fcu_boto.connect_ec2()
             c.terminate_instances([self.instance_id])
Ejemplo n.º 2
0
 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 = fcu_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()
     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)
Ejemplo n.º 3
0
 def main(self):
     fp = StringIO.StringIO()
     fcu_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 fcu_boto.config.getbool(self.name, 'exit_on_completion', True):
         ec2 = fcu_boto.connect_ec2()
         ec2.terminate_instances([self.instance_id])
Ejemplo n.º 4
0
 def attach(self):
     ec2 = fcu_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':
         fcu_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:
                 fcu_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
     fcu_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):
         fcu_boto.log.info('%s still does not exist, waiting 2 seconds' %
                           self.device)
         time.sleep(2)
Ejemplo n.º 5
0
    def start(self):
        self.stop()
        ec2 = fcu_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)
Ejemplo n.º 6
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'a:b:c:g:hi:k:m:n:o:rs:w', [
            'ami', 'bucket', 'class', 'group', 'help', 'inputqueue', 'keypair',
            'module', 'numinstances', 'outputqueue', 'reload', 'script_name',
            'wait'
        ])
    except:
        usage()
    params = {
        'module_name': None,
        'script_name': None,
        'class_name': None,
        'script_bucket': None,
        'group': 'default',
        'keypair': None,
        'ami': None,
        'num_instances': 1,
        'input_queue_name': None,
        'output_queue_name': None
    }
    reload = None
    wait = None
    for o, a in opts:
        if o in ('-a', '--ami'):
            params['ami'] = a
        if o in ('-b', '--bucket'):
            params['script_bucket'] = a
        if o in ('-c', '--class'):
            params['class_name'] = a
        if o in ('-g', '--group'):
            params['group'] = a
        if o in ('-h', '--help'):
            usage()
        if o in ('-i', '--inputqueue'):
            params['input_queue_name'] = a
        if o in ('-k', '--keypair'):
            params['keypair'] = a
        if o in ('-m', '--module'):
            params['module_name'] = a
        if o in ('-n', '--num_instances'):
            params['num_instances'] = int(a)
        if o in ('-o', '--outputqueue'):
            params['output_queue_name'] = a
        if o in ('-r', '--reload'):
            reload = True
        if o in ('-s', '--script'):
            params['script_name'] = a
        if o in ('-w', '--wait'):
            wait = True

    # check required fields
    required = ['ami']
    for pname in required:
        if not params.get(pname, None):
            print('%s is required' % pname)
            usage()
    if params['script_name']:
        # first copy the desired module file to S3 bucket
        if reload:
            print('Reloading module %s to S3' % params['script_name'])
        else:
            print('Copying module %s to S3' % params['script_name'])
        l = imp.find_module(params['script_name'])
        c = fcu_boto.connect_s3()
        bucket = c.get_bucket(params['script_bucket'])
        key = bucket.new_key(params['script_name'] + '.py')
        key.set_contents_from_file(l[0])
        params['script_md5'] = key.md5
    # we have everything we need, now build userdata string
    l = []
    for k, v in params.items():
        if v:
            l.append('%s=%s' % (k, v))
    c = fcu_boto.connect_ec2()
    l.append('aws_access_key_id=%s' % c.aws_access_key_id)
    l.append('aws_secret_access_key=%s' % c.aws_secret_access_key)
    for kv in args:
        l.append(kv)
    s = '|'.join(l)
    if not reload:
        rs = c.get_all_images([params['ami']])
        img = rs[0]
        r = img.run(user_data=s,
                    key_name=params['keypair'],
                    security_groups=[params['group']],
                    max_count=params.get('num_instances', 1))
        print('AMI: %s - %s (Started)' % (params['ami'], img.location))
        print('Reservation %s contains the following instances:' % r.id)
        for i in r.instances:
            print('\t%s' % i.id)
        if wait:
            running = False
            while not running:
                time.sleep(30)
                [i.update() for i in r.instances]
                status = [i.state for i in r.instances]
                print(status)
                if status.count('running') == len(r.instances):
                    running = True
            for i in r.instances:
                print('Instance: %s' % i.ami_launch_index)
                print('Public DNS Name: %s' % i.public_dns_name)
                print('Private DNS Name: %s' % i.private_dns_name)
Ejemplo n.º 7
0
 def ec2(self):
     if self._ec2 is None:
         self._ec2 = fcu_boto.connect_ec2()
     return self._ec2