fixed/improved on initial import/cleanup/update_template
authorBarthaM@N3SIM1218.D03.arc.local
Fri, 23 May 2014 15:04:52 +0100
changeset 1712701e921bf4c
parent 170 81d5f845d966
child 172 3d2812a07881
fixed/improved on initial import/cleanup/update_template
OpenSecurity/bin/initial_vm.sh
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/initial_vm.sh	Thu May 22 16:01:28 2014 +0100
     1.2 +++ b/OpenSecurity/bin/initial_vm.sh	Fri May 23 15:04:52 2014 +0100
     1.3 @@ -135,23 +135,25 @@
     1.4  
     1.5  # detach disk image
     1.6  #
     1.7 -#echo "detaching disk image ..."
     1.8 -#"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --medium none
     1.9 +echo "detaching disk image ..."
    1.10 +"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --medium none
    1.11  
    1.12  # turn disk image into normal
    1.13  #
    1.14 -#echo "turning disk image into normal ..."
    1.15 -#"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --device ${VDISK_DEVICE} --type hdd --mtype normal --medium "${VDISK_IMAGE}" 
    1.16 +VDISK_PORT=0
    1.17 +VDISK_DEVICE=0
    1.18 +echo "turning disk image into normal ..."
    1.19 +"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --device ${VDISK_DEVICE} --type hdd --mtype normal --medium "${VDISK_IMAGE}" 
    1.20  
    1.21  # detach disk image
    1.22  #
    1.23 -echo "detach disk image ..."
    1.24 -"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --medium none
    1.25 +#echo "detach disk image ..."
    1.26 +#"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --medium none
    1.27  
    1.28  # immutablize disk
    1.29  #
    1.30 -echo "reattach immutable disk image ..."
    1.31 -"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --device ${VDISK_DEVICE} --type hdd --mtype immutable --medium "${VDISK_IMAGE}"
    1.32 +#echo "reattach immutable disk image ..."
    1.33 +#"${VBOX_MANAGER}" storageattach SecurityDVM --storagectl ${VDISK_CONTROLLER} --port ${VDISK_PORT} --device ${VDISK_DEVICE} --type hdd --mtype immutable --medium "${VDISK_IMAGE}"
    1.34  
    1.35  echo "imported initial OsecVM.ova image"
    1.36  
     2.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Thu May 22 16:01:28 2014 +0100
     2.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Fri May 23 15:04:52 2014 +0100
     2.3 @@ -145,10 +145,15 @@
     2.4          
     2.5          if gvm_mgr.vmRootName in gvm_mgr.listVM():
     2.6              gvm_mgr.poweroffVM(gvm_mgr.vmRootName)
     2.7 -            gvm_mgr.storageDetach(gvm_mgr.vmRootName)
     2.8 -            gvm_mgr.removeSnapshots(gvm_mgr.vmRootName)
     2.9 -            #TODO:// unregister vmdk with the vbox
    2.10 -            gvm_mgr.removeVMFolder(gvm_mgr.vmRootName)
    2.11 +            tmplateUUID = gvm_mgr.getTemplateUUID()
    2.12 +            if tmplateUUID != None:
    2.13 +                logger.debug('found parent uuid ' + tmplateUUID)
    2.14 +                gvm_mgr.storageDetach(gvm_mgr.vmRootName)
    2.15 +                gvm_mgr.removeSnapshots(tmplateUUID)
    2.16 +                gvm_mgr.removeImage(tmplateUUID)
    2.17 +            else:
    2.18 +                logger.debug('parent uuid not found')
    2.19 +            gvm_mgr.removeVM(gvm_mgr.vmRootName)
    2.20          
    2.21          trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_initial_import.log')
    2.22          trace_file = open(trace_file_name, 'w+')
     3.1 --- a/OpenSecurity/bin/vmmanager.pyw	Thu May 22 16:01:28 2014 +0100
     3.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri May 23 15:04:52 2014 +0100
     3.3 @@ -148,10 +148,15 @@
     3.4              except urllib2.URLError:
     3.5                  logger.info("Network drive disconnect skipped. OpenSecurity Tray client not started yet.")
     3.6              
     3.7 +        #TODO://continue here
     3.8 +        #self.removeSnapshots('SecurityDVM')
     3.9 +        
    3.10          for vm in self.listSDVM():
    3.11              self.poweroffVM(vm)
    3.12              self.removeVM(vm)
    3.13          
    3.14 +        
    3.15 +        
    3.16      # return hosty system properties
    3.17      def getSystemProperties(self):
    3.18          result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
    3.19 @@ -303,8 +308,52 @@
    3.20      
    3.21      def changeStorageType(self, filename, storage_type):
    3.22          Cygwin.checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
    3.23 -    
    3.24 -    def removeSnapshots(self, vm_name):
    3.25 +                
    3.26 +    # list storage snaphots for VM
    3.27 +    def updateTemplate(self):
    3.28 +        self.stop()
    3.29 +        self.cleanup()
    3.30 +        self.poweroffVM(self.vmRootName)
    3.31 +        self.waitShutdown(self.vmRootName)
    3.32 +        
    3.33 +        # check for updates
    3.34 +        self.genCertificateISO(self.vmRootName)
    3.35 +        self.attachCertificateISO(self.vmRootName)
    3.36 +        imageUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
    3.37 +        self.storageDetach(self.vmRootName)
    3.38 +        self.removeSnapshots(imageUUID)
    3.39 +        
    3.40 +        template_storage = self.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
    3.41 +        #TODO:// modify to take vm name as argument
    3.42 +        self.changeStorageType(template_storage,'normal')
    3.43 +        self.storageAttach(self.vmRootName)
    3.44 +        self.startVM(self.vmRootName)
    3.45 +        self.waitStartup(self.vmRootName)
    3.46 +        
    3.47 +        tmp_ip = VMManager.getHostOnlyIP(self.vmRootName)
    3.48 +        tmp_machine_folder = Cygwin.cygPath(self.machineFolder)
    3.49 +        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
    3.50 +        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
    3.51 +        
    3.52 +        #check if reboot is required
    3.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'))
    3.54 +        if "Yes" in result[1]:
    3.55 +            self.stopVM(self.vmRootName)
    3.56 +            self.waitShutdown(self.vmRootName)
    3.57 +            self.startVM(self.vmRootName)
    3.58 +            self.waitStartup(self.vmRootName)
    3.59 +        
    3.60 +        self.hibernateVM(self.vmRootName)
    3.61 +        self.waitShutdown(self.vmRootName)
    3.62 +        self.storageDetach(self.vmRootName)
    3.63 +        self.changeStorageType(template_storage,'immutable')
    3.64 +        self.storageAttach(self.vmRootName)
    3.65 +        
    3.66 +        self.start()
    3.67 +
    3.68 +    #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk"
    3.69 +    #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"    
    3.70 +    def getDiskImages(self):
    3.71          results = Cygwin.checkResult(Cygwin.vboxExecute('list hdds'))[1]
    3.72          results = results.replace('Parent UUID', 'Parent')
    3.73          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
    3.74 @@ -317,58 +366,36 @@
    3.75                      k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
    3.76                      props[k] = v;
    3.77              snaps[props['UUID']] = props
    3.78 -        
    3.79 -        template_storage = self.machineFolder + '\\' + vm_name + '\\' + vm_name + '.vmdk'
    3.80 -        
    3.81 +        return snaps
    3.82 +    
    3.83 +    def getTemplateUUID(self):
    3.84 +        images = self.getDiskImages()
    3.85 +        template_storage = self.machineFolder + '\\' + self.vmRootName + '/' +self.vmRootName + '.vmdk'
    3.86          # find template uuid
    3.87 -        template_uuid = ''
    3.88 -        for hdd in snaps.values():
    3.89 +        template_uuid = None
    3.90 +        for hdd in images.values():
    3.91              if hdd['Location'] == template_storage:
    3.92                  template_uuid = hdd['UUID']
    3.93 -        logger.debug('found parent uuid ' + template_uuid)
    3.94 +                break
    3.95 +        return template_uuid
    3.96          
    3.97 +    def removeSnapshots(self, imageUUID):
    3.98 +        snaps = self.getDiskImages()
    3.99          # remove snapshots 
   3.100          for hdd in snaps.values():
   3.101 -            if hdd['Parent'] == template_uuid:
   3.102 -                #template_uuid = hdd['UUID']
   3.103 -                logger.debug('removing snapshot ' + hdd['UUID'])
   3.104 -                Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
   3.105 -                # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   3.106 +            if hdd['Parent'] == imageUUID:
   3.107 +                snapshotUUID = hdd['UUID']
   3.108 +                self.removeImage(snapshotUUID)
   3.109                  
   3.110 -    # list storage snaphots for VM
   3.111 -    def updateTemplate(self):
   3.112 -        self.stop()
   3.113 -        self.cleanup()
   3.114 -        self.poweroffVM('SecurityDVM')
   3.115 -        self.waitShutdown('SecurityDVM')
   3.116 -        
   3.117 -        # check for updates
   3.118 -        self.genCertificateISO('SecurityDVM')
   3.119 -        self.attachCertificateISO('SecurityDVM')
   3.120 -        
   3.121 -        self.storageDetach('SecurityDVM')
   3.122 -        self.removeSnapshots('SecurityDVM')
   3.123 -        
   3.124 -        template_storage = self.machineFolder + '\\' + 'SecurityDVM' + '\\' + 'SecurityDVM' + '.vmdk'
   3.125 -        #TODO:// modify to take vm name as argument
   3.126 -        self.changeStorageType(template_storage,'normal')
   3.127 -        self.storageAttach('SecurityDVM')
   3.128 -        self.startVM('SecurityDVM')
   3.129 -        self.waitStartup('SecurityDVM')
   3.130 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
   3.131 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
   3.132 -        #self.stopVM('SecurityDVM')
   3.133 -        self.hibernateVM('SecurityDVM')
   3.134 -        self.waitShutdown('SecurityDVM')
   3.135 -        self.storageDetach('SecurityDVM')
   3.136 -        self.changeStorageType(template_storage,'immutable')
   3.137 -        self.storageAttach('SecurityDVM')
   3.138 -        
   3.139 -        self.start()
   3.140 +    def removeImage(self, imageUUID):
   3.141 +        logger.debug('removing snapshot ' + imageUUID)
   3.142 +        Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + imageUUID + '} --delete'))#[1]
   3.143 +        # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   3.144      
   3.145      #remove VM from the system. should be used on VMs returned by listSDVMs    
   3.146      def removeVM(self, vm_name):
   3.147          logger.info('Removing ' + vm_name)
   3.148 +        
   3.149          Cygwin.checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
   3.150          #TODO:// try to close medium if still existing
   3.151          #Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
   3.152 @@ -518,6 +545,8 @@
   3.153  
   3.154      def getNetworkDrive(self, vm_name):
   3.155          ip = self.getHostOnlyIP(vm_name)
   3.156 +        if ip == None:
   3.157 +            return None
   3.158          result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
   3.159          for line in result[1].splitlines():
   3.160              if line != None and ip in line: