summaryrefslogtreecommitdiff
path: root/cloudinit/transforms/rightscale_userdata.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-16 12:14:54 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-16 12:14:54 -0700
commit75b46cc80c6a9ab65c3c7f430ffaa4157d0db369 (patch)
tree710f157ce2bdd3cd4c593ef2f726dd6eaa177a51 /cloudinit/transforms/rightscale_userdata.py
parent68e710207900ff8d933da6f79adc84bce980685d (diff)
downloadvyos-cloud-init-75b46cc80c6a9ab65c3c7f430ffaa4157d0db369.tar.gz
vyos-cloud-init-75b46cc80c6a9ab65c3c7f430ffaa4157d0db369.zip
Remove the need to have transforms start with "cc_".
1. Just let them have no prefix (ie blank) unless this isn't wanted later.
Diffstat (limited to 'cloudinit/transforms/rightscale_userdata.py')
-rw-r--r--cloudinit/transforms/rightscale_userdata.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/cloudinit/transforms/rightscale_userdata.py b/cloudinit/transforms/rightscale_userdata.py
new file mode 100644
index 00000000..40d76c89
--- /dev/null
+++ b/cloudinit/transforms/rightscale_userdata.py
@@ -0,0 +1,102 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2011 Canonical Ltd.
+# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
+#
+# Author: Scott Moser <scott.moser@canonical.com>
+# Author: Juerg Haefliger <juerg.haefliger@hp.com>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+##
+## The purpose of this script is to allow cloud-init to consume
+## rightscale style userdata. rightscale user data is key-value pairs
+## in a url-query-string like format.
+##
+## for cloud-init support, there will be a key named
+## 'CLOUD_INIT_REMOTE_HOOK'.
+##
+## This cloud-config module will
+## - read the blob of data from raw user data, and parse it as key/value
+## - for each key that is found, download the content to
+## the local instance/scripts directory and set them executable.
+## - the files in that directory will be run by the user-scripts module
+## Therefore, this must run before that.
+##
+##
+
+import os
+
+from cloudinit import url_helper as uhelp
+from cloudinit import util
+from cloudinit.settings import PER_INSTANCE
+
+from urlparse import parse_qs
+
+frequency = PER_INSTANCE
+
+my_name = "cc_rightscale_userdata"
+my_hookname = 'CLOUD_INIT_REMOTE_HOOK'
+
+
+def handle(name, _cfg, cloud, log, _args):
+ try:
+ ud = cloud.get_userdata_raw()
+ except:
+ log.warn("Failed to get raw userdata in transform %s", name)
+ return
+
+ try:
+ mdict = parse_qs(ud)
+ if not mdict or not my_hookname in mdict:
+ log.debug(("Skipping transform %s, "
+ "did not find %s in parsed"
+ " raw userdata"), name, my_hookname)
+ return
+ except:
+ util.logexc(log, ("Failed to parse query string %s"
+ " into a dictionary"), ud)
+ raise
+
+ wrote_fns = []
+ captured_excps = []
+
+ # These will eventually be then ran by the cc_scripts_user
+ # TODO: maybe this should just be a new user data handler??
+ # Instead of a late transform that acts like a user data handler?
+ scripts_d = cloud.get_ipath_cur('scripts')
+ urls = mdict[my_hookname]
+ for (i, url) in enumerate(urls):
+ fname = os.path.join(scripts_d, "rightscale-%02i" % (i))
+ try:
+ (content, st) = uhelp.readurl(url)
+ # Ensure its a valid http response (and something gotten)
+ if uhelp.ok_http_code(st) and content:
+ util.write_file(fname, content, mode=0700)
+ wrote_fns.append(fname)
+ except Exception as e:
+ captured_excps.append(e)
+ util.logexc(log, "%s failed to read %s and write %s",
+ my_name, url, fname)
+
+ if wrote_fns:
+ log.debug("Wrote out rightscale userdata to %s files", len(wrote_fns))
+
+ if len(wrote_fns) != len(urls):
+ skipped = len(urls) - len(wrote_fns)
+ log.debug("%s urls were skipped or failed", skipped)
+
+ if captured_excps:
+ log.warn("%s failed with exceptions, re-raising the last one",
+ len(captured_excps))
+ raise captured_excps[-1]