-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.py
135 lines (99 loc) · 3.34 KB
/
main.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
import os
import signal
import sys
import dcoscli
import docopt
import six
from dcos import constants, emitting, errors, http, subcommand, util
from dcos.errors import DCOSException
from dcoscli.subcommand import SubcommandMain, default_doc
from six.moves import urllib
logger = util.get_logger(__name__)
emitter = emitting.FlatEmitter()
def main():
try:
return _main()
except DCOSException as e:
emitter.publish(e)
return 1
def _get_versions(dcos_url):
"""Print DCOS and DCOS-CLI versions
:param dcos_url: url to DCOS cluster
:type dcos_url: str
:returns: Process status
:rtype: int
"""
dcos_info = {}
try:
url = urllib.parse.urljoin(
dcos_url, 'dcos-metadata/dcos-version.json')
res = http.get(url, timeout=1)
if res.status_code == 200:
dcos_info = res.json()
except Exception as e:
logger.exception(e)
pass
emitter.publish(
"dcoscli.version={}\n".format(dcoscli.version) +
"dcos.version={}\n".format(dcos_info.get("version", "N/A")) +
"dcos.commit={}\n".format(dcos_info.get(
"dcos-image-commit", "N/A")) +
"dcos.bootstrap-id={}".format(dcos_info.get("bootstrap-id", "N/A"))
)
return 0
def _main():
signal.signal(signal.SIGINT, signal_handler)
http.silence_requests_warnings()
config = util.get_config()
set_ssl_info_env_vars(config)
args = docopt.docopt(default_doc("dcos"), options_first=True)
log_level = args['--log-level']
if log_level and not _config_log_level_environ(log_level):
return 1
if args['--debug']:
os.environ[constants.DCOS_DEBUG_ENV] = 'true'
util.configure_process_from_environ()
if args['--version']:
return _get_versions(config.get("core.dcos_url"))
command = args['<command>']
if not command:
command = "help"
if command in subcommand.default_subcommands():
sc = SubcommandMain(command, args['<args>'])
else:
executable = subcommand.command_executables(command)
sc = subcommand.SubcommandProcess(
executable, command, args['<args>'])
exitcode, _ = sc.run_and_capture()
return exitcode
def _config_log_level_environ(log_level):
"""
:param log_level: Log level to set
:type log_level: str
:returns: True if the log level was configured correctly; False otherwise.
:rtype: bool
"""
log_level = log_level.lower()
if log_level in constants.VALID_LOG_LEVEL_VALUES:
os.environ[constants.DCOS_LOG_LEVEL_ENV] = log_level
return True
msg = 'Log level set to an unknown value {!r}. Valid values are {!r}'
emitter.publish(msg.format(log_level, constants.VALID_LOG_LEVEL_VALUES))
return False
def signal_handler(signal, frame):
emitter.publish(
errors.DefaultError("User interrupted command with Ctrl-C"))
sys.exit(0)
def set_ssl_info_env_vars(config):
"""Set SSL info from config to environment variable if enviornment
variable doesn't exist
:param config: config
:type config: Toml
:rtype: None
"""
if 'core.ssl_verify' in config and (
not os.environ.get(constants.DCOS_SSL_VERIFY_ENV)):
os.environ[constants.DCOS_SSL_VERIFY_ENV] = six.text_type(
config['core.ssl_verify'])
if __name__ == "__main__":
main()