diff --git a/ezmomi/cli.py b/ezmomi/cli.py index 1bc89ac..078ee05 100644 --- a/ezmomi/cli.py +++ b/ezmomi/cli.py @@ -6,11 +6,6 @@ from ezmomi import EZMomi -# DEV -import sys -from pprint import pprint -#ENDDEV - def cli(): # Set up command line arguments parser = argparse.ArgumentParser( @@ -23,16 +18,7 @@ def cli(): # parse arguments args = parser.parse_args() - - # required arguments - - pprint(args) - notset = list() - if not args.hostname: - parser.error('hostname not set') - - sys.exit() # initialize ezmomi instance ez = EZMomi(**vars(args)) diff --git a/ezmomi/ezmomi.py b/ezmomi/ezmomi.py index 24560da..f1f0a54 100644 --- a/ezmomi/ezmomi.py +++ b/ezmomi/ezmomi.py @@ -14,9 +14,77 @@ class EZMomi(object): def __init__(self, **kwargs): # load up our configs and connect to the vSphere server - self.config = kwargs + self.config = self.get_configs(kwargs) self.connect() + def get_configs(self, kwargs): + default_cfg_dir = "%s/.config/ezmomi" % os.path.expanduser("~") + default_config_file = "%s/config.yml" % default_cfg_dir + + # use path from env var if it's set and valid + if 'EZMOMI_CONFIG' in os.environ: + if os.path.isfile(os.environ['EZMOMI_CONFIG']): + config_file = os.environ['EZMOMI_CONFIG'] + else: + print "%s does not exist. Set the EZMOMI_CONFIG environment" \ + "variable to your config file's path." + sys.exit(1) + + # or use the default config file path if it exists + elif os.path.isfile(default_config_file): + config_file = default_config_file + # else create the default config path and copy the example config there + else: + from shutil import copy + if not os.path.exists(default_cfg_dir): + os.makedirs(default_cfg_dir) + config_file = default_config_file + + # copy example config + ezmomi_module_dir = os.path.dirname(os.path.abspath(__file__)) + ezmomi_ex_config = ("%s/config/config.yml.example" + % ezmomi_module_dir) + try: + copy(ezmomi_ex_config, default_cfg_dir) + except: + print ("Error copying example config file from %s to %s" + % (ezmomi_ex_config, default_cfg_dir)) + sys.exit(1) + + print "I could not find a config.yml file, so I copied an example " \ + "to your home directory at %s/config.yml.example. Please " \ + "rename this to config.yml and add your vSphere " \ + "environment's settings." % default_cfg_dir + sys.exit(0) + try: + config = yaml.load(file(config_file)) + except IOError: + print 'Unable to open config file. The default path for the ezmomi' \ + ' config file is ~/.config/ezmomi/config.yml. You can also ' \ + 'specify the config file path by setting the EZMOMI_CONFIG ' \ + 'environment variable.' + sys.exit(1) + except Exception: + print 'Unable to read config file. YAML syntax issue, perhaps?' + sys.exit(1) + + # Check all required values were supplied either via command line + # or config. override defaults from config.yml with any supplied + # command line arguments + notset = list() + for key, value in kwargs.items(): + if value: + config[key] = value + elif (value is None) and (key not in config): + # compile list of parameters that were not set + notset.append(key) + + if notset: + print "Required parameters not set: %s\n" % notset + sys.exit(1) + + return config + ''' Connect to vCenter server ''' diff --git a/ezmomi/params.py b/ezmomi/params.py index cf6736b..1705dc6 100644 --- a/ezmomi/params.py +++ b/ezmomi/params.py @@ -2,78 +2,18 @@ Command line option definitions ''' -import sys -import os -import yaml - -def get_configs(): - default_cfg_dir = "%s/.config/ezmomi" % os.path.expanduser("~") - default_config_file = "%s/config.yml" % default_cfg_dir - - # use path from env var if it's set and valid - if 'EZMOMI_CONFIG' in os.environ: - if os.path.isfile(os.environ['EZMOMI_CONFIG']): - config_file = os.environ['EZMOMI_CONFIG'] - else: - print "%s does not exist. Set the EZMOMI_CONFIG environment" \ - "variable to your config file's path." - sys.exit(1) - - # or use the default config file path if it exists - elif os.path.isfile(default_config_file): - config_file = default_config_file - # else create the default config path and copy the example config there - else: - from shutil import copy - if not os.path.exists(default_cfg_dir): - os.makedirs(default_cfg_dir) - config_file = default_config_file - - # copy example config - ezmomi_module_dir = os.path.dirname(os.path.abspath(__file__)) - ezmomi_ex_config = ("%s/config/config.yml.example" - % ezmomi_module_dir) - try: - copy(ezmomi_ex_config, default_cfg_dir) - except: - print ("Error copying example config file from %s to %s" - % (ezmomi_ex_config, default_cfg_dir)) - sys.exit(1) - - print "I could not find a config.yml file, so I copied an example " \ - "to your home directory at %s/config.yml.example. Please " \ - "rename this to config.yml and add your vSphere " \ - "environment's settings." % default_cfg_dir - sys.exit(0) - try: - config = yaml.load(file(config_file)) - except IOError: - print 'Unable to open config file. The default path for the ezmomi' \ - ' config file is ~/.config/ezmomi/config.yml. You can also ' \ - 'specify the config file path by setting the EZMOMI_CONFIG ' \ - 'environment variable.' - sys.exit(1) - except Exception: - print 'Unable to read config file. YAML syntax issue, perhaps?' - sys.exit(1) - - return config - -def get_default(config, param): - if param in config: - return config[param] def add_params(subparsers): - # default configs - config = get_configs() # list list_parser = subparsers.add_parser( 'list', help='List VMware objects on your VMware server' ) + list_parser.add_argument( '--type', - help='Object type, e.g. Network, VirtualMachine.', + required=True, + help='Object type, e.g. Network, VirtualMachine.' ) # clone @@ -85,37 +25,32 @@ def add_params(subparsers): '--server', type=str, help='vCenter server', - default=get_default(config, 'server'), ) clone_parser.add_argument( '--port', type=str, help='vCenter server port', - default=get_default(config, 'port'), ) clone_parser.add_argument( '--username', type=str, help='vCenter username', - default=get_default(config, 'username'), ) clone_parser.add_argument( '--password', type=str, help='vCenter password', - default=get_default(config, 'password'), ) clone_parser.add_argument( '--template', type=str, - help='VM template name to clone from', - default=get_default(config, 'template'), + help='VM template name to clone from' ) clone_parser.add_argument( '--hostname', + required=True, type=str, help='New host name', - default=get_default(config, 'hostname'), ) clone_parser.add_argument( '--ips', @@ -127,20 +62,17 @@ def add_params(subparsers): clone_parser.add_argument( '--cpus', type=int, - help='Number of CPUs', - default=get_default(config, 'cpus'), + help='Number of CPUs' ) clone_parser.add_argument( '--mem', type=int, - help='Memory in GB', - default=get_default(config, 'mem'), + help='Memory in GB' ) clone_parser.add_argument( '--domain', type=str, - help='Domain, e.g. "example.com"', - default=get_default(config, 'domain'), + help='Domain, e.g. "example.com"' ) # destroy