/
config.py
738 lines (662 loc) · 26.9 KB
/
config.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
import os
import urllib
import StringIO
import ConfigParser
from starcluster import utils
from starcluster import static
from starcluster import cluster
from starcluster import awsutils
from starcluster import deathrow
from starcluster import exception
from starcluster.cluster import Cluster
from starcluster.utils import AttributeDict
from starcluster.logger import log
DEBUG_CONFIG = False
def get_easy_s3(config_file=None, cache=False):
"""
Factory for EasyS3 class that attempts to load AWS credentials from
the StarCluster config file. Returns an EasyS3 object if
successful.
"""
cfg = get_config(config_file, cache)
return cfg.get_easy_s3()
def get_easy_ec2(config_file=None, cache=False):
"""
Factory for EasyEC2 class that attempts to load AWS credentials from
the StarCluster config file. Returns an EasyEC2 object if
successful.
"""
cfg = get_config(config_file, cache)
return cfg.get_easy_ec2()
def get_cluster_manager(config_file=None, cache=False):
"""
Factory for ClusterManager class that attempts to load AWS credentials from
the StarCluster config file. Returns a ClusterManager object if successful
"""
cfg = get_config(config_file, cache)
return cfg.get_cluster_manager()
def get_config(config_file=None, cache=False):
"""Factory for StarClusterConfig object"""
return StarClusterConfig(config_file, cache).load()
class StarClusterConfig(object):
"""
Loads StarCluster configuration settings defined in config_file
which defaults to ~/.starclustercfg
Settings are available as follows:
cfg = StarClusterConfig()
or
cfg = StarClusterConfig('/path/to/my/config.cfg')
cfg.load()
aws_info = cfg.aws
cluster_cfg = cfg.clusters['mycluster']
key_cfg = cfg.keys['gsg-keypair']
print cluster_cfg
"""
global_settings = static.GLOBAL_SETTINGS
aws_settings = static.AWS_SETTINGS
key_settings = static.KEY_SETTINGS
volume_settings = static.EBS_VOLUME_SETTINGS
plugin_settings = static.PLUGIN_SETTINGS
cluster_settings = static.CLUSTER_SETTINGS
permission_settings = static.PERMISSION_SETTINGS
# until i can find a way to query AWS for instance types...
instance_types = static.INSTANCE_TYPES
def __init__(self, config_file=None, cache=False):
self.cfg_file = config_file or static.STARCLUSTER_CFG_FILE
self.cfg_file = os.path.expanduser(self.cfg_file)
self.cfg_file = os.path.expandvars(self.cfg_file)
self.type_validators = {
int: self._get_int,
float: self._get_float,
str: self._get_string,
bool: self._get_bool,
list: self._get_list,
}
self._config = None
self.globals = AttributeDict()
self.aws = AttributeDict()
self.clusters = AttributeDict()
self.keys = AttributeDict()
self.vols = AttributeDict()
self.plugins = AttributeDict()
self.permissions = AttributeDict()
self.cache = cache
def __repr__(self):
return "<StarClusterConfig: %s>" % self.cfg_file
def _get_urlfp(self, url):
log.debug("Loading url: %s" % url)
try:
fp = urllib.urlopen(url)
if fp.getcode() == 404:
raise exception.ConfigError("url %s does not exist" % url)
fp.name = url
return fp
except IOError, e:
raise exception.ConfigError(
"error loading config from url %s\n%s" % (url, e))
def _get_fp(self, cfg_file):
log.debug("Loading file: %s" % cfg_file)
if os.path.exists(cfg_file):
if not os.path.isfile(cfg_file):
raise exception.ConfigError(
'config %s exists but is not a regular file' % cfg_file)
else:
raise exception.ConfigNotFound("config file %s does not exist\n" %
cfg_file, cfg_file)
return open(cfg_file)
def _get_cfg_fp(self, cfg_file=None):
cfg = cfg_file or self.cfg_file
if utils.is_url(cfg):
return self._get_urlfp(cfg)
else:
return self._get_fp(cfg)
def _get_bool(self, config, section, option):
try:
opt = config.getboolean(section, option)
return opt
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
except ValueError:
raise exception.ConfigError(
"Expected True/False value for setting %s in section [%s]" %
(option, section))
def _get_int(self, config, section, option):
try:
opt = config.getint(section, option)
return opt
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
except ValueError:
raise exception.ConfigError(
"Expected integer value for setting %s in section [%s]" %
(option, section))
def _get_float(self, config, section, option):
try:
opt = config.getfloat(section, option)
return opt
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
except ValueError:
raise exception.ConfigError(
"Expected float value for setting %s in section [%s]" %
(option, section))
def _get_string(self, config, section, option):
try:
opt = config.get(section, option)
return opt
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
def _get_list(self, config, section, option):
val = self._get_string(config, section, option)
if val:
val = [v.strip() for v in val.split(',')]
return val
def __load_config(self):
"""
Populates self._config with a new ConfigParser instance
"""
cfg = self._get_cfg_fp()
try:
cp = InlineCommentsIgnoredConfigParser()
cp.readfp(cfg)
self._config = cp
try:
self.globals = self._load_section('global',
self.global_settings)
includes = self.globals.get('include')
if not includes:
return cp
mashup = StringIO.StringIO()
cfg = self._get_cfg_fp()
mashup.write(cfg.read() + '\n')
for include in includes:
include = os.path.expanduser(include)
include = os.path.expandvars(include)
try:
contents = self._get_cfg_fp(include).read()
mashup.write(contents + '\n')
except exception.ConfigNotFound:
raise exception.ConfigError("include %s not found" %
include)
mashup.seek(0)
cp = InlineCommentsIgnoredConfigParser()
cp.readfp(mashup)
self._config = cp
except exception.ConfigSectionMissing:
pass
return cp
except ConfigParser.MissingSectionHeaderError:
raise exception.ConfigHasNoSections(cfg.name)
except ConfigParser.ParsingError, e:
raise exception.ConfigError(e)
def reload(self):
"""
Reloads the configuration file
"""
self.__load_config()
return self.load()
@property
def config(self):
if self._config is None:
self._config = self.__load_config()
return self._config
def _load_settings(self, section_name, settings, store,
filter_settings=True):
"""
Load section settings into a dictionary
"""
section = self.config._sections.get(section_name)
if not section:
raise exception.ConfigSectionMissing(
'Missing section %s in config' % section_name)
store.update(section)
section_conf = store
for setting in settings:
requirements = settings[setting]
func, required, default, options, callback = requirements
func = self.type_validators.get(func)
value = func(self.config, section_name, setting)
if value is not None:
if options and not value in options:
raise exception.ConfigError(
'"%s" setting in section "%s" must be one of: %s' %
(setting, section_name,
', '.join([str(o) for o in options])))
if callback:
value = callback(value)
section_conf[setting] = value
if filter_settings:
for key in store.keys():
if key not in settings and key != '__name__':
store.pop(key)
def _check_required(self, section_name, settings, store):
"""
Check that all required settings were specified in the config.
Raises ConfigError otherwise.
Note that if a setting specified has required=True and
default is not None then this method will not raise an error
because a default was given. In short, if a setting is required
you must provide None as the 'default' value.
"""
section_conf = store
for setting in settings:
requirements = settings[setting]
required = requirements[1]
value = section_conf.get(setting)
if value is None and required:
raise exception.ConfigError(
'missing required option %s in section "%s"' %
(setting, section_name))
def _load_defaults(self, settings, store):
"""
Sets the default for each setting in settings regardless of whether
the setting was specified in the config or not.
"""
section_conf = store
for setting in settings:
default = settings[setting][2]
if section_conf.get(setting) is None:
if DEBUG_CONFIG:
log.debug('%s setting not specified. Defaulting to %s' %
(setting, default))
section_conf[setting] = default
def _load_extends_settings(self, section_name, store):
"""
Loads all settings from other template(s) specified by a section's
'extends' setting.
This method walks a dependency tree of sections from bottom up. Each
step is a group of settings for a section in the form of a dictionary.
A 'master' dictionary is updated with the settings at each step. This
causes the next group of settings to override the previous, and so on.
The 'section_name' settings are at the top of the dependency tree.
"""
section = store[section_name]
extends = section.get('extends')
if extends is None:
return
if DEBUG_CONFIG:
log.debug('%s extends %s' % (section_name, extends))
extensions = [section]
while extends is not None:
try:
section = store[extends]
if section in extensions:
exts = ', '.join([self._get_section_name(x['__name__'])
for x in extensions])
raise exception.ConfigError(
"Cyclical dependency between sections %s. "
"Check your EXTENDS settings." % exts)
extensions.insert(0, section)
except KeyError:
raise exception.ConfigError(
"%s can't extend non-existent section %s" %
(section_name, extends))
extends = section.get('extends')
transform = AttributeDict()
for extension in extensions:
transform.update(extension)
store[section_name] = transform
def _load_keypairs(self, store):
cluster_section = store
keyname = cluster_section.get('keyname')
if not keyname:
return
keypair = self.keys.get(keyname)
if keypair is None:
raise exception.ConfigError(
"keypair '%s' not defined in config" % keyname)
cluster_section['keyname'] = keyname
cluster_section['key_location'] = keypair.get('key_location')
def _load_volumes(self, store):
cluster_section = store
volumes = cluster_section.get('volumes')
if not volumes or isinstance(volumes, AttributeDict):
return
vols = AttributeDict()
cluster_section['volumes'] = vols
for volume in volumes:
if not volume in self.vols:
raise exception.ConfigError(
"volume '%s' not defined in config" % volume)
vol = self.vols.get(volume).copy()
del vol['__name__']
vols[volume] = vol
def _load_plugins(self, store):
cluster_section = store
plugins = cluster_section.get('plugins')
if not plugins or isinstance(plugins[0], AttributeDict):
return
plugs = []
for plugin in plugins:
if not plugin in self.plugins:
raise exception.ConfigError(
"plugin '%s' not defined in config" % plugin)
plugs.append(self.plugins.get(plugin))
cluster_section['plugins'] = plugs
def _load_permissions(self, store):
cluster_section = store
permissions = cluster_section.get('permissions')
if not permissions or isinstance(permissions, AttributeDict):
return
perms = AttributeDict()
cluster_section['permissions'] = perms
for perm in permissions:
if perm in self.permissions:
p = self.permissions.get(perm)
p['__name__'] = p['__name__'].split()[-1]
perms[perm] = p
else:
raise exception.ConfigError(
"permission '%s' not defined in config" % perm)
def _load_instance_types(self, store):
cluster_section = store
instance_types = cluster_section.get('node_instance_type')
if isinstance(instance_types, basestring):
return
itypes = []
cluster_section['node_instance_types'] = itypes
total_num_nodes = 0
choices_string = ', '.join(static.INSTANCE_TYPES.keys())
try:
default_instance_type = instance_types[-1]
if not default_instance_type in static.INSTANCE_TYPES:
raise exception.ConfigError(
"invalid node_instance_type specified: '%s'\n"
"must be one of: %s" %
(default_instance_type, choices_string))
except IndexError:
default_instance_type = None
cluster_section['node_instance_type'] = default_instance_type
for type_spec in instance_types[:-1]:
type_spec = type_spec.split(':')
if len(type_spec) > 3:
raise exception.ConfigError(
"invalid node_instance_type item specified: %s" %
type_spec)
itype = type_spec[0]
itype_image = None
itype_num = 1
if not itype in static.INSTANCE_TYPES:
raise exception.ConfigError(
"invalid type specified (%s) in node_instance_type "
"item: '%s'\nmust be one of: %s" %
(itype, type_spec, choices_string))
if len(type_spec) == 2:
itype, next_var = type_spec
try:
itype_num = int(next_var)
except (TypeError, ValueError):
itype_image = next_var
elif len(type_spec) == 3:
itype, itype_image, itype_num = type_spec
try:
itype_num = int(itype_num)
if itype_num < 1:
raise TypeError
total_num_nodes += itype_num
except (ValueError, TypeError):
raise exception.ConfigError(
"number of instances (%s) of type '%s' must "
"be an integer > 1" % (itype_num, itype))
itype_dic = AttributeDict(size=itype_num, image=itype_image,
type=itype)
itypes.append(itype_dic)
def _load_section(self, section_name, section_settings,
filter_settings=True):
"""
Returns a dictionary containing all section_settings for a given
section_name by first loading the settings in the config, loading
the defaults for all settings not specified, and then checking
that all required options have been specified
"""
store = AttributeDict()
self._load_settings(section_name, section_settings, store,
filter_settings)
self._load_defaults(section_settings, store)
self._check_required(section_name, section_settings, store)
return store
def _get_section_name(self, section):
"""
Returns section name minus prefix
e.g.
$ print self._get_section('cluster smallcluster')
$ smallcluster
"""
return section.split()[1]
def _get_sections(self, section_prefix):
"""
Returns all sections starting with section_prefix
e.g.
$ print self._get_sections('cluster')
$ ['cluster smallcluster', 'cluster mediumcluster', ..]
"""
return [s for s in self.config.sections() if
s.startswith(section_prefix)]
def _load_sections(self, section_prefix, section_settings,
filter_settings=True):
"""
Loads all sections starting with section_prefix and returns a
dictionary containing the name and dictionary of settings for each
section.
keys --> section name (as returned by self._get_section_name)
values --> dictionary of settings for a given section
e.g.
$ print self._load_sections('volumes', self.plugin_settings)
{'myvol': {'__name__': 'volume myvol',
'device': None,
'mount_path': '/home',
'partition': 1,
'volume_id': 'vol-999999'},
'myvol2': {'__name__': 'volume myvol2',
'device': None,
'mount_path': '/myvol2',
'partition': 1,
'volume_id': 'vol-999999'},
"""
sections = self._get_sections(section_prefix)
sections_store = AttributeDict()
for sec in sections:
name = self._get_section_name(sec)
sections_store[name] = self._load_section(sec, section_settings,
filter_settings)
return sections_store
def _load_cluster_sections(self, cluster_sections):
"""
Loads all cluster sections. Similar to _load_sections but also handles
populating specified keypair, volume, plugins, permissions, etc.
settings
"""
clusters = cluster_sections
cluster_store = AttributeDict()
for cl in clusters:
name = self._get_section_name(cl)
cluster_store[name] = AttributeDict()
self._load_settings(cl, self.cluster_settings, cluster_store[name])
for cl in clusters:
name = self._get_section_name(cl)
self._load_extends_settings(name, cluster_store)
self._load_defaults(self.cluster_settings, cluster_store[name])
self._load_keypairs(cluster_store[name])
self._load_volumes(cluster_store[name])
self._load_plugins(cluster_store[name])
self._load_permissions(cluster_store[name])
self._load_instance_types(cluster_store[name])
self._check_required(cl, self.cluster_settings,
cluster_store[name])
return cluster_store
def load(self):
"""
Populate this config object from the StarCluster config
"""
log.debug('Loading config')
try:
self.globals = self._load_section('global', self.global_settings)
except exception.ConfigSectionMissing:
pass
try:
self.aws = self._load_section('aws info', self.aws_settings)
except exception.ConfigSectionMissing:
log.warn("no [aws info] section found in config")
log.warn("attempting to load credentials from environment...")
self.aws.update(self.get_aws_from_environ())
self.keys = self._load_sections('key', self.key_settings)
self.vols = self._load_sections('volume', self.volume_settings)
self.plugins = self._load_sections('plugin', self.plugin_settings,
filter_settings=False)
self.permissions = self._load_sections('permission',
self.permission_settings)
sections = self._get_sections('cluster')
self.clusters = self._load_cluster_sections(sections)
return self
def get_aws_from_environ(self):
"""
Returns AWS credentials defined in the user's shell
environment.
"""
awscreds = {}
for key in static.AWS_SETTINGS:
if key.upper() in os.environ:
awscreds[key] = os.environ.get(key.upper())
elif key in os.environ:
awscreds[key] = os.environ.get(key)
return awscreds
def get_aws_credentials(self):
"""
Returns AWS credentials defined in the configuration
file. Defining any of the AWS settings in the environment
overrides the configuration file.
"""
# first override with environment settings if they exist
self.aws.update(self.get_aws_from_environ())
return self.aws
def get_cluster_template(self, template_name, tag_name=None,
ec2_conn=None):
"""
Returns Cluster instance configured with the settings in the
config file.
template_name is the name of a cluster section defined in the config
tag_name if not specified will be set to template_name
"""
try:
kwargs = {}
tag_name = tag_name or template_name
kwargs.update(dict(cluster_tag=tag_name))
kwargs.update(self.clusters[template_name])
plugs = kwargs.get('plugins')
kwargs['plugins'] = deathrow._load_plugins(plugs,
debug=DEBUG_CONFIG)
if not ec2_conn:
ec2_conn = self.get_easy_ec2()
clust = Cluster(ec2_conn, **kwargs)
return clust
except KeyError:
raise exception.ClusterTemplateDoesNotExist(template_name)
def get_default_cluster_template(self):
"""
Returns the default_template specified in the [global] section
of the config. Raises NoDefaultTemplateFound if no default cluster
template has been specified in the config.
"""
default = self.globals.get('default_template')
if not default:
raise exception.NoDefaultTemplateFound(
options=self.clusters.keys())
if not default in self.clusters:
raise exception.ClusterTemplateDoesNotExist(default)
return default
def get_clusters(self):
clusters = []
for cl in self.clusters:
clusters.append(self.get_cluster_template(cl, tag_name=cl))
return clusters
def get_plugin(self, plugin):
try:
return self.plugins[plugin]
except KeyError:
raise exception.PluginNotFound(plugin)
def get_key(self, keyname):
try:
return self.keys[keyname]
except KeyError:
raise exception.KeyNotFound(keyname)
def get_easy_s3(self):
"""
Factory for EasyEC2 class that attempts to load AWS credentials from
the StarCluster config file. Returns an EasyS3 object if
successful.
"""
aws = self.get_aws_credentials()
try:
s3 = awsutils.EasyS3(**aws)
return s3
except TypeError:
raise exception.ConfigError("no aws credentials found")
def get_easy_ec2(self):
"""
Factory for EasyEC2 class that attempts to load AWS credentials from
the StarCluster config file. Returns an EasyEC2 object if
successful.
"""
aws = self.get_aws_credentials()
try:
ec2 = awsutils.EasyEC2(**aws)
return ec2
except TypeError:
raise exception.ConfigError("no aws credentials found")
def get_cluster_manager(self):
ec2 = self.get_easy_ec2()
return cluster.ClusterManager(self, ec2)
class InlineCommentsIgnoredConfigParser(ConfigParser.ConfigParser):
"""
Class for custom config file parsing that ignores inline comments.
By default, ConfigParser.ConfigParser only ignores inline comments denoted
by a semicolon. This class extends this support to allow inline comments
denoted by '#' as well. Just as with semicolons, a spacing character must
precede the pound sign for it to be considered an inline comment.
For example, the following line would have the inline comment ignored:
FOO = bar # some comment...
And would be parsed as:
FOO = bar
The following would NOT have the comment removed:
FOO = bar# some comment...
"""
def readfp(self, fp, filename=None):
"""
Overrides ConfigParser.ConfigParser.readfp() to ignore inline comments.
"""
if filename is None:
try:
filename = fp.name
except AttributeError:
filename = '<???>'
# We don't use the file iterator here because ConfigParser.readfp()
# guarantees to only call readline() on fp, so we want to adhere to
# this as well.
commentless_fp = StringIO.StringIO()
line = fp.readline()
while line:
pound_pos = line.find('#')
# A pound sign only starts an inline comment if it is preceded by
# whitespace.
if pound_pos > 0 and line[pound_pos - 1].isspace():
line = line[:pound_pos].rstrip() + '\n'
commentless_fp.write(line)
line = fp.readline()
commentless_fp.seek(0)
# Cannot use super() because ConfigParser is not a new-style class.
ConfigParser.ConfigParser.readfp(self, commentless_fp, filename)
if __name__ == "__main__":
from pprint import pprint
cfg = StarClusterConfig().load()
pprint(cfg.aws)
pprint(cfg.clusters)
pprint(cfg.keys)
pprint(cfg.vols)