def stop_server(auth_client, server_uuid): """Function to stop server""" # Check it's state first, as it is an error to stop it when it is already stopped (or in any other state # apart from running). server_state = get_server_state(auth_client, server_uuid) if (server_state == 'STARTING'): print("Server appears to be starting; waiting until it has completed before stopping") ret = wait_for_server(auth_client, server_uuid, 'RUNNING') if (ret != 0): raise Exception("Server not in RUNNING state, cannot be stopped") server_state = get_server_state(auth_client, server_uuid) if (server_state == 'RUNNING'): change_server_status(auth_client, server_uuid, 'STOPPED') if (server_state == 'NOT_FOUND'): return server_state # Check we actually made it to STOPPED state ret = wait_for_server(auth_client, server_uuid, 'STOPPED') if (ret != 0): raise Exception("Server failed to STOP") server_state = get_server_state(auth_client, server_uuid) return server_state
def start_server(auth_parms, server_data): """Function to start server, uuid in server_data""" server_uuid = server_data[0] server_state = get_server_state(auth_parms, server_uuid) if server_state == 'STOPPED': rc = change_server_status(auth_parms=auth_parms, server_uuid=server_uuid, state='RUNNING') # change_server_status() waits on the server getting to the requested state, so we don't # need to call wait_for_server() here. However, we do (1) need to check the status and (2) # wait on the server actually being accessible (as opposed to having a RUNNING state in # FCO, which really just means that the underlying kvm process has started). # # 1. Check rc (0 is good) if (rc != 0): raise Exception("Failed to put server " + server_uuid + " in to running state") server_resultset = list_resource_by_uuid(auth_parms, uuid=server_uuid, res_type='SERVER') print("Server result set is:") print server_resultset server_ip = server_resultset['list'][0]['nics'][0]['ipAddresses'][0]['ipAddress'] # yuk print("server IP=" + server_ip) # Step 2. Wait on it being accessible. It is possible that the server doesn't have ssh installed, # or it is firewalled, so don't fail here if we can't connect, just carry on and let # the caller deal with any potential issue. The alternative is a hard-coded sleep, or # trying a ping (platform specific and/or root privs needed). is_ssh_port_open(server_ip, 30) server_data.append(server_ip) return server_data
def start_server(auth_parms, server_uuid): """Function to start server, uuid in server_data""" server_state = get_server_state(auth_parms, server_uuid) if server_state == 'STOPPED': rc = change_server_status(auth_parms=auth_parms, server_uuid=server_uuid, state='RUNNING') if (rc != 0): raise Exception("Failed to put server " + server_uuid + " in to running state") server_resultset = list_resource_by_uuid(auth_parms, uuid=server_uuid, res_type='SERVER') server_ip = server_resultset['list'][0]['nics'][0]['ipAddresses'][0]['ipAddress'] is_ssh_port_open(server_ip, 30)
def StartVM(customerUUID, customerUsername, customerPassword, serverUUID, isVerbose=False): auth_client = api_session(customerUsername, customerUUID, customerPassword) server_state = get_server_state(auth_client, serverUUID) if (server_state == 'RUNNING'): print "Server is already running" return if (server_state == 'STOPPED' or server_state == 'STOPPING'): start_server(auth_client, serverUUID) print "Server is now RUNNING " else: print "Server could not be started because it is - %s " %server_state