Ce contenu n'est pas disponible dans la langue sélectionnée.
A.2. Python SDK Example: Hosts
createHost
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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)
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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)
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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')
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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')
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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)
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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)
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
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
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)
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