remove gotan from the lxc tests
[tests.git] / system / PlcapiUrlScanner.py
1 #!/usr/bin/env python
2 #
3 # this checks various forms of URLS for reaching a PLCAPI
4 # i.e. with http:// or https:// (only the latter is expected to work)
5 # with or without a trailing slash
6 # using a hostname or an IP
7
8 import socket
9 import xmlrpclib
10 import traceback
11
12 class PlcapiUrlScanner:
13
14     # turns out the config has an ip but no name..
15     def __init__ (self, auth, hostname=None, ip=None, verbose=False):
16         self.auth=auth
17         if not hostname and not ip:
18             raise Exception,"PlcapiUrlScanner needs _some_ input"
19         if hostname:
20             if not ip: 
21                 try:    ip=socket.gethostbyname(hostname)
22                 except: 
23                     hostname="%s.pl.sophia.inria.fr"%hostname
24                     ip=socket.gethostbyname(hostname)
25         else:
26             if not hostname: hostname=socket.gethostbyaddr(ip)[0]
27         self.hostname=hostname
28         self.ip=ip
29         self.verbose=verbose
30         
31     def try_url (self,url):
32         try:
33             xmlrpclib.ServerProxy (url, verbose=self.verbose, allow_none=True).GetNodes(self.auth)
34             print 'YES',url
35             return True
36         except xmlrpclib.ProtocolError as e:
37             print '... (http error %s)'%e.errcode,url
38             return False
39         except Exception as e:
40             print '---',type(e).__name__,url,e
41             if self.verbose: traceback.print_exc()
42             return False
43
44     def try_url_required (self, url, required):
45         result=self.try_url(url)
46         if required and not result:     return False
47         else:                           return True
48
49     def scan(self):
50         overall=True
51         for protocol in ['http','https']:
52             for dest in [ self.hostname, self.ip ]:
53                 for port in [ '',':80',':443']:
54                     for path in ['PLCAPI','PLCAPI/']:
55                         if protocol=='http' and port==':443': continue
56                         if protocol=='https' and port==':80': continue
57                         required = (protocol=='https') and (path=='PLCAPI/')
58                         url="%s://%s%s/%s"%(protocol,dest,port,path)
59                         if not self.try_url_required (url,required): overall=False
60         return overall
61
62 from optparse import OptionParser
63 import sys
64
65 auth={'AuthMethod':'password','Username':'root@test.onelab.eu','AuthString':'test++'}
66
67 def main ():
68     usage="%prog hostname"
69     parser=OptionParser()
70     parser.add_option("-v","--verbose",dest='verbose',action='store_true',default=False)
71     (options,args)=parser.parse_args()
72     if len(args)!=1:
73         parser.print_help()
74         sys.exit(1)
75     hostname=args[0]
76     success=PlcapiUrlScanner (auth=auth, hostname=hostname,verbose=options.verbose).scan()
77     sys.exit(0 if success else -1)
78
79 if __name__ == '__main__':
80     main()