def shutdown(domain):
    '''
    Stop given domain
    '''
    
    # Find libvirt connection for domain
    _, lv_connection = util.find_domain(domain)
    
    # Get libvirt domain object
    lv_domain = lv_connection.lookupByName(domain)
    
    # Destroy domain
    print 'Shutdown domain: %s' % domain 
    lv_domain.destroy()
    
    
 def handler(domain_name, node_from, node_to, starsst, end, info, 
             success, error):
     logger.info('Migrated status: %s - %s' % (domain_name, success))
     
     # Migration failed
     if success == False:
         print 'Error - migration failed, checking if domain still exists...'
         _, lv_connection = util.find_domain(domain)
         if lv_connection is not None:
             logger.info('Retrying migration: %s -> %s' % (domain, target_node))
             reactor.callFromThread(lambda: allocation.migrateDomain(domain, lv_connection.getHostname(), 
                                                                 target_node, handler))
             # Retry
             return
         else:
             print 'Error - Fatal - Could not retry migration, domain is not active in the infrastructure'
     
     # Trigger defer callback - for successful and unsuccessful migrations
     reactor.callFromThread(lambda: dret.callback(domain_name)) 
def start_and_migrate(domain, target_node):
    '''
    Start domain and migrate it to the target node
    '''
    
    # Get libivrt connection
    _, lv_connection = util.find_domain(domain)
    
    # Lookup libvirt domain object
    lv_domain = lv_connection.lookupByName(domain)
    
    # Launch domain
    print 'Starting domain: %s' % domain
    started = False
    while not started:
        try:
            lv_domain.create()
            started = True
        except:
            print 'ERROR while starting domain'
            print sys.exc_info()[0]
            print 'retrying...'
    
    # Create a deferred to return    
    dret = defer.Deferred()
    
    # Migration callback handler
    def handler(domain_name, node_from, node_to, starsst, end, info, 
                success, error):
        logger.info('Migrated status: %s - %s' % (domain_name, success))
        
        # Migration failed
        if success == False:
            print 'Error - migration failed, checking if domain still exists...'
            _, lv_connection = util.find_domain(domain)
            if lv_connection is not None:
                logger.info('Retrying migration: %s -> %s' % (domain, target_node))
                reactor.callFromThread(lambda: allocation.migrateDomain(domain, lv_connection.getHostname(), 
                                                                    target_node, handler))
                # Retry
                return
            else:
                print 'Error - Fatal - Could not retry migration, domain is not active in the infrastructure'
        
        # Trigger defer callback - for successful and unsuccessful migrations
        reactor.callFromThread(lambda: dret.callback(domain_name)) 
    
    
    # Check if migration is necessary
    if lv_connection.getHostname().find(target_node) == -1:
        # Migration is neccessary
        logger.info('Migrating: %s -> %s from %s' % (domain, target_node, lv_connection.getHostname()))
        allocation.migrateDomain(domain, lv_connection.getHostname(), 
                                 target_node, handler)
    else:
        # Migration is NOT necessary
        logger.info('Skipping: %s -> %s from %s' % (domain, target_node, lv_connection.getHostname()))
        # reactor.callLater(3, lambda: dret.callback(domain))
        return domain
        
    # Return deferred object that will be called after the migration
    return dret
Esempio n. 4
0
def migrateAllocation(migrations):
    """
    Gets a list of migrations. Each list element is a tupel of the structure: 
    (domain name, target node index in the nodes model)
    """

    # trigger migrations
    for migration in migrations:
        # Get domain and target node
        domain_name = migration[0]
        target_node = conf_nodes.get_node_name(migration[1])

        # Search the node which currently holds the domain
        lv_domain, lv_connection_source = util.find_domain(domain_name)
        if lv_domain == None:
            print "WARN: Skipping migration - could not find domain: %s" % (domain_name)
            continue

        # Check if domain is running (parater is an unused flag)
        domain_state = lv_domain.state(0)[0]
        if domain_state != 1:
            # Destroy
            print "(Re)starting the domain %s" % (domain_name)

            # If domain is not shut off
            if domain_state != 5:
                try:
                    lv_domain.destroy()
                except:
                    pass

            # Start domain and wait for it
            try:
                lv_domain.create()
                time.sleep(10)
            except:
                pass

        # Is migration necessary
        source_host = util.get_hostname(lv_connection_source)
        target_host = target_node

        # Skip if migration is not necessesary
        if source_host == target_host:
            print "Skipping migration - identical source and target node %s = %s" % (source_host, target_host)
            continue

        # Check if mig
        try:
            # Get target node connection
            lv_connection_target = util.connections[target_node]

            # Trigger migration without bandwith limitation (last parameter)
            print "Migrating domain: %s -> %s" % (domain_name, target_host)
            lv_domain = lv_domain.migrate(
                lv_connection_target,
                VIR_MIGRATE_LIVE | VIR_MIGRATE_UNDEFINE_SOURCE | VIR_MIGRATE_PERSIST_DEST,
                domain_name,
                None,
                0,
            )
        except:
            print "Skipping - migration failed"
            traceback.print_exc(file=sys.stdout)