~oddbloke/cloud-init/lp1506187

« back to all changes in this revision

Viewing changes to cloudinit/sources/DataSourceAzure.py

  • Committer: Daniel Watkins
  • Date: 2015-10-20 09:36:04 UTC
  • mfrom: (1149.1.1 cloud-init)
  • Revision ID: daniel.watkins@canonical.com-20151020093604-4luumedfqkjj2r9a
AltCloud: Remove --quiet option from udevadm call

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 get_metadata_from_fabric
 
34
from cloudinit.sources.helpers.azure import (
 
35
    get_metadata_from_fabric, iid_from_shared_config_content)
35
36
 
36
37
LOG = logging.getLogger(__name__)
37
38
 
40
41
AGENT_START = ['service', 'walinuxagent', 'start']
41
42
BOUNCE_COMMAND = ['sh', '-xc',
42
43
    "i=$interface; x=0; ifdown $i || x=$?; ifup $i || x=$?; exit $x"]
 
44
DATA_DIR_CLEAN_LIST = ['SharedConfig.xml']
43
45
 
44
46
BUILTIN_DS_CONFIG = {
45
47
    'agent_command': AGENT_START,
142
144
                            self.ds_cfg['agent_command'])
143
145
 
144
146
            ddir = self.ds_cfg['data_dir']
 
147
            shcfgxml = os.path.join(ddir, "SharedConfig.xml")
 
148
            wait_for = [shcfgxml]
145
149
 
146
150
            fp_files = []
147
151
            key_value = None
156
160
 
157
161
            missing = util.log_time(logfunc=LOG.debug, msg="waiting for files",
158
162
                                    func=wait_for_files,
159
 
                                    args=(fp_files,))
 
163
                                    args=(wait_for + fp_files,))
160
164
        if len(missing):
161
165
            LOG.warn("Did not find files, but going on: %s", missing)
162
166
 
163
167
        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
 
164
176
        metadata['public-keys'] = key_value or pubkeys_from_crt_files(fp_files)
165
177
        return metadata
166
178
 
217
229
        user_ds_cfg = util.get_cfg_by_path(self.cfg, DS_CFG_PATH, {})
218
230
        self.ds_cfg = util.mergemanydict([user_ds_cfg, self.ds_cfg])
219
231
 
 
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
 
220
247
        # walinux agent writes files world readable, but expects
221
248
        # the directory to be protected.
222
249
        write_files(ddir, files, dirmode=0o700)
232
259
                     " on Azure.", exc_info=True)
233
260
            return False
234
261
 
235
 
        self.metadata['instance-id'] = util.read_dmi_data('system-uuid')
236
262
        self.metadata.update(fabric_data)
237
263
 
238
264
        found_ephemeral = find_fabric_formatted_ephemeral_disk()
623
649
    return (md, ud, cfg, {'ovf-env.xml': contents})
624
650
 
625
651
 
 
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
 
626
658
class BrokenAzureDataSource(Exception):
627
659
    pass
628
660