File: unattended-upgrade-shutdown

package info (click to toggle)
unattended-upgrades 0.62.2%2Bsqueeze1
  • links: PTS
  • area: main
  • in suites: squeeze-lts
  • size: 360 kB
  • ctags: 56
  • sloc: python: 569; sh: 81; makefile: 79
file content (98 lines) | stat: -rwxr-xr-x 3,414 bytes parent folder | download | duplicates (2)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
# Copyright (c) 2009 Canonical Ltd
#
# AUTHOR:
# Michael Vogt <mvo@ubuntu.com>
#
# unattended-upgrade-shutdown - helper that checks if a 
# unattended-upgrade is in progress and waits until it exists
#
# This file is part of unattended-upgrades 
#
# unattended-upgrades is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# unattended-upgrades is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with unattended-upgrades; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import apt_pkg
import time
import sys
import logging
import logging.handlers
import gettext
import subprocess
import os.path

from optparse import OptionParser
from gettext import gettext as _

def do_usplash(msg):
    if os.path.exists("/sbin/usplash_write"):
        logging.debug("Running usplash_write")
        subprocess.call(["/sbin/usplash_write","TEXT", msg])
        subprocess.call(["/sbin/usplash_write","PULSATE"])

def do_plymouth(msg):
    if os.path.exists("/bin/plymouth"):
        logging.debug("Running plymouth --text")
        subprocess.call(["/bin/plymouth","message", "--text", msg])

if __name__ == "__main__":
    # setup gettext
    localesApp="unattended-upgrades"
    localesDir="/usr/share/locale"
    gettext.bindtextdomain(localesApp, localesDir)
    gettext.textdomain(localesApp)

    parser = OptionParser()
    parser.add_option("", "--debug",
                      action="store_true", dest="debug", default=False,
                      help="print debug messages")
    parser.add_option("", "--delay", default=10,
                      help="delay in minutes to wait for unattended-upgrades")
    parser.add_option("", "--lock-file", 
                      default="/var/run/unattended-upgrades.lock",
                      help="lock file location")
    (options, args) = parser.parse_args()
    
    # setup logging
    level = logging.INFO
    if options.debug:
        level = logging.DEBUG
    logging.basicConfig(level=level, format="%(levelname)s - %(message)s")
    logger = logging.getLogger()
    try:
        logger.addHandler(logging.handlers.SysLogHandler(
                "/dev/log", logging.handlers.SysLogHandler.LOG_DAEMON))
    except Exception, e:
        logging.debug("failed to setup syslog logger: %s" % e)

    # run
    start_time = time.time()
    while True: 
        res = apt_pkg.get_lock(options.lock_file)
        logging.debug("GetLock returned %i" % res)
        if res > 0:
            logging.debug("Lock not taken")
            sys.exit(0)
	# wait a some seconds and try again
        msg = _("Unattended-upgrade in progress during shutdown, "
                "sleeping for 5s")
        logging.warning(msg)
        do_plymouth(msg)
        do_usplash(msg)
	time.sleep(5)
	if (time.time() - start_time) > options.delay*60:
            logging.warning(_("Giving up on lockfile after %s delay") % options.delay)
            sys.exit(1)
    sys.exit(0)