-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
76 lines (70 loc) · 3.11 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
from fabric.api import env
from . import warn,error
import sys
# -------------- Configuration helpers ------------------
def configure():
"""Runs configuration, if it has not already run.
All config settings in env.defaults are applied to env, if not already set.
All config keys listed in env.ints have their values converted to ints.
env.provider_config_functions are tried until one succeeds or fails. E.g. env.provider_config_functions = [aws_config, gce_config]
If a provider_config_function reports that it is selected but failed, sys.exit(1)
"""
if env.get("configured") is None:
if not "defaults" in env:
env.defaults = {}
env.defaults['num_nodes'] = 1
env.defaults['provisioning_timeout'] = 300
if not "ints" in env:
env.ints = []
env.ints.append('num_nodes')
env.ints.append('provisioning_timeout')
_apply_defaults_()
env.configured = False
if env.provider_config_functions and len(env.provider_config_functions) > 0:
for f in env.provider_config_functions:
result = f()
if result is None:
# try another provider
pass
elif result:
# successfully configured this provider
env.configured = True
break
else:
# This provider was selected, but configuration failed.
# Error should have already been logged by provider_config_function.
sys.exit(1)
if not env.configured:
warn("No cloud provider config functions were supplied via env.provider_config_functions and/or no provider was selected via env.provider")
# This might have been intentional for non-cloud use, so don't abort
env.configured = True
if not "group" in env:
env.group = env.user
return True
def _apply_defaults_():
"Apply default values for optional settings"
if "defaults" in env:
for k in env.defaults:
if not k in env:
env[k] = env.defaults[k]
# Force numeric conversions to make easier use later on
if "ints" in env:
for k in env.ints:
env[k] = int(env[k])
# Force boolean conversions to make easier use later on
if "bools" in env:
for k in env.bools:
if k in env:
env[k] = env[k].lower() in ("y","yes","t","true","1","enabled","on")
else:
env[k] = False
def verify_env_contains_keys(*keys):
"""Returns true if env contains the specified key(s). Logs error and returns false otherwise."""
missing = filter(lambda key : key not in env, keys)
if missing:
# create key1=val1,key2=val2,...
set_arg_example = ",".join(map(lambda x: x[0]+"=val%d" % x[1], zip(keys,range(1,len(keys)+1))))
error("Unspecified parameters: %s. Please run with --set %s or specify in config file and run with fab -c <configfile>" % (", ".join(missing), set_arg_example))
return False
else:
return True