This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
configure.py
300 lines (232 loc) · 8.72 KB
/
configure.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/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 provides functions to generate an ec2stack configuration file.
"""
import os
import argparse
from ConfigParser import SafeConfigParser
def main():
"""
Entry point into the configuration application.
"""
config_folder = _create_config_folder()
_create_config_file(config_folder)
def _create_config_folder():
"""
Creates a folder to hold the user's configuration files.
@return: Path of the configuration folder.
"""
config_folder = os.path.join(os.path.expanduser('~'), '.ec2stack')
if not os.path.exists(config_folder):
os.makedirs(config_folder)
os.chmod(config_folder, 0700)
return config_folder
def _create_config_file(config_folder):
"""
Reads in configuration items and writes them out to the configuration file.
@param config_folder: Path of the configuration folder.
"""
args = _generate_args()
profile = args.pop('profile')
advanced_network_enabled = args.pop('advanced')
config_file_path = config_folder + '/ec2stack.conf'
config = _modify_config_profile(
config_file_path, profile, advanced_network_enabled)
config_file = open(config_file_path, 'w+')
config.write(config_file)
def _generate_args():
"""
Generate command line arguments for ec2stack-configure.
@return: args.
"""
parser = argparse.ArgumentParser(
'Command line utility for configuring ec2stack'
)
parser.add_argument(
'-p',
'--profile',
required=False,
help='The profile to configure, default is initial',
default='initial'
)
parser.add_argument(
'-a',
'--advanced',
required=False,
help='Turn advanced network config on for application',
default=False
)
args = parser.parse_args()
return vars(args)
def _modify_config_profile(config_file, profile, advanced_network_enabled):
"""
Modify configuration profile.
@param config_file: current config file configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
config = SafeConfigParser()
config.read(config_file)
if not config.has_section(profile):
config.add_section(profile)
config = _set_mandatory_attributes_of_profile(config, profile)
if advanced_network_enabled:
config = _set_advanced_network_attributes_of_profile(config, profile)
config = _set_optional_attributes_of_profile(config, profile)
return config
def _set_mandatory_attributes_of_profile(config, profile):
"""
Modify mandatory attributes of profile.
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
config = _set_attribute_of_profile(
config, profile, 'ec2stack_bind_address', 'EC2Stack bind address', 'localhost'
)
config = _set_attribute_of_profile(
config, profile, 'ec2stack_port', 'EC2Stack bind port', '5000'
)
config = _set_attribute_of_profile(
config, profile, 'cloudstack_host', 'Cloudstack host', 'localhost'
)
config = _set_attribute_of_profile(
config, profile, 'cloudstack_port', 'Cloudstack port', '8080'
)
config = _set_attribute_of_profile(
config, profile, 'cloudstack_protocol', 'Cloudstack protocol', 'http'
)
config = _set_attribute_of_profile(
config, profile, 'cloudstack_path', 'Cloudstack path', '/client/api'
)
config = _set_attribute_of_profile(
config, profile, 'cloudstack_custom_disk_offering', 'Cloudstack custom disk offering name', 'Custom'
)
while True:
config = _set_attribute_of_profile(
config, profile, 'cloudstack_default_zone', 'Cloudstack default zone name', ''
)
if config.get(profile, 'cloudstack_default_zone') is not '':
break
return config
def _set_advanced_network_attributes_of_profile(config, profile):
"""
Modify advanced network attributes of profile.
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
config = _set_attribute_of_profile(
config, profile, 'vpc_offering_id', 'VPC offering id', ''
)
return config
def _set_optional_attributes_of_profile(config, profile):
"""
Modify optional attributes of profile.
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
configure_instance_type_mapings = raw_input(
'Do you wish to input instance type mappings? (Yes/No): '
)
if configure_instance_type_mapings.lower() in ['yes', 'y']:
config = _read_user_instance_mappings(config, profile)
configure_resource_type_mapings = raw_input(
'Do you wish to input resource type to resource id mappings' +
' for tag support? (Yes/No): '
)
if configure_resource_type_mapings.lower() in ['yes', 'y']:
config = _read_user_resource_type_mappings(config, profile)
return config
def _read_user_instance_mappings(config, profile):
"""
Add instance type mappings to profile.
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
instance_section = profile + "instancemap"
if not config.has_section(instance_section):
config.add_section(instance_section)
while True:
key = raw_input(
'Insert the AWS EC2 instance type you wish to map: '
)
value = raw_input(
'Insert the name of the instance type you wish to map this to: '
)
config.set(instance_section, key, value)
add_more = raw_input(
'Do you wish to add more mappings? (Yes/No): ')
if add_more.lower() in ['no', 'n']:
break
return config
def _read_user_resource_type_mappings(config, profile):
"""
Add resource type mappings to profile.
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@return: configparser configuration.
"""
resource_section = profile + "resourcemap"
if not config.has_section(resource_section):
config.add_section(resource_section)
while True:
key = raw_input(
'Insert the cloudstack resource id you wish to map: '
)
value = raw_input(
'Insert the cloudstack resource type you wish to map this to: '
)
config.set(resource_section, key, value)
add_more = raw_input(
'Do you wish to add more mappings? (Yes/No): ')
if add_more.lower() in ['no', 'n']:
break
return config
def _set_attribute_of_profile(config, profile, attribute, message, default):
"""
Set attribute of profile
@param config: current configparser configuration.
@param profile: the profile to set the attribute in.
@param attribute: the attribute to set.
@param message: the message to prompt the user with.
@param default: the default value to use if none is entered.
@return: configparser configuration.
"""
if config.has_option(profile, attribute):
default = config.get(profile, attribute)
attribute_value = _read_in_config_attribute_or_use_default(
message, default)
config.set(profile, attribute, attribute_value)
return config
def _read_in_config_attribute_or_use_default(message, default):
"""
Add resource type mappings to profile.
@param message: the message to prompt the user with.
@param default: the default value to use if none is entered.
@return: configparser configuration.
"""
attribute = raw_input(message + ' [' + default + ']: ')
if attribute == '':
attribute = default
return attribute