File: medusa_controller.py

package info (click to toggle)
medusa 2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,260 kB
  • sloc: ansic: 19,185; sh: 9,421; python: 294; makefile: 163; perl: 120
file content (286 lines) | stat: -rwxr-xr-x 10,150 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python
import xmlrpclib, re, sys, socket
from optparse import OptionParser
from time import strftime

#static global variables 
MEDUSA_VER = "2.1_devel"
status_log = open("status.log",'a')
pattern = re.compile ('error', re.IGNORECASE)
module_pattern = re.compile('.*\.mod : .*? : version.*', re.IGNORECASE)
services_pattern = re.compile('error', re.IGNORECASE)
time = strftime("%Y-%m-%d %H:%M:%S")


#function time...wooo its function time. 
def test_medusa_modules(host):
	host_log = open(host.strip('\n'), 'a')
	module_error_flag = False
	host_log = open(host, 'a')
	host_ip = socket.gethostbyname(host)
	try:
		proxy = xmlrpclib.ServerProxy("http://" + host_ip + ":31137/")
	except socket.error:
		status_log.write("%s: Unable to connect %s.\n" %(time,host))
		return False
		
	#Test medusa modules
	try:
		module_test_data = proxy.medusa_module_test()
	except socket.error:
		status_log.write("%s: Unable to connect %s.\n" %(time,host))
		return False	
		
	for x in module_test_data:
		host_log.write( "%s: %s: Module build information:%s.\n" %(time,host,x.strip('\n')))
		#matches = module_pattern.search(str(x))
		#if matches != None:
		#	status_log.write( "%s: %s: Module failed to build:%s.\n" %(time,host,x.strip('\n')))
		#	module_error_flag = True
		
	#if not module_error_flag:
	#	host_log.write("%s: %s: Modules were complied correctly.\n" %(time, host,))

def test_medusa(host,config_file):		
	#test medusa services
	host_log = open(host.strip('\n'), 'a')
	host_log = open(host, 'a')
	
	host_ip = socket.gethostbyname(host)
	try:
		proxy = xmlrpclib.ServerProxy("http://" + host_ip + ":31137/")
	except socket.error:
		status_log.write("%s: Unable to connect %s.\n" %(time,host))
		return False
		
	tests = read_file(config_file)
	for x in tests:
		vars = x.split(':', 4)
		target_host = vars[0]
		service = vars[1]
		user_name = vars[2]
		passwd = vars[3]
		if len(vars) != 4 :
			options = vars[4]
		else:
			options = " "
		host_log.write("\n%s: Starting medusa tests for %s.\n" %(time, target_host))
		test_data = proxy.run_medusa_tests(service, target_host,user_name,passwd,options)
		matched  = services_pattern.search(test_data)
		if matched:
			host_log.write(test_data + "\n")
			status_log.write("%s: %s: Failed service test: %s on target: %s.\n" %(time,host,service,target_host))
		else:
			host_log.write(test_data + "\n")
			status_log.write("%s: %s: passed service test : %s on target host: %s.\n" %(time,host, service, target_host))
		
	return True


def install_medusa(host):
	fail_check_flag = False
	host_ip = socket.gethostbyname(host)
	try:
		proxy = xmlrpclib.ServerProxy("http://" + host_ip + ":31137/")
		file_path = proxy.download_file("http://www.foofus.net/jmk/tmp/medusa-%s.tar.gz"  %(MEDUSA_VER,))
	except socket.error:
		status_log.write("%s: Unable to connect %s\n" %(time,host,))
		return False
		
	#Open log after we make sure that we can open a connect to the remote host. 
	host_log = open(host.strip('\n'), 'a')	
	
	
	configure_log_data= proxy.configure(file_path)
	host_log.write("%s: Issuing configure:\n" %(time,))
	for x in configure_log_data:
		try:
			matches = pattern.search(str(x))
			if matches:
				status_log.write("%s: %s: Configure failed please check logs for %s.\n" %(time, host.strip('\n'),host.strip('\n')))
				host_log.write( x + "\n")
				fail_check_flag = True
			else:
				host_log.write(x + "\n")
		except UnicodeEncodeError:
                        pass

	make_log_data = proxy.make_medusa()
	host_log.write("%s: Issuing make:\n" %(time,))
	for x in make_log_data:
		try:
			matches = pattern.search(str(x))
			if matches:
				status_log.write("%s: %s: Make failed please check logs for %s.\n" %(time, host.strip('\n'),host.strip('\n')))
				host_log.write(x+ "\n")
				fail_check_flag = True
			else:
				host_log.write(x + "\n")
		except UnicodeEncodeError:
			pass

	make_install_log = proxy.install_medusa()
	host_log.write("%s: Issuing make install:\n" %(time,))
	for x in make_install_log:
		try:
			matches = pattern.search(str(x))
			if matches:
				status_log.write("%s: %s: Make install failed please check logs for %s.\n" %(time,host.strip('\n'),host.strip('\n')))
				host_log.write(x+ "\n")
				fail_check_flag = True
			else:
				host_log.write(x + "\n")
		except UnicodeEncodeError:
                        pass
		
	if not fail_check_flag:
		proxy.cleanup_install_files()
		status_log.write( "%s: %s: Passed medusa install.\n" %(time,host.strip('\n')))
		return True
	else:
		return False
	

def build_medusa(host):
	fail_check_flag = False
	host_ip = socket.gethostbyname(host)
	try:
		proxy = xmlrpclib.ServerProxy("http://" + host_ip + ":31137/")
		file_path = proxy.download_file("http://www.foofus.net/jmk/tmp/medusa-%s.tar.gz"  %(MEDUSA_VER,))
	except socket.error:
		status_log.write("%s: Unable to connect %s\n" %(time,host,))
		return False
		
	#Open log after we make sure that we can open a connect to the remote host. 
	host_log = open(host.strip('\n'), 'a')	
	
	configure_log_data= proxy.configure(file_path)
	host_log.write("%s: Issuing configure:\n" %(time,))
	for x in configure_log_data:
		try:
			matches = pattern.search(str(x))
			if matches:
				status_log.write("%s: %s: Configure failed please check logs for %s.\n" %(time, host.strip('\n'),host.strip('\n')))
				host_log.write( x + "\n")
				fail_check_flag = True
			else:
				host_log.write(x + "\n")
		except UnicodeEncodeError:
                        pass

	make_log_data = proxy.make_medusa()
	host_log.write("%s: Issuing make:\n" %(time,))
	for x in make_log_data:
		try:
			matches = pattern.search(str(x))
			if matches:
				status_log.write("%s: %s: Make failed please check logs for %s.\n" %(time, host.strip('\n'),host.strip('\n')))
				host_log.write(x+ "\n")
				fail_check_flag = True
			else:
				host_log.write(x + "\n")
		except UnicodeEncodeError:
			pass
	if not fail_check_flag:
		proxy.cleanup_install_files()
		status_log.write( "%s: %s: Passed medusa configure and build.\n" %(time,host.strip('\n')))
		return True
	else:
		return False

def read_file ( filename):
	try:
		f = open( filename, 'r')
		list = f.readlines()
		return list
	except IOError:
		print "Error: Can not read %s.\n" %(filename,)
		sys.exit(1)

def options_list(usage):
	parser = OptionParser()
	parser.add_option( "-c", "--config",dest ="config_file", help="-c/--config: Configuration file for the device you want to test medusa against\n")
	parser.add_option("-f", "--file", dest="hosts_file", help="-f/--file: File with a lists of hosts to build medusa on\n")
	parser.add_option("-H","--host", dest="host", help="-H/--host:  host to build medusa on\n")
	parser.add_option("-i", "--install", dest ="install_medusa" ,action="store_true", help= "-i/--install: used to install medusa on host\n")
	parser.add_option("-t", "--test", dest="test_medusa", action="store_true", help="-t/-test:Just run medusa tests on hosts\n")
	parser.add_option("-b", "--build", dest="build_medusa", action="store_true", help="-b/--build will download and build medusa. It will not install it.")
	(options, args) = parser.parse_args()
	config_file = options.config_file
	hosts_file = options.hosts_file
	host = options.host
	install_medusa_flag = options.install_medusa
	test_medusa_flag = options.test_medusa
	build_medusa_flag = options.build_medusa
	
	if hosts_file == None and host == None:
		print "Missing option.\nUsage: %s\n" %(usage,)
		sys.exit(1)
	elif config_file == None and test_medusa_flag == True:
		print "Missing option.\nUsage: %s\n" %(usage,)
		sys.exit(1)
	
	else:
		if hosts_file == None:
		   hosts_file = 0
		return (hosts_file,host,config_file, install_medusa_flag, test_medusa_flag, build_medusa_flag)


def main():
	usage = "-f/--file: File with a list of hosts to build medusa on\n-H/--host: Host to build medusa on\n-c/--config: Configuration file for the device you want to test medusa on\nFile format is as follows: host:service:user:pass.\n-i/--install: used to install medusa on host.\n-t/--test:Just run medusa tests on hosts.\n-b/-build will download and build medusa. It will not install it.\n"
	(hosts_file,host, config_file, install_medusa_flag, test_medusa_flag, build_medusa_flag) = options_list(usage)
		
	if hosts_file != 0:
		hosts = read_file(hosts_file)
		if install_medusa_flag and test_medusa_flag == 1:
			print "Plase wait while Medusa is installed and tested are being run.\n"
			for host in hosts:
				status = install_medusa(host.strip('\n'))
				if status:
					test_medusa_modules(host.strip('\n'))
					test_medusa(host.strip('\n'), config_file)	
		elif  install_medusa_flag == 1:
			print "Plase wait while medusa is being installed\n"
			for host in hosts:
				install_medusa(host.strip('\n'))
				test_medusa_modules(host.strip('\n'))
		elif test_medusa_flag == 1:
			print "Please wait while medusa is being tested\n"
			for host in hosts:
				test_medusa(host.strip('n'), config_file)
		elif build_medusa_flag == 1:
			print "Please wait while medusa is being build\n"
			for host in hosts:
				build_medusa(host.strip('\n'))
		else:
			print "Do you know what you are doing?\n" + usage + "\n"
			sys.exit(1)
	else: 
		if install_medusa_flag and test_medusa_flag == 1:
                	print "Plase wait while Medusa is installed and tested are being run.\n"
                        status = install_medusa(host)
                        if status:
				test_medusa_modules(host)
                        	test_medusa(host, config_file)	
        	elif  install_medusa_flag == 1:
                	print "Plase wait while medusa is being installed\n"
                        install_medusa(host)
			test_medusa_modules(host)
        	elif test_medusa_flag == 1:
                	print "Please wait while medusa is being tested\n"
                        test_medusa(host, config_file)
		elif build_medusa_flag == 1:
			build_medusa(host.strip('\n'))
		else:
		    print "Do you know what you are doing?\n" + usage + "\n"
		    sys.exit(1)

	


if __name__ == "__main__":
	try:
		main()
		print "Program completed"
	except KeyboardInterrupt:
		print "\noooo got a CTL-C from the console. Exiting. I hope next time you know what you are doing."