def _cache_attach_data(attach, context, frame, index, path): cache_dir = _get_config_path().parent / "cache" file = cache_dir / "files" / os.sep.join( path.split("/")) / frame / str(index) attach_file = cache_dir / "attachs" / os.sep.join( path.split("/")) / frame / (str(index) + ".json") if not file.exists() or not attach_file.exists( ) or file.stat().st_size != attach.get("length"): data = BytesContent(base64.b64decode(attach["data"])) if "data" in attach else \ StreamContent(*context.protocol.download(attach["download_url"])) if file.exists(): os.remove(file) if attach_file.exists(): os.remove(attach_file) file.parent.mkdir(parents=True, exist_ok=True) data.to_file(file, show_progress=False) attach_file.parent.mkdir(parents=True, exist_ok=True) with open(attach_file, 'a') as a: a.write(json.dumps(attach)) data = FileContent(file) return data
def token_func(_: Namespace): dstack_config = from_yaml_file(_get_config_path(None)) # TODO: Support non-default profiles profile = dstack_config.get_profile("default") if profile is None or profile.token is None: sys.exit("Call 'dstack config' first") else: print(profile.token)
def remove_profile(args: Namespace): conf = from_yaml_file(_get_config_path(args.file)) if args.force or confirm( f"Do you want to delete profile '{args.profile}'"): conf.remove_profile(args.profile) conf.save()
def list_profiles(args: Namespace): conf = from_yaml_file(_get_config_path(args.file)) profiles = conf.list_profiles() print("list of available profiles:\n") for name in profiles: profile = profiles[name] print(f"{name}:") print(f"\ttoken: {hide_token(profile.token)}") if profile.server != __server_url__: print(f"\t\tserver: {profile.server}")
def list_profiles(args: Namespace): conf = from_yaml_file(_get_config_path(args.file)) print("list of available profiles:\n") profiles = conf.list_profiles() for name in profiles: profile = profiles[name] print(name) print(f"\tUser: {profile.user}") print(f"\tToken: {hide_token(profile.token)}") if profile.server != API_SERVER: print(f"\tServer: {profile.server}")
def __init__(self, config: Config = None, base_path: Optional[Path] = None, java_factory: Optional[_JavaFactory] = None, verify: bool = True): def my_java_factory(java_home: Path) -> Java: return Java(java_home) config_path = _get_config_path() self.base_path = base_path or config_path.parent self._conf = config or from_yaml_file(path=config_path) self._java_factory = java_factory if java_factory else my_java_factory self._verify = verify if not self._conf.get_profile("dstack"): profile = Profile(self._PROFILE, "dstack", None, API_SERVER, verify=verify) self._conf.add_or_replace_profile(profile) configure(self._conf)
def add_or_modify_profile(args: Namespace): file = Path(args.file) if args.file else None conf = from_yaml_file(_get_config_path(file)) profile = conf.get_profile(args.profile) token = get_or_ask(args, profile, "token", "Token: ", secure=True) if profile is None: profile = Profile(args.profile, token, args.server, not args.no_verify) elif args.force or (token != profile.token and confirm( f"Do you want to replace the token for the profile '{args.profile}'" )): profile.token = token profile.server = args.server profile.verify = not args.no_verify conf.add_or_replace_profile(profile) conf.save()
def login_func(args: Namespace): dstack_config = from_yaml_file(_get_config_path(None)) # TODO: Support non-default profiles profile = dstack_config.get_profile("default") if profile is None: profile = Profile("default", None, args.server, not args.no_verify) if args.server is not None: profile.server = args.server profile.verify = not args.no_verify user = get_or_ask(args, None, "user", "Username: "******"password", "Password: "******"user": user, "password": password } headers = { "Content-Type": f"application/json; charset=utf-8" } login_response = requests.request(method="GET", url=f"{profile.server}/users/login", params=login_params, headers=headers, verify=profile.verify) if login_response.status_code == 200: token = login_response.json()["token"] profile.token = token dstack_config.add_or_replace_profile(profile) dstack_config.save() print(f"{colorama.Fore.LIGHTBLACK_EX}OK{colorama.Fore.RESET}") else: response_json = login_response.json() if response_json.get("message") is not None: print(response_json["message"]) else: login_response.raise_for_status()
def logout_func(_: Namespace): config_path = _get_config_path(None) if config_path.exists(): config_path.unlink() print(f"{colorama.Fore.LIGHTBLACK_EX}OK{colorama.Fore.RESET}")