Example #1
0
def main():
    foreman = Foreman(conf["foreman_api_url"], conf["foreman_username"],
                      conf["foreman_password"])

    _cloud_response = requests.get(os.path.join(API_URL, "cloud"))
    cloud_list = []
    if _cloud_response.status_code == 200:
        cloud_list = _cloud_response.json()

    _host_response = requests.get(os.path.join(API_URL, "host"))
    host_list = []
    if _host_response.status_code == 200:
        host_list = _host_response.json()

    if not os.path.exists(conf["json_web_path"]):
        os.makedirs(conf["json_web_path"])

    old_jsons = [
        file for file in os.listdir(conf["json_web_path"]) if ".json" in file
    ]
    for file in old_jsons:
        os.remove(os.path.join(conf["json_web_path"], file))

    over_cloud = foreman.get_parametrized(
        "params.%s" % conf["foreman_director_parameter"], "true")

    columns = [
        "macaddress", "ipmi url", "ipmi user", "ipmi password", "ipmi tool"
    ]
    lines = []
    for cloud in cloud_list:
        lines.append(",".join(columns))
        tickets = cloud["ticket"]
        if tickets:
            foreman_password = tickets[0]
        else:
            foreman_password = conf["ipmi_password"]

        for host in host_list:
            is_overcloud = host["name"] in over_cloud.keys()
            if is_overcloud:
                mac = over_cloud[host]["mac"]
                ipmi_url = "mgmt-%s" % host["name"]
                ipmi_username = conf["ipmi_cloud_username"]
                ipmi_tool = "pxe_ipmitool"
                line = ",".join([
                    mac, ipmi_url, ipmi_username, foreman_password, ipmi_tool
                ])
                lines.append(line)

        with NamedTemporaryFile() as ntp:
            for line in lines:
                new_line = "%s\n" % line
                ntp.write(bytes(new_line.encode("utf-8")))
            ntp.seek(0)
            content = csv_to_instack(ntp.name)

            if not os.path.exists(conf["json_web_path"]):
                pathlib.Path(conf["json_web_path"]).mkdir(parents=True,
                                                          exist_ok=True)

            json_file = os.path.join(conf["json_web_path"],
                                     "%s_instackenv.json" % cloud['name'])
            now = datetime.now()
            if os.path.exists(json_file):
                os.rename(
                    json_file,
                    "%s_%s" % (json_file, now.strftime("%Y-%m-%d_%H:%M:%S")))
            with open(json_file, "w+") as _json_file:
                _json_file.seek(0)
                _json_file.write(content)
            os.chmod(json_file, 644)
Example #2
0
        _host.update(build=False, realeased=True, last_build=datetime.now())
        return
    else:
        _host.update(build=False, realeased=True, last_build=datetime.now())
        return


if __name__ == "__main__":
    foreman = Foreman(
        conf["foreman_api_url"],
        conf["foreman_username"],
        conf["foreman_password"]
    )

    # get all overcloud hosts
    overclouds = foreman.get_parametrized("params.%s" % conf["foreman_director_parameter"], "true")
    logger.debug("Overclouds: %s" % overclouds)
    for host in overclouds:
        _quads_host = Host.objects(name=host).first()
        if _quads_host and not _quads_host.nullos:
            build_state = foreman.get_host_build_status(host)
            if build_state:
                _quads_host.update(nullos=True, build=True)

    # get all undercloud hosts
    underclouds = foreman.get_parametrized("params.%s" % conf["foreman_director_parameter"], "false")
    logger.debug("Underclouds: %s" % underclouds)
    for host in underclouds:
        _quads_host = Host.objects(name=host).first()
        if _quads_host and _quads_host.nullos:
            _quads_host.update(nullos=False, build=True)