Beispiel #1
0
def check_tags(api, options):
    """Checks if required tags have been already defined and creates them if missing"""
    if options.verbosity >= 1:
        print "Looking for tags prior to start..."

    if not api.tags.get(name="elas_manage"):
        if options.verbosity >= 2:
            print "Creating tag elas_manage..."
        api.tags.add(params.Tag(name="elas_manage"))

    if not api.tags.get(name="elas_start"):
        if options.verbosity >= 2:
            print "Creating tag elas_start..."
        api.tags.add(params.Tag(name="elas_start"))

    return
Beispiel #2
0
def deactivate_host(target):
    """Deactivates hosts putting it on maintenance and associating required tags"""
    host = api.hosts.get(id=target)
    # Shutting down one host at a time...
    if options.verbosity > 0:
        print "Shutting down target %s" % target

    #Add elas_maint TAG to host
    host.tags.add(params.Tag(name="elas_maint"))

    #Set host on maintenance
    try:
        host.deactivate()
    except:
        print "Error deactivating host %s" % api.hosts.get(id=target).name

    #Get host IP
    ip = host.address

    #Should wait until host state is 'maintenance'

    i = 0
    while i < 30:
        if api.hosts.get(id=target).status.state == "maintenance":
            if options.verbosity > 6:
                print "Host is now on maintenance..."
            i = 30
        else:
            if options.verbosity > 6:
                print "Host still not on maintenance... sleeping"
            time.sleep(2)
        i = i + 1

    if api.hosts.get(id=target).status.state == "maintenance":
        #Execute power action
        ## /etc/pki/rhevm/keys/rhevm_id_rsa
        if options.verbosity >= 1:
            print "Sending %s the power action %s" % (host, options.action)

        comando = "/usr/bin/ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=10 -i /etc/pki/ovirt-engine/keys/engine_id_rsa root@%s %s " % (
            ip, options.action)

        os.system(comando)
        os.system(comando)
        os.system(comando)
    return
def check_tags(api, options):
    """Checks if required tags have been already defined and creates them if missing

    @param api: points to API object to reuse access
    @param options: points to options object to reuse values provided on parent
    """
    if options.verbosity >= 1:
        print("Looking for tags prior to start...")

    tags = "elas_maint elas_manage elas_start elas_upgrade "

    for tag in tags:
        if not api.tags.get(name=tag):
            if options.verbosity >= 2:
                print "Creating tag %s..." % tag
            api.tags.add(params.Tag(name=tag))

    return
Beispiel #4
0
################################ MAIN PROGRAM ############################
if __name__ == "__main__":
    #Check if we have defined needed tags and create them if missing
    check_tags(api, options)

    # TAGALL?
    #Add elas_maint TAG to every single host to automate the management
    if options.tagall == 1:

        if options.verbosity >= 1:
            print "Tagging all hosts with elas_manage"

        for host in listhosts(api):
            try:
                host.tags.add(params.Tag(name="elas_manage"))
            except:
                print "Error adding elas_manage tag to host %s" % host.name

    #Sanity checks
    ## Check hosts with elas_maint tag and status active
    query = "status = up"
    for host in listhosts(api, query):
        if host.status.state == "up":
            if api.hosts.get(id=host.id).tags.get(name="elas_maint"):
                if options.verbosity >= 1:
                    print "Host %s is tagged as elas_maint and it's active, removing tag..." % host.id
                api.hosts.get(id=host.id).tags.get(name="elas_maint").delete()

    if not options.cluster:
        # Processing each cluster of our RHEVM
def upgrade_host(target):
    """Deactivates hosts putting it on maintenance and associating required tags before upgrading"""
    host = api.hosts.get(id=target)
    # Shutting down one host at a time...
    if options.verbosity > 0:
        print "Preparing target %s" % target

    #Add elas_maint TAG to host
    host.tags.add(params.Tag(name="elas_upgrade"))

    #Set host on maintenance
    try:
        host.deactivate()
    except:
        print "Error deactivating host %s" % api.hosts.get(id=target).name

    #Get host IP
    ip = host.address

    #Should wait until host state is 'maintenance'

    i = 0
    while i < 30:
        if api.hosts.get(id=target).status.state == "maintenance":
            if options.verbosity > 6:
                print "Host is now on maintenance..."
            i = 30
        else:
            if options.verbosity > 6:
                print "Host still not on maintenance... sleeping"
            time.sleep(2)
        i = i + 1

    if api.hosts.get(id=target).status.state == "maintenance":
        #Execute upgrade for host with latest available version
        image = "%s" % get_max_version()
        try:
            print "Trying to upgrade %s" % host.name
            host.install(params.Action(image=image))
        except:
            print "Host failed to install"

        i = 0
        while i < options.delay:
            if api.hosts.get(id=target).status.state == "up":
                if options.verbosity > 6:
                    print "Host is now up again..."
                i = options.delay
            else:
                if options.verbosity > 6:
                    print "Host still not up... sleeping"
                time.sleep(1)
            i = i + 1

        # if host is listed as installation failed, enable it as this is a pending BZ, and probably host is OK
        if api.hosts.get(id=target).status.state == "install_failed":
            api.hosts.get(id=target).activate()

        # Remove tags no longer used
        api.hosts.get(id=host.id).tags.get(name="elas_upgrade").delete()
        api.hosts.get(id=host.id).tags.get(name="elas_maint").delete()

    return