-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfpctl.py
More file actions
executable file
·1127 lines (955 loc) · 50.8 KB
/
fpctl.py
File metadata and controls
executable file
·1127 lines (955 loc) · 50.8 KB
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import datetime
import errno
import getpass
import json
import logging
import os
import re
import subprocess
import sys
import time
from operator import itemgetter
from collections import OrderedDict
try:
from colorama import Fore, Style
from terminaltables import SingleTable
except ImportError as e:
print('==> ERROR: cannot import a required Python module. Run fpctl setup to ensure '
'all dependencies are installed.')
print('Missing module: {}'.format(e.name))
sys.exit(1)
FPCTL_CONFIG_DIR = os.path.expanduser('~/.config/fpctl')
FPCTL_ROOT_DIR = os.path.expanduser('~/.local')
with open(os.path.join(FPCTL_CONFIG_DIR, '.installed')) as f:
root_dir = f.readline().strip()
if (os.path.isdir(root_dir)):
FPCTL_ROOT_DIR = root_dir
else:
raise FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT),
root_dir)
FPCTL_DATA_DIR = os.path.expanduser(os.path.join(FPCTL_ROOT_DIR, 'share/fpctl'))
INVENTORY_READOUT_GROUP = 'flp-readout'
INVENTORY_QCTASK_GROUP = 'qc-task'
INVENTORY_QCCHECKER_GROUP = 'qc-checker'
INVENTORY_QCREPOSITORY_GROUP = 'qc-repository'
INVENTORY_INFOLOGGERSERVER_GROUP = 'infologger-server'
DEFAULT_INVENTORY_PATH = os.path.join(FPCTL_CONFIG_DIR, 'inventory')
TARGET_GROUPS = ['flp-readout', 'qc-task', 'qc-checker']
TASK_NAMES = ['readout', 'qctask', 'qcchecker']
C_WARN = Style.BRIGHT + Fore.YELLOW + '==> WARNING: ' + Style.RESET_ALL
C_YELL = Style.BRIGHT + Fore.YELLOW + '==> ' + Style.RESET_ALL
C_QUEST = C_YELL
C_ERR = Style.BRIGHT + Fore.RED + '==> ERROR: ' + Style.RESET_ALL
C_RED = Style.BRIGHT + Fore.RED + '==> ' + Style.RESET_ALL
C_MSG = Style.BRIGHT + Fore.GREEN + '==> ' + Style.RESET_ALL
C_ITEM_NO_PADDING = Style.BRIGHT + Fore.BLUE + '-> ' + Style.RESET_ALL
C_ITEM = ' ' + C_ITEM_NO_PADDING
BULLET = '\u25CF '
ANSIBLE_SSH_DOCUMENTATION = 'https://github.com/AliceO2Group/Control/fpctl#authentication-on-the-target-system'
class Reprinter:
def __init__(self):
self.text = ''
def moveup(self, lines):
for _ in range(lines):
sys.stdout.write("\x1b[A")
def reprint(self, text):
# Clear previous text by overwritig non-spaces with spaces
self.moveup(self.text.count("\n"))
sys.stdout.write(re.sub(r"[^\s]", " ", self.text))
# Print new text
lines = min(self.text.count("\n"), text.count("\n"))
self.moveup(lines)
sys.stdout.write(text)
self.text = text
class Inventory:
def __init__(self, inventory_path):
self.inventory_path = inventory_path
self.inventory_file_lines = []
self.hosts_cache_file_path = os.path.join(FPCTL_DATA_DIR, 'hosts_cache.json')
self.pubkey_file_path = ''
self.hosts_cache = dict()
self.SSH_DIR = os.path.join(os.path.expanduser('~'), '.ssh')
self.__init_cache()
def __init_cache(self):
hosts_cache = dict()
if os.path.isfile(self.hosts_cache_file_path):
try:
with open(self.hosts_cache_file_path, 'r') as hosts_cache_file:
hosts_cache = json.load(hosts_cache_file)
except Exception as e:
print(C_WARN + 'A fpctl hosts cache exists but loading failed, so the ' +
'cache will be overwritten. If you see this message more than ' +
'once, try reinstalling fpctl.')
self.hosts_cache = hosts_cache
def __write_cache(self):
try:
with open(self.hosts_cache_file_path, 'w') as hosts_cache_file:
json.dump(self.hosts_cache, hosts_cache_file)
except Exception as e:
print(C_WARN + 'Cannot write fpctl hosts cache. If you see this ' +
'message more than once, try reinstalling fpctl.')
def load(self):
output = subprocess.check_output(['ansible',
'all',
'-i{}'.format(self.inventory_path),
'--list-hosts'])
inventory_hosts = output.decode(sys.stdout.encoding).splitlines()
inventory_hosts = inventory_hosts[1:] # we throw away the first line which is only a summary
self.inventory_hosts = [line.strip() for line in inventory_hosts]
inventory_file_lines = []
with open(self.inventory_path, 'r') as inventory_file:
inventory_file_lines = inventory_file.readlines()
self.inventory_file_lines = inventory_file_lines
if inventory_hosts != ['localhost']:
self.__check_for_ssh_keys()
def check_hosts(self, force=False):
try:
self.__check_for_ssh_auth(force)
self.__check_for_sudo_nopasswd(force)
finally:
if self.hosts_cache:
self.__write_cache()
def __check_for_ssh_keys(self):
self.force_deploy_ssh_keys = False
if not os.path.isdir(self.SSH_DIR):
self.force_deploy_ssh_keys = \
query_yes_no('SSH configuration directory not found. fpctl needs '
'a public/private SSH key pair to operate on the '
'target machines. The SSH configuration directory, '
'as well as a key pair, can be created for you and '
'deployed on the target systems. Do you wish to '
'proceed?', default='yes')
if self.force_deploy_ssh_keys:
os.mkdir(self.SSH_DIR)
else:
print(C_RED + 'Since Ansible requires passwordless authentication on the target '
'hosts in order to work, fpctl cannot continue.\n' + C_RED +
'Please see {} for instructions on how to '
'set up passwordless authentication for Ansible/fpctl.'
.format(ANSIBLE_SSH_DOCUMENTATION))
self.__write_cache()
sys.exit(1)
if self.force_deploy_ssh_keys:
self.__create_rsa_keypair()
return
candidate_keyfiles = [['id_rsa.pub', 'id_rsa'],
['id_dsa.pub', 'id_dsa']]
candidate_keyfiles = [[os.path.join(self.SSH_DIR, jtem) for jtem in item] for item in candidate_keyfiles]
for keypair in candidate_keyfiles:
if os.path.isfile(keypair[0]) and \
os.path.isfile(keypair[1]):
self.pubkey_file_path = keypair[0]
print(C_MSG + 'Found SSH public/private key pair {0}/{1}.'
.format(os.path.basename(keypair[0]),
os.path.basename(keypair[1])))
break
if not self.pubkey_file_path:
self.force_deploy_ssh_keys = \
query_yes_no('No suitable SSH public/private key pairs were found. '
'fpctl needs '
'a public/private SSH key pair to operate on the '
'target machines. The SSH configuration directory, '
'as well as a key pair, can be created for you and '
'deployed on the target systems. Do you wish to '
'proceed?', default='yes')
if self.force_deploy_ssh_keys:
self.__create_rsa_keypair()
else:
print(C_RED + 'Since Ansible requires passwordless authentication on the target '
'hosts in order to work, fpctl cannot continue.\n' + C_RED +
'Please see {} for instructions on how to '
'set up passwordless authentication for Ansible/fpctl.'
.format(ANSIBLE_SSH_DOCUMENTATION))
self.__write_cache()
sys.exit(1)
def __create_rsa_keypair(self):
self.force_deploy_ssh_keys = True
rc = subprocess.call('ssh-keygen -t rsa -N "" -f id_rsa -q',
shell=True,
cwd=self.SSH_DIR)
if rc != 0:
print(C_ERR + 'Cannot create RSA key pair.')
self.__write_cache()
sys.exit(1)
self.pubkey_file_path = os.path.join(self.SSH_DIR, 'id_rsa.pub')
def __deploy_ssh_keys(self, hosts_that_cannot_ssh):
if not self.pubkey_file_path:
print(C_ERR + 'Cannot find SSH public key for deployment on target system.')
self.__write_cache()
sys.exit(1)
for host_user_tuple in hosts_that_cannot_ssh:
ansible_user = host_user_tuple[1]
target_hostname = host_user_tuple[0]
password = getpass.getpass(prompt='[ssh] password for {0}@{1}: '
.format(ansible_user, target_hostname))
SCI_CALL = '/usr/bin/sshpass -p{3} /usr/bin/ssh-copy-id -i {0} {1}@{2}'.format(
self.pubkey_file_path,
ansible_user,
target_hostname,
password)
rc = subprocess.call(SCI_CALL, shell=True)
if rc != 0:
print(C_ERR + 'Cannot deploy SSH public key to target system.')
self.__write_cache()
sys.exit(1)
def __check_for_ssh_auth(self, force=False):
hosts_that_cannot_ssh = []
has_localhosts = False
result = []
for target_hostname in self.inventory_hosts:
ansible_user = os.environ.get('USER')
for line in self.inventory_file_lines:
if line.startswith(target_hostname) and 'ansible_user='in line:
splitline = line.split(' ')
for word in splitline:
if word.startswith('ansible_user='):
ansible_user = word.strip()[13:]
break # we found an ansible_user override, so we break and go on
if not force and \
target_hostname in self.hosts_cache and \
'auth_methods' in self.hosts_cache[target_hostname] and \
self.hosts_cache[target_hostname]['auth_methods'] and \
'ansible_user' in self.hosts_cache[target_hostname] and \
self.hosts_cache[target_hostname]['ansible_user'] == ansible_user:
result.append({'host': target_hostname,
'auth': self.hosts_cache[target_hostname]['auth_methods']})
continue
# HACK: we check if there's an ansible_user specified for this hostname in the
# inventory file. This should be replaced with ansible-python binding.
if target_hostname == 'localhost':
has_localhosts = True
self.hosts_cache['localhost'] = {'auth_methods': ['local'],
'ansible_user': ansible_user}
result.append({'host': 'localhost',
'auth': ['local']})
continue
output = b''
try:
output = subprocess.check_output(['ssh',
'-o BatchMode=yes',
'-o ConnectTimeout=5',
'-o StrictHostKeyChecking=no',
'-o GSSAPIAuthentication=yes',
'-o PubkeyAuthentication=no',
'{0}@{1}'.format(ansible_user, target_hostname),
'echo fpctl GSSAPIAuthentication ok'],
stderr=subprocess.STDOUT)
logging.debug('SSH GSSAPI check output:{}'.format(output.decode(sys.stdout.encoding)))
except subprocess.CalledProcessError as e:
logging.debug('SSH GSSAPI check error: {}'.format(e.output))
gssapi_auth_ok = 'fpctl GSSAPIAuthentication ok' in output.decode(sys.stdout.encoding)
try:
output = subprocess.check_output(['ssh',
'-o BatchMode=yes',
'-o ConnectTimeout=5',
'-o StrictHostKeyChecking=no',
'-o GSSAPIAuthentication=no',
'-o PubkeyAuthentication=yes',
'{0}@{1}'.format(ansible_user, target_hostname),
'echo fpctl PubkeyAuthentication ok'],
stderr=subprocess.STDOUT)
logging.debug('SSH Pubkey check output:{}'.format(output.decode(sys.stdout.encoding)))
except subprocess.CalledProcessError as e:
logging.debug('SSH Pubkey check error: {}'.format(e.output))
pubkey_auth_ok = 'fpctl PubkeyAuthentication ok' in output.decode(sys.stdout.encoding)
self.hosts_cache[target_hostname] = {'auth_methods': [],
'ansible_user': ansible_user}
if not pubkey_auth_ok and not gssapi_auth_ok:
hosts_that_cannot_ssh.append((target_hostname, ansible_user))
if pubkey_auth_ok or gssapi_auth_ok:
auth_ok = []
if pubkey_auth_ok:
auth_ok.append('public key')
self.hosts_cache[target_hostname]['auth_methods'].append('public key')
if gssapi_auth_ok:
auth_ok.append('GSSAPI/Kerberos')
self.hosts_cache[target_hostname]['auth_methods'].append('GSSAPI/Kerberos')
result.append({'host': target_hostname, 'auth': auth_ok})
if has_localhosts:
print(C_QUEST + 'At least one of your target systems is localhost. SSH authentication '
'checks were skipped for localhost inventory entries. '
'Make sure that you have ansible_connection=local '
'in your inventory, and that passwordless sudo is enabled.')
if hosts_that_cannot_ssh:
print(C_WARN + 'The following hosts do not appear to support passwordless '
'authentication (through either GSSAPI/Kerberos or public key):')
for host_user_tuple in hosts_that_cannot_ssh:
print(C_ITEM + '{0}@{1}'.format(host_user_tuple[1], host_user_tuple[0]))
if not self.force_deploy_ssh_keys:
self.force_deploy_ssh_keys = \
query_yes_no('fpctl can try to enable passwordless public key '
'authentication (excluding Kerberos) on these '
'hosts by adding your SSH public key '
'to their authorized keys list. '
'Would you like to proceed?', default="yes")
if self.force_deploy_ssh_keys:
self.__deploy_ssh_keys(hosts_that_cannot_ssh)
else:
print(C_RED + 'Since Ansible requires passwordless authentication on the target '
'hosts in order to work, fpctl cannot continue.\n' + C_RED +
'Please see {} for instructions on how to '
'set up passwordless authentication for Ansible/fpctl.'
.format(ANSIBLE_SSH_DOCUMENTATION))
self.__write_cache()
sys.exit(1)
print(C_MSG + 'Hosts in inventory:')
for item in result:
print(C_ITEM + item['host'] + ' [authentication: ' + ', '.join(item['auth']) + ']')
def __check_for_sudo_nopasswd(self, force=False):
for target_hostname in self.inventory_hosts:
ansible_user = os.environ.get('USER')
for line in self.inventory_file_lines:
if line.startswith(target_hostname) and 'ansible_user='in line:
splitline = line.split(' ')
for word in splitline:
if word.startswith('ansible_user='):
ansible_user = word.strip()[13:]
break # we found an ansible_user override, so we break and go on
become_with_ksu = False
for line in self.inventory_file_lines:
if line.startswith(target_hostname) and 'ansible_become_method=ksu' in line:
become_with_ksu = True
break # if this host is set up with Kerberos+ksu, we skip to the next
if become_with_ksu:
continue
if not force and \
target_hostname in self.hosts_cache and \
'sudo_nopasswd' in self.hosts_cache[target_hostname] and \
self.hosts_cache[target_hostname]['sudo_nopasswd'] and \
'ansible_user' in self.hosts_cache[target_hostname] and \
self.hosts_cache[target_hostname]['ansible_user'] == ansible_user:
continue
output = b''
if target_hostname == 'localhost':
try:
output = subprocess.check_output(['/bin/sudo -kn echo "fpctl sudo ok"'],
shell=True,
stderr=subprocess.STDOUT)
logging.debug('local sudo check output:{}'.format(output.decode(sys.stdout.encoding)))
except subprocess.CalledProcessError as e:
logging.debug('local sudo check error: {}'.format(e.output))
else:
try:
output = subprocess.check_output(['ssh',
'-o BatchMode=yes',
'-o ConnectTimeout=5',
'-o StrictHostKeyChecking=no',
'{0}@{1}'.format(ansible_user, target_hostname),
'/bin/sudo -kn echo "fpctl sudo ok"'],
stderr=subprocess.STDOUT)
logging.debug('SSH sudo check output:{}'.format(output.decode(sys.stdout.encoding)))
except subprocess.CalledProcessError as e:
logging.debug('SSH sudo check error: {}'.format(e.output))
sudo_ok = 'fpctl sudo ok' in output.decode(sys.stdout.encoding)
self.hosts_cache[target_hostname]['sudo_nopasswd'] = sudo_ok
if not sudo_ok:
if query_yes_no('Passwordless sudo not set on host {0}. fpctl requires '
'sudo NOPASSWD configuration in order to work. To '
'enable this, you should add a file named "zzz-fpctl" to '
'the /etc/sudoers.d directory on host {0}, with the '
'content "{1} ALL=(ALL) NOPASSWD: ALL".\n'
'You may quit fpctl and do it yourself, or fpctl can do '
'this for you now. Do you wish to proceed with enabling '
'passwordless sudo?'.format(target_hostname, ansible_user),
default="yes"):
sudoers_extra_path = '/etc/sudoers.d/zzz-fpctl'
file_cmd = '/bin/sudo -Sk su -c "EDITOR=tee visudo -f {}"' \
.format(sudoers_extra_path)
password = getpass.getpass(prompt='[sudo] password for {0}@{1}: '
.format(ansible_user, target_hostname))
sudoers_line = '{} ALL=(ALL) NOPASSWD: ALL\n'.format(ansible_user)
if target_hostname == 'localhost':
p = subprocess.Popen(file_cmd,
shell=True,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.DEVNULL,
universal_newlines=True)
else:
p = subprocess.Popen(['ssh',
'-o BatchMode=yes',
'-o ConnectTimeout=5',
'-o StrictHostKeyChecking=no',
'{0}@{1}'.format(ansible_user, target_hostname),
file_cmd],
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.DEVNULL,
universal_newlines=True)
p.communicate('{0}\n{1}'.format(password, sudoers_line))
if p.returncode:
print(C_ERR + 'Could not set up passwordless sudo on host {}. fpctl will now quit.'
.format(target_hostname))
self.__write_cache()
sys.exit(p.returncode)
else:
self.hosts_cache[target_hostname]['sudo_nopasswd'] = True
print(C_MSG + 'Passwordless sudo OK on host {}.'.format(target_hostname))
else:
print(C_ERR + 'Passwordless sudo not allowed on host {}. fpctl will now quit.'
.format(target_hostname))
self.__write_cache()
sys.exit(0)
def __get_next_run_number():
run_number_path = os.path.join(FPCTL_CONFIG_DIR, "run_counter")
last_run_number = 0
if os.path.isfile(run_number_path):
run_number_line = ""
with open(run_number_path, 'r') as run_counter:
run_number_line = run_counter.readline()
last_run_number = int(run_number_line)
run_number = last_run_number + 1
with open(run_number_path, 'w') as run_counter:
run_counter.writelines([str(run_number)])
return run_number
def check_for_correct_task(args):
if args.task:
if args.task not in TASK_NAMES:
print(C_ERR + 'Unknown task "{}".'.format(args.task))
print(C_RED + 'Available tasks:')
for task_name in TASK_NAMES:
print(C_ITEM + task_name)
sys.exit(1)
def print_summary(inventory_path):
all_target_groups = TARGET_GROUPS + ['qc-repository', 'infologger-server']
all_task_names = TASK_NAMES + [Style.RESET_ALL + Style.DIM + Fore.WHITE + '(none)' + Style.RESET_ALL,
Style.RESET_ALL + Style.DIM + Fore.WHITE + '(none)' + Style.RESET_ALL]
systemd_units = ['flpprototype-readout',
'flpprotocype-qctask',
'flpprototype-qcchecker',
Style.RESET_ALL + Style.DIM + Fore.WHITE + '(none)' + Style.RESET_ALL,
'infoLoggerServer']
target_hosts = []
for group in all_target_groups:
output = subprocess.check_output(['ansible',
group,
'-i{}'.format(inventory_path),
'--list-hosts'])
if b'hosts (0)' in output:
target_hosts.append([])
continue
inventory_hosts = output.decode(sys.stdout.encoding).splitlines()
inventory_hosts = inventory_hosts[1:] # we throw away the first line which is only a summary
inventory_hosts = [line.strip() for line in inventory_hosts]
target_hosts.append(inventory_hosts)
headers = list('\n'.join(Style.BRIGHT + Fore.BLUE + line + Style.RESET_ALL for line in item.splitlines()) for item in
['Inventory groups',
'Target hosts',
'Systemd units\n(on target hosts)',
'Tasks\n(accessible through fpctl)'])
rows = list(zip(('[' + item + ']' for item in all_target_groups),
('\n'.join(item) for item in target_hosts),
systemd_units,
(Style.BRIGHT + Fore.BLUE + item + Style.RESET_ALL for item in all_task_names)))
table = SingleTable([headers] +
rows)
table.inner_row_border = True
table.CHAR_H_INNER_HORIZONTAL = b'\xcd'.decode('ibm437')
table.CHAR_OUTER_TOP_HORIZONTAL = b'\xcd'.decode('ibm437')
table.CHAR_OUTER_TOP_LEFT = b'\xd5'.decode('ibm437')
table.CHAR_OUTER_TOP_RIGHT = b'\xb8'.decode('ibm437')
table.CHAR_OUTER_TOP_INTERSECT = b'\xd1'.decode('ibm437')
table.CHAR_H_OUTER_LEFT_INTERSECT = b'\xc6'.decode('ibm437')
table.CHAR_H_OUTER_RIGHT_INTERSECT = b'\xb5'.decode('ibm437')
table.CHAR_H_INNER_INTERSECT = b'\xd8'.decode('ibm437')
print(table.table)
print(C_MSG + 'Configuration files were deployed in /etc/flpprototype.d on the target systems.')
print(C_MSG + 'FLP prototype software is installed in /opt/alisw. If you wish to use ' +
'it manually, you must run "module load flpproto" after you SSH into a target system.')
print(C_MSG + 'It is now possible to control the tasks listed in the last column through fpctl.')
print(C_MSG + 'Also, if you have a local instance of InfoBrowser, you may use it with this ' +
'configuration file: {} by copying the file to your /etc directory.'.format(os.path.join(FPCTL_CONFIG_DIR, 'infoLogger.cfg')))
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(C_QUEST +
question + prompt + '\n' +
C_QUEST + '------------------------------------\n' +
C_QUEST)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write(C_QUEST +
"Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n" +
C_QUEST)
def get_inventory_path(inventory_option):
"""Get the path of the inventory file. May interact with user."""
inventory_path = DEFAULT_INVENTORY_PATH
if inventory_option:
inventory_path = os.path.abspath(inventory_option)
if not os.path.isfile:
raise FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT),
inventory_path)
else:
return inventory_path
else:
if not os.path.isfile(inventory_path):
if query_yes_no('Ansible inventory file not found at {}. fpctl can '
'autogenerate a default one for you, with target localhost. '
'This means that all FLP prototype software will be '
'deployed on your current system. '
'Would you like to proceed?'
.format(inventory_path), default="yes"):
with open(inventory_path, 'w') as inventory_file:
loc = 'localhost ansible_connection=local'
inv = ''
for group in [INVENTORY_READOUT_GROUP,
INVENTORY_QCTASK_GROUP,
INVENTORY_QCCHECKER_GROUP,
INVENTORY_QCREPOSITORY_GROUP,
INVENTORY_INFOLOGGERSERVER_GROUP]:
inv += '[{0}]\n{1}\n'.format(group, loc)
print("{}".format(inv), file=inventory_file)
else:
raise FileNotFoundError(errno.ENOENT,
os.strerror(errno.ENOENT),
inventory_path)
return inventory_path
def deploy(args):
"""Handler for deploy command"""
inventory_path = get_inventory_path(args.inventory)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts(force=True)
ansible_cwd = os.path.join(FPCTL_DATA_DIR, 'system-configuration/ansible')
ansible_systemd_path = os.path.join(FPCTL_DATA_DIR, 'Control/fpctl/systemd/system')
ansible_systemd_var = 'flpprototype_systemd={}'.format(ansible_systemd_path)
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = [ansible_systemd_var]
if (args.ansible_extra_vars):
ansible_extra_vars += args.ansible_extra_vars.split(' ')
ansible_cmd = ['ansible-playbook',
os.path.join(ansible_cwd, 'site.yml'),
'-i{}'.format(inventory_path),
'-s',
'-e"{}"'.format(' '.join(ansible_extra_vars))]
if args.verbose:
ansible_cmd += ['-vvv']
if ansible_extra_params:
ansible_cmd += ansible_extra_params
ansible_env = os.environ.copy()
ansible_env['ANSIBLE_CONFIG'] = os.path.join(FPCTL_CONFIG_DIR, 'ansible.cfg')
ansible_proc = subprocess.Popen(' '.join(ansible_cmd),
shell=True,
cwd=ansible_cwd,
env=ansible_env)
ansible_proc.communicate()
print(C_MSG + 'Deployment summary:')
print_summary(inventory_path)
print(C_MSG + 'All done.')
def configure(args):
"""Handler for configure command"""
inventory_path = get_inventory_path(args.inventory)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts()
ansible_cwd = os.path.join(FPCTL_DATA_DIR, 'system-configuration/ansible')
ansible_systemd_path = os.path.join(FPCTL_DATA_DIR, 'Control/fpctl/systemd/system')
ansible_systemd_var = 'flpprototype_systemd={}'.format(ansible_systemd_path)
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = [ansible_systemd_var]
if (args.ansible_extra_vars):
ansible_extra_vars += args.ansible_extra_vars.split(' ')
ansible_cmd = ['ansible-playbook',
os.path.join(ansible_cwd, 'site.yml'),
'-i{}'.format(inventory_path),
'-s',
'-tconfiguration'
'-e"{}"'.format(' '.join(ansible_extra_vars))]
if args.verbose:
ansible_cmd += ['-vvv']
if ansible_extra_params:
ansible_cmd += ansible_extra_params
ansible_env = os.environ.copy()
ansible_env['ANSIBLE_CONFIG'] = os.path.join(FPCTL_CONFIG_DIR, 'ansible.cfg')
ansible_proc = subprocess.Popen(' '.join(ansible_cmd),
shell=True,
cwd=ansible_cwd,
env=ansible_env)
ansible_proc.communicate()
print(C_MSG + 'Configuration summary:')
print_summary(inventory_path)
print(C_MSG + 'All done.')
def run(args):
"""Handler for run command"""
inventory_path = get_inventory_path(args.inventory)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts()
host = args.host
custom_command = args.command
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = []
if (args.ansible_extra_vars):
ansible_extra_vars += args.ansible_extra_vars.split(' ')
ansible_cmd = ['ansible',
host,
'-i"{}"'.format(inventory_path)]
if args.verbose:
ansible_cmd += ['-vvv']
if ansible_extra_params:
ansible_cmd += ansible_extra_params
if ansible_extra_vars:
ansible_cmd += ['-e"{}"'.format(' '.join(ansible_extra_vars))]
ansible_cmd += ['-a"{}"'.format(custom_command)]
ansible_env = os.environ.copy()
ansible_env['ANSIBLE_CONFIG'] = os.path.join(FPCTL_CONFIG_DIR, 'ansible.cfg')
ansible_proc = subprocess.Popen(' '.join(ansible_cmd),
shell=True,
env=ansible_env)
ansible_proc.communicate()
print(C_MSG + 'All done.')
def start(args):
"""Handler for start command"""
inventory_path = get_inventory_path(args.inventory)
check_for_correct_task(args)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts()
ansible_cwd = os.path.join(FPCTL_DATA_DIR, 'system-configuration/ansible')
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = []
if (args.ansible_extra_vars):
ansible_extra_vars += args.ansible_extra_vars.split(' ')
ansible_extra_vars += "fpctl_run_number={}".format(__get_next_run_number())
ansible_cmd = ['ansible-playbook',
os.path.join(ansible_cwd, 'control.yml'),
'-i{}'.format(inventory_path),
'-s',
'-t{}control-start'
.format('{}-'.format(args.task) if args.task else '')]
if args.verbose:
ansible_cmd += ['-vvv']
if ansible_extra_params:
ansible_cmd += ansible_extra_params
if ansible_extra_vars:
ansible_cmd += ['-e"{}"'.format(' '.join(ansible_extra_vars))]
ansible_env = os.environ.copy()
ansible_env['ANSIBLE_CONFIG'] = os.path.join(FPCTL_CONFIG_DIR, 'ansible.cfg')
ansible_proc = subprocess.Popen(' '.join(ansible_cmd),
shell=True,
cwd=ansible_cwd,
env=ansible_env)
ansible_proc.communicate()
print(C_MSG + 'All done.')
def status(args):
"""Handler for status command"""
inventory_path = get_inventory_path(args.inventory)
check_for_correct_task(args)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts()
ansible_cwd = os.path.join(FPCTL_DATA_DIR, 'system-configuration/ansible')
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = []
if (args.ansible_extra_vars):
ansible_extra_vars += args.ansible_extra_vars.split(' ')
print(C_MSG + 'Checking status...')
ansible_cmd = ['ansible-playbook',
os.path.join(ansible_cwd, 'control.yml'),
'-i{}'.format(inventory_path),
'-s',
'-t{}control-status'
.format('{}-'.format(args.task) if args.task else '')]
if args.verbose:
ansible_cmd += ['-vvv']
if ansible_extra_params:
ansible_cmd += ansible_extra_params
if ansible_extra_vars:
ansible_cmd += ['-e"{}"'.format(' '.join(ansible_extra_vars))]
ansible_env = os.environ.copy()
ansible_env['ANSIBLE_CONFIG'] = os.path.join(FPCTL_CONFIG_DIR, 'ansible.cfg')
rp = Reprinter()
previous_now = datetime.datetime.now()
loop = True
try:
while loop:
if not args.follow:
loop = False
ansible_proc = subprocess.Popen(' '.join(ansible_cmd),
shell=True,
cwd=ansible_cwd,
env=ansible_env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output_lines = []
if args.verbose:
while True:
nextline = ansible_proc.stdout.readline()
u_nextline = nextline.decode(sys.stdout.encoding)
if 'PLAY RECAP' in u_nextline.strip():
break
sys.stdout.write(u_nextline)
output_lines.append(u_nextline.rstrip())
sys.stdout.flush()
out, err = ansible_proc.communicate()
if not args.verbose:
output_lines = out.decode(sys.stdout.encoding).splitlines()
# The output from the a control playbook contains a specially formatted debug
# module instance. We need to extract it and parse it as JSON.
in_json = False
json_entries = []
for line in output_lines:
if 'TASK' in line and 'control-status-output-json' in line:
in_json = True
continue
if in_json:
if line.startswith('task path') or line.startswith('META:'):
continue
if line.startswith('ok: ['):
json_entries.append('{\n')
elif not line.strip():
in_json = False
else:
json_entries[-1] += (line + '\n')
else:
continue
# print(C_MSG + 'Raw output:\n' + '\n'.join(output_lines))
json_objects = []
for entry in json_entries:
# print(C_ITEM + 'ITEM: ' + entry)
obj = json.loads(entry)
# print(C_ITEM + 'OBJECT:' + str(obj))
json_objects.append(obj['msg'])
# By service
tables = dict()
rows = []
available_task_names = TASK_NAMES
available_target_groups = TARGET_GROUPS
if args.task:
# this is ok because check_for_correct_task runs early on
available_task_names = [args.task]
available_target_groups = [TARGET_GROUPS[TASK_NAMES.index(args.task)]]
for i in range(len(available_task_names)):
servicename = available_task_names[i]
target_group = available_target_groups[i]
if servicename not in tables:
tables[servicename] = list()
for obj in json_objects:
if obj['service'] == servicename:
units = dict()
for line in obj['systemctl_status_output']:
unitname = line.split(':')[0]
unitstatus = line.split(':')[1]
unitname = re.sub('\.service$', '', unitname)
units[unitname] = unitstatus
for line in obj['systemctl_list_unit_files_output']:
unitname = re.sub('\.service$', '', line)
if '@' in unitname:
continue
if unitname not in units:
units[unitname] = 'inactive'
units = OrderedDict(sorted(units.items()))
unitnames = []
unitstatuses = []
for i, (unitname, unitstatus) in enumerate(units.items()):
c_bullet = BULLET
if unitstatus == 'active':
c_bullet = Style.BRIGHT + Fore.GREEN + BULLET + Style.RESET_ALL
elif unitstatus == 'reloading' or unitstatus == 'activating' or unitstatus == 'deactivating':
c_bullet = Style.BRIGHT + Fore.YELLOW + BULLET + Style.RESET_ALL
elif unitstatus == 'inactive':
c_bullet = Style.BRIGHT + Fore.WHITE + BULLET + Style.RESET_ALL
elif unitstatus == 'failed' or unitstatus == 'error':
c_bullet = Style.BRIGHT + Fore.RED + BULLET + Style.RESET_ALL
unitnames.append(' ' + unitname)
unitstatuses.append(c_bullet + unitstatus)
tables[servicename].append([' ' + obj['host'],
'\n'.join(unitnames),
'\n'.join(unitstatuses)])
tables[servicename] = sorted(tables[servicename], key=itemgetter(0))
for row in tables[servicename]:
if not row[1]:
row[1] = Style.DIM + Fore.WHITE + ' (no units found)' + Style.RESET_ALL
if not row[2]:
row[2] = Style.DIM + Fore.WHITE + BULLET + '(none)' + Style.RESET_ALL
headers = [Style.BRIGHT + Fore.BLUE + 'Inventory group' + Style.RESET_ALL + '\n Target hosts',
Style.BRIGHT + Fore.BLUE + 'Task' + Style.RESET_ALL + '\n Systemd units',
' \nStatus']
rows += [[Style.BRIGHT + Fore.BLUE + '[' + target_group + ']' + Style.RESET_ALL,
Style.BRIGHT + Fore.BLUE + servicename + Style.RESET_ALL]]
rows += tables[servicename]
table = SingleTable([headers] +
rows)
table.inner_row_border = True
table.inner_column_border = False
table.CHAR_H_INNER_HORIZONTAL = b'\xcd'.decode('ibm437')
table.CHAR_OUTER_TOP_HORIZONTAL = b'\xcd'.decode('ibm437')
table.CHAR_OUTER_TOP_LEFT = b'\xd5'.decode('ibm437')
table.CHAR_OUTER_TOP_RIGHT = b'\xb8'.decode('ibm437')
table.CHAR_OUTER_TOP_INTERSECT = b'\xd1'.decode('ibm437')
table.CHAR_H_OUTER_LEFT_INTERSECT = b'\xc6'.decode('ibm437')
table.CHAR_H_OUTER_RIGHT_INTERSECT = b'\xb5'.decode('ibm437')
table.CHAR_H_INNER_INTERSECT = b'\xd8'.decode('ibm437')
if not args.follow:
print(table.table)
return
now = datetime.datetime.now()
MIN_DELAY_SECONDS = 1
if now - previous_now < datetime.timedelta(seconds=MIN_DELAY_SECONDS):
time.sleep((datetime.timedelta(seconds=MIN_DELAY_SECONDS) - (now - previous_now)).total_seconds())
now = datetime.datetime.now()
table.title = now.strftime('%y-%m-%d %H:%M:%S')
to_print = (table.table + '\n' +
C_YELL + 'Status refreshed in {0:.2f} seconds. [Ctrl+C] to quit.\n'
.format((now - previous_now).total_seconds()))
rp.reprint(to_print)
previous_now = now
except KeyboardInterrupt as e:
print('\n' + C_MSG + 'User interrupt.')
sys.exit(0)
def stop(args):
"""Handler for stop command"""
inventory_path = get_inventory_path(args.inventory)
check_for_correct_task(args)
inv = Inventory(inventory_path)
inv.load()
inv.check_hosts()
ansible_cwd = os.path.join(FPCTL_DATA_DIR, 'system-configuration/ansible')
ansible_extra_params = []
if (args.ansible_extra_params):
ansible_extra_params += args.ansible_extra_params.split(' ')
ansible_extra_vars = []