Exemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup', action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects', metavar='project', nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    default_has_github = registry.get_defaults('has-github', True)

    LOCAL_GIT_DIR = registry.get_defaults('local-git-dir', '/var/lib/git')
    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    ACL_DIR = registry.get_defaults('acl-dir')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')
    GERRIT_REPLICATE = registry.get_defaults('gerrit-replicate', True)
    GERRIT_OS_SYSTEM_USER = registry.get_defaults('gerrit-system-user',
                                                  'gerrit2')
    GERRIT_OS_SYSTEM_GROUP = registry.get_defaults('gerrit-system-group',
                                                   'gerrit2')
    DEFAULT_HOMEPAGE = registry.get_defaults('homepage')
    DEFAULT_HAS_ISSUES = registry.get_defaults('has-issues', False)
    DEFAULT_HAS_DOWNLOADS = registry.get_defaults('has-downloads', False)
    DEFAULT_HAS_WIKI = registry.get_defaults('has-wiki', False)
    GITHUB_SECURE_CONFIG = registry.get_defaults(
        'github-config',
        '/etc/github/github-projects.secure.config')

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST,
                                     GERRIT_USER,
                                     GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                description = section.get('description', None)
                homepage = section.get('homepage', DEFAULT_HOMEPAGE)
                upstream = section.get('upstream', None)
                upstream_prefix = section.get('upstream-prefix', None)
                track_upstream = 'track-upstream' in options
                repo_path = os.path.join(JEEPYB_CACHE_DIR, project)

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (
                    GERRIT_HOST,
                    GERRIT_PORT,
                    project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                acl_config = section.get(
                    'acl-config',
                    '%s.config' % os.path.join(ACL_DIR, project))

                # Create the project in Gerrit first, since it will fail
                # spectacularly if its project directory or local replica
                # already exist on disk
                project_created = create_gerrit_project(
                    project, project_list, gerrit)

                # Create the repo for the local git mirror
                create_local_mirror(
                    LOCAL_GIT_DIR, project_git,
                    GERRIT_OS_SYSTEM_USER, GERRIT_OS_SYSTEM_GROUP)

                if not os.path.exists(repo_path) or project_created:
                    # We don't have a local copy already, get one

                    # Make Local repo
                    push_string = make_local_copy(
                        repo_path, project, project_list,
                        git_opts, ssh_env, upstream, GERRIT_HOST, GERRIT_PORT,
                        project_git, GERRIT_GITID)
                else:
                    # We do have a local copy of it already, make sure it's
                    # in shape to have work done.
                    update_local_copy(
                        repo_path, track_upstream, git_opts, ssh_env)

                description = (
                    find_description_override(repo_path) or description)

                if project_created:
                    push_to_gerrit(
                        repo_path, project, push_string, remote_url, ssh_env)
                    if GERRIT_REPLICATE:
                        gerrit.replicate(project)

                # If we're configured to track upstream, make sure we have
                # upstream's refs, and then push them to the appropriate
                # branches in gerrit
                if track_upstream:
                    sync_upstream(repo_path, project, ssh_env, upstream_prefix)

                if acl_config:
                    process_acls(
                        acl_config, project, ACL_DIR, section,
                        remote_url, repo_path, ssh_env, gerrit, GERRIT_GITID)

                if 'has-github' in options or default_has_github:
                    created = create_github_project(
                        DEFAULT_HAS_ISSUES, DEFAULT_HAS_DOWNLOADS,
                        DEFAULT_HAS_WIKI, GITHUB_SECURE_CONFIG,
                        options, project, description, homepage)
                    if created and GERRIT_REPLICATE:
                        gerrit.replicate(project)

            except Exception:
                log.exception(
                    "Problems creating %s, moving on." % project)
                continue
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 2
0
def get_proj_list(gerrit):
    project_list = gerrit.listProjects()

    return project_list
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup',
                        action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects',
                        metavar='project',
                        nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    default_has_github = registry.get_defaults('has-github', True)

    LOCAL_GIT_DIR = registry.get_defaults('local-git-dir', '/var/lib/git')
    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    ACL_DIR = registry.get_defaults('acl-dir')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')
    GERRIT_REPLICATE = registry.get_defaults('gerrit-replicate', True)
    GERRIT_OS_SYSTEM_USER = registry.get_defaults('gerrit-system-user',
                                                  'gerrit2')
    GERRIT_OS_SYSTEM_GROUP = registry.get_defaults('gerrit-system-group',
                                                   'gerrit2')
    DEFAULT_HOMEPAGE = registry.get_defaults('homepage')
    DEFAULT_HAS_ISSUES = registry.get_defaults('has-issues', False)
    DEFAULT_HAS_DOWNLOADS = registry.get_defaults('has-downloads', False)
    DEFAULT_HAS_WIKI = registry.get_defaults('has-wiki', False)
    GITHUB_SECURE_CONFIG = registry.get_defaults(
        'github-config', '/etc/github/github-projects.secure.config')

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST, GERRIT_USER, GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                description = section.get('description', None)
                homepage = section.get('homepage', DEFAULT_HOMEPAGE)
                upstream = section.get('upstream', None)
                upstream_prefix = section.get('upstream-prefix', None)
                track_upstream = 'track-upstream' in options
                repo_path = os.path.join(JEEPYB_CACHE_DIR, project)

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (GERRIT_HOST, GERRIT_PORT,
                                                 project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                acl_config = section.get(
                    'acl-config', '%s.config' % os.path.join(ACL_DIR, project))

                # Create the project in Gerrit first, since it will fail
                # spectacularly if its project directory or local replica
                # already exist on disk
                project_created = create_gerrit_project(
                    project, project_list, gerrit)

                # Create the repo for the local git mirror
                create_local_mirror(LOCAL_GIT_DIR, project_git,
                                    GERRIT_OS_SYSTEM_USER,
                                    GERRIT_OS_SYSTEM_GROUP)

                if not os.path.exists(repo_path) or project_created:
                    # We don't have a local copy already, get one

                    # Make Local repo
                    push_string = make_local_copy(repo_path, project,
                                                  project_list, git_opts,
                                                  ssh_env, upstream,
                                                  GERRIT_HOST, GERRIT_PORT,
                                                  project_git, GERRIT_GITID)
                else:
                    # We do have a local copy of it already, make sure it's
                    # in shape to have work done.
                    update_local_copy(repo_path, track_upstream, git_opts,
                                      ssh_env)

                description = (find_description_override(repo_path)
                               or description)

                if project_created:
                    push_to_gerrit(repo_path, project, push_string, remote_url,
                                   ssh_env)
                    if GERRIT_REPLICATE:
                        gerrit.replicate(project)

                # If we're configured to track upstream, make sure we have
                # upstream's refs, and then push them to the appropriate
                # branches in gerrit
                if track_upstream:
                    sync_upstream(repo_path, project, ssh_env, upstream_prefix)

                if acl_config:
                    process_acls(acl_config, project, ACL_DIR, section,
                                 remote_url, repo_path, ssh_env, gerrit,
                                 GERRIT_GITID)

                if 'has-github' in options or default_has_github:
                    created = create_github_project(DEFAULT_HAS_ISSUES,
                                                    DEFAULT_HAS_DOWNLOADS,
                                                    DEFAULT_HAS_WIKI,
                                                    GITHUB_SECURE_CONFIG,
                                                    options, project,
                                                    description, homepage)
                    if created and GERRIT_REPLICATE:
                        gerrit.replicate(project)

            except Exception:
                log.exception("Problems creating %s, moving on." % project)
                continue
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 4
0
def get_proj_list(gerrit):
    project_list = gerrit.listProjects()

    return project_list
Exemplo n.º 5
0
def main():

    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
                                   '/home/gerrit2/projects.yaml')
    configs = [config for config in yaml.load_all(open(PROJECTS_YAML))]
    defaults = configs[0][0]
    default_has_github = defaults.get('has-github', True)

    LOCAL_GIT_DIR = defaults.get('local-git-dir', '/var/lib/git')
    ACL_DIR = defaults.get('acl-dir')
    GERRIT_HOST = defaults.get('gerrit-host')
    GERRIT_USER = defaults.get('gerrit-user')
    GERRIT_KEY = defaults.get('gerrit-key')
    GERRIT_GITID = defaults.get('gerrit-committer')

    gerrit = gerritlib.gerrit.Gerrit('localhost',
                                     GERRIT_USER,
                                     29418,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in configs[1]:
            project = section['project']
            options = section.get('options', dict())
            description = section.get('description', None)
            homepage = section.get('homepage', defaults.get('homepage', None))
            upstream = section.get('upstream', None)

            project_git = "%s.git" % project
            project_dir = os.path.join(LOCAL_GIT_DIR, project_git)

            if 'has-github' in options or default_has_github:
                create_github_project(defaults, options, project,
                                      description, homepage)

            remote_url = "ssh://localhost:29418/%s" % project
            if project not in project_list:
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    if upstream:
                        run_command("git clone %(upstream)s %(repo_path)s" %
                                    dict(upstream=upstream,
                                         repo_path=repo_path))
                        git_command(repo_path,
                                    "fetch origin "
                                    "+refs/heads/*:refs/copy/heads/*",
                                    env=ssh_env)
                        push_string = "push %s +refs/copy/heads/*:refs/heads/*"
                    else:
                        run_command("git init %s" % repo_path)
                        with open(os.path.join(repo_path,
                                               ".gitreview"),
                                  'w') as gitreview:
                            gitreview.write("""[gerrit]
host=%s
port=29418
project=%s
""" % (GERRIT_HOST, project_git))
                        git_command(repo_path, "add .gitreview")
                        cmd = ("commit -a -m'Added .gitreview' --author='%s'"
                               % GERRIT_GITID)
                        git_command(repo_path, cmd)
                        push_string = "push --all %s"
                    gerrit.createProject(project)

                    if not os.path.exists(project_dir):
                        run_command("git --bare init %s" % project_dir)
                        run_command("chown -R gerrit2:gerrit2 %s"
                                    % project_dir)

                    git_command(repo_path,
                                push_string % remote_url,
                                env=ssh_env)
                    git_command(repo_path,
                                "push --tags %s" % remote_url, env=ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)

            try:
                acl_config = section.get('acl-config',
                                         '%s.config' % os.path.join(ACL_DIR,
                                                                    project))
            except AttributeError:
                acl_config = None

            if acl_config:
                if not os.path.isfile(acl_config):
                    write_acl_config(project,
                                     ACL_DIR,
                                     section.get('acl-base', None),
                                     section.get('acl-append', []),
                                     section.get('acl-parameters', {}))
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    ret, _ = run_command_status("git init %s" % repo_path)
                    if ret != 0:
                        continue
                    if (fetch_config(project,
                                     remote_url,
                                     repo_path,
                                     ssh_env) and
                        copy_acl_config(project, repo_path,
                                        acl_config) and
                            create_groups_file(project, gerrit, repo_path)):
                        push_acl_config(project,
                                        remote_url,
                                        repo_path,
                                        GERRIT_GITID,
                                        ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup', action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects', metavar='project', nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    IMPORT_DIR = os.path.join(JEEPYB_CACHE_DIR, 'import')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')

    PROJECT_CACHE_FILE = os.path.join(JEEPYB_CACHE_DIR, 'project.cache')
    project_cache = {}
    if os.path.exists(PROJECT_CACHE_FILE):
        project_cache = json.loads(open(PROJECT_CACHE_FILE, 'r').read())

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST,
                                     GERRIT_USER,
                                     GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = u.make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                track_upstream = 'track-upstream' in options
                if not track_upstream:
                    continue

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                upstream = section.get('upstream', None)
                upstream_prefix = section.get('upstream-prefix', None)
                repo_path = os.path.join(IMPORT_DIR, project)

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (
                    GERRIT_HOST,
                    GERRIT_PORT,
                    project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                project_cache.setdefault(project, {})
                if not project_cache[project]['pushed-to-gerrit']:
                    continue

                # Make Local repo
                if not os.path.exists(repo_path):
                    u.make_local_copy(
                        repo_path, project, project_list,
                        git_opts, ssh_env, upstream, GERRIT_HOST,
                        GERRIT_PORT, project_git, GERRIT_GITID)
                else:
                    update_local_copy(
                        repo_path, track_upstream, git_opts, ssh_env)

                fsck_repo(repo_path)
                sync_upstream(repo_path, project, ssh_env, upstream_prefix)

            except Exception:
                log.exception(
                    "Problems creating %s, moving on." % project)
                continue
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 7
0
def main():

    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
                                   '/home/gerrit2/projects.yaml')
    configs = [config for config in yaml.load_all(open(PROJECTS_YAML))]
    defaults = configs[0][0]
    default_has_issues = defaults.get('has-issues', False)
    default_has_downloads = defaults.get('has-downloads', False)
    default_has_wiki = defaults.get('has-wiki', False)

    LOCAL_GIT_DIR = defaults.get('local-git-dir', '/var/lib/git')
    ACL_DIR = defaults.get('acl-dir')
    GERRIT_HOST = defaults.get('gerrit-host')
    GERRIT_USER = defaults.get('gerrit-user')
    GERRIT_KEY = defaults.get('gerrit-key')

    GITHUB_SECURE_CONFIG = defaults.get(
        'github-config',
        '/etc/github/github-projects.secure.config')

    secure_config = ConfigParser.ConfigParser()
    secure_config.read(GITHUB_SECURE_CONFIG)

    # Project creation doesn't work via oauth
    ghub = github.Github(secure_config.get("github", "username"),
                         secure_config.get("github", "password"))
    orgs = ghub.get_user().get_orgs()
    orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs))

    gerrit = gerritlib.gerrit.Gerrit('localhost',
                                     GERRIT_USER,
                                     29418,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in configs[1]:
            project = section['project']
            options = section.get('options', dict())
            description = section.get('description', None)
            homepage = section.get('homepage', defaults.get('homepage', None))
            upstream = section.get('upstream', None)

            project_git = "%s.git" % project
            project_dir = os.path.join(LOCAL_GIT_DIR, project_git)

            # Find the project's repo
            project_split = project.split('/', 1)
            if len(project_split) > 1:
                repo_name = project_split[1]
            else:
                repo_name = project
            has_issues = 'has-issues' in options or default_has_issues
            has_downloads = 'has-downloads' in options or default_has_downloads
            has_wiki = 'has-wiki' in options or default_has_wiki
            try:
                org = orgs_dict[project_split[0].lower()]
            except KeyError:
                # We do not have control of this github org ignore the project.
                continue
            try:
                repo = org.get_repo(repo_name)
            except github.GithubException:
                repo = org.create_repo(repo_name,
                                       homepage=homepage,
                                       has_issues=has_issues,
                                       has_downloads=has_downloads,
                                       has_wiki=has_wiki)
            if description:
                repo.edit(repo_name, description=description)
            if homepage:
                repo.edit(repo_name, homepage=homepage)

            repo.edit(repo_name, has_issues=has_issues,
                      has_downloads=has_downloads,
                      has_wiki=has_wiki)

            if 'gerrit' not in [team.name for team in repo.get_teams()]:
                teams = org.get_teams()
                teams_dict = dict(zip([t.name.lower() for t in teams], teams))
                teams_dict['gerrit'].add_to_repos(repo)

            remote_url = "ssh://*****:*****@lists.openstack.org>'"
                        git_command(repo_path, cmd)
                        push_string = "push --all %s"
                    gerrit.createProject(project)

                    if not os.path.exists(project_dir):
                        run_command("git --bare init %s" % project_dir)
                        run_command("chown -R gerrit2:gerrit2 %s"
                                    % project_dir)

                    git_command(repo_path,
                                push_string % remote_url,
                                env=ssh_env)
                    git_command(repo_path,
                                "push --tags %s" % remote_url, env=ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)

            try:
                acl_config = section.get('acl-config',
                                         '%s.config' % os.path.join(ACL_DIR,
                                                                    project))
            except AttributeError:
                acl_config = None

            if acl_config:
                if not os.path.isfile(acl_config):
                    write_acl_config(project,
                                     ACL_DIR,
                                     section.get('acl-base', None),
                                     section.get('acl-append', []),
                                     section.get('acl-parameters', {}))
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    ret, _ = run_command_status("git init %s" % repo_path)
                    if ret != 0:
                        continue
                    if (fetch_config(project,
                                     remote_url,
                                     repo_path,
                                     ssh_env) and
                        copy_acl_config(project, repo_path,
                                        acl_config) and
                            create_groups_file(project, gerrit, repo_path)):
                        push_acl_config(project,
                                        remote_url,
                                        repo_path,
                                        ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 8
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup', action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects', metavar='project', nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    IMPORT_DIR = os.path.join(JEEPYB_CACHE_DIR, 'import')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')

    PROJECT_CACHE_FILE = os.path.join(JEEPYB_CACHE_DIR, 'project.cache')
    project_cache = {}
    if os.path.exists(PROJECT_CACHE_FILE):
        project_cache = json.loads(open(PROJECT_CACHE_FILE, 'r').read())

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST,
                                     GERRIT_USER,
                                     GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = u.make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                track_upstream = 'track-upstream' in options
                if not track_upstream:
                    continue

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                upstream = section.get('upstream', None)
                upstream_prefix = section.get('upstream-prefix', None)
                repo_path = os.path.join(IMPORT_DIR, project)

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (
                    GERRIT_HOST,
                    GERRIT_PORT,
                    project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                project_cache.setdefault(project, {})
                if not project_cache[project]['pushed-to-gerrit']:
                    continue

                # Make Local repo
                if not os.path.exists(repo_path):
                    u.make_local_copy(
                        repo_path, project, project_list,
                        git_opts, ssh_env, upstream, GERRIT_HOST,
                        GERRIT_PORT, project_git, GERRIT_GITID)
                else:
                    update_local_copy(
                        repo_path, track_upstream, git_opts, ssh_env)

                u.fsck_repo(repo_path)
                sync_upstream(repo_path, project, ssh_env, upstream_prefix)

            except Exception:
                log.exception(
                    "Problems creating %s, moving on." % project)
                continue
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 9
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup',
                        action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects',
                        metavar='project',
                        nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    default_has_github = registry.get_defaults('has-github', True)

    LOCAL_GIT_DIR = registry.get_defaults('local-git-dir', '/var/lib/git')
    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    ACL_DIR = registry.get_defaults('acl-dir')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GITREVIEW_GERRIT_HOST = registry.get_defaults('gitreview-gerrit-host',
                                                  GERRIT_HOST)
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GITREVIEW_GERRIT_PORT = int(
        registry.get_defaults('gitreview-gerrit-port', GERRIT_PORT))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')
    GERRIT_REPLICATE = registry.get_defaults('gerrit-replicate', True)
    GERRIT_OS_SYSTEM_USER = registry.get_defaults('gerrit-system-user',
                                                  'gerrit2')
    GERRIT_OS_SYSTEM_GROUP = registry.get_defaults('gerrit-system-group',
                                                   'gerrit2')
    DEFAULT_HOMEPAGE = registry.get_defaults('homepage')
    DEFAULT_HAS_ISSUES = registry.get_defaults('has-issues', False)
    DEFAULT_HAS_DOWNLOADS = registry.get_defaults('has-downloads', False)
    DEFAULT_HAS_WIKI = registry.get_defaults('has-wiki', False)
    GITHUB_SECURE_CONFIG = registry.get_defaults(
        'github-config', '/etc/github/github-projects.secure.config')
    PROJECT_CACHE_FILE = os.path.join(JEEPYB_CACHE_DIR, 'project.cache')
    project_cache = {}
    if os.path.exists(PROJECT_CACHE_FILE):
        project_cache = json.loads(open(PROJECT_CACHE_FILE, 'r').read())
    acl_cache = {}
    for acl_file in glob.glob(os.path.join(ACL_DIR, '*/*.config')):
        sha256 = hashlib.sha256()
        sha256.update(open(acl_file, 'r').read())
        acl_cache[acl_file] = sha256.hexdigest()

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST, GERRIT_USER, GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = u.make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:
        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                description = section.get('description', None)
                homepage = section.get('homepage', DEFAULT_HOMEPAGE)
                upstream = section.get('upstream', None)
                repo_path = os.path.join(JEEPYB_CACHE_DIR, project)

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (GERRIT_HOST, GERRIT_PORT,
                                                 project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                acl_config = section.get(
                    'acl-config', '%s.config' % os.path.join(ACL_DIR, project))
                project_cache.setdefault(project, {})

                # Create the project in Gerrit first, since it will fail
                # spectacularly if its project directory or local replica
                # already exist on disk
                project_created = project_cache[project].get(
                    'project-created', False)
                if not project_created:
                    try:
                        project_created = create_gerrit_project(
                            project, project_list, gerrit)
                        project_cache[project]['project-created'] = True
                    except Exception:
                        project_cache[project]['project-created'] = False
                        continue

                pushed_to_gerrit = project_cache[project].get(
                    'pushed-to-gerrit', False)
                if not pushed_to_gerrit:
                    # We haven't pushed to gerrit, so grab the repo again
                    if os.path.exists(repo_path):
                        shutil.rmtree(repo_path)

                    # Make Local repo
                    push_string = u.make_local_copy(repo_path, project,
                                                    project_list, git_opts,
                                                    ssh_env, upstream,
                                                    GITREVIEW_GERRIT_HOST,
                                                    GITREVIEW_GERRIT_PORT,
                                                    project_git, GERRIT_GITID)

                    description = (find_description_override(repo_path)
                                   or description)

                    u.fsck_repo(repo_path)

                    if push_string:
                        push_to_gerrit(repo_path, project, push_string,
                                       remote_url, ssh_env)
                    project_cache[project]['pushed-to-gerrit'] = True
                    if GERRIT_REPLICATE:
                        gerrit.replicate(project)

                # Create the repo for the local git mirror
                create_local_mirror(LOCAL_GIT_DIR, project_git,
                                    GERRIT_OS_SYSTEM_USER,
                                    GERRIT_OS_SYSTEM_GROUP)

                if acl_config:
                    acl_sha = acl_cache.get(acl_config)
                    if project_cache[project].get('acl-sha') != acl_sha:

                        if not os.path.exists(repo_path):
                            u.make_local_copy(repo_path, project, project_list,
                                              git_opts, ssh_env, upstream,
                                              GERRIT_HOST, GERRIT_PORT,
                                              project_git, GERRIT_GITID)
                        process_acls(acl_config, project, ACL_DIR, section,
                                     remote_url, repo_path, ssh_env, gerrit,
                                     GERRIT_GITID)
                        project_cache[project]['acl-sha'] = acl_sha
                    else:
                        log.info("%s has matching sha, skipping ACLs", project)

                if 'has-github' in options or default_has_github:
                    created = create_update_github_project(
                        DEFAULT_HAS_ISSUES, DEFAULT_HAS_DOWNLOADS,
                        DEFAULT_HAS_WIKI, GITHUB_SECURE_CONFIG, options,
                        project, description, homepage, project_cache[project])
                    if created and GERRIT_REPLICATE:
                        gerrit.replicate(project)
                    project_cache[project]['created-in-github'] = created

            except Exception:
                log.exception("Problems creating %s, moving on." % project)
                continue
            finally:
                # Clean up after ourselves - this repo has no use
                if os.path.exists(repo_path):
                    shutil.rmtree(repo_path)
    finally:
        with open(PROJECT_CACHE_FILE, 'w') as cache_out:
            log.info("Writing cache file %s", PROJECT_CACHE_FILE)
            cache_out.write(json.dumps(project_cache, sort_keys=True,
                                       indent=2))
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 10
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    l.setup_logging_arguments(parser)
    parser.add_argument('--nocleanup', action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects', metavar='project', nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()
    l.configure_logging(args)

    default_has_github = registry.get_defaults('has-github', True)

    LOCAL_GIT_DIR = registry.get_defaults('local-git-dir', '/var/lib/git')
    JEEPYB_CACHE_DIR = registry.get_defaults('jeepyb-cache-dir',
                                             '/var/lib/jeepyb')
    ACL_DIR = registry.get_defaults('acl-dir')
    GERRIT_HOST = registry.get_defaults('gerrit-host')
    GITREVIEW_GERRIT_HOST = registry.get_defaults(
        'gitreview-gerrit-host', GERRIT_HOST)
    GERRIT_PORT = int(registry.get_defaults('gerrit-port', '29418'))
    GITREVIEW_GERRIT_PORT = int(registry.get_defaults(
        'gitreview-gerrit-port', GERRIT_PORT))
    GERRIT_USER = registry.get_defaults('gerrit-user')
    GERRIT_KEY = registry.get_defaults('gerrit-key')
    GERRIT_GITID = registry.get_defaults('gerrit-committer')
    GERRIT_REPLICATE = registry.get_defaults('gerrit-replicate', True)
    GERRIT_OS_SYSTEM_USER = registry.get_defaults('gerrit-system-user',
                                                  'gerrit2')
    GERRIT_OS_SYSTEM_GROUP = registry.get_defaults('gerrit-system-group',
                                                   'gerrit2')
    DEFAULT_HOMEPAGE = registry.get_defaults('homepage')
    DEFAULT_HAS_ISSUES = registry.get_defaults('has-issues', False)
    DEFAULT_HAS_DOWNLOADS = registry.get_defaults('has-downloads', False)
    DEFAULT_HAS_WIKI = registry.get_defaults('has-wiki', False)
    GITHUB_SECURE_CONFIG = registry.get_defaults(
        'github-config',
        '/etc/github/github-projects.secure.config')
    PROJECT_CACHE_FILE = os.path.join(JEEPYB_CACHE_DIR, 'project.cache')
    project_cache = {}
    if os.path.exists(PROJECT_CACHE_FILE):
        project_cache = json.loads(open(PROJECT_CACHE_FILE, 'r').read())
    acl_cache = {}
    for acl_file in glob.glob(os.path.join(ACL_DIR, '*/*.config')):
        sha256 = hashlib.sha256()
        sha256.update(open(acl_file, 'r').read())
        acl_cache[acl_file] = sha256.hexdigest()

    gerrit = gerritlib.gerrit.Gerrit(GERRIT_HOST,
                                     GERRIT_USER,
                                     GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = u.make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:
        # Collect processed errors,if any
        process_errors = []
        for section in registry.configs_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                log.info("Processing project: %s" % project)

                # Figure out all of the options
                options = section.get('options', dict())
                description = section.get('description', None)
                homepage = section.get('homepage', DEFAULT_HOMEPAGE)
                upstream = section.get('upstream', None)
                repo_path = os.path.join(JEEPYB_CACHE_DIR, project)

                # If this project doesn't want to use gerrit, exit cleanly.
                if 'no-gerrit' in options:
                    continue

                project_git = "%s.git" % project
                remote_url = "ssh://%s:%s/%s" % (
                    GERRIT_HOST,
                    GERRIT_PORT,
                    project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                acl_config = section.get(
                    'acl-config',
                    '%s.config' % os.path.join(ACL_DIR, project))
                project_cache.setdefault(project, {})

                # Create the project in Gerrit first, since it will fail
                # spectacularly if its project directory or local replica
                # already exist on disk
                project_created = project_cache[project].get(
                    'project-created', False)
                if not project_created:
                    try:
                        project_created = create_gerrit_project(
                            project, project_list, gerrit)
                        project_cache[project]['project-created'] = True
                    except Exception:
                        project_cache[project]['project-created'] = False
                        continue

                pushed_to_gerrit = project_cache[project].get(
                    'pushed-to-gerrit', False)
                if not pushed_to_gerrit:
                    # We haven't pushed to gerrit, so grab the repo again
                    if os.path.exists(repo_path):
                        shutil.rmtree(repo_path)

                    # Make Local repo
                    push_string = u.make_local_copy(
                        repo_path, project, project_list,
                        git_opts, ssh_env, upstream, GITREVIEW_GERRIT_HOST,
                        GITREVIEW_GERRIT_PORT, project_git, GERRIT_GITID)

                    description = (
                        find_description_override(repo_path)
                        or description)

                    u.fsck_repo(repo_path)

                    if push_string:
                        push_to_gerrit(
                            repo_path, project, push_string,
                            remote_url, ssh_env)
                    project_cache[project]['pushed-to-gerrit'] = True
                    if GERRIT_REPLICATE:
                        gerrit.replicate(project)

                # Create the repo for the local git mirror
                create_local_mirror(
                    LOCAL_GIT_DIR, project_git,
                    GERRIT_OS_SYSTEM_USER, GERRIT_OS_SYSTEM_GROUP)

                if acl_config:
                    acl_sha = acl_cache.get(acl_config)
                    if project_cache[project].get('acl-sha') != acl_sha:

                        if not os.path.exists(repo_path):
                            u.make_local_copy(
                                repo_path, project, project_list,
                                git_opts, ssh_env, upstream, GERRIT_HOST,
                                GERRIT_PORT, project_git, GERRIT_GITID)
                        process_acls(
                            acl_config, project, ACL_DIR, section,
                            remote_url, repo_path, ssh_env, gerrit,
                            GERRIT_GITID)
                        project_cache[project]['acl-sha'] = acl_sha
                    else:
                        log.info("%s has matching sha, skipping ACLs",
                                 project)

                if 'has-github' in options or default_has_github:
                    created = create_update_github_project(
                        DEFAULT_HAS_ISSUES, DEFAULT_HAS_DOWNLOADS,
                        DEFAULT_HAS_WIKI, GITHUB_SECURE_CONFIG,
                        options, project, description, homepage,
                        project_cache[project])
                    if created and GERRIT_REPLICATE:
                        gerrit.replicate(project)
                    project_cache[project]['created-in-github'] = created

            except Exception:
                msg = "Problems creating %s, moving on." % project
                log.exception(msg)
                process_errors.append(msg)
                continue
            finally:
                # Clean up after ourselves - this repo has no use
                if os.path.exists(repo_path):
                    shutil.rmtree(repo_path)
    finally:
        with open(PROJECT_CACHE_FILE, 'w') as cache_out:
            log.info("Writing cache file %s", PROJECT_CACHE_FILE)
            cache_out.write(json.dumps(
                project_cache, sort_keys=True, indent=2))
        os.unlink(ssh_env['GIT_SSH'])
        if len(process_errors) > 0:
            log.error("%d problems has been caught during run:\n %s" % (
                len(process_errors), process_errors))
            sys.exit(1)
Exemplo n.º 11
0
def main():
    parser = argparse.ArgumentParser(description='Manage projects')
    parser.add_argument('-v', dest='verbose', action='store_true',
                        help='verbose output')
    parser.add_argument('--nocleanup', action='store_true',
                        help='do not remove temp directories')
    parser.add_argument('projects', metavar='project', nargs='*',
                        help='name of project(s) to process')
    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.ERROR)

    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
                                   '/home/gerrit2/projects.yaml')
    yaml_docs = [config for config in yaml.safe_load_all(open(PROJECTS_YAML))]

    PROJECTS_INI = os.environ.get('PROJECTS_INI',
                                  '/home/gerrit2/projects.ini')
    if os.path.exists(PROJECTS_INI):
        # New-style - supports projects.ini
        projects_yaml_list = yaml_docs[0]
        defaults = ConfigParser.ConfigParser()
        defaults.read(PROJECTS_INI)

        default_has_github = get_option(
            defaults, 'projects', 'has-github', True)

        LOCAL_GIT_DIR = get_option(
            defaults, 'projects', 'local-git-dir', '/var/lib/git')
        JEEPYB_CACHE_DIR = get_option(
            defaults, 'projects', 'jeepyb-cache-dir', '/var/lib/jeepyb')
        ACL_DIR = defaults.get('projects', 'acl-dir')
        GERRIT_HOST = defaults.get('projects', 'gerrit-host')
        GERRIT_PORT = int(get_option(
            defaults, 'projects', 'gerrit-port', '29418'))
        GERRIT_USER = defaults.get('projects', 'gerrit-user')
        GERRIT_KEY = defaults.get('projects', 'gerrit-key')
        GERRIT_GITID = defaults.get('projects', 'gerrit-committer')
        GERRIT_SYSTEM_USER = get_option(
            defaults, 'projects', 'gerrit-system-user', 'gerrit2')
        GERRIT_SYSTEM_GROUP = get_option(
            defaults, 'projects', 'gerrit-system-group', 'gerrit2')
        DEFAULT_HOMEPAGE = get_option(defaults, 'projects', 'homepage', None)
        DEFAULT_HAS_ISSUES = get_option(
            defaults, 'projects', 'has-issues', False)
        DEFAULT_HAS_DOWNLOADS = get_option(
            defaults, 'projects', 'has-downloads', False)
        DEFAULT_HAS_WIKI = get_option(defaults, 'projects', 'has-wiki', False)
        GITHUB_SECURE_CONFIG = get_option(
            defaults, 'projects', 'github-config',
            '/etc/github/github-projects.secure.config')
    else:
        # Old-style - embedded
        projects_yaml_list = yaml_docs[1]
        defaults = yaml_docs[0][0]
        default_has_github = defaults.get('has-github', True)

        LOCAL_GIT_DIR = defaults.get('local-git-dir', '/var/lib/git')
        JEEPYB_CACHE_DIR = defaults.get('jeepyb-cache-dir', '/var/lib/jeepyb')
        ACL_DIR = defaults.get('acl-dir')
        GERRIT_HOST = defaults.get('gerrit-host')
        GERRIT_PORT = int(defaults.get('gerrit-port', '29418'))
        GERRIT_USER = defaults.get('gerrit-user')
        GERRIT_KEY = defaults.get('gerrit-key')
        GERRIT_GITID = defaults.get('gerrit-committer')
        GERRIT_SYSTEM_USER = defaults.get('gerrit-system-user', 'gerrit2')
        GERRIT_SYSTEM_GROUP = defaults.get('gerrit-system-group', 'gerrit2')
        DEFAULT_HOMEPAGE = defaults.get('homepage', None)
        DEFAULT_HAS_ISSUES = defaults.get('has-issues', False)
        DEFAULT_HAS_DOWNLOADS = defaults.get('has-downloads', False)
        DEFAULT_HAS_WIKI = defaults.get('has-wiki', False)
        GITHUB_SECURE_CONFIG = defaults.get(
            'github-config',
            '/etc/github/github-projects.secure.config')

    gerrit = gerritlib.gerrit.Gerrit('localhost',
                                     GERRIT_USER,
                                     GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in projects_yaml_list:
            project = section['project']
            if args.projects and project not in args.projects:
                continue

            try:
                # Figure out all of the options
                options = section.get('options', dict())
                description = section.get('description', None)
                homepage = section.get('homepage', DEFAULT_HOMEPAGE)
                upstream = section.get('upstream', None)
                upstream_prefix = section.get('upstream-prefix', None)
                track_upstream = 'track-upstream' in options
                repo_path = os.path.join(JEEPYB_CACHE_DIR, project)

                project_git = "%s.git" % project
                remote_url = "ssh://localhost:%s/%s" % (GERRIT_PORT, project)
                git_opts = dict(upstream=upstream,
                                repo_path=repo_path,
                                remote_url=remote_url)
                acl_config = section.get(
                    'acl-config',
                    '%s.config' % os.path.join(ACL_DIR, project))

                # Create the project in Gerrit first, since it will fail
                # spectacularly if its project directory or local replica
                # already exist on disk
                project_created = create_gerrit_project(
                    project, project_list, gerrit)

                # Create the repo for the local git mirror
                create_local_mirror(
                    LOCAL_GIT_DIR, project_git,
                    GERRIT_SYSTEM_USER, GERRIT_SYSTEM_GROUP)

                if not os.path.exists(repo_path) or project_created:
                    # We don't have a local copy already, get one

                    # Make Local repo
                    push_string = make_local_copy(
                        repo_path, project, project_list,
                        git_opts, ssh_env, upstream, GERRIT_HOST, GERRIT_PORT,
                        project_git, GERRIT_GITID)
                else:
                    # We do have a local copy of it already, make sure it's
                    # in shape to have work done.
                    update_local_copy(
                        repo_path, track_upstream, git_opts, ssh_env)

                description = (
                    find_description_override(repo_path) or description)

                if project_created:
                    push_to_gerrit(
                        repo_path, project, push_string, remote_url, ssh_env)
                    gerrit.replicate(project)

                # If we're configured to track upstream, make sure we have
                # upstream's refs, and then push them to the appropriate
                # branches in gerrit
                if track_upstream:
                    sync_upstream(repo_path, project, ssh_env, upstream_prefix)

                if acl_config:
                    process_acls(
                        acl_config, project, ACL_DIR, section,
                        remote_url, repo_path, ssh_env, gerrit, GERRIT_GITID)

                if 'has-github' in options or default_has_github:
                    created = create_github_project(
                        DEFAULT_HAS_ISSUES, DEFAULT_HAS_DOWNLOADS,
                        DEFAULT_HAS_WIKI, GITHUB_SECURE_CONFIG,
                        options, project, description, homepage)
                    if created:
                        gerrit.replicate(project)

            except Exception:
                log.exception(
                    "Problems creating %s, moving on." % project)
                continue
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 12
0
def main():

    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
                                   '/home/gerrit2/projects.yaml')
    configs = [config for config in yaml.load_all(open(PROJECTS_YAML))]
    defaults = configs[0][0]
    default_has_github = defaults.get('has-github', True)

    LOCAL_GIT_DIR = defaults.get('local-git-dir', '/var/lib/git')
    ACL_DIR = defaults.get('acl-dir')
    GERRIT_HOST = defaults.get('gerrit-host')
    GERRIT_PORT = int(defaults.get('gerrit-port', '29418'))
    GERRIT_USER = defaults.get('gerrit-user')
    GERRIT_KEY = defaults.get('gerrit-key')
    GERRIT_GITID = defaults.get('gerrit-committer')
    GERRIT_SYSTEM_USER = defaults.get('gerrit-system-user', 'gerrit2')
    GERRIT_SYSTEM_GROUP = defaults.get('gerrit-system-group', 'gerrit2')

    gerrit = gerritlib.gerrit.Gerrit('localhost', GERRIT_USER, GERRIT_PORT,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in configs[1]:
            project = section['project']
            options = section.get('options', dict())
            description = section.get('description', None)
            homepage = section.get('homepage', defaults.get('homepage', None))
            upstream = section.get('upstream', None)

            project_git = "%s.git" % project
            project_dir = os.path.join(LOCAL_GIT_DIR, project_git)

            if 'has-github' in options or default_has_github:
                create_github_project(defaults, options, project, description,
                                      homepage)

            remote_url = "ssh://localhost:%s/%s" % (GERRIT_PORT, project)
            if project not in project_list:
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    if upstream:
                        run_command(
                            "git clone %(upstream)s %(repo_path)s" %
                            dict(upstream=upstream, repo_path=repo_path))
                        git_command(repo_path, "fetch origin "
                                    "+refs/heads/*:refs/copy/heads/*",
                                    env=ssh_env)
                        push_string = "push %s +refs/copy/heads/*:refs/heads/*"
                    else:
                        run_command("git init %s" % repo_path)
                        with open(os.path.join(repo_path, ".gitreview"),
                                  'w') as gitreview:
                            gitreview.write("""[gerrit]
host=%s
port=%s
project=%s
""" % (GERRIT_HOST, GERRIT_PORT, project_git))
                        git_command(repo_path, "add .gitreview")
                        cmd = ("commit -a -m'Added .gitreview' --author='%s'" %
                               GERRIT_GITID)
                        git_command(repo_path, cmd)
                        push_string = "push --all %s"
                    gerrit.createProject(project)

                    if not os.path.exists(project_dir):
                        run_command("git --bare init %s" % project_dir)
                        run_command("chown -R %s:%s %s" %
                                    (GERRIT_SYSTEM_USER, GERRIT_SYSTEM_GROUP,
                                     project_dir))

                    git_command(repo_path,
                                push_string % remote_url,
                                env=ssh_env)
                    git_command(repo_path,
                                "push --tags %s" % remote_url,
                                env=ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)

            try:
                acl_config = section.get(
                    'acl-config', '%s.config' % os.path.join(ACL_DIR, project))
            except AttributeError:
                acl_config = None

            if acl_config:
                if not os.path.isfile(acl_config):
                    write_acl_config(project, ACL_DIR,
                                     section.get('acl-base', None),
                                     section.get('acl-append', []),
                                     section.get('acl-parameters', {}))
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    ret, _ = run_command_status("git init %s" % repo_path)
                    if ret != 0:
                        continue
                    if (fetch_config(project, remote_url, repo_path, ssh_env)
                            and copy_acl_config(project, repo_path, acl_config)
                            and create_groups_file(project, gerrit,
                                                   repo_path)):
                        push_acl_config(project, remote_url, repo_path,
                                        GERRIT_GITID, ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)
    finally:
        os.unlink(ssh_env['GIT_SSH'])
Exemplo n.º 13
0
def main():

    PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
                                   '/home/gerrit2/projects.yaml')
    configs = [config for config in yaml.load_all(open(PROJECTS_YAML))]
    defaults = configs[0][0]
    default_has_issues = defaults.get('has-issues', False)
    default_has_downloads = defaults.get('has-downloads', False)
    default_has_wiki = defaults.get('has-wiki', False)

    LOCAL_GIT_DIR = defaults.get('local-git-dir', '/var/lib/git')
    GERRIT_HOST = defaults.get('gerrit-host')
    GERRIT_USER = defaults.get('gerrit-user')
    GERRIT_KEY = defaults.get('gerrit-key')

    GITHUB_SECURE_CONFIG = defaults.get(
        'github-config',
        '/etc/github/github-projects.secure.config')

    secure_config = ConfigParser.ConfigParser()
    secure_config.read(GITHUB_SECURE_CONFIG)

    # Project creation doesn't work via oauth
    ghub = github.Github(secure_config.get("github", "username"),
                         secure_config.get("github", "password"))
    orgs = ghub.get_user().get_orgs()
    orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs))

    gerrit = gerritlib.gerrit.Gerrit('localhost',
                                     GERRIT_USER,
                                     29418,
                                     GERRIT_KEY)
    project_list = gerrit.listProjects()
    ssh_env = make_ssh_wrapper(GERRIT_USER, GERRIT_KEY)
    try:

        for section in configs[1]:
            project = section['project']
            options = section.get('options', dict())
            description = section.get('description', None)
            homepage = section.get('homepage', defaults.get('homepage', None))
            upstream = section.get('upstream', None)

            project_git = "%s.git" % project
            project_dir = os.path.join(LOCAL_GIT_DIR, project_git)

            # Find the project's repo
            project_split = project.split('/', 1)
            if len(project_split) > 1:
                repo_name = project_split[1]
            else:
                repo_name = project
            has_issues = 'has-issues' in options or default_has_issues
            has_downloads = 'has-downloads' in options or default_has_downloads
            has_wiki = 'has-wiki' in options or default_has_wiki
            try:
                org = orgs_dict[project_split[0].lower()]
            except KeyError:
                # We do not have control of this github org ignore the project.
                continue
            try:
                repo = org.get_repo(repo_name)
            except github.GithubException:
                repo = org.create_repo(repo_name,
                                       homepage=homepage,
                                       has_issues=has_issues,
                                       has_downloads=has_downloads,
                                       has_wiki=has_wiki)
            if description:
                repo.edit(repo_name, description=description)
            if homepage:
                repo.edit(repo_name, homepage=homepage)

            repo.edit(repo_name, has_issues=has_issues,
                      has_downloads=has_downloads,
                      has_wiki=has_wiki)

            if 'gerrit' not in [team.name for team in repo.get_teams()]:
                teams = org.get_teams()
                teams_dict = dict(zip([t.name.lower() for t in teams], teams))
                teams_dict['gerrit'].add_to_repos(repo)

            remote_url = "ssh://*****:*****@lists.openstack.org>'"
                        git_command(repo_path, cmd)
                    gerrit.createProject(project)

                    if not os.path.exists(project_dir):
                        run_command("git --bare init %s" % project_dir)
                        run_command("chown -R gerrit2:gerrit2 %s"
                                    % project_dir)

                    git_command(repo_path,
                                "push --all %s" % remote_url,
                                env=ssh_env)
                    git_command(repo_path,
                                "push --tags %s" % remote_url, env=ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)

            if 'acl_config' in section:
                tmpdir = tempfile.mkdtemp()
                try:
                    repo_path = os.path.join(tmpdir, 'repo')
                    ret, _ = run_command_status("git init %s" % repo_path)
                    if ret != 0:
                        continue
                    if (fetch_config(project,
                                     remote_url,
                                     repo_path,
                                     ssh_env) and
                        copy_acl_config(project, repo_path,
                                        section['acl_config']) and
                            create_groups_file(project, gerrit, repo_path)):
                        push_acl_config(project,
                                        remote_url,
                                        repo_path,
                                        ssh_env)
                finally:
                    run_command("rm -fr %s" % tmpdir)
    finally:
        os.unlink(ssh_env['GIT_SSH'])