A.2. Python SDK Example: Hosts


createHost
def createHost(clusterName, hostName, hostAddress, hostPassword):
     """ create host """
     msg = "Installing host '%s' on '%s'"
     LOGGER.info(msg % (hostAddress, clusterName))

     cluster = API.clusters.get(clusterName)
     assert api.clusters.get(
         name="c1").get_gluster_service() is True
     API.hosts.add(params.Host(
             name=hostName,
             address=hostAddress,
             cluster=cluster,
             root_password=hostPassword))
     host = API.hosts.get(hostName)
     assert host is not None

     waitForState(host, states.host.up,
             failStates = states.host.install_failed,
             timeout = config.HOST_INSTALL_TIMEOUT,
             restoreState=states.host.non_operational)
waitForTasks
 def waitForTasks(host, max_times=3, sleep_time=10):
     """
     Max 3(default) times try to deactive host, if there are running tasks
     So try to wait about 30seconds, 3x10s(default)
     Parameters:
      * host - host to be deactivated
      * max_times - max times time try to deactive host
      * sleep_time - time to sleep between tryies
     """
     while max_times > 0:
         try:
             host.deactivate()
             break
         except errors.RequestError as er:
             max_times -= 1
             if max_times == 0:
                 raise er
             sleep(sleep_time)
removeHost
 def removeHost(hostName):
     """ remove Host"""
     host = API.hosts.get(hostName)
     if host is not None:
         LOGGER.info("Deactivating host '%s'" % hostName)

         # Max 3 times try to deactive host, if there are running tasks
         # So try to wait about 30seconds, 3x10s
         waitForTasks(host)
         waitForState(host, states.host.maintenance)

         LOGGER.info("Deleting host")
         host.delete()
         assert updateObject(host) is None, "Failed to remove host"
     else:
         raise errors.RequestError("Unable to see any host")
     #dc = API.datacenters.get(config.MAIN_DC_NAME) ???
     #waitForState(dc, 'up')
activeDeactivateHost
def activeDeactiveHost(hostName):
     """ Active, deactive host """
     LOGGER.info("Activating/deactivating host '%s'" %hostName)
     host = API.hosts.get(hostName)
     waitForTasks(host)

     LOGGER.info("Waiting for maintence")
     host = API.hosts.get(hostName)
     waitForState(host, states.host.maintenance)
     host.activate()
     LOGGER.info("Waiting for 'up' state")
     waitForHostUpState(host)

     # Check DC state
     dc = API.datacenters.get(config.MAIN_DC_NAME)
     waitForState(dc, 'up')
checkHostStatus
def checkHostStatus(hostName):
     """ Check if is status up -> do UP """
     host = API.hosts.get(hostName)
     if host is None:
         LOGGER.info("Host '%s' dont exists." % hostName)
         return
     if host.status.state != states.host.up:
         LOGGER.info("Host '%s' state is '%s'" % (hostName, host.status.state))
         if host.status.state != states.host.maintenance:
             host.deactivate()
             waitForState(host, states.host.maintenance, timeout=180)
         LOGGER.info("Activating")
         host.activate()
         #waitForState(host, states.host.up)
         waitForHostUpState(host)
configureHostNetwork
def configureHostNetwork(hostName):
     """
     Try to change network properties.
     Parameters:
      * hostName - name of host to be changed
     """
     h = getFilterHeader()
     # Deactive host - need to be before configuring network
     loginAsAdmin()
     host = API.hosts.get(hostName)
     waitForTasks(host)
     waitForState(host, states.host.maintenance)

     try:
         host = API.hosts.get(hostName)
         loginAsUser(filter_=h)
         for nic in host.nics.list():
             if nic.status.state == 'up':
                 nic.set_boot_protocol("dhcp")
                 nic.update()
                 break
     except Exception as e:
         raise e
     else:
         pass
     finally:
         # Activate host after test
         loginAsAdmin()
         host = API.hosts.get(hostName)
         host.activate()
         waitForHostUpState(host)
         dc = API.datacenters.get(config.MAIN_DC_NAME)
         waitForState(dc, states.host.up)
         loginAsUser(filter_=h)
waitForHostUpState
maxTry = 3
 def waitForHostUpState(host):
     """
     Wait for host, when its state is up.
     Wait for 3x 240s. Could happend that host don't come up, so try again.
     Parameters:
      * host - host that should be wait for
     """
     try:
         waitForState(host, states.host.up, timeout=240)
     except Exception as e:
         global maxTry
         maxTry -= 1
         if maxTry == 0:
             maxTry = 3
             raise e
         if host.status.state == states.host.non_operational or\
                 host.status.state == states.host.unassigned:
             host.deactivate()
             waitForState(host, states.host.maintenance)
             host.activate()
             waitForHostUpState(host)
22632%2C+Console+Developer+Guide-322-09-2014+17%3A11%3A35Report a bug
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top