def init_gitlab():
    # type: ()->gitlab.Gitlab
    config_path = __find_gitlab_connection_config_file()
    config = (gitlab.config.GitlabConfigParser(gitlab_id="global",
                                               config_files=[config_path])
              if config_path is not None else None)

    token = getattr(config, "private_token",
                    None) or uos.get_env_or_else(GITLAB_CLIENT_TOKEN)
    if token is None:
        raise ClientInitializationException(
            "GitLab token was not provided. It must be defined in {0} environment variable or config file"
            .format(GITLAB_CLIENT_TOKEN))

    url = getattr(config, "url", None) or uos.get_env_or_else(
        GITLAB_CLIENT_URL, "https://gitlab.com")
    # ssl_verify is always set by GitlabConfigParser, using inline if to handle `false` value read from config file
    ssl_verify = (config.ssl_verify if config else uos.get_env_or_else(
        GITLAB_CLIENT_SSL_VERIFY, True))
    api_version = getattr(config, "api_version", None) or uos.get_env_or_else(
        GITLAB_CLIENT_API_VERSION, "4")

    return gitlab.Gitlab(url=url,
                         private_token=token,
                         ssl_verify=ssl_verify,
                         api_version=api_version)
 def __init__(self, configurers=None):
     # type: ()->GitlabConfigurationAsCode
     self.mode = Mode[uos.get_env_or_else(GITLAB_MODE, Mode.APPLY.name)]
     if self.mode == Mode.TEST:
         logger.info("TEST MODE ENABLED. NO CHANGES WILL BE APPLIED")
     path = uos.get_env_or_else(GITLAB_CONFIG_FILE,
                                "{0}/gitlab.yml".format(os.getcwd()))
     self.gitlab = init_gitlab_client()
     self.config = GitlabConfiguration.from_file(path)
     self.configurers = (configurers
                         if configurers else create_all_configurers(
                             self.gitlab, self.config, self.mode))
     version, revision = self.gitlab.version()
     logger.info("GitLab version: %s, revision: %s", version, revision)
def init_gitlab_from_env():
    # type: ()->gitlab.Gitlab
    token = uos.get_env_or_else(GITLAB_CLIENT_TOKEN)
    if token is None:
        raise ClientInitializationError(
            "GitLab token was not provided. It must be defined in {0} environment variable"
            .format(GITLAB_CLIENT_TOKEN))

    url = uos.get_env_or_else(GITLAB_CLIENT_URL, "https://gitlab.com")
    ssl_verify = uos.get_env_or_else(GITLAB_CLIENT_SSL_VERIFY, True)
    api_version = uos.get_env_or_else(GITLAB_CLIENT_API_VERSION, "4")

    return gitlab.Gitlab(url=url,
                         private_token=token,
                         ssl_verify=ssl_verify,
                         api_version=api_version)
    def __init__(self):
        # type: ()->GitlabConfigurationAsCode
        self.mode = Mode[uos.get_env_or_else(GITLAB_MODE, Mode.APPLY.name)]
        if self.mode == Mode.TEST:
            logger.info("TEST MODE ENABLED. NO CHANGES WILL BE APPLIED")
        path = uos.get_env_or_else(GITLAB_CONFIG_FILE,
                                   "{0}/gitlab.yml".format(os.getcwd()))
        self.gitlab = init_gitlab_client()
        self.configurers = {}
        self.config = GitlabConfiguration.from_file(path)
        self.configurers["settings"] = SettingsConfigurer(
            self.gitlab, self.config.settings, self.mode)
        self.configurers["license"] = LicenseConfigurer(
            self.gitlab, self.config.license, self.mode)

        version, revision = self.gitlab.version()
        logger.info("GitLab version: %s, revision: %s", version, revision)
示例#5
0
def __init_session(gitlab):
    # type: (gitlab.Gitlab)->()
    certificate = uos.get_env_or_else(GITLAB_CLIENT_CERTIFICATE)
    key = uos.get_env_or_else(GITLAB_CLIENT_KEY)
    if certificate is None and key is None:
        return
    elif certificate is None and key is not None:
        pass
    elif certificate is not None and key is None:
        pass
    else:
        check_config = __check_file_exists(
            certificate, "Client Certificate"
        ) and __check_file_exists(key, "Client Key")
        if not check_config:
            raise ClientInitializationError(
                "GitLab client authentication env vars were provided, but point to incorrect file(s)"
            )
        session = requests.Session()
        session.cert = (certificate, key)
        gitlab.session = session
示例#6
0
def __find_gitlab_connection_config_file():
    # type: ()->str
    config_path = uos.get_env_or_else(GITLAB_CLIENT_CONFIG_FILE)
    if config_path is not None:
        if not __check_file_exists(config_path, "GitLab Client"):
            logger.error(
                "Configuration file was not found under path %s, which was defined in %s env variable."
                "Provide path to existing file or remove this variable and configure client"
                "using environment variables instead of configuration file",
                config_path,
                GITLAB_CLIENT_CONFIG_FILE,
            )
            return None
    else:
        for path in GITLAB_CONFIG_FILE_DEFAULT_PATHS:
            if __check_file_exists(path, "GitLab Client"):
                config_path = path
                break
    return config_path
示例#7
0
import os

import yaml

from gcasc import GcascException
from gcasc.utils import os as uos
from gcasc.utils.yaml_include import YamlIncluderConstructor

# includer relative to current path from which script is executed
YamlIncluderConstructor.add_to_loader_class(
    loader_class=yaml.FullLoader, base_dir=os.path.dirname(os.path.realpath(__file__))
)

# includer relative to GitLab configuration file path
config_path = uos.get_env_or_else(
    ["GITLAB_CONFIG_FILE", "GITLAB_CONFIG_PATH"], "{0}/x".format(os.getcwd())
).split("/")
del config_path[-1]
YamlIncluderConstructor.add_to_loader_class(
    loader_class=yaml.FullLoader, base_dir="/".join(config_path)
)


class GitlabConfiguration(object):
    def __init__(self, config):
        # type: (dict)->GitlabConfiguration
        if config is None:
            raise GcascException("GitLab configuration is empty")

        if not isinstance(config, dict):
            raise GcascException("Configuration provided must be of dictionary type")