Exemple #1
0
def create_message(
    cloud_obj,
    day,
    cloud_info,
    host_list_expire,
):
    template_file = "message"
    cloud = cloud_obj.name
    real_owner = cloud_obj.owner
    ticket = cloud_obj.ticket
    cc = cloud_obj.ccuser

    cc_users = conf["report_cc"].split(",")
    for user in cc:
        cc_users.append("%s@%s" % (user, conf["domain"]))
    with open(os.path.join(TEMPLATES_PATH, template_file)) as _file:
        template = Template(_file.read())
    quads_request_url = conf.get("quads_request_url")
    content = template.render(
        days_to_report=day,
        cloud_info=cloud_info,
        wp_wiki=conf["wp_wiki"],
        quads_request_url=quads_request_url,
        quads_request_deadline_day=conf["quads_request_deadline_day"],
        quads_notify_until_extended=conf["quads_notify_until_extended"],
        cloud=cloud,
        hosts=host_list_expire,
    )
    postman = Postman(
        "QUADS upcoming expiration for %s - %s" % (cloud, ticket),
        real_owner,
        cc_users,
        content,
    )
    postman.send_email()
Exemple #2
0
def get_vlan(cloud_obj, index, last_nic=False):
    if cloud_obj.vlan and last_nic:
        return int(cloud_obj.vlan.vlan_id)
    else:
        vlan_first = int(conf.get("sw_vlan_first", 1100)) - 10
        cloud_offset = int(cloud_obj.name[5:]) * 10
        base_vlan = vlan_first + cloud_offset
        vlan = base_vlan + list(OFFSETS.values())[index * int(cloud_obj.qinq)]
        return vlan
Exemple #3
0
class ModelSearchForm(FlaskForm):
    models_list = conf.get("models").split(",")
    models_choices = []
    for model in models_list:
        models_choices.append((model, model))
    model = SelectMultipleField("Models:", choices=models_choices)
    start = DateField("Start at",
                      format="%m/%d/%Y",
                      validators=[validators.data_required()])
    end = DateField("End at",
                    format="%m/%d/%Y",
                    validators=[validators.data_required()])
Exemple #4
0
    def __init__(self,
                 url,
                 username=None,
                 password=None,
                 semaphore=None,
                 loop=None):
        logger.debug(":Initializing Jira object:")
        self.url = url
        self.username = username
        self.password = password
        if not loop:
            self.loop = asyncio.new_event_loop()
            self.new_loop = True
        else:
            self.loop = loop
            self.new_loop = False
        if not semaphore:
            self.semaphore = asyncio.Semaphore(20)
        else:
            self.semaphore = semaphore

        jira_auth = conf.get("jira_auth")
        if jira_auth and jira_auth == "token":
            token = conf.get("jira_token")
            if not token:
                raise JiraException(
                    "Jira authentication is set to BearerAuth but no "
                    "token has been defined on the configuration file")
            payload = "Bearer: %s" % token
        else:
            if self.username and self.password:
                payload = BasicAuth(self.username, self.password)
            else:
                raise JiraException(
                    "Jira authentication is set to BasicAuth but no "
                    "username or password have been defined")
        self.headers = {"Authorization": payload}
Exemple #5
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.")