Skip to content
Permalink
Browse files
Enable ec2stack to run with profiles
  • Loading branch information
brogand93 committed Jul 3, 2014
1 parent 46df109 commit 193adc9fab12fd56c91f4bb62dc1e538a931c592
Showing 2 changed files with 68 additions and 16 deletions.
@@ -6,8 +6,10 @@

import os
import sys
import argparse

from flask import Flask
from ConfigParser import SafeConfigParser

from ec2stack.controllers import *
from ec2stack.core import DB
@@ -26,12 +28,14 @@ def create_app(settings=None):
if settings:
app.config.from_object(settings)
else:
args = _generate_args()
profile = args.pop('profile')
app.config['DEBUG'] = args.pop('debug')
config_file = _load_config_file()
app.config.from_pyfile(config_file)
database_uri = _load_database()
_config_from_config_profile(config_file, profile, app)
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri

_valid_config_file(app)
DB.init_app(app)

default_controller = __import__(
@@ -43,6 +47,33 @@ def create_app(settings=None):
return app


def _generate_args():
"""
Generate command line arguments for ec2stack.
"""
parser = argparse.ArgumentParser()

parser.add_argument(
'-p',
'--profile',
required=False,
help='The profile to run ec2stack with, default is initial',
default='initial'
)

parser.add_argument(
'-d',
'--debug',
required=False,
help='Turn debug on for application',
default=False
)

args = parser.parse_args()

return vars(args)


def _load_config_file():
"""
Checks that the user's configuration file exists and returns its path.
@@ -60,20 +91,36 @@ def _load_config_file():
return config_file


def _valid_config_file(app):
def _config_from_config_profile(config_file, profile, app):
"""
Validates that the configuration file has all the required parameters.
@param app: The flask application.
Configures ec2stack app based on configuration profile.
"""
for config_item in ['EC2STACK_BIND_ADDRESS', 'EC2STACK_PORT',
'CLOUDSTACK_HOST', 'CLOUDSTACK_PORT',
'CLOUDSTACK_PROTOCOL', 'CLOUDSTACK_PATH',
'CLOUDSTACK_CUSTOM_DISK_OFFERING',
'CLOUDSTACK_DEFAULT_ZONE']:
if config_item not in app.config:
sys.exit('Configuration file is missing %s' % config_item)
config = SafeConfigParser()
config.read(config_file)

if not config.has_section(profile):
sys.exit('No profile matching ' + profile
+ ' found in configuration, please run ec2stack-configure -p '
+ profile)

for attribute in config.options(profile):
app.config[attribute.upper()] = config.get(profile, attribute)

instance_type_map = {}
instance_section = profile + "instancemap"
if config.has_section(instance_section):
for attribute in config.options():
instance_type_map[attribute] = config.get(instance_section, attribute)

app.config['INSTANCE_TYPE_MAP'] = instance_type_map

resource_type_map = {}
resource_section = profile + "resourcemap"
if config.has_section(resource_section):
for attribute in config.options(resource_section):
resource_type_map[attribute] = config.get(resource_section, attribute)

app.config['RESOURCE_TYPE_MAP '] = resource_type_map

def _load_database():
"""
@@ -51,7 +51,7 @@ def _create_config_file(config_folder):

def _generate_args():
"""
Generate command line arguments for configuration.
Generate command line arguments for ec2stack-configure.
"""
parser = argparse.ArgumentParser(
'Command line utility for configuring ec2stack'
@@ -71,6 +71,9 @@ def _generate_args():


def _modify_config_profile(config_file, profile):
"""
Modify configuration profile
"""
config = SafeConfigParser()
config.read(config_file)

@@ -133,7 +136,8 @@ def _set_optional_attributes_of_profile(config, profile):

def _read_user_instance_mappings(config, profile):
instance_section = profile + "instancemap"
config.add_section(instance_section)
if not config.has_section(instance_section):
config.add_section(instance_section)

while True:
key = raw_input(
@@ -156,7 +160,8 @@ def _read_user_instance_mappings(config, profile):

def _read_user_resource_type_mappings(config, profile):
resource_section = profile + "resourcemap"
config.add_section(resource_section)
if not config.has_section(resource_section):
config.add_section(resource_section)

while True:
key = raw_input(

0 comments on commit 193adc9

Please sign in to comment.