OpenSecurity/bin/vmmanager.pyw
changeset 171 2701e921bf4c
parent 170 81d5f845d966
child 172 3d2812a07881
     1.1 --- a/OpenSecurity/bin/vmmanager.pyw	Thu May 22 16:01:28 2014 +0100
     1.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri May 23 15:04:52 2014 +0100
     1.3 @@ -148,10 +148,15 @@
     1.4              except urllib2.URLError:
     1.5                  logger.info("Network drive disconnect skipped. OpenSecurity Tray client not started yet.")
     1.6              
     1.7 +        #TODO://continue here
     1.8 +        #self.removeSnapshots('SecurityDVM')
     1.9 +        
    1.10          for vm in self.listSDVM():
    1.11              self.poweroffVM(vm)
    1.12              self.removeVM(vm)
    1.13          
    1.14 +        
    1.15 +        
    1.16      # return hosty system properties
    1.17      def getSystemProperties(self):
    1.18          result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
    1.19 @@ -303,8 +308,52 @@
    1.20      
    1.21      def changeStorageType(self, filename, storage_type):
    1.22          Cygwin.checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
    1.23 -    
    1.24 -    def removeSnapshots(self, vm_name):
    1.25 +                
    1.26 +    # list storage snaphots for VM
    1.27 +    def updateTemplate(self):
    1.28 +        self.stop()
    1.29 +        self.cleanup()
    1.30 +        self.poweroffVM(self.vmRootName)
    1.31 +        self.waitShutdown(self.vmRootName)
    1.32 +        
    1.33 +        # check for updates
    1.34 +        self.genCertificateISO(self.vmRootName)
    1.35 +        self.attachCertificateISO(self.vmRootName)
    1.36 +        imageUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
    1.37 +        self.storageDetach(self.vmRootName)
    1.38 +        self.removeSnapshots(imageUUID)
    1.39 +        
    1.40 +        template_storage = self.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
    1.41 +        #TODO:// modify to take vm name as argument
    1.42 +        self.changeStorageType(template_storage,'normal')
    1.43 +        self.storageAttach(self.vmRootName)
    1.44 +        self.startVM(self.vmRootName)
    1.45 +        self.waitStartup(self.vmRootName)
    1.46 +        
    1.47 +        tmp_ip = VMManager.getHostOnlyIP(self.vmRootName)
    1.48 +        tmp_machine_folder = Cygwin.cygPath(self.machineFolder)
    1.49 +        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
    1.50 +        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
    1.51 +        
    1.52 +        #check if reboot is required
    1.53 +        result = Cygwin.checkResult(Cygwin.sshExecute('"if [ -f /var/run/reboot-required ]; then echo \\\"Yes\\\"; fi"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
    1.54 +        if "Yes" in result[1]:
    1.55 +            self.stopVM(self.vmRootName)
    1.56 +            self.waitShutdown(self.vmRootName)
    1.57 +            self.startVM(self.vmRootName)
    1.58 +            self.waitStartup(self.vmRootName)
    1.59 +        
    1.60 +        self.hibernateVM(self.vmRootName)
    1.61 +        self.waitShutdown(self.vmRootName)
    1.62 +        self.storageDetach(self.vmRootName)
    1.63 +        self.changeStorageType(template_storage,'immutable')
    1.64 +        self.storageAttach(self.vmRootName)
    1.65 +        
    1.66 +        self.start()
    1.67 +
    1.68 +    #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk"
    1.69 +    #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"    
    1.70 +    def getDiskImages(self):
    1.71          results = Cygwin.checkResult(Cygwin.vboxExecute('list hdds'))[1]
    1.72          results = results.replace('Parent UUID', 'Parent')
    1.73          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
    1.74 @@ -317,58 +366,36 @@
    1.75                      k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
    1.76                      props[k] = v;
    1.77              snaps[props['UUID']] = props
    1.78 -        
    1.79 -        template_storage = self.machineFolder + '\\' + vm_name + '\\' + vm_name + '.vmdk'
    1.80 -        
    1.81 +        return snaps
    1.82 +    
    1.83 +    def getTemplateUUID(self):
    1.84 +        images = self.getDiskImages()
    1.85 +        template_storage = self.machineFolder + '\\' + self.vmRootName + '/' +self.vmRootName + '.vmdk'
    1.86          # find template uuid
    1.87 -        template_uuid = ''
    1.88 -        for hdd in snaps.values():
    1.89 +        template_uuid = None
    1.90 +        for hdd in images.values():
    1.91              if hdd['Location'] == template_storage:
    1.92                  template_uuid = hdd['UUID']
    1.93 -        logger.debug('found parent uuid ' + template_uuid)
    1.94 +                break
    1.95 +        return template_uuid
    1.96          
    1.97 +    def removeSnapshots(self, imageUUID):
    1.98 +        snaps = self.getDiskImages()
    1.99          # remove snapshots 
   1.100          for hdd in snaps.values():
   1.101 -            if hdd['Parent'] == template_uuid:
   1.102 -                #template_uuid = hdd['UUID']
   1.103 -                logger.debug('removing snapshot ' + hdd['UUID'])
   1.104 -                Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
   1.105 -                # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   1.106 +            if hdd['Parent'] == imageUUID:
   1.107 +                snapshotUUID = hdd['UUID']
   1.108 +                self.removeImage(snapshotUUID)
   1.109                  
   1.110 -    # list storage snaphots for VM
   1.111 -    def updateTemplate(self):
   1.112 -        self.stop()
   1.113 -        self.cleanup()
   1.114 -        self.poweroffVM('SecurityDVM')
   1.115 -        self.waitShutdown('SecurityDVM')
   1.116 -        
   1.117 -        # check for updates
   1.118 -        self.genCertificateISO('SecurityDVM')
   1.119 -        self.attachCertificateISO('SecurityDVM')
   1.120 -        
   1.121 -        self.storageDetach('SecurityDVM')
   1.122 -        self.removeSnapshots('SecurityDVM')
   1.123 -        
   1.124 -        template_storage = self.machineFolder + '\\' + 'SecurityDVM' + '\\' + 'SecurityDVM' + '.vmdk'
   1.125 -        #TODO:// modify to take vm name as argument
   1.126 -        self.changeStorageType(template_storage,'normal')
   1.127 -        self.storageAttach('SecurityDVM')
   1.128 -        self.startVM('SecurityDVM')
   1.129 -        self.waitStartup('SecurityDVM')
   1.130 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
   1.131 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
   1.132 -        #self.stopVM('SecurityDVM')
   1.133 -        self.hibernateVM('SecurityDVM')
   1.134 -        self.waitShutdown('SecurityDVM')
   1.135 -        self.storageDetach('SecurityDVM')
   1.136 -        self.changeStorageType(template_storage,'immutable')
   1.137 -        self.storageAttach('SecurityDVM')
   1.138 -        
   1.139 -        self.start()
   1.140 +    def removeImage(self, imageUUID):
   1.141 +        logger.debug('removing snapshot ' + imageUUID)
   1.142 +        Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + imageUUID + '} --delete'))#[1]
   1.143 +        # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   1.144      
   1.145      #remove VM from the system. should be used on VMs returned by listSDVMs    
   1.146      def removeVM(self, vm_name):
   1.147          logger.info('Removing ' + vm_name)
   1.148 +        
   1.149          Cygwin.checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
   1.150          #TODO:// try to close medium if still existing
   1.151          #Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
   1.152 @@ -518,6 +545,8 @@
   1.153  
   1.154      def getNetworkDrive(self, vm_name):
   1.155          ip = self.getHostOnlyIP(vm_name)
   1.156 +        if ip == None:
   1.157 +            return None
   1.158          result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
   1.159          for line in result[1].splitlines():
   1.160              if line != None and ip in line: