iupdate template function and consolidation
authormb
Wed, 29 Jan 2014 14:18:17 +0100
changeset 5459f1d824a070
parent 48 495ebaa3af5b
child 55 42238cd74afe
iupdate template function and consolidation
OpenSecurity/bin/opensecurityd.py
OpenSecurity/bin/vmmanager.py
     1.1 --- a/OpenSecurity/bin/opensecurityd.py	Thu Jan 09 13:42:32 2014 +0100
     1.2 +++ b/OpenSecurity/bin/opensecurityd.py	Wed Jan 29 14:18:17 2014 +0100
     1.3 @@ -63,11 +63,11 @@
     1.4      '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
     1.5      '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
     1.6      '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
     1.7 -    '/',                                'os_root'               # http://localhost:8080/                                        GET
     1.8 +    '/',                                'os_root',              # http://localhost:8080/                                        GET
     1.9 +    '/update_template',                 'os_update_template'    # http://localhost:8080/update_template                         GET
    1.10  )
    1.11  
    1.12 -
    1.13 -# ------------------------------------------------------------
    1.14 + # ------------------------------------------------------------
    1.15  # vars
    1.16  
    1.17  # Global VMManager instance
    1.18 @@ -221,6 +221,15 @@
    1.19          res += "}"
    1.20          return res
    1.21  
    1.22 +class os_update_template:
    1.23 +    """OpenSecurity '/update_template' handler
    1.24 +    
    1.25 +    - GET: update template vm
    1.26 +    """
    1.27 +    
    1.28 +    def GET(self):
    1.29 +        #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
    1.30 +        return gvm_mgr.updateTemplate()
    1.31  
    1.32  # start
    1.33  if __name__ == "__main__":
     2.1 --- a/OpenSecurity/bin/vmmanager.py	Thu Jan 09 13:42:32 2014 +0100
     2.2 +++ b/OpenSecurity/bin/vmmanager.py	Wed Jan 29 14:18:17 2014 +0100
     2.3 @@ -80,26 +80,6 @@
     2.4      
     2.5      def isSDVMStarted(self, ip):
     2.6          return self.startNotifications.contains(ip)
     2.7 -             
     2.8 -    def execute(self, cmd, wait_return=True ):
     2.9 -        if DEBUG:
    2.10 -            print('trying to launch: ' + cmd)
    2.11 -        process = Popen(cmd, stdout=PIPE, stderr=PIPE) #shell = True
    2.12 -        if DEBUG:
    2.13 -            print('launched: ' + cmd)
    2.14 -        if not wait_return:
    2.15 -            return [0, 'working in background', '']
    2.16 -        result = process.wait()
    2.17 -        res_stdout = process.stdout.read();
    2.18 -        res_stderr = process.stderr.read();
    2.19 -        if DEBUG:
    2.20 -            if res_stdout != "":
    2.21 -                print res_stdout
    2.22 -            if res_stderr != "":
    2.23 -                print res_stderr
    2.24 -        if result !=0:
    2.25 -            raise VMManagerException(res_stderr)
    2.26 -        return result, res_stdout, res_stderr
    2.27      
    2.28      def getVBoxManagePath(self):
    2.29          """get the path to the VirtualBox installation on this system"""
    2.30 @@ -115,7 +95,7 @@
    2.31      # return hosty system properties
    2.32      def getSystemProperties(self):
    2.33          cmd = self.vboxManage + ' list systemproperties'
    2.34 -        result = self.execute(cmd)
    2.35 +        result = self.hostExecute(cmd)
    2.36          if result[1]=='':
    2.37              return None
    2.38          props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    2.39 @@ -123,12 +103,13 @@
    2.40      
    2.41      # return the folder containing the guest VMs     
    2.42      def getDefaultMachineFolder(self):
    2.43 -        return self.systemProperties["Default machine folder"]
    2.44 +        machineFolder = self.systemProperties["Default machine folder"]
    2.45 +        return machineFolder
    2.46      
    2.47      #list the hostonly IFs exposed by the VBox host
    2.48      def getHostOnlyIFs(self):
    2.49          cmd = 'VBoxManage list hostonlyifs'
    2.50 -        result = self.execute(cmd)[1]
    2.51 +        result = self.hostExecute(cmd)[1]
    2.52          if result=='':
    2.53              return None
    2.54          props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    2.55 @@ -136,7 +117,7 @@
    2.56          
    2.57      def listRSDS(self):
    2.58          cmd = 'VBoxManage list usbhost'
    2.59 -        results = self.execute(cmd)[1]
    2.60 +        results = self.hostExecute(cmd)[1]
    2.61          results = results.split('Host USB Devices:')[1].strip()
    2.62          
    2.63          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
    2.64 @@ -160,7 +141,14 @@
    2.65      # list all existing VMs registered with VBox
    2.66      def listVM(self):
    2.67          cmd = 'VBoxManage list vms'
    2.68 -        result = self.execute(cmd)[1]
    2.69 +        result = self.hostExecute(cmd)[1]
    2.70 +        vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
    2.71 +        return vms
    2.72 +    
    2.73 +    # list running VMs
    2.74 +    def listRunningVMS(self):
    2.75 +        cmd = 'VBoxManage list runningvms'
    2.76 +        result = self.hostExecute(cmd)[1]
    2.77          vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
    2.78          return vms
    2.79      
    2.80 @@ -194,63 +182,155 @@
    2.81      # configures hostonly networking and DHCP server. requires admin rights
    2.82      def configureHostNetworking(self):
    2.83          #cmd = 'vboxmanage list hostonlyifs'
    2.84 -        #self.execute(cmd)
    2.85 +        #self.hostExecute(cmd)
    2.86          #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
    2.87 -        #self.execute(cmd)
    2.88 +        #self.hostExecute(cmd)
    2.89          #cmd = 'vboxmanage hostonlyif create'
    2.90 -        #self.execute(cmd)
    2.91 +        #self.hostExecute(cmd)
    2.92          cmd = 'VBoxManage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'
    2.93 -        self.execute(cmd)
    2.94 +        self.hostExecute(cmd)
    2.95          #cmd = 'vboxmanage dhcpserver add'
    2.96 -        #self.execute(cmd)
    2.97 +        #self.hostExecute(cmd)
    2.98          cmd = 'VBoxManage dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.200'
    2.99 -        self.execute(cmd)
   2.100 +        self.hostExecute(cmd)
   2.101      
   2.102      #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
   2.103      def createVM(self, vm_name):
   2.104          hostonly_if = self.getHostOnlyIFs()
   2.105 +        cmd = 'VBoxManage createvm --name ' + vm_name + ' --ostype Debian --register'
   2.106 +        self.hostExecute(cmd)
   2.107 +        cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat' 
   2.108 +        self.hostExecute(cmd)
   2.109 +        cmd = 'VBoxManage storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'
   2.110 +        self.hostExecute(cmd)
   2.111 +        return
   2.112 +    
   2.113 +    # attach storage image to controller
   2.114 +    def storageAttach(self, vm_name):
   2.115 +        if self.isStorageAttached(vm_name):
   2.116 +            self.storageDetach(vm_name)
   2.117          machineFolder = self.getDefaultMachineFolder()
   2.118 -        cmd = 'VBoxManage createvm --name ' + vm_name + ' --ostype Debian --register'
   2.119 -        self.execute(cmd)
   2.120 -        cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat' 
   2.121 -        self.execute(cmd)
   2.122 -        cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --portcount 2'
   2.123 -        self.execute(cmd)
   2.124 -        cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --medium \"'+ machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"' #--mtype immutable
   2.125 -        self.execute(cmd)
   2.126 +        cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'
   2.127 +        self.hostExecute(cmd)
   2.128          return
   2.129      
   2.130 +    # return true if storage is attached 
   2.131 +    def isStorageAttached(self, vm_name):
   2.132 +        info = self.getVMInfo(vm_name)
   2.133 +        return (info['SATA-0-0']!='none')
   2.134 +    
   2.135 +    # detach storage from controller
   2.136 +    def storageDetach(self, vm_name):
   2.137 +        if self.isStorageAttached(vm_name):
   2.138 +            cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'
   2.139 +            self.hostExecute(cmd)
   2.140 +        return
   2.141 +    
   2.142 +    def changeStorageType(self, filename, type):
   2.143 +        cmd = 'VBoxManage modifyhd \"' + filename + '\" --type ' + type
   2.144 +        self.hostExecute(cmd)
   2.145 +        return
   2.146 +    
   2.147 +    # list storage snaphots for VM
   2.148 +    def updateTemplate(self):
   2.149 +        self.poweroffVM('SecurityDVM')
   2.150 +        self.waitShutdown('SecurityDVM')
   2.151 +        
   2.152 +        # check for updates
   2.153 +        self.genCertificateISO('SecurityDVM')
   2.154 +        self.attachCertificateISO('SecurityDVM')
   2.155 +        
   2.156 +        self.storageDetach('SecurityDVM')
   2.157 +        cmd = 'VBoxManage list hdds'
   2.158 +        results = self.hostExecute(cmd)[1]
   2.159 +        results = results.replace('Parent UUID', 'Parent')
   2.160 +        items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   2.161 +        
   2.162 +        snaps = dict()   
   2.163 +        for item in items:
   2.164 +            #print item
   2.165 +            props = dict()
   2.166 +            for line in item.splitlines():
   2.167 +                if line != "":         
   2.168 +                    k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
   2.169 +                    props[k] = v;
   2.170 +            snaps[props['UUID']] = props
   2.171 +        
   2.172 +        machineFolder = self.getDefaultMachineFolder()
   2.173 +        template_storage = machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
   2.174 +        
   2.175 +        # find template uuid
   2.176 +        template_uuid = ''
   2.177 +        for hdd in snaps.values():
   2.178 +            if hdd['Location'] == template_storage:
   2.179 +                template_uuid = hdd['UUID']
   2.180 +        print 'found parent uuid ' + template_uuid
   2.181 +        
   2.182 +        # remove snapshots 
   2.183 +        for hdd in snaps.values():
   2.184 +            if hdd['Parent'] == template_uuid:
   2.185 +                #template_uuid = hdd['UUID']
   2.186 +                print 'removing snapshot ' + hdd['UUID']
   2.187 +                cmd = 'VBoxManage closemedium disk {' + hdd['UUID'] + '} --delete'
   2.188 +                results = self.hostExecute(cmd)[1]
   2.189 +                # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   2.190 +        
   2.191 +        self.changeStorageType(template_storage,'normal')
   2.192 +        self.storageAttach('SecurityDVM')
   2.193 +        self.startVM('SecurityDVM')
   2.194 +        self.waitStartup('SecurityDVM')
   2.195 +        self.guestExecute('SecurityDVM', 'sudo apt-get -y update' )
   2.196 +        self.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
   2.197 +        self.stopVM('SecurityDVM')
   2.198 +        self.waitShutdown('SecurityDVM')
   2.199 +        self.storageDetach('SecurityDVM')
   2.200 +        self.changeStorageType(template_storage,'immutable')
   2.201 +        self.storageAttach('SecurityDVM')
   2.202 +        
   2.203 +    
   2.204      #remove VM from the system. should be used on VMs returned by listSDVMs    
   2.205      def removeVM(self, vm_name):
   2.206          print('removing ' + vm_name)
   2.207          cmd = 'VBoxManage unregistervm ' + vm_name + ' --delete'
   2.208 -        print self.execute(cmd)
   2.209 +        print self.hostExecute(cmd)
   2.210          machineFolder = self.getDefaultMachineFolder()
   2.211          cmd = self.cygwin_path + 'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
   2.212 -        print self.execute(cmd)
   2.213 +        print self.hostExecute(cmd)
   2.214      
   2.215      # start VM
   2.216      def startVM(self, vm_name):
   2.217          print('starting ' +  vm_name)
   2.218          cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' 
   2.219 -        result = self.execute(cmd)
   2.220 +        result = self.hostExecute(cmd)
   2.221          while not string.find(str(result), 'successfully started',):
   2.222              print "Failed to start SDVM: ", vm_name, " retrying"
   2.223              time.sleep(1)
   2.224 -            result = self.execute(cmd)
   2.225 +            result = self.hostExecute(cmd)
   2.226          return result[0]
   2.227 -        
   2.228 -    # stop VM    
   2.229 +    
   2.230 +    # return wether VM is running or not
   2.231 +    def isVMRunning(self, vm_name):
   2.232 +        return vm_name in self.listRunningVMS()    
   2.233 +    
   2.234 +    # stop VM
   2.235      def stopVM(self, vm_name):
   2.236          print('stopping ' + vm_name)
   2.237 +        cmd = 'sudo shutdown -h now'
   2.238 +        self.guestExecute('SecurityDVM', cmd)
   2.239 +            
   2.240 +    # poweroff VM
   2.241 +    def poweroffVM(self, vm_name):
   2.242 +        if not self.isVMRunning(vm_name):
   2.243 +            return
   2.244 +        print('powering off ' + vm_name)
   2.245          cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff'
   2.246 -        self.execute(cmd)
   2.247 +        self.hostExecute(cmd)
   2.248      
   2.249      # return the hostOnly IP for a running guest    
   2.250      def getHostOnlyIP(self, vm_name):
   2.251          print('gettting hostOnly IP address ' + vm_name)
   2.252          cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'
   2.253 -        result = self.execute(cmd)
   2.254 +        result = self.hostExecute(cmd)
   2.255          if result=='':
   2.256              return None
   2.257          result = result[1]
   2.258 @@ -261,14 +341,14 @@
   2.259      # attach removable storage device to VM by provision of filter
   2.260      def attachRSD(self, vm_name, rsd_filter):
   2.261          cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision
   2.262 -        print self.execute(cmd)
   2.263 +        print self.hostExecute(cmd)
   2.264          
   2.265      
   2.266      # return the description set for an existing VM
   2.267      def getVMInfo(self, vm_name):
   2.268          cmd = 'VBoxManage showvminfo ' + vm_name + ' --machinereadable'
   2.269 -        results = self.execute(cmd)[1]
   2.270 -        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
   2.271 +        results = self.hostExecute(cmd)[1]
   2.272 +        props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
   2.273          return props
   2.274      
   2.275      # return the configured USB filter for an existing VM 
   2.276 @@ -284,31 +364,40 @@
   2.277      #generates ISO containing authorized_keys for use with guest VM
   2.278      def genCertificateISO(self, vm_name):
   2.279          machineFolder = self.getDefaultMachineFolder()
   2.280 +        machineFolder = self.cygwinPath(machineFolder)
   2.281 +        # remove .ssh folder if exists
   2.282 +        cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'
   2.283 +        self.hostExecute(cmd)
   2.284 +        # remove .ssh folder if exists
   2.285 +        cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'
   2.286 +        self.hostExecute(cmd)
   2.287          # create .ssh folder in vm_name
   2.288 -        cmd = self.cygwin_path+'bash.exe --login -c \"mkdir -p \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
   2.289 -        self.execute(cmd)
   2.290 +        cmd = self.cygwin_path+'bash.exe --login -c \"mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'
   2.291 +        self.hostExecute(cmd)
   2.292          # generate dvm_key pair in vm_name / .ssh     
   2.293 -        cmd = self.cygwin_path+'bash.exe --login -c \"ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key\\\"\"'   #'echo -e "y\\n" |',
   2.294 -        self.execute(cmd)
   2.295 -        # set permissions for keys
   2.296 -        #TODO: test without chmod
   2.297 -        cmd = self.cygwin_path+'bash.exe --login -c \"chmod 500 \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\*\\\"\"'
   2.298 -        self.execute(cmd)
   2.299 +        cmd = self.cygwin_path+'bash.exe --login -c \"ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"'   #'echo -e "y\\n" |',
   2.300 +        self.hostExecute(cmd)
   2.301          # move out private key
   2.302 -        cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key\\\" \\\"' + machineFolder + '\\' + vm_name + '\\\"'
   2.303 -        self.execute(cmd)
   2.304 +        cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'
   2.305 +        self.hostExecute(cmd)
   2.306 +        # set permissions for private key
   2.307 +        cmd = self.cygwin_path+'bash.exe --login -c \"chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"'
   2.308 +        self.hostExecute(cmd)
   2.309          # rename public key to authorized_keys
   2.310 -        cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\dvm_key.pub\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\authorized_keys\\\"'
   2.311 -        self.execute(cmd)
   2.312 +        cmd = self.cygwin_path+'bash.exe --login -c \"mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'
   2.313 +        self.hostExecute(cmd)
   2.314 +        # set permissions for authorized_keys
   2.315 +        cmd = self.cygwin_path+'bash.exe --login -c \"chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"'
   2.316 +        self.hostExecute(cmd)
   2.317          # generate iso image with .ssh/authorized keys
   2.318 -        cmd = self.cygwin_path+'bash.exe --login -c \"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\\\" \\\"' + machineFolder + '\\' + vm_name + '\\.ssh\\\"\"'
   2.319 -        self.execute(cmd)
   2.320 +        cmd = self.cygwin_path+'bash.exe --login -c \"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'
   2.321 +        self.hostExecute(cmd)
   2.322      
   2.323      # attaches generated ssh public cert to guest vm
   2.324      def attachCertificateISO(self, vm_name):
   2.325          machineFolder = self.getDefaultMachineFolder()
   2.326 -        cmd = 'vboxmanage storageattach ' + vm_name + ' --storagectl contr1 --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'
   2.327 -        result = self.execute(cmd)
   2.328 +        cmd = 'vboxmanage storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'
   2.329 +        result = self.hostExecute(cmd)
   2.330          return result
   2.331      
   2.332      handleDeviceChangeLock = threading.Lock()
   2.333 @@ -348,6 +437,23 @@
   2.334              VMManager.handleDeviceChangeLock.release()
   2.335              return new_ip
   2.336      
   2.337 +    # wait for machine to come up
   2.338 +    def waitStartup(self, vm_name): 
   2.339 +        new_ip = None
   2.340 +        while new_ip == None:
   2.341 +            time.sleep(1)
   2.342 +            new_ip = self.getHostOnlyIP(vm_name)
   2.343 +        while new_ip not in self.startNotifications:
   2.344 +            time.sleep(1)
   2.345 +        return
   2.346 +    
   2.347 +    # wait for machine to shutdown
   2.348 +    def waitShutdown(self, vm_name):
   2.349 +        while vm_name in self.listRunningVMS():
   2.350 +            time.sleep(1)
   2.351 +        return
   2.352 +        
   2.353 +    # handles browsing request    
   2.354      def handleBrowsingRequest(self):
   2.355          if VMManager.handleDeviceChangeLock.acquire(True):
   2.356              new_ip = None
   2.357 @@ -356,12 +462,7 @@
   2.358              self.genCertificateISO(new_sdvm)
   2.359              self.attachCertificateISO(new_sdvm)
   2.360              self.startVM(new_sdvm)
   2.361 -            # wait for machine to come up
   2.362 -            while new_ip == None:
   2.363 -                time.sleep(1)
   2.364 -                new_ip = self.getHostOnlyIP(new_sdvm)
   2.365 -            while new_ip not in self.startNotifications:
   2.366 -                time.sleep(1)
   2.367 +            self.waitStartup(new_sdvm)
   2.368              if new_ip != None:
   2.369                  self.mapNetworkDrive('g:', '\\\\' + new_ip + '\\Download', None, None)
   2.370              #TODO: cleanup notifications somwhere else (eg. machine shutdown)
   2.371 @@ -369,17 +470,44 @@
   2.372              VMManager.handleDeviceChangeLock.release()
   2.373          return new_sdvm
   2.374      
   2.375 +    def cygwinPath(self, path):
   2.376 +        # TODO: test if env ist cygwin
   2.377 +        cmd = self.cygwin_path + 'bash.exe --login -c \"cygpath -u \\\"' + path + '\\\"\"' 
   2.378 +        return self.hostExecute(cmd)[1].rstrip('\n')
   2.379 +    
   2.380 +    #executes command on host system
   2.381 +    def hostExecute(self, cmd, wait_return=True ):
   2.382 +        if DEBUG:
   2.383 +            print('trying to launch: ' + cmd)
   2.384 +        process = Popen(cmd, stdout=PIPE, stderr=PIPE) #shell = True
   2.385 +        if DEBUG:
   2.386 +            print('launched: ' + cmd)
   2.387 +        if not wait_return:
   2.388 +            return [0, 'working in background', '']
   2.389 +        result = process.wait()
   2.390 +        res_stdout = process.stdout.read();
   2.391 +        res_stderr = process.stderr.read();
   2.392 +        if DEBUG:
   2.393 +            if res_stdout != "":
   2.394 +                print res_stdout
   2.395 +            if res_stderr != "":
   2.396 +                print res_stderr
   2.397 +        if result !=0:
   2.398 +            raise VMManagerException(res_stderr)
   2.399 +        return result, res_stdout, res_stderr
   2.400 +    
   2.401      # executes command over ssh on guest vm
   2.402 -    def sshGuestExecute(self, vm_name, prog, user_name='osecuser'):
   2.403 +    def guestExecute(self, vm_name, prog, user_name='osecuser'):
   2.404          # get vm ip
   2.405          address = self.getHostOnlyIP(vm_name)
   2.406          machineFolder = self.getDefaultMachineFolder()
   2.407 -        # run command
   2.408 -        cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\"  ' + user_name + '@' + address + ' ' + prog + '\"'
   2.409 -        return self.execute(cmd)
   2.410 +        machineFolder = self.cygwinPath(machineFolder)
   2.411 +        # run command //mintty.exe -e
   2.412 +        cmd = self.cygwin_path + 'bash.exe --login -c \"/usr/bin/ssh -v -i \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"  ' + user_name + '@' + address + ' ' + prog + '\"'
   2.413 +        return self.hostExecute(cmd)
   2.414      
   2.415      # executes command over ssh on guest vm with X forwarding
   2.416 -    def sshGuestX11Execute(self, vm_name, prog, user_name='osecuser'):
   2.417 +    def guestExecuteX11(self, vm_name, prog, user_name='osecuser'):
   2.418          #TODO: verify if X server is running on user account 
   2.419          #TODO: set DISPLAY accordingly
   2.420          address = self.getHostOnlyIP(vm_name)
   2.421 @@ -447,6 +575,7 @@
   2.422              return -1
   2.423          return 1
   2.424  
   2.425 +
   2.426  if __name__ == '__main__':
   2.427      man = VMManager.getInstance()
   2.428      #man.removeVM('SecurityDVM0')
   2.429 @@ -463,9 +592,22 @@
   2.430      #man.attachCertificateISO(new_vm)
   2.431      
   2.432      #man.attachCertificateISO(vm_name)
   2.433 -    #man.sshGuestExecute(vm_name, "ls")
   2.434 -    man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
   2.435 -    time.sleep(60)
   2.436 +    #man.guestExecute(vm_name, "ls")
   2.437 +    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
   2.438 +    #time.sleep(60)
   2.439 +    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
   2.440 +    #man.genCertificateISO('SecurityDVM')
   2.441 +    #man.attachCertificateISO('SecurityDVM')
   2.442 +    #man.isStorageAttached('SecurityDVM')
   2.443 +    man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   2.444 +    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
   2.445 +    
   2.446 +    #man.stopVM('SecurityDVM')
   2.447 +    #man.storageDetach('SecurityDVM')
   2.448 +    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
   2.449 +    #man.storageAttach('SecurityDVM')
   2.450 +    
   2.451 +    
   2.452      #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
   2.453      #man.execute(cmd)
   2.454      
   2.455 \ No newline at end of file