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 +++++++++++++++++++++-- doc/examples/include-once.txt | 7 +++++++ doc/userdata.txt | 8 ++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 doc/examples/include-once.txt 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: diff --git a/doc/examples/include-once.txt b/doc/examples/include-once.txt new file mode 100644 index 00000000..0cf74e5e --- /dev/null +++ b/doc/examples/include-once.txt @@ -0,0 +1,7 @@ +#include-once +# entries are one url per line. comment lines beginning with '#' are allowed +# urls are passed to urllib.urlopen, so the format must be supported there +# This entries will just be processed ONE TIME by cloud-init, any further +# iterations won't process this file +http://www.ubuntu.com/robots.txt +http://www.w3schools.com/html/lastpage.htm diff --git a/doc/userdata.txt b/doc/userdata.txt index 00c16b25..3af1e632 100644 --- a/doc/userdata.txt +++ b/doc/userdata.txt @@ -36,6 +36,14 @@ finds. However, certain types of user-data are handled specially. 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 +* Include File Once + begins with #include-once or Content-Type: text/x-include-once-url + This content is a "include" file. The file contains a list of + 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 + * Cloud Config Data begins with #cloud-config or Content-Type: text/cloud-config -- cgit v1.2.3