~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to cloudinit/sources/DataSourceConfigDrive.py

  • Committer: Scott Moser
  • Date: 2013-09-11 21:04:19 UTC
  • mfrom: (1.4.5)
  • Revision ID: smoser@ubuntu.com-20130911210419-3vt5ze6ph3hu8dz1
* New upstream snapshot.
  * Add OpenNebula datasource.
  * Support reading 'random_seed' from metadata and writing to /dev/urandom
  * fix for bug in log_time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#    You should have received a copy of the GNU General Public License
19
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
 
21
import base64
21
22
import json
22
23
import os
23
24
 
41
42
VALID_DSMODES = ("local", "net", "pass", "disabled")
42
43
 
43
44
 
 
45
class ConfigDriveHelper(object):
 
46
    def __init__(self, distro):
 
47
        self.distro = distro
 
48
 
 
49
    def on_first_boot(self, data):
 
50
        if not data:
 
51
            data = {}
 
52
        if 'network_config' in data:
 
53
            LOG.debug("Updating network interfaces from config drive")
 
54
            self.distro.apply_network(data['network_config'])
 
55
        files = data.get('files')
 
56
        if files:
 
57
            LOG.debug("Writing %s injected files", len(files))
 
58
            try:
 
59
                write_files(files)
 
60
            except IOError:
 
61
                util.logexc(LOG, "Failed writing files")
 
62
 
 
63
 
44
64
class DataSourceConfigDrive(sources.DataSource):
45
65
    def __init__(self, sys_cfg, distro, paths):
46
66
        sources.DataSource.__init__(self, sys_cfg, distro, paths)
49
69
        self.seed_dir = os.path.join(paths.seed_dir, 'config_drive')
50
70
        self.version = None
51
71
        self.ec2_metadata = None
 
72
        self.helper = ConfigDriveHelper(distro)
52
73
 
53
74
    def __str__(self):
54
75
        root = sources.DataSource.__str__(self)
187
208
        # instance-id
188
209
        prev_iid = get_previous_iid(self.paths)
189
210
        cur_iid = md['instance-id']
190
 
 
191
 
        if ('network_config' in results and self.dsmode == "local" and
192
 
            prev_iid != cur_iid):
193
 
            LOG.debug("Updating network interfaces from config drive (%s)",
194
 
                      dsmode)
195
 
            self.distro.apply_network(results['network_config'])
196
 
 
197
 
        # file writing occurs in local mode (to be as early as possible)
198
 
        if self.dsmode == "local" and prev_iid != cur_iid and results['files']:
199
 
            LOG.debug("writing injected files")
200
 
            try:
201
 
                write_files(results['files'])
202
 
            except:
203
 
                util.logexc(LOG, "Failed writing files")
 
211
        if prev_iid != cur_iid and self.dsmode == "local":
 
212
            self.helper.on_first_boot(results)
204
213
 
205
214
        # dsmode != self.dsmode here if:
206
215
        #  * dsmode = "pass",  pass means it should only copy files and then
338
347
        except KeyError:
339
348
            raise BrokenConfigDriveDir("No uuid entry in metadata")
340
349
 
 
350
    if 'random_seed' in results['metadata']:
 
351
        random_seed = results['metadata']['random_seed']
 
352
        try:
 
353
            results['metadata']['random_seed'] = base64.b64decode(random_seed)
 
354
        except (ValueError, TypeError) as exc:
 
355
            raise BrokenConfigDriveDir("Badly formatted random_seed: %s" % exc)
 
356
 
341
357
    def read_content_path(item):
342
358
        # do not use os.path.join here, as content_path starts with /
343
359
        cpath = os.path.sep.join((source_dir, "openstack",