3
# Copyright (C) 2013 Yahoo! Inc.
5
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
from StringIO import StringIO
22
from cloudinit.settings import PER_INSTANCE
23
from cloudinit import util
25
frequency = PER_INSTANCE
28
def _decode(data, encoding=None):
31
if not encoding or encoding.lower() in ['raw']:
33
elif encoding.lower() in ['base64', 'b64']:
34
return base64.b64decode(data)
35
elif encoding.lower() in ['gzip', 'gz']:
36
return util.decomp_gzip(data, quiet=False)
38
raise IOError("Unknown random_seed encoding: %s" % (encoding))
41
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)
47
my_cfg = cfg['random_seed']
48
seed_path = my_cfg.get('file', '/dev/urandom')
50
seed_buf.write(_decode(my_cfg.get('data', ''),
51
encoding=my_cfg.get('encoding')))
53
metadata = cloud.datasource.metadata
54
if metadata and 'random_seed' in metadata:
55
seed_buf.write(metadata['random_seed'])
57
seed_data = seed_buf.getvalue()
59
log.debug("%s: adding %s bytes of random seed entrophy to %s", name,
60
len(seed_data), seed_path)
61
util.append_file(seed_path, seed_data)