예제 #1
0
    def __setup(self):
        
        LOG.info("TODO: Add code for loading 'complete_image' here")

        if not self.vm_session:
            
            LOG.info("Creating a new ESX Session to %s" % self.service_url)
            self.vm_session = esx.login(self.service_url,self.un,self.pw)
            
            # notify drone about the new host
            s, hostname = esx.getHostnameESX(self.vm_session)
            s, ip = esx.getIPaddrESX(self.vm_session)
            
            LOG.info("Setup EventEmitter host with %s %s" % (hostname,ip))

        
        # Check if there's enough disk space. If not, die
        self.__check_space_available()
        
        # Connect (or reconnect) to host via VIX, if enabled.
        #if int(getArg('vix_enable','HoneyClient::Manager::ESX::Clone')):
        #    self.vix_disconnect_vm()
        #    self.vix_disconnect_host()
        #    self.vix_connect_host()

        if self.bypass_firewall:
            LOG.info("TODO: Setup Firewall...")
        
        if self.num_snapshots >= getArg('max_num_snapshots','HoneyClient::Manager::ESX'):
            LOG.info("Suspending Clone VM. Reached the maximum number of snapshots")
            
            s,r = esx.suspendVM(self.vm_session,self.quick_clone_vm_name)
            
            self.quick_clone_vm_name = None
            self.name = None
            self.mac_address = None
            self.ip_address = None
            self.num_snapshots = 0
        
        # dont_init is ONLY used for testing without fully init() the object
        if self.dont_init:
            # we're done here
            return
        else:
            try:
                self.__do_init()
            except SystemExit:
                # Suspend the VM and try again
                LOG.error("Unable to init VM %s - Retrying..." % self.quick_clone_vm_name)
                LOG.info("Suspending the VM")

                suspended_at = datetime.now()
                
                try:
                    s,r = esx.suspendVM(self.vm_session,self.quick_clone_vm_name)
                    self.__change_status("suspended",suspended_at)
                except SystemExit:
                    LOG.error("Unable to suspend the VM")
예제 #2
0
    def __do_init(self):
        """
        replaces the 'init' call in the Perl code
        """
        if not self.quick_clone_vm_name or not self.name or not self.mac_address or not self.ip_address:
            LOG.info("Quick cloning master VM: %s" % self.master_vm_name)
            s, dest_name = esx.quickCloneVM(self.vm_session,self.master_vm_name)
            
            self.quick_clone_vm_name = dest_name
            self.num_snapshots += 1
            self.__change_status("initialized")

            registered = False
            while registered:
                s, registered = esx.isRegisteredVM(self.vm_session,self.quick_clone_vm_name)
                if not registered:
                    # poll for it
                    sleep( self.retry_period )
            
            LOG.info("Retrieving config of clone VM")
            s, self.vm_config = esx.getConfigVM(self.vm_session,self.quick_clone_vm_name)
            self.__change_status("registered")

            started = "no"
            while started != 'poweredOn':
                s, started = esx.getStateVM(self.vm_session,self.quick_clone_vm)
                if started != 'poweredOn':
                    sleep(self.retry_period)
            self.__change_status('running')

            LOG.info("No waiting on valid MAC/IP for clone")
            temp_ip = None
            while not self.ip_address or not self.mac_address:
                s, self.mac_address = esx.getMACaddrVM(self.vm_session,self.quick_clone_vm)
                s, temp_ip = esx.getIPaddrVM(self.vm_session,self.quick_clone_vm)
                
                if temp_ip and temp_ip != self.ip_address:
                    LOG.info("Cloned VM has a new IP")
                    
                    # TODO: call self._deny_network()
                    
                    self.ip_address = temp_ip

                if not self.ip_address or not self.mac_address:
                    snapname = getArg("default_quick_clone_snapshot_name","HoneyClient::Manager::ESX")
                    self.__check_for_bsod(snapname)
                    sleep(self.retry_period)
                    continue
                
                LOG.info("TODO: allow_network")
                LOG.info("get Agent Handle")
                LOG.info("get Agent properties")

                # NEED TO CHECK HERE FOR THE AGENT AND WAIT FOR IT
                # THIS WILL BE 'if' block
                
                LOG.info("Create operational snapshot of the VM")
                desc = getArg("operational_quick_clone_snapshot_description","HoneyClient::Manager::ESX")
                s, snapname = esx.snapshotVM(self.vm_session,self.quick_clone_vm_name,desc)
                self.name = snapname
                self.num_snapshots += 1

                s, hostname = esx.getHostnameESX(self.vm_session)
                s, ip = esx.getIPaddrESX(self.vm_session)
                
                LOG.info("TODO: notify the drone here with a message")
        else:
            
            if not self.name:
                self.__croak("Unable to start clone. No operational snapshot provided")

            LOG.info("Reverting clone VM to operational snapshot")
            
            s = esx.revertVM(self.vm_session,self.quick_clone_vm_name,self.name)
            # Rename the snapshot
            desc = getArg("operational_quick_clone_snapshot_description","HoneyClient::Manager::ESX")
            s, newname = esx.renameSnapshotVM(self.vm_session,self.quick_clone_vm_name,self.name,desc)
            self.name = newname
            LOG.info("Renamed operational snapshot from %s to %s",(self.quick_clone_vm_name,self.name))
            LOG.info("Get the VM config file")

            s, self.vm_config = esx.getConfigVM(self.vm_session,self.quick_clone_vm_name)
            LOG.info("Starting clone VM")
            esx.startVM(self.vm_session,self.quick_clone_vm_name)
            
            # Wait to make sure it's running
            running = False
            while not running:
                s, st = esx.getStateVM(self.vm_session,self.quick_clone_vm_name)
                if st == 'poweredOn':
                    running = True
                else:
                    sleep(self.retry_period)
                    
            self.__change_status("running")

            LOG.info("TODO: start agent & notify drone")