def set_config_task(self, task_config): if not isinstance(task_config, dict): raise exceptions.KuberlabClientException( 'App task config must be dict, not %s.' % task_config.__class__.__name__ ) name = task_config.get('name') if not name: raise exceptions.KuberlabClientException( 'App task config name required, missing "name" key.' ) # Check if task exists try: self.get_config_task(name) except exceptions.KuberlabClientException: # Task doesn't exist. Add a new task. self.config['spec']['tasks'].append(task_config) else: # Replace task. tasks = self.config['spec']['tasks'] self.config['spec']['tasks'] = [ task_config if x['name'] == name else x for x in tasks ] return self.update_with_config(self.config)
def get_config_task(self, name): tasks = self.get_config_tasks() for t in tasks: if t.name == name: return t raise exceptions.KuberlabClientException( 'App task [name=%s] not found.' % name )
def take_action(self, args): klab_client = self.app.client statuses = klab_client.apps.status(args.workspace, args.app) for status in statuses.component_states: if status.get('name') == args.name: return format_status(status, lister=False) raise exceptions.KuberlabClientException( 'Component "%s" not found in status list.' % args.name)
def take_action(self, args): klab_client = self.app.client storage = klab_client.storage.list(args.workspace, args.cluster_id) for st in storage: if st.Name == args.name: return format_storage(st) raise exceptions.KuberlabClientException( 'Storage "%s" not found in storage list.' % args.name)
def update_with_config(self, config): if not isinstance(config, dict): raise exceptions.KuberlabClientException( 'App config must be dict, not %s.' % config.__class__.__name__ ) self.full_config['Configuration'] = config return self.manager.update( self.WorkspaceName, self.Name, self.full_config )
def take_action(self, args): klab_client = self.app.client packages = klab_client.apps.packages_list(args.workspace, args.app, args.all) for package in packages: if package.manager == args.manager: return format_package(package, lister=False) raise exceptions.KuberlabClientException( 'Manager "%s" not found in package manager list.' % args.manager)
def create_session(base_url=_DEFAULT_KUBERLAB_URL, **kwargs): """Creates a new session for kuberlab client. :param base_url: kuberlab API base url. :param username: username :param password: password :param token: API token created via API :return: request.Session object. """ username = kwargs.get('username') password = kwargs.get('password') token = kwargs.get('token') ses = requests.Session() insecure = kwargs.pop('insecure') if insecure is not None: ses.verify = not bool(insecure) if token: ses.headers['Authorization'] = 'Bearer %s' % token return ses elif username and password: auth_url = '%s/auth/login' % base_url resp = ses.post( auth_url, json={ 'LoginOrEmail': username, 'Password': password }, headers={'Content-Type': 'application/json'}, verify=False, ) if resp.status_code != 200: raise exceptions.KuberlabClientException('Invalid auth: %s.' % resp.content) return ses raise exceptions.KuberlabClientException( "Provide either token or username and password.")
def do_action_on_many(action, resources, success_msg, error_msg): """Helper to run an action on many resources.""" failure_flag = False for resource in resources: try: action(resource) print(success_msg % resource) except Exception as e: failure_flag = True print(e) if failure_flag: raise exceptions.KuberlabClientException(error_msg)
def take_action(self, args): klab_client = self.app.client dests = klab_client.apps.get_destinations(args.workspace) found = None for dest in dests: if dest.Type == args.type and dest.ID == args.id: found = dest if not found: raise exceptions.KuberlabClientException( 'App destination [Type=%s, ID=%s] not found.' % (args.type, args.id)) return format_destination(found)
def take_action(self, args): klab_client = self.app.client app = klab_client.apps.get(args.workspace, args.name) sources = app.get_sources() found = None for source in sources: if source.name == args.source: found = source if not found: raise exceptions.KuberlabClientException( 'App source [name=%s] not found.' % args.source) return format_source(found)
def decorator(self, *args, **kwargs): if not hasattr(self, 'build'): raise exceptions.KuberlabClientException('Task has no build yet') return func(self, *args, **kwargs)