39
39
#
40
40
41
41
import argparse
42
+ import logging
42
43
import sys
43
44
import subprocess
44
45
import json
48
49
from fnmatch import fnmatch
49
50
from enum import StrEnum , auto
50
51
52
+ from lib .commands import ssh
53
+
51
54
class DataType (StrEnum ):
52
55
FILE = auto ()
53
56
FILE_SYMLINK = auto ()
@@ -62,15 +65,6 @@ def ignore_file(filename, ignored_files):
62
65
63
66
return False
64
67
65
- def ssh_cmd (host , cmd ):
66
- args = ["ssh" , f"root@{ host } " , cmd ]
67
-
68
- cmdres = subprocess .run (args , capture_output = True , text = True )
69
- if cmdres .returncode :
70
- raise Exception (cmdres .stderr )
71
-
72
- return cmdres .stdout
73
-
74
68
def ssh_get_files (host , file_type , folders ):
75
69
md5sum = False
76
70
readlink = False
@@ -101,7 +95,7 @@ def ssh_get_files(host, file_type, folders):
101
95
# This is much more efficient than using find '-exec md5sum {}'
102
96
find_cmd += " -print0 | xargs -0 md5sum"
103
97
104
- rawres = ssh_cmd (host , find_cmd )
98
+ rawres = ssh (host , [ find_cmd ] )
105
99
106
100
res = dict ()
107
101
for line in rawres .splitlines ():
@@ -113,7 +107,7 @@ def ssh_get_files(host, file_type, folders):
113
107
def ssh_get_packages (host ):
114
108
packages = dict ()
115
109
116
- res = ssh_cmd (host , "rpm -qa --queryformat '%{NAME} %{VERSION}\n '" )
110
+ res = ssh (host , [ "rpm -qa --queryformat '%{NAME} %{VERSION}\n '" ] )
117
111
for line in res .splitlines ():
118
112
entries = line .split (' ' , 1 )
119
113
packages [entries [0 ]] = entries [1 ]
@@ -131,7 +125,7 @@ def get_data(host, folders):
131
125
ref_data [DataType .PACKAGE ] = ssh_get_packages (host )
132
126
except Exception as e :
133
127
print (e , file = sys .stderr )
134
- exit (- 1 )
128
+ exit (1 )
135
129
136
130
return ref_data
137
131
@@ -160,8 +154,8 @@ def remote_diff(host_ref, host_test, filename):
160
154
file_test = None
161
155
162
156
# check remote files are text files
163
- cmd = f "file -b { shlex .quote (filename )} "
164
- file_type = ssh_cmd (host_ref , cmd )
157
+ cmd = [ "file" , "-b" , shlex .quote (filename )]
158
+ file_type = ssh (host_ref , cmd )
165
159
if not file_type .lower ().startswith ("ascii" ):
166
160
print ("Binary file. Not showing diff" )
167
161
return
@@ -292,7 +286,7 @@ def load_reference_files(filename):
292
286
return json .load (fd )
293
287
except Exception as e :
294
288
print (f"Error: { e } " , file = sys .stderr )
295
- exit (- 1 )
289
+ exit (1 )
296
290
297
291
# Save files from a reference host in json format
298
292
def save_reference_data (files , filename ):
@@ -301,9 +295,11 @@ def save_reference_data(files, filename):
301
295
json .dump (files , fd , indent = 4 )
302
296
except Exception as e :
303
297
print (f"Error: { e } " , file = sys .stderr )
304
- exit (- 1 )
298
+ exit (1 )
305
299
306
300
def main ():
301
+ logging .basicConfig (format = '[%(levelname)s] %(message)s' , level = logging .INFO )
302
+
307
303
ref_data = None
308
304
folders = ["/boot" , "/etc" , "/opt" , "/usr" ]
309
305
ignored_file_patterns = [
@@ -347,6 +343,7 @@ def main():
347
343
'/etc/sysconfig/xencommons' ,
348
344
'/etc/sysctl.d/91-net-ipv6.conf' ,
349
345
'/etc/vconsole.conf' ,
346
+ '/etc/xapi.d/plugins/vmssc' ,
350
347
'/etc/xsconsole/state.txt' ,
351
348
'/etc/xensource-inventory' ,
352
349
'/etc/xensource/boot_time_cpus' ,
@@ -385,31 +382,31 @@ def main():
385
382
386
383
if args .ref_host is None and args .show_diff :
387
384
print ("Missing parameters. -d must be used with -r. Try --help" , file = sys .stderr )
388
- return - 1
385
+ return 1
389
386
390
387
if args .load_ref :
391
388
if not args .json_output :
392
- print ( f "Get reference data from { args .load_ref } " )
389
+ logging . info ( "Get reference data from %s" , args .load_ref )
393
390
ref_data = load_reference_files (args .load_ref )
394
391
elif args .ref_host :
395
392
if not args .json_output :
396
- print ( f "Get reference data from { args .ref_host } " )
393
+ logging . info ( "Get reference data from %s" , args .ref_host )
397
394
ref_data = get_data (args .ref_host , args .folders )
398
395
399
396
if args .save_ref :
400
397
if not args .json_output :
401
- print ( f "Saving reference data to { args .save_ref } " )
398
+ logging . info ( "Saving reference data to %s" , args .save_ref )
402
399
save_reference_data (ref_data , args .save_ref )
403
400
404
401
if ref_data is None or args .test_host is None :
405
402
if args .save_ref :
406
403
return 0
407
404
408
405
print ("\n Missing parameters. Try --help" , file = sys .stderr )
409
- return - 1
406
+ return 1
410
407
411
408
if not args .json_output :
412
- print ( f "Get test host data from { args .test_host } " )
409
+ logging . info ( "Get test host data from %s" , args .test_host )
413
410
test_data = get_data (args .test_host , args .folders )
414
411
415
412
ref = dict ([('data' , ref_data ), ('host' , args .ref_host )])
0 commit comments