-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-gui.py
389 lines (339 loc) · 12.4 KB
/
cf-gui.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
#!/usr/bin/env python
# encoding: utf-8
# author: sylvain rouquette
try:
from colorama.ansi import clear_screen
import colorama
except:
print('''
you need to install the requirements first:
pip install -r requirements.txt
''')
from datetime import datetime, timedelta
import json
import os
import re
import subprocess
import sys
import time
import gettext
t = gettext.translation('cf-gui', 'locale', fallback=True, languages=['en'])
t.install()
COMMANDS = {
'env': 'cf env {name}',
'logs': 'cf logs {name}',
'logs recent': 'cf logs --recent {name}',
'push': 'cf push {name} -d {domain}',
'restart': 'cf restart {name}',
'target': 'cf target -s {name}',
'refresh': 'refresh',
}
def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty, select
def setup_term(fd, when=termios.TCSAFLUSH):
mode = termios.tcgetattr(fd)
mode[tty.LFLAG] = mode[tty.LFLAG] & ~(termios.ECHO | termios.ICANON)
termios.tcsetattr(fd, when, mode)
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
setup_term(fd)
try:
rw, wl, xl = select.select([fd], [], [])
except select.error:
return
if rw:
readch = lambda nb: bytes(sys.stdin.read(nb), 'utf-8')
ch = readch(1)
if ch != b'\x1b':
return ch
else:
return readch(2)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return _getch
getch = _find_getch()
class CommandExecutor:
def execute(self, command):
print(command)
if sys.platform == 'win32':
with open('cf-command.bat', 'wt') as f:
f.write(command)
else:
with open('cf-command.sh', 'wt') as f:
command = ['#!/usr/bin/env bash', command]
f.write('\n'.join(command))
if not 'push' in command and not 'refresh' in command:
cmd = ['cf']
cmd.extend(command[3:].split(' '))
self.popen(cmd)
def popen(self):
pass
def target(self):
return '''
User: Syl
Space: Jaeger
'''.splitlines()
def spaces(self):
return '''
name
Jaeger
Imaging
'''.splitlines()
def routes(self):
return '''
healthcloud jaeger.domain.com
'''.splitlines()
def services(self):
return '''
Getting apps in org domain.com/ space Jaeger as syl...
name requested state instances memory disk urls
group_builder started 1/1 512M 1G group_builder.jaeger.domain.com
volume_controller started ?/1 384M 1G volume_controller.jaeger.domain.com
mpr_rendering started 0/1 384M 1G mpr_rendering.jaeger.domain.com
'''.splitlines()
class CfCommandExecutor(CommandExecutor):
def popen(self, command, **kwargs):
process = subprocess.Popen(command, universal_newlines=True, **kwargs)
return process.communicate()[0]
def target(self):
return self.popen(['cf', 'target'], stdout=subprocess.PIPE).split('\n')
def spaces(self):
return self.popen(['cf', 'spaces'], stdout=subprocess.PIPE).split('\n')
def routes(self):
return self.popen(['cf', 'routes'], stdout=subprocess.PIPE).split('\n')
def services(self):
return self.popen(['cf', 'apps'], stdout=subprocess.PIPE).split('\n')
class Settings:
FILE = os.path.sep.join((os.path.expanduser('~'), 'cf-gui.json'))
TIME_FORMAT = '%Y-%m-%d %H:%M'
PATTERNS = {
'user' : re.compile(r'^%s:.*\s+([^\s]+)' % _('User')),
'space' : re.compile(r'^%s:.*\s+([^\s]+)' % _('Space')),
'domain' : re.compile(r'^.*healthcloud[^\s]?\s+([^\s]+)'),
'check_space': re.compile(r'^%s.*%s\s+([^\s]+)\s+' % (_('Getting'), _('space'))),
'service': re.compile(r'^([^\s]+)(\s+\w+\s+)(.{3})(\s+[^\s]+\s+[^\s]+\s+)([^\s]+)') # (name)(ignore)(status)(ignore)(routes)
}
def __init__(self, executor):
self.updated = False
self.json = {}
try:
with open(Settings.FILE) as f:
self.json = json.load(f)
except:
pass
self.check(executor)
@property
def space_name(self):
return self.json['target']['space']
@space_name.setter
def space_name(self, value):
self.updated = True
self.json['target']['space'] = value
@property
def space(self):
return self.json['spaces'][self.space_name]
@property
def spaces(self):
return [{ 'name': key } for key in self.json['spaces'].keys()]
@property
def services(self):
return [dict(domain = self.space['domain'], **service) for service in self.space['services']]
def check(self, executor):
if not 'target' in self.json:
print('updating target...')
self.update_target(executor.target())
if not 'spaces' in self.json:
print('updating spaces...')
self.update_spaces(executor.spaces())
if not 'domain' in self.space:
print('updating domain...')
self.update_domain(executor.routes())
if not 'services' in self.space or not self.check_space_timestamp():
print('updating space %s...' % self.space_name)
self.update_space(executor.services())
def check_space_timestamp(self):
if not 'timestamp' in self.space:
return False
if datetime.now() - datetime.strptime(self.space['timestamp'], Settings.TIME_FORMAT) > timedelta(hours=8):
return False
return True
def save(self):
if not self.updated:
return
print('updated')
with open(Settings.FILE, 'wt') as f:
json.dump(self.json, f)
def update_target(self, lines):
self.updated = True
result = {}
def match_and_update(key, line):
m = Settings.PATTERNS[key].match(line)
if m:
result[key] = m.group(1)
target_info = ('user', 'space')
for line in lines:
for key in target_info:
if not key in result:
match_and_update(key, line)
if 'target' in self.json:
self.json['target'] = dict(self.json['target'], **result)
else:
self.json['target'] = result
if not 'space' in self.json['target']:
raise Exception('target not found.', lines)
def update_spaces(self, lines):
self.updated = True
if not 'spaces' in self.json:
self.json['spaces'] = {}
spaces_definition = False
for line in lines:
if not spaces_definition:
if line.startswith(_('name')):
spaces_definition = True
continue
line = line.strip()
if line and not line in self.json['spaces']:
self.json['spaces'][line] = {}
def update_domain(self, lines):
self.updated = True
for line in lines:
m = Settings.PATTERNS['domain'].match(line)
if not m:
continue
self.space['domain'] = m.group(1)
break
if not 'domain' in self.space:
raise Exception('domain not found.', lines)
def update_space(self, lines):
self.updated = True
for line in lines:
m = Settings.PATTERNS['check_space'].match(line)
if m and self.space_name != m.group(1):
raise Exception("target doesn't match current space: %s != %s" % (self.space_name, m.group(1)))
services_definition = False
services = []
for line in lines:
if not services_definition:
if line.startswith(_('name')):
services_definition = True
continue
m = Settings.PATTERNS['service'].match(line)
if not m:
continue
services.append({
'name': m.group(1),
'status': m.group(3),
'routes': m.group(5)
})
self.space['services'] = services
self.space['timestamp'] = str(datetime.now().strftime(Settings.TIME_FORMAT))
if len(services) == 0:
raise Exception('services not found.', lines)
def refresh(self):
del self.space['timestamp']
self.updated = True
class Menu:
def __init__(self, listener, items, command):
self.listener = listener
self.current = 0
self.items = sorted(items, key = lambda item: item['name'])
self.command = command
def next(self):
self.current = (self.current + 1) % len(self.items)
def prev(self):
self.current = (self.current - 1) % len(self.items)
def select(self, index):
self.current = index
def __str__(self):
result = []
for i, item in enumerate(self.items):
start = '> ' if i == self.current else ' '
if not 'status' in item:
color = colorama.Fore.RESET
elif item['status'].startswith('0/'):
color = colorama.Fore.RED
elif item['status'].startswith('?/'):
color = colorama.Fore.MAGENTA
else:
color = colorama.Fore.RESET
result.append('%s%s %d %s' % (color, start, i, item['name']))
return '\n'.join(result)
def activate(self):
return self.listener.execute_command(self.command, self.items[self.current])
class MenuMain(Menu):
def __init__(self, listener, items):
super().__init__(listener, items, None)
def activate(self):
return self.listener.execute_main(self.items[self.current]['command'])
class MenuFactory:
def __init__(self):
pass
def create(self, listener, items, command):
if command == 'main':
return MenuMain(listener, items)
else:
return Menu(listener, items, command)
class App:
def __init__(self, settings, menu_factory, executor):
self.settings = settings
self.menu_factory = menu_factory
self.executor = executor
reformatted_commands = [{ 'name': key, 'command': value } for key, value in COMMANDS.items()]
self.menu = self.menu_factory.create(self, reformatted_commands, 'main')
def execute_command(self, command, args):
if 'target' in command:
self.settings.space_name = args['name']
self.executor.execute(command.format(**args))
return True
def execute_main(self, command):
if 'refresh' in command:
self.settings.refresh()
return True
elif 'target' in command:
self.menu = self.menu_factory.create(self, settings.spaces, command)
else:
self.menu = self.menu_factory.create(self, settings.services, command)
return False
def run(self):
WIN32 = sys.platform == 'win32'
decode_key = lambda key: key if type(key) == int else int.from_bytes(key.encode(), byteorder='big')
while True:
print(clear_screen())
print(self.menu)
key = getch()
if WIN32 and key == b'\xe0':
arrow = getch()
if arrow == b'H': # up
self.menu.prev()
elif arrow == b'P': # down
self.menu.next()
elif not WIN32 and len(key) > 1:
if key == b'[A': # up
self.menu.prev()
elif key == b'[B': # down
self.menu.next()
elif key == b'\r' or key == b'\n':
if self.menu.activate():
break
elif key >= b'0' and key <= b'9':
self.menu.select(key[0] - ord('0'))
if self.menu.activate():
break
elif key == b'\x1b' or key == b'\x03' or key == b'q':
break
if __name__ == '__main__':
colorama.init()
executor = CfCommandExecutor()
settings = Settings(executor)
app = App(settings, MenuFactory(), executor)
app.run()
settings.save()