Modified update_template to cope with unattached .vmdk
authorBarthaM@N3SIM1218.D03.arc.local
Thu, 17 Jul 2014 10:20:10 +0100
changeset 21259ebaa44c12c
parent 211 cb04add9c85a
child 213 2e0b94e12bfc
Modified update_template to cope with unattached .vmdk
Added start method to vmmanager
Modified vmmanager to not start automatically over getInstance() invocation
Modified cygwin to corectly get the root folder (OpenSecurity//bin)
OpenSecurity/bin/cygwin.py
OpenSecurity/bin/environment.py
OpenSecurity/bin/opensecurity_util.py
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/test_vmmanager.pyw
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/cygwin.py	Tue Jul 01 10:50:48 2014 +0200
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Thu Jul 17 10:20:10 2014 +0100
     1.3 @@ -52,14 +52,14 @@
     1.4  def once(theClass):
     1.5      """get the path to our local cygwin installment"""
     1.6      home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
     1.7 +    e = Environment('OpenSecurity')
     1.8      path_hint = [ 
     1.9 -        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin')), 
    1.10 -        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin64')), 
    1.11 +        os.path.abspath(os.path.join(e.prefix_path, 'cygwin')), 
    1.12 +        os.path.abspath(os.path.join(e.prefix_path, 'cygwin64')), 
    1.13          os.path.abspath(os.path.join(home_drive, 'cygwin')),
    1.14          os.path.abspath(os.path.join(home_drive, 'cygwin64'))
    1.15      ]
    1.16      path_valid = [ p for p in path_hint if os.path.exists(p) ]
    1.17 -        
    1.18      theClass.cygwin_root = path_valid[0]
    1.19      theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
    1.20      theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
    1.21 @@ -150,8 +150,7 @@
    1.22              _startupinfo.wShowWindow = _subprocess.SW_HIDE
    1.23              #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
    1.24          
    1.25 -	    result, res_stdout, res_stderr = None, None, None
    1.26 -		
    1.27 +        result, res_stdout, res_stderr = None, None, None
    1.28          try:
    1.29              # quote the executable otherwise we run into troubles
    1.30              # when the path contains spaces and additonal arguments
    1.31 @@ -163,11 +162,11 @@
    1.32              process = Popen(args, startupinfo = _startupinfo, stdin = stdin, stdout = stdout, stderr = stderr, shell = False)
    1.33              if not wait_return:
    1.34                  return [0, 'working in background', '']
    1.35 -				
    1.36 +            
    1.37              res_stdout, res_stderr = process.communicate()
    1.38              result = process.returncode
    1.39              logger.debug('Finished: ' + program + ' ' + ''.join(arguments))
    1.40 -			#result = process.wait()
    1.41 +            #result = process.wait()
    1.42              #res_stdout = process.stdout.read();
    1.43              #res_stderr = process.stderr.read();
    1.44  
    1.45 @@ -178,10 +177,10 @@
    1.46          return result, res_stdout, res_stderr
    1.47      
    1.48      @staticmethod
    1.49 -    def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
    1.50 +    def vboxExecute(command, wait_return=True, window = False, bash_opts='', try_count = 3):
    1.51          retry = 0
    1.52          result = None
    1.53 -        while retry < 3:
    1.54 +        while retry < try_count:
    1.55              if Cygwin.executeLock.acquire(True):
    1.56                  result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
    1.57                  Cygwin.executeLock.release()
    1.58 @@ -209,8 +208,8 @@
    1.59      def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
    1.60          command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
    1.61          return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
    1.62 -	
    1.63 -	# executes command over ssh on guest vm
    1.64 +
    1.65 +    # executes command over ssh on guest vm
    1.66      @staticmethod
    1.67      def sshBackgroundExecute(command, address, user_name, certificate, wait_return=True, window = False):
    1.68          command = ' -f -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
    1.69 @@ -263,12 +262,12 @@
    1.70  if __name__ == "__main__":
    1.71      logger = setupLogger('Cygwin')
    1.72      c = Cygwin()
    1.73 -    #logger.info(c.root())
    1.74 -    #logger.info(c.bin())
    1.75 -    #logger.info(c.bash())
    1.76 -    #logger.info(c.ssh())
    1.77 -    #logger.info(c.x11())
    1.78 -    #logger.info(c.home())   
    1.79 +    logger.info(c.root())
    1.80 +    logger.info(c.bin())
    1.81 +    logger.info(c.bash())
    1.82 +    logger.info(c.ssh())
    1.83 +    logger.info(c.x11())
    1.84 +    logger.info(c.home())   
    1.85      
    1.86      #PSEXEC -i -s -d CMD
    1.87      #tasklist /v /fo list /fi "IMAGENAME eq explorer.exe"
    1.88 @@ -277,8 +276,6 @@
    1.89      #runner.start()
    1.90      
    1.91      #Cygwin.start_X11()
    1.92 -    
    1.93 -    
    1.94              
    1.95      #time.sleep(500)
    1.96      
     2.1 --- a/OpenSecurity/bin/environment.py	Tue Jul 01 10:50:48 2014 +0200
     2.2 +++ b/OpenSecurity/bin/environment.py	Thu Jul 17 10:20:10 2014 +0100
     2.3 @@ -46,6 +46,7 @@
     2.4      """Hold some nifty environment stuff in a dedicated class."""
     2.5  
     2.6      _log_warning_shown = False
     2.7 +    _prefix_path = ''
     2.8      
     2.9      def __init__(self, application = None):
    2.10          
    2.11 @@ -66,7 +67,13 @@
    2.12          if sys.platform == 'linux2':
    2.13              self._prefix_path = os.path.split(sys.path[0])[0]
    2.14          elif sys.platform == 'win32' or sys.platform == 'cygwin':
    2.15 -            self._prefix_path = os.path.normpath(os.path.join(sys.path[0], '..'))
    2.16 +            for app_path in sys.path:
    2.17 +                if 'OpenSecurity\\bin' in app_path:
    2.18 +                    self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
    2.19 +                    break
    2.20 +        
    2.21 +        if self._prefix_path == '':
    2.22 +            raise OSError()
    2.23              
    2.24          # the data path where all data files are stored
    2.25          if sys.platform == 'linux2':
    2.26 @@ -142,7 +149,7 @@
    2.27  def test():
    2.28  
    2.29      """Test: class Environment"""
    2.30 -    e = Environment('My Application')
    2.31 +    e = Environment('OpenSecurity')
    2.32      print('prefix_path: "{0}"'.format(e.prefix_path))
    2.33      print('  data_path: "{0}"'.format(e.data_path))
    2.34      print('   log_path: "{0}"'.format(e.log_path))
     3.1 --- a/OpenSecurity/bin/opensecurity_util.py	Tue Jul 01 10:50:48 2014 +0200
     3.2 +++ b/OpenSecurity/bin/opensecurity_util.py	Thu Jul 17 10:20:10 2014 +0100
     3.3 @@ -1,12 +1,42 @@
     3.4  #!/bin/env python
     3.5  # -*- coding: utf-8 -*-
     3.6  
     3.7 +# ------------------------------------------------------------
     3.8 +# opensecurityd
     3.9 +#   
    3.10 +# the opensecurityd as RESTful server
    3.11 +#
    3.12 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
    3.13 +#
    3.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    3.15 +# AIT Austrian Institute of Technology GmbH
    3.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    3.17 +# http://www.ait.ac.at
    3.18 +#
    3.19 +# This program is free software; you can redistribute it and/or
    3.20 +# modify it under the terms of the GNU General Public License
    3.21 +# as published by the Free Software Foundation version 2.
    3.22 +# 
    3.23 +# This program is distributed in the hope that it will be useful,
    3.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.26 +# GNU General Public License for more details.
    3.27 +# 
    3.28 +# You should have received a copy of the GNU General Public License
    3.29 +# along with this program; if not, write to the Free Software
    3.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    3.31 +# Boston, MA  02110-1301, USA.
    3.32 +# ------------------------------------------------------------
    3.33 +
    3.34 +
    3.35 +# ------------------------------------------------------------
    3.36 +# imports
    3.37 +
    3.38  import logging
    3.39  import os
    3.40  import urllib
    3.41  import urllib2
    3.42  
    3.43 -
    3.44  # local
    3.45  from environment import Environment
    3.46  
     4.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Tue Jul 01 10:50:48 2014 +0200
     4.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Thu Jul 17 10:20:10 2014 +0100
     4.3 @@ -3,10 +3,11 @@
     4.4  
     4.5  # ------------------------------------------------------------
     4.6  # opensecurityd
     4.7 -# 
     4.8 +#   
     4.9  # the opensecurityd as RESTful server
    4.10  #
    4.11  # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    4.12 +#        Mihai Bartha, <mihai.bartha@ait.ac.at>       
    4.13  #
    4.14  # Copyright (C) 2013 AIT Austrian Institute of Technology
    4.15  # AIT Austrian Institute of Technology GmbH
    4.16 @@ -70,6 +71,7 @@
    4.17      '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
    4.18      '/update_template',                 'os_update_template',   # http://localhost:8080/update_template                         GET
    4.19      '/terminate',                       'os_terminate',         # http://localhost:8080/terminate                               GET
    4.20 +    '/initialize',                      'os_initialize',        # http://localhost:8080/initialize                               GET
    4.21      '/',                                'os_root'               # http://localhost:8080/                                        GET
    4.22  )
    4.23  
    4.24 @@ -377,6 +379,21 @@
    4.25          server.stop()
    4.26          return None
    4.27  
    4.28 +class os_initialize:
    4.29 +    """OpenSecurity '/initialize' handler
    4.30 +    
    4.31 +    - GET: initialize / starts the vmmanager.
    4.32 +
    4.33 +    """
    4.34 +    
    4.35 +    def GET(self):
    4.36 +        log_call(web.ctx.environ)
    4.37 +        global gvm_mgr
    4.38 +        gvm_mgr.cleanup()
    4.39 +        gvm_mgr.start()
    4.40 +        global server
    4.41 +        server.run()
    4.42 +        return None
    4.43  
    4.44  class os_update_template:
    4.45      """OpenSecurity '/update_template' handler
    4.46 @@ -434,7 +451,7 @@
    4.47  
    4.48      # ensure a VMManger is yet loaded
    4.49      gvm_mgr = vmmanager.VMManager.getInstance()
    4.50 -    
    4.51 +    gvm_mgr.start()
    4.52      # tweak sys.argv to control wep.py server start behavior
    4.53      sys.argv = [__file__, "8080"]
    4.54      server = web.application(opensecurity_urls, globals(), autoreload = False)
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/OpenSecurity/bin/test_vmmanager.pyw	Thu Jul 17 10:20:10 2014 +0100
     5.3 @@ -0,0 +1,155 @@
     5.4 +#!/bin/env python
     5.5 +# -*- coding: utf-8 -*-
     5.6 +
     5.7 +# ------------------------------------------------------------
     5.8 +# opensecurityd
     5.9 +#   
    5.10 +# the opensecurityd as RESTful server
    5.11 +#
    5.12 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
    5.13 +#
    5.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    5.15 +# AIT Austrian Institute of Technology GmbH
    5.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    5.17 +# http://www.ait.ac.at
    5.18 +#
    5.19 +# This program is free software; you can redistribute it and/or
    5.20 +# modify it under the terms of the GNU General Public License
    5.21 +# as published by the Free Software Foundation version 2.
    5.22 +# 
    5.23 +# This program is distributed in the hope that it will be useful,
    5.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.26 +# GNU General Public License for more details.
    5.27 +# 
    5.28 +# You should have received a copy of the GNU General Public License
    5.29 +# along with this program; if not, write to the Free Software
    5.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    5.31 +# Boston, MA  02110-1301, USA.
    5.32 +# ------------------------------------------------------------
    5.33 +
    5.34 +
    5.35 +# ------------------------------------------------------------
    5.36 +# imports
    5.37 +
    5.38 +import unittest
    5.39 +
    5.40 +import os
    5.41 +import os.path
    5.42 +import sys
    5.43 +import cygwin
    5.44 +import vmmanager
    5.45 +gvm_mgr = None
    5.46 +
    5.47 +class TestVMManager(unittest.TestCase):
    5.48 +    
    5.49 +    def setUp(self):
    5.50 +        pass
    5.51 +    
    5.52 +    @classmethod
    5.53 +    def setUpClass(self):
    5.54 +        global gvm_mgr
    5.55 +        gvm_mgr = vmmanager.VMManager.getInstance()
    5.56 +        pass
    5.57 +    
    5.58 +    @unittest.skip("skipping")
    5.59 +    def testGetTemplateUUID(self):
    5.60 +        template = vmmanager.VMManager.getTemplateUUID()
    5.61 +        self.assertIsNotNone(template,  "returned no UUID for template")
    5.62 +    
    5.63 +    #@unittest.skip("skipping (requires running vmmanager)")    
    5.64 +    def testUpdateTemplate(self):
    5.65 +        gvm_mgr.updateTemplate()
    5.66 +        pass
    5.67 +    
    5.68 +    #@classmethod
    5.69 +    #def tearOffClass(self):
    5.70 +    #    gvm_mgr.stop()
    5.71 +    #    gvm_mgr.cleanup()
    5.72 +        
    5.73 +
    5.74 +if __name__ == '__main__':
    5.75 +    TestVMManager.setUpClass()
    5.76 +    
    5.77 +    suite = unittest.TestLoader().loadTestsFromTestCase(TestVMManager)
    5.78 +    unittest.TextTestRunner().run(suite)
    5.79 +    
    5.80 +#    logger = setupLogger('Cygwin')
    5.81 +#    c = Cygwin()
    5.82 +#    unittest.main()
    5.83 +    #man = VMManager.getInstance()
    5.84 +    #man.listVM()
    5.85 +    #print man.getConnectedRSDs()
    5.86 +    #print man.getNetworkDrives()
    5.87 +    #man.genNetworkDrive()
    5.88 +    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
    5.89 +    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
    5.90 +    #print list(map(chr, range(68, 91))) 
    5.91 +    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
    5.92 +    #devices = VMManager.getConnectedRSDS()
    5.93 +    #print devices
    5.94 +    
    5.95 +    #drives = VMManager.getLogicalDrives()
    5.96 +    #print drives
    5.97 +    #print VMManager.getDriveType("E")
    5.98 +    #print VMManager.getVolumeInfo("E")
    5.99 +    #print VMManager.getNetworkPath("E")
   5.100 +    
   5.101 +    #vmm.backupFile()
   5.102 +    #for device in devices.values():
   5.103 +    #    #print device
   5.104 +    #    if VMManager.isMassStorageDevice(device):
   5.105 +    #        print device
   5.106 +        
   5.107 +    
   5.108 +    
   5.109 +    #time.sleep(-1)
   5.110 +    #man.listVM()
   5.111 +    #man.listVM()
   5.112 +    #man.listVM()
   5.113 +    #man.listVM()
   5.114 +    #man.genCertificateISO('SecurityDVM0')
   5.115 +    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
   5.116 +    #logger = setupLogger('VMManager')
   5.117 +    #c = Cygwin()
   5.118 +    
   5.119 +    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
   5.120 +    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
   5.121 +    #man.removeVM('SecurityDVM0')
   5.122 +    #man.netUse('192.168.56.134', 'USB\\')
   5.123 +    #ip = '192.168.56.139'
   5.124 +    
   5.125 +    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
   5.126 +    #man.handleDeviceChange()
   5.127 +    #print man.listSDVM()
   5.128 +    #man.configureHostNetworking()
   5.129 +    #new_vm = man.generateSDVMName()
   5.130 +    #man.createVM(new_vm)
   5.131 +    
   5.132 +    #print Cygwin.cmd()
   5.133 +    #man.isAvailable('c:')
   5.134 +    #ip = man.getHostOnlyIP('SecurityDVM0')
   5.135 +    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
   5.136 +    
   5.137 +    #man.genCertificateISO(new_vm)
   5.138 +    #man.attachCertificateISO(new_vm)
   5.139 +    
   5.140 +    #man.attachCertificateISO(vm_name)
   5.141 +    #man.guestExecute(vm_name, "ls")
   5.142 +    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
   5.143 +    #time.sleep(60)
   5.144 +    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
   5.145 +    #man.genCertificateISO('SecurityDVM')
   5.146 +    #man.attachCertificateISO('SecurityDVM')
   5.147 +    #man.isStorageAttached('SecurityDVM')
   5.148 +    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   5.149 +    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
   5.150 +    
   5.151 +    #man.stopVM('SecurityDVM')
   5.152 +    #man.storageDetach('SecurityDVM')
   5.153 +    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
   5.154 +    #man.storageAttach('SecurityDVM')
   5.155 +    
   5.156 +    
   5.157 +    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
   5.158 +    #man.execute(cmd)
   5.159 \ No newline at end of file
     6.1 --- a/OpenSecurity/bin/vmmanager.pyw	Tue Jul 01 10:50:48 2014 +0200
     6.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Thu Jul 17 10:20:10 2014 +0100
     6.3 @@ -1,8 +1,37 @@
     6.4 -'''
     6.5 -Created on Nov 19, 2013
     6.6 +#!/bin/env python
     6.7 +# -*- coding: utf-8 -*-
     6.8  
     6.9 -@author: BarthaM
    6.10 -'''
    6.11 +# ------------------------------------------------------------
    6.12 +# opensecurityd
    6.13 +#   
    6.14 +# the opensecurityd as RESTful server
    6.15 +#
    6.16 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
    6.17 +#
    6.18 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    6.19 +# AIT Austrian Institute of Technology GmbH
    6.20 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    6.21 +# http://www.ait.ac.at
    6.22 +#
    6.23 +# This program is free software; you can redistribute it and/or
    6.24 +# modify it under the terms of the GNU General Public License
    6.25 +# as published by the Free Software Foundation version 2.
    6.26 +# 
    6.27 +# This program is distributed in the hope that it will be useful,
    6.28 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.29 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.30 +# GNU General Public License for more details.
    6.31 +# 
    6.32 +# You should have received a copy of the GNU General Public License
    6.33 +# along with this program; if not, write to the Free Software
    6.34 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    6.35 +# Boston, MA  02110-1301, USA.
    6.36 +# ------------------------------------------------------------
    6.37 +
    6.38 +
    6.39 +# ------------------------------------------------------------
    6.40 +# imports
    6.41 +
    6.42  import os
    6.43  import os.path
    6.44  from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
    6.45 @@ -27,6 +56,7 @@
    6.46  import win32wnet
    6.47  import urllib
    6.48  import urllib2
    6.49 +import unittest
    6.50  DEBUG = True
    6.51  
    6.52  
    6.53 @@ -64,7 +94,14 @@
    6.54      
    6.55      #def __getitem__(self, item):
    6.56      #    return self.coords[item]
    6.57 - 
    6.58 +def once(theClass):
    6.59 +    theClass.systemProperties = theClass.getSystemProperties()
    6.60 +    theClass.machineFolder =    theClass.systemProperties["Default machine folder"]
    6.61 +    theClass.hostonlyIFs =      theClass.getHostOnlyIFs()
    6.62 +    theClass.blacklistedRSD =   theClass.loadRSDBlacklist()
    6.63 +    return theClass
    6.64 +    
    6.65 +@once
    6.66  class VMManager(object):
    6.67      vmRootName = "SecurityDVM"
    6.68      systemProperties = None
    6.69 @@ -78,14 +115,9 @@
    6.70  
    6.71   
    6.72      def __init__(self):
    6.73 -        self.systemProperties = self.getSystemProperties()
    6.74 -        self.machineFolder = self.systemProperties["Default machine folder"]
    6.75 -        self.hostonlyIFs = self.getHostOnlyIFs()
    6.76 -        VMManager.blacklistedRSD = self.loadRSDBlacklist()
    6.77          # only proceed if we have a working background environment
    6.78          if self.backend_ok():
    6.79              self.cleanup()
    6.80 -            self.start()
    6.81          else:
    6.82              logger.critical(self.status_message)
    6.83      
    6.84 @@ -116,24 +148,24 @@
    6.85      
    6.86      # return the folder containing the guest VMs     
    6.87      def getMachineFolder(self):
    6.88 -        return self.machineFolder
    6.89 +        return VMManager.machineFolder
    6.90  
    6.91      def backend_ok(self):
    6.92  
    6.93          """check if the backend (VirtualBox) is sufficient for our task"""
    6.94  
    6.95          # ensure we have our system props
    6.96 -        if self.systemProperties == None:
    6.97 -            self.systemProperties = self.getSystemProperties()
    6.98 -        if self.systemProperties == None:
    6.99 +        if VMManager.systemProperties == None:
   6.100 +            VMManager.systemProperties = self.getSystemProperties()
   6.101 +        if VMManager.systemProperties == None:
   6.102              self.status_message = 'Failed to get backend system properties. Is Backend (VirtualBox?) installed?'
   6.103              return False
   6.104  
   6.105          # check for existing Extension pack
   6.106 -        if not 'Remote desktop ExtPack' in self.systemProperties:
   6.107 +        if not 'Remote desktop ExtPack' in VMManager.systemProperties:
   6.108              self.status_message = 'No remote desktop extension pack found. Please install the "Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads.'
   6.109              return False
   6.110 -        if self.systemProperties['Remote desktop ExtPack'] == 'Oracle VM VirtualBox Extension Pack ':
   6.111 +        if VMManager.systemProperties['Remote desktop ExtPack'] == 'Oracle VM VirtualBox Extension Pack ':
   6.112              self.status_message = 'Unsure if suitable extension pack is installed. Please install the "Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads.'
   6.113              return False
   6.114  
   6.115 @@ -336,11 +368,11 @@
   6.116          if self.isSDVMExisting(vm_name):
   6.117              return
   6.118          #remove eventually existing SDVM folder
   6.119 -        machineFolder = Cygwin.cygPath(self.machineFolder)
   6.120 +        machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   6.121          Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   6.122          hostonly_if = self.getHostOnlyIFs()
   6.123          Cygwin.checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
   6.124 -        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
   6.125 +        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'))
   6.126          Cygwin.checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
   6.127  
   6.128      #create new SecurityDVM with automatically generated name from template (thread safe)        
   6.129 @@ -354,7 +386,7 @@
   6.130      def storageAttach(self, vm_name):
   6.131          if self.isStorageAttached(vm_name):
   6.132              self.storageDetach(vm_name)
   6.133 -        Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
   6.134 +        Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ VMManager.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
   6.135      
   6.136      # return true if storage is attached 
   6.137      def isStorageAttached(self, vm_name):
   6.138 @@ -379,19 +411,22 @@
   6.139          # check for updates
   6.140          self.genCertificateISO(self.vmRootName)
   6.141          self.attachCertificateISO(self.vmRootName)
   6.142 -        imageUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
   6.143 +        
   6.144 +        #templateUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
   6.145 +        templateUUID = self.getTemplateUUID()
   6.146 +        
   6.147          self.storageDetach(self.vmRootName)
   6.148 -        self.removeSnapshots(imageUUID)
   6.149 +        self.removeSnapshots(templateUUID)
   6.150          
   6.151 -        template_storage = self.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
   6.152 +        template_storage = VMManager.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
   6.153          #TODO:// modify to take vm name as argument
   6.154          self.changeStorageType(template_storage,'normal')
   6.155          self.storageAttach(self.vmRootName)
   6.156          self.startVM(self.vmRootName)
   6.157 -        self.waitStartup(self.vmRootName)
   6.158 +        self.waitStartup(self.vmRootName, timeout_ms = 30000)
   6.159          
   6.160          tmp_ip = self.getHostOnlyIP(self.vmRootName)
   6.161 -        tmp_machine_folder = Cygwin.cygPath(self.machineFolder)
   6.162 +        tmp_machine_folder = Cygwin.cygPath(VMManager.machineFolder)
   6.163          Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
   6.164          Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
   6.165          
   6.166 @@ -403,17 +438,19 @@
   6.167              self.startVM(self.vmRootName)
   6.168              self.waitStartup(self.vmRootName)
   6.169          
   6.170 -        self.hibernateVM(self.vmRootName)
   6.171 +        #self.hibernateVM(self.vmRootName)
   6.172 +        self.stopVM(self.vmRootName)
   6.173          self.waitShutdown(self.vmRootName)
   6.174          self.storageDetach(self.vmRootName)
   6.175          self.changeStorageType(template_storage,'immutable')
   6.176          self.storageAttach(self.vmRootName)
   6.177          
   6.178 -        self.start()
   6.179 +        #self.start()
   6.180  
   6.181      #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk"
   6.182 -    #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"    
   6.183 -    def getDiskImages(self):
   6.184 +    #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"
   6.185 +    @staticmethod    
   6.186 +    def getDiskImages():
   6.187          results = Cygwin.checkResult(Cygwin.vboxExecute('list hdds'))[1]
   6.188          results = results.replace('Parent UUID', 'Parent')
   6.189          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   6.190 @@ -428,9 +465,10 @@
   6.191              snaps[props['UUID']] = props
   6.192          return snaps
   6.193      
   6.194 -    def getTemplateUUID(self):
   6.195 -        images = self.getDiskImages()
   6.196 -        template_storage = self.machineFolder + '\\' + self.vmRootName + '/' +self.vmRootName + '.vmdk'
   6.197 +    @staticmethod 
   6.198 +    def getTemplateUUID():
   6.199 +        images = VMManager.getDiskImages()
   6.200 +        template_storage = VMManager.machineFolder + '\\' + VMManager.vmRootName + '\\' + VMManager.vmRootName + '.vmdk'
   6.201          # find template uuid
   6.202          template_uuid = None
   6.203          for hdd in images.values():
   6.204 @@ -462,18 +500,19 @@
   6.205          self.removeVMFolder(vm_name)
   6.206      
   6.207      def removeVMFolder(self, vm_name):
   6.208 -        machineFolder = Cygwin.cygPath(self.machineFolder)
   6.209 +        machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   6.210          Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   6.211      
   6.212      # start VM
   6.213      def startVM(self, vm_name):
   6.214          logger.info('Starting ' +  vm_name)
   6.215 -        result = Cygwin.checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
   6.216 +        #TODO: modify to use Cygwin.checkResult() of make it retry 3 times
   6.217 +        result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )
   6.218          while 'successfully started' not in result[1]:
   6.219              logger.error("Failed to start SDVM: " + vm_name + " retrying")
   6.220              logger.error("Command returned:\n" + result[2])
   6.221              time.sleep(1)
   6.222 -            result = Cygwin.checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
   6.223 +            result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless')
   6.224          return result[0]
   6.225      
   6.226      # return wether VM is running or not
   6.227 @@ -483,12 +522,12 @@
   6.228      # stop VM
   6.229      def stopVM(self, vm_name):
   6.230          logger.info('Sending shutdown signal to ' + vm_name)
   6.231 -        Cygwin.checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
   6.232 +        Cygwin.checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key' ))
   6.233      
   6.234      # stop VM
   6.235      def hibernateVM(self, vm_name):
   6.236          logger.info('Sending hibernate-disk signal to ' + vm_name)
   6.237 -        Cygwin.checkResult(Cygwin.sshBackgroundExecute( '"sudo hibernate-disk"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
   6.238 +        Cygwin.checkResult(Cygwin.sshBackgroundExecute( '"sudo hibernate-disk"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
   6.239              
   6.240      # poweroff VM
   6.241      def poweroffVM(self, vm_name):
   6.242 @@ -502,7 +541,7 @@
   6.243          if vm_name == None:
   6.244              logger.info('Getting hostOnly IP address for Host')
   6.245              #TODO:// optimise to store on init local variable and return that value (avoid calling list hostonlyifs)
   6.246 -            return self.hostonlyIFs['IPAddress']
   6.247 +            return VMManager.hostonlyIFs['IPAddress']
   6.248          else:
   6.249              logger.info('Getting hostOnly IP address ' + vm_name)
   6.250              result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
   6.251 @@ -521,7 +560,7 @@
   6.252      
   6.253      #generates ISO containing authorized_keys for use with guest VM
   6.254      def genCertificateISO(self, vm_name):
   6.255 -        machineFolder = Cygwin.cygPath(self.machineFolder)
   6.256 +        machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   6.257          # remove .ssh folder if exists
   6.258          Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
   6.259          # remove .ssh folder if exists
   6.260 @@ -543,12 +582,12 @@
   6.261      
   6.262      # attaches generated ssh public cert to guest vm
   6.263      def attachCertificateISO(self, vm_name):
   6.264 -        result = Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
   6.265 +        result = Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + VMManager.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
   6.266          return result
   6.267      
   6.268      # wait for machine to come up
   6.269 -    def waitStartup(self, vm_name, timeout_ms = 30000):
   6.270 -        Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
   6.271 +    def waitStartup(self, vm_name, timeout_ms = 1000):
   6.272 +        Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   6.273          return self.getHostOnlyIP(vm_name)
   6.274      
   6.275      # wait for machine to shutdown
   6.276 @@ -681,7 +720,6 @@
   6.277          command = '-r -o StrictHostKeyChecking=no -i "' + certificate + '" "' + src + '" "osecuser@' + self.browsingManager.ip_addr + ':' + dest + '"'
   6.278          return Cygwin.execute(Cygwin.cygwin_scp, command, wait_return=True, window=False)    
   6.279  
   6.280 -
   6.281  #handles browsing session creation 
   6.282  class BrowsingHandler(threading.Thread):
   6.283      vmm = None
   6.284 @@ -748,8 +786,10 @@
   6.285                  self.vmm.storageAttach(self.vm_name)
   6.286                  self.vmm.genCertificateISO(self.vm_name)
   6.287                  self.vmm.attachCertificateISO(self.vm_name)
   6.288 +                
   6.289                  self.vmm.startVM(self.vm_name)
   6.290 -                self.ip_addr = self.vmm.waitStartup(self.vm_name)
   6.291 +                
   6.292 +                self.ip_addr = self.vmm.waitStartup(self.vm_name, timeout_ms=30000)
   6.293                  if self.ip_addr == None:
   6.294                      logger.error("Failed to get ip address")
   6.295                      continue
   6.296 @@ -797,8 +837,12 @@
   6.297                  self.started.set()
   6.298                  logger.info("Browsing SDVM running.")
   6.299                  self.restart.wait()
   6.300 +            except OpenSecurityException, e:
   6.301 +                logger.error(''.join(e))
   6.302              except:
   6.303 +                logger.error("Unexpected error: " + sys.exc_info()[0])
   6.304                  logger.error("BrowsingHandler failed. Cleaning up")
   6.305 +                #self.running= False
   6.306                  
   6.307  class DeviceHandler(threading.Thread): 
   6.308      vmm = None
   6.309 @@ -881,82 +925,3 @@
   6.310                          continue
   6.311                      
   6.312              self.existingRSDs = tmp_rsds
   6.313 -                        
   6.314 -
   6.315 -if __name__ == '__main__':
   6.316 -    #man = VMManager.getInstance()
   6.317 -    #man.listVM()
   6.318 -    #print man.getConnectedRSDs()
   6.319 -    #print man.getNetworkDrives()
   6.320 -    #man.genNetworkDrive()
   6.321 -    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
   6.322 -    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
   6.323 -    #print list(map(chr, range(68, 91))) 
   6.324 -    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
   6.325 -    #devices = VMManager.getConnectedRSDS()
   6.326 -    #print devices
   6.327 -    
   6.328 -    drives = VMManager.getLogicalDrives()
   6.329 -    print drives
   6.330 -    print VMManager.getDriveType("E")
   6.331 -    print VMManager.getVolumeInfo("E")
   6.332 -    print VMManager.getNetworkPath("E")
   6.333 -    
   6.334 -    #vmm.backupFile()
   6.335 -    #for device in devices.values():
   6.336 -    #    #print device
   6.337 -    #    if VMManager.isMassStorageDevice(device):
   6.338 -    #        print device
   6.339 -        
   6.340 -    
   6.341 -    
   6.342 -    #time.sleep(-1)
   6.343 -    #man.listVM()
   6.344 -    #man.listVM()
   6.345 -    #man.listVM()
   6.346 -    #man.listVM()
   6.347 -    #man.genCertificateISO('SecurityDVM0')
   6.348 -    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
   6.349 -    #logger = setupLogger('VMManager')
   6.350 -    #c = Cygwin()
   6.351 -    
   6.352 -    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
   6.353 -    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
   6.354 -    #man.removeVM('SecurityDVM0')
   6.355 -    #man.netUse('192.168.56.134', 'USB\\')
   6.356 -    #ip = '192.168.56.139'
   6.357 -    
   6.358 -    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
   6.359 -    #man.handleDeviceChange()
   6.360 -    #print man.listSDVM()
   6.361 -    #man.configureHostNetworking()
   6.362 -    #new_vm = man.generateSDVMName()
   6.363 -    #man.createVM(new_vm)
   6.364 -    
   6.365 -    #print Cygwin.cmd()
   6.366 -    #man.isAvailable('c:')
   6.367 -    #ip = man.getHostOnlyIP('SecurityDVM0')
   6.368 -    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
   6.369 -    
   6.370 -    #man.genCertificateISO(new_vm)
   6.371 -    #man.attachCertificateISO(new_vm)
   6.372 -    
   6.373 -    #man.attachCertificateISO(vm_name)
   6.374 -    #man.guestExecute(vm_name, "ls")
   6.375 -    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
   6.376 -    #time.sleep(60)
   6.377 -    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
   6.378 -    #man.genCertificateISO('SecurityDVM')
   6.379 -    #man.attachCertificateISO('SecurityDVM')
   6.380 -    #man.isStorageAttached('SecurityDVM')
   6.381 -    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   6.382 -    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
   6.383 -    
   6.384 -    #man.stopVM('SecurityDVM')
   6.385 -    #man.storageDetach('SecurityDVM')
   6.386 -    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
   6.387 -    #man.storageAttach('SecurityDVM')
   6.388 -    
   6.389 -    
   6.390 -    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
   6.391 -    #man.execute(cmd)