Esempio n. 1
0
def create_job(scheduler, job_id, job_name, func, func_kwargs, trigger_args):
    """Create a new job/model.
    """
    _LOGGER.debug(
        'job_id: %s, job_name: %s, func: %s, func_kwargs: %r, trigger_args: '
        '%r', job_id, job_name, func, func_kwargs, trigger_args)

    job = get_job(scheduler, job_id)
    if job:
        raise exc.FoundError('{} already exists'.format(job_id))

    _LOGGER.info('Adding job %s', job_id)
    try:
        job = scheduler.add_job(func,
                                trigger='cron',
                                id=job_id,
                                name=job_name,
                                misfire_grace_time=ONE_DAY_IN_SECS,
                                kwargs=func_kwargs,
                                **trigger_args)
    except ValueError as err:
        raise exc.InvalidInputError(
            __name__,
            str(err),
        )

    return job
Esempio n. 2
0
def create_user(iam_conn, user_name):
    """Create IAM user.
    """
    try:
        response = iam_conn.create_user(Path='/', UserName=user_name)
        return response['User']
    except iam_conn.exceptions.EntityAlreadyExistsException as err:
        raise exc.FoundError(str(err))
Esempio n. 3
0
def create_role(iam_conn, role_name, policy_document):
    """Create IAM role.
    """
    try:
        response = iam_conn.create_role(
            Path='/',
            RoleName=role_name,
            AssumeRolePolicyDocument=policy_document)
        return response['Role']
    except iam_conn.exceptions.EntityAlreadyExistsException as err:
        raise exc.FoundError(str(err))
Esempio n. 4
0
def _explain(inst_id):
    """Explain application placement"""
    with lc.LogContext(_LOGGER, inst_id):
        start = time.time()
        ro_scheduler = get_readonly_scheduler()
        _LOGGER.info('ro_scheduler was ready in %s secs', time.time() - start)

        try:
            instance = ro_scheduler.cell.apps[inst_id]
        except KeyError:
            raise exc.NotFoundError(inst_id)

        if instance.server:
            raise exc.FoundError('instance {} is already placed on {}'.format(
                inst_id, instance.server))

        return reports.explain_placement(ro_scheduler.cell, instance,
                                         'servers')
Esempio n. 5
0
def create_role(iam_conn,
                role_name,
                path,
                policy_document,
                max_session_duration=3600):
    """Create IAM role.
    """

    if not path:
        path = '/'

    try:
        response = iam_conn.create_role(
            Path=path,
            MaxSessionDuration=max_session_duration,
            RoleName=role_name,
            AssumeRolePolicyDocument=policy_document)
        return response['Role']
    except iam_conn.exceptions.EntityAlreadyExistsException as err:
        raise exc.FoundError(str(err))
Esempio n. 6
0
        def create(rsrc_id, rsrc):
            """Create (configure) AWS image."""
            image = rsrc_id
            _LOGGER.info('Create image: %s', image)

            ec2_conn = awscontext.GLOBAL.ec2
            sts_conn = awscontext.GLOBAL.sts

            # Check if image exists. Create is async, so this is optimization
            # to return Found early.
            found = False
            try:
                get(image)
                found = True
            except Exception as _err:  # pylint: disable=broad-except
                # TODO: catch appropriate exception.
                pass

            if found:
                raise exc.FoundError('Image {} already exists.'.format(image))

            base_image_account = rsrc.get('base_image_account')
            _LOGGER.info('base account: %s', base_image_account)

            base_image = rsrc.get('base_image')
            base_image_id = aws_cli_admin.image_id(ec2_conn,
                                                   sts_conn,
                                                   base_image,
                                                   account=base_image_account)
            _LOGGER.info('base image id: %s', base_image_id)

            secgroup = rsrc.get('secgroup')
            if secgroup:
                secgroup_id = aws_cli_admin.secgroup_id(ec2_conn, secgroup)
            else:
                secgroup_id = self._secgroup
            _LOGGER.info('secgroup id: %s', secgroup_id)

            subnet = rsrc.get('subnet')
            if subnet:
                subnet_id = aws_cli_admin.subnet_id(ec2_conn, subnet)
            else:
                subnet_id = self._subnet
            _LOGGER.info('subnet id: %s', secgroup_id)

            key = rsrc.get('key')

            cloud_init = ud.CloudInit()
            for content in rsrc.get('userdata', []):
                cloud_init.add(content)

            cloud_init.add_cloud_config({
                'image_description': '',
                'image_name': image,
            })

            tags = [{
                'ResourceType':
                'instance',
                'Tags': [{
                    'Key': 'Name',
                    'Value': 'ImageBuild-{}'.format(image)
                }]
            }]

            instance = ec2client.create_instance(
                ec2_conn,
                user_data=cloud_init.userdata(),
                image_id=base_image_id,
                instance_type='t2.small',
                key=key,
                tags=tags,
                secgroup_ids=secgroup_id,
                subnet_id=subnet_id,
                instance_profile=instance_profile,
                disk=10)

            _LOGGER.info('Started instance: %s',
                         instance['Instances'][0]['InstanceId'])

            return {}