summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorBen Howard <ben.howard@canonical.com>2014-01-24 12:29:04 -0700
committerBen Howard <ben.howard@canonical.com>2014-01-24 12:29:04 -0700
commitfb55c1079375454d2a2a2f82c6c1812759eeb1f1 (patch)
tree38dea394d0ecd7a0969291fbf4a6d63778be1f5d /cloudinit
parent5aa7d4ccf984ac296f58fa355bdce17d175dcc7d (diff)
downloadvyos-cloud-init-fb55c1079375454d2a2a2f82c6c1812759eeb1f1.tar.gz
vyos-cloud-init-fb55c1079375454d2a2a2f82c6c1812759eeb1f1.zip
Fixes for SmartOS datasource (LP: #1272115):
1. fixed conflation of user-data and cloud-init user-data. Cloud-init user-data is now namespaced as 'cloud-init:user-data'. 2. user-scripts are now fetched from the meta-data service each boot and executed as in the scripts directory 3. datacenter name is now namespaced as sdc:datacenter 4. user-scripts should be shebanged if there is no file magic
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/sources/DataSourceSmartOS.py45
-rw-r--r--cloudinit/util.py72
2 files changed, 111 insertions, 6 deletions
diff --git a/cloudinit/sources/DataSourceSmartOS.py b/cloudinit/sources/DataSourceSmartOS.py
index 6593ce6e..6bd4a5c7 100644
--- a/cloudinit/sources/DataSourceSmartOS.py
+++ b/cloudinit/sources/DataSourceSmartOS.py
@@ -25,7 +25,9 @@
# requests on the console. For example, to get the hostname, you
# would send "GET hostname" on /dev/ttyS1.
#
-
+# Certain behavior is defined by the DataDictionary
+# http://us-east.manta.joyent.com/jmc/public/mdata/datadict.html
+# Comments with "@datadictionary" are snippets of the definition
import base64
from cloudinit import log as logging
@@ -43,10 +45,11 @@ SMARTOS_ATTRIB_MAP = {
'local-hostname': ('hostname', True),
'public-keys': ('root_authorized_keys', True),
'user-script': ('user-script', False),
- 'user-data': ('user-data', False),
+ 'legacy-user-data': ('user-data', False),
+ 'user-data': ('cloud-init:user-data', False),
'iptables_disable': ('iptables_disable', True),
'motd_sys_info': ('motd_sys_info', True),
- 'availability_zone': ('datacenter_name', True),
+ 'availability_zone': ('sdc:datacenter_name', True),
'vendordata': ('sdc:operator-script', False),
}
@@ -71,7 +74,11 @@ BUILTIN_DS_CONFIG = {
'seed_timeout': 60,
'no_base64_decode': ['root_authorized_keys',
'motd_sys_info',
- 'iptables_disable'],
+ 'iptables_disable',
+ 'user-data',
+ 'user-script',
+ 'sdc:datacenter_name',
+ ],
'base64_keys': [],
'base64_all': False,
'disk_aliases': {'ephemeral0': '/dev/vdb'},
@@ -88,6 +95,11 @@ BUILTIN_CLOUD_CONFIG = {
'device': 'ephemeral0'}],
}
+# @datadictionary: this is legacy path for placing files from metadata
+# per the SmartOS location. It is not preferable, but is done for
+# legacy reasons
+LEGACY_USER_D = "/var/db"
+
class DataSourceSmartOS(sources.DataSource):
def __init__(self, sys_cfg, distro, paths):
@@ -107,6 +119,9 @@ class DataSourceSmartOS(sources.DataSource):
self.smartos_no_base64 = self.ds_cfg.get('no_base64_decode')
self.b64_keys = self.ds_cfg.get('base64_keys')
self.b64_all = self.ds_cfg.get('base64_all')
+ self.script_base_d = os.path.join(self.paths.get_cpath("scripts"))
+ self.user_script_d = os.path.join(self.paths.get_cpath("scripts"),
+ 'per-boot')
def __str__(self):
root = sources.DataSource.__str__(self)
@@ -144,14 +159,32 @@ class DataSourceSmartOS(sources.DataSource):
smartos_noun, strip = attribute
md[ci_noun] = self.query(smartos_noun, strip=strip)
+ # @datadictionary: This key has no defined format, but its value
+ # is written to the file /var/db/mdata-user-data on each boot prior
+ # to the phase that runs user-script. This file is not to be executed.
+ # This allows a configuration file of some kind to be injected into
+ # the machine to be consumed by the user-script when it runs.
+ u_script = md.get('user-script')
+ u_script_f = "%s/99_user_script" % self.user_script_d
+ u_script_l = "%s/user-script" % LEGACY_USER_D
+ util.write_content(u_script, u_script_f, link=u_script_l,
+ executable=True)
+
+ # @datadictionary: This key may contain a program that is written
+ # to a file in the filesystem of the guest on each boot and then
+ # executed. It may be of any format that would be considered
+ # executable in the guest instance.
+ u_data = md.get('legacy-user-data')
+ u_data_f = "%s/mdata-user-data" % LEGACY_USER_D
+ util.write_content(u_data, u_data_f)
+
+ # Handle the cloud-init regular meta
if not md['local-hostname']:
md['local-hostname'] = system_uuid
ud = None
if md['user-data']:
ud = md['user-data']
- elif md['user-script']:
- ud = md['user-script']
self.metadata = util.mergemanydict([md, self.metadata])
self.userdata_raw = ud
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 77f9ab36..5f64cb69 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1904,3 +1904,75 @@ def expand_dotted_devname(dotted):
return toks
else:
return (dotted, None)
+
+
+def write_executable_content(script, script_f):
+ """
+ This writes executable content and ensures that the shebang
+ exists.
+ """
+ write_file(script_f, script, mode=0700)
+ try:
+ cmd = ["file", "--brief", "--mime-type", script_f]
+ (f_type, _err) = subp(cmd)
+
+ LOG.debug("script %s mime type is %s" % (script_f, f_type))
+
+ # if the magic is text/plain, re-write with the shebang
+ if f_type.strip() == "text/plain":
+ with open(script_f, 'w') as f:
+ f.write("#!/bin/bash\n")
+ f.write(script)
+ LOG.debug("added shebang to file %s" % script_f)
+
+ except ProcessExecutionError as e:
+ logexc(LOG, "Failed to identify script type for %s" % script_f, e)
+ return False
+
+ except IOError as e:
+ logexc(LOG, "Failed to add shebang to file %s" % script_f, e)
+ return False
+
+ return True
+
+
+def write_content(content, content_f, link=None,
+ executable=False):
+ """
+ Write the content to content_f. Under the following rules:
+ 1. Backup previous content_f
+ 2. Write the contente
+ 3. If no content, remove the file
+ 4. If there is a link, create it
+
+ @param content: what to write
+ @param content_f: the file name
+ @param backup_d: the directory to save the backup at
+ @param link: if defined, location to create a symlink to
+ @param executable: is the file executable
+ """
+
+ if content:
+ if not executable:
+ write_file(content_f, content, mode=0400)
+ else:
+ w = write_executable_content(content, content_f)
+ if not w:
+ LOG.debug("failed to write file to %s" % content_f)
+ return False
+
+ if not content and os.path.exists(content_f):
+ os.unlink(content_f)
+
+ if link:
+ try:
+ if os.path.islink(link):
+ os.unlink(link)
+ if content and os.path.exists(content_f):
+ ensure_dir(os.path.dirname(link))
+ os.symlink(content_f, link)
+ except IOError as e:
+ logexc(LOG, "failed establishing content link", e)
+ return False
+
+ return True