diff options
Diffstat (limited to 'cloudinit/transforms/cc_bootcmd.py')
-rw-r--r-- | cloudinit/transforms/cc_bootcmd.py | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/cloudinit/transforms/cc_bootcmd.py b/cloudinit/transforms/cc_bootcmd.py index f584da02..a2efad32 100644 --- a/cloudinit/transforms/cc_bootcmd.py +++ b/cloudinit/transforms/cc_bootcmd.py @@ -17,32 +17,36 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import cloudinit.util as util -import subprocess -import tempfile + import os -from cloudinit.CloudConfig import per_always -frequency = per_always +import tempfile + +from cloudinit import util +from cloudinit.settings import PER_ALWAYS +frequency = PER_ALWAYS + + +def handle(name, cfg, cloud, log, _args): -def handle(_name, cfg, cloud, log, _args): if "bootcmd" not in cfg: + log.debug("Skipping module named %s, no 'bootcomd' key in configuration", name) return - try: - content = util.shellify(cfg["bootcmd"]) - tmpf = tempfile.TemporaryFile() - tmpf.write(content) - tmpf.seek(0) - except: - log.warn("failed to shellify bootcmd") - raise - - try: - env = os.environ.copy() - env['INSTANCE_ID'] = cloud.get_instance_id() - subprocess.check_call(['/bin/sh'], env=env, stdin=tmpf) - tmpf.close() - except: - log.warn("failed to run commands from bootcmd") - raise + with tempfile.NamedTemporaryFile(suffix=".sh") as tmpf: + try: + content = util.shellify(cfg["bootcmd"]) + tmpf.write(content) + tmpf.flush() + except: + log.warn("Failed to shellify bootcmd") + raise + + try: + env = os.environ.copy() + env['INSTANCE_ID'] = cloud.get_instance_id() + cmd = ['/bin/sh', tmpf.name] + util.subp(cmd, env=env, capture=False) + except: + log.warn("Failed to run commands from bootcmd") + raise |