Fixed black tray icon issue (new method for checking if SDVM is started)
authorBarthaM@N3SIM1218.D03.arc.local
Thu, 14 Aug 2014 09:51:11 +0100
changeset 2174162648fb167
parent 216 358381a8c60a
child 218 327f282364b9
Fixed black tray icon issue (new method for checking if SDVM is started)
New configuration check on the hostonly IF and DHCP server
OpenSecurity/bin/test_vmmanager.pyw
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/test_vmmanager.pyw	Wed Aug 06 17:05:54 2014 +0100
     1.2 +++ b/OpenSecurity/bin/test_vmmanager.pyw	Thu Aug 14 09:51:11 2014 +0100
     1.3 @@ -88,7 +88,8 @@
     1.4              return proxy['ProxyServer']
     1.5          else: 
     1.6              return ""
     1.7 -        
     1.8 +    
     1.9 +    @unittest.skip("skipping")      
    1.10      def testMatchProxy(self):
    1.11          #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
    1.12          #212.17.86.109:8080
    1.13 @@ -100,8 +101,28 @@
    1.14      #def tearOffClass(self):
    1.15      #    gvm_mgr.stop()
    1.16      #    gvm_mgr.cleanup()
    1.17 +    
    1.18 +    
    1.19 +    #VBoxManage list hostonlyifs
    1.20 +    #VBoxManage list dhcpservers
    1.21 +    #VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
    1.22 +    #VBoxManage dhcpserver add --ifname "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable
    1.23 +    #VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --enable
    1.24 +    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --dhcp
    1.25 +    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.1 --netmask 255.255.255.0
    1.26 +
    1.27 +    
    1.28 +    
    1.29 +    def testHostOnlyDHCP(self):
    1.30 +        #list hostonlyifs
    1.31 +        #Cygwin.vboxExecute("list hostonlyifs")
    1.32          
    1.33 -
    1.34 +        hostonlyifs = gvm_mgr.getHostOnlyIFs()
    1.35 +        print hostonlyifs
    1.36 +        
    1.37 +        dhcpservers = gvm_mgr.getDHCPServers()
    1.38 +        print dhcpservers
    1.39 +        
    1.40  if __name__ == '__main__':
    1.41      TestVMManager.setUpClass()
    1.42      
     2.1 --- a/OpenSecurity/bin/vmmanager.pyw	Wed Aug 06 17:05:54 2014 +0100
     2.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Thu Aug 14 09:51:11 2014 +0100
     2.3 @@ -97,7 +97,7 @@
     2.4  def once(theClass):
     2.5      theClass.systemProperties = theClass.getSystemProperties()
     2.6      theClass.machineFolder =    theClass.systemProperties["Default machine folder"]
     2.7 -    theClass.hostonlyIFs =      theClass.getHostOnlyIFs()
     2.8 +    #theClass.hostonlyIF =       theClass.getHostOnlyIFs()["VirtualBox Host-Only Ethernet Adapter"]
     2.9      theClass.blacklistedRSD =   theClass.loadRSDBlacklist()
    2.10      return theClass
    2.11      
    2.12 @@ -108,7 +108,7 @@
    2.13      _instance = None
    2.14      machineFolder = ''
    2.15      rsdHandler = None
    2.16 -    hostonlyIFs = None
    2.17 +    hostonlyIF = None
    2.18      browsingManager = None
    2.19      blacklistedRSD = None
    2.20      status_message = 'Starting up...'
    2.21 @@ -117,6 +117,7 @@
    2.22      def __init__(self):
    2.23          # only proceed if we have a working background environment
    2.24          if self.backend_ok():
    2.25 +            VMManager.hostonlyIF = self.getHostOnlyIFs()["VirtualBox Host-Only Ethernet Adapter"]
    2.26              self.cleanup()
    2.27          else:
    2.28              logger.critical(self.status_message)
    2.29 @@ -131,11 +132,33 @@
    2.30      #list the hostonly IFs exposed by the VBox host
    2.31      @staticmethod    
    2.32      def getHostOnlyIFs():
    2.33 -        result = Cygwin.vboxExecute('list hostonlyifs')[1]
    2.34 -        if result=='':
    2.35 -            return None
    2.36 -        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    2.37 -        return props    
    2.38 +        results = Cygwin.vboxExecute('list hostonlyifs')[1]
    2.39 +        ifs = dict()
    2.40 +        if results=='':
    2.41 +            return ifs
    2.42 +        items = list( "Name:    " + result for result in results.split('Name:    ') if result != '')
    2.43 +        for item in items:
    2.44 +            if item != "":
    2.45 +                props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in item.strip().splitlines()))
    2.46 +                ifs[props["Name"]] = props
    2.47 +        return ifs
    2.48 +                    
    2.49 +        #props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    2.50 +        #return props
    2.51 +    
    2.52 +    #list the hostonly IFs exposed by the VBox host
    2.53 +    @staticmethod    
    2.54 +    def getDHCPServers():
    2.55 +        results = Cygwin.vboxExecute('list dhcpservers')[1]
    2.56 +        if results=='':
    2.57 +            return dict()
    2.58 +        items = list( "NetworkName:    " + result for result in results.split('NetworkName:    ') if result != '')
    2.59 +        dhcps = dict()   
    2.60 +        for item in items:
    2.61 +            if item != "":
    2.62 +                props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in item.strip().splitlines()))
    2.63 +                dhcps[props["NetworkName"]] = props
    2.64 +        return dhcps 
    2.65          
    2.66      # return hosty system properties
    2.67      @staticmethod
    2.68 @@ -149,6 +172,35 @@
    2.69      # return the folder containing the guest VMs     
    2.70      def getMachineFolder(self):
    2.71          return VMManager.machineFolder
    2.72 +    
    2.73 +    # verifies the hostonly interface and DHCP server settings
    2.74 +    def verifyHostOnlySettings(self):
    2.75 +        interfaceName = "VirtualBox Host-Only Ethernet Adapter"
    2.76 +        networkName = "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
    2.77 +        
    2.78 +        hostonlyifs = self.getHostOnlyIFs()
    2.79 +        if not interfaceName in hostonlyifs.keys():
    2.80 +            Cygwin.vboxExecute('hostonlyif create')
    2.81 +            hostonlyifs = self.getHostOnlyIFs()
    2.82 +            if not interfaceName in hostonlyifs.keys():
    2.83 +                return False
    2.84 +        
    2.85 +        interface = hostonlyifs[interfaceName]
    2.86 +        if interface['VBoxNetworkName'] != networkName or interface['DHCP'] != 'Disabled' or interface['IPAddress'] != '192.168.56.1':
    2.87 +            Cygwin.vboxExecute('hostonlyif ipconfig "' + interfaceName + '" --ip 192.168.56.1 --netmask 255.255.255.0')
    2.88 +            
    2.89 +        dhcpservers = self.getDHCPServers()
    2.90 +        if not networkName in dhcpservers.keys():
    2.91 +            Cygwin.vboxExecute('dhcpserver add --ifname "' + interfaceName + '" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable')
    2.92 +            dhcpservers = self.getDHCPServers()
    2.93 +            if not networkName in dhcpservers.keys():
    2.94 +                return False
    2.95 +            
    2.96 +        server = dhcpservers[networkName]
    2.97 +        if server['IP'] != '192.168.56.100' or server['NetworkMask'] != '255.255.255.0' or server['lowerIPAddress'] != '192.168.56.101' or server['upperIPAddress'] != '192.168.56.254' or server['Enabled'] != 'Yes':
    2.98 +            Cygwin.vboxExecute('VBoxManage dhcpserver modify --netname "' + networkName + '" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable')
    2.99 +        
   2.100 +        return True
   2.101  
   2.102      def backend_ok(self):
   2.103  
   2.104 @@ -178,6 +230,8 @@
   2.105          # basically all seems nice and ready to rumble
   2.106          self.status_message = 'All is ok.'
   2.107  
   2.108 +        self.verifyHostOnlySettings()
   2.109 +        
   2.110          return True
   2.111      
   2.112      def stop(self):
   2.113 @@ -370,9 +424,8 @@
   2.114          #remove eventually existing SDVM folder
   2.115          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   2.116          Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   2.117 -        hostonly_if = self.getHostOnlyIFs()
   2.118          Cygwin.checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
   2.119 -        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
   2.120 +        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + self.hostonlyIF['Name'] + '\" --nic2 nat'))
   2.121          Cygwin.checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
   2.122  
   2.123      #create new SecurityDVM with automatically generated name from template (thread safe)        
   2.124 @@ -540,8 +593,7 @@
   2.125      def getHostOnlyIP(self, vm_name):
   2.126          if vm_name == None:
   2.127              logger.info('Getting hostOnly IP address for Host')
   2.128 -            #TODO:// optimise to store on init local variable and return that value (avoid calling list hostonlyifs)
   2.129 -            return VMManager.hostonlyIFs['IPAddress']
   2.130 +            return VMManager.hostonlyIF['IPAddress']
   2.131          else:
   2.132              logger.info('Getting hostOnly IP address ' + vm_name)
   2.133              result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
   2.134 @@ -587,7 +639,14 @@
   2.135      
   2.136      # wait for machine to come up
   2.137      def waitStartup(self, vm_name, timeout_ms = 1000):
   2.138 -        Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   2.139 +        #Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   2.140 +        started = False
   2.141 +        while not started:
   2.142 +            result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' SDVMStarted'))[1]
   2.143 +            if "Value: True" in result:
   2.144 +                started = True
   2.145 +            else:
   2.146 +                time.sleep(3) 
   2.147          return self.getHostOnlyIP(vm_name)
   2.148      
   2.149      # wait for machine to shutdown