|
Packit Service |
a04d08 |
# Copyright (C) 2012 Canonical Ltd.
|
|
Packit Service |
a04d08 |
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
|
|
Packit Service |
a04d08 |
# Copyright (C) 2012 Yahoo! Inc.
|
|
Packit Service |
a04d08 |
#
|
|
Packit Service |
a04d08 |
# Author: Scott Moser <scott.moser@canonical.com>
|
|
Packit Service |
a04d08 |
# Author: Juerg Haefliger <juerg.haefliger@hp.com>
|
|
Packit Service |
a04d08 |
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
|
|
Packit Service |
a04d08 |
#
|
|
Packit Service |
a04d08 |
# This file is part of cloud-init. See LICENSE file for license information.
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
import os
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
from cloudinit import handlers
|
|
Packit Service |
a04d08 |
from cloudinit import log as logging
|
|
Packit Service |
a04d08 |
from cloudinit import util
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
from cloudinit.settings import (PER_ALWAYS)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
LOG = logging.getLogger(__name__)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class BootHookPartHandler(handlers.Handler):
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
# The content prefixes this handler understands.
|
|
Packit Service |
a04d08 |
prefixes = ['#cloud-boothook']
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
def __init__(self, paths, datasource, **_kwargs):
|
|
Packit Service |
a04d08 |
handlers.Handler.__init__(self, PER_ALWAYS)
|
|
Packit Service |
a04d08 |
self.boothook_dir = paths.get_ipath("boothooks")
|
|
Packit Service |
a04d08 |
self.instance_id = None
|
|
Packit Service |
a04d08 |
if datasource:
|
|
Packit Service |
a04d08 |
self.instance_id = datasource.get_instance_id()
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
def _write_part(self, payload, filename):
|
|
Packit Service |
a04d08 |
filename = util.clean_filename(filename)
|
|
Packit Service |
a04d08 |
filepath = os.path.join(self.boothook_dir, filename)
|
|
Packit Service |
a04d08 |
contents = util.strip_prefix_suffix(util.dos2unix(payload),
|
|
Packit Service |
a04d08 |
prefix=self.prefixes[0])
|
|
Packit Service |
a04d08 |
util.write_file(filepath, contents.lstrip(), 0o700)
|
|
Packit Service |
a04d08 |
return filepath
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
def handle_part(self, data, ctype, filename, payload, frequency):
|
|
Packit Service |
a04d08 |
if ctype in handlers.CONTENT_SIGNALS:
|
|
Packit Service |
a04d08 |
return
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
filepath = self._write_part(payload, filename)
|
|
Packit Service |
a04d08 |
try:
|
|
Packit Service |
a04d08 |
env = os.environ.copy()
|
|
Packit Service |
a04d08 |
if self.instance_id is not None:
|
|
Packit Service |
a04d08 |
env['INSTANCE_ID'] = str(self.instance_id)
|
|
Packit Service |
11b429 |
util.subp([filepath], env=env)
|
|
Packit Service |
11b429 |
except util.ProcessExecutionError:
|
|
Packit Service |
a04d08 |
util.logexc(LOG, "Boothooks script %s execution error", filepath)
|
|
Packit Service |
a04d08 |
except Exception:
|
|
Packit Service |
a04d08 |
util.logexc(LOG, "Boothooks unknown error when running %s",
|
|
Packit Service |
a04d08 |
filepath)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
# vi: ts=4 expandtab
|