seed_random: do not capture command output, provide env RANDOM_SEED_FILE

call the command without capturing output, and provide RANDOM_SEED_FILE
to the environment that it is run in.
This commit is contained in:
Scott Moser 2014-03-04 14:35:29 -05:00
commit f1bca70f8c
2 changed files with 21 additions and 10 deletions

View File

@ -20,6 +20,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import base64 import base64
import os
from StringIO import StringIO from StringIO import StringIO
from cloudinit.settings import PER_INSTANCE from cloudinit.settings import PER_INSTANCE
@ -43,7 +44,7 @@ def _decode(data, encoding=None):
raise IOError("Unknown random_seed encoding: %s" % (encoding)) raise IOError("Unknown random_seed encoding: %s" % (encoding))
def handle_random_seed_command(command, required): def handle_random_seed_command(command, required, env=None):
if not command and required: if not command and required:
raise ValueError("no command found but required=true") raise ValueError("no command found but required=true")
elif not command: elif not command:
@ -57,7 +58,7 @@ def handle_random_seed_command(command, required):
else: else:
LOG.debug("command '%s' not found for seed_command", cmd) LOG.debug("command '%s' not found for seed_command", cmd)
return return
util.subp(command) util.subp(command, env=env, capture=False)
def handle(name, cfg, cloud, log, _args): def handle(name, cfg, cloud, log, _args):
@ -84,7 +85,9 @@ def handle(name, cfg, cloud, log, _args):
command = mycfg.get('command', ['pollinate', '-q']) command = mycfg.get('command', ['pollinate', '-q'])
req = mycfg.get('command_required', False) req = mycfg.get('command_required', False)
try: try:
handle_random_seed_command(command=command, required=req) env = os.environ.copy()
env['RANDOM_SEED_FILE'] = seed_path
handle_random_seed_command(command=command, required=req, env=env)
except ValueError as e: except ValueError as e:
log.warn("handling random command [%s] failed: %s", command, e) log.warn("handling random command [%s] failed: %s", command, e)
raise e raise e

View File

@ -61,8 +61,11 @@ class TestRandomSeed(t_help.TestCase):
def _which(self, program): def _which(self, program):
return self.whichdata.get(program) return self.whichdata.get(program)
def _subp(self, args): def _subp(self, *args, **kwargs):
self.subp_called.append(tuple(args)) # supports subp calling with cmd as args or kwargs
if 'args' not in kwargs:
kwargs['args'] = args[0]
self.subp_called.append(kwargs)
return return
def _compress(self, text): def _compress(self, text):
@ -173,7 +176,8 @@ class TestRandomSeed(t_help.TestCase):
self.whichdata = {'pollinate': '/usr/bin/pollinate'} self.whichdata = {'pollinate': '/usr/bin/pollinate'}
cc_seed_random.handle('test', {}, c, LOG, []) cc_seed_random.handle('test', {}, c, LOG, [])
self.assertEquals(self.subp_called, [('pollinate', '-q')]) subp_args = [f['args'] for f in self.subp_called]
self.assertIn(['pollinate', '-q'], subp_args)
def test_seed_command_not_provided_pollinate_not_available(self): def test_seed_command_not_provided_pollinate_not_available(self):
c = self._get_cloud('ubuntu', {}) c = self._get_cloud('ubuntu', {})
@ -195,15 +199,19 @@ class TestRandomSeed(t_help.TestCase):
cfg = {'random_seed': {'command_required': True, 'command': ['foo']}} cfg = {'random_seed': {'command_required': True, 'command': ['foo']}}
cc_seed_random.handle('test', cfg, c, LOG, []) cc_seed_random.handle('test', cfg, c, LOG, [])
self.assertEquals(self.subp_called, [('foo',)]) self.assertIn(['foo'], [f['args'] for f in self.subp_called])
def test_seed_command_non_default(self): def test_file_in_environment_for_command(self):
c = self._get_cloud('ubuntu', {}) c = self._get_cloud('ubuntu', {})
self.whichdata = {'foo': 'foo'} self.whichdata = {'foo': 'foo'}
cfg = {'random_seed': {'command_required': True, 'command': ['foo']}} cfg = {'random_seed': {'command_required': True, 'command': ['foo'],
'file': self._seed_file}}
cc_seed_random.handle('test', cfg, c, LOG, []) cc_seed_random.handle('test', cfg, c, LOG, [])
self.assertEquals(self.subp_called, [('foo',)]) # this just instists that the first time subp was called,
# RANDOM_SEED_FILE was in the environment set up correctly
subp_env = [f['env'] for f in self.subp_called]
self.assertEqual(subp_env[0].get('RANDOM_SEED_FILE'), self._seed_file)
def apply_patches(patches): def apply_patches(patches):