Skip to content

Commit

Permalink
Revert "#15 working on default args"
Browse files Browse the repository at this point in the history
This reverts commit 33a9d58.
  • Loading branch information
snobear committed Aug 21, 2014
1 parent c109d88 commit 24baabf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 91 deletions.
14 changes: 0 additions & 14 deletions ezmomi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))

Expand Down
70 changes: 69 additions & 1 deletion ezmomi/ezmomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down
84 changes: 8 additions & 76 deletions ezmomi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 24baabf

Please sign in to comment.