Exemplo n.º 1
0
    def __init__(self, endpoint=None):
        if not endpoint:
            cc = tell_cluster_config()
            endpoint = cc.get('alertmanager')
            if not endpoint:
                raise click.Abort(
                    f'alertmanager not provided in cluster config: {cc}')

        self.endpoint = endpoint.rstrip('/')
Exemplo n.º 2
0
    def __init__(self):
        cc = tell_cluster_config()
        kibana_host = cc.get('kibana')
        if not kibana_host:
            error('kibana not configured for this cluster', exit=1)

        self.endpoint = f'http://{kibana_host}'
        self.timeout_ms = self.timeout * 1000
        self.headers = {
            'kbn-xsrf': 'true',
        }
Exemplo n.º 3
0
def tell_scm():
    cc = tell_cluster_config()
    endpoint = cc.get('gitlab')
    if not endpoint:
        error('gitlab not configured in cluster config', exit=1)

    token = must_get_env(
        'GITLAB_API_TOKEN',
        f'get your own token at {endpoint}/-/profile/personal_access_tokens',
    )
    return GitLabSCM(endpoint, token)
Exemplo n.º 4
0
 def query_cpu(self, appname, proc_name, **kwargs):
     cc = tell_cluster_config()
     query_template = cc.get('pql_template', {}).get('cpu')
     if not query_template:
         raise ValueError(
             'pql_template.cpu not configured in cluster config')
     q = query_template.format(appname=appname,
                               proc_name=proc_name,
                               range=self.query_range)
     kwargs.setdefault('step', self.query_step)
     kwargs['end'] = datetime.now(timezone.utc)
     res = self.query(q, **kwargs)
     return res
Exemplo n.º 5
0
    def __init__(self, endpoint=None):
        if not endpoint:
            cc = tell_cluster_config()
            endpoint = cc.get('prometheus')
            if not endpoint:
                raise click.Abort(
                    f'prometheus not provided in cluster config: {cc}')

        ctx = context(silent=True)
        self.query_range = (ctx.obj.get('values', {}).get(
            'prometheus_query_range', '7d') if ctx else '7d')
        self.query_step = int(int(parse_timespan(self.query_range)) / 1440)
        self.endpoint = endpoint
Exemplo n.º 6
0
 def memory_quantile(self, appname, proc_name, **kwargs):
     cc = tell_cluster_config()
     query_template = cc.get('pql_template', {}).get('memory_quantile')
     if not query_template:
         raise ValueError(
             'pql_template.memory_quantile not configured in cluster config'
         )
     q = query_template.format(appname=appname,
                               proc_name=proc_name,
                               range=self.query_range)
     kwargs.setdefault('step', self.query_step)
     res = self.query(q, **kwargs)
     if not res:
         return
     # [{'metric': {}, 'value': [1583388354.31, '744079360']}]
     memory_quantile = int(float(res[0]['value'][-1]))
     return memory_quantile
Exemplo n.º 7
0
    def __init__(self, registry=None, **kwargs):
        if not registry:
            cc = tell_cluster_config()
            registry = cc['registry']

        self.registry = registry
        if '/' in registry:
            host, self.namespace = registry.split('/')
        else:
            host = registry
            self.namespace = ''

        if host == 'docker.io':
            api_host = 'index.docker.io'
            self.endpoint = f'http://{api_host}'
        else:
            self.endpoint = f'http://{registry}'

        self.dockerhub_password = kwargs.get('dockerhub_password')
        self.dockerhub_username = kwargs.get('dockerhub_username')
Exemplo n.º 8
0
    def __init__(self, registry=None, harbor_token=None, **kwargs):
        if not all([registry, harbor_token]):
            cc = tell_cluster_config()
            registry = cc['registry']
            if 'harbor_token' not in cc:
                raise ValueError('harbor_token not provided in cluster config')
            harbor_token = cc['harbor_token']

        self.registry = registry
        try:
            host, project = registry.split('/')
        except ValueError as e:
            raise ValueError(f'bad registry: {registry}') from e
        self.endpoint = f'http://{host}/api/v2.0'
        self.headers = {
            # get from your harbor console
            'authorization': f'Basic {harbor_token}',
            'accept': 'application/json',
        }
        self.project = project
Exemplo n.º 9
0
    def __init__(self,
                 registry=None,
                 access_key_id=None,
                 access_key_secret=None,
                 **kwargs):
        if not all([registry, access_key_id, access_key_secret]):
            cc = tell_cluster_config()
            registry = cc['registry']
            access_key_id = cc.get('access_key_id')
            access_key_secret = cc.get('access_key_secret')

        self.registry = registry
        self.repo_namespace = registry.split('/')[-1]
        if not all([access_key_id, access_key_secret]):
            raise ValueError(
                'access_key_id, access_key_secret not provided in cluster config'
            )

        self.cred = credential.Credential(access_key_id, access_key_secret)
        self.cvm_client = cvm_client.CvmClient(self.cred, "ap-beijing")
        self.tcr_client = TcrClient(self.cred, "ap-beijing")
Exemplo n.º 10
0
    def turn_(self, InstanceIds=None, cluster=None, state: str = 'on'):
        if not InstanceIds:
            InstanceIds = tell_cluster_config(cluster).get('instance_ids')

        if not InstanceIds:
            warn('instance_ids not defined in cluster info, cannot proceed',
                 exit=1)

        for id_ in InstanceIds:
            ids = [id_]
            if state.lower() == 'off':
                req = cvm_models.StopInstancesRequest()
                req.InstanceIds = ids
                req.ForceStop = True
                req.StoppedMode = 'STOP_CHARGING'
                try:
                    self.cvm_client.StopInstances(req)
                except TencentCloudSDKException as e:
                    if e.code == 'UnauthorizedOperation':
                        error(f'weird error: {e.code}', exit=True)
                    if e.code != 'InvalidInstanceState.Stopped':
                        debug(f'retry due to {e.code}')
                        raise
            elif state.lower() == 'on':
                req = cvm_models.StartInstancesRequest()
                req.InstanceIds = ids
                try:
                    self.cvm_client.StartInstances(req)
                except TencentCloudSDKException as e:
                    if e.code not in {
                            'InvalidInstanceState.Running',
                            'UnsupportedOperation.InstanceStateRunning',
                    }:
                        debug(f'retry due to {e.code}')
                        raise
            else:
                error(f'weird state {state}, choose from {self.VM_STATES}',
                      exit=True)
Exemplo n.º 11
0
    def __init__(
        self,
        access_key_id=None,
        access_key_secret=None,
        registry=None,
        **kwargs,
    ):
        if not all([access_key_id, access_key_secret, registry]):
            cc = tell_cluster_config()
            access_key_id = cc.get('access_key_id')
            access_key_secret = cc.get('access_key_secret')
            if not all([access_key_id, access_key_secret]):
                raise ValueError(
                    'access_key_id, access_key_secret not provided in cluster config'
                )
            if not registry:
                registry = cc['registry']

        _, region_id, _, _, repo_namespace = re.split(r'[\./]', registry)
        self.registry = f'registry.{region_id}.aliyuncs.com/{repo_namespace}'
        self.acs_client = AcsClient(access_key_id, access_key_secret,
                                    region_id)
        self.repo_namespace = repo_namespace
        self.endpoint = f'cr.{region_id}.aliyuncs.com'
Exemplo n.º 12
0
    res = runner.invoke(*args, obj=obj, env=env, **kwargs)
    if returncode is not None:
        real_code = rc(res)
        if real_code != returncode:
            print(res.output)
            traceback.print_exception(*res.exc_info)

        assert real_code == returncode

    return res


run(lain, args=['use', TEST_CLUSTER])

with click.Context(click.Command('lain'), obj={}):
    TEST_CLUSTER_CONFIG = tell_cluster_config(TEST_CLUSTER)

DUMMY_URL = f'http://{DUMMY_APPNAME}.{TEST_CLUSTER_CONFIG["domain"]}'
DUMMY_URL_HTTPS = f'https://{DUMMY_APPNAME}.{TEST_CLUSTER_CONFIG["domain"]}'
# this url will point to proc.web-dev in example_lain_yaml
DUMMY_DEV_URL = f'http://{DUMMY_APPNAME}-dev.{TEST_CLUSTER_CONFIG["domain"]}'
RANDOM_STRING = ''.join([choice(ascii_letters) for n in range(9)])
BUILD_TREASURE_NAME = 'treasure.txt'
DUMMY_JOBS_CLAUSE = {
    'init': {
        'initContainers': [{
            'name': f'{DUMMY_APPNAME}-init-container',
            'command': ['echo', RANDOM_STRING],
        }],
        'imagePullPolicy':
        'Always',