summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/CloudConfig')
-rw-r--r--cloudinit/CloudConfig/cc_byobu.py43
-rw-r--r--cloudinit/CloudConfig/cc_grub_dpkg.py3
-rw-r--r--cloudinit/CloudConfig/cc_mounts.py9
-rw-r--r--cloudinit/CloudConfig/cc_set_hostname.py7
-rw-r--r--cloudinit/CloudConfig/cc_ssh.py42
-rw-r--r--cloudinit/CloudConfig/cc_timezone.py1
-rw-r--r--cloudinit/CloudConfig/cc_update_etc_hosts.py34
7 files changed, 110 insertions, 29 deletions
diff --git a/cloudinit/CloudConfig/cc_byobu.py b/cloudinit/CloudConfig/cc_byobu.py
index 1a4545af..406a1f67 100644
--- a/cloudinit/CloudConfig/cc_byobu.py
+++ b/cloudinit/CloudConfig/cc_byobu.py
@@ -27,19 +27,40 @@ def handle(name,cfg,cloud,log,args):
if not value: return
- if value == "user":
- user = util.get_cfg_option_str(cfg,"user","ubuntu")
- cmd = [ 'sudo', '-Hu', user, 'byobu-launcher-install' ]
- elif value == "system":
- shcmd="echo '%s' | debconf-set-selections && %s" % \
- ( "byobu byobu/launch-by-default boolean true",
- "dpkg-reconfigure byobu --frontend=noninteractive" )
- cmd = [ "/bin/sh", "-c", shcmd ]
- else:
+ if value == "user" or value == "system":
+ value = "enable-%s" % value
+
+ valid = ( "enable-user", "enable-system", "enable",
+ "disable-user", "disable-system", "disable" )
+ if not value in valid:
log.warn("Unknown value %s for byobu_by_default" % value)
- return
- log.debug("enabling byobu for %s" % value)
+ mod_user = value.endswith("-user")
+ mod_sys = value.endswith("-system")
+ if value.startswith("enable"):
+ bl_inst = "install"
+ dc_val = "byobu byobu/launch-by-default boolean true"
+ mod_sys = True
+ else:
+ if value == "disable":
+ mod_user = True
+ mod_sys = True
+ bl_inst = "uninstall"
+ dc_val = "byobu byobu/launch-by-default boolean false"
+
+ shcmd = ""
+ if mod_user:
+ user = util.get_cfg_option_str(cfg,"user","ubuntu")
+ shcmd += " sudo -Hu \"%s\" byobu-launcher-%s" % (user, bl_inst)
+ shcmd += " || X=$(($X+1)); "
+ if mod_sys:
+ shcmd += "echo \"%s\" | debconf-set-selections" % dc_val
+ shcmd += " && dpkg-reconfigure byobu --frontend=noninteractive"
+ shcmd += " || X=$(($X+1)); "
+
+ cmd = [ "/bin/sh", "-c", "%s %s %s" % ("X=0;", shcmd, "exit $X" ) ]
+
+ log.debug("setting byobu to %s" % value)
try:
subprocess.check_call(cmd)
diff --git a/cloudinit/CloudConfig/cc_grub_dpkg.py b/cloudinit/CloudConfig/cc_grub_dpkg.py
index dafb43cf..b26e90e8 100644
--- a/cloudinit/CloudConfig/cc_grub_dpkg.py
+++ b/cloudinit/CloudConfig/cc_grub_dpkg.py
@@ -31,7 +31,8 @@ def handle(name,cfg,cloud,log,args):
idevs_empty=util.get_cfg_option_str(cfg["grub-dpkg"],
"grub-pc/install_devices_empty",None)
- if os.path.exists("/dev/sda1") and not os.path.exists("/dev/sda"):
+ if (( os.path.exists("/dev/sda1") and not os.path.exists("/dev/sda") ) or
+ ( os.path.exists("/dev/xvda1") and not os.path.exists("/dev/xvda") )):
if idevs == None: idevs=""
if idevs_empty == None: idevs_empty="true"
else:
diff --git a/cloudinit/CloudConfig/cc_mounts.py b/cloudinit/CloudConfig/cc_mounts.py
index 8ee4f718..592a030a 100644
--- a/cloudinit/CloudConfig/cc_mounts.py
+++ b/cloudinit/CloudConfig/cc_mounts.py
@@ -32,12 +32,13 @@ def is_mdname(name):
return False
def handle(name,cfg,cloud,log,args):
- # these are our default set of mounts
- defmnts = [ [ "ephemeral0", "/mnt", "auto", "defaults,nobootwait", "0", "2" ],
- [ "swap", "none", "swap", "sw", "0", "0" ] ]
-
# fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno
defvals = [ None, None, "auto", "defaults,nobootwait", "0", "2" ]
+ defvals = cfg.get("mount_default_fields", defvals)
+
+ # these are our default set of mounts
+ defmnts = [ [ "ephemeral0", "/mnt", "auto", defvals[3], "0", "2" ],
+ [ "swap", "none", "swap", "sw", "0", "0" ] ]
cfgmnt = [ ]
if cfg.has_key("mounts"):
diff --git a/cloudinit/CloudConfig/cc_set_hostname.py b/cloudinit/CloudConfig/cc_set_hostname.py
index 49368019..2b130810 100644
--- a/cloudinit/CloudConfig/cc_set_hostname.py
+++ b/cloudinit/CloudConfig/cc_set_hostname.py
@@ -24,12 +24,7 @@ def handle(name,cfg,cloud,log,args):
return(True)
try:
- hostname_prefix = util.get_cfg_option_str(cfg, "hostname_prefix", None)
- hostname_attr = util.get_cfg_option_str(cfg, "hostname_attribute", "hostname")
- hostname_function = getattr(cloud, 'get_' + hostname_attr, None)
- if hostname_fucntion is None: hostname_fucntion = cloud.get_hostname
- hostname = util.get_cfg_option_str(cfg,"hostname", hostname_function)
- if hostname_prefix: hostname = hostname_prefix + "-" + hostname
+ hostname = util.get_cfg_option_str(cfg,"hostname",cloud.get_hostname())
set_hostname(hostname, log)
except Exception as e:
util.logexc(log)
diff --git a/cloudinit/CloudConfig/cc_ssh.py b/cloudinit/CloudConfig/cc_ssh.py
index c4603d2b..ee03de22 100644
--- a/cloudinit/CloudConfig/cc_ssh.py
+++ b/cloudinit/CloudConfig/cc_ssh.py
@@ -20,7 +20,15 @@ import os
import glob
import subprocess
+DISABLE_ROOT_OPTS="no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" rather than the user \\\"root\\\".\';echo;sleep 10\""
+
+
+global_log = None
+
def handle(name,cfg,cloud,log,args):
+ global global_log
+ global_log = log
+
# remove the static keys from the pristine image
for f in glob.glob("/etc/ssh/ssh_host_*_key*"):
try: os.unlink(f)
@@ -55,14 +63,17 @@ def handle(name,cfg,cloud,log,args):
try:
user = util.get_cfg_option_str(cfg,'user')
disable_root = util.get_cfg_option_bool(cfg, "disable_root", True)
+ disable_root_opts = util.get_cfg_option_str(cfg, "disable_root_opts",
+ DISABLE_ROOT_OPTS)
keys = cloud.get_public_ssh_keys()
if cfg.has_key("ssh_authorized_keys"):
cfgkeys = cfg["ssh_authorized_keys"]
keys.extend(cfgkeys)
- apply_credentials(keys,user,disable_root)
+ apply_credentials(keys,user,disable_root, disable_root_opts)
except:
+ util.logexc(log)
log.warn("applying credentials failed!\n")
send_ssh_keys_to_console()
@@ -70,13 +81,13 @@ def handle(name,cfg,cloud,log,args):
def send_ssh_keys_to_console():
subprocess.call(('/usr/lib/cloud-init/write-ssh-key-fingerprints',))
-def apply_credentials(keys, user, disable_root):
+def apply_credentials(keys, user, disable_root, disable_root_opts=DISABLE_ROOT_OPTS):
keys = set(keys)
if user:
setup_user_keys(keys, user, '')
if disable_root:
- key_prefix = 'command="echo \'Please login as the user \\\"%s\\\" rather than the user \\\"root\\\".\';echo;sleep 10" ' % user
+ key_prefix = disable_root_opts.replace('$USER', user)
else:
key_prefix = ''
@@ -93,13 +104,34 @@ def setup_user_keys(keys, user, key_prefix):
os.mkdir(ssh_dir)
os.chown(ssh_dir, pwent.pw_uid, pwent.pw_gid)
- authorized_keys = '%s/.ssh/authorized_keys' % pwent.pw_dir
+ try:
+ ssh_cfg = parse_ssh_config()
+ akeys = ssh_cfg.get("AuthorizedKeysFile","%h/.ssh/authorized_keys")
+ akeys = akeys.replace("%h", pwent.pw_dir)
+ akeys = akeys.replace("%u", user)
+ authorized_keys = akeys
+ except Exception as e:
+ authorized_keys = '%s/.ssh/authorized_keys' % pwent.pw_dir
+ util.logexc(global_log)
+
fp = open(authorized_keys, 'a')
- fp.write(''.join(['%s%s\n' % (key_prefix, key) for key in keys]))
+ key_prefix = key_prefix.replace("\n"," ")
+ fp.write(''.join(['%s %s\n' % (key_prefix.strip(), key) for key in keys]))
fp.close()
os.chown(authorized_keys, pwent.pw_uid, pwent.pw_gid)
os.umask(saved_umask)
+def parse_ssh_config(fname="/etc/ssh/sshd_config"):
+ ret = { }
+ fp=open(fname)
+ for l in fp.readlines():
+ l = l.strip()
+ if not l or l.startswith("#"):
+ continue
+ key,val = l.split(None,1)
+ ret[key]=val
+ fp.close()
+ return(ret)
diff --git a/cloudinit/CloudConfig/cc_timezone.py b/cloudinit/CloudConfig/cc_timezone.py
index f221819e..a26df8f9 100644
--- a/cloudinit/CloudConfig/cc_timezone.py
+++ b/cloudinit/CloudConfig/cc_timezone.py
@@ -25,7 +25,6 @@ frequency = per_instance
tz_base = "/usr/share/zoneinfo"
def handle(name,cfg,cloud,log,args):
- print args
if len(args) != 0:
timezone = args[0]
else:
diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py
index 856cbae1..10ee5435 100644
--- a/cloudinit/CloudConfig/cc_update_etc_hosts.py
+++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py
@@ -17,12 +17,44 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cloudinit.util as util
from cloudinit.CloudConfig import per_always
+import platform
+import StringIO
frequency = per_always
def handle(name,cfg,cloud,log,args):
if not util.get_cfg_option_bool(cfg,"manage_etc_hosts",False):
- log.debug("manage_etc_hosts is not set. not modifying /etc/hosts")
+ log.debug("manage_etc_hosts is not set, checking sanity of /etc/hosts")
+ with open('/etc/hosts', 'r') as etchosts:
+ current_hostname = platform.node()
+ hosts_line = "# Added by cloud-init\n127.0.1.1\t%s.localdomain %s\n" % (current_hostname, current_hostname)
+ need_write = False
+ need_change = True
+ new_etchosts = StringIO.StringIO()
+ for line in etchosts:
+ split_line = [s.strip() for s in line.split()]
+ # skip over malformed /etc/hosts entries
+ if len(split_line) < 2:
+ continue
+ ip, hostnames = split_line[0], split_line[1:]
+ if ip == "127.0.1.1":
+ for hostname in hostnames:
+ if hostname == current_hostname:
+ need_change = False
+ if need_change == True:
+ line = hosts_line
+ need_change = False
+ need_write = True
+ new_etchosts.write(line)
+ etchosts.close()
+ if need_change == True:
+ new_etchosts.write(hosts_line)
+ need_write = True
+ if need_write == True:
+ new_etcfile = open ('/etc/hosts','wb')
+ new_etcfile.write(new_etchosts.getvalue())
+ new_etcfile.close()
+ new_etchosts.close()
return
try: