コード例 #1
0
ファイル: repokid_cli.py プロジェクト: opt9/repokid
def repo_all_roles(account_number,
                   dynamo_table,
                   config,
                   hooks,
                   commit=False,
                   scheduled=True):
    """
    Repo all scheduled or eligible roles in an account.  Collect any errors and display them at the end.

    Args:
        account_number (string)
        dynamo_table
        config
        commit (bool): actually make the changes
        scheduled (bool): if True only repo the scheduled roles, if False repo all the (eligible) roles

    Returns:
        None
    """
    errors = []

    role_ids_in_account = role_ids_for_account(dynamo_table, account_number)
    roles = Roles([])
    for role_id in role_ids_in_account:
        roles.append(
            Role(
                get_role_data(dynamo_table,
                              role_id,
                              fields=['Active', 'RoleName', 'RepoScheduled'])))

    roles = roles.filter(active=True)

    cur_time = int(time.time())
    if scheduled:
        roles = [
            role for role in roles
            if (role.repo_scheduled and cur_time > role.repo_scheduled)
        ]

    LOGGER.info('Repoing these {}roles from account {}:\n\t{}'.format(
        'scheduled ' if scheduled else '', account_number,
        ', '.join([role.role_name for role in roles])))

    for role in roles:
        error = repo_role(account_number,
                          role.role_name,
                          dynamo_table,
                          config,
                          hooks,
                          commit=commit)
        if error:
            errors.append(error)

    if errors:
        LOGGER.error('Error(s) during repo: \n{}'.format(errors))
    else:
        LOGGER.info('Everything successful!')
コード例 #2
0
ファイル: repo.py プロジェクト: sghill/repokid
def _repo_all_roles(account_number,
                    dynamo_table,
                    config,
                    hooks,
                    commit=False,
                    scheduled=True,
                    limit=-1):
    """
    Repo all scheduled or eligible roles in an account.  Collect any errors and display them at the end.

    Args:
        account_number (string)
        dynamo_table
        config
        commit (bool): actually make the changes
        scheduled (bool): if True only repo the scheduled roles, if False repo all the (eligible) roles
        limit (int): limit number of roles to be repoed per run (< 0 is unlimited)

    Returns:
        None
    """
    errors = []

    role_ids_in_account = role_ids_for_account(dynamo_table, account_number)
    roles = Roles([])
    for role_id in role_ids_in_account:
        roles.append(
            Role(
                get_role_data(
                    dynamo_table,
                    role_id,
                    fields=["Active", "RoleName", "RepoScheduled"],
                )))

    roles = roles.filter(active=True)

    cur_time = int(time.time())

    if scheduled:
        roles = [
            role for role in roles
            if (role.repo_scheduled and cur_time > role.repo_scheduled)
        ]

    LOGGER.info("Repoing these {}roles from account {}:\n\t{}".format(
        "scheduled " if scheduled else "",
        account_number,
        ", ".join([role.role_name for role in roles]),
    ))

    repokid.hooks.call_hooks(hooks, "BEFORE_REPO_ROLES", {
        "account_number": account_number,
        "roles": roles
    })

    count = 0
    repoed = Roles([])
    for role in roles:
        if limit >= 0 and count == limit:
            break
        error = _repo_role(
            account_number,
            role.role_name,
            dynamo_table,
            config,
            hooks,
            commit=commit,
            scheduled=scheduled,
        )
        if error:
            errors.append(error)
        repoed.append(role)
        count += 1

    if errors:
        LOGGER.error(
            f"Error(s) during repo: \n{errors} (account: {account_number})")
    else:
        LOGGER.info(
            f"Successfully repoed {count} roles in account {account_number}")

    repokid.hooks.call_hooks(
        hooks,
        "AFTER_REPO_ROLES",
        {
            "account_number": account_number,
            "roles": repoed,
            "errors": errors
        },
    )