Example #1
0
class TestForeman(object):
    def __init__(self):
        self.foreman = None

    def setup(self):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        self.foreman = Foreman(
            conf["foreman_api_url"],
            conf["ipmi_username"],
            conf["ipmi_password"],
            loop=loop,
        )

    def teardown(self):
        self.foreman.loop.close()

    def test_get_all_hosts(self):
        hosts = self.foreman.loop.run_until_complete(
            self.foreman.get_all_hosts())
        assert isinstance(hosts, dict)

    def test_get_broken_hosts(self):
        hosts = self.foreman.loop.run_until_complete(
            self.foreman.get_broken_hosts())
        assert isinstance(hosts, dict)
Example #2
0
def main():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    foreman = Foreman(
        Config["foreman_api_url"],
        Config["foreman_username"],
        Config["foreman_password"],
        loop=loop,
    )
    all_hosts = loop.run_until_complete(foreman.get_all_hosts())

    blacklist = re.compile("|".join(
        [re.escape(word) for word in Config["exclude_hosts"].split("|")]))
    hosts = {}
    for host, properties in all_hosts.items():
        if not blacklist.search(host):
            if properties.get("sp_name", False):
                properties["host_ip"] = properties["ip"]
                properties["host_mac"] = properties["mac"]
                properties["ip"] = properties.get("sp_ip")
                properties["mac"] = properties.get("sp_mac")
                consolidate_ipmi_data(host, "macaddr", properties["host_mac"])
                consolidate_ipmi_data(host, "oobmacaddr",
                                      properties.get("sp_mac"))
                svctag_file = os.path.join(Config["data_dir"], "ipmi", host,
                                           "svctag")
                svctag = ""
                if os.path.exists(svctag_file):
                    with open(svctag_file) as _file:
                        svctag = _file.read()
                properties["svctag"] = svctag.strip()
                hosts[host] = properties

    _full_path = os.path.join(Config["wp_wiki_git_repo_path"], "main.md")

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

    with open(_full_path, "w") as _f:
        _f.seek(0)
        for rack in Config["racks"].split():
            if rack_has_hosts(rack, hosts):
                _f.write(render_header(rack))

                for host, properties in hosts.items():
                    if rack in host:
                        host_obj = Host.objects(name=host).first()
                        if host_obj and not host_obj.retired:
                            _f.write(render_row(host_obj, properties))
                _f.write("\n")

        _f.truncate()
Example #3
0
def main():
    foreman = Foreman(
        conf["foreman_api_url"],
        conf["foreman_username"],
        conf["foreman_password"],
    )

    all_hosts = foreman.get_all_hosts()

    blacklist = re.compile("|".join(
        [re.escape(word) for word in conf["exclude_hosts"].split("|")]))
    hosts = {}
    for host, properties in all_hosts.items():
        if not blacklist.search(host):
            mgmt_host = foreman.get_idrac_host_with_details(host)
            if mgmt_host:
                properties["host_ip"] = properties["ip"]
                properties["host_mac"] = properties["mac"]
                properties["ip"] = mgmt_host["ip"]
                properties["mac"] = mgmt_host["mac"]
                consolidate_ipmi_data(host, "macaddr", properties["host_mac"])
                consolidate_ipmi_data(host, "oobmacaddr", mgmt_host["mac"])
                svctag_file = os.path.join(conf["data_dir"], "ipmi", host,
                                           "svctag")
                svctag = ""
                if os.path.exists(svctag_file):
                    with open(svctag_file) as _file:
                        svctag = _file.read()
                properties["svctag"] = svctag.strip()
                hosts[host] = properties

    _full_path = os.path.join(conf["wp_wiki_git_repo_path"], "main.md")

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

    with open(_full_path, "w") as _f:
        _f.seek(0)
        for rack in conf["racks"].split():
            if rack_has_hosts(rack, hosts):
                _f.write(render_header(rack))

                for host, properties in hosts.items():
                    if rack in host:
                        _f.write(render_row(host, properties))
                _f.write("\n")

        _f.truncate()
Example #4
0
class TestForeman(object):
    def setup(self):
        self.foreman = Foreman(
            conf["foreman_api_url"],
            conf["ipmi_username"],
            conf["ipmi_password"],
        )

    def test_get_all_hosts(self):
        hosts = self.foreman.get_all_hosts()
        assert isinstance(hosts, dict)

    def test_get_broken_hosts(self):
        hosts = self.foreman.get_broken_hosts()
        assert isinstance(hosts, dict)
def main():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    foreman = Foreman(
        conf["foreman_api_url"],
        conf["foreman_username"],
        conf["foreman_password"],
        loop=loop,
    )

    lines = []
    all_hosts = loop.run_until_complete(foreman.get_all_hosts())
    blacklist = re.compile("|".join(
        [re.escape(word) for word in conf["exclude_hosts"].split("|")]))

    broken_hosts = Host.objects(broken=True)
    domain_broken_hosts = [
        host for host in broken_hosts if conf["domain"] in host.name
    ]

    mgmt_hosts = {}
    for host, properties in all_hosts.items():
        if not blacklist.search(host):
            if properties.get("sp_name", False):
                properties["host_ip"] = all_hosts.get(host, {"ip": None})["ip"]
                properties["host_mac"] = all_hosts.get(host,
                                                       {"mac": None})["mac"]
                properties["ip"] = properties.get("sp_ip")
                properties["mac"] = properties.get("sp_mac")
                mgmt_hosts[properties.get("sp_name")] = properties

    lines.append("### **SUMMARY**\n")
    _summary = print_summary()
    lines.extend(_summary)
    details_header = ["\n", "### **DETAILS**\n", "\n"]
    lines.extend(details_header)
    summary_response = requests.get(os.path.join(API_URL, "summary"))
    _cloud_summary = []
    if summary_response.status_code == 200:
        _cloud_summary = summary_response.json()
    for cloud in [cloud for cloud in _cloud_summary if cloud["count"] > 0]:
        name = cloud["name"]
        owner = cloud["owner"]
        lines.append("### <a name=%s></a>\n" % name.strip())
        lines.append(
            "### **%s : %s (%s) -- %s**\n\n" %
            (name.strip(), cloud["count"], cloud["description"], owner))
        lines.extend(print_header())
        _cloud_obj = Cloud.objects(name=name).first()
        _hosts = sorted(
            Host.objects(cloud=_cloud_obj, retired=False, broken=False),
            key=lambda x: x.name,
        )
        for host in _hosts:
            lines.extend(add_row(host))
        lines.append("\n")

    lines.extend(print_unmanaged(mgmt_hosts))
    lines.extend(print_faulty(domain_broken_hosts))

    _full_path = os.path.join(conf["wp_wiki_git_repo_path"], "assignments.md")

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

    with open(_full_path, "w+") as _f:
        _f.seek(0)
        for cloud in lines:
            _line = cloud if cloud else ""
            _f.write(_line)

        _f.truncate()
Example #6
0
def main():
    loop = asyncio.get_event_loop()

    foreman_admin = Foreman(
        conf["foreman_api_url"],
        conf["foreman_username"],
        conf["foreman_password"],
        loop=loop,
    )

    ignore = ["cloud01"]
    foreman_rbac_exclude = conf.get("foreman_rbac_exclude")
    if foreman_rbac_exclude:
        ignore.extend(foreman_rbac_exclude.split("|"))
    clouds = Cloud.objects()
    for cloud in clouds:

        infra_pass = f"{conf['infra_location']}@{cloud.ticket}"
        loop.run_until_complete(
            foreman_admin.update_user_password(cloud.name, infra_pass)
        )

        foreman_cloud_user = Foreman(
            conf["foreman_api_url"],
            cloud.name,
            infra_pass,
            loop=loop,
        )

        if cloud.name not in ignore:
            logger.info(f"Processing {cloud.name}")

            cloud_hosts = loop.run_until_complete(foreman_cloud_user.get_all_hosts())

            user_id = loop.run_until_complete(foreman_admin.get_user_id(cloud.name))
            admin_id = loop.run_until_complete(
                foreman_admin.get_user_id(conf["foreman_username"])
            )

            current_schedule = Schedule.current_schedule(cloud=cloud)
            if current_schedule:

                logger.info(f"  Current Host Permissions:")
                for host, properties in cloud_hosts.items():
                    logger.info(f"    {host}")

                    match = [
                        schedule.host.name
                        for schedule in current_schedule
                        if schedule.host.name == host
                    ]
                    if not match:
                        _host_id = loop.run_until_complete(
                            foreman_admin.get_host_id(host)
                        )
                        loop.run_until_complete(
                            foreman_admin.put_element(
                                "hosts", _host_id, "owner_id", admin_id
                            )
                        )
                        logger.info(f"* Removed permission {host}")

                for schedule in current_schedule:
                    match = [
                        host
                        for host, _ in cloud_hosts.items()
                        if host == schedule.host.name
                    ]
                    if not match:
                        # want to run these separately to avoid ServerDisconnect
                        _host_id = loop.run_until_complete(
                            foreman_admin.get_host_id(schedule.host.name)
                        )
                        loop.run_until_complete(
                            foreman_admin.put_element(
                                "hosts", _host_id, "owner_id", user_id
                            )
                        )
                        logger.info(f"* Added permission {schedule.host.name}")
            else:
                if cloud_hosts:
                    logger.info("  No active schedule, removing pre-existing roles.")
                    for host, properties in cloud_hosts.items():
                        _host_id = loop.run_until_complete(
                            foreman_admin.get_host_id(host)
                        )
                        loop.run_until_complete(
                            foreman_admin.put_element(
                                "hosts", _host_id, "owner_id", admin_id
                            )
                        )
                        logger.info(f"* Removed permission {host}")
                else:
                    logger.info("  No active schedule nor roles assigned.")
Example #7
0
def main():
    foreman = Foreman(conf["foreman_api_url"], conf["foreman_username"],
                      conf["foreman_password"])

    lines = []
    all_hosts = foreman.get_all_hosts()
    blacklist = re.compile("|".join(
        [re.escape(word) for word in conf["exclude_hosts"].split("|")]))

    broken_hosts = foreman.get_broken_hosts()
    domain_broken_hosts = {
        host: properties
        for host, properties in broken_hosts.items() if conf["domain"] in host
    }

    mgmt_hosts = {}
    for host, properties in all_hosts.items():
        if not blacklist.search(host):
            mgmt_host = foreman.get_idrac_host_with_details(host)
            if mgmt_host:
                properties["host_ip"] = all_hosts.get(host, {"ip": None})["ip"]
                properties["host_mac"] = all_hosts.get(host,
                                                       {"mac": None})["mac"]
                properties["ip"] = mgmt_host["ip"]
                properties["mac"] = mgmt_host["mac"]
                mgmt_hosts[mgmt_host["name"]] = properties

    lines.append("### **SUMMARY**\n")
    _summary = print_summary()
    lines.extend(_summary)
    details_header = ["\n", "### **DETAILS**\n", "\n"]
    lines.extend(details_header)
    summary_response = requests.get(os.path.join(API_URL, "summary"))
    _cloud_summary = []
    if summary_response.status_code == 200:
        _cloud_summary = summary_response.json()
    _cloud_hosts = []
    for cloud in [cloud for cloud in _cloud_summary if cloud["count"] > 0]:
        name = cloud["name"]
        owner = cloud["owner"]
        lines.append("### <a name=%s></a>\n" % name.strip())
        lines.append("### **%s -- %s**\n\n" % (name.strip(), owner))
        lines.extend(print_header())
        _cloud_hosts = requests.get(
            os.path.join(API_URL, "host?cloud=%s" % name))
        if _cloud_hosts.status_code == 200:
            for host in _cloud_hosts.json():
                if "cloud" in host:
                    lines.extend(add_row(host))
        lines.append("\n")

    lines.extend(print_unmanaged(mgmt_hosts, domain_broken_hosts))
    lines.extend(print_faulty(domain_broken_hosts))

    _full_path = os.path.join(conf["wp_wiki_git_repo_path"], "assignments.md")

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

    with open(_full_path, "w+") as _f:
        _f.seek(0)
        for cloud in lines:
            _line = cloud if cloud else ""
            _f.write(_line)

        _f.truncate()