summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit')
-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
-rw-r--r--cloudinit/DataSourceEc2.py87
-rw-r--r--cloudinit/UserDataHandler.py1
-rw-r--r--cloudinit/__init__.py27
-rw-r--r--cloudinit/boto_utils.py8
-rw-r--r--cloudinit/util.py6
12 files changed, 189 insertions, 79 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:
diff --git a/cloudinit/DataSourceEc2.py b/cloudinit/DataSourceEc2.py
index 9f1cf840..4abb6e47 100644
--- a/cloudinit/DataSourceEc2.py
+++ b/cloudinit/DataSourceEc2.py
@@ -27,10 +27,12 @@ import sys
import boto_utils
import os.path
import errno
+import urlparse
class DataSourceEc2(DataSource.DataSource):
api_ver = '2009-04-04'
seeddir = seeddir + '/ec2'
+ metadata_address = "http://169.254.169.254"
def __str__(self):
return("DataSourceEc2")
@@ -46,8 +48,8 @@ class DataSourceEc2(DataSource.DataSource):
try:
if not self.wait_for_metadata_service():
return False
- self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver)
- self.metadata = boto_utils.get_instance_metadata(self.api_ver)
+ self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver, None, self.metadata_address)
+ self.metadata = boto_utils.get_instance_metadata(self.api_ver, self.metadata_address)
return True
except Exception as e:
print e
@@ -100,30 +102,58 @@ class DataSourceEc2(DataSource.DataSource):
log.warn("Failed to get timeout, using %s" % timeout)
sleeptime = 1
- address = '169.254.169.254'
+
+ def_mdurls = ["http://169.254.169.254", "http://instance-data:8773"]
+ try:
+ mdurls = mcfg.get("metadata_urls", def_mdurls)
+ except Exception as e:
+ mdurls = def_mdurls
+ util.logexc(log)
+ log.warn("Failed to get metadata URLs, using defaults")
+
starttime = time.time()
-
- url="http://%s/%s/meta-data/instance-id" % (address,self.api_ver)
- for x in range(sleeps):
- # given 100 sleeps, this ends up total sleep time of 1050 sec
- sleeptime=int(x/5)+1
- reason = ""
- try:
- req = urllib2.Request(url)
- resp = urllib2.urlopen(req, timeout=timeout)
- if resp.read() != "": return True
- reason = "empty data [%s]" % resp.getcode()
- except urllib2.HTTPError as e:
- reason = "http error [%s]" % e.code
- except urllib2.URLError as e:
- reason = "url error [%s]" % e.reason
-
- if x == 0:
- log.warning("waiting for metadata service at %s\n" % url)
-
- log.warning(" %s [%02s/%s]: %s\n" %
- (time.strftime("%H:%M:%S",time.gmtime()), x+1, sleeps, reason))
+ # Remove addresses from the list that wont resolve.
+ filtered = [x for x in mdurls if try_to_resolve_metadata(x)]
+
+ if set(filtered) != set(mdurls):
+ log.debug("removed the following from metadata urls: %s" %
+ list((set(mdurls) - set(filtered))))
+
+ if len(filtered):
+ mdurls = filtered
+ else:
+ log.warn("Empty metadata url list! using default list")
+ mdurls = def_mdurls
+
+ log.debug("Searching the following metadata urls: %s" % mdurls)
+
+ for x in range(sleeps):
+ for url in mdurls:
+ iurl="%s/%s/meta-data/instance-id" % (url, self.api_ver)
+
+ # given 100 sleeps, this ends up total sleep time of 1050 sec
+ sleeptime=int(x/5)+1
+
+ reason = ""
+ try:
+ req = urllib2.Request(iurl)
+ resp = urllib2.urlopen(req, timeout=timeout)
+ if resp.read() != "":
+ self.metadata_address = url
+ log.debug("Using metadata source: '%s'" % url)
+ return True
+ reason = "empty data [%s]" % resp.getcode()
+ except urllib2.HTTPError as e:
+ reason = "http error [%s]" % e.code
+ except urllib2.URLError as e:
+ reason = "url error [%s]" % e.reason
+
+ #not needed? Addresses being checked are displayed above
+ #if x == 0:
+ # log.warn("waiting for metadata service at %s" % url)
+
+ log.warn("'%s' failed: %s" % (url, reason))
time.sleep(sleeptime)
log.critical("giving up on md after %i seconds\n" %
@@ -181,6 +211,15 @@ class DataSourceEc2(DataSource.DataSource):
return True
return False
+def try_to_resolve_metadata(url):
+ try:
+ addr = urlparse.urlsplit(url).netloc.split(":")[0]
+ socket.getaddrinfo(addr, None)
+ return True
+ except Exception as e:
+ return False
+
+
datasources = [
( DataSourceEc2, ( DataSource.DEP_FILESYSTEM , DataSource.DEP_NETWORK ) ),
]
diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py
index fbb000fc..83377dab 100644
--- a/cloudinit/UserDataHandler.py
+++ b/cloudinit/UserDataHandler.py
@@ -74,7 +74,6 @@ def explode_cc_archive(archive,parts):
if mtype == None:
mtype = type_from_startswith(payload,def_type)
- print "adding %s,%s" % (filename, mtype)
parts['content'].append(content)
parts['names'].append(filename)
parts['types'].append(mtype)
diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py
index 24e12d08..82dfaa8f 100644
--- a/cloudinit/__init__.py
+++ b/cloudinit/__init__.py
@@ -243,11 +243,6 @@ class CloudInit:
util.write_file(self.get_ipath('userdata'),
self.datasource.get_userdata(), 0600)
- def initctl_emit(self):
- cc_path = get_ipath_cur('cloud_config')
- subprocess.Popen(['initctl', 'emit', 'cloud-config',
- '%s=%s' % (cfg_env_name,cc_path)]).communicate()
-
def sem_getpath(self,name,freq):
if freq == 'once-per-instance':
return("%s/%s" % (self.get_ipath("sem"),name))
@@ -393,14 +388,15 @@ class CloudInit:
filename=filename.replace(os.sep,'_')
scriptsdir = get_ipath_cur('scripts')
util.write_file("%s/%s" %
- (scriptsdir,filename), payload, 0700)
+ (scriptsdir,filename), util.dos2unix(payload), 0700)
def handle_upstart_job(self,data,ctype,filename,payload):
if ctype == "__end__" or ctype == "__begin__": return
if not filename.endswith(".conf"):
filename=filename+".conf"
- util.write_file("%s/%s" % ("/etc/init",filename), payload, 0644)
+ util.write_file("%s/%s" % ("/etc/init",filename),
+ util.dos2unix(payload), 0644)
def handle_cloud_config(self,data,ctype,filename,payload):
if ctype == "__begin__":
@@ -427,26 +423,15 @@ class CloudInit:
if ctype == "__begin__": return
filename=filename.replace(os.sep,'_')
+ payload = util.dos2unix(payload)
prefix="#cloud-boothook"
- dos=False
start = 0
if payload.startswith(prefix):
- start = len(prefix)
- if payload[start] == '\r':
- start=start+1
- dos = True
- else:
- if payload.find('\r\n',0,100) >= 0:
- dos = True
-
- if dos:
- payload=payload[start:].replace('\r\n','\n')
- elif start != 0:
- payload=payload[start:]
+ start = len(prefix) + 1
boothooks_dir = self.get_ipath("boothooks")
filepath = "%s/%s" % (boothooks_dir,filename)
- util.write_file(filepath, payload, 0700)
+ util.write_file(filepath, payload[start:], 0700)
try:
env=os.environ.copy()
env['INSTANCE_ID']= self.datasource.get_instance_id()
diff --git a/cloudinit/boto_utils.py b/cloudinit/boto_utils.py
index b38483fa..a2cb9ca6 100644
--- a/cloudinit/boto_utils.py
+++ b/cloudinit/boto_utils.py
@@ -60,7 +60,7 @@ def retry_url(url, retry_on_404=True):
sys.stderr.write('Caught exception reading instance data, giving up\n')
return ''
-def get_instance_metadata(version='latest'):
+def get_instance_metadata(version='latest',url='http://169.254.169.254'):
"""
Returns the instance metadata as a nested Python dictionary.
Simple values (e.g. local_hostname, hostname, etc.) will be
@@ -68,11 +68,11 @@ def get_instance_metadata(version='latest'):
be stored in the dict as a list of string values. More complex
fields such as public-keys and will be stored as nested dicts.
"""
- url = 'http://169.254.169.254/%s/meta-data/' % version
+ url = '%s/%s/meta-data/' % (url,version)
return _get_instance_metadata(url)
-def get_instance_userdata(version='latest', sep=None):
- url = 'http://169.254.169.254/%s/user-data' % version
+def get_instance_userdata(version='latest', sep=None,url='http://169.254.169.254'):
+ url = '%s/%s/user-data' % (url,version)
user_data = retry_url(url, retry_on_404=False)
if user_data:
if sep:
diff --git a/cloudinit/util.py b/cloudinit/util.py
index fc4233de..8f6a6b0d 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -389,3 +389,9 @@ def shellify(cmdlist):
else:
content="%s%s\n" % ( content, str(args) )
return content
+
+def dos2unix(input):
+ # find first end of line
+ pos = input.find('\n')
+ if pos <= 0 or input[pos-1] != '\r': return(input)
+ return(input.replace('\r\n','\n'))