OpenSecurity/bin/vmmanager.pyw
author mb
Fri, 21 Feb 2014 11:04:04 +0100
changeset 78 23551f635ca9
parent 66 d768c98d1e48
child 79 617009c32da0
permissions -rwxr-xr-x
changed service config
     1 '''
     2 Created on Nov 19, 2013
     3 
     4 @author: BarthaM
     5 '''
     6 import os
     7 import os.path
     8 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
     9 import subprocess
    10 import sys
    11 import re
    12 
    13 from cygwin import Cygwin
    14 from environment import Environment
    15 import threading
    16 import time
    17 import string
    18 
    19 import shutil
    20 import stat
    21 import tempfile
    22 from opensecurity_util import logger, setupLogger, OpenSecurityException
    23 
    24 DEBUG = True
    25 
    26 class VMManagerException(Exception):
    27     def __init__(self, value):
    28         self.value = value
    29     def __str__(self):
    30         return repr(self.value)
    31 
    32 class USBFilter:
    33     vendorid = ""
    34     productid = ""
    35     revision = ""
    36     
    37     def __init__(self, vendorid, productid, revision):
    38         self.vendorid = vendorid.lower()
    39         self.productid = productid.lower()
    40         self.revision = revision.lower()
    41         return
    42     
    43     def __eq__(self, other):
    44         return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
    45     
    46     def __hash__(self):
    47         return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
    48     
    49     def __repr__(self):
    50         return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
    51  
    52 class VMManager(object):
    53     vmRootName = "SecurityDVM"
    54     systemProperties = None
    55     startNotifications = list()
    56     _instance = None
    57     machineFolder = ''
    58     attachedRSDs = None  
    59     connectedRSDs = None
    60     
    61     def __init__(self):
    62         self.systemProperties = self.getSystemProperties()
    63         self.machineFolder = self.systemProperties["Default machine folder"]
    64         self.cleanup()
    65         self.attachedRSDs = self.getAttachedRSDs()
    66         self.connectedRSDs = self.getConnectedRSDS()
    67         return
    68     
    69     @staticmethod
    70     def getInstance():
    71         if VMManager._instance == None:
    72             VMManager._instance = VMManager()
    73         return VMManager._instance
    74     
    75     def cleanup(self):
    76         self.unmapNetworkDrive('G:')
    77         self.unmapNetworkDrive('H:')
    78         for vm in self.listSDVM():
    79             self.poweroffVM(vm)
    80             self.removeVM(vm)
    81         
    82     def putStartNotification(self, ip):
    83         self.startNotifications.append(ip)
    84     
    85     def isSDVMStarted(self, ip):
    86         return self.startNotifications.contains(ip)
    87     
    88     # return hosty system properties
    89     def getSystemProperties(self):
    90         result = Cygwin.vboxExecute('list systemproperties')
    91         if result[1]=='':
    92             return None
    93         props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    94         #logger.debug(props)
    95         return props
    96     
    97     # return the folder containing the guest VMs     
    98     def getMachineFolder(self):
    99         return self.machineFolder
   100     
   101     #list the hostonly IFs exposed by the VBox host
   102     def getHostOnlyIFs(self):
   103         result = Cygwin.vboxExecute('list hostonlyifs')[1]
   104         if result=='':
   105             return None
   106         props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
   107         return props
   108 
   109     # list all existing VMs registered with VBox
   110     def listVM(self):
   111         result = Cygwin.vboxExecute('list vms')[1]
   112         vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
   113         return vms
   114     
   115     # list running VMs
   116     def listRunningVMS(self):
   117         result = Cygwin.vboxExecute('list runningvms')[1]
   118         vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
   119         return vms
   120     
   121     # list existing SDVMs
   122     def listSDVM(self):
   123         vms = self.listVM()
   124         svdms = []
   125         for vm in vms:
   126             if vm.startswith(self.vmRootName) and vm != self.vmRootName:
   127                 svdms.append(vm)
   128         return svdms
   129     
   130     # generate valid (not already existing SDVM name). necessary for creating a new VM
   131     def generateSDVMName(self):
   132         vms = self.listVM()
   133         for i in range(0,999):
   134             if(not self.vmRootName+str(i) in vms):
   135                 return self.vmRootName+str(i)
   136         return ''
   137     
   138     # return the RSDs connected to the host
   139     def getConnectedRSDS(self):
   140         results = Cygwin.vboxExecute('list usbhost')[1]
   141         results = results.split('Host USB Devices:')[1].strip()
   142         
   143         items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   144         rsds = dict()   
   145         for item in items:
   146             props = dict()
   147             for line in item.splitlines():
   148                 if line != "":         
   149                     k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
   150                     props[k] = v
   151             
   152             if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
   153                 usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'], 
   154                                         re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
   155                                         re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
   156                 rsds[props['UUID']] = usb_filter;
   157                 logger.debug(usb_filter)
   158         return rsds
   159     
   160     # return the RSDs attached to all existing SDVMs
   161     def getAttachedRSDs(self):
   162         vms = self.listSDVM()
   163         attached_devices = dict()
   164         for vm in vms:
   165             rsd_filter = self.getUSBFilter(vm)
   166             if rsd_filter != None:
   167                 attached_devices[vm] = rsd_filter
   168         return attached_devices
   169     
   170     # configures hostonly networking and DHCP server. requires admin rights
   171     def configureHostNetworking(self):
   172         #cmd = 'vboxmanage list hostonlyifs'
   173         #Cygwin.vboxExecute(cmd)
   174         #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
   175         #Cygwin.vboxExecute(cmd)
   176         #cmd = 'vboxmanage hostonlyif create'
   177         #Cygwin.vboxExecute(cmd)
   178         Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0')
   179         #cmd = 'vboxmanage dhcpserver add'
   180         #Cygwin.vboxExecute(cmd)
   181         Cygwin.vboxExecute('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')
   182     
   183     #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
   184     def createVM(self, vm_name):
   185         hostonly_if = self.getHostOnlyIFs()
   186         Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register')
   187         Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat')
   188         Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2')
   189         return
   190     
   191     # attach storage image to controller
   192     def storageAttach(self, vm_name):
   193         if self.isStorageAttached(vm_name):
   194             self.storageDetach(vm_name)
   195         Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')
   196         return
   197     
   198     # return true if storage is attached 
   199     def isStorageAttached(self, vm_name):
   200         info = self.getVMInfo(vm_name)
   201         return (info['SATA-0-0']!='none')
   202     
   203     # detach storage from controller
   204     def storageDetach(self, vm_name):
   205         if self.isStorageAttached(vm_name):
   206             Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')
   207         return
   208     
   209     def changeStorageType(self, filename, storage_type):
   210         Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)
   211         return
   212     
   213     # list storage snaphots for VM
   214     def updateTemplate(self):
   215         self.cleanup()
   216         self.poweroffVM('SecurityDVM')
   217         self.waitShutdown('SecurityDVM')
   218         
   219         # check for updates
   220         self.genCertificateISO('SecurityDVM')
   221         self.attachCertificateISO('SecurityDVM')
   222         
   223         self.storageDetach('SecurityDVM')
   224         results = Cygwin.vboxExecute('list hdds')[1]
   225         results = results.replace('Parent UUID', 'Parent')
   226         items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   227         
   228         snaps = dict()   
   229         for item in items:
   230             props = dict()
   231             for line in item.splitlines():
   232                 if line != "":         
   233                     k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
   234                     props[k] = v;
   235             snaps[props['UUID']] = props
   236         
   237         
   238         template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
   239         
   240         # find template uuid
   241         template_uuid = ''
   242         for hdd in snaps.values():
   243             if hdd['Location'] == template_storage:
   244                 template_uuid = hdd['UUID']
   245         logger.debug('found parent uuid ' + template_uuid)
   246         
   247         # remove snapshots 
   248         for hdd in snaps.values():
   249             if hdd['Parent'] == template_uuid:
   250                 #template_uuid = hdd['UUID']
   251                 logger.debug('removing snapshot ' + hdd['UUID'])
   252                 results = Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete')[1]
   253                 # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   254         
   255         self.changeStorageType(template_storage,'normal')
   256         self.storageAttach('SecurityDVM')
   257         self.startVM('SecurityDVM')
   258         self.waitStartup('SecurityDVM')
   259         Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'  )
   260         Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'  )
   261         self.stopVM('SecurityDVM')
   262         #self.hibernateVM('SecurityDVM')
   263         self.waitShutdown('SecurityDVM')
   264         self.storageDetach('SecurityDVM')
   265         self.changeStorageType(template_storage,'immutable')
   266         self.storageAttach('SecurityDVM')
   267         self.handleDeviceChange()
   268     
   269     #remove VM from the system. should be used on VMs returned by listSDVMs    
   270     def removeVM(self, vm_name):
   271         logger.info('Removing ' + vm_name)
   272         Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')
   273         machineFolder = Cygwin.cygPath(self.machineFolder)
   274         Cygwin.bashExecute('"/usr/bin/rm -rf ' + machineFolder + '/' + vm_name + '"')
   275     
   276     # start VM
   277     def startVM(self, vm_name):
   278         logger.info('Starting ' +  vm_name)
   279         result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )
   280         while not string.find(str(result), 'successfully started',):
   281             logger.error("Failed to start SDVM: " + vm_name + " retrying")
   282             time.sleep(1)
   283             result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless')
   284         return result[0]
   285     
   286     # return wether VM is running or not
   287     def isVMRunning(self, vm_name):
   288         return vm_name in self.listRunningVMS()    
   289     
   290     # stop VM
   291     def stopVM(self, vm_name):
   292         logger.info('Sending shutdown signal to ' + vm_name)
   293         Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )
   294     
   295     # stop VM
   296     def hibernateVM(self, vm_name):
   297         logger.info('Sending shutdown signal to ' + vm_name)
   298         Cygwin.sshExecute( '"sudo hibernate-disk"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' )
   299             
   300     # poweroff VM
   301     def poweroffVM(self, vm_name):
   302         if not self.isVMRunning(vm_name):
   303             return
   304         logger.info('Powering off ' + vm_name)
   305         return Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff')
   306     
   307     # return the hostOnly IP for a running guest
   308     @staticmethod    
   309     def getHostOnlyIP(vm_name):
   310         logger.info('Gettting hostOnly IP address ' + vm_name)
   311         result = Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP')
   312         if result=='':
   313             return None
   314         result = result[1]
   315         if result.startswith('No value set!'):
   316             return None
   317         return result[result.index(':')+1:].strip()
   318     
   319     # attach removable storage device to VM by provision of filter
   320     def attachRSD(self, vm_name, rsd_filter):
   321         return Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision)
   322     
   323     # detach removable storage from VM by 
   324     def detachRSD(self, vm_name):
   325         return Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name )
   326         
   327     
   328     # return the description set for an existing VM
   329     def getVMInfo(self, vm_name):
   330         results = Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable')[1]
   331         props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
   332         #logger.debug(props)
   333         return props
   334     
   335     # return the configured USB filter for an existing VM 
   336     def getUSBFilter(self, vm_name):
   337         props = self.getVMInfo(vm_name)
   338         keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
   339         keyset = set(props.keys())
   340         usb_filter = None
   341         if keyset.issuperset(keys):
   342             usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
   343         return usb_filter
   344     
   345     #generates ISO containing authorized_keys for use with guest VM
   346     def genCertificateISO(self, vm_name):
   347         machineFolder = Cygwin.cygPath(self.machineFolder)
   348         # remove .ssh folder if exists
   349         cmd = '\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"'
   350         Cygwin.bashExecute(cmd)
   351         # remove .ssh folder if exists
   352         Cygwin.bashExecute('\"/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')
   353         # create .ssh folder in vm_name
   354         Cygwin.bashExecute('\"/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')
   355         # generate dvm_key pair in vm_name / .ssh     
   356         Cygwin.bashExecute('\"/usr/bin/ssh-keygen -q -t rsa -N \\"\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"\"')
   357         # move out private key
   358         Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"')
   359         # set permissions for private key
   360         Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"\"')
   361         # rename public key to authorized_keys
   362         Cygwin.bashExecute('\"/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')
   363         # set permissions for authorized_keys
   364         Cygwin.bashExecute('\"/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"\"')
   365         # generate iso image with .ssh/authorized keys
   366         Cygwin.bashExecute('\"/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"\"')
   367     
   368     # attaches generated ssh public cert to guest vm
   369     def attachCertificateISO(self, vm_name):
   370         result = Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"')
   371         return result
   372     
   373     
   374     handleDeviceChangeLock = threading.Lock()
   375     trigger = False
   376     # handles device change events
   377     def handleDeviceChange(self):
   378         if VMManager.handleDeviceChangeLock.acquire(True):
   379             #logger.debug("triggered")
   380             #VMManager.handleDeviceChangeLock.release()
   381             #return
   382             #destroy unused vms
   383             #diff = DictDiffer(self.connectedRSDs, tmp_conn)
   384             retry = 0
   385             while retry < 30:
   386                 if self.getConnectedRSDS().keys() == self.connectedRSDs.keys():
   387                     logger.info("Nothing's changed. Waiting for VBox USB sub-system to update...")
   388                 else:
   389                     self.connectedRSDs = self.getConnectedRSDS()
   390                     break
   391                 time.sleep(1)
   392                 retry+=1
   393             
   394             if retry == 30:
   395                 VMManager.handleDeviceChangeLock.release()
   396                 return None
   397 
   398             logger.info("Something's changed")
   399             
   400             self.attachedRSDs = self.getAttachedRSDs()
   401             
   402             for vm_name in self.attachedRSDs.keys():
   403                 if self.attachedRSDs[vm_name] not in self.connectedRSDs.values():
   404                     self.unmapNetworkDrive('h:')
   405                     #self.stopVM(vm_name)
   406                     self.detachRSD(vm_name)
   407                     self.poweroffVM(vm_name)
   408                     self.removeVM(vm_name)
   409             #create new vm for attached device if any
   410             self.attachedRSDs = self.getAttachedRSDs()
   411             self.connectedRSDs = self.getConnectedRSDS()
   412             
   413             new_ip = None
   414             for connected_device in self.connectedRSDs.values():
   415                 if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()):
   416                     new_sdvm = self.generateSDVMName()
   417                     self.createVM(new_sdvm)
   418                     self.storageAttach(new_sdvm)
   419                     self.attachRSD(new_sdvm, connected_device)
   420                     self.startVM(new_sdvm)
   421                     new_ip = self.waitStartup(new_sdvm)
   422                     
   423                     if new_ip != None:
   424                         self.mapNetworkDrive('h:', '\\\\' + new_ip + '\\USB', None, None)
   425                     #TODO: cleanup notifications somwhere else (eg. machine shutdown)
   426                     self.startNotifications.remove(new_ip)
   427             VMManager.handleDeviceChangeLock.release()
   428             return new_ip
   429     
   430     # wait for machine to come up
   431     def waitStartup(self, vm_name): 
   432         new_ip = None
   433         while new_ip == None:
   434             time.sleep(1)
   435             new_ip = VMManager.getHostOnlyIP(vm_name)
   436         while new_ip not in self.startNotifications:
   437             time.sleep(1)
   438         return new_ip
   439     
   440     # wait for machine to shutdown
   441     def waitShutdown(self, vm_name):
   442         while vm_name in self.listRunningVMS():
   443             time.sleep(1)
   444         return
   445         
   446     # handles browsing request    
   447     def handleBrowsingRequest(self):
   448         if VMManager.handleDeviceChangeLock.acquire(True):
   449             new_sdvm = self.generateSDVMName()
   450             self.createVM(new_sdvm)
   451             self.storageAttach(new_sdvm)
   452             self.genCertificateISO(new_sdvm)
   453             self.attachCertificateISO(new_sdvm)
   454             self.startVM(new_sdvm)
   455             new_ip = self.waitStartup(new_sdvm)
   456             if new_ip != None:
   457                 self.mapNetworkDrive('g:', '\\\\' + new_ip + '\\Download', None, None)
   458             #TODO: cleanup notifications somwhere else (eg. machine shutdown)
   459             self.startNotifications.remove(new_ip)
   460             VMManager.handleDeviceChangeLock.release()
   461         return new_sdvm
   462     
   463     #Small function to check the availability of network resource.
   464     #def isAvailable(self, path):
   465         #return os.path.exists(path)
   466         #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES')
   467         #return string.find(result[1], 'YES',)
   468     
   469     #Small function to check if the mention location is a directory
   470     def isDirectory(self, path):
   471         result = Cygwin.cmdExecute('dir ' + path + ' | FIND ".."')
   472         return string.find(result[1], 'DIR',)
   473 
   474     def mapNetworkDrive(self, drive, networkPath, user, password):
   475         self.unmapNetworkDrive(drive)
   476         #Check for drive availability
   477         if os.path.exists(drive):
   478             logger.error("Drive letter is already in use: " + drive)
   479             return -1
   480         #Check for network resource availability
   481         while not os.path.exists(networkPath):
   482             time.sleep(1)
   483             logger.info("Path not accessible: " + networkPath + " retrying")
   484             #return -1
   485     
   486         command = 'USE ' + drive + ' ' + networkPath
   487         if user != None:
   488             command += ' ' + password + ' /User' + user
   489     
   490         #TODO: Execute 'NET USE' command with authentication
   491         result = Cygwin.execute('C:\\Windows\\system32\\net.exe', command)
   492         if string.find(result[1], 'successfully',) == -1:
   493             logger.error("Failed: NET " + command)
   494             return -1
   495         return 1
   496     
   497     def unmapNetworkDrive(self, drive):
   498         if not os.path.exists(drive):
   499             return -1
   500         result = Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES')
   501         if string.find(str(result), 'successfully',) == -1:
   502             logger.error(result[2])
   503             return -1
   504         return 1
   505 
   506 class DeviceHandler(threading.Thread): 
   507     vmm = None
   508     triggered = False
   509     def __init__(self, zahl): 
   510         threading.Thread.__init__(self) 
   511         self.vmm = None 
   512  
   513     def run(self):
   514         while True:
   515             if self.triggered:
   516                 logger.debug("triggered")
   517                 triggered = False
   518             else:
   519                 time.sleep(1)
   520                 
   521 
   522 if __name__ == '__main__':
   523     man = VMManager.getInstance()
   524     #man.listVM()
   525     print man.getConnectedRSDs()
   526     
   527     #man.listVM()
   528     #man.listVM()
   529     #man.listVM()
   530     #man.listVM()
   531     #man.genCertificateISO('SecurityDVM0')
   532     #man.guestExecute('SecurityDVM0', '/bin/ls -la')
   533     #logger = setupLogger('VMManager')
   534     c = Cygwin()
   535     
   536     #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
   537     #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
   538     #man.removeVM('SecurityDVM0')
   539     #man.netUse('192.168.56.134', 'USB\\')
   540     #ip = '192.168.56.139'
   541     
   542     #man.cygwin_path = 'c:\\cygwin64\\bin\\'
   543     #man.handleDeviceChange()
   544     #print man.listSDVM()
   545     #man.configureHostNetworking()
   546     #new_vm = man.generateSDVMName()
   547     #man.createVM(new_vm)
   548     
   549     #print Cygwin.cmd()
   550     #man.isAvailable('c:')
   551     #ip = man.getHostOnlyIP('SecurityDVM0')
   552     #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
   553     
   554     #man.genCertificateISO(new_vm)
   555     #man.attachCertificateISO(new_vm)
   556     
   557     #man.attachCertificateISO(vm_name)
   558     #man.guestExecute(vm_name, "ls")
   559     #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
   560     #time.sleep(60)
   561     #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
   562     #man.genCertificateISO('SecurityDVM')
   563     #man.attachCertificateISO('SecurityDVM')
   564     #man.isStorageAttached('SecurityDVM')
   565     #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   566     #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
   567     
   568     #man.stopVM('SecurityDVM')
   569     #man.storageDetach('SecurityDVM')
   570     #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
   571     #man.storageAttach('SecurityDVM')
   572     
   573     
   574     #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
   575     #man.execute(cmd)
   576