From 778d2015ec49170ff4525b63903d7a656ad44b2e Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 3 Mar 2014 15:01:18 -0500 Subject: cc_seed_random: fix bug and support pollinate command there was a bug that prevented seeding of /dev/urandom from metadata provided by the datasource unless the user provided random_seed config. This should, instead, be the default behavior. --- cloudinit/config/cc_seed_random.py | 50 +++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_seed_random.py b/cloudinit/config/cc_seed_random.py index 22a31f29..56c19ad5 100644 --- a/cloudinit/config/cc_seed_random.py +++ b/cloudinit/config/cc_seed_random.py @@ -1,8 +1,11 @@ # vi: ts=4 expandtab # # Copyright (C) 2013 Yahoo! Inc. +# Copyright (C) 2014 Canonical, Ltd # # Author: Joshua Harlow +# Author: Dustin Kirkland +# Author: Scott Moser # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, as @@ -17,12 +20,15 @@ # along with this program. If not, see . import base64 +import os from StringIO import StringIO from cloudinit.settings import PER_INSTANCE +from cloudinit import log as logging from cloudinit import util frequency = PER_INSTANCE +LOG = logging.getLogger(__name__) def _decode(data, encoding=None): @@ -38,24 +44,50 @@ def _decode(data, encoding=None): raise IOError("Unknown random_seed encoding: %s" % (encoding)) -def handle(name, cfg, cloud, log, _args): - if not cfg or "random_seed" not in cfg: - log.debug(("Skipping module named %s, " - "no 'random_seed' configuration found"), name) +def handle_random_seed_command(command, required, env=None): + if not command and required: + raise ValueError("no command found but required=true") + elif not command: + LOG.debug("no command provided") return - my_cfg = cfg['random_seed'] - seed_path = my_cfg.get('file', '/dev/urandom') + cmd = command[0] + if not util.which(cmd): + if required: + raise ValueError("command '%s' not found but required=true", cmd) + else: + LOG.debug("command '%s' not found for seed_command", cmd) + return + util.subp(command, env=env) + + +def handle(name, cfg, cloud, log, _args): + mycfg = cfg.get('random_seed', {}) + seed_path = mycfg.get('file', '/dev/urandom') + seed_data = mycfg.get('data', '') + seed_buf = StringIO() - seed_buf.write(_decode(my_cfg.get('data', ''), - encoding=my_cfg.get('encoding'))) + if seed_data: + seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding'))) + # 'random_seed' is set up by Azure datasource, and comes already in + # openstack meta_data.json metadata = cloud.datasource.metadata if metadata and 'random_seed' in metadata: seed_buf.write(metadata['random_seed']) seed_data = seed_buf.getvalue() if len(seed_data): - log.debug("%s: adding %s bytes of random seed entrophy to %s", name, + log.debug("%s: adding %s bytes of random seed entropy to %s", name, len(seed_data), seed_path) util.append_file(seed_path, seed_data) + + command = mycfg.get('command', ['pollinate', '-q']) + req = mycfg.get('command_required', False) + try: + env = os.environ.copy() + env['RANDOM_SEED_FILE'] = seed_path + handle_random_seed_command(command=command, required=req, env=env) + except ValueError as e: + log.warn("handling random command [%s] failed: %s", command, e) + raise e -- cgit v1.2.3 From d7b79b1c5703a9fc4d533d15efa5fdb1f4f8352b Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 3 Mar 2014 16:33:11 -0500 Subject: allow random command's output to go through by default we call 'pollinate -q' which is nice and quiet. if the user wants to be noisy, let them. --- cloudinit/config/cc_seed_random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_seed_random.py b/cloudinit/config/cc_seed_random.py index 56c19ad5..49a6b3e8 100644 --- a/cloudinit/config/cc_seed_random.py +++ b/cloudinit/config/cc_seed_random.py @@ -58,7 +58,7 @@ def handle_random_seed_command(command, required, env=None): else: LOG.debug("command '%s' not found for seed_command", cmd) return - util.subp(command, env=env) + util.subp(command, env=env, capture=False) def handle(name, cfg, cloud, log, _args): -- cgit v1.2.3 From d9661a8ef4c6003ef48757715965ebb5c071c80b Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 12 Mar 2014 10:59:13 -0400 Subject: final_message: allow replacement of capital name keys. documentation of final_message in doc/examples/cloud-config-final-message.txt showed '$UPTIME' and '$TIMESTAMP' would be available, but only the lower case versions of these strings were available. This change just makes all lower case and upper case keys available here to avoid breaking anyone who used the functional-but-not-correctly-documented lower case names. LP: #1286164 --- cloudinit/config/cc_final_message.py | 1 + 1 file changed, 1 insertion(+) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_final_message.py b/cloudinit/config/cc_final_message.py index e92cba4a..b24294e4 100644 --- a/cloudinit/config/cc_final_message.py +++ b/cloudinit/config/cc_final_message.py @@ -53,6 +53,7 @@ def handle(_name, cfg, cloud, log, args): 'version': cver, 'datasource': str(cloud.datasource), } + subs.update(dict([(k.upper(), v) for k, v in subs.items()])) util.multi_log("%s\n" % (templater.render_string(msg_in, subs)), console=False, stderr=True, log=log) except Exception: -- cgit v1.2.3 From 5d9726742c22f4c80ce2f386d09c1bbcf4b67164 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 1 Apr 2014 14:20:57 -0400 Subject: pyflakes cleanups --- cloudinit/config/cc_power_state_change.py | 1 - tests/unittests/test__init__.py | 6 +----- tests/unittests/test_datasource/test_maas.py | 1 - tests/unittests/test_datasource/test_smartos.py | 3 --- tests/unittests/test_handler/test_handler_yum_add_repo.py | 1 - 5 files changed, 1 insertion(+), 11 deletions(-) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_power_state_change.py b/cloudinit/config/cc_power_state_change.py index 561c5abd..8f99e887 100644 --- a/cloudinit/config/cc_power_state_change.py +++ b/cloudinit/config/cc_power_state_change.py @@ -22,7 +22,6 @@ from cloudinit import util import errno import os import re -import signal import subprocess import time diff --git a/tests/unittests/test__init__.py b/tests/unittests/test__init__.py index 8c41c1ca..03065c8b 100644 --- a/tests/unittests/test__init__.py +++ b/tests/unittests/test__init__.py @@ -1,14 +1,10 @@ -import logging import os -import StringIO -import sys -from mocker import MockerTestCase, ANY, ARGS, KWARGS +from mocker import MockerTestCase, ARGS, KWARGS from cloudinit import handlers from cloudinit import helpers from cloudinit import importer -from cloudinit import log from cloudinit import settings from cloudinit import url_helper from cloudinit import util diff --git a/tests/unittests/test_datasource/test_maas.py b/tests/unittests/test_datasource/test_maas.py index bd5d23fd..73cfadcb 100644 --- a/tests/unittests/test_datasource/test_maas.py +++ b/tests/unittests/test_datasource/test_maas.py @@ -3,7 +3,6 @@ import os from cloudinit.sources import DataSourceMAAS from cloudinit import url_helper -from cloudinit import util from tests.unittests.helpers import populate_dir import mocker diff --git a/tests/unittests/test_datasource/test_smartos.py b/tests/unittests/test_datasource/test_smartos.py index 8f9fa27d..45f1708a 100644 --- a/tests/unittests/test_datasource/test_smartos.py +++ b/tests/unittests/test_datasource/test_smartos.py @@ -24,10 +24,7 @@ import base64 from cloudinit import helpers as c_helpers -from cloudinit import stages -from cloudinit import util from cloudinit.sources import DataSourceSmartOS -from cloudinit.settings import (PER_INSTANCE) from tests.unittests import helpers import os import os.path diff --git a/tests/unittests/test_handler/test_handler_yum_add_repo.py b/tests/unittests/test_handler/test_handler_yum_add_repo.py index 8df592f9..7c6f7c40 100644 --- a/tests/unittests/test_handler/test_handler_yum_add_repo.py +++ b/tests/unittests/test_handler/test_handler_yum_add_repo.py @@ -1,4 +1,3 @@ -from cloudinit import helpers from cloudinit import util from cloudinit.config import cc_yum_add_repo -- cgit v1.2.3