Esempio n. 1
0
def get_credentials(settings_file, url=None):
    """Return credentials for Nexus instantiation."""
    if settings_file:
        try:
            with open(settings_file, 'r') as f:
                settings = yaml.safe_load(f)
        except IOError:
            log.error('Error reading settings file "{}"'.format(settings_file))
            sys.exit(1)

        if url and set(['user', 'password']).issubset(settings):
            settings['nexus'] = url
            return settings
        elif set(['nexus', 'user', 'password']).issubset(settings):
            return settings
    elif url:
        try:
            user = config.get_setting("nexus", "username")
            password = config.get_setting("nexus", "password")
        except (configparser.NoOptionError, configparser.NoSectionError):
            return {"nexus": url, "user": "", "password": ""}
        return {"nexus": url, "user": user, "password": password}
    log.error(
        'Please define a settings.yaml file, or include a url if using ' +
        'lftools.ini')
    sys.exit(1)
Esempio n. 2
0
def cli(ctx, debug, interactive, password, username):
    """CLI entry point for lftools."""
    if debug:
        logging.getLogger("").setLevel(logging.DEBUG)

    ctx.obj['DEBUG'] = debug
    log.debug('DEBUG mode enabled.')

    # Start > Credentials
    if username is None:
        if interactive:
            username = input('Username: '******'global', 'username')
            except (configparser.NoOptionError,
                    configparser.NoSectionError) as e:
                username = None

    if password is None:
        if interactive:
            password = getpass.getpass('Password: '******'global', 'password')
            except (configparser.NoOptionError,
                    configparser.NoSectionError) as e:
                password = None

    ctx.obj['username'] = username
    ctx.obj['password'] = password
Esempio n. 3
0
def get_setting(ctx, section, option):
    """Print section or setting from config file."""
    try:
        result = config.get_setting(section, option)
    except (configparser.NoOptionError, configparser.NoSectionError) as e:
        log.error(e)
        sys.exit(1)

    if isinstance(result, list):
        for i in result:
            log.info('{}: {}'.format(i, config.get_setting(section, i)))
    else:
        log.info(result)
Esempio n. 4
0
def init(ctx, name, url):
    """Initialize jenkins_jobs.ini config for new server section."""
    jenkins = ctx.obj['jenkins']
    username = ctx.obj['username']
    password = ctx.obj['password']

    if not username or not password:
        log.error('Username or password not set.')
        sys.exit(1)

    _require_jjb_ini(jenkins.config_file)

    config = configparser.ConfigParser()
    config.read(jenkins.config_file)

    token = get_token(url, change=True,
                      username=username, password=password)
    try:
        config.add_section(name)
    except configparser.DuplicateSectionError as e:
        log.error(e)
        sys.exit(1)

    config.set(name, 'url', url)
    config.set(name, 'user', lftools_cfg.get_setting('global', 'username'))
    config.set(name, 'password', token)

    with open(jenkins.config_file, 'w') as configfile:
        config.write(configfile)
Esempio n. 5
0
def modifyrepo(ctx, organization, repository, has_issues, has_projects,
               has_wiki):
    """Modify a Github repo within an Organization.

    By default has_issues has_wiki and has_projects is set to false.
    See --help to use this command to enable these options.
    """
    token = config.get_setting("github", "token")
    g = Github(token)
    orgName = organization

    has_issues = has_issues or False
    has_wiki = has_wiki or False
    has_projects = has_projects or False

    try:
        org = g.get_organization(orgName)
    except GithubException as ghe:
        print(ghe)

    repos = org.get_repos()

    for repo in repos:
        if repo.name == repository:
            repo.edit(has_issues=has_issues)
            repo.edit(has_wiki=has_wiki)
            repo.edit(has_wiki=has_projects)
Esempio n. 6
0
def oauth_helper():
    """Helper script to get access_token for lfid api."""
    logging.getLogger("oauth2client").setLevel(logging.ERROR)
    client_id = config.get_setting("lfid", "clientid")
    client_secret = config.get_setting("lfid", "client_secret")
    refresh_token = config.get_setting("lfid", "refresh_token")
    token_uri = config.get_setting("lfid", "token_uri")
    url = config.get_setting("lfid", "url")

    credentials = client.OAuth2Credentials(
        access_token=
        None,  # set access_token to None since we use a refresh token
        client_id=client_id,
        client_secret=client_secret,
        refresh_token=refresh_token,
        token_expiry=None,
        token_uri=token_uri,
        user_agent=None)
    credentials.refresh(httplib2.Http())
    access_token = credentials.access_token
    return access_token, url
Esempio n. 7
0
def createteam(ctx, organization, name, repo, privacy):
    """Create a Github team within an Organization."""
    token = config.get_setting("github", "token")
    g = Github(token)
    orgName = organization
    print("Creating team {} for repo {} under organization {} ".format(
        name, repo, orgName))
    try:
        org = g.get_organization(orgName)
    except GithubException as ghe:
        print(ghe)

    try:
        repos = org.get_repos
    except GithubException as ghe:
        print(ghe)

    try:
        teams = org.get_teams
    except GithubException as ghe:
        print(ghe)

    my_repos = [repo]
    repos = [repo for repo in repos() if repo.name in my_repos]
    for repo in repos:
        print(repo)

    if repos:
        print("repo found")
    else:
        print("repo not found")
        sys.exit(1)

    for team in teams():
        if team.name == name:
            print("team {} already exists".format(team))
            sys.exit(1)
    try:
        org.create_team(name=name, repo_names=repos, privacy=privacy)
    except GithubException as ghe:
        print(ghe)
Esempio n. 8
0
def createrepo(ctx, organization, repository, description, has_issues,
               has_projects, has_wiki):
    """Create a Github repo within an Organization.

    By default has_issues has_wiki and has_projects is set to false.
    See --help to create a repo with these enabled.
    """
    token = config.get_setting("github", "token")
    g = Github(token)
    orgName = organization
    has_issues = has_issues or False
    has_wiki = has_wiki or False
    has_projects = has_projects or False
    print("Creating repo under organization: ", orgName)
    try:
        org = g.get_organization(orgName)
    except GithubException as ghe:
        print(ghe)
    repos = org.get_repos()
    for repo in repos:
        if repo.name == repository:
            print("repo already exists")
            sys.exit(1)
    try:
        org.create_repo(
            repository,
            allow_rebase_merge=False,
            auto_init=False,
            description=description,
            has_issues=has_issues,
            has_projects=has_projects,
            has_wiki=has_wiki,
            private=False,
        )
    except GithubException as ghe:
        print(ghe)
Esempio n. 9
0
def list(ctx, organization, repos, audit, full, teams, repofeatures):
    """List an Organization's GitHub repos."""
    token = config.get_setting("github", "token")
    g = Github(token)
    orgName = organization

    try:
        org = g.get_organization(orgName)
    except GithubException as ghe:
        print(ghe)

    if repos:
        print("All repos for organization: ", orgName)
        repos = org.get_repos()
        for repo in repos:
            print(repo.name)

    if audit:
        print("{} members without 2fa:".format(orgName))
        try:
            members = org.get_members(filter_="2fa_disabled")
        except GithubException as ghe:
            print(ghe)
        for member in members:
            print(member.login)
        print("{} outside collaborators without 2fa:".format(orgName))
        try:
            collaborators = org.get_outside_collaborators(
                filter_="2fa_disabled")
        except GithubException as ghe:
            print(ghe)
        for collaborator in collaborators:
            print(collaborator.login)

    if repofeatures:
        repos = org.get_repos()
        for repo in repos:
            print("{} wiki:{} issues:{}".format(repo.name, repo.has_wiki,
                                                repo.has_issues))
            issues = repo.get_issues
            for issue in issues():
                print("{}".format(issue))

    if full:
        print("---")
        print("#  All owners for {}:".format(orgName))
        print("{}-owners:".format(orgName))

        try:
            members = org.get_members(role="admin")
        except GithubException as ghe:
            print(ghe)
        for member in members:
            print("  - '{}'".format(member.login))
        print("#  All members for {}".format(orgName))
        print("{}-members:".format(orgName))

        try:
            members = org.get_members()
        except GithubException as ghe:
            print(ghe)
        for member in members:
            print("  - '{}'".format(member.login))
        print("#  All members and all teams for {}".format(orgName))

        try:
            teams = org.get_teams
        except GithubException as ghe:
            print(ghe)
        for team in teams():
            print("{}:".format(team.name))
            for user in team.get_members():
                print("  - '{}'".format(user.login))
            print("")
        teams = None

    if teams:
        try:
            teams = org.get_teams
        except GithubException as ghe:
            print(ghe)
        for team in teams():
            print("{}".format(team.name))
Esempio n. 10
0
def user(ctx, organization, user, team, delete, admin):
    """Add and Remove users from an org team."""
    token = config.get_setting("github", "token")
    g = Github(token)
    orgName = organization
    try:
        org = g.get_organization(orgName)
    except GithubException as ghe:
        print(ghe)
    try:
        user_object = g.get_user(user)
        print(user_object)
    except GithubException as ghe:
        print(ghe)
        print("user {} not found".format(user))
        sys.exit(1)
    # check if user is a member
    try:
        is_member = org.has_in_members(user_object)
        print("Is {} a member of org {}".format(user, is_member))
    except GithubException as ghe:
        print(ghe)
    # get teams
    try:
        teams = org.get_teams
    except GithubException as ghe:
        print(ghe)

    my_teams = [team]
    teams = [team for team in teams() if team.name in my_teams]

    if delete:
        if is_member:
            for t in teams:
                team_id = (t.id)
            try:
                team = org.get_team(team_id)
                team.remove_membership(user_object)
            except GithubException as ghe:
                print(ghe)
        else:
            print("{} is not a member of org cannot delete".format(user))

    if user and not delete:
        if admin and is_member:
            try:
                team.add_membership(member=user_object, role="maintainer")
            except GithubException as ghe:
                print(ghe)
        if admin and not is_member:
            try:
                org.invite_user(user=user_object, role="admin", teams=teams)
            except GithubException as ghe:
                print(ghe)

        if not admin and is_member:
            try:
                team.add_membership(member=user_object, role="member")
            except GithubException as ghe:
                print(ghe)

        if not admin and not is_member:
            try:
                org.invite_user(user=user_object, teams=teams)
            except GithubException as ghe:
                print(ghe)