forked from apache/cloudstack-ec2stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
173 lines (135 loc) · 4.82 KB
/
__init__.py
File metadata and controls
173 lines (135 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# encoding: utf-8
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
"""This module creates the flask application.
"""
import os
import sys
import argparse
from alembic import command
from alembic.config import Config as AlembicConfig
from flask import Flask
from ConfigParser import SafeConfigParser
from ec2stack.controllers import *
from ec2stack.core import DB
from ec2stack.models import User
def create_app(settings=None):
"""
Creates a flask application.
@param settings: Settings override object.
@return: The flask application.
"""
app = Flask(__name__)
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()
database_uri = _load_database()
_config_from_config_profile(config_file, profile, app)
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri
DB.init_app(app)
default_controller = __import__(
'ec2stack.controllers.' + 'default', None, None, 'DEFAULT'
)
default_controller = getattr(default_controller, 'DEFAULT')
app.register_blueprint(default_controller)
return app
def _generate_args():
"""
Generate command line arguments for ec2stack-configure.
@return: args.
"""
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.
@return: The path to the user's configuration file.
"""
config_file = os.path.join(
os.path.expanduser('~'),
'.ec2stack/ec2stack.conf'
)
if not os.path.exists(config_file):
sys.exit('No configuration found, please run ec2stack-configure')
return config_file
def _config_from_config_profile(config_file, profile, app):
"""
Configures ec2stack app based on configuration profile.
@param config_file: current config file configuration.
@param profile: the profile to set the attribute in.
"""
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_section):
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():
"""
Checks that the user's database exists and returns its uri.
@return: The uri to the user's database.
"""
database_file = os.path.join(
os.path.expanduser('~'),
'.ec2stack/ec2stack.sqlite'
)
if not os.path.exists(database_file):
directory = os.path.join(os.path.dirname(__file__), '../migrations')
config = AlembicConfig(os.path.join(
directory,
'alembic.ini'
))
config.set_main_option('script_location', directory)
command.upgrade(config, 'head', sql=False, tag=None)
return 'sqlite:///' + database_file