From 725ea78fe96b3282f67d67ddd6079f24f055c746 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 16 Feb 2012 13:31:19 -0500 Subject: initial version of DataSourceConfigDrive --- cloudinit/DataSourceConfigDrive.py | 208 +++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 cloudinit/DataSourceConfigDrive.py (limited to 'cloudinit/DataSourceConfigDrive.py') diff --git a/cloudinit/DataSourceConfigDrive.py b/cloudinit/DataSourceConfigDrive.py new file mode 100644 index 00000000..1c6021b3 --- /dev/null +++ b/cloudinit/DataSourceConfigDrive.py @@ -0,0 +1,208 @@ +# Copyright (C) 2012 Canonical Ltd. +# +# Author: Scott Moser +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import cloudinit.DataSource as DataSource + +from cloudinit import seeddir as base_seeddir +import cloudinit.util as util +import os.path +import os +import json + +DEFAULT_IID = "iid-dsconfigdrive" + + +class DataSourceConfigDrive(DataSource.DataSource): + seed = None + seeddir = base_seeddir + '/config_drive' + cfg = {} + userdata_raw = None + metadata = None + dsmode = "local" + + def __str__(self): + mstr = "DataSourceConfigDrive" + mstr = mstr + " [seed=%s]" % self.seed + return(mstr) + + def get_data(self): + found = None + md = {} + ud = "" + + defaults = {"instance-id": DEFAULT_IID} + + 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) + + self.seed = found + self.metadata = md + self.userdata_raw = ud + + if 'dsmode' in md and md['dsmode'] == self.dsmode: + return True + + return False + + def get_public_ssh_keys(self): + if not 'public-keys' in self.metadata: + return([]) + return([self.metadata['public-keys'], ]) + + # the data sources' config_obj is a cloud-config formated + # object that came to it from ways other than cloud-config + # because cloud-config content would be handled elsewhere + def get_config_obj(self): + return(self.cfg) + + +class DataSourceConfigDriveNet(DataSourceConfigDrive): + dsmode = "net" + + +class nonConfigDriveDir(Exception): + pass + + +def update_network_config(content): + """ + Update [write] /etc/network/interfaces + """ + util.write_file("/etc/network/interfaces", content) + util.subp(['ifup', '--all']) + + +def cfg_drive_device(): + """ get the config drive device. return a string like '/dev/vdb' + or None (if there is no non-root device attached). This does not + check the contents, only reports that if there *were* a config_drive + attached, it would be this device. + per config_drive documentation, this is + "associated as the last available disk on the instance" + """ + + if 'CLOUD_INIT_CONFIG_DRIVE_DEVICE' in os.environ: + return(os.environ['CLOUD_INIT_CONFIG_DRIVE_DEVICE']) + + # we are looking for a raw block device (sda, not sda1) with a vfat + # filesystem on it. + + letters = "abcdefghijklmnopqrstuvwxyz" + devs = util.find_devs_with("TYPE=vfat") + + # filter out anything not ending in a letter (ignore partitions) + devs = [f for f in devs if f[-1] not in letters] + + # sort them in reverse so "last" device is first + devs.sort(reverse=True) + + if len(devs): + return(devs[0]) + + return(None) + + +def read_config_drive_dir(source_dir): + """ + read_config_drive_dir(source_dir): + read source_dir, and return a tuple with metadata dict and user-data + string populated. If not a valid dir, raise a nonConfigDriveDir + """ + md = {} + ud = "" + + flist = ("etc/network/interfaces", "root/.ssh/authorized_keys", "meta.js") + found = [f for f in flist if os.path.isfile("%s/%s" % (source_dir, f))] + + if len(found) == 0: + raise nonConfigDriveDir("%s: %s" % (source_dir, "no files found")) + + if "etc/network/interfaces" in found: + with open("%s/%s" % (source_dir, "/etc/network/interfaces")) as fp: + md['interfaces'] = fp.read() + + if "root/.ssh/authorized_keys" in found: + with open("%s/%s" % (source_dir, "root/.ssh/authorized_keys")) as fp: + md['public_keys'] = fp.read() + + meta_js = {} + + if "meta.js" in found: + content = '' + with open("%s/%s" % (source_dir, "meta.js")) as fp: + content = fp.read() + md['meta_js'] = content + try: + meta_js = json.loads(content) + except ValueError: + raise nonConfigDriveDir("%s: %s" % + (source_dir, "invalid json in meta.js")) + + for copy in ('public_keys', 'dsmode', 'instance-id', 'dscfg'): + if copy in meta_js: + md[copy] = meta_js[copy] + + if 'user-data' in meta_js: + ud = meta_js['user-data'] + + return(md, ud) + +datasources = ( + (DataSourceConfigDrive, (DataSource.DEP_FILESYSTEM, )), + (DataSourceConfigDriveNet, + (DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK)), +) + + +# return a list of data sources that match this set of dependencies +def get_datasource_list(depends): + return(DataSource.list_from_depends(depends, datasources)) + +if __name__ == "__main__": + def main(): + import sys + import pprint + print cfg_drive_device() + (md, ud) = read_config_drive_dir(sys.argv[1]) + print "=== md ===" + pprint.pprint(md) + print "=== ud ===" + print(ud) + + main() + +# vi: ts=4 expandtab -- cgit v1.2.3 From 7e770b1bc1a73afe0a5b37bb2242fce930089890 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 16 Feb 2012 15:10:45 -0500 Subject: DataSourceConfigDrive: generally seems functional --- cloudinit/DataSourceConfigDrive.py | 55 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'cloudinit/DataSourceConfigDrive.py') diff --git a/cloudinit/DataSourceConfigDrive.py b/cloudinit/DataSourceConfigDrive.py index 1c6021b3..04b9e0ce 100644 --- a/cloudinit/DataSourceConfigDrive.py +++ b/cloudinit/DataSourceConfigDrive.py @@ -17,10 +17,12 @@ import cloudinit.DataSource as DataSource from cloudinit import seeddir as base_seeddir +from cloudinit import log import cloudinit.util as util import os.path import os import json +import subprocess DEFAULT_IID = "iid-dsconfigdrive" @@ -34,7 +36,7 @@ class DataSourceConfigDrive(DataSource.DataSource): dsmode = "local" def __str__(self): - mstr = "DataSourceConfigDrive" + mstr = "DataSourceConfigDrive[%s]" % self.dsmode mstr = mstr + " [seed=%s]" % self.seed return(mstr) @@ -43,13 +45,14 @@ class DataSourceConfigDrive(DataSource.DataSource): md = {} ud = "" - defaults = {"instance-id": DEFAULT_IID} + defaults = {"instance-id": DEFAULT_IID, "dsmode": "pass"} - try: - (md, ud) = read_config_drive_dir(self.seeddir) - found = self.seeddir - except nonConfigDriveDir: - 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() @@ -69,19 +72,36 @@ class DataSourceConfigDrive(DataSource.DataSource): md = util.mergedict(md, defaults) + if 'interfaces' in md and md['dsmode'] in (self.dsmode, "pass"): + 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['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 'dsmode' in md and md['dsmode'] == self.dsmode: + if md['dsmode'] == self.dsmode: return True + log.debug("%s: not claiming datasource, dsmode=%s" % + (self, md['dsmode'])) return False def get_public_ssh_keys(self): if not 'public-keys' in self.metadata: return([]) - return([self.metadata['public-keys'], ]) + return(self.metadata['public-keys']) # the data sources' config_obj is a cloud-config formated # object that came to it from ways other than cloud-config @@ -98,14 +118,6 @@ class nonConfigDriveDir(Exception): pass -def update_network_config(content): - """ - Update [write] /etc/network/interfaces - """ - util.write_file("/etc/network/interfaces", content) - util.subp(['ifup', '--all']) - - def cfg_drive_device(): """ get the config drive device. return a string like '/dev/vdb' or None (if there is no non-root device attached). This does not @@ -125,7 +137,7 @@ def cfg_drive_device(): devs = util.find_devs_with("TYPE=vfat") # filter out anything not ending in a letter (ignore partitions) - devs = [f for f in devs if f[-1] not in letters] + devs = [f for f in devs if f[-1] in letters] # sort them in reverse so "last" device is first devs.sort(reverse=True) @@ -157,7 +169,10 @@ def read_config_drive_dir(source_dir): if "root/.ssh/authorized_keys" in found: with open("%s/%s" % (source_dir, "root/.ssh/authorized_keys")) as fp: - md['public_keys'] = fp.read() + content = fp.read() + lines = content.splitlines() + keys = [l for l in lines if len(l) and not l.startswith("#")] + md['public-keys'] = keys meta_js = {} @@ -172,7 +187,7 @@ def read_config_drive_dir(source_dir): raise nonConfigDriveDir("%s: %s" % (source_dir, "invalid json in meta.js")) - for copy in ('public_keys', 'dsmode', 'instance-id', 'dscfg'): + for copy in ('public-keys', 'dsmode', 'instance-id', 'dscfg'): if copy in meta_js: md[copy] = meta_js[copy] -- cgit v1.2.3 From 45bff1e1c6b1a5a1d9d52e109d38680853c58b11 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 17 Feb 2012 10:49:35 -0500 Subject: DataSourceConfigDrive: change 'interfaces' to 'network-interfaces' Instead of a metadata entry named 'interfaces', use 'network-interfaces' which is a somewhat less likely namespace collision. --- cloudinit/DataSourceConfigDrive.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cloudinit/DataSourceConfigDrive.py') diff --git a/cloudinit/DataSourceConfigDrive.py b/cloudinit/DataSourceConfigDrive.py index 04b9e0ce..af776e08 100644 --- a/cloudinit/DataSourceConfigDrive.py +++ b/cloudinit/DataSourceConfigDrive.py @@ -78,7 +78,7 @@ class DataSourceConfigDrive(DataSource.DataSource): else: log.debug("updating network interfaces from configdrive") - util.write_file("/etc/network/interfaces", md['interfaces']) + util.write_file("/etc/network/interfaces", md['network-interfaces']) try: (out, err) = util.subp(['ifup', '--all']) if len(out) or len(err): @@ -165,7 +165,7 @@ def read_config_drive_dir(source_dir): if "etc/network/interfaces" in found: with open("%s/%s" % (source_dir, "/etc/network/interfaces")) as fp: - md['interfaces'] = fp.read() + md['network-interfaces'] = fp.read() if "root/.ssh/authorized_keys" in found: with open("%s/%s" % (source_dir, "root/.ssh/authorized_keys")) as fp: -- cgit v1.2.3 From 979bee2c9ee477ea495c5df4f420d5ca2123a840 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 17 Feb 2012 11:00:39 -0500 Subject: DataSourceConfigDrive: update interfaces (and ifup) only on local Previously, the 'ifup --all' and update of /etc/network/interfaces was done only if the specified 'dsmode' (which defaults to 'pass') was either 'pass' or *this* dsmode. That meant that it would be updated once on DataSourceConfigDrive and on DataSourceConfigDriveNet. This changes that to only happen on local. --- cloudinit/DataSourceConfigDrive.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'cloudinit/DataSourceConfigDrive.py') diff --git a/cloudinit/DataSourceConfigDrive.py b/cloudinit/DataSourceConfigDrive.py index af776e08..f44344a2 100644 --- a/cloudinit/DataSourceConfigDrive.py +++ b/cloudinit/DataSourceConfigDrive.py @@ -72,13 +72,16 @@ class DataSourceConfigDrive(DataSource.DataSource): md = util.mergedict(md, defaults) - if 'interfaces' in md and md['dsmode'] in (self.dsmode, "pass"): + # 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']) + util.write_file("/etc/network/interfaces", + md['network-interfaces']) try: (out, err) = util.subp(['ifup', '--all']) if len(out) or len(err): -- cgit v1.2.3 From 3b3386dd794c9063db99fc0c9422119e8536b18d Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 17 Feb 2012 16:59:04 -0500 Subject: ConfigDrive: better support public-keys in meta flags This makes the user able to pass in multi-line input to the public-key flag, and it will be handled correctly (just as if it came from the authorized_keys file) --- cloudinit/DataSourceConfigDrive.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'cloudinit/DataSourceConfigDrive.py') diff --git a/cloudinit/DataSourceConfigDrive.py b/cloudinit/DataSourceConfigDrive.py index f44344a2..2db4a76a 100644 --- a/cloudinit/DataSourceConfigDrive.py +++ b/cloudinit/DataSourceConfigDrive.py @@ -162,6 +162,7 @@ def read_config_drive_dir(source_dir): flist = ("etc/network/interfaces", "root/.ssh/authorized_keys", "meta.js") found = [f for f in flist if os.path.isfile("%s/%s" % (source_dir, f))] + keydata = "" if len(found) == 0: raise nonConfigDriveDir("%s: %s" % (source_dir, "no files found")) @@ -172,10 +173,7 @@ def read_config_drive_dir(source_dir): if "root/.ssh/authorized_keys" in found: with open("%s/%s" % (source_dir, "root/.ssh/authorized_keys")) as fp: - content = fp.read() - lines = content.splitlines() - keys = [l for l in lines if len(l) and not l.startswith("#")] - md['public-keys'] = keys + keydata = fp.read() meta_js = {} @@ -190,7 +188,14 @@ def read_config_drive_dir(source_dir): raise nonConfigDriveDir("%s: %s" % (source_dir, "invalid json in meta.js")) - for copy in ('public-keys', 'dsmode', 'instance-id', 'dscfg'): + keydata = meta_js.get('public-keys', keydata) + + if keydata: + lines = keydata.splitlines() + md['public-keys'] = [l for l in lines + if len(l) and not l.startswith("#")] + + for copy in ('dsmode', 'instance-id', 'dscfg'): if copy in meta_js: md[copy] = meta_js[copy] -- cgit v1.2.3