3
# Copyright (C) 2012 Canonical Ltd.
4
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
5
# Copyright (C) 2012 Yahoo! Inc.
7
# Author: Scott Moser <scott.moser@canonical.com>
8
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
9
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License version 3, as
13
# published by the Free Software Foundation.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with this program. If not, see <http://www.gnu.org/licenses/>.
27
from cloudinit import handlers
28
from cloudinit import log as logging
29
from cloudinit import util
31
from cloudinit.settings import (PER_INSTANCE)
33
LOG = logging.getLogger(__name__)
34
UPSTART_PREFIX = "#upstart-job"
37
class UpstartJobPartHandler(handlers.Handler):
38
def __init__(self, paths, **_kwargs):
39
handlers.Handler.__init__(self, PER_INSTANCE)
40
self.upstart_dir = paths.upstart_conf_d
44
handlers.type_from_starts_with(UPSTART_PREFIX),
47
def handle_part(self, data, ctype, filename, payload, frequency):
48
if ctype in handlers.CONTENT_SIGNALS:
51
# See: https://bugs.launchpad.net/bugs/819507
52
if frequency != PER_INSTANCE:
55
if not self.upstart_dir:
58
filename = util.clean_filename(filename)
59
(_name, ext) = os.path.splitext(filename)
64
filename = filename + ".conf"
66
payload = util.dos2unix(payload)
67
path = os.path.join(self.upstart_dir, filename)
68
util.write_file(path, payload, 0o644)
71
util.subp(["initctl", "reload-configuration"], capture=False)
74
def _has_suitable_upstart():
76
# a bug in upstart means that invoking reload-configuration
77
# at this stage in boot causes havoc. So, try to determine if upstart
78
# is installed, and reloading configuration is OK.
79
if not os.path.exists("/sbin/initctl"):
82
(version_out, _err) = util.subp(["initctl", "version"])
84
util.logexc(LOG, "initctl version failed")
87
# expecting 'initctl version' to output something like: init (upstart X.Y)
88
if re.match("upstart 1.[0-7][)]", version_out):
90
if "upstart 0." in version_out:
92
elif "upstart 1.8" in version_out:
93
if not os.path.exists("/usr/bin/dpkg-query"):
96
(dpkg_ver, _err) = util.subp(["dpkg-query",
97
"--showformat=${Version}",
98
"--show", "upstart"], rcs=[0, 1])
100
util.logexc(LOG, "dpkg-query failed")
104
good = "1.8-0ubuntu1.2"
105
util.subp(["dpkg", "--compare-versions", dpkg_ver, "ge", good])
107
except util.ProcessExecutionError as e:
111
util.logexc(LOG, "dpkg --compare-versions failed [%s]",
113
except Exception as e:
114
util.logexc(LOG, "dpkg --compare-versions failed")
119
SUITABLE_UPSTART = _has_suitable_upstart()