~free.ekanayaka/landscape-client/lucid-1.5.4-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/monitor/rebootrequired.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import logging
3
3
 
4
 
from landscape.monitor.monitor import MonitorPlugin
 
4
from landscape.lib.fs import read_file
 
5
from landscape.monitor.plugin import MonitorPlugin
5
6
 
6
7
 
7
8
class RebootRequired(MonitorPlugin):
8
9
    """
9
10
    Report whether the system requires a reboot.
 
11
 
 
12
    @param reboot_required_filename: The path to the flag file that indicates
 
13
        if the system needs to be rebooted.
10
14
    """
11
15
 
12
16
    persist_name = "reboot-required"
13
17
    run_interval = 900 # 15 minutes
 
18
    run_immediately = True
14
19
 
15
20
    def __init__(self, reboot_required_filename="/var/run/reboot-required"):
16
 
        self._reboot_required_filename = reboot_required_filename
 
21
        self._flag_filename = reboot_required_filename
 
22
        self._packages_filename = reboot_required_filename + ".pkgs"
17
23
 
18
 
    def _check_reboot_required(self):
 
24
    def _get_flag(self):
19
25
        """Return a boolean indicating whether the computer needs a reboot."""
20
 
        return os.path.exists(self._reboot_required_filename)
 
26
        return os.path.exists(self._flag_filename)
 
27
 
 
28
    def _get_packages(self):
 
29
        """Return the list of packages that required a reboot, if any."""
 
30
        if not os.path.exists(self._packages_filename):
 
31
            return []
 
32
 
 
33
        lines = read_file(self._packages_filename).splitlines()
 
34
        packages = set(line.strip().decode("utf-8") for line in lines if line)
 
35
        return sorted(packages)
21
36
 
22
37
    def _create_message(self):
23
38
        """Return the body of the reboot-required message to be sent."""
24
39
        message = {}
25
 
        key = "flag"
26
 
        value = self._check_reboot_required()
27
 
        if value != self._persist.get(key):
 
40
        flag = self._get_flag()
 
41
        packages = self._get_packages()
 
42
        for key, value in [("flag", flag), ("packages", packages)]:
 
43
            if value == self._persist.get(key):
 
44
                continue
28
45
            self._persist.set(key, value)
29
46
            message[key] = value
30
47
        return message
37
54
        """
38
55
        message = self._create_message()
39
56
        if message:
40
 
            message["type"] = "reboot-required"
 
57
            message["type"] = "reboot-required-info"
41
58
            logging.info("Queueing message with updated "
42
59
                         "reboot-required status.")
43
60
            self.registry.broker.send_message(message, urgent=True)
45
62
    def run(self):
46
63
        """Send reboot-required messages if the server accepts them."""
47
64
        return self.registry.broker.call_if_accepted(
48
 
            "reboot-required", self.send_message)
 
65
            "reboot-required-info", self.send_message)