Skip to content

Commit

Permalink
Move config-related code out of cloudenvy.core
Browse files Browse the repository at this point in the history
  • Loading branch information
bcwaldon committed Oct 22, 2013
1 parent cbc0507 commit 44987e6
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 84 deletions.
12 changes: 5 additions & 7 deletions cloudenvy/clouds/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ class CloudAPI(object):
def __init__(self, config):
self._client = None
self.config = config
self.user_config = config['cloudenvy']
self.project_config = config['project_config']

# OpenStack Auth Items
self.user = self.user_config['cloud'].get('os_username', None)
self.password = self.user_config['cloud'].get('os_password', None)
self.tenant_name = self.user_config['cloud'].get('os_tenant_name',
self.user = self.config.user_config['cloud'].get('os_username', None)
self.password = self.config.user_config['cloud'].get('os_password', None)
self.tenant_name = self.config.user_config['cloud'].get('os_tenant_name',
None)
self.auth_url = self.user_config['cloud'].get('os_auth_url', None)
self.region_name = self.user_config['cloud'].get('os_region_name',
self.auth_url = self.config.user_config['cloud'].get('os_auth_url', None)
self.region_name = self.config.user_config['cloud'].get('os_region_name',
None)

@property
Expand Down
2 changes: 1 addition & 1 deletion cloudenvy/commands/dotfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run(self, config, args):
envy = cloudenvy.core.Envy(config)

if envy.ip():
host_string = '%s@%s' % (envy.remote_user, envy.ip())
host_string = '%s@%s' % (envy.config.remote_user, envy.ip())

temp_tar = tempfile.NamedTemporaryFile(delete=True)

Expand Down
6 changes: 3 additions & 3 deletions cloudenvy/commands/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def run(self, config, args):
envy = cloudenvy.core.Envy(config)

if envy.ip():
host_string = '%s@%s' % (envy.remote_user, envy.ip())
host_string = '%s@%s' % (envy.config.remote_user, envy.ip())

with fabric.api.settings(host_string=host_string):
use_sudo = envy.project_config.get('files_use_sudo', True)
files = envy.project_config.get('files', {}).items()
use_sudo = envy.config.project_config.get('files_use_sudo', True)
files = envy.config.project_config.get('files', {}).items()
files = [(os.path.expanduser(loc), rem) for loc, rem in files]

for local_path, remote_path in files:
Expand Down
12 changes: 6 additions & 6 deletions cloudenvy/commands/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ def run(self, config, args):
envy = cloudenvy.core.Envy(config)

logging.info('Running provision scripts for Envy \'%s\'.' %
envy.project_config['name'])
envy.name)
if not envy.ip():
logging.error('Could not determine IP.')
return

with fabric.api.settings(
host_string=envy.ip(), user=envy.remote_user,
host_string=envy.ip(), user=envy.config.remote_user,
forward_agent=True, disable_known_hosts=True):

if args.scripts:
scripts = [os.path.expanduser(script) for
script in args.scripts]
elif 'provision_scripts' in envy.project_config:
elif 'provision_scripts' in envy.config.project_config:
scripts = [os.path.expanduser(script) for script in
envy.project_config['provision_scripts']]
elif 'provision_script_path' in envy.project_config:
provision_script = envy.project_config['provision_script_path']
envy.config.project_config['provision_scripts']]
elif 'provision_script_path' in envy.config.project_config:
provision_script = envy.config.project_config['provision_script_path']
scripts = [os.path.expanduser(provision_script)]
else:
raise SystemExit('Please specify the path to your provision '
Expand Down
2 changes: 1 addition & 1 deletion cloudenvy/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run(self, config, args):
envy = cloudenvy.core.Envy(config)

if envy.ip():
host_string = '%s@%s' % (envy.remote_user, envy.ip())
host_string = '%s@%s' % (envy.config.remote_user, envy.ip())
with fabric.api.settings(host_string=host_string):
fabric.operations.run(args.command)
else:
Expand Down
2 changes: 1 addition & 1 deletion cloudenvy/commands/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run(self, config, args):
logging.error('Could not determine IP.')
return

host_string = '%s@%s' % (envy.remote_user, envy.ip())
host_string = '%s@%s' % (envy.config.remote_user, envy.ip())

with fabric.api.settings(host_string=host_string):
fabric.operations.put(
Expand Down
4 changes: 2 additions & 2 deletions cloudenvy/commands/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def run(self, config, args):
forward_agent = '-o ForwardAgent=yes'

options = [disable_known_hosts]
if envy.forward_agent:
if envy.config.forward_agent:
options.append(forward_agent)

fabric.operations.local('ssh %s %s@%s' % (' '.join(options),
envy.remote_user,
envy.config.remote_user,
envy.ip()))
else:
logging.error('Could not determine IP.')
4 changes: 2 additions & 2 deletions cloudenvy/commands/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def run(self, config, args):
if not args.no_files:
self.commands['files'].run(config, args)
if not args.no_provision \
and (envy.project_config.get("auto_provision", True)
and 'provision_scripts' in envy.project_config):
and (envy.config.project_config.get("auto_provision", True)
and 'provision_scripts' in envy.config.project_config):
try:
self.commands['provision'].run(config, args)
except SystemExit:
Expand Down
46 changes: 46 additions & 0 deletions cloudenvy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@


class EnvyConfig(object):

def __init__(self, config):
self.base_name = config['project_config'].get('base_name')
self.config = config
self.user_config = config['cloudenvy']
self.project_config = config['project_config']
self.default_config = config['defaults']

image_name = self.project_config.get('image_name')
image_id = self.project_config.get('image_id', None)
image = self.project_config.get('image')
self.image = image_name or image_id or image

self.flavor = self.project_config.get(
'flavor_name', self.default_config['flavor_name'])
self.remote_user = self.project_config.get(
'remote_user', self.default_config['remote_user'])
self.auto_provision = self.project_config.get('auto_provision', False)
self.sec_group_name = self.project_config.get('sec_group_name',
self.base_name)

self.keypair_name = self._get_config('keypair_name')
self.keypair_location = self._get_config('keypair_location')
self.forward_agent = self._get_config('forward_agent')

def _get_config(self, name, default=None):
"""Traverse the various config files in order of specificity.
The order is as follows, most important (specific) to least:
Project
Cloud
User
Default
"""
value = self.project_config.get(
name,
self.user_config['cloud'].get(
name,
self.user_config.get(
name,
self.default_config.get(name,
default))))
return value


class Config(object):
"""Base class for envy commands"""

def __init__(self, args):
Expand Down
80 changes: 21 additions & 59 deletions cloudenvy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,13 @@

class Envy(object):
def __init__(self, config):
self.name = config['project_config'].get('name')
self.base_name = config['project_config'].get('base_name')
self.config = config
self.user_config = config['cloudenvy']
self.project_config = config['project_config']
self.default_config = config['defaults']

self.cloud_api = openstack.CloudAPI(self.config)
self.image_name = self.project_config.get('image_name')
self.image_id = self.project_config.get('image_id', None)
self.image = self.project_config.get('image')
self.flavor_name = self.project_config.get(
'flavor_name', self.default_config['flavor_name'])
self.remote_user = self.project_config.get(
'remote_user', self.default_config['remote_user'])
self.auto_provision = self.project_config.get('auto_provision', False)
self.sec_group_name = self.project_config.get('sec_group_name',
self.base_name)

self.keypair_name = self._get_config('keypair_name')
self.keypair_location = self._get_config('keypair_location')
self.forward_agent = self._get_config('forward_agent')
self.name = config.project_config.get('name')

self._server = None
self._ip = None

def _get_config(self, name, default=None):
"""Traverse the various config files in order of specificity.
The order is as follows, most important (specific) to least:
Project
Cloud
User
Default
"""
value = self.project_config.get(
name,
self.user_config['cloud'].get(
name,
self.user_config.get(
name,
self.default_config.get(name,
default))))
return value

def list_servers(self):
return self.cloud_api.list_servers()

Expand All @@ -79,36 +42,35 @@ def ip(self):
% self.name)

def build_server(self):
image_name = self.image_name or self.image_id or self.image
logging.info("Using image: %s" % image_name)
logging.info("Using image: %s" % self.config.image)
try:
image = self.cloud_api.find_image(image_name)
image = self.cloud_api.find_image(self.config.image)
except novaclient.exceptions.NoUniqueMatch:
msg = ('There are more than one images named %s. Please specify '
'image id in your config.') % image_name
raise SystemExit(msg)
'image id in your config.')
raise SystemExit(msg % self.config.image)
if not image:
raise SystemExit('The image %s does not exist.' % image_name)
flavor = self.cloud_api.find_flavor(self.flavor_name)
raise SystemExit('The image %s does not exist.' %
self.config.image)
flavor = self.cloud_api.find_flavor(self.config.flavor)
if not flavor:
raise SystemExit('The flavor %s does not exist.' %
self.flavor_name)
self.config.flavor)
build_kwargs = {
'name': self.name,
'image': image,
'flavor': flavor,
}

# TODO(jakedahn): security group name should be pulled from config.
logging.info('Using security group: %s', self.sec_group_name)
self._ensure_sec_group_exists(self.sec_group_name)
build_kwargs['security_groups'] = [self.sec_group_name]
logging.info('Using security group: %s', self.config.sec_group_name)
self._ensure_sec_group_exists(self.config.sec_group_name)
build_kwargs['security_groups'] = [self.config.sec_group_name]

if self.keypair_name is not None:
logging.info('Using keypair: %s', self.keypair_name)
self._ensure_keypair_exists(self.keypair_name,
self.keypair_location)
build_kwargs['key_name'] = self.keypair_name
if self.config.keypair_name is not None:
logging.info('Using keypair: %s', self.config.keypair_name)
self._ensure_keypair_exists(self.config.keypair_name,
self.config.keypair_location)
build_kwargs['key_name'] = self.config.keypair_name

build_kwargs['meta'] = {}
#TODO(gabrielhurley): Allow user-defined server metadata, see
Expand Down Expand Up @@ -172,12 +134,12 @@ def _ensure_sec_group_exists(self, name):
except novaclient.exceptions.BadRequest:
logging.error('Security Group "%s" already exists.' % name)

if 'sec_groups' in self.project_config:
if 'sec_groups' in self.config.project_config:
rules = [tuple(rule.split(', ')) for rule in
self.project_config['sec_groups']]
self.config.project_config['sec_groups']]
else:
rules = [tuple(rule.split(', ')) for rule in
self.default_config['sec_groups']]
self.config.default_config['sec_groups']]
for rule in rules:
logging.debug('... adding rule: %s', rule)
try:
Expand Down
5 changes: 3 additions & 2 deletions cloudenvy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pkgutil
import string

from cloudenvy.config import EnvyConfig
from cloudenvy.config import EnvyConfig,Config

import cloudenvy.commands

Expand Down Expand Up @@ -80,7 +80,8 @@ def main():
_init_commands(commands, command_subparser)

args = parser.parse_args()
config = EnvyConfig(args)
config = Config(args)
config = EnvyConfig(config)

if args.verbosity == 3:
logging.getLogger().setLevel(logging.DEBUG)
Expand Down

0 comments on commit 44987e6

Please sign in to comment.