Пример #1
0
    def database_insert_server(self, instance_id, status=None,
                               launched_at=None, life_time=None,
                               image_key=None, token=None):
        logging.debug('Insert instance %s', instance_id)

        query = self.database.query(Instance).filter(
            Instance.provider_id == instance_id
        )

        if query.count() > 0:
            data_instance = query.first()
        else:
            data_instance = Instance()
            data_instance.launched_at = datetime.datetime.now()

        data_instance.provider_id = instance_id
        data_instance.status = status

        if image_key:
            data_instance.image_key = image_key

        # Overwrite launched
        if launched_at is not None:
            data_instance.launched_at = launched_at

        if life_time is not None:
            data_instance.life_time = life_time

        if token is not None:
            data_instance.token = token

        self.database.merge(data_instance)
        self.database.commit()
        return data_instance
Пример #2
0
    def create_instance(self, image_key, time, token):
        logging.info("Create Instance")
        image = self.config.images[image_key]
        active_instance = self.database_count_active_instance(image_key)

        if self.check_user_own_instance_type(token, image_key):
            raise DemoExceptionUserAlreadyHaveInstanceImage()
        elif 'max_instance' in image\
                and active_instance >= image['max_instance']:
            raise DemoExceptionToMuchInstanceImage()
        else:
            # Add a demand
            database_instance = Instance()
            database_instance.status = 'ASK'
            database_instance.launched_at = datetime.datetime.now()
            database_instance.life_time = 10
            database_instance.image_key = image_key
            database_instance.token = '0'
            self.database.add(database_instance)
            self.database.commit()

            pool = self.get_pooled_instance_database(image_key)
            if len(pool) > 0:
                new_instance_id = pool.pop(0)['id']
            else:
                new_instance_id = self.provider.create_instance(image)

            # If more than max go to max
            life_time = image['time_default']
            if time is not None and 'time_max' in image and\
                    time <= image['time_max']:
                life_time = time

            self.database_insert_server(
                new_instance_id, status='CREATED',
                life_time=life_time,
                image_key=image_key, token=token,
                launched_at=datetime.datetime.now()
            )

            # Remove the demand
            self.database.delete(database_instance)
            self.database.commit()

            return new_instance_id