Skip to content
Permalink
Browse files

Fix for foreman remove_ext_interfaces.

When trying to remove extraneous interfaces from foreman we were
not passing credentials.
Also removed a few foreman parameters settings since no longer
necessary for 1.1.

Fixes: #279
Change-Id: I1a3827f4106eb40c92f4734cef8b83d2cefcd452
  • Loading branch information...
grafuls committed Aug 8, 2019
1 parent ad4d5cc commit ae140772b25a4a05bbd7584ca2ae49488d3277fa
Showing with 59 additions and 25 deletions.
  1. +52 −11 quads/tools/foreman.py
  2. +7 −14 quads/tools/move_and_rebuild_hosts.py
@@ -23,8 +23,7 @@ def get(self, endpoint):
response = requests.get(
self.url + endpoint,
auth=(self.username, self.password),
verify=False

verify=False,
)
except RequestException:
logger.exception("There was something wrong with your request.")
@@ -58,7 +57,8 @@ def put_host_parameter(self, host_id, parameter_id, value):
self.url + endpoint,
json=data,
auth=(self.username, self.password),
verify=False)
verify=False,
)
except RequestException as ex:
logger.debug(ex)
logger.error("There was something wrong with your request.")
@@ -105,17 +105,24 @@ def update_user_password(self, login, password):
return False

def put_element(self, element_name, element_id, param_name, param_value):
logger.debug("PUT param: {%s:%s}" % (param_name, param_value))
params = {
param_name: param_value
}
return self.put_elements(element_name, element_id, params)

def put_elements(self, element_name, element_id, params):
logger.debug("PUT param: %s" % params)
endpoint = "/%s/%s" % (element_name, element_id)
data = {
element_name[:-1]: {param_name: param_value}
element_name[:-1]: params
}
try:
response = requests.put(
self.url + endpoint,
json=data,
auth=(self.username, self.password),
verify=False)
verify=False,
)
except RequestException:
logger.exception("There was something wrong with your request.")
return False
@@ -128,6 +135,36 @@ def put_parameter(self, host_name, name, value):
_host_id = self.get_host_id(host_name)
return self.put_element("hosts", _host_id, name, value)

def put_parameters(self, host_name, params):
logger.debug("PUT param: %s" % params)
_host_id = self.get_host_id(host_name)
return self.put_elements("hosts", _host_id, params)

def put_parameters_by_name(self, host, params):
logger.debug("PUT param: %s" % params)
data = {}
for param in params:
param_name = param.get("name")
param_value = param.get("value")
param_identifier = param.get("identifier", "name")

param_id = None
if param_name == "media":
put_name = "medium"
else:
put_name = param_name[:-1]
endpoint = "/%s" % param_name
result = self.get(endpoint)
for item in result["results"]:
if item.get(param_identifier, None) == param_value:
param_id = item["id"]
break
if param_id:
data["%s_id" % put_name] = param_id
data["%s_name" % put_name] = param_value
success = self.put_parameters(host, data)
return success

def put_parameter_by_name(self, host, name, value, identifier="name"):
logger.debug("PUT param: {%s:%s}" % (name, value))
param_id = None
@@ -142,9 +179,9 @@ def put_parameter_by_name(self, host, name, value, identifier="name"):
param_id = item["id"]
break
if param_id:
self.put_parameter(host, "%s_id" % put_name, param_id)
self.put_parameter(host, "%s_name" % put_name, value)
return True
success = self.put_parameter(host, "%s_id" % put_name, param_id)
success = self.put_parameter(host, "%s_name" % put_name, value) and success
return success
return False

def verify_credentials(self):
@@ -257,9 +294,13 @@ def remove_extraneous_interfaces(self, host):
success = True
extraneous_interfaces = self.get_host_extraneous_interfaces(_host_id)
for interface in extraneous_interfaces:
endpoint = "/hosts/%s/interfaces/%s" % (_host_id, interface["id"])
endpoint = self.url + "/hosts/%s/interfaces/%s" % (_host_id, interface["id"])
try:
response = requests.delete(endpoint)
response = requests.delete(
endpoint,
auth=(self.username, self.password),
verify=False,
)
except RequestException as ex:
logger.debug(ex)
logger.error("There was something wrong with your request.")
@@ -161,21 +161,14 @@ def move_and_rebuild(host, old_cloud, new_cloud, rebuild=False):
badfish.reboot_server()
return False

foreman_success = foreman.remove_extraneous_interfaces(host)

foreman_success = foreman.set_host_parameter(host, "rhel73", "false") and foreman_success
foreman_success = foreman.set_host_parameter(host, "rhel75", "false") and foreman_success
foreman_success = foreman.set_host_parameter(host, "overcloud", "true") and foreman_success
foreman_success = foreman.set_host_parameter(host, "overcloud", "true")
foreman_success = foreman.put_parameter(host, "build", 1) and foreman_success
foreman_success = foreman.put_parameter_by_name(
host, "operatingsystems", conf["foreman_default_os"], "title"
) and foreman_success
foreman_success = foreman.put_parameter_by_name(
host, "ptables", conf["foreman_default_ptable"]
) and foreman_success
foreman_success = foreman.put_parameter_by_name(
host, "media", conf["foreman_default_medium"]
) and foreman_success
params = [
{"name": "operatingsystems", "value": conf["foreman_default_os"], "identifier": "title"},
{"name": "ptables", "value": conf["foreman_default_ptable"]},
{"name": "media", "value": conf["foreman_default_medium"]},
]
foreman_success = foreman.put_parameters_by_name(host, params) and foreman_success
if not foreman_success:
logger.error("There was something wrong setting Foreman host parameters.")

0 comments on commit ae14077

Please sign in to comment.
You can’t perform that action at this time.