From 57ea45b2bc86895582de65928c555e6f0430b287 Mon Sep 17 00:00:00 2001 From: Marc Cluet Date: Mon, 25 Jul 2011 13:27:27 +0100 Subject: Added new feature include-once --- cloudinit/UserDataHandler.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'cloudinit/UserDataHandler.py') diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index 83377dab..4fd6ef28 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -25,6 +25,7 @@ import yaml starts_with_mappings={ '#include' : 'text/x-include-url', + '#include-once' : 'text/x-include-once-url', '#!' : 'text/x-shellscript', '#cloud-config' : 'text/cloud-config', '#upstart-job' : 'text/upstart-job', @@ -45,16 +46,34 @@ def decomp_str(str): def do_include(str,parts): import urllib + import os + import base64 # is just a list of urls, one per line # also support '#include ' + includeonce = False for line in str.splitlines(): if line == "#include": continue - if line.startswith("#include"): + if line == "#include-once": + includeonce == True + if line.startswith("#include-once"): + line = line[len("#include-once"):].lstrip() + includeonce = True + elif line.startswith("#include"): line = line[len("#include"):].lstrip() if line.startswith("#"): continue - content = urllib.urlopen(line).read() + if includeonce == True: + uniquestring = base64.encodestring(line).strip('\n') + includeonce_filename = "/var/lib/cloud/instance/.includeonce.%s" % uniquestring + if os.path.isfile(includeonce_filename): continue + includeonce_file = open(includeonce_filename,'w') + includeonce_file.close() + try: + content = urllib.urlopen(line).read() + except Exception as e: + log.debug(traceback.format_exc(e)) process_includes(email.message_from_string(decomp_str(content)),parts) + def explode_cc_archive(archive,parts): for ent in yaml.load(archive): # ent can be one of: -- cgit v1.2.3 From a6dce5ac548de073918d679503f447d265847066 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 26 Jul 2011 10:22:23 -0400 Subject: make 'include-once' really "download source once per-instance" Marc's implementation would only ever process the include-once urls a single time. This changes that to process them every time, with the second time coming from a file on disk rather than the url. You can then do expiring or one time use URLs in the include-once and have all function of if the content was there every time. The cached file is readable by root-only. --- cloudinit/UserDataHandler.py | 20 +++++++++++++------- doc/userdata.txt | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'cloudinit/UserDataHandler.py') diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index 4fd6ef28..4ac0e2cd 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -22,6 +22,7 @@ from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders import yaml +from cloudinit import util, get_ipath_cur starts_with_mappings={ '#include' : 'text/x-include-url', @@ -61,16 +62,21 @@ def do_include(str,parts): elif line.startswith("#include"): line = line[len("#include"):].lstrip() if line.startswith("#"): continue - if includeonce == True: - uniquestring = base64.encodestring(line).strip('\n') - includeonce_filename = "/var/lib/cloud/instance/.includeonce.%s" % uniquestring - if os.path.isfile(includeonce_filename): continue - includeonce_file = open(includeonce_filename,'w') - includeonce_file.close() + + # urls cannot not have leading or trailing white space + uniquestring = base64.encodestring(line).strip() + includeonce_filename = "%/urlcache/%s" % (get_ipath_cur("data"), uniquestring) try: - content = urllib.urlopen(line).read() + if includeonce and os.path.isfile(includeonce_filename): + with open(includeonce_filename, "r") as fp: + content = fp.read() + else: + content = urllib.urlopen(line).read() + if includeonce: + util.write_file(includeonce_filename, content, mode=0600) except Exception as e: log.debug(traceback.format_exc(e)) + process_includes(email.message_from_string(decomp_str(content)),parts) diff --git a/doc/userdata.txt b/doc/userdata.txt index 3af1e632..cc691ae6 100644 --- a/doc/userdata.txt +++ b/doc/userdata.txt @@ -42,7 +42,9 @@ finds. However, certain types of user-data are handled specially. urls, one per line. Each of the URLs will be read, and their content will be passed through this same set of rules. Ie, the content read from the URL can be gzipped, mime-multi-part, or plain text - This file will just be processed once by cloud-init + This file will just be downloaded only once per instance, and its + contents cached for subsequent boots. This allows you to pass in + one-time-use or expiring URLs. * Cloud Config Data begins with #cloud-config or Content-Type: text/cloud-config -- cgit v1.2.3 From cf65c6c8e0da58698139223888f23adf7093d12f Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 26 Jul 2011 13:54:56 -0400 Subject: include-once: fixups found in testing at this point, the following user-data file in /var/lib/cloud/seed/nocloud-net/user-data will do what you would expect: $ cat > /var/lib/cloud/seed/nocloud-net/user-data < Date: Tue, 26 Jul 2011 14:04:02 -0400 Subject: use md5sum as the unique identifier rather than base64 base64 encode will grow with the size of the url, possibly resulting in silly-long filenames. md5sum will keep it to a constant length. --- cloudinit/UserDataHandler.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'cloudinit/UserDataHandler.py') diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index fa8ce716..9670c0cb 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -24,6 +24,7 @@ from email import encoders import yaml import cloudinit import cloudinit.util as util +import md5 starts_with_mappings={ '#include' : 'text/x-include-url', @@ -49,7 +50,6 @@ def decomp_str(str): def do_include(str,parts): import urllib import os - import base64 # is just a list of urls, one per line # also support '#include ' includeonce = False @@ -66,8 +66,10 @@ def do_include(str,parts): if line.startswith("#"): continue # urls cannot not have leading or trailing white space - uniquestring = base64.encodestring(line).strip() - includeonce_filename = "%s/urlcache/%s" % (cloudinit.get_ipath_cur("data"), uniquestring) + msum = md5.new() + msum.update(line.strip()) + includeonce_filename = "%s/urlcache/%s" % ( + cloudinit.get_ipath_cur("data"), msum.hexdigest()) try: if includeonce and os.path.isfile(includeonce_filename): with open(includeonce_filename, "r") as fp: -- cgit v1.2.3