Integrated import script (rewritten in python) into opensecurity/vmmanager.py
authorBarthaM@N3SIM1218.D03.arc.local
Fri, 05 Sep 2014 12:28:30 +0100
changeset 221853af9cfab6a
parent 220 f5805ee62d80
child 222 ac227c9dc244
Integrated import script (rewritten in python) into opensecurity/vmmanager.py
Improoved user feedback upon import and update as well as logging.
Reduced system shutdown times and ui response times
Improoved the decoupling between UI and OSec subsystem.
Various other fixes
OpenSecurity/bin/opensecurity_client_restful_server.py
OpenSecurity/bin/opensecurity_util.py
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/test_vmmanager.pyw
OpenSecurity/bin/ui/configure_dialog.py
OpenSecurity/bin/ui/ui_ConfigureDialog.py
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py	Fri Aug 29 11:44:45 2014 +0100
     1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py	Fri Sep 05 12:28:30 2014 +0100
     1.3 @@ -332,7 +332,7 @@
     1.4              if path in network_path:
     1.5                  return drive
     1.6      return None
     1.7 -	
     1.8 +
     1.9  def mapDrive(drive, networkPath, user, password):
    1.10      if (os.path.exists(networkPath)):
    1.11          logger.debug(networkPath + " is found...")
    1.12 @@ -349,7 +349,7 @@
    1.13          return 1    
    1.14  
    1.15  mount_lock = threading.Lock()
    1.16 -		
    1.17 +
    1.18  # handles netumount request                    
    1.19  class MountNetworkDriveHandler(threading.Thread): 
    1.20      networkPath = None
    1.21 @@ -406,7 +406,7 @@
    1.22      
    1.23      try:
    1.24          win32wnet.WNetCancelConnection2(drive, 1, force)
    1.25 -        logger,info(drive + "successfully unmapped...")
    1.26 +        logger.info(drive + "successfully unmapped...")
    1.27          return 0
    1.28      except:
    1.29          logger.error("Unmap failed, try again...")
     2.1 --- a/OpenSecurity/bin/opensecurity_util.py	Fri Aug 29 11:44:45 2014 +0100
     2.2 +++ b/OpenSecurity/bin/opensecurity_util.py	Fri Sep 05 12:28:30 2014 +0100
     2.3 @@ -85,7 +85,7 @@
     2.4      return logger
     2.5  
     2.6  logger = setupLogger()
     2.7 -
     2.8 +import_logger = setupLogger('OpenSecurity_initial_import')
     2.9  
    2.10  def showTrayMessage(text, timeout):
    2.11      """show a message on the system tray
     3.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Fri Aug 29 11:44:45 2014 +0100
     3.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Fri Sep 05 12:28:30 2014 +0100
     3.3 @@ -144,37 +144,27 @@
     3.4      """
     3.5      
     3.6      def GET(self):
     3.7 +        global gvm_mgr
     3.8          log_call(web.ctx.environ)
     3.9 -        global gvm_mgr
    3.10  
    3.11 -        gvm_mgr.stop()
    3.12 -        gvm_mgr.cleanup()
    3.13 -        
    3.14 -        if gvm_mgr.vmRootName in gvm_mgr.listVM():
    3.15 -            gvm_mgr.poweroffVM(gvm_mgr.vmRootName)
    3.16 -            tmplateUUID = gvm_mgr.getTemplateUUID()
    3.17 -            if tmplateUUID != None:
    3.18 -                logger.debug('found parent uuid ' + tmplateUUID)
    3.19 -                gvm_mgr.detachStorage(gvm_mgr.vmRootName)
    3.20 -                gvm_mgr.removeSnapshots(tmplateUUID)
    3.21 -                gvm_mgr.removeImage(tmplateUUID)
    3.22 -            else:
    3.23 -                logger.debug('parent uuid not found')
    3.24 -            gvm_mgr.removeVM(gvm_mgr.vmRootName)
    3.25 -        gvm_mgr.removeVMFolder(gvm_mgr.vmRootName)
    3.26 -        
    3.27 -        trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_initial_import.log')
    3.28 -        trace_file = open(trace_file_name, 'w+')
    3.29 -
    3.30 -        vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
    3.31 -        
    3.32 -        initial_import_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'initial_vm.sh')))
    3.33 -        Cygwin.bashExecute('\\"' + initial_import_script + '\\" \'' + vm_image + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) 
    3.34 -        res = '{ "init_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
    3.35 -        
    3.36 +        gvm_mgr.startInitialImport()
    3.37 +        res = '{ "init_log": "' + (Environment('OpenSecurity').log_path+'\\OpenSecurity_initial_import.log').replace('\\', '\\\\') + '" }'
    3.38          return res
    3.39  
    3.40 +class os_update_template:
    3.41 +    """OpenSecurity '/update_template' handler
    3.42 +    
    3.43 +    - GET: update template vm
    3.44 +    """
    3.45 +    
    3.46 +    def GET(self):
    3.47 +        global gvm_mgr
    3.48 +        log_call(web.ctx.environ)
    3.49  
    3.50 +        gvm_mgr.startUpdateTemplate()
    3.51 +        res = '{ "init_log": "' + (Environment('OpenSecurity').log_path+'\\OpenSecurity_initial_import.log').replace('\\', '\\\\') + '" }'
    3.52 +        return res
    3.53 +    
    3.54  class os_initial_image:
    3.55      """OpenSecurity '/initial_image' handler
    3.56      
    3.57 @@ -194,8 +184,7 @@
    3.58              res += '"date": ' + str(os.path.getmtime(t)) + ''
    3.59              res += '}}'
    3.60          return res
    3.61 -
    3.62 -
    3.63 +    
    3.64  class os_root:
    3.65      """OpenSecurity '/' handler
    3.66      
    3.67 @@ -397,23 +386,10 @@
    3.68          global gvm_mgr
    3.69          gvm_mgr.cleanup()
    3.70          gvm_mgr.start()
    3.71 -        global server
    3.72 -        server.run()
    3.73 +        #global server
    3.74 +        #server.run()
    3.75          return None
    3.76  
    3.77 -class os_update_template:
    3.78 -    """OpenSecurity '/update_template' handler
    3.79 -    
    3.80 -    - GET: update template vm
    3.81 -    """
    3.82 -    
    3.83 -    def GET(self):
    3.84 -        #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
    3.85 -        global gvm_mgr
    3.86 -        log_call(web.ctx.environ)
    3.87 -        return gvm_mgr.updateTemplate()
    3.88 -
    3.89 -
    3.90  class os_vm:
    3.91      """OpenSecurity '/vms/[VM]' handler
    3.92      
     4.1 --- a/OpenSecurity/bin/test_vmmanager.pyw	Fri Aug 29 11:44:45 2014 +0100
     4.2 +++ b/OpenSecurity/bin/test_vmmanager.pyw	Fri Sep 05 12:28:30 2014 +0100
     4.3 @@ -56,7 +56,7 @@
     4.4      
     4.5      @unittest.skip("skipping")
     4.6      def testGetTemplateUUID(self):
     4.7 -        template = vmmanager.VMManager.getTemplateUUID()
     4.8 +        template = vmmanager.VMManager.getVDiskUUID(gvm_mgr.templateImage)
     4.9          self.assertIsNotNone(template,  "returned no UUID for template")
    4.10      
    4.11      @unittest.skip("skipping")    
    4.12 @@ -103,6 +103,18 @@
    4.13      #    gvm_mgr.cleanup()
    4.14      
    4.15      
    4.16 +    def testImportTemplate(self):
    4.17 +        gvm_mgr.cleanup()
    4.18 +        if 'SecurityDVM' in gvm_mgr.listVMS():
    4.19 +            gvm_mgr.removeVM('SecurityDVM')
    4.20 +        
    4.21 +        uuid = gvm_mgr.getVDiskUUID(gvm_mgr.templateImage)
    4.22 +        if uuid:
    4.23 +            gvm_mgr.removeImage(uuid)
    4.24 +        
    4.25 +        gvm_mgr.removeVMFolder('SecurityDVM')
    4.26 +        gvm_mgr.importTemplate('C:\Windows\System32\config\systemprofile\VirtualBox VMs\OsecVM.ova')
    4.27 +        gvm_mgr.updateTemplate()
    4.28      #VBoxManage list hostonlyifs
    4.29      #VBoxManage list dhcpservers
    4.30      #VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
     5.1 --- a/OpenSecurity/bin/ui/configure_dialog.py	Fri Aug 29 11:44:45 2014 +0100
     5.2 +++ b/OpenSecurity/bin/ui/configure_dialog.py	Fri Sep 05 12:28:30 2014 +0100
     5.3 @@ -113,7 +113,7 @@
     5.4          self.ui.btnDownload.clicked.connect(self.clicked_download)
     5.5          self.ui.btnExplorer.clicked.connect(self.clicked_explorer)
     5.6          self.ui.btnImport.clicked.connect(self.clicked_import)
     5.7 -        self.ui.btnInitialize.clicked.connect(self.clicked_initialize)
     5.8 +        self.ui.btnUpdate.clicked.connect(self.clicked_update)
     5.9          self.ui.btnRefresh.clicked.connect(self.clicked_refresh)
    5.10          self._file_watcher.timeout.connect(self.update_progress)
    5.11  
    5.12 @@ -164,17 +164,15 @@
    5.13          self.ui.edtProgress.clear()
    5.14  
    5.15          try:
    5.16 -
    5.17 -            # get general server info
    5.18              j = json.load(urllib2.urlopen('http://127.0.0.1:8080/init'))
    5.19              self._file_watched_name = str(j['init_log'])
    5.20 -
    5.21          except:
    5.22              pass
    5.23      
    5.24 -    def clicked_initialize(self):
    5.25 +    
    5.26 +    def clicked_update(self):
    5.27  
    5.28 -        """Import button has been clicked."""
    5.29 +        """Update button has been clicked."""
    5.30  
    5.31          # remove old stuff
    5.32          self._file_watched_name = ''
    5.33 @@ -182,9 +180,8 @@
    5.34          self.ui.edtProgress.clear()
    5.35  
    5.36          try:
    5.37 -
    5.38 -            json.load(urllib2.urlopen('http://127.0.0.1:8080/initialize'))
    5.39 -
    5.40 +            j = json.load(urllib2.urlopen('http://127.0.0.1:8080/update_template'))
    5.41 +            self._file_watched_name = str(j['init_log'])
    5.42          except:
    5.43              pass
    5.44  
     6.1 --- a/OpenSecurity/bin/ui/ui_ConfigureDialog.py	Fri Aug 29 11:44:45 2014 +0100
     6.2 +++ b/OpenSecurity/bin/ui/ui_ConfigureDialog.py	Fri Sep 05 12:28:30 2014 +0100
     6.3 @@ -90,11 +90,11 @@
     6.4          self.btnImport = QtGui.QPushButton(self.tabConfiguration)
     6.5          self.btnImport.setMinimumSize(QtCore.QSize(100, 0))
     6.6          self.btnImport.setObjectName(_fromUtf8("btnImport"))
     6.7 -        self.btnInitialize = QtGui.QPushButton(self.tabConfiguration)
     6.8 -        self.btnInitialize.setMinimumSize(QtCore.QSize(100, 0))
     6.9 -        self.btnInitialize.setObjectName(_fromUtf8("btnInitialize"))
    6.10 +        self.btnUpdate = QtGui.QPushButton(self.tabConfiguration)
    6.11 +        self.btnUpdate.setMinimumSize(QtCore.QSize(100, 0))
    6.12 +        self.btnUpdate.setObjectName(_fromUtf8("btnUpdate"))
    6.13          self.lyDownload.addWidget(self.btnImport)
    6.14 -        self.lyDownload.addWidget(self.btnInitialize)
    6.15 +        self.lyDownload.addWidget(self.btnUpdate)
    6.16          self.lyTabConfig.addLayout(self.lyDownload)
    6.17          self.lblProgress = QtGui.QLabel(self.tabConfiguration)
    6.18          self.lblProgress.setObjectName(_fromUtf8("lblProgress"))
    6.19 @@ -198,7 +198,7 @@
    6.20          self.tvTemplate.headerItem().setText(3, _translate("ConfigureDialog", "Path", None))
    6.21          self.btnDownload.setText(_translate("ConfigureDialog", "&Download", None))
    6.22          self.btnImport.setText(_translate("ConfigureDialog", "&Import", None))
    6.23 -        self.btnInitialize.setText(_translate("ConfigureDialog", "&Initialize", None))
    6.24 +        self.btnUpdate.setText(_translate("ConfigureDialog", "&Update", None))
    6.25          self.lblProgress.setText(_translate("ConfigureDialog", "Progress:", None))
    6.26          self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabConfiguration), _translate("ConfigureDialog", "Configuration", None))
    6.27          self.tvMachines.setSortingEnabled(True)
     7.1 --- a/OpenSecurity/bin/vmmanager.pyw	Fri Aug 29 11:44:45 2014 +0100
     7.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri Sep 05 12:28:30 2014 +0100
     7.3 @@ -47,7 +47,7 @@
     7.4  import shutil
     7.5  import stat
     7.6  import tempfile
     7.7 -from opensecurity_util import logger, setupLogger, OpenSecurityException, showTrayMessage
     7.8 +from opensecurity_util import logger, import_logger, setupLogger, OpenSecurityException, showTrayMessage
     7.9  import ctypes
    7.10  import itertools
    7.11  import win32api
    7.12 @@ -99,6 +99,7 @@
    7.13      theClass.machineFolder =    theClass.systemProperties["Default machine folder"]
    7.14      #theClass.hostonlyIF =       theClass.getHostOnlyIFs()["VirtualBox Host-Only Ethernet Adapter"]
    7.15      theClass.blacklistedRSD =   theClass.loadRSDBlacklist()
    7.16 +    theClass.templateImage =    theClass.machineFolder + '\\' + theClass.vmRootName + '\\' + theClass.vmRootName + '.vmdk'
    7.17      return theClass
    7.18      
    7.19  @once
    7.20 @@ -112,6 +113,9 @@
    7.21      browsingManager = None
    7.22      blacklistedRSD = None
    7.23      status_message = 'Starting up...'
    7.24 +    templateImage = None
    7.25 +    importHandler = None
    7.26 +    updateHandler = None
    7.27   
    7.28      def __init__(self):
    7.29          # only proceed if we have a working background environment
    7.30 @@ -203,7 +207,7 @@
    7.31  
    7.32      def template_installed(self):
    7.33          """ check if we do have our root VMs installed """
    7.34 -        vms = self.listVM()
    7.35 +        vms = self.listVMS()
    7.36          if not self.vmRootName in vms:
    7.37              self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.'
    7.38              return False
    7.39 @@ -248,7 +252,16 @@
    7.40              self.browsingManager = None
    7.41          Cygwin.allowExec()
    7.42      
    7.43 -    def start(self):
    7.44 +    def start(self, force = False):
    7.45 +        if not force:
    7.46 +            if self.importHandler and self.importHandler.isAlive():
    7.47 +                logger.info("Initial update running canceling start.")
    7.48 +                return
    7.49 +        
    7.50 +            if self.updateHandler and self.updateHandler.isAlive():
    7.51 +                logger.info("Update running canceling start.")
    7.52 +                return
    7.53 +        
    7.54          self.stop()
    7.55          Cygwin.allowExec()
    7.56          if self.backend_ok() and self.template_installed():
    7.57 @@ -272,7 +285,7 @@
    7.58              self.removeVM(vm)
    7.59  
    7.60      # list all existing VMs registered with VBox
    7.61 -    def listVM(self):
    7.62 +    def listVMS(self):
    7.63          result = Cygwin.vboxExecute('list vms')[1]
    7.64          vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
    7.65          return vms
    7.66 @@ -285,7 +298,7 @@
    7.67      
    7.68      # list existing SDVMs
    7.69      def listSDVM(self):
    7.70 -        vms = self.listVM()
    7.71 +        vms = self.listVMS()
    7.72          svdms = []
    7.73          for vm in vms:
    7.74              if vm.startswith(self.vmRootName) and vm != self.vmRootName:
    7.75 @@ -294,7 +307,7 @@
    7.76      
    7.77      # generate valid (not already existing SDVM name). necessary for creating a new VM
    7.78      def genSDVMName(self):
    7.79 -        vms = self.listVM()
    7.80 +        vms = self.listVMS()
    7.81          for i in range(0,999):
    7.82              if(not self.vmRootName+str(i) in vms):
    7.83                  return self.vmRootName+str(i)
    7.84 @@ -378,15 +391,6 @@
    7.85          return rsds
    7.86      
    7.87     
    7.88 -    #def getAttachedRSD(self, vm_name):
    7.89 -    #    props = self.getVMInfo(vm_name)
    7.90 -    #    keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1', 'USBFilterSerialNumber1'])
    7.91 -    #    keyset = set(props.keys())
    7.92 -    #    usb_filter = None
    7.93 -    #    if keyset.issuperset(keys):
    7.94 -    #        usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
    7.95 -    #    return usb_filter
    7.96 -    
    7.97      # return the attached USB device as usb descriptor for an existing VM 
    7.98      def getAttachedRSD(self, vm_name):
    7.99          props = self.getVMInfo(vm_name)
   7.100 @@ -452,73 +456,48 @@
   7.101              self.createVM(vm_name)
   7.102          return vm_name
   7.103      
   7.104 +    #VMManager.machineFolder + '\SecurityDVM\SecurityDVM.vmdk
   7.105      # attach storage image to controller
   7.106 -    def attachStorage(self, vm_name):
   7.107 -        if self.isStorageAttached(vm_name):
   7.108 -            self.detachStorage(vm_name)
   7.109 -        Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ VMManager.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')
   7.110 +    def attachVDisk(self, vm_name, vdisk_controller, vdisk_port, vdisk_device, vdisk_image):
   7.111 +        if self.isVDiskAttached(vm_name, vdisk_controller, vdisk_port, vdisk_device):
   7.112 +            self.detachVDisk(vm_name, vdisk_controller, vdisk_port, vdisk_device)
   7.113 +        Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl '+ vdisk_controller + ' --port ' + vdisk_port + ' --device ' + vdisk_device + ' --type hdd --medium "'+ vdisk_image + '"')
   7.114      
   7.115      # return true if storage is attached 
   7.116 -    def isStorageAttached(self, vm_name):
   7.117 +    def isVDiskAttached(self, vm_name, vdisk_controller, vdisk_port, vdisk_device):
   7.118          info = self.getVMInfo(vm_name)
   7.119 -        return (info['SATA-0-0']!='none')
   7.120 +        return (info[vdisk_controller+'-'+vdisk_port+'-'+vdisk_device] != 'none')
   7.121      
   7.122      # detach storage from controller
   7.123 -    def detachStorage(self, vm_name):
   7.124 -        if self.isStorageAttached(vm_name):
   7.125 -            Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')
   7.126 +    def detachVDisk(self, vm_name, vdisk_controller, vdisk_port, vdisk_device):
   7.127 +        if self.isVDiskAttached(vm_name, vdisk_controller, vdisk_port, vdisk_device):
   7.128 +            Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl ' + vdisk_controller + ' --port ' + vdisk_port + ' --device ' + vdisk_device + ' --medium none')
   7.129      
   7.130 -    def changeStorageType(self, filename, storage_type):
   7.131 -        Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)
   7.132 -                
   7.133 -    # list storage snaphots for VM
   7.134 -    def updateTemplate(self):
   7.135 -        self.stop()
   7.136 -        self.cleanup()
   7.137 -        self.poweroffVM(self.vmRootName)
   7.138 -        self.waitShutdown(self.vmRootName)
   7.139 +    # modify type of the vdisk_image
   7.140 +    def changeVDiskType(self, vdisk_image, storage_type):
   7.141 +        Cygwin.vboxExecute('modifyhd "' + vdisk_image + '" --type ' + storage_type)
   7.142          
   7.143 -        # check for updates
   7.144 -        self.genCertificate(self.vmRootName)
   7.145 -        self.attachCertificate(self.vmRootName)
   7.146 +    # grab VM storage controller, port and device for vdisk image name
   7.147 +    def getVDiskController(self, vm_name, image_name = '.vmdk'):
   7.148 +        vm_description = self.getVMInfo(vm_name)
   7.149 +        vdisk_controller = None
   7.150 +        for key, value in vm_description.iteritems():
   7.151 +            if image_name in value:
   7.152 +                vdisk_controller = key
   7.153 +                break
   7.154 +        return vdisk_controller
   7.155 +    
   7.156 +    # return attached vmdk image name containing image_name 
   7.157 +    def getVDiskImage(self, vm_name, image_name = '.vmdk'):
   7.158 +        vmInfo = self.getVMInfo(vm_name)
   7.159 +        vdisk_image = None
   7.160 +        for value in vmInfo.values():
   7.161 +            if image_name in value:
   7.162 +                break
   7.163 +        return vdisk_image 
   7.164          
   7.165 -        #templateUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
   7.166 -        templateUUID = self.getTemplateUUID()
   7.167 -        
   7.168 -        self.detachStorage(self.vmRootName)
   7.169 -        self.removeSnapshots(templateUUID)
   7.170 -        
   7.171 -        template_storage = VMManager.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
   7.172 -        #TODO:// modify to take vm name as argument
   7.173 -        self.changeStorageType(template_storage,'normal')
   7.174 -        self.attachStorage(self.vmRootName)
   7.175 -        self.startVM(self.vmRootName)
   7.176 -        self.waitStartup(self.vmRootName)
   7.177 -        
   7.178 -        tmp_ip = self.getHostOnlyIP(self.vmRootName)
   7.179 -        tmp_machine_folder = Cygwin.cygPath(VMManager.machineFolder)
   7.180 -        Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.181 -        Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.182 -        
   7.183 -        #check if reboot is required
   7.184 -        result = Cygwin.sshExecute('"if [ -f /var/run/reboot-required ]; then echo \\\"Yes\\\"; fi"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.185 -        if "Yes" in result[1]:
   7.186 -            self.stopVM(self.vmRootName)
   7.187 -            self.waitShutdown(self.vmRootName)
   7.188 -            self.startVM(self.vmRootName)
   7.189 -            self.waitStartup(self.vmRootName)
   7.190 -        
   7.191 -        #self.hibernateVM(self.vmRootName)
   7.192 -        self.stopVM(self.vmRootName)
   7.193 -        self.waitShutdown(self.vmRootName)
   7.194 -        self.detachStorage(self.vmRootName)
   7.195 -        self.changeStorageType(template_storage,'immutable')
   7.196 -        self.attachStorage(self.vmRootName)
   7.197 -        
   7.198 -    #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk"
   7.199 -    #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"
   7.200      @staticmethod    
   7.201 -    def getDiskImages():
   7.202 +    def getVDiskImages():
   7.203          results = Cygwin.vboxExecute('list hdds')[1]
   7.204          results = results.replace('Parent UUID', 'Parent')
   7.205          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   7.206 @@ -534,19 +513,18 @@
   7.207          return snaps
   7.208      
   7.209      @staticmethod 
   7.210 -    def getTemplateUUID():
   7.211 -        images = VMManager.getDiskImages()
   7.212 -        template_storage = VMManager.machineFolder + '\\' + VMManager.vmRootName + '\\' + VMManager.vmRootName + '.vmdk'
   7.213 +    def getVDiskUUID(vdisk_image):
   7.214 +        images = VMManager.getVDiskImages()
   7.215          # find template uuid
   7.216          template_uuid = None
   7.217          for hdd in images.values():
   7.218 -            if hdd['Location'] == template_storage:
   7.219 +            if hdd['Location'] == vdisk_image:
   7.220                  template_uuid = hdd['UUID']
   7.221                  break
   7.222          return template_uuid
   7.223 -        
   7.224 +    
   7.225      def removeSnapshots(self, imageUUID):
   7.226 -        snaps = self.getDiskImages()
   7.227 +        snaps = self.getVDiskImages()
   7.228          # remove snapshots 
   7.229          for hdd in snaps.values():
   7.230              if hdd['Parent'] == imageUUID:
   7.231 @@ -561,15 +539,14 @@
   7.232      #remove VM from the system. should be used on VMs returned by listSDVMs    
   7.233      def removeVM(self, vm_name):
   7.234          logger.info('Removing ' + vm_name)
   7.235 -        
   7.236          Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')
   7.237 -        #TODO:// try to close medium if still existing
   7.238 +        #try to close medium if still existing
   7.239          #Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete')
   7.240          self.removeVMFolder(vm_name)
   7.241      
   7.242      def removeVMFolder(self, vm_name):
   7.243          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   7.244 -        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"')
   7.245 +        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '\\' + vm_name + '\\\"')
   7.246      
   7.247      # start VM
   7.248      def startVM(self, vm_name):
   7.249 @@ -790,10 +767,9 @@
   7.250          
   7.251      def getAppDataDir(self, sid):    
   7.252          key = win32api.RegOpenKey(win32con.HKEY_USERS, sid + '\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
   7.253 -        value, type = win32api.RegQueryValueEx(key, "AppData")
   7.254 +        value, _ = win32api.RegQueryValueEx(key, "AppData")
   7.255          win32api.RegCloseKey(key)
   7.256          return value
   7.257 -        
   7.258          #key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' + '\\' + sid)
   7.259          #value, type = win32api.RegQueryValueEx(key, "ProfileImagePath")
   7.260          #print value
   7.261 @@ -807,8 +783,177 @@
   7.262          certificate = Cygwin.cygPath(self.getMachineFolder()) + '/' + self.browsingManager.vm_name + '/dvm_key'
   7.263          #command = '-r -v -o StrictHostKeyChecking=no -i \"' + certificate + '\" \"' + src + '\" \"osecuser@' + self.browsingManager.ip_addr + ':' + dest + '\"'
   7.264          command = '-r -o StrictHostKeyChecking=no -i "' + certificate + '" "' + src + '" "osecuser@' + self.browsingManager.ip_addr + ':' + dest + '"'
   7.265 -        return Cygwin.execute(Cygwin.cygwin_scp, command, wait_return=True, window=False)    
   7.266 +        return Cygwin.execute(Cygwin.cygwin_scp, command, wait_return=True, window=False)
   7.267 +    
   7.268 +    #import initial template
   7.269 +    def importTemplate(self, image_path):
   7.270 +        import_logger.info('Stopping Opensecurity...')
   7.271 +        self.stop()
   7.272 +        
   7.273 +        import_logger.info('Cleaning up system in preparation for import...')
   7.274 +        self.cleanup()
   7.275 +        
   7.276 +        import_logger.info('Removing template SDVM...')
   7.277 +        # if template exists
   7.278 +        if self.vmRootName in self.listVMS():
   7.279 +            # shutdown template if running
   7.280 +            self.poweroffVM(self.vmRootName)
   7.281 +            # detach and remove VDisk
   7.282 +            tmplateUUID = self.getVDiskUUID(self.templateImage)
   7.283 +            if tmplateUUID != None:
   7.284 +                logger.debug('Found template VDisk uuid ' + tmplateUUID)
   7.285 +                controller = self.getVDiskController(self.vmRootName)
   7.286 +                if controller:
   7.287 +                    controller = controller.split('-')
   7.288 +                    self.detachVDisk(self.vmRootName, controller[0], controller[1], controller[2])
   7.289 +                self.removeSnapshots(tmplateUUID)
   7.290 +                self.removeImage(tmplateUUID)
   7.291 +            else:
   7.292 +                logger.info('Template uuid not found')
   7.293 +            # remove VM    
   7.294 +            self.removeVM(self.vmRootName)
   7.295 +        # remove template VM folder 
   7.296 +        self.removeVMFolder(self.vmRootName)
   7.297 +        import_logger.info('Cleanup finished...')
   7.298 +        
   7.299 +        import_logger.info('Checking privileges...')
   7.300 +        result = Cygwin.bashExecute('id -G')
   7.301 +        if '544' not in result[1]:
   7.302 +            import_logger.debug('Insufficient privileges.')
   7.303 +            import_logger.debug("Trying to continue...")
   7.304 +        
   7.305 +        # check OpenSecurity Initial VM Image
   7.306 +        import_logger.debug('Looking for VM image: ' + image_path)
   7.307 +        result = os.path.isfile(image_path)
   7.308 +      
   7.309 +        if not result:
   7.310 +            import_logger.debug('Warning: no OpenSecurity Initial Image found.')
   7.311 +            import_logger.debug('Please download using the OpenSecurity download tool.')
   7.312 +            raise OpenSecurityException('OpenSecurity Initial Image not found.')
   7.313 +        logger.debug('Initial VM image: ' + image_path + ' found')
   7.314 +        
   7.315 +        if not self.template_installed():
   7.316 +            import_logger.info('Importing SDVm template: ' + image_path)
   7.317 +            Cygwin.vboxExecute('import "' + image_path + '" --vsys 0 --vmname ' + VMManager.vmRootName + ' --unit 12 --disk "' + self.templateImage + '"')
   7.318 +        else:
   7.319 +            import_logger.info('Found ' + VMManager.vmRootName + ' already present in VBox reusing it.')
   7.320 +            import_logger.info('if you want a complete new import please remove the VM first.')
   7.321 +            import_logger.info('starting OpenSecurity service...')
   7.322 +            return
   7.323  
   7.324 +        # remove unnecessary IDE controller
   7.325 +        Cygwin.vboxExecute('storagectl ' + VMManager.vmRootName + ' --name IDE --remove')
   7.326 +
   7.327 +        info = self.getVDiskController(VMManager.vmRootName, self.templateImage)
   7.328 +        if info:
   7.329 +            info = info.split('-')
   7.330 +            self.detachVDisk(VMManager.vmRootName, info[0], info[1], info[2])
   7.331 +        
   7.332 +        self.changeVDiskType(self.templateImage, 'immutable')
   7.333 +        self.attachVDisk(VMManager.vmRootName, info[0], info[1], info[2], self.templateImage)
   7.334 +        import_logger.info('Initial import finished.')    
   7.335 +        
   7.336 +    # update template 
   7.337 +    def updateTemplate(self):
   7.338 +        import_logger.debug('Stopping Opensecurity...')
   7.339 +        self.stop()
   7.340 +        
   7.341 +        import_logger.debug('Cleaning up system in preparation for update...')
   7.342 +        self.cleanup()
   7.343 +        
   7.344 +        import_logger.info('Cleanup finished...')
   7.345 +        
   7.346 +        # shutdown template if running
   7.347 +        self.poweroffVM(self.vmRootName)
   7.348 +        
   7.349 +        import_logger.info('Starting template VM...')
   7.350 +        # check for updates
   7.351 +        self.genCertificate(self.vmRootName)
   7.352 +        self.attachCertificate(self.vmRootName)
   7.353 +
   7.354 +        import_logger.info('Removing snapshots...')        
   7.355 +        
   7.356 +        self.detachVDisk(self.vmRootName, 'SATA', '0', '0')
   7.357 +        templateUUID = self.getVDiskUUID(self.templateImage)
   7.358 +        self.removeSnapshots(templateUUID)
   7.359 +        
   7.360 +        import_logger.info('Setting VDisk image to normal...')
   7.361 +        self.changeVDiskType(self.templateImage, 'normal')
   7.362 +        self.attachVDisk(self.vmRootName, 'SATA', '0', '0', self.templateImage)
   7.363 +        
   7.364 +        import_logger.info('Starting VM...')
   7.365 +        self.startVM(self.vmRootName)
   7.366 +        self.waitStartup(self.vmRootName)
   7.367 +        
   7.368 +        import_logger.info('Updating components...')
   7.369 +        tmp_ip = self.getHostOnlyIP(self.vmRootName)
   7.370 +        tmp_machine_folder = Cygwin.cygPath(VMManager.machineFolder)
   7.371 +        Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.372 +        Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.373 +        
   7.374 +        import_logger.info('Restarting template VM...')
   7.375 +        #check if reboot is required
   7.376 +        result = Cygwin.sshExecute('"if [ -f /var/run/reboot-required ]; then echo \\\"Yes\\\"; fi"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   7.377 +        if "Yes" in result[1]:
   7.378 +            self.stopVM(self.vmRootName)
   7.379 +            self.waitShutdown(self.vmRootName)
   7.380 +            self.startVM(self.vmRootName)
   7.381 +            self.waitStartup(self.vmRootName)
   7.382 +        
   7.383 +        import_logger.info('Stopping template VM...')
   7.384 +        self.stopVM(self.vmRootName)
   7.385 +        self.waitShutdown(self.vmRootName)
   7.386 +        
   7.387 +        import_logger.info('Setting VDisk image to immutable...')
   7.388 +        self.detachVDisk(self.vmRootName, 'SATA', '0', '0') 
   7.389 +        self.changeVDiskType(self.templateImage, 'immutable')
   7.390 +        self.attachVDisk(self.vmRootName,  'SATA', '0', '0', self.templateImage)
   7.391 +        
   7.392 +        import_logger.info('Update template finished...')
   7.393 +    
   7.394 +    def startInitialImport(self):
   7.395 +        if self.importHandler and self.importHandler.isAlive():
   7.396 +            import_logger.info("Initial import already running.")
   7.397 +            return
   7.398 +        self.importHandler = InitialImportHandler(self)
   7.399 +        self.importHandler.start()
   7.400 +        import_logger.info("Initial import started.")
   7.401 +        
   7.402 +    def startUpdateTemplate(self):
   7.403 +        if self.updateHandler and self.updateHandler.isAlive():
   7.404 +            import_logger.info("Initial import already running.")
   7.405 +            return
   7.406 +        self.updateHandler = UpdateHandler(self)
   7.407 +        self.updateHandler.start()
   7.408 +        import_logger.info("Initial import started.")
   7.409 +
   7.410 +class UpdateHandler(threading.Thread):
   7.411 +    vmm = None    
   7.412 +    def __init__(self, vmmanager):
   7.413 +        threading.Thread.__init__(self)
   7.414 +        self.vmm = vmmanager
   7.415 +    
   7.416 +    def run(self):
   7.417 +        try:
   7.418 +            self.vmm.updateTemplate()
   7.419 +        except:
   7.420 +            import_logger.info("Update template failed. Refer to service log for details.")
   7.421 +        self.vmm.start(force=True)
   7.422 +    
   7.423 +class InitialImportHandler(threading.Thread):
   7.424 +    vmm = None    
   7.425 +    def __init__(self, vmmanager):
   7.426 +        threading.Thread.__init__(self)
   7.427 +        self.vmm = vmmanager
   7.428 +    
   7.429 +    def run(self):
   7.430 +        try:
   7.431 +            self.vmm.importTemplate(self.vmm.getMachineFolder() + '\\OsecVM.ova')
   7.432 +            self.vmm.updateTemplate()
   7.433 +        except:
   7.434 +            import_logger.info("Initial import failed. Refer to service log for details.")
   7.435 +        self.vmm.start(force=True)
   7.436 +            
   7.437  #handles browsing session creation 
   7.438  class BrowsingHandler(threading.Thread):
   7.439      vmm = None
   7.440 @@ -879,7 +1024,7 @@
   7.441              
   7.442              try:
   7.443                  self.vm_name = self.vmm.newSDVM()
   7.444 -                self.vmm.attachStorage(self.vm_name)
   7.445 +                self.vmm.attachVDisk(self.vm_name, 'SATA', '0', '0', self.vmm.templateImage)
   7.446                  self.vmm.genCertificate(self.vm_name)
   7.447                  self.vmm.attachCertificate(self.vm_name)
   7.448                  
   7.449 @@ -990,7 +1135,7 @@
   7.450                  showTrayMessage('Mounting device...', 7000)
   7.451                  if (self.attachedRSDs and False) or (new_device not in self.attachedRSDs.values()):
   7.452                      new_sdvm = self.vmm.newSDVM()
   7.453 -                    self.vmm.attachStorage(new_sdvm)
   7.454 +                    self.vmm.attachVDisk(new_sdvm, 'SATA', '0', '0', self.vmm.templateImage)
   7.455                      self.vmm.startVM(new_sdvm)
   7.456                      new_ip = self.vmm.waitStartup(new_sdvm)
   7.457                      if new_ip == None: