Exemplo n.º 1
0
    def run_instance(self):
        if not self._image:
            raise InstanceError(
                'Cannot run EC2 instance: image (AMI) is not defined')

        # Remove active tag.
        active_filter = {
            'tag-key': self.INSTANCE_STATE_TAG,
            'tag-value': 'True'
        }
        active_reserv = self._conn.get_all_instances(filters=active_filter)
        active_instances = [i for r in active_reserv for i in r.instances]

        if len(active_instances) > 0:
            #active_insts = active_reserv.instances
            if len(active_instances) > 1:
                Logger.error(
                    'EC2 instances consistency error - more than one active EC2 instance'
                )
            for inst in active_instances:
                inst.remove_tag(self.INSTANCE_STATE_TAG, 'True')
                inst.add_tag(self.INSTANCE_STATE_TAG, 'False')
                if inst.state != self.TERMINATED_STATE:
                    Logger.info('{} instance {} is not longer active'.format(
                        inst.instance_type, inst.id))

        self._run_instance()
        pass
Exemplo n.º 2
0
    def create_spot_instance(self):
        # Create spot instance
        req = self._conn.request_spot_instances(
            price=self._args.spot_price,
            image_id=self._args.image,
            key_name=self.get_key_name(),
            instance_type=self._args.instance_type,
            security_groups=self.get_security_groups(),
            monitoring_enabled=self.toBool(self._args.monitoring),
            #subnet_id = self._args.subnet_id,
            placement=self._args.zone,
            ebs_optimized=self.toBool(self._args.ebs_optimized))
        job_instance_id = self._wait_for_an_instance(req)

        if not job_instance_id:
            self._conn.cancel_spot_instance_requests(req[0].id)
            raise InstanceError(
                u'Unable to obtain {} spot instance in region {} for price ${}: {}'
                .format((self._args.instance_type, self._args.region,
                         self._args.spot_price, 'the request was canceled')))

        Logger.info(u'{} spot instance was created: {}'.format(
            self._type, job_instance_id))
        reservations = self._conn.get_all_instances(
            instance_ids=job_instance_id)
        instance = reservations[0].instances[0]
        return instance
Exemplo n.º 3
0
    def find_volume(self):
        (volumes, rest_volumes) = self.all_volumes()

        for volume in volumes:
            if volume.tags[self.VOLUME_TAG] == self._storage:
                return volume
        msg = u'Cannot find storage volume {}. Verify that the volume was created.'.format(
            self._storage)
        raise InstanceError(msg)
Exemplo n.º 4
0
    def get_key_name(self):
        if not self._keypair_name:
            raise InstanceError(
                'AWS keypair cannot be created: KeyName is not specified in AWS section in the config file'
            )
        if not self._keypair_dir:
            raise InstanceError(
                'AWS keypair cannot be created: KeyDir is not specified in AWS section in the config file'
            )

        # Check if the key exists and create one if does not.
        try:
            key = self._conn.get_all_key_pairs(
                keynames=[self._keypair_name])[0]
        except self._conn.ResponseError as e:
            if e.code == 'InvalidKeyPair.NotFound':
                Logger.info(
                    'AWS key {} does not exist: creating the key'.format(
                        self._keypair_name))
                # Create an SSH key to use when logging into instances.
                key = self._conn.create_key_pair(self._keypair_name)
                Logger.info('AWS key was created: {}'.format(
                    self._keypair_name))

                # Expand key dir.
                key_dir = os.path.expandvars(self._keypair_dir)
                if not os.path.isdir(self._keypair_dir):
                    os.mkdir(self._keypair_dir, 0o700)

                #  Private key has to be stored locally.
                key_file = os.path.join(key_dir, self._keypair_name + '.pem')
                #key.save(key_file) # doesn't work in python3
                fp = open(key_file, 'w')
                fp.write(key.material)
                fp.close()
                os.chmod(key_file, 0o600)
                Logger.info(
                    'AWS private key file was saved: {}'.format(key_file))
            else:
                raise

        return self._keypair_name
Exemplo n.º 5
0
    def get_key_name(self):
        if not self._keypair_name:
            raise InstanceError(
                'AWS keypair cannot be created: KeyName is not specified in AWS section in the config file'
            )
        if not self._keypair_dir:
            raise InstanceError(
                'AWS keypair cannot be created: KeyDir is not specified in AWS section in the config file'
            )

        # Check if the key exists and create one if does not.
        try:
            key = self._conn.get_all_key_pairs(
                keynames=[self._keypair_name])[0]
        except self._conn.ResponseError as e:
            if e.code != 'InvalidKeyPair.NotFound':
                raise
            self.create_new_keypair()

        return self._keypair_name
Exemplo n.º 6
0
    def run_instance(self):
        if not self._image:
            raise InstanceError(
                'Cannot run EC2 instance: image (AMI) is not defined')

        instance = self.create_instance()

        # Remove active tag.
        active_filter = {
            'tag-key': self.INSTANCE_STATE_TAG,
            'tag-value': 'True'
        }
        active_reserv = self._conn.get_all_instances(filters=active_filter)
        active_instances = [i for r in active_reserv for i in r.instances]

        if len(active_instances) > 0:
            #active_insts = active_reserv.instances
            if len(active_instances) > 1:
                Logger.error(
                    'EC2 instances consistency error - more than one active EC2 instance'
                )
            for inst in active_instances:
                inst.remove_tag(self.INSTANCE_STATE_TAG, 'True')
                inst.add_tag(self.INSTANCE_STATE_TAG, 'False')
                if inst.state != self.TERMINATED_STATE:
                    Logger.log('{} instance {} is not longer active'.format(
                        inst.instance_type, inst.id))

        # Assign the created instace as active.
        instance.add_tag(self.INSTANCE_STATE_TAG, 'True')
        Logger.info('New {} instance {} was selected as active'.format(
            instance.instance_type, instance.id))

        Logger.info('Waiting for a running status')
        while instance.state != 'running':
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(1)
            instance.update()
        sys.stdout.write("\n")

        self._conn.attach_volume(self.find_volume().id, instance.id,
                                 "/dev/sdx")
        pass
Exemplo n.º 7
0
    def create_spot_instance(self):
        # Create spot instance
        req = self._conn.request_spot_instances(
            price=self._args.spot_price,
            image_id=self._args.image,
            key_name=self.get_key_name(),
            instance_type=self._args.instance_type,
            security_groups=self.get_security_groups(),
            monitoring_enabled=self.toBool(self._args.monitoring),
            #subnet_id = self._args.subnet_id,
            placement=self._args.zone,
            ebs_optimized=self.toBool(self._args.ebs_optimized))
        job_instance_id = None
        sec = 0
        Logger.info(u'Waiting for a spot instance. Request {}.'.format(
            req[0].id))
        while job_instance_id == None and sec < self._args.spot_timeout:
            sys.stdout.write(".")
            sys.stdout.flush()

            job_sir_id = req[0].id
            reqs = self._conn.get_all_spot_instance_requests()
            for sir in reqs:
                if sir.id == job_sir_id:
                    job_instance_id = sir.instance_id
                    break
            time.sleep(1)
            sec += 1
        sys.stdout.write("\n")

        if not job_instance_id:
            self._conn.cancel_spot_instance_requests(req[0].id)
            raise InstanceError(
                u'Unable to obtain {} spot instance in region {} for price ${}: {}'
                .format((self._args.instance_type, self._args.region,
                         self._args.spot_price, 'the request was canceled')))

        Logger.info(u'{} spot instance was created: {}'.format(
            self._type, job_instance_id))
        reservations = self._conn.get_all_instances(
            instance_ids=job_instance_id)
        instance = reservations[0].instances[0]
        return instance