|
Packit Service |
a04d08 |
# Copyright (C) 2017 Canonical Ltd.
|
|
Packit Service |
a04d08 |
# Copyright (C) 2017-2019 VMware Inc.
|
|
Packit Service |
a04d08 |
#
|
|
Packit Service |
a04d08 |
# Author: Maitreyee Saikia <msaikia@vmware.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 logging
|
|
Packit Service |
a04d08 |
import os
|
|
Packit Service |
a04d08 |
import stat
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
9bfd13 |
from cloudinit import subp
|
|
Packit Service |
a04d08 |
from cloudinit import util
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
LOG = logging.getLogger(__name__)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class CustomScriptNotFound(Exception):
|
|
Packit Service |
a04d08 |
pass
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class CustomScriptConstant(object):
|
|
Packit Service |
a04d08 |
CUSTOM_TMP_DIR = "/root/.customization"
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
# The user defined custom script
|
|
Packit Service |
a04d08 |
CUSTOM_SCRIPT_NAME = "customize.sh"
|
|
Packit Service |
a04d08 |
CUSTOM_SCRIPT = os.path.join(CUSTOM_TMP_DIR,
|
|
Packit Service |
a04d08 |
CUSTOM_SCRIPT_NAME)
|
|
Packit Service |
a04d08 |
POST_CUSTOM_PENDING_MARKER = "/.guest-customization-post-reboot-pending"
|
|
Packit Service |
a04d08 |
# The cc_scripts_per_instance script to launch custom script
|
|
Packit Service |
a04d08 |
POST_CUSTOM_SCRIPT_NAME = "post-customize-guest.sh"
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class RunCustomScript(object):
|
|
Packit Service |
a04d08 |
def __init__(self, scriptname, directory):
|
|
Packit Service |
a04d08 |
self.scriptname = scriptname
|
|
Packit Service |
a04d08 |
self.directory = directory
|
|
Packit Service |
a04d08 |
self.scriptpath = os.path.join(directory, scriptname)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
def prepare_script(self):
|
|
Packit Service |
a04d08 |
if not os.path.exists(self.scriptpath):
|
|
Packit Service |
a04d08 |
raise CustomScriptNotFound("Script %s not found!! "
|
|
Packit Service |
a04d08 |
"Cannot execute custom script!"
|
|
Packit Service |
a04d08 |
% self.scriptpath)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
util.ensure_dir(CustomScriptConstant.CUSTOM_TMP_DIR)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
LOG.debug("Copying custom script to %s",
|
|
Packit Service |
a04d08 |
CustomScriptConstant.CUSTOM_SCRIPT)
|
|
Packit Service |
a04d08 |
util.copy(self.scriptpath, CustomScriptConstant.CUSTOM_SCRIPT)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
# Strip any CR characters from the decoded script
|
|
Packit Service |
a04d08 |
content = util.load_file(
|
|
Packit Service |
a04d08 |
CustomScriptConstant.CUSTOM_SCRIPT).replace("\r", "")
|
|
Packit Service |
a04d08 |
util.write_file(CustomScriptConstant.CUSTOM_SCRIPT,
|
|
Packit Service |
a04d08 |
content,
|
|
Packit Service |
a04d08 |
mode=0o544)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class PreCustomScript(RunCustomScript):
|
|
Packit Service |
a04d08 |
def execute(self):
|
|
Packit Service |
a04d08 |
"""Executing custom script with precustomization argument."""
|
|
Packit Service |
a04d08 |
LOG.debug("Executing pre-customization script")
|
|
Packit Service |
a04d08 |
self.prepare_script()
|
|
Packit Service |
9bfd13 |
subp.subp([CustomScriptConstant.CUSTOM_SCRIPT, "precustomization"])
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
class PostCustomScript(RunCustomScript):
|
|
Packit Service |
a04d08 |
def __init__(self, scriptname, directory, ccScriptsDir):
|
|
Packit Service |
a04d08 |
super(PostCustomScript, self).__init__(scriptname, directory)
|
|
Packit Service |
a04d08 |
self.ccScriptsDir = ccScriptsDir
|
|
Packit Service |
a04d08 |
self.ccScriptPath = os.path.join(
|
|
Packit Service |
a04d08 |
ccScriptsDir,
|
|
Packit Service |
a04d08 |
CustomScriptConstant.POST_CUSTOM_SCRIPT_NAME)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
def execute(self):
|
|
Packit Service |
a04d08 |
"""
|
|
Packit Service |
a04d08 |
This method copy the post customize run script to
|
|
Packit Service |
a04d08 |
cc_scripts_per_instance directory and let this
|
|
Packit Service |
a04d08 |
module to run post custom script.
|
|
Packit Service |
a04d08 |
"""
|
|
Packit Service |
a04d08 |
self.prepare_script()
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
LOG.debug("Copying post customize run script to %s",
|
|
Packit Service |
a04d08 |
self.ccScriptPath)
|
|
Packit Service |
a04d08 |
util.copy(
|
|
Packit Service |
a04d08 |
os.path.join(self.directory,
|
|
Packit Service |
a04d08 |
CustomScriptConstant.POST_CUSTOM_SCRIPT_NAME),
|
|
Packit Service |
a04d08 |
self.ccScriptPath)
|
|
Packit Service |
a04d08 |
st = os.stat(self.ccScriptPath)
|
|
Packit Service |
a04d08 |
os.chmod(self.ccScriptPath, st.st_mode | stat.S_IEXEC)
|
|
Packit Service |
a04d08 |
LOG.info("Creating post customization pending marker")
|
|
Packit Service |
a04d08 |
util.ensure_file(CustomScriptConstant.POST_CUSTOM_PENDING_MARKER)
|
|
Packit Service |
a04d08 |
|
|
Packit Service |
a04d08 |
# vi: ts=4 expandtab
|