Skip to content

Commit

Permalink
Fix for issue skypilot-org#2015
Browse files Browse the repository at this point in the history
  • Loading branch information
ksasi committed Jun 18, 2023
1 parent 463ba61 commit c28e4ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
43 changes: 35 additions & 8 deletions sky/clouds/lambda_cloud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Lambda Cloud."""
import os
import yaml
import json
import typing
from typing import Dict, Iterator, List, Optional, Tuple
Expand Down Expand Up @@ -216,16 +218,31 @@ def _make(instance_list):

@classmethod
def check_credentials(cls) -> Tuple[bool, Optional[str]]:
"""Checks if the user has access credentials to this cloud."""
#Lambda-TODO - create a configuration script for this cloud.
required_fields = ['api_key']
credential_file = os.path.expanduser('~/.lambda_cloud/lambda_keys')
help_str = (' Store your api key '
f'in {credential_file} in the following format:\n'
' api_key = [YOUR API KEY]\n'
'To configure credentials, go to:\n '
' https://cloud.lambdalabs.com/api-keys\n '
'and generate API key ')
base_config = _read_credential_file()
if not base_config:
return (False, 'Missing credential file at '
f'{credential_file}.\n' + help_str)
for field in required_fields:
if field not in base_config:
return (False, f'Missing field "{field}" in '
f'{credential_file}.\n' + help_str)
# verifies ability of user to list instances,
# e.g. bad API KEY.
try:
lambda_utils.LambdaCloudClient().list_instances()
except (AssertionError, KeyError, lambda_utils.LambdaCloudError):
return False, ('Failed to access Lambda Cloud with credentials. '
'To configure credentials, go to:\n '
' https://cloud.lambdalabs.com/api-keys\n '
'to generate API key and add the line\n '
' api_key = [YOUR API KEY]\n '
'to ~/.lambda_cloud/lambda_keys')
return True, None
return True, None
except (AssertionError, KeyError, lambda_utils.LambdaCloudError) as e:
return (False, f'{str(e)}' + help_str)

def get_credential_file_mounts(self) -> Dict[str, str]:
return {
Expand Down Expand Up @@ -263,3 +280,13 @@ def check_disk_tier_enabled(cls, instance_type: str,
disk_tier: str) -> None:
raise exceptions.NotSupportedError(
'Lambda does not support disk tiers.')


def _read_credential_file():
try:
with open(os.path.expanduser('~/.lambda_cloud/lambda_keys'),
'r',
encoding='utf-8') as f:
return yaml.safe_load(f)
except FileNotFoundError:
return False
48 changes: 36 additions & 12 deletions sky/clouds/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
to access the SCP catalog and check credentials for the SCP access.
"""

import os
import yaml
import json
import typing
from typing import Dict, Iterator, List, Optional, Tuple
Expand Down Expand Up @@ -282,20 +284,32 @@ def _make(instance_list):

@classmethod
def check_credentials(cls) -> Tuple[bool, Optional[str]]:
"""Checks if the user has access credentials to this cloud."""
#SCP-TODO - create a configuration script for this cloud.
required_fields = ['access_key', 'secret_key', 'project_id']
credential_file = os.path.expanduser('~/.scp/scp_credential')
help_str = (
' Store your access key, secret key and project id '
f'in {credential_file} in the following format:\n'
' access_key: [YOUR API ACCESS KEY]\n'
' secret_key: [YOUR API SECRET KEY]\n'
' project_id: [YOUR PROJECT ID]\n'
'For more info see: https://cloud.samsungsds.com/openapiguide')
base_config = _read_credential_file()
if not base_config:
return (False, 'Missing credential file at '
f'{credential_file}.\n' + help_str)
for field in required_fields:
if field not in base_config:
return (False, f'Missing field "{field}" in '
f'{credential_file}.\n' + help_str)
# verifies ability of user to list instances,
# e.g. bad API KEY.
try:
scp_utils.SCPClient().list_instances()
except (AssertionError, KeyError, scp_utils.SCPClientError):
return False, (
'Failed to access SCP with credentials. '
'To configure credentials, see: '
'https://cloud.samsungsds.com/openapiguide\n'
f'{cls._INDENT_PREFIX}Generate API key and add the '
'following line to ~/.scp/scp_credential:\n'
f'{cls._INDENT_PREFIX} access_key = [YOUR API ACCESS KEY]\n'
f'{cls._INDENT_PREFIX} secret_key = [YOUR API SECRET KEY]\n'
f'{cls._INDENT_PREFIX} project_id = [YOUR PROJECT ID]')

return True, None
return True, None
except (AssertionError, KeyError, scp_utils.SCPClientError) as e:
return (False, f'{str(e)} not configured' + help_str)

def get_credential_file_mounts(self) -> Dict[str, str]:
return {
Expand Down Expand Up @@ -333,3 +347,13 @@ def is_disk_size_allowed(resources):
f'Input: {resources.disk_size}')
return False, []
return True, [resources]


def _read_credential_file():
try:
with open(os.path.expanduser('~/.scp/scp_credential'),
'r',
encoding='utf-8') as f:
return yaml.safe_load(f)
except FileNotFoundError:
return False

0 comments on commit c28e4ec

Please sign in to comment.