summaryrefslogtreecommitdiff
path: root/src/caas/services/icingapollservice.py
blob: d1796d04771716a9e79eb85f1f95cef0191ffa4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from .helpers import Jinja
from .ansibleservice import AnsibleService
from ..models import IcingaBmc
from django.conf import settings
import logging


logger = logging.getLogger("caas")


class IcingaPollService(AnsibleService):
    def __init__(self, target, git, branch, playbook, zmq, url):
        super().__init__("icinga_poll", target, git, branch, playbook, zmq)
        self.url = url

    def deploy(self):
        logger.debug("%s Enter" % self.target)
        try:
            icinga_poll_kwargs = {"ilo_host_address": self.target}
            icinga_poll = IcingaBmc.objects.get(**icinga_poll_kwargs)
        except IcingaBmc.DoesNotExist:
            logger.error("%s FAILED: while running icinga_poll playbook: failed to look up BMC" % self.target)
            return -1

        self.target = icinga_poll.ilo_hostname
        # host_vars file
        hostvars_config = {"bmc_ip": icinga_poll.ilo_host_address,
                           "bmc_username": icinga_poll.bmc_username,
                           "bmc_password": icinga_poll.bmc_password,
                           "icinga_api_url": settings.ICINGA_API_URL,
                           "icinga_api_username": settings.ICINGA_API_USERNAME,
                           "icinga_api_password": settings.ICINGA_API_PASSWORD,
                           "middleware_endpoint": settings.CAAS_MIDDLEWARE_ENDPOINT,
                           "middleware_username": settings.CAAS_MIDDLEWARE_USERNAME,
                           "middleware_password": settings.CAAS_MIDDLEWARE_PASSWORD,
                           "middleware_url": self.url}
        hostvars_content = Jinja.render("icinga_poll_host_vars.j2", **hostvars_config)
        hostvars = self.target
        # inventory file
        inventory_config = {"vendor_name": icinga_poll.vendor_name, "target": self.target}
        inventory_content = Jinja.render("icinga_poll_inventory.j2", **inventory_config)
        inventory = "%s.yaml" % self.target
        logger.debug("%s Sending playbook to queue" % self.target)
        self.run_playbook(inventory, inventory_content, hostvars, hostvars_content)
        logger.debug("%s Sent playbook to queue" % self.target)
        return 0