-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor
executable file
·371 lines (319 loc) · 12.9 KB
/
monitor
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Author: Alberto Planas <[email protected]>
#
# Copyright 2019 SUSE LINUX GmbH, Nuernberg, Germany.
#
# 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.
import argparse
import getpass
import json
import logging
import os
from pathlib import Path
import pprint
import ssl
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
LOG = logging.getLogger(__name__)
TOKEN_FILE = '~/.salt-api-token'
class SaltAPI():
def __init__(self, url, username, password, eauth, insecure,
token_file=TOKEN_FILE, debug=False):
self.url = url
self.username = username
self.password = password
self.eauth = eauth
self.insecure = insecure
self.token_file = token_file
self.debug = debug
is_https = urllib.parse.urlparse(url).scheme == 'https'
if debug or (is_https and insecure):
if insecure:
context = ssl._create_unverified_context()
handler = urllib.request.HTTPSHandler(context=context,
debuglevel=int(debug))
else:
handler = urllib.request.HTTPHandler(debuglevel=int(debug))
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
self.token = None
self.expire = 0.0
def login(self, remove=False):
"""Login into the Salt API service."""
if remove:
self._drop_token()
self.token, self.expire = self._read_token()
if self.expire < time.time() + 30:
self.token, self.expire = self._login()
self._write_token()
def logout(self):
"""Logout from the Salt API service."""
self._drop_token()
self._post('/logout')
def events(self):
"""SSE event stream from Salt API service."""
for line in api._req_sse('/events', None, 'GET'):
line = line.decode('utf-8').strip()
if not line or line.startswith((':', 'retry:')):
continue
key, value = line.split(':', 1)
if key == 'tag':
tag = value.strip()
continue
if key == 'data':
data = json.loads(value)
yield (tag, data)
def minions(self, mid=None):
"""Return the list of minions."""
if mid:
action = '/minions/{}'.format(mid)
else:
action = '/minions'
return self._get(action)['return'][0]
def run_job(self, tgt, fun, **kwargs):
"""Start an execution command and return jid."""
data = {
'tgt': tgt,
'fun': fun,
}
data.update(kwargs)
return self._post('/minions', data)['return'][0]
def jobs(self, jid=None):
"""Return the list of jobs."""
if jid:
action = '/jobs/{}'.format(jid)
else:
action = '/jobs'
return self._get(action)['return'][0]
def stats(self):
"""Return a dump of statistics."""
return self._get('/stats')['return'][0]
def _login(self):
"""Login into the Salt API service."""
data = {
'username': self.username,
'password': self.password,
'eauth': self.eauth,
}
result = self._post('/login', data)
return result['return'][0]['token'], result['return'][0]['expire']
def _get(self, action, data=None):
return self._req(action, data, 'GET')
def _post(self, action, data=None):
return self._req(action, data, 'POST')
def _req(self, action, data, method):
"""HTTP GET / POST to Salt API."""
headers = {
'User-Agent': 'salt-autoinstaller monitor',
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
}
if self.token:
headers['X-Auth-Token'] = self.token
url = urllib.parse.urljoin(self.url, action)
if method == 'GET':
data = urllib.parse.urlencode(data).encode() if data else None
if data:
url = '{}?{}'.format(url, data)
data = None
elif method == 'POST':
data = json.dumps(data).encode() if data else {}
else:
raise ValueError('Method {} not valid'.format(method))
result = {}
try:
request = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(request) as response:
result = json.loads(response.read().decode('utf-8'))
except (urllib.error.HTTPError, urllib.error.URLError) as exc:
LOG.debug('Error with request', exc_info=True)
status = getattr(exc, 'code', None)
if status == 401:
print('Authentication denied')
if status == 500:
print('Server error.')
exit(-1)
return result
def _req_sse(self, action, data, method):
"""HTTP SSE GET / POST to Salt API."""
headers = {
'User-Agent': 'salt-autoinstaller monitor',
'Accept': 'text/event-stream',
'Content-Type': 'application/json',
'Connection': 'Keep-Alive',
'X-Requested-With': 'XMLHttpRequest',
}
if self.token:
headers['X-Auth-Token'] = self.token
url = urllib.parse.urljoin(self.url, action)
if method == 'GET':
data = urllib.parse.urlencode(data).encode() if data else None
if data:
url = '{}?{}'.format(url, data)
data = None
elif method == 'POST':
data = json.dumps(data).encode() if data else {}
else:
raise ValueError('Method {} not valid'.format(method))
try:
request = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(request) as response:
yield from response
except (urllib.error.HTTPError, urllib.error.URLError) as e:
LOG.debug('Error with request', exc_info=True)
status = getattr(e, 'code', None)
if status == 401:
print('Authentication denied')
if status == 500:
print('Server error.')
exit(-1)
def _read_token(self):
"""Return the token and expire time from the token file."""
token, expire = None, 0.0
if self.token_file:
token_path = Path(self.token_file).expanduser()
if token_path.is_file():
token, expire = token_path.read_text().split()
try:
expire = float(expire)
except ValueError:
expire = 0.0
return token, expire
def _write_token(self):
"""Save the token and expire time into the token file."""
self._drop_token()
if self.token_file:
token_path = Path(self.token_file).expanduser()
token_path.touch(mode=0o600)
token_path.write_text('{} {}'.format(self.token, self.expire))
def _drop_token(self):
"""Remove the token file if present."""
if self.token_file:
token_path = Path(self.token_file).expanduser()
if token_path.is_file():
token_path.unlink()
def print_minions(minions):
"""Print a list of minions."""
print('Registered minions:')
for minion in minions:
print('- {}'.format(minion))
def print_minion(minion):
"""Print detailed information of a minion."""
pprint.pprint(minion)
def print_jobs(jobs):
"""Print a list of jobs."""
print('Registered jobs:')
for job, info in jobs.items():
print('- {}'.format(job))
pprint.pprint(info)
def print_job(job):
"""Print detailed information of a job."""
pprint.pprint(job)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='salt-autoinstaller monitor tool via salt-api.')
parser.add_argument('-u', '--saltapi-url',
default=os.environ.get('SALTAPI_URL',
'https://localhost:8000'),
help='Specify the host url. Overwrite SALTAPI_URL.')
parser.add_argument('-a', '--auth', '--eauth', '--extended-auth',
default=os.environ.get('SALTAPI_EAUTH', 'pam'),
help='Specify the external_auth backend to '
'authenticate against and interactively prompt '
'for credentials. Overwrite SALTAPI_EAUTH.')
parser.add_argument('-n', '--username',
default=os.environ.get('SALTAPI_USER'),
help='Optional, defaults to user name. will '
'be prompt if empty unless --non-interactive. '
'Overwrite SALTAPI_USER.')
parser.add_argument('-p', '--password',
default=os.environ.get('SALTAPI_PASS'),
help='Optional, but will be prompted unless '
'--non-interactive. Overwrite SALTAPI_PASS.')
parser.add_argument('--non-interactive', action='store_true',
default=False,
help='Optional, fail rather than waiting for input.')
parser.add_argument('-r', '--remove', action='store_true',
default=False,
help='Remove the toked cached in the system.')
parser.add_argument('-i', '--insecure', action='store_true',
default=False,
help='Ignore any SSL certificate that may be '
'encountered. Note that it is recommended to resolve '
'certificate errors for production.')
parser.add_argument('-H', '--debug-http', action='store_true',
default=False,
help='Output the HTTP request/response headers on stderr.')
parser.add_argument('-m', '--minions', action='store_true',
default=False,
help='List available minions.')
parser.add_argument('--show-minion', metavar='MID', default=None,
help='Show the details of a minion.')
parser.add_argument('-j', '--jobs', action='store_true',
default=False,
help='List available jobs.')
parser.add_argument('--show-job', metavar='JID', default=None,
help='Show the details of a job.')
parser.add_argument('-e', '--events', action='store_true',
default=False,
help='Show events from salt-master.')
parser.add_argument('target', nargs='?',
help='Minion ID where to launch the installer.')
args = parser.parse_args()
if not args.saltapi_url:
print('Please, provide a valid Salt API URL', file=sys.stderr)
exit(-1)
if args.non_interactive:
if args.username is None:
print('Please, provide a valid user name', file=sys.stderr)
exit(-1)
if args.password is None:
print('Please, provide a valid password', file=sys.stderr)
exit(-1)
else:
if args.username is None:
args.username = input('Username: ')
if args.password is None:
args.password = getpass.getpass(prompt='Password: ')
api = SaltAPI(url=args.saltapi_url,
username=args.username,
password=args.password,
eauth=args.auth,
insecure=args.insecure,
debug=args.debug_http)
api.login(args.remove)
if args.minions:
print_minions(api.minions())
if args.show_minion:
print_minion(api.minions(args.show_minion))
if args.jobs:
print_jobs(api.jobs())
if args.show_job:
print_job(api.jobs(args.show_job))
if args.target:
print_job(api.run_job(args.target, 'state.highstate'))
if args.events:
for tag, data in api.events():
print('- {}'.format(tag))
pprint.pprint(data)