Example #1
0
def update_zuul():
    zuul_units = []

    for rid in relation_ids('zuul-configurator'):
        [zuul_units.append(u) for u in related_units(rid)]

    if not zuul_units:
        log('*** No related zuul units, skipping config.')
        return

    log("*** Updating zuul.")
    layout_path = '/etc/zuul/layout.yaml'

    if not os.path.isdir(ZUUL_CONFIG_DIR):
        log('Could not find zuul config directory at expected location, '
            'skipping zuul update (%s)' % ZUUL_CONFIG_DIR)
        return

    log('Installing layout from %s to %s.' % (ZUUL_CONFIG_DIR, layout_path))
    common.sync_dir(ZUUL_CONFIG_DIR, layout_path)

    stop_zuul()
    start_zuul()

    return True
Example #2
0
def update_theme(theme_dest, static_dest):
    if not os.path.isdir(THEME_DIR):
        log('Gerrit theme directory not found @ %s, skipping theme refresh.' %
            THEME_DIR, level=WARNING)
        return False

    theme_orig = os.path.join(THEME_DIR, 'files')
    static_orig = os.path.join(THEME_DIR, 'static')

    if False in [os.path.isdir(theme_orig), os.path.isdir(static_orig)]:
        log('Theme directory @ %s missing required subdirs: files, static. '
            'Skipping theme refresh.' % THEME_DIR, level=WARNING)
        return False

    log('Installing theme from %s to %s.' % (theme_orig, theme_dest))
    common.sync_dir(theme_orig, theme_dest)
    log('Installing static files from %s to %s.' % (theme_orig, theme_dest))
    common.sync_dir(static_orig, static_dest)

    return True
Example #3
0
def update_hooks(hooks_dest, settings):
    if not os.path.isdir(HOOKS_DIR):
        log('Gerrit hooks directory not found @ %s, skipping hooks refresh.' %
            HOOKS_DIR, level=WARNING)
        return False

    log('Installing gerrit hooks in %s to %s.' % (HOOKS_DIR, hooks_dest))
    common.sync_dir(HOOKS_DIR, hooks_dest)

    #  hook allow tags like {{var}}, so replace all entries in file
    for filename in os.listdir(hooks_dest):
        current_path = os.path.join(hooks_dest, filename)
        if os.path.isfile(current_path):
            with open(current_path, 'r') as f:
                contents = f.read()
            for key, value in settings.items():
                pattern = '{{%s}}' % (key)
                contents = contents.replace(pattern, value)
            with open(current_path, 'w') as f:
                f.write(contents)

    return True
Example #4
0
def update_theme(theme_dest, static_dest):
    if not os.path.isdir(THEME_DIR):
        log('Gerrit theme directory not found @ %s, skipping theme refresh.' %
            THEME_DIR,
            level=WARNING)
        return False

    theme_orig = os.path.join(THEME_DIR, 'files')
    static_orig = os.path.join(THEME_DIR, 'static')

    if False in [os.path.isdir(theme_orig), os.path.isdir(static_orig)]:
        log('Theme directory @ %s missing required subdirs: files, static. '
            'Skipping theme refresh.' % THEME_DIR,
            level=WARNING)
        return False

    log('Installing theme from %s to %s.' % (theme_orig, theme_dest))
    common.sync_dir(theme_orig, theme_dest)
    log('Installing static files from %s to %s.' % (theme_orig, theme_dest))
    common.sync_dir(static_orig, static_dest)

    return True
Example #5
0
def update_hooks(hooks_dest, settings):
    if not os.path.isdir(HOOKS_DIR):
        log('Gerrit hooks directory not found @ %s, skipping hooks refresh.' %
            HOOKS_DIR,
            level=WARNING)
        return False

    log('Installing gerrit hooks in %s to %s.' % (HOOKS_DIR, hooks_dest))
    common.sync_dir(HOOKS_DIR, hooks_dest)

    #  hook allow tags like {{var}}, so replace all entries in file
    for filename in os.listdir(hooks_dest):
        current_path = os.path.join(hooks_dest, filename)
        if os.path.isfile(current_path):
            with open(current_path, 'r') as f:
                contents = f.read()
            for key, value in settings.items():
                pattern = '{{%s}}' % (key)
                contents = contents.replace(pattern, value)
            with open(current_path, 'w') as f:
                f.write(contents)

    return True
Example #6
0
def update_permissions(admin_username, admin_email, admin_privkey):
    if not os.path.isdir(PERMISSIONS_DIR):
        log('Gerrit permissions directory not found @ %s, skipping '
            'permissions refresh.' % PERMISSIONS_DIR, level=WARNING)
        return False

    # create launchpad directory and setup permissions
    if not os.path.isdir(LAUNCHPAD_DIR):
        os.mkdir(LAUNCHPAD_DIR)
        cmd = ['chown', "%s:%s" % (GERRIT_USER, GERRIT_USER), LAUNCHPAD_DIR]
        subprocess.check_call(cmd)
        os.chmod(LAUNCHPAD_DIR, 0774)

    # check if we have creds, push to dir
    if config('lp-credentials-file'):
        creds = b64decode(config('lp-credentials-file'))
        with open(os.path.join(LAUNCHPAD_DIR, 'creds'), 'w') as f:
            f.write(creds)

    # if we have teams and schedule, update cronjob
    if config('lp-schedule'):
        command = ('%s %s %s > %s 2>&1' %
                   (os.path.join(os.environ['CHARM_DIR'], 'scripts',
                    'query_lp_members.py'), admin_username, admin_privkey,
                    LOGS_PATH+'/launchpad_sync.log'))
        cron.schedule_generic_job(
            config('lp-schedule'), 'root', 'launchpad_sync', command)

    repo_name = 'All-Projects.git'
    repo_url = ('ssh://%s@localhost:%s/%s' % (admin_username, SSH_PORT,
                                              repo_name))

    # parse groups file and create groups
    gerrit_client = GerritClient(host='localhost', user=admin_username,
                                 port=SSH_PORT, key_file=admin_privkey)

    with open(GROUPS_CONFIG_FILE, 'r') as f:
        groups_config = yaml.load(f)

    # Create group(s)
    for group, _ in groups_config.items():
        gerrit_client.create_group(group)

    # Update git repo with permissions
    log('Installing gerrit permissions from %s.' % PERMISSIONS_DIR)
    try:
        tmppath = tempfile.mkdtemp('', 'gerritperms')
        if tmppath:
            cmd = ["chown", "%s:%s" % (GERRIT_USER, GERRIT_USER), tmppath]
            subprocess.check_call(cmd)
            os.chmod(tmppath, 0774)

            config_ref = 'refs/meta/config:refs/remotes/origin/meta/config'

            for cmd in [['git', 'init'],
                        ['git', 'remote', 'add', 'repo', repo_url],
                        ['git', 'fetch', 'repo', config_ref],
                        ['git', 'checkout', 'meta/config']]:
                common.run_as_user(user=GERRIT_USER, cmd=cmd, cwd=tmppath)

            common.sync_dir(os.path.join(PERMISSIONS_DIR, 'All-Projects'),
                            tmppath)

            # Only proceed if the repo has NOT been successfully initialised.
            if is_permissions_initialised(repo_name, tmppath):
                log("%s is already initialised - skipping update permissions" %
                    (repo_name), level=INFO)
                return False

            try:
                setup_gerrit_groups(tmppath, admin_username, admin_email)
            except GerritConfigurationException as exc:
                log(str(exc), level=ERROR)
                return False
        else:
            log('Failed to create permissions temporary directory',
                level=ERROR)
            return False
    except Exception as e:
        log('Failed to create permissions: %s' % str(e), level=ERROR)

    return True
Example #7
0
def update_permissions(admin_username, admin_email, admin_privkey):
    if not os.path.isdir(PERMISSIONS_DIR):
        log('Gerrit permissions directory not found @ %s, skipping '
            'permissions refresh.' % PERMISSIONS_DIR,
            level=WARNING)
        return False

    # create launchpad directory and setup permissions
    if not os.path.isdir(LAUNCHPAD_DIR):
        os.mkdir(LAUNCHPAD_DIR)
        cmd = ['chown', "%s:%s" % (GERRIT_USER, GERRIT_USER), LAUNCHPAD_DIR]
        subprocess.check_call(cmd)
        os.chmod(LAUNCHPAD_DIR, 0774)

    # check if we have creds, push to dir
    if config('lp-credentials-file'):
        creds = b64decode(config('lp-credentials-file'))
        with open(os.path.join(LAUNCHPAD_DIR, 'creds'), 'w') as f:
            f.write(creds)

    # if we have teams and schedule, update cronjob
    if config('lp-schedule'):
        command = ('%s %s %s > %s 2>&1' %
                   (os.path.join(os.environ['CHARM_DIR'], 'scripts',
                                 'query_lp_members.py'), admin_username,
                    admin_privkey, LOGS_PATH + '/launchpad_sync.log'))
        cron.schedule_generic_job(config('lp-schedule'), 'root',
                                  'launchpad_sync', command)

    repo_name = 'All-Projects.git'
    repo_url = ('ssh://%s@localhost:%s/%s' %
                (admin_username, SSH_PORT, repo_name))

    # parse groups file and create groups
    gerrit_client = GerritClient(host='localhost',
                                 user=admin_username,
                                 port=SSH_PORT,
                                 key_file=admin_privkey)

    with open(GROUPS_CONFIG_FILE, 'r') as f:
        groups_config = yaml.load(f)

    # Create group(s)
    for group, _ in groups_config.items():
        gerrit_client.create_group(group)

    # Update git repo with permissions
    log('Installing gerrit permissions from %s.' % PERMISSIONS_DIR)
    try:
        tmppath = tempfile.mkdtemp('', 'gerritperms')
        if tmppath:
            cmd = ["chown", "%s:%s" % (GERRIT_USER, GERRIT_USER), tmppath]
            subprocess.check_call(cmd)
            os.chmod(tmppath, 0774)

            config_ref = 'refs/meta/config:refs/remotes/origin/meta/config'

            for cmd in [['git', 'init'],
                        ['git', 'remote', 'add', 'repo', repo_url],
                        ['git', 'fetch', 'repo', config_ref],
                        ['git', 'checkout', 'meta/config']]:
                common.run_as_user(user=GERRIT_USER, cmd=cmd, cwd=tmppath)

            common.sync_dir(os.path.join(PERMISSIONS_DIR, 'All-Projects'),
                            tmppath)

            # Only proceed if the repo has NOT been successfully initialised.
            if is_permissions_initialised(repo_name, tmppath):
                log("%s is already initialised - skipping update permissions" %
                    (repo_name),
                    level=INFO)
                return False

            try:
                setup_gerrit_groups(tmppath, admin_username, admin_email)
            except GerritConfigurationException as exc:
                log(str(exc), level=ERROR)
                return False
        else:
            log('Failed to create permissions temporary directory',
                level=ERROR)
            return False
    except Exception as e:
        log('Failed to create permissions: %s' % str(e), level=ERROR)

    return True