Changed settings validation code in launch_ec2_nodes.py

This commit is contained in:
troymc 2016-05-17 11:26:09 +02:00
parent 8839440fd7
commit f168e97880

View File

@ -23,6 +23,14 @@ import boto3
from awscommon import get_naeips from awscommon import get_naeips
SETTINGS = ['NUM_NODES', 'BRANCH', 'WHAT_TO_DEPLOY', 'USE_KEYPAIRS_FILE',
'IMAGE_ID', 'INSTANCE_TYPE']
class SettingsTypeError(TypeError):
pass
# Ensure they're using Python 2.5-2.7 # Ensure they're using Python 2.5-2.7
pyver = sys.version_info pyver = sys.version_info
major = pyver[0] major = pyver[0]
@ -48,32 +56,34 @@ deploy_conf_file = args.deploy_conf_file
# Import all the variables set in the AWS deployment configuration file # Import all the variables set in the AWS deployment configuration file
# (Remove the '.py' from the end of deploy_conf_file.) # (Remove the '.py' from the end of deploy_conf_file.)
cf = importlib.import_module(deploy_conf_file[:-3]) cf = importlib.import_module(deploy_conf_file[:-3])
try:
NUM_NODES = cf.NUM_NODES dir_cf = dir(cf) # = a list of the attributes of cf
BRANCH = cf.BRANCH for setting in SETTINGS:
WHAT_TO_DEPLOY = cf.WHAT_TO_DEPLOY if setting not in dir_cf:
USE_KEYPAIRS_FILE = cf.USE_KEYPAIRS_FILE sys.exit('{} was not set '.format(setting) +
IMAGE_ID = cf.IMAGE_ID 'in the specified AWS deployment '
INSTANCE_TYPE = cf.INSTANCE_TYPE 'configuration file {}'.format(deploy_conf_file))
except AttributeError as e: exec('{0} = cf.{0}'.format(setting))
print('One of the AWS deployment configuration settings was '
'not set in the AWS deployment configuration file ' +
'{}'.format(deploy_conf_file))
print('Read this traceback to find out which one (in ALL_CAPS):')
raise
# Validate the variables set in the AWS deployment configuration file # Validate the variables set in the AWS deployment configuration file
try: if not isinstance(NUM_NODES, int):
assert isinstance(NUM_NODES, int) raise SettingsTypeError('NUM_NODES should be an int')
assert isinstance(BRANCH, str)
assert isinstance(WHAT_TO_DEPLOY, str) if not isinstance(BRANCH, str):
assert isinstance(USE_KEYPAIRS_FILE, bool) raise SettingsTypeError('BRANCH should be a string')
assert isinstance(IMAGE_ID, str)
assert isinstance(INSTANCE_TYPE, str) if not isinstance(WHAT_TO_DEPLOY, str):
except AssertionError as e: raise SettingsTypeError('WHAT_TO_DEPLOY should be a string')
print('One of the AWS deployment settings has a value of the wrong type.')
print('Read this traceback to find out which one (in ALL_CAPS):') if not isinstance(USE_KEYPAIRS_FILE, bool):
raise msg = 'USE_KEYPAIRS_FILE should a boolean (True or False)'
raise SettingsTypeError(msg)
if not isinstance(IMAGE_ID, str):
raise SettingsTypeError('IMAGE_ID should be a string')
if not isinstance(INSTANCE_TYPE, str):
raise SettingsTypeError('INSTANCE_TYPE should be a string')
if NUM_NODES > 64: if NUM_NODES > 64:
raise ValueError('NUM_NODES should be less than or equal to 64. ' raise ValueError('NUM_NODES should be less than or equal to 64. '