forked from Zettaknight/zettaknight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzettaknight_utils.py
1425 lines (1044 loc) · 52.9 KB
/
zettaknight_utils.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
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/python
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2016 Matthew Carter, Ralph M Goodberlet.
#
# This file is part of Zettaknight.
#
# Zettaknight is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Zettaknight is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zettaknight. If not, see <http://www.gnu.org/licenses/>.
#
# Import python libs
import sys
import logging
import logging.handlers
import subprocess
import shlex
import yaml
import json
import os
import socket
import inspect
import datetime
import re
import termios
import shutil
import paramiko
import zettaknight_globs
import zettaknight_zfs
def zlog(*args):
'''
log function used for zettaknight purposes
'''
if len(args) != 2:
ret[zettaknight_globs.fqdn]['log']['1'] = "log function takes exactly 2 arguments (message, level)"
parse_output(ret)
return
message = args[0]
level = args[1]
ret = ""
date = datetime.datetime.today()
#test if level_zlog in globs is a string or int
if zettaknight_globs.zettaknight_conf is not None:
if 'level_zlog' not in zettaknight_globs.zettaknight_conf.iterkeys():
level_int = 4
else:
if not isinstance(zettaknight_globs.zettaknight_conf['level_zlog'], int):
if zettaknight_globs.zettaknight_conf['level_zlog'] == "DEBUG":
level_int = 5
if zettaknight_globs.zettaknight_conf['level_zlog'] == "INFO":
level_int = 4
if zettaknight_globs.zettaknight_conf['level_zlog'] == "WARNING":
level_int = 3
if zettaknight_globs.zettaknight_conf['level_zlog'] == "ERROR":
level_int = 2
if zettaknight_globs.zettaknight_conf['level_zlog'] == "CRITICAL":
level_int = 1
else: #if zettaknight_conf is empty, allows ERROR and CRITICAL messages to be reported to standard out
level_int = 4
if level_int >= 5:
if level.upper() == "DEBUG":
level = printcolors("{0}".format(level.upper()), "OKBLUE")
ret = "{0} {1} {2}".format(date, level, message)
if level_int >= 4:
if level.upper() == "INFO":
level = printcolors("{0}".format(level.upper()), "OKBLUE")
ret = "{0} {1} {2}".format(date, level, message)
if level.upper() == "SUCCESS":
level = printcolors("{0}".format(level.upper()), "OKGREEN")
ret = "{0} {1} {2}".format(date, level, message)
if level_int >= 3:
if level.upper() == "WARNING":
level = printcolors("{0}".format(level.upper()), "WARNING")
ret = "{0} {1} {2}".format(date, level, message)
if level_int >= 2:
if level.upper() == "ERROR":
level = printcolors("{0}".format(level.upper()), "FAIL")
ret = "{0} {1} {2}".format(date, level, message)
if level_int >= 1:
if level.upper() == "CRITICAL":
level = printcolors("{0}".format(level.upper()), "HEADER")
ret = "{0} {1} {2}".format(date, level, message)
if ret:
print ret
return ret
def pipe_this(*args):
'''
This function interates over each element in the list and passes it through a pipe.
Example:
ls /tmp | grep "bob" | grep "20150910"
would be
pipe_this("ls /tmp", "grep bob", "grep 20150910")
The function returns the subprocess object of the piped commands.
'''
cmd_list = args
pipe = None
for cmd in cmd_list:
if pipe is None:
pipe = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE)
else:
pipe = subprocess.Popen(shlex.split(cmd), stdin = pipe.stdout, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
pipe.wait()
ret = pipe
return ret
def pipe_this2(arg):
'''
This function is a re-write of the orginal pipe_this. pipe_this2 better replicates the standard pipe notation familiar in bash.
Example:
ls /tmp | grep "bob" | grep "20150910"
would be
pipe_this2("ls /tmp | grep bob | grep 20150910")
'''
#print(arg)
try:
if "|" in arg:
cmd_list = arg.split("|")
#print(cmd_list)
else:
raise Exception("| not found in command, exiting")
except Exception as e:
zettaknight_utils.zlog("funtion pipe_this2 encountered an unrecoverable error: {0}".format(e), "CRITICAL")
sys.exit(1)
pipe = None
for cmd in cmd_list:
#cmd = cmd.split()
if pipe is None:
pipe = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE)
else:
pipe = subprocess.Popen(shlex.split(cmd), stdin = pipe.stdout, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
pipe.wait()
ret = pipe
return ret
def mail_out(email_message, email_subject, email_recipient):
'''
multiple email recipients can be denoted as "<email 1> <email 2>"
'''
ret = {}
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn]['mail'] = {}
mail_out_cmd = "bash {0} -s '{2}' -r '{3}' -m '{1}'".format(zettaknight_globs.mail_out_script, email_message, email_subject, email_recipient)
ret[zettaknight_globs.fqdn]['mail'] = spawn_job(mail_out_cmd)
return ret
def parse_output(out_dict):
zlog("parse_output started, dict obj passed:\n\t{0}".format(out_dict), "DEBUG")
'''
accepts information in the following format
ret[dataset]['job'][<ret code>][<payload>]
'''
Zlogger = logging.getLogger('Zlogger')
Zlogger.setLevel(logging.INFO)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
Zlogger.addHandler(handler)
# if zettaknight_globs.mm_flag:
# zettaknight_globs.nocolor_flag = True
for dataset in out_dict.iterkeys():
json_out = {}
json_out["dataset"] = dataset
json_out["elapsed time (sec)"] = zettaknight_globs.elapsed_time
for job in out_dict[dataset].iterkeys():
json_out["job"] = job
for exit_status, output in out_dict[dataset][job].iteritems():
if isinstance(output, dict):
for value in out_dict[dataset][job][exit_status].itervalues():
for exit_status, output in value.iteritems():
json_out["exit status"] = exit_status
json_out["output"] = output
else:
json_out["exit status"] = exit_status
json_out["output"] = output
Zlogger.info(json.dumps({"Zettaknight": json_out}, sort_keys = True))
#print(printcolors("dictionary object passed to parse_output:\n{0}".format(out_dict), "WARNING"))
for dataset in out_dict.iterkeys():
global_ret = ""
mail_this = False
a = printcolors("\nDataset: {0}\n────────┐".format(dataset), "HEADER")
print(a)
for job in out_dict[dataset].iterkeys():
#print(printcolors(" {0}:".format(job), "OKBLUE"))
for exit_status, output in out_dict[dataset][job].iteritems():
if isinstance(output, dict):
for value in out_dict[dataset][job][exit_status].itervalues():
for exit_status, output in value.iteritems():
#print("\n\noutput is : \n{0}\n\n ".format(output))
output = str(output.replace('\n', '\n '))
else:
output = str(output.replace('\n', '\n '))
if str(exit_status) is "0":
task = "{0}".format(printcolors(" ├────────┬ {0}:".format(job),"OKBLUE"))
task_output = "{0}".format(printcolors(" ├──────── {0}\n".format(output), "OKGREEN"))
else:
if zettaknight_globs.mail_error_flag:
mail_this = True
task = "{0}".format(printcolors(" ├────────┬ {0}:".format(job), "OKBLUE"))
task_output = "{0}".format(printcolors(" ├──────── {0}\n".format(output), "FAIL"))
msg = "{0}\n{1}".format(task, task_output)
global_ret = "{0}{1}\n".format(global_ret, msg)
print(global_ret)
# if zettaknight_globs.mm_flag:
# mm_msg = re.sub("[├─┬┐]", "", global_ret)
# mm_msg = re.sub("%", " percent", mm_msg)
# mm_msg = re.sub(" ", "", mm_msg)
# mm_post(mm_msg)
if zettaknight_globs.mail_flag or mail_this:
global_ret = "Zettaknight:\n{0}{1}".format(a, global_ret)
try:
contact = zettaknight_globs.zfs_conf[dataset]['contact']
except KeyError:
contact = zettaknight_globs.default_contact_info
mail_sub = re.sub("[├─┬┐]", "", a)
mail_msg = re.sub("[├─┬┐]", "", global_ret) #re.sub = regular expression substitution (sed)
send_mail = mail_out(mail_msg, "Job report for Dataset: {0}, on {1}".format(dataset, zettaknight_globs.fqdn), contact)
return global_ret
def printcolors(msg, value):
#printcolors uses ansi color codes to change output colors
colors = {
'HEADER' : '\033[95m',
'OKBLUE' : '\033[96m',
'OKGREEN' : '\033[92m',
'WARNING' : '\033[93m',
'FAIL' : '\033[91m',
'ENDC' : '\033[0m'
}
if zettaknight_globs.nocolor_flag:
return(str(msg))
else:
return(colors[value] + str(msg) + colors['ENDC'])
def spawn_job(cmd):
#print(_printcolors("\033[0mnRunning command: {0}".format(cmd), "HEADER"))
ret = {}
try:
zlog("[spawn_job] running command:\n\t{0}".format(cmd), "DEBUG")
cmd_run = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
cmd_run.wait()
cmd_run_stdout = cmd_run.stdout.read()
if not cmd_run_stdout:
if int(cmd_run.returncode) == 0:
cmd_run_stdout = "Job succeeded"
else:
cmd_run_stdout = "Job failed"
ret = {cmd_run.returncode: cmd_run_stdout}
except Exception as e:
returncode = 1
ret = {returncode: e}
print(printcolors(ret, "FAIL"))
pass
zlog("[spawn_job] return:\n\t{0}".format(ret), "DEBUG")
return ret
def spawn_jobs(*args):
'''
function expects a list
'''
zlog("args of type {0} passed to spawn_jobs:\n\t{1}".format(type(args), args), "DEBUG")
output = {}
ret = []
try:
for arg in args:
if isinstance(arg, list):
for list_item in arg:
zlog("starting background job:\n\t{0}".format(list_item), "INFO")
output["{0}".format(list_item)] = subprocess.Popen(shlex.split(list_item), stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
else:
zlog("spawn_jobs expected a list, instead recieved a {0}".format(type(arg)), "CRITICAL")
for out in output.itervalues():
zlog("issuing wait to object:\n\t{0}".format(out), "INFO")
out.wait()
stdout = out.stdout.read()
if not stdout:
if int(out.returncode) == 0:
stdout = "Job succeeded"
else:
stdout = "Job failed"
ret.append({out.returncode: stdout})
except Exception as e:
returncode = 1
ret.append({returncode: e})
zlog("{0}".format(e), "ERROR")
pass
return ret
def ssh_keygen(keyfile, remote_ssh=False):
'''
'''
import paramiko
ret = {}
if zettaknight_globs.help_flag:
ret = """SSH Keygen:
Function to create a ssh key - either the default Zettaknight key defined in Zettaknight conf files, or a keyfile passed in as an argument.
Usage:
zettaknight ssh_keygen
Optional Arguments:
keyfile
Specifies a location for the new keyfile. By default this information is pulled from configuration files.
remote_ssh
Specifies a remote host to copy the ssh key to.
Normally this function is called by other Zettaknight functions and does not need to be called directly."""
return ret
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn]['Generate SSH Key'] = {}
if not keyfile:
keyfile=zettaknight_globs.identity_file
ssh_cmd = "bash {0} -k {1}".format(zettaknight_globs.ssh_keygen_script, keyfile)
if remote_ssh:
ssh_cmd = "{0} -r {1}".format(ssh_cmd, remote_ssh)
try:
user, remote = remote_ssh.split("@")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote, username=user, key_filename=keyfile)
remote_sudo_cmd = "hostname"
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(remote_sudo_cmd)
ssh_output = ssh_stdout.readlines()
print(ssh_output)
if ssh_output:
ret[zettaknight_globs.fqdn]['Generate SSH Key'] = {'0': "Nothing to do.\nKey authentication already setup for {0} with keyfile {1}".format(remote_ssh, keyfile)}
#parse_output(ret)
#return ret
except Exception as e:
pass
try:
ret[zettaknight_globs.fqdn]['Generate SSH Key'] = spawn_job(ssh_cmd)
except Exception as e:
ret[zettaknight_globs.fqdn]['Generate SSH Key']['1'] = {}
ret[zettaknight_globs.fqdn]['Generate SSH Key']['1'] = e
print(printcolors(e,"FAIL"))
#arse_output(ret)
return ret
def replace_keys(**kwargs):
'''
'''
ret = {}
if zettaknight_globs.help_flag:
ret = """Replace Keys:
Function to create a new ssh key and replace old luks keys on defined luks devices
Usage:
zettaknight replace_keys keyfile=<keyfile>
Required Arguments:
keyfile
Specifies a name for the new keyfile.
Optional Arguments:
delete
If delete=True is specified, the old keyfile will be deleted after it is replaced.
"""
return ret
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn]['Replace SSH Keys'] = {}
ret[zettaknight_globs.fqdn]['Create SSH Key'] = {}
ret[zettaknight_globs.fqdn]['Replace SSH Keys']['1'] = {}
if 'keyfile' in kwargs.iterkeys():
keyfile = kwargs['keyfile']
if not os.path.exists(keyfile):
ret[zettaknight_globs.fqdn]['Create SSH Key'] = ssh_keygen(keyfile)
else:
ret[zettaknight_globs.fqdn]['Replace SSH Keys']['1'] = "keyfile is empty, replace_keys requires a keyfile"
return ret
try:
luks_key_cmd = "bash {0} -k {1}".format(zettaknight_globs.luks_key_script, keyfile)
if 'delete' in kwargs.iterkeys():
luks_key_cmd = "{0} -k {1} -d".format(zettaknight_globs.luks_key_script, destination_id)
ret[zettaknight_globs.fqdn]['Replace Luks Keys'] = spawn_job(luks_key_cmd)
except Exception as e:
zlog("{0}".format(e), "ERROR")
ret[zettaknight_globs.fqdn]['Replace SSH Keys']['1'] = e
return ret
def backup_luks_headers(**kwargs):
ret = {}
if zettaknight_globs.help_flag:
ret = """Backup LUKS Headers:
Function to backup headers for currently defined LUKS devices. By default, headers are backed up to the
Zettaknight store defined in configuration files. Target argument can be supplied to redirect where the
headers are backed up to.
Usage:
zettaknight backup_luks_headers (target=<output directory>)
Optional Arguments:
target
Redirects output to provided directory.
"""
return ret
ret[zettaknight_globs.fqdn] = {}
#set location for luks header backups if empty
if 'target' in kwargs.iterkeys():
target = kwargs['target']
else:
target = zettaknight_globs.luks_header_dir
try:
if target:
if not os.path.isdir(target):
os.mkdir(target)
luks_backup_cmd = "bash {0} -l {1}".format(zettaknight_globs.luks_header_backup_script, target)
ret[zettaknight_globs.fqdn]['Backup Luks Headers'] = spawn_job(luks_backup_cmd)
except Exception as e:
ret[zettaknight_globs.fqdn]['1'] = e
pass
return ret
def check_quiet(quiet):
if quiet:
if str(quiet) is not "quiet":
try:
raise Exception("{0} argument not recognized by function: {1}".format(quiet, inspect.stack()[1][3]))
except Exception as e:
zlog("{0}".format(e), "WARNING")
sys.exit(0)
return quiet
def create_config(**kwargs):
'''
'''
ret = {}
if zettaknight_globs.help_flag:
ret = """Create Config:
Function to create/update configuration files for newly created or previously unmanaged datasets.
User will be queried to provide any information that is not provided as an argument or in configuration
files.
Usage:
zettaknight create_config (dataset=<dataset> <arg>=<value>)
Optional Arguments:
dataset
Dataset name configuration is to be created for
user
Username to use for snapshot replication
quota
Quota to set on dataset
refquota
Refquota to set on dataset
reservation
Reservation to set on dataset
refreservation
Refreservation to set on dataset
retention
Number of days to keep snapshots
secure
Whether snapshots should be replicated over SSH
contact
Contact e-mail to send job error output to.
interval
Interval at which to take snapshots.
remote_server
Remote server to replicate snapshots to."""
return ret
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn]['Create Config'] = {}
#print(kwargs)
new_conf = {}
#create var dictionary and depth
var = {}
var['snap'] = {}
var['snap']['interval'] = {}
var['snap']['remote_server'] = {}
var['reservation'] = {}
var['refreservation'] = {}
var['quota'] = {}
var['refquota'] = {}
var['secure'] = {}
var['user'] = {}
var['retention'] = {}
var['contact'] = {}
dataset_list = []
#config_dict = zettaknight_globs.zfs_conf
conff = open(zettaknight_globs.config_file_new, 'r')
config_dict = yaml.safe_load(conff)
#print(config_dict)
#if a dataset is passed in, only do work for that particular dataset
if 'dataset' in kwargs.iterkeys():
#print("a dataset was passed in : {0}").format(kwargs['dataset'])
dataset = kwargs['dataset']
dataset_list.append(dataset)
else:
if config_dict:
for dset in config_dict.iterkeys():
if not dset in dataset_list:
#print("dataset {0} defined in configuration file, not in arguments. Adding to list".format(dset))
dataset_list.append(dset)
#print("dataset_list = {0}".format(dataset_list))
for dataset in dataset_list:
#print(dataset)
if 'defaults' in config_dict.iterkeys():
if 'user' in config_dict['defaults'].iterkeys():
var['user'] = config_dict['defaults']['user']
if 'quota' in config_dict['defaults'].iterkeys():
var['quota'] = config_dict['defaults']['quota']
if 'refquota' in config_dict['defaults'].iterkeys():
var['refquota'] = config_dict['defaults']['refquota']
if 'reservation' in config_dict['defaults'].iterkeys():
var['reservation'] = config_dict['defaults']['reservation']
if 'refreservation' in config_dict['defaults'].iterkeys():
var['refreservation'] = config_dict['defaults']['refreservation']
if 'retention' in config_dict['defaults'].iterkeys():
var['retention'] = config_dict['defaults']['retention']
if 'secure' in config_dict['defaults'].iterkeys():
var['secure'] = config_dict['defaults']['secure']
if 'contact' in config_dict['defaults'].iterkeys():
var['contact'] = config_dict['defaults']['contact']
if 'snap' in config_dict['defaults'].iterkeys():
if 'interval' in config_dict['defaults']['snap'].iterkeys():
var['snap']['interval'] = config_dict['defaults']['snap']['interval']
if 'remote_server' in config_dict['defaults']['snap'].iterkeys():
var['snap']['remote_server'] = config_dict['defaults']['snap']['remote_server']
# if kwargs are passed in, overwrite defaults
if 'user' in kwargs.iterkeys():
var['user'] = kwargs['user']
if 'quota' in kwargs.iterkeys():
var['quota'] = kwargs['quota']
if 'refquota' in kwargs.iterkeys():
var['refquota'] = kwargs['refquota']
if 'reservation' in kwargs.iterkeys():
var['reservation'] = kwargs['reservation']
if 'refreservation' in kwargs.iterkeys():
var['refreservation'] = kwargs['refreservation']
if 'retention' in kwargs.iterkeys():
var['retention'] = kwargs['retention']
if 'secure' in kwargs.iterkeys():
var['secure'] = kwargs['secure']
if 'contact' in kwargs.iterkeys():
#var['contact'] = kwargs['contact']
var['contact'] = list(strip_input(kwargs['contact']).split(" "))
if 'interval' in kwargs.iterkeys():
#var['snap']['interval'] = kwargs['interval']
var['snap']['interval'] = list(strip_input(kwargs['interval']).split(" "))
if 'remote_server' in kwargs.iterkeys():
#var['snap']['remote_server'] = kwargs['remote_server']
var['snap']['remote_server'] = list(strip_input(kwargs['remote_server']).split(" "))
#test if defaults and kwargs are empty, if so, prompt user for input
if not var['user']:
var['user'] = query_return_item("username for snapshot replication? ")
if not var['quota']:
var['quota'] = query_return_item("Enter quota for {0}: ".format(dataset))
if not var['refquota']:
var['refquota'] = 'none'
if not var['reservation']:
var['reservation'] = query_return_item("Enter reservation for {0}: ".format(dataset))
if not var['refreservation']:
var['refreservation'] = 'none'
if not var['retention']:
var['retention'] = query_return_item("How many days would you like zettaknight to keep snapshots for {0}: ".format(dataset))
if not var['secure']:
var['secure'] = query_yes_no("send snaps over ssh?: ")
if not var['contact']:
var['contact'] = query_return_list("Who do you want zettaknight to contact when an error is encountered? [[email protected] [email protected] ...]")
if not var['snap']['interval']:
var['snap']['interval'] = query_return_list("How often would you like zettaknight to run backups?\ni.e run every 4 hours [everyhour=4], to run at 10:30am, [hour=10]")
if not var['snap']['remote_server']:
var['snap']['remote_server'] = query_return_list("What server(s) do you want to replicate to? Will accept DNS names or IP addresses")
#if var is defined for in defaults, and
if 'defaults' in config_dict.iterkeys():
if 'user' in config_dict['defaults'].iterkeys():
if var['user'] == config_dict['defaults']['user']:
del var['user']
if 'quota' in config_dict['defaults'].iterkeys():
if var['quota'] == config_dict['defaults']['quota']:
del var['quota']
if 'refquota' in config_dict['defaults'].iterkeys():
if var['refquota'] == config_dict['defaults']['refquota']:
del var['refquota']
if 'reservation' in config_dict['defaults'].iterkeys():
if var['reservation'] == config_dict['defaults']['reservation']:
del var['reservation']
if 'refreservation' in config_dict['defaults'].iterkeys():
if var['refreservation'] == config_dict['defaults']['refreservation']:
del var['refreservation']
if 'retention' in config_dict['defaults'].iterkeys():
if var['retention'] == config_dict['defaults']['retention']:
del var['retention']
if 'secure' in config_dict['defaults'].iterkeys():
if var['secure'] == config_dict['defaults']['secure']:
del var['secure']
if 'contact' in config_dict['defaults'].iterkeys():
if var['contact'] == config_dict['defaults']['contact']:
del var['contact']
if 'snap' in config_dict['defaults'].iterkeys():
if 'interval' in config_dict['defaults']['snap'].iterkeys():
if var['snap']['interval'] == config_dict['defaults']['snap']['interval']:
del var['snap']['interval']
if 'remote_server' in config_dict['defaults']['snap'].iterkeys():
if var['snap']['remote_server'] == config_dict['defaults']['snap']['remote_server']:
del var['snap']['remote_server']
if not var['snap']: #if var['snap'] is empty, remove it so it is not printed by the yaml.safe.dump
del var['snap']
new_conf[str(dataset)] = var
config_dict[str(dataset)] = var
print(printcolors("\n\n\nThe following will be added to {0}\n", "HEADER").format(zettaknight_globs.config_file_new))
out = yaml.safe_dump(new_conf, default_flow_style=False)
out2 = yaml.safe_dump(config_dict, default_flow_style=False)
print(out)
try:
response = query_yes_no("Would you like to commit changes?")
if response:
with open(zettaknight_globs.config_file_new, "w") as myfile:
myfile.write(yaml.safe_dump(config_dict, default_flow_style=False))
print(printcolors("changes committed", "OKGREEN"))
ret[zettaknight_globs.fqdn]['Create Config']['0'] = {}
ret[zettaknight_globs.fqdn]['Create Config']['0'] = out2
else:
print(printcolors("exit requested", "WARNING"))
return
except Exception as e:
ret[zettaknight_globs.fqdn]['Create Config']['1'] = e
return ret
def sharenfs(*args):
'''
Updates /etc/exports, runs exportfs -ar
'''
ret ={}
if zettaknight_globs.help_flag:
ret = """ShareNFS:
Function to add entries in /etc/exports for provided dataset and iprange, and export the newly defined share.
Usage:
zettaknight sharenfs <dataset> <ip_range>
Required Arguments:
dataset
The dataset to begin sharing
ip_range
The IP or IP_range to share provided dataset to."""
return ret
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn]['Create NFS Share'] = {}
if len(args) != 2:
ret[zettaknight_globs.fqdn]['Create NFS Share']['1'] = "sharenfs function takes exactly 2 arguments (dataset, ip_range)"
parse_output(ret)
return
dataset = args[0]
ip_range = args[1]
#query datasets and verify provided dataset exists
check_dset_cmd = "/sbin/zfs list -o name -H {0}".format(dataset)
check_dset_run = spawn_job(check_dset_cmd)
chk_code, chk_msg = check_dset_run.popitem()
if int(chk_code) == 1:
ret[zettaknight_globs.fqdn]['Create NFS Share']['1'] = "Provided dataset {0} does not exist.".format(dataset)
parse_output(ret)
return
#Check if dataset already defined in /etc/exports
check_share_cmd = "/bin/grep '^/{0}' /etc/exports".format(dataset)
check_share_run = spawn_job(check_share_cmd)
chk_code, chk_msg = check_share_run.popitem()
if int(chk_code) == 1:
#add share
print("\n\nAdding share!\n\n")
new_share_cmd = "/bin/echo /{0} {1}(rw,async,no_root_squash)".format(dataset, ip_range)
new_share_tee = "tee -a /etc/exports"
new_share_run = pipe_this(new_share_cmd, new_share_tee)
new_share_run = {new_share_run.returncode : new_share_run.stdout.read()}
chk_code, chk_msg = new_share_run.popitem()
if int(chk_code) == 1:
ret[zettaknight_globs.fqdn]['Create NFS Share']['1'] = chk_msg
parse_output(ret)
return
else:
if ip_range not in chk_msg:
#append requested ip_range to existing share definition
new_chk_msg = "{0},{1}(rw,async,no_root_squash)".format(chk_msg, ip_range)
export_sed_cmd = "/bin/sed -i 's/{0}/{1}/' /etc/exports".format(chk_msg, new_chk_msg)
export_sed_run = spawn_job(export_sed_cmd)
chk_code, chk_msg = export_sed_run.popitem()
if int(chk_code) == 1:
ret[zettaknight_globs.fqdn]['Create NFS Share']['1'] = chk_msg
parse_output(ret)
return
export_cmd = "/usr/sbin/exportfs -arv"
ret[zettaknight_globs.fqdn]['Create NFS Share'] = spawn_job(export_cmd)
return ret
def sharesmb(*args):
'''
needs to update a block of information for /etc/samba/smb.conf
/etc/samba.conf
[<share>]
...OPTIONS
'''
#smbpasswd -a <user>
return
def strip_input(msg):
'''
intended to strip the leading a trailing whitespace from passed in message
'''
if msg.startswith(" "):
msg = msg[1:]
if msg.endswith(" "):
cut_index = len(msg) - 1
msg = msg[:cut_index]
return msg
def query_return_item(question):
'''
'''
print(printcolors(str(question), "WARNING"))
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
input = raw_input().lower()
answer = strip_input(input)
return answer
def query_return_list(question):
'''
'''
resp_list = []
print(printcolors("{0}".format(question), "WARNING"))
print(printcolors("WARNING: Multiples entries separated by whitespace", "HEADER"))
try:
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
input = raw_input().lower()
#print("query_return_list was given: {0}".format(input))
resp_list = list(strip_input(input).split(" "))
#print("returning: {0}".format(resp_list))
except Exception as e:
zlog("{0}".format(e), "CRITICAL")
sys.exit(1)
return resp_list
def query_yes_no(question, default="yes"):
valid = {}
valid['yes'] = True
valid['no'] = False
if default is None:
prompt = " [yes/no] "
elif default == "yes":
prompt = " [YES/no] "
elif default == "no":
prompt = " [yes/NO] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
#sys.stdout.write(question + prompt)
print(printcolors(str(question) + str(prompt), "WARNING"))
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
#print(valid[choice])
return valid[choice]
else:
print(printcolors("Please respond with 'yes' or 'no'", "FAIL"))
def backup_files(*args):
'''
Accepts a list of files and backs them up to a zfs filesystem
'''
ret = {}
if zettaknight_globs.help_flag:
ret = """Backup Files:
Function to backup Zettaknight configuration files, as well as other files relevant to pool and dataset functionality.
ie. /etc/exports and /etc/crypttab
Usage:
zettaknight backup_files"""
return ret
ret[zettaknight_globs.fqdn] = {}
ret[zettaknight_globs.fqdn][zettaknight_globs.zettaknight_store] = {}
ret[zettaknight_globs.fqdn][zettaknight_globs.zettaknight_store]['0'] = {}
ret[zettaknight_globs.fqdn][zettaknight_globs.zettaknight_store]['1'] = {}