Set up the gitlab instance so it can be used in OpenMetricsBaseCheck
762577
def _create_gitlab_prometheus_instance(self, instance, init_config):
        
        # Mapping from Prometheus metrics names to Datadog ones
        # For now it's a 1:1 mapping
        allowed_metrics = init_config.get('allowed_metrics')
        if allowed_metrics is None:
            raise CheckException("At least one metric must be whitelisted in `allowed_metrics`.")

        gitlab_instance = deepcopy(instance)
        # gitlab uses 'prometheus_endpoint' and not 'prometheus_url', so we have to rename the key
        gitlab_instance['prometheus_url'] = instance.get('prometheus_endpoint')

        gitlab_instance.update(
            {
                'namespace': 'gitlab',
                'metrics': allowed_metrics,
                # Defaults that were set when gitlab was based on PrometheusCheck
                'send_monotonic_counter': instance.get('send_monotonic_counter', False),
                'health_service_check': instance.get('health_service_check', False),
            }
        )

        return gitlab_instance
143323
def setup(self, quiet=False, bootstrap_server=False):
        """"""
        def lazy_initialize_ensime():
            if not self.ensime:
                called_by = inspect.stack()[4][3]
                self.log.debug(str(inspect.stack()))
                self.log.debug('setup(quiet=%s, bootstrap_server=%s) called by %s()',
                               quiet, bootstrap_server, called_by)

                installed = self.launcher.strategy.isinstalled()
                if not installed and not bootstrap_server:
                    if not quiet:
                        scala = self.launcher.config.get('scala-version')
                        msg = feedback["prompt_server_install"].format(scala_version=scala)
                        self.editor.raw_message(msg)
                    return False

                try:
                    self.ensime = self.launcher.launch()
                except InvalidJavaPathError:
                    self.editor.message('invalid_java')  # TODO: also disable plugin

            return bool(self.ensime)

        def ready_to_connect():
            if not self.ws and self.ensime.is_ready():
                self.connect_ensime_server()
            return True

        # True if ensime is up and connection is ok, otherwise False
        return self.running and lazy_initialize_ensime() and ready_to_connect()
207954
def main(reraise_exceptions=False, **kwargs):
    """"""
    exit_status = 0

    try:
        cli.main(**kwargs)
    except SoftLayer.SoftLayerAPIError as ex:
        if 'invalid api token' in ex.faultString.lower():
            print("Authentication Failed: To update your credentials, use 'slcli config setup'")
            exit_status = 1
        else:
            print(str(ex))
            exit_status = 1
    except SoftLayer.SoftLayerError as ex:
        print(str(ex))
        exit_status = 1
    except exceptions.CLIAbort as ex:
        print(str(ex.message))
        exit_status = ex.code
    except Exception:
        if reraise_exceptions:
            raise

        import traceback
        print("An unexpected error has occured:")
        print(str(traceback.format_exc()))
        print("Feel free to report this error as it is likely a bug:")
        print("    https://github.com/softlayer/softlayer-python/issues")
        print("The following snippet should be able to reproduce the error")
        exit_status = 1

    sys.exit(exit_status)
952363
public function gitlabInit() {
    $result = $this->taskFilesystemStack()
      ->copy($this->getConfigValue('blt.root') . '/scripts/gitlab/gitlab-ci.yml', $this->getConfigValue('repo.root') . '/.gitlab-ci.yml', TRUE)
      ->stopOnFail()
      ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
      ->run();
    if (!$result->wasSuccessful()) {
      throw new BltException("Could not initialize the GitLab Pipelines configuration.");
    }
    $this->say("<info>A pre-configured .gitlab-ci.yml file was copied to your repository root.</info>");
    $this->logger->warning("GitLab support is experimental and may not support all BLT features.");
  }
332163
def create(self):
        """"""
        # Point to chart repo.
        out = helm(
            "repo",
            "add",
            "jupyterhub",
            self.helm_repo
        )
        out = helm("repo", "update")

        # Get token to secure Jupyterhub
        secret_yaml = self.get_security_yaml()

        # Get Jupyterhub.
        out = helm(
            "upgrade",
            "--install",
            self.release,
            "jupyterhub/jupyterhub",
            namespace=self.namespace,
            version=self.version,
            input=secret_yaml
        )
        if out.returncode != 0:
            print(out.stderr)
        else:
            print(out.stdout)
266590
async def update_gitlab_loop(update_metrics, params):
    
    gitlab_api = gitlab.Gitlab(url=params['url'], private_token=params['token'], api_version=4)
    while True:
        try:
            metrics = update_gitlab_runners(gitlab_api)
            update_metrics(metrics)
        except Exception:
            update_metrics({})
            log.exception('During loop')
        await asyncio.sleep(LOOP)
205264
def gitlab_user_exists?(gitlab_url, private_token)
      with_gitlab(gitlab_url, private_token) do |g|
        g.user
      end

      true
    rescue Gitlab::Error::Unauthorized
      false
    end
519545
def _install(_):
    
    if not os.path.exists(".git"):
        fatal("not in a git repo.")

    config = __load_config()

    if config.get("checks") is None:
        fatal("global ``checks`` variable missing from configuration.")

    # We try to load all checks to check that they exist NOW.
    for hook in config["checks"]:
        checks = __load_checks(config["checks"][hook])
        # Also verify that they apply to the hooks.
        __verify_checks_for_hook(checks, hook)

    reply = input("This will overwrite your current hooks. "
                  "Proceed [yes/No]? ")
    if reply != "yes":
        raise Exit(0)

    for hook in config["checks"]:
        hook_f = open(os.path.join(".git/hooks/", hook), 'w')
        hook_f.write("""#!/usr/bin/env python
# GENRATED BY GLERBL. DO NOT MODIFY.
from glerbl.__main__ import main


exit(main(["glerbl", "hook", "{0}"]))
""".format(hook))
        os.fchmod(hook_f.fileno(),
                  os.fstat(hook_f.fileno()).st_mode | stat.S_IXUSR)
        hook_f.close()
1089050
def set_up(self):
        """"""
        self.path.profile = self.path.gen.joinpath("profile")

        if not self.path.profile.exists():
            self.path.profile.mkdir()

        self.python = hitchpylibrarytoolkit.project_build(
            "strictyaml",
            self.path,
            self.given["python version"],
            {"ruamel.yaml": self.given["ruamel version"]},
        ).bin.python

        self.example_py_code = (
            ExamplePythonCode(self.python, self.path.gen)
            .with_code(self.given.get("code", ""))
            .with_setup_code(
                self.given.get("setup", "")
            )
            .with_terminal_size(160, 100)
            .with_strings(
                yaml_snippet_1=self.given.get("yaml_snippet_1"),
                yaml_snippet=self.given.get("yaml_snippet"),
                yaml_snippet_2=self.given.get("yaml_snippet_2"),
                modified_yaml_snippet=self.given.get("modified_yaml_snippet"),
            )
        )
1249986
def _authenticate_https(org):
    """"""
    _CREDENTIAL_SOCKET.parent.mkdir(mode=0o700, exist_ok=True)
    try:
        Git.cache = f"-c credential.helper= -c credential.helper='cache --socket {_CREDENTIAL_SOCKET}'"
        git = Git(Git.cache)

        # Get credentials from cache if possible
        with _spawn(git("credential fill"), quiet=True) as child:
            child.sendline("protocol=https")
            child.sendline("host=github.com")
            child.sendline("")
            i = child.expect(["Username for '.+'", "Password for '.+'",
                              "username=([^\r]+)\r\npassword=([^\r]+)\r\n"])
            if i == 2:
                username, password = child.match.groups()
            else:
                username = password = None
                child.close()
                child.exitstatus = 0

        # No credentials found, need to ask user
        if password is None:
            username = _prompt_username(_("GitHub username: "))
            password = _prompt_password(_("GitHub password: "))

        # Check if credentials are correct
        res = requests.get("https://api.github.com/user", auth=(username, password))

        # Check for 2-factor authentication https://developer.github.com/v3/auth/#working-with-two-factor-authentication
        if "X-GitHub-OTP" in res.headers:
            raise Error("Looks like you have two-factor authentication enabled!"
                        " Please generate a personal access token and use it as your password."
                        " See https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line for more info.")

        if res.status_code != 200:
            logger.info(res.headers)
            logger.info(res.text)
            raise Error(_("Invalid username and/or password.") if res.status_code ==
                        401 else _("Could not authenticate user."))

        # Canonicalize (capitalization of) username,
        # Especially if user logged in via email address
        username = res.json()["login"]

        # Credentials are correct, best cache them
        with _spawn(git("-c credentialcache.ignoresighup=true credential approve"), quiet=True) as child:
            child.sendline("protocol=https")
            child.sendline("host=github.com")
            child.sendline(f"path={org}/{username}")
            child.sendline(f"username={username}")
            child.sendline(f"password={password}")
            child.sendline("")

        yield User(name=username,
                   repo=f"https://{username}@github.com/{org}/{username}")
    except BaseException:
        # Some error occured while this context manager is active, best forget credentials.
        logout()
        raise
1076087
def main():
    
    # see:
    # https://packaging.python.org/tutorials/distributing-packages/#uploading-your-project-to-pypi
    twine_repo = os.getenv('TWINE_REPOSITORY_URL') or os.getenv('TWINE_REPOSITORY')
    print('tagging and releasing to %s as %s' % (
        twine_repo,
        os.getenv('TWINE_USERNAME')
    ))

    if not twine_repo:
        raise Exception('cannot release to implicit pypi repository. explicitly set the repo/url.')

    version = subprocess.check_output(['pipenv', 'run', 'python', 'setup.py', '--version']).decode().strip()
    if 'dev' in version:
        raise Exception('cannot release unversioned project: %s' % version)

    print('Preparing environment')
    subprocess.check_call(['git', 'config', '--global', 'user.name', 'monty-bot'])
    subprocess.check_call(['git', 'config', '--global', 'user.email', 'monty-bot@arm.com'])
    url = subprocess.check_output(['git', 'remote', 'get-url', 'origin'])
    branch_name = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
    new_url = git_url_ssh_to_https(url.decode())
    subprocess.check_call(['git', 'remote', 'set-url', 'origin', new_url])
    branch_spec = 'origin/%s' % branch_name.decode('utf-8').strip()
    subprocess.check_call(['git', 'branch', '--set-upstream-to', branch_spec])
    print('Committing the changelog & version')
    subprocess.check_call(['git', 'add', 'src/auto_version/__version__.py'])
    subprocess.check_call(['git', 'add', 'CHANGELOG.md'])
    subprocess.check_call(['git', 'add', 'docs/news/*'])
    message = ':checkered_flag: :newspaper: Releasing version %s\n[skip ci]' % version
    subprocess.check_call(['git', 'commit', '-m', message])
    print('Tagging the project')
    subprocess.check_call(['git', 'tag', '-a', version, '-m', 'Release %s' % version])
    print('Pushing changes back to GitHub')
    subprocess.check_call(['git', 'push', '--follow-tags'])
    print('Marking this commit as latest')
    subprocess.check_call(['git', 'tag', '-f', 'latest'])
    subprocess.check_call(['git', 'push', '-f', '--tags'])
    print('Generating a release package')
    subprocess.check_call(
        ['pipenv', 'run', 'python', 'setup.py', 'clean', '--all', 'bdist_wheel', '--dist-dir', 'release-dist'])
    print('Uploading to PyPI')
    subprocess.check_call(['pipenv', 'run', 'python', '-m', 'twine', 'upload', 'release-dist/*'])
    print('Done.')
834249
def main():
    
    logging.basicConfig(
        format='[%(asctime)s] %(levelname)s -- %(message)s',
        level=logging.DEBUG)

    parser = argparse.ArgumentParser(description='')
    parser.add_argument('git_url', help='Url of the repo to sync')
    parser.add_argument('branch_name', default='master', help='Branch of repo to sync', nargs='?')
    parser.add_argument('repo_dir', default='.', help='Path to clone repo under', nargs='?')
    args = parser.parse_args()

    for line in GitPuller(
        args.git_url,
        args.branch_name,
        args.repo_dir
    ).pull():
        print(line)
712753
def deploy_sandbox_shared_setup(verbose=True, app=None, web_procs=1):
    """"""
    if verbose:
        out = None
    else:
        out = open(os.devnull, 'w')

    (id, tmp) = setup_experiment(debug=False, verbose=verbose, app=app)

    # Log in to Heroku if we aren't already.
    log("Making sure that you are logged in to Heroku.")
    ensure_heroku_logged_in()

    # Change to temporary directory.
    cwd = os.getcwd()
    os.chdir(tmp)

    # Commit Heroku-specific files to tmp folder's git repo.
    cmds = ["git init",
            "git add --all",
            'git commit -m "Experiment ' + id + '"']
    for cmd in cmds:
        subprocess.call(cmd, stdout=out, shell=True)
        time.sleep(0.5)

    # Load psiTurk configuration.
    config = PsiturkConfig()
    config.load_config()

    # Initialize the app on Heroku.
    log("Initializing app on Heroku...")
    subprocess.call(
        "heroku apps:create " + id +
        " --buildpack https://github.com/thenovices/heroku-buildpack-scipy",
        stdout=out,
        shell=True)

    database_size = config.get('Database Parameters', 'database_size')

    try:
        if config.getboolean('Easter eggs', 'whimsical'):
            whimsical = "true"
        else:
            whimsical = "false"
    except:
        whimsical = "false"

    # Set up postgres database and AWS/psiTurk environment variables.
    cmds = [
        "heroku addons:create heroku-postgresql:{}".format(database_size),

        "heroku pg:wait",

        "heroku addons:create rediscloud:250",

        "heroku addons:create papertrail",

        "heroku config:set HOST=" +
        id + ".herokuapp.com",

        "heroku config:set aws_access_key_id=" +
        config.get('AWS Access', 'aws_access_key_id'),

        "heroku config:set aws_secret_access_key=" +
        config.get('AWS Access', 'aws_secret_access_key'),

        "heroku config:set aws_region=" +
        config.get('AWS Access', 'aws_region'),

        "heroku config:set psiturk_access_key_id=" +
        config.get('psiTurk Access', 'psiturk_access_key_id'),

        "heroku config:set psiturk_secret_access_id=" +
        config.get('psiTurk Access', 'psiturk_secret_access_id'),

        "heroku config:set auto_recruit=" +
        config.get('Experiment Configuration', 'auto_recruit'),

        "heroku config:set wallace_email_username=" +
        config.get('Email Access', 'wallace_email_address'),

        "heroku config:set wallace_email_key=" +
        config.get('Email Access', 'wallace_email_password'),

        "heroku config:set heroku_email_address=" +
        config.get('Heroku Access', 'heroku_email_address'),

        "heroku config:set heroku_password=" +
        config.get('Heroku Access', 'heroku_password'),

        "heroku config:set whimsical=" + whimsical,
    ]
    for cmd in cmds:
        subprocess.call(cmd + " --app " + id, stdout=out, shell=True)

    # Set the notification URL in the cofig file to the notifications URL.
    config.set(
        "Server Parameters",
        "notification_url",
        "http://" + id + ".herokuapp.com/notifications")

    # Set the database URL in the config file to the newly generated one.
    log("Saving the URL of the postgres database...")
    db_url = subprocess.check_output(
        "heroku config:get DATABASE_URL --app " + id, shell=True)
    config.set("Database Parameters", "database_url", db_url.rstrip())
    subprocess.call("git add config.txt", stdout=out, shell=True),
    time.sleep(0.25)
    subprocess.call(
        'git commit -m "Save URLs for database and notifications"',
        stdout=out,
        shell=True)
    time.sleep(0.25)

    # Launch the Heroku app.
    log("Pushing code to Heroku...")
    subprocess.call("git push heroku HEAD:master", stdout=out,
                    stderr=out, shell=True)

    scale_up_dynos(id)

    time.sleep(8)

    # Launch the experiment.
    log("Launching the experiment on MTurk...")
    subprocess.call(
        'curl --data "" http://{}.herokuapp.com/launch'.format(id),
        shell=True)

    time.sleep(8)

    url = subprocess.check_output("heroku logs --app " + id + " | sort | " +
                                  "sed -n 's|.*URL:||p'", shell=True)

    log("URLs:")
    click.echo(url)

    # Return to the branch whence we came.
    os.chdir(cwd)

    log("Completed deployment of experiment " + id + ".")
522238
def assert_in_setup_repo(setup_fpath, name=''):
    
    setup_dir, setup_fname = split(setup_fpath)
    cwd = os.getcwd()
    #repo_dname = split(setup_dir)[1]
    #print('cwd       = %r' % (cwd))
    #print('repo_dname = %r' % repo_dname)
    #print('setup_dir = %r' % (setup_dir))
    #print('setup_fname = %r' % (setup_fname))
    try:
        assert setup_fname == 'setup.py', 'name is not setup.py'
        #assert name == '' or repo_dname == name,
        ('name=%r' % name)
        assert cwd == setup_dir, 'cwd is not setup_dir'
        assert exists(setup_dir), 'setup dir does not exist'
        assert exists(join(setup_dir, 'setup.py')), 'setup.py does not exist'
    except AssertionError as ex:
        printex(ex, 'ERROR!: setup.py must be run from repository root')
        raise
458268
private GitLabApiForm createGitLabApiForm() {
        GitLabApiForm formData = new GitLabApiForm();
        return (customAttributesEnabled ? formData.withParam("with_custom_attributes", true) : formData);
    }
1120557
def main(argv=None):
    
    @contextmanager
    def tmpdir():
        """ Create a self-deleting temporary directory. """
        path = mkdtemp()
        try:
            yield path
        finally:
            rmtree(path)
        return

    def test():
        """ Execute the test suite. """
        install = "{:s} install -r requirements-test.txt"
        check_call(install.format(pip).split())
        pytest = join(path, "bin", "py.test")
        test = "{:s} test/".format(pytest)
        check_call(test.split())
        uninstall = "{:s} uninstall -y -r requirements-test.txt"
        check_call(uninstall.format(pip).split())
        return

    args = _cmdline(argv)
    path = join(abspath(args.root), args.name)
    with tmpdir() as tmp:
        clone = "git clone {:s} {:s}".format(args.repo, tmp)
        check_call(clone.split())
        chdir(tmp)
        checkout = "git checkout {:s}".format(args.checkout)
        check_call(checkout.split())
        virtualenv = "virtualenv {:s}".format(path)
        check_call(virtualenv.split())
        pip = join(path, "bin", "pip")
        install = "{:s} install -U -r requirements.txt .".format(pip)
        check_call(install.split())
        if args.test:
            test()
    return 0
1079756
def with_gitlab(gitlab_url = options[:endpoint], private_token = options[:api_token])
      yield Gitlab.client(endpoint:      gitlab_endpoint(gitlab_url),
                          private_token: private_token,
                          user_agent:    'Errbit User Agent')
    end