3
3
# Copyright (C) 2013 Yahoo! Inc.
4
# Copyright (C) 2014 Canonical, Ltd
5
6
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
# Author: Dustin Kirkland <kirkland@ubuntu.com>
8
# Author: Scott Moser <scott.moser@canonical.com>
7
10
# This program is free software: you can redistribute it and/or modify
8
11
# it under the terms of the GNU General Public License version 3, as
20
23
from StringIO import StringIO
22
25
from cloudinit.settings import PER_INSTANCE
26
from cloudinit import log as logging
23
27
from cloudinit import util
25
29
frequency = PER_INSTANCE
30
LOG = logging.getLogger(__name__)
28
33
def _decode(data, encoding=None):
38
43
raise IOError("Unknown random_seed encoding: %s" % (encoding))
46
def handle_random_seed_command(command, required):
47
if not command and required:
48
raise ValueError("no command found but required=true")
50
LOG.debug("no command provided")
54
if not util.which(cmd):
56
raise ValueError("command '%s' not found but required=true", cmd)
58
LOG.debug("command '%s' not found for seed_command", cmd)
41
63
def handle(name, cfg, cloud, log, _args):
42
if not cfg or "random_seed" not in cfg:
43
log.debug(("Skipping module named %s, "
44
"no 'random_seed' configuration found"), name)
64
mycfg = cfg.get('random_seed', {})
65
seed_path = mycfg.get('file', '/dev/urandom')
66
seed_data = mycfg.get('data', '')
47
my_cfg = cfg['random_seed']
48
seed_path = my_cfg.get('file', '/dev/urandom')
49
68
seed_buf = StringIO()
50
seed_buf.write(_decode(my_cfg.get('data', ''),
51
encoding=my_cfg.get('encoding')))
70
seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding')))
72
# 'random_seed' is set up by Azure datasource, and comes already in
73
# openstack meta_data.json
53
74
metadata = cloud.datasource.metadata
54
75
if metadata and 'random_seed' in metadata:
55
76
seed_buf.write(metadata['random_seed'])
57
78
seed_data = seed_buf.getvalue()
59
log.debug("%s: adding %s bytes of random seed entrophy to %s", name,
80
log.debug("%s: adding %s bytes of random seed entropy to %s", name,
60
81
len(seed_data), seed_path)
61
82
util.append_file(seed_path, seed_data)
84
command = mycfg.get('command', ['pollinate', '-q'])
85
req = mycfg.get('command_required', False)
87
handle_random_seed_command(command=command, required=req)
88
except ValueError as e:
89
log.warn("handling random command [%s] failed: %s", command, e)