def login(endpoint: str, service_principal: bool = False, username: str = None, password: str = None) -> None: logger = get_logger('login') try: requests.get(f'https://{endpoint}/api/healthy').raise_for_status() except (requests.HTTPError, requests.ConnectionError): logger.error(f'Cannot reach endpoint https://{endpoint}/api/healthy') sys.exit(1) auth = AuthSettings() if service_principal: if not username or not password: logger.error('Username or password is missing.') sys.exit(1) if not auth.login_service_principal(username, password): logger.error(f'Fail to login using service principal {username}.') sys.exit(1) else: if not auth.login(): sys.exit(1) config = A01Config() config.endpoint = endpoint if not config.save(): logger.error(f'Cannot read or write to file {CONFIG_FILE}') sys.exit(1) sys.exit(0)
def create_run(image: str, from_failures: str = None, live: bool = False, parallelism: int = 3, query: str = None, remark: str = '', email: bool = False, secret: str = None, mode: str = None, exclude: str = None, agent: str = 'latest') -> None: auth = AuthSettings() remark = remark or '' creator = auth.get_user_name() agent = agent.replace('.', '-') reg, image_name = image.split('/', 1) reg = reg.split('.')[0] try: run_model = Run(name=f'Run of {image_name} from {reg}', settings={ 'a01.reserved.imagename': image, 'a01.reserved.imagepullsecret': 'azureclidev-registry', 'a01.reserved.secret': secret, 'a01.reserved.storageshare': 'k8slog', 'a01.reserved.testquery': query, 'a01.reserved.testexcludequery': exclude, 'a01.reserved.remark': remark, 'a01.reserved.useremail': auth.user_id if email else '', 'a01.reserved.initparallelism': parallelism, 'a01.reserved.livemode': str(live), 'a01.reserved.testmode': mode, 'a01.reserved.fromrunfailure': from_failures, 'a01.reserved.agentver': agent, }, details={ 'a01.reserved.creator': creator, 'a01.reserved.client': f'CLI {a01.__version__}' }, owner=creator, status='Initialized') run = run_model.post() print(f'Published run {run.id}') sys.exit(0) except ValueError as ex: logger = logging.getLogger(__name__) logger.error(ex) sys.exit(1)
def get_runs(me: bool = False, last: int = 20, skip: int = 0, owner: str = None) -> None: # pylint: disable=invalid-name try: if me and owner: raise ValueError('--me and --user are mutually exclusive.') elif me: owner = AuthSettings().get_user_name() runs = a01.models.RunCollection.get(owner=owner, last=last, skip=skip) output_in_table(runs.get_table_view(), headers=runs.get_table_header()) except ValueError as err: logger.error(err) sys.exit(1) except AuthenticationError as err: logger.error(err) print('You need to login. Usage: a01 login.', file=sys.stderr) sys.exit(1)
class AsyncSession(ClientSession): def __init__(self) -> None: super(AsyncSession, self).__init__() self.auth = AuthSettings() self.endpoint = A01Config().ensure_config().endpoint_uri self.logger = getLogger(__name__) def get_path(self, path: str) -> str: return f'{self.endpoint}/{path}' async def get_json(self, path: str) -> Union[List, dict, float, str, None]: if self.auth.is_expired and not self.auth.refresh(): self.logger.error('Fail to refresh access token. Please login again.') sys.exit(1) headers = {'Authorization': self.auth.access_token} async with self.get(self.get_path(path), headers=headers) as resp: try: return await resp.json() except ContentTypeError: self.logger.error('Incorrect content type') self.logger.error(await resp.text()) raise
def logout() -> None: AuthSettings().logout()
def __init__(self) -> None: super(AsyncSession, self).__init__() self.auth = AuthSettings() self.endpoint = A01Config().ensure_config().endpoint_uri self.logger = getLogger(__name__)
def whoami() -> None: try: print(AuthSettings().summary) except AuthenticationError: print('You need to login. Usage: a01 login.')