Skip to content

Commit 79d9162

Browse files
demartinofrasean-smith
authored andcommitted
Handle ClientError in pcluster configure
Signed-off-by: Francesco De Martino <[email protected]>
1 parent 9cf977f commit 79d9162

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

cli/pcluster/easyconfig.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from __future__ import print_function
2-
from __future__ import absolute_import
1+
32
# Copyright 2013-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
43
#
54
# Licensed under the Apache License, Version 2.0 (the 'License'). You may not use this file except in compliance with the
@@ -10,23 +9,39 @@
109
# or in the 'LICENSE.txt' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
1110
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
1211
# limitations under the License.
12+
from __future__ import absolute_import, print_function
1313

14-
from future import standard_library
15-
standard_library.install_aliases()
16-
from builtins import input
17-
import configparser
18-
import sys
19-
import boto3
20-
import os
14+
import errno
15+
import functools
2116
import logging
17+
import os
2218
import stat
23-
import errno
19+
import sys
20+
21+
import boto3
22+
import configparser
23+
from botocore.exceptions import BotoCoreError, ClientError
24+
from builtins import input
25+
from future import standard_library
2426

2527
from . import cfnconfig
2628

29+
standard_library.install_aliases()
30+
2731
logger = logging.getLogger('pcluster.pcluster')
2832
unsupported_regions = ['ap-northeast-3', 'cn-north-1', 'cn-northwest-1']
2933

34+
def handle_client_exception(func):
35+
@functools.wraps(func)
36+
def wrapper():
37+
try:
38+
func()
39+
except (BotoCoreError, ClientError) as e:
40+
print("Failed with error: %s" % e)
41+
print("Hint: please check your AWS credentials.")
42+
sys.exit(1)
43+
return wrapper
44+
3045
def prompt(prompt, default_value=None, hidden=False, options=None, check_validity=False):
3146
if hidden and default_value is not None:
3247
user_prompt = prompt + ' [*******' + default_value[-4:] + ']: '
@@ -54,6 +69,7 @@ def prompt(prompt, default_value=None, hidden=False, options=None, check_validit
5469
else:
5570
return var
5671

72+
@handle_client_exception
5773
def get_regions():
5874
ec2 = boto3.client('ec2')
5975
regions = ec2.describe_regions().get('Regions')
@@ -72,6 +88,7 @@ def ec2_conn(aws_access_key_id, aws_secret_access_key, aws_region_name):
7288
aws_secret_access_key=aws_secret_access_key)
7389
return ec2
7490

91+
@handle_client_exception
7592
def list_keys(aws_access_key_id, aws_secret_access_key, aws_region_name):
7693
conn = ec2_conn(aws_access_key_id, aws_secret_access_key, aws_region_name)
7794
keypairs = conn.describe_key_pairs()
@@ -86,6 +103,7 @@ def list_keys(aws_access_key_id, aws_secret_access_key, aws_region_name):
86103

87104
return keynames
88105

106+
@handle_client_exception
89107
def list_vpcs(aws_access_key_id, aws_secret_access_key, aws_region_name):
90108
conn = ec2_conn(aws_access_key_id, aws_secret_access_key, aws_region_name)
91109
vpcs = conn.describe_vpcs()
@@ -100,6 +118,7 @@ def list_vpcs(aws_access_key_id, aws_secret_access_key, aws_region_name):
100118

101119
return vpcids
102120

121+
@handle_client_exception
103122
def list_subnets(aws_access_key_id, aws_secret_access_key, aws_region_name, vpc_id):
104123
conn = ec2_conn(aws_access_key_id, aws_secret_access_key, aws_region_name)
105124
subnets = conn.describe_subnets(Filters=[{'Name': 'vpcId', 'Values': [vpc_id]}])
@@ -133,6 +152,10 @@ def configure(args):
133152
cluster_template = prompt('Cluster Template', config.get('global', 'cluster_template') if config.has_option('global', 'cluster_template') else 'default')
134153
aws_access_key_id = prompt('AWS Access Key ID', config.get('aws', 'aws_access_key_id') if config.has_option('aws', 'aws_access_key_id') else None, True)
135154
aws_secret_access_key = prompt('AWS Secret Access Key ID', config.get('aws', 'aws_secret_access_key') if config.has_option('aws', 'aws_secret_access_key') else None, True)
155+
if not aws_access_key_id or not aws_secret_access_key:
156+
print("You chose not to configure aws credentials in parallelcluster config file.\n"
157+
"Please make sure you export a valid AWS_PROFILE or you have them exported in "
158+
"the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.")
136159

137160
# Use built in boto regions as an available option
138161
aws_region_name = prompt('AWS Region ID', config.get('aws', 'aws_region_name') if config.has_option('aws', 'aws_region_name') else None, options=get_regions(), check_validity=True)

0 commit comments

Comments
 (0)