From 81ca0bc300ac5d8b8c0498727132e82dab72a396 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 30 Jan 2012 09:24:30 -0500 Subject: netinfo.py: minor pylint complaint on unused 'devname' --- cloudinit/netinfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py index 80223256..7e07812e 100644 --- a/cloudinit/netinfo.py +++ b/cloudinit/netinfo.py @@ -61,7 +61,7 @@ def netdev_info(empty=""): devs[curdev][target] = toks[i][len(field) + 1:] if empty != "": - for (devname, dev) in devs.iteritems(): + for (_devname, dev) in devs.iteritems(): for field in dev: if dev[field] == "": dev[field] = empty -- cgit v1.2.3 From fb2fb2ff30db6f1f06f15974a6f6be5c5ed518a6 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 30 Jan 2012 09:24:41 -0500 Subject: support empty lines in '#include' files (LP: #923043) LP: #923043 --- ChangeLog | 1 + cloudinit/UserDataHandler.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/ChangeLog b/ChangeLog index cc43aaad..bdba748e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -21,6 +21,7 @@ - fix pylint warnings [Juerg Haefliger] (LP: #914739) - add support for adding and deleting CA Certificates [Mike Milner] (LP: #915232) - in ci-info lines, use '.' to indicate empty field for easier machine reading + - support empty lines in "#include" files (LP: #923043) 0.6.2: - fix bug where update was not done unless update was explicitly set. It would not be run if 'upgrade' or packages were set to be installed diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index 93d1d36a..98729056 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -71,6 +71,8 @@ def do_include(content, appendmsg): line = line[len("#include"):].lstrip() if line.startswith("#"): continue + if line.strip() == "": + continue # urls cannot not have leading or trailing white space msum = hashlib.md5() # pylint: disable=E1101 -- cgit v1.2.3 From 16fd813d8543dd629346e81c4043411c32b7d9d1 Mon Sep 17 00:00:00 2001 From: Jeff Bauer Date: Sat, 11 Feb 2012 09:27:14 -0600 Subject: add support for salt minions --- cloudinit/CloudConfig/cc_salt_minion.py | 55 +++++++++++++++++++++++++++++++ doc/examples/cloud-config-salt-minion.txt | 53 +++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 cloudinit/CloudConfig/cc_salt_minion.py create mode 100644 doc/examples/cloud-config-salt-minion.txt diff --git a/cloudinit/CloudConfig/cc_salt_minion.py b/cloudinit/CloudConfig/cc_salt_minion.py new file mode 100644 index 00000000..9a710e72 --- /dev/null +++ b/cloudinit/CloudConfig/cc_salt_minion.py @@ -0,0 +1,55 @@ +# vi: ts=4 expandtab +# +# Author: Jeff Bauer +# +# 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 os +import os.path +import subprocess +import cloudinit.CloudConfig as cc +import yaml + +def handle(_name, cfg, cloud, log, _args): + # If there isn't a salt key in the configuration don't do anything + if 'salt_minion' not in cfg: + return + salt_cfg = cfg['salt_minion'] + # Start by installing the salt package ... + cc.install_packages(("salt",)) + config_dir = '/etc/salt' + if not os.path.isdir(config_dir): + os.makedirs(config_dir) + # ... and then update the salt configuration + if 'conf' in salt_cfg: + # Add all sections from the conf object to /etc/salt/minion + minion_config = os.path.join(config_dir, 'minion') + yaml.dump(salt_cfg['conf'], + file(minion_config, 'w'), + default_flow_style=False) + # ... copy the key pair if specified + if 'public_key' in salt_cfg and 'private_key' in salt_cfg: + pki_dir = '/etc/salt/pki' + cumask = os.umask(077) + if not os.path.isdir(pki_dir): + os.makedirs(pki_dir) + pub_name = os.path.join(pki_dir, 'minion.pub') + pem_name = os.path.join(pki_dir, 'minion.pem') + with open(pub_name, 'w') as f: + f.write(salt_cfg['public_key']) + with open(pem_name, 'w') as f: + f.write(salt_cfg['private_key']) + os.umask(cumask) + + # Start salt-minion + subprocess.check_call(['service', 'salt-minion', 'start']) diff --git a/doc/examples/cloud-config-salt-minion.txt b/doc/examples/cloud-config-salt-minion.txt new file mode 100644 index 00000000..939fdc8b --- /dev/null +++ b/doc/examples/cloud-config-salt-minion.txt @@ -0,0 +1,53 @@ +#cloud-config +# +# This is an example file to automatically setup and run a salt +# minion when the instance boots for the first time. +# Make sure that this file is valid yaml before starting instances. +# It should be passed as user-data when starting the instance. + +salt_minion: + # conf contains all the directives to be assigned in /etc/salt/minion. + + conf: + # Set the location of the salt master server, if the master server cannot be + # resolved, then the minion will fail to start. + + master: salt.example.com + + # Salt keys are manually generated by: salt-key --gen-keys=GEN_KEYS, + # where GEN_KEYS is the name of the keypair, e.g. 'minion'. The keypair + # will be copied to /etc/salt/pki on the minion instance. + + public_key: | + -----BEGIN PUBLIC KEY----- + MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAwI4yqk1Y12zVmu9Ejlua + h2FD6kjrt+N9XfGqZUUVNeRb7CA0Sj5Q6NtgoaiXuIrSea2sLda6ivqAGmtxMMrP + zpf3FwsYWxBUNF7D4YeLmYjvcTbfr3bCOIRnPNXZ+4isuvvEiM02u2cO0okZSgeb + dofNa1NbTLYAQr9jZZb7GPKrTO4CKy0xzBih/A+sl6dL9PNDmqXQEjyJS6PXG1Vj + PvD5jpSrxuIl5Ms/+2Ro3ALgvC8dgoY/3m3csnd06afumGKv5YOGtf+bnWLhc0bf + 6Sk8Q6i5t0Bl+HAULSPr+B9x/I0rN76ZnPvTj1+hJ0zTof4d0hOLx/K5OQyt7AKo + 4wIBAQ== + -----END PUBLIC KEY----- + + private_key: | + -----BEGIN RSA PRIVATE KEY----- + Proc-Type: 4,ENCRYPTED + DEK-Info: AES-128-CBC,ECE30DBBA56E2DF06B7BC415F8870994 + + YQOE5HIsghqjRsxPQqiWMH/VHmyFH6xIpBcmzxzispEHwBojlvLXviwvR66YhgNw + 7smwE10Ik4/cwwiHTZqCk++jPATPygBiqQkUijCWzcT9kfaxmqdP4PL+hu9g7kGC + KrD2Bm8/oO08s957aThuHC1sABRcJ1V3FRzJT6Za4fwweyvHVYRnmgaDA6zH0qV8 + NqBSB2hnNXKEdh6UFz9QGcrQxnRjfdIaW64zoEX7jT7gYYL7FkGXBa3XdMOA4fnl + adRwLFMs0jfilisZv8oUbPdZ6J6x3o8p8LVecCF8tdZt1zkcLSIXKnoDFpHSISGs + BD9aqD+E4ejynM/tPaVFq4IHzT8viN6h6WcH8fbpClFZ66Iyy9XL3/CjAY7Jzhh9 + fnbc4Iq28cdbmO/vkR7JyVOgEMWe1BcSqtro70XoUNRY8uDJUPqohrhm/9AigFRA + Pwyf3LqojxRnwXjHsZtGltUtEAPZzgh3fKJnx9MyRR7DPXBRig7TAHU7n2BFRhHA + TYThy29bK6NkIc/cKc2kEQVo98Cr04PO8jVxZM332FlhiVlP0kpAp+tFj7aMzPTG + sJumb9kPbMsgpEuTCONm3yyoufGEBFMrIJ+Po48M2RlYOh50VkO09pI+Eu7FPtVB + H4gKzoJIpZZ/7vYXQ3djM8s9hc5gD5CVExTZV4drbsXt6ITiwHuxZ6CNHRBPL5AY + wmF8QZz4oivv1afdSe6E6OGC3uVmX3Psn5CVq2pE8VlRDKFy1WqfU2enRAijSS2B + rtJs263fOJ8ZntDzMVMPgiAlzzfA285KUletpAeUmz+peR1gNzkE0eKSG6THOCi0 + rfmR8SeEzyNvin0wQ3qgYiiHjHbbFhJIMAQxoX+0hDSooM7Wo5wkLREULpGuesTg + A6Fe3CiOivMDraNGA7H6Yg== + -----END RSA PRIVATE KEY----- + -- cgit v1.2.3 From fcefeb469261bf19ffa249ceddf1ca003e520e5d Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 15 Feb 2012 13:49:41 -0500 Subject: fix update-etc-hosts, 'manage_etc_hosts' is not a boolean, but a string --- cloudinit/CloudConfig/cc_update_etc_hosts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py index 572e6750..6ad2fca8 100644 --- a/cloudinit/CloudConfig/cc_update_etc_hosts.py +++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py @@ -28,7 +28,7 @@ frequency = per_always def handle(_name, cfg, cloud, log, _args): (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud) - manage_hosts = util.get_cfg_option_bool(cfg, "manage_etc_hosts", False) + manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False) if manage_hosts in ("True", "true", True, "template"): # render from template file try: -- cgit v1.2.3 From 4213f7c28c2b6788938b1504e5dec7fcda2b8806 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 15 Feb 2012 16:03:37 -0500 Subject: DataSourceOVF: only search for OVF data on ISO9660 filesystems I believe this will resolve LP: #898373 by making DataSourceOVF restrict its mounting of filesystems to iso9660. By doing this, it will never mount a ext3 (or btrfs or any fs that mountall would fsck) and thus will avoid any races with that. LP: #898373 --- ChangeLog | 1 + cloudinit/DataSourceOVF.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 45af41ed..5b0cfb90 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,7 @@ - in ci-info lines, use '.' to indicate empty field for easier machine reading - support empty lines in "#include" files (LP: #923043) - support configuration of salt minions (Jeff Bauer) (LP: #927795) + - DataSourceOVF: only search for OVF data on ISO9660 filesystems (LP: #898373) 0.6.2: - fix bug where update was not done unless update was explicitly set. It would not be run if 'upgrade' or packages were set to be installed diff --git a/cloudinit/DataSourceOVF.py b/cloudinit/DataSourceOVF.py index 1f2b622e..a0b1b518 100644 --- a/cloudinit/DataSourceOVF.py +++ b/cloudinit/DataSourceOVF.py @@ -162,7 +162,7 @@ def get_ovf_env(dirname): # transport functions take no input and return # a 3 tuple of content, path, filename -def transport_iso9660(require_iso=False): +def transport_iso9660(require_iso=True): # default_regex matches values in # /lib/udev/rules.d/60-cdrom_id.rules -- cgit v1.2.3 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 +++++++++++++++++++++++++++++++++++++ cloudinit/__init__.py | 2 +- cloudinit/util.py | 80 ++++++++++++++ config/cloud.cfg | 2 +- 4 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 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 diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py index 7a34e053..0fc61795 100644 --- a/cloudinit/__init__.py +++ b/cloudinit/__init__.py @@ -29,7 +29,7 @@ cfg_env_name = "CLOUD_CFG" cfg_builtin = """ log_cfgs: [] -datasource_list: ["NoCloud", "OVF", "Ec2"] +datasource_list: ["NoCloud", "ConfigDrive", "OVF", "Ec2"] def_log_file: /var/log/cloud-init.log syslog_fix_perms: syslog:adm """ diff --git a/cloudinit/util.py b/cloudinit/util.py index e6489648..b53aa5fe 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -32,6 +32,7 @@ import re import socket import sys import time +import tempfile import traceback import urlparse @@ -630,3 +631,82 @@ def close_stdin(): return with open(os.devnull) as fp: os.dup2(fp.fileno(), sys.stdin.fileno()) + +def find_devs_with(criteria): + """ + find devices matching given criteria (via blkid) + criteria can be *one* of: + TYPE= + LABEL=