def get_data(self):
        defaults = { 
            "local-hostname" : "ubuntuhost",
            "instance-id" : "nocloud"
        }

        found = [ ]
        md = { }
        ud = ""

        try:
            # parse the kernel command line, getting data passed in
            if parse_cmdline_data(self.cmdline_id, md):
                found.append("cmdline")
        except:
            util.logexc(cloudinit.log,util.WARN)
            return False

        # check to see if the seeddir has data.
        seedret={ }
        if util.read_optional_seed(seedret,base=self.seeddir + "/"):
            md = util.mergedict(md,seedret['meta-data'])
            ud = seedret['user-data']
            found.append(self.seeddir)
            cloudinit.log.debug("using seeded cache data in %s" % self.seeddir)

        # there was no indication on kernel cmdline or data
        # in the seeddir suggesting this handler should be used.
        if len(found) == 0:
            return False

        # the special argument "seedfrom" indicates we should
        # attempt to seed the userdata / metadata from its value
        if "seedfrom" in md:
            seedfrom = md["seedfrom"]
            seedfound = False
            for proto in self.supported_seed_starts:
                if seedfrom.startswith(proto):
                    seedfound=proto
                    break
            if not seedfound:
                cloudinit.log.debug("seed from %s not supported by %s" %
                    (seedfrom, self.__class__))
                return False

            # this could throw errors, but the user told us to do it
            # so if errors are raised, let them raise
            (md_seed,ud) = util.read_seeded(seedfrom)
            cloudinit.log.debug("using seeded cache data from %s" % seedfrom)

            # values in the command line override those from the seed
            md = util.mergedict(md,md_seed)
            found.append(seedfrom)

        md = util.mergedict(md,defaults)
        self.seed = ",".join(found)
        self.metadata = md;
        self.userdata_raw = ud
        return True
    def get_data(self):
        found = []
        md = {}
        ud = ""

        defaults = {
            "instance-id": "iid-dsovf"
        }

        (seedfile, contents) = get_ovf_env(base_seeddir)
        if seedfile:
            # found a seed dir
            seed = "%s/%s" % (base_seeddir, seedfile)
            (md, ud, cfg) = read_ovf_environment(contents)
            self.environment = contents

            found.append(seed)
        else:
            np = {'iso': transport_iso9660,
                  'vmware-guestd': transport_vmware_guestd, }
            name = None
            for name, transfunc in np.iteritems():
                (contents, _dev, _fname) = transfunc()
                if contents:
                    break

            if contents:
                (md, ud, cfg) = read_ovf_environment(contents)
                self.environment = contents
                found.append(name)

        # There was no OVF transports found
        if len(found) == 0:
            return False

        if 'seedfrom' in md and md['seedfrom']:
            seedfrom = md['seedfrom']
            seedfound = False
            for proto in self.supported_seed_starts:
                if seedfrom.startswith(proto):
                    seedfound = proto
                    break
            if not seedfound:
                log.debug("seed from %s not supported by %s" %
                    (seedfrom, self.__class__))
                return False

            (md_seed, ud) = util.read_seeded(seedfrom, timeout=None)
            log.debug("using seeded cache data from %s" % seedfrom)

            md = util.mergedict(md, md_seed)
            found.append(seedfrom)

        md = util.mergedict(md, defaults)
        self.seed = ",".join(found)
        self.metadata = md
        self.userdata_raw = ud
        self.cfg = cfg
        return True
    def get_data(self):
        found = []
        md = {}
        ud = ""

        defaults = {"instance-id": "iid-dsovf"}

        (seedfile, contents) = get_ovf_env(base_seeddir)
        if seedfile:
            # found a seed dir
            seed = "%s/%s" % (base_seeddir, seedfile)
            (md, ud, cfg) = read_ovf_environment(contents)
            self.environment = contents

            found.append(seed)
        else:
            np = {
                'iso': transport_iso9660,
                'vmware-guestd': transport_vmware_guestd,
            }
            name = None
            for name, transfunc in np.iteritems():
                (contents, _dev, _fname) = transfunc()
                if contents:
                    break

            if contents:
                (md, ud, cfg) = read_ovf_environment(contents)
                self.environment = contents
                found.append(name)

        # There was no OVF transports found
        if len(found) == 0:
            return False

        if 'seedfrom' in md and md['seedfrom']:
            seedfrom = md['seedfrom']
            seedfound = False
            for proto in self.supported_seed_starts:
                if seedfrom.startswith(proto):
                    seedfound = proto
                    break
            if not seedfound:
                log.debug("seed from %s not supported by %s" %
                          (seedfrom, self.__class__))
                return False

            (md_seed, ud) = util.read_seeded(seedfrom, timeout=None)
            log.debug("using seeded cache data from %s" % seedfrom)

            md = util.mergedict(md, md_seed)
            found.append(seedfrom)

        md = util.mergedict(md, defaults)
        self.seed = ",".join(found)
        self.metadata = md
        self.userdata_raw = ud
        self.cfg = cfg
        return True
    def get_data(self):
        defaults = {"local-hostname": "ubuntuhost", "instance-id": "nocloud"}

        found = []
        md = {}
        ud = ""

        try:
            # parse the kernel command line, getting data passed in
            if parse_cmdline_data(self.cmdline_id, md):
                found.append("cmdline")
        except:
            util.logexc(cloudinit.log, util.WARN)
            return False

        # check to see if the seeddir has data.
        seedret = {}
        if util.read_optional_seed(seedret, base=self.seeddir + "/"):
            md = util.mergedict(md, seedret['meta-data'])
            ud = seedret['user-data']
            found.append(self.seeddir)
            cloudinit.log.debug("using seeded cache data in %s" % self.seeddir)

        # there was no indication on kernel cmdline or data
        # in the seeddir suggesting this handler should be used.
        if len(found) == 0:
            return False

        # the special argument "seedfrom" indicates we should
        # attempt to seed the userdata / metadata from its value
        if "seedfrom" in md:
            seedfrom = md["seedfrom"]
            seedfound = False
            for proto in self.supported_seed_starts:
                if seedfrom.startswith(proto):
                    seedfound = proto
                    break
            if not seedfound:
                cloudinit.log.debug("seed from %s not supported by %s" %
                                    (seedfrom, self.__class__))
                return False

            # this could throw errors, but the user told us to do it
            # so if errors are raised, let them raise
            (md_seed, ud) = util.read_seeded(seedfrom)
            cloudinit.log.debug("using seeded cache data from %s" % seedfrom)

            # values in the command line override those from the seed
            md = util.mergedict(md, md_seed)
            found.append(seedfrom)

        md = util.mergedict(md, defaults)
        self.seed = ",".join(found)
        self.metadata = md
        self.userdata_raw = ud
        return True
 def test_nested_merge(self):
     """Test nested merge."""
     source = {"key1": {"key1.1": "value1.1"}}
     candidate = {"key1": {"key1.2": "value1.2"}}
     result = mergedict(source, candidate)
     self.assertEqual(
         {"key1": {"key1.1": "value1.1", "key1.2": "value1.2"}}, result)
    def get_data(self):
        found = None
        md = {}
        ud = ""

        defaults = {"instance-id": DEFAULT_IID, "dsmode": "pass"}

        if os.path.isdir(self.seeddir):
            try:
                (md, ud) = read_config_drive_dir(self.seeddir)
                found = self.seeddir
            except nonConfigDriveDir:
                pass

        if not found:
            dev = cfg_drive_device()
            if dev:
                try:
                    (md, ud) = util.mount_callback_umount(dev,
                        read_config_drive_dir)
                    found = dev
                except (nonConfigDriveDir, util.mountFailedError):
                    pass

        if not found:
            return False

        if 'dsconfig' in md:
            self.cfg = md['dscfg']

        md = util.mergedict(md, defaults)

        # update interfaces and ifup only on the local datasource
        # this way the DataSourceConfigDriveNet doesn't do it also.
        if 'network-interfaces' in md and self.dsmode == "local":
            if md['dsmode'] == "pass":
                log.info("updating network interfaces from configdrive")
            else:
                log.debug("updating network interfaces from configdrive")

            util.write_file("/etc/network/interfaces",
                md['network-interfaces'])
            try:
                (out, err) = util.subp(['ifup', '--all'])
                if len(out) or len(err):
                    log.warn("ifup --all had stderr: %s" % err)

            except subprocess.CalledProcessError as exc:
                log.warn("ifup --all failed: %s" % (exc.output[1]))

        self.seed = found
        self.metadata = md
        self.userdata_raw = ud

        if md['dsmode'] == self.dsmode:
            return True

        log.debug("%s: not claiming datasource, dsmode=%s" %
            (self, md['dsmode']))
        return False
Beispiel #7
0
 def get_config_obj(self,cfgfile):
     try:
         cfg = util.read_conf(cfgfile)
     except:
         cloudinit.log.critical("Failed loading of cloud config '%s'. Continuing with empty config\n" % cfgfile)
         cloudinit.log.debug(traceback.format_exc() + "\n")
         cfg = None
     if cfg is None: cfg = { }
     return(util.mergedict(cfg,self.cloud.cfg))
Beispiel #8
0
 def test_nested_merge(self):
     """Test nested merge."""
     source = {"key1": {"key1.1": "value1.1"}}
     candidate = {"key1": {"key1.2": "value1.2"}}
     result = mergedict(source, candidate)
     self.assertEqual(
         {"key1": {
             "key1.1": "value1.1",
             "key1.2": "value1.2"
         }}, result)
Beispiel #9
0
    def get_config_obj(self, cfgfile):
        try:
            cfg = util.read_conf(cfgfile)
        except:
            # TODO: this 'log' could/should be passed in
            cloudinit.log.critical("Failed loading of cloud config '%s'. "
                                   "Continuing with empty config\n" % cfgfile)
            cloudinit.log.debug(traceback.format_exc() + "\n")
            cfg = None
        if cfg is None:
            cfg = {}

        try:
            ds_cfg = self.cloud.datasource.get_config_obj()
        except:
            ds_cfg = {}

        cfg = util.mergedict(cfg, ds_cfg)
        return (util.mergedict(cfg, self.cloud.cfg))
Beispiel #10
0
    def get_config_obj(self, cfgfile):
        try:
            cfg = util.read_conf(cfgfile)
        except:
            # TODO: this 'log' could/should be passed in
            cloudinit.log.critical("Failed loading of cloud config '%s'. "
                                   "Continuing with empty config\n" % cfgfile)
            cloudinit.log.debug(traceback.format_exc() + "\n")
            cfg = None
        if cfg is None:
            cfg = {}

        try:
            ds_cfg = self.cloud.datasource.get_config_obj()
        except:
            ds_cfg = {}

        cfg = util.mergedict(cfg, ds_cfg)
        return(util.mergedict(cfg, self.cloud.cfg))
Beispiel #11
0
 def get_config_obj(self, cfgfile):
     try:
         cfg = util.read_conf(cfgfile)
     except:
         cloudinit.log.critical(
             "Failed loading of cloud config '%s'. Continuing with empty config\n"
             % cfgfile)
         cloudinit.log.debug(traceback.format_exc() + "\n")
         cfg = None
     if cfg is None: cfg = {}
     return (util.mergedict(cfg, self.cloud.cfg))
    def read_cfg(self):
        if self.cfg:
            return(self.cfg)

        try:
            conf = util.get_base_cfg(self.sysconfig, cfg_builtin, parsed_cfgs)
        except Exception:
            conf = get_builtin_cfg()

        # support reading the old ConfigObj format file and merging
        # it into the yaml dictionary
        try:
            from configobj import ConfigObj
            oldcfg = ConfigObj(self.old_conffile)
            if oldcfg is None:
                oldcfg = {}
            conf = util.mergedict(conf, oldcfg)
        except:
            pass

        return(conf)
Beispiel #13
0
    def read_cfg(self):
        if self.cfg:
            return (self.cfg)

        try:
            conf = util.get_base_cfg(self.sysconfig, cfg_builtin, parsed_cfgs)
        except Exception:
            conf = get_builtin_cfg()

        # support reading the old ConfigObj format file and merging
        # it into the yaml dictionary
        try:
            from configobj import ConfigObj
            oldcfg = ConfigObj(self.old_conffile)
            if oldcfg is None:
                oldcfg = {}
            conf = util.mergedict(conf, oldcfg)
        except:
            pass

        return (conf)
 def test_neither_dict(self):
     """Test if neither candidate or source is dict source wins."""
     source = "source"
     candidate = "candidate"
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
 def test_non_dict_source(self):
     """Test non-dict source is not modified with a dict candidate."""
     source = "not a dict"
     candidate = {"key": "value"}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
 def test_non_dict_candidate(self):
     """Test non-dict candidate is discarded."""
     source = {"key": "value"}
     candidate = "not a dict"
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
 def test_empty_source(self):
     """Test empty source is replaced by candidate."""
     source = {}
     candidate = {"key": "value"}
     result = mergedict(source, candidate)
     self.assertEqual(candidate, result)
 def test_empty_candidate(self):
     """Test empty candidate doesn't change source."""
     source = {"key": "value"}
     candidate = {}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
 def test_merge_does_not_override(self):
     """Test that candidate doesn't override source."""
     source = {"key1": "value1", "key2": "value2"}
     candidate = {"key1": "value2", "key2": "NEW VALUE"}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
Beispiel #20
0
 def test_empty_candidate(self):
     """Test empty candidate doesn't change source."""
     source = {"key": "value"}
     candidate = {}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
 def test_simple_merge(self):
     """Test simple non-conflict merge."""
     source = {"key1": "value1"}
     candidate = {"key2": "value2"}
     result = mergedict(source, candidate)
     self.assertEqual({"key1": "value1", "key2": "value2"}, result)
Beispiel #22
0
 def test_non_dict_candidate(self):
     """Test non-dict candidate is discarded."""
     source = {"key": "value"}
     candidate = "not a dict"
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
Beispiel #23
0
 def test_simple_merge(self):
     """Test simple non-conflict merge."""
     source = {"key1": "value1"}
     candidate = {"key2": "value2"}
     result = mergedict(source, candidate)
     self.assertEqual({"key1": "value1", "key2": "value2"}, result)
    def get_data(self):
        defaults = {
            "instance-id": "nocloud", "dsmode": self.dsmode
        }

        found = []
        md = {}
        ud = ""

        try:
            # parse the kernel command line, getting data passed in
            if parse_cmdline_data(self.cmdline_id, md):
                found.append("cmdline")
        except:
            util.logexc(log)
            return False

        # check to see if the seeddir has data.
        seedret = {}
        if util.read_optional_seed(seedret, base=self.seeddir + "/"):
            md = util.mergedict(md, seedret['meta-data'])
            ud = seedret['user-data']
            found.append(self.seeddir)
            log.debug("using seeded cache data in %s" % self.seeddir)

        # if the datasource config had a 'seedfrom' entry, then that takes
        # precedence over a 'seedfrom' that was found in a filesystem
        # but not over external medi
        if 'seedfrom' in self.ds_cfg and self.ds_cfg['seedfrom']:
            found.append("ds_config")
            md["seedfrom"] = self.ds_cfg['seedfrom']

        fslist = util.find_devs_with("TYPE=vfat")
        fslist.extend(util.find_devs_with("TYPE=iso9660"))

        label_list = util.find_devs_with("LABEL=cidata")
        devlist = list(set(fslist) & set(label_list))
        devlist.sort(reverse=True)

        for dev in devlist:
            try:
                (newmd, newud) = util.mount_callback_umount(dev,
                    util.read_seeded)
                md = util.mergedict(newmd, md)
                ud = newud

                # for seed from a device, the default mode is 'net'.
                # that is more likely to be what is desired.
                # If they want dsmode of local, then they must
                # specify that.
                if 'dsmode' not in md:
                    md['dsmode'] = "net"

                log.debug("using data from %s" % dev)
                found.append(dev)
                break
            except OSError, e:
                if e.errno != errno.ENOENT:
                    raise
            except util.mountFailedError:
                log.warn("Failed to mount %s when looking for seed" % dev)
                    break
            if not seedfound:
                log.debug("seed from %s not supported by %s" %
                    (seedfrom, self.__class__))
                return False

            if 'network-interfaces' in md:
                seeded_interfaces = self.dsmode

            # this could throw errors, but the user told us to do it
            # so if errors are raised, let them raise
            (md_seed, ud) = util.read_seeded(seedfrom, timeout=None)
            log.debug("using seeded cache data from %s" % seedfrom)

            # values in the command line override those from the seed
            md = util.mergedict(md, md_seed)
            found.append(seedfrom)

        md = util.mergedict(md, defaults)

        # update the network-interfaces if metadata had 'network-interfaces'
        # entry and this is the local datasource, or 'seedfrom' was used
        # and the source of the seed was self.dsmode
        # ('local' for NoCloud, 'net' for NoCloudNet')
        if ('network-interfaces' in md and
            (self.dsmode in ("local", seeded_interfaces))):
            log.info("updating network interfaces from nocloud")

            util.write_file("/etc/network/interfaces",
                md['network-interfaces'])
            try:
Beispiel #26
0
 def test_non_dict_source(self):
     """Test non-dict source is not modified with a dict candidate."""
     source = "not a dict"
     candidate = {"key": "value"}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
Beispiel #27
0
 def test_merge_does_not_override(self):
     """Test that candidate doesn't override source."""
     source = {"key1": "value1", "key2": "value2"}
     candidate = {"key1": "value2", "key2": "NEW VALUE"}
     result = mergedict(source, candidate)
     self.assertEqual(source, result)
    def get_data(self):
        found = None
        md = {}
        ud = ""

        defaults = {"instance-id": DEFAULT_IID, "dsmode": "pass"}

        if os.path.isdir(self.seeddir):
            try:
                (md, ud) = read_config_drive_dir(self.seeddir)
                found = self.seeddir
            except nonConfigDriveDir:
                pass

        if not found:
            dev = cfg_drive_device()
            if dev:
                try:
                    (md,
                     ud) = util.mount_callback_umount(dev,
                                                      read_config_drive_dir)
                    found = dev
                except (nonConfigDriveDir, util.mountFailedError):
                    pass

        if not found:
            return False

        if 'dsconfig' in md:
            self.cfg = md['dscfg']

        md = util.mergedict(md, defaults)

        # update interfaces and ifup only on the local datasource
        # this way the DataSourceConfigDriveNet doesn't do it also.
        if 'network-interfaces' in md and self.dsmode == "local":
            if md['dsmode'] == "pass":
                log.info("updating network interfaces from configdrive")
            else:
                log.debug("updating network interfaces from configdrive")

            util.write_file("/etc/network/interfaces",
                            md['network-interfaces'])
            try:
                (out, err) = util.subp(['ifup', '--all'])
                if len(out) or len(err):
                    log.warn("ifup --all had stderr: %s" % err)

            except subprocess.CalledProcessError as exc:
                log.warn("ifup --all failed: %s" % (exc.output[1]))

        self.seed = found
        self.metadata = md
        self.userdata_raw = ud

        if md['dsmode'] == self.dsmode:
            return True

        log.debug("%s: not claiming datasource, dsmode=%s" %
                  (self, md['dsmode']))
        return False
Beispiel #29
0
 def test_empty_source(self):
     """Test empty source is replaced by candidate."""
     source = {}
     candidate = {"key": "value"}
     result = mergedict(source, candidate)
     self.assertEqual(candidate, result)
Beispiel #30
0
 def test_neither_dict(self):
     """Test if neither candidate or source is dict source wins."""
     source = "source"
     candidate = "candidate"
     result = mergedict(source, candidate)
     self.assertEqual(source, result)