~oddbloke/cloud-init/lp1506187

« back to all changes in this revision

Viewing changes to cloudinit/sources/DataSourceAzure.py

  • Committer: Daniel Watkins
  • Date: 2015-10-30 16:26:31 UTC
  • Revision ID: daniel.watkins@canonical.com-20151030162631-xmsz2bdgf25zb6d1
Use DMI data to find Azure instance IDs.

This replaces the use of SharedConfig.xml in both the walinuxagent case,
and the case where we communicate with the Azure fabric ourselves.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from cloudinit.settings import PER_ALWAYS
32
32
from cloudinit import sources
33
33
from cloudinit import util
34
 
from cloudinit.sources.helpers.azure import (
35
 
    get_metadata_from_fabric, iid_from_shared_config_content)
 
34
from cloudinit.sources.helpers.azure import get_metadata_from_fabric
36
35
 
37
36
LOG = logging.getLogger(__name__)
38
37
 
41
40
AGENT_START = ['service', 'walinuxagent', 'start']
42
41
BOUNCE_COMMAND = ['sh', '-xc',
43
42
    "i=$interface; x=0; ifdown $i || x=$?; ifup $i || x=$?; exit $x"]
44
 
DATA_DIR_CLEAN_LIST = ['SharedConfig.xml']
45
43
 
46
44
BUILTIN_DS_CONFIG = {
47
45
    'agent_command': AGENT_START,
144
142
                            self.ds_cfg['agent_command'])
145
143
 
146
144
            ddir = self.ds_cfg['data_dir']
147
 
            shcfgxml = os.path.join(ddir, "SharedConfig.xml")
148
 
            wait_for = [shcfgxml]
149
145
 
150
146
            fp_files = []
151
147
            key_value = None
160
156
 
161
157
            missing = util.log_time(logfunc=LOG.debug, msg="waiting for files",
162
158
                                    func=wait_for_files,
163
 
                                    args=(wait_for + fp_files,))
 
159
                                    args=(fp_files,))
164
160
        if len(missing):
165
161
            LOG.warn("Did not find files, but going on: %s", missing)
166
162
 
167
163
        metadata = {}
168
 
        if shcfgxml in missing:
169
 
            LOG.warn("SharedConfig.xml missing, using static instance-id")
170
 
        else:
171
 
            try:
172
 
                metadata['instance-id'] = iid_from_shared_config(shcfgxml)
173
 
            except ValueError as e:
174
 
                LOG.warn("failed to get instance id in %s: %s", shcfgxml, e)
175
 
 
176
164
        metadata['public-keys'] = key_value or pubkeys_from_crt_files(fp_files)
177
165
        return metadata
178
166
 
229
217
        user_ds_cfg = util.get_cfg_by_path(self.cfg, DS_CFG_PATH, {})
230
218
        self.ds_cfg = util.mergemanydict([user_ds_cfg, self.ds_cfg])
231
219
 
232
 
        if found != ddir:
233
 
            cached_ovfenv = util.load_file(
234
 
                os.path.join(ddir, 'ovf-env.xml'), quiet=True, decode=False)
235
 
            if cached_ovfenv != files['ovf-env.xml']:
236
 
                # source was not walinux-agent's datadir, so we have to clean
237
 
                # up so 'wait_for_files' doesn't return early due to stale data
238
 
                cleaned = []
239
 
                for f in [os.path.join(ddir, f) for f in DATA_DIR_CLEAN_LIST]:
240
 
                    if os.path.exists(f):
241
 
                        util.del_file(f)
242
 
                        cleaned.append(f)
243
 
                if cleaned:
244
 
                    LOG.info("removed stale file(s) in '%s': %s",
245
 
                             ddir, str(cleaned))
246
 
 
247
220
        # walinux agent writes files world readable, but expects
248
221
        # the directory to be protected.
249
222
        write_files(ddir, files, dirmode=0o700)
259
232
                     " on Azure.", exc_info=True)
260
233
            return False
261
234
 
 
235
        self.metadata['instance-id'] = util.read_dmi_data('system-uuid')
262
236
        self.metadata.update(fabric_data)
263
237
 
264
238
        found_ephemeral = find_fabric_formatted_ephemeral_disk()
649
623
    return (md, ud, cfg, {'ovf-env.xml': contents})
650
624
 
651
625
 
652
 
def iid_from_shared_config(path):
653
 
    with open(path, "rb") as fp:
654
 
        content = fp.read()
655
 
    return iid_from_shared_config_content(content)
656
 
 
657
 
 
658
626
class BrokenAzureDataSource(Exception):
659
627
    pass
660
628