Index: bin/launch_instance |
diff --git a/bin/launch_instance b/bin/launch_instance |
index cb857d9282dff6d1479e79faa3139a3499934e0e..53032ad7a20a29d744e7c9930fad591fd7e0ab42 100755 |
--- a/bin/launch_instance |
+++ b/bin/launch_instance |
@@ -31,7 +31,7 @@ f.write(\"\"\"%s\"\"\") |
f.close() |
""" |
import boto.pyami.config |
-from boto.utils import fetch_file |
+import boto.utils |
import re, os |
import ConfigParser |
@@ -55,7 +55,7 @@ class Config(boto.pyami.config.Config): |
file_url = os.path.join(os.getcwd(), file_url) |
file_url = "file://%s" % file_url |
(base_url, file_name) = file_url.rsplit("/", 1) |
- base_config = fetch_file(file_url) |
+ base_config = boto.utils.fetch_file(file_url) |
base_config.seek(0) |
for line in base_config.readlines(): |
match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line) |
@@ -71,7 +71,7 @@ class Config(boto.pyami.config.Config): |
self.set('Credentials', 'aws_access_key_id', ec2.aws_access_key_id) |
self.set('Credentials', 'aws_secret_access_key', ec2.aws_secret_access_key) |
- |
+ |
def __str__(self): |
"""Get config as string""" |
from StringIO import StringIO |
@@ -79,6 +79,31 @@ class Config(boto.pyami.config.Config): |
self.write(s) |
return s.getvalue() |
+SCRIPTS = [] |
+ |
+def scripts_callback(option, opt, value, parser): |
+ arg = value.split(',') |
+ if len(arg) == 1: |
+ SCRIPTS.append(arg[0]) |
+ else: |
+ SCRIPTS.extend(arg) |
+ setattr(parser.values, option.dest, SCRIPTS) |
+ |
+def add_script(scr_url): |
+ """Read a script and any scripts that are added using #import""" |
+ base_url = '/'.join(scr_url.split('/')[:-1]) + '/' |
+ script_raw = boto.utils.fetch_file(scr_url) |
+ script_content = '' |
+ for line in script_raw.readlines(): |
+ match = re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line) |
+ #if there is an import |
+ if match: |
+ #Read the other script and put it in that spot |
+ script_content += add_script("%s/%s" % (base_url, match.group(1))) |
+ else: |
+ #Otherwise, add the line and move on |
+ script_content += line |
+ return script_content |
if __name__ == "__main__": |
try: |
@@ -107,6 +132,7 @@ if __name__ == "__main__": |
parser.add_option("-w", "--wait", help="Wait until instance is running", default=False, action="store_true", dest="wait") |
parser.add_option("-d", "--dns", help="Returns public and private DNS (implicates --wait)", default=False, action="store_true", dest="dns") |
parser.add_option("-T", "--tag", help="Set tag", default=None, action="append", dest="tags", metavar="key:value") |
+ parser.add_option("-s", "--scripts", help="Pass in a script or a folder containing scripts to be run when the instance starts up, assumes cloud-init. Specify scripts in a list specified by commas. If multiple scripts are specified, they are run lexically (A good way to ensure they run in the order is to prefix filenames with numbers)", type='string', action="callback", callback=scripts_callback) |
(options, args) = parser.parse_args() |
@@ -158,8 +184,32 @@ if __name__ == "__main__": |
# If it's a cloud init AMI, |
# then we need to wrap the config in our |
# little wrapper shell script |
+ |
if options.cloud_init: |
user_data = CLOUD_INIT_SCRIPT % user_data |
+ scriptuples = [] |
+ if options.scripts: |
+ scripts = options.scripts |
+ scriptuples.append(('user_data', user_data)) |
+ for scr in scripts: |
+ scr_url = scr |
+ if not re.match("^([a-zA-Z0-9]*:\/\/)(.*)", scr_url): |
+ if not scr_url.startswith("/"): |
+ scr_url = os.path.join(os.getcwd(), scr_url) |
+ try: |
+ newfiles = os.listdir(scr_url) |
+ for f in newfiles: |
+ #put the scripts in the folder in the array such that they run in the correct order |
+ scripts.insert(scripts.index(scr) + 1, scr.split("/")[-1] + "/" + f) |
+ except OSError: |
+ scr_url = "file://%s" % scr_url |
+ try: |
+ scriptuples.append((scr, add_script(scr_url))) |
+ except Exception, e: |
+ pass |
+ |
+ user_data = boto.utils.write_mime_multipart(scriptuples, compress=True) |
+ |
shutdown_proc = "terminate" |
if options.save_ebs: |
shutdown_proc = "save" |
@@ -194,4 +244,3 @@ if __name__ == "__main__": |
if options.dns: |
print "Public DNS name: %s" % instance.public_dns_name |
print "Private DNS name: %s" % instance.private_dns_name |
- |