def test_empty_config(self): conf = from_yaml_file(self.config_path) # shouldn't raise an exception conf.list_profiles() conf.add_or_replace_profile(Profile("default", "user", "test_token", API_SERVER, verify=True)) conf.save() conf = from_yaml_file(self.config_path) self.assertEqual(1, len(conf.list_profiles())) self.assertEqual("test_token", conf.get_profile("default").token)
def test_save_and_load(self): self.create_yaml_file(self.config_path, self.conf_example()) conf = from_yaml_file(self.config_path) default = conf.get_profile("default") profile = conf.get_profile("other") profile.token = "my_new_token" conf.add_or_replace_profile(profile) conf.save() conf = from_yaml_file(self.config_path) self.assertEqual(profile.token, conf.get_profile("other").token) self.assertEqual(default.token, conf.get_profile("default").token)
def test_get_float_property_saved_by_java_library(self): content = self.conf_example() content.update({"server": {"version": 0.1}}) self.create_yaml_file(self.config_path, content) conf = from_yaml_file(self.config_path) self.assertEqual("0.1", conf.get_property("server.version"))
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, installer_path: Optional[Path] = None, java_factory: Optional[_JavaFactory] = None, verify: bool = True): def my_java_factory(java_home: Path) -> Java: return Java(java_home) self.installer_path = installer_path or _get_installer_path() config_path = self.installer_path / "config.yaml" self.config = 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.config.get_profile(self._PROFILE): profile = Profile(self._PROFILE, self._USER, None, API_SERVER, verify=verify) self.config.add_or_replace_profile(profile)
def test_set_get_properties(self): conf = from_yaml_file(self.config_path) conf.set_property("simple", "my value") self.assertEqual("my value", conf.get_property("simple")) self.assertIsNone(conf.get_property("missing")) conf.set_property("a.b", "hello") conf.set_property("a.c", "world") self.assertEqual("hello", conf.get_property("a.b")) self.assertEqual("world", conf.get_property("a.c")) conf.set_property("a.x.c", "hello") conf.set_property("a.y.d", "world") self.assertEqual("hello", conf.get_property("a.x.c")) self.assertEqual("world", conf.get_property("a.y.d"))
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()