diff --git a/sky/provision/do/utils.py b/sky/provision/do/utils.py index 4ca35dee7e7..012c666706f 100644 --- a/sky/provision/do/utils.py +++ b/sky/provision/do/utils.py @@ -42,24 +42,33 @@ def _init_client(): global _client, CREDENTIALS_PATH if _client is None: CREDENTIALS_PATH = None + credentials_found = 0 for path in POSSIBLE_CREDENTIALS_PATHS: if os.path.exists(path): CREDENTIALS_PATH = path + credentials_found += 1 + logger.debug(f'Digital Ocean credential path found at {path}') + assert credentials_found == 1, 'more than 1 credential file found' if CREDENTIALS_PATH is None: raise DigitalOceanError( 'no credentials file found from ' f'the following paths {POSSIBLE_CREDENTIALS_PATHS}') - try: - api_token = common_utils.read_yaml( - CREDENTIALS_PATH)['auth-contexts']['skypilot'] - except KeyError as e: - raise KeyError('No valid token found for skypilot.' - 'Try setting your auth token' - 'to the skypilot context,' - '`doctl auth init --context skypilot`') from e - - _client = do.pydo.Client(token=api_token) + auth_contexts = common_utils.read_yaml( + CREDENTIALS_PATH)['auth-contexts'] + for context, api_token in auth_contexts.items(): + try: + test_client = do.pydo.Client(token=api_token) + test_client.droplets.list() + logger.debug(f'using {context} context') + _client = test_client + break + except do.exceptions().HttpResponseError: + continue + else: + raise DigitalOceanError( + 'no valid api tokens found try ' + 'setting a new API token with `doctl auth init`') return _client