diff options
author | Chris Moyer <kopertop@gmail.com> | 2011-07-01 12:11:22 -0700 |
---|---|---|
committer | Chris Moyer <kopertop@gmail.com> | 2011-07-01 12:11:22 -0700 |
commit | 320e1e4b5d73824087278e0ceb901b00935aaa02 (patch) | |
tree | 5b287cfbc2da1383721babcbd9b3bcfdd1b2d093 | |
parent | 7470e4b9490747d1885efc30f319f1849db59129 (diff) | |
parent | a833a5b9bc1f7917ad753fb6b48f4c24101290e1 (diff) | |
download | boto-320e1e4b5d73824087278e0ceb901b00935aaa02.tar.gz |
Merge pull request #244 from ryansb/master
Made the -s option accept a comma-separated list and allowed #import in scripts
-rwxr-xr-x | bin/launch_instance | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/bin/launch_instance b/bin/launch_instance index 3a6ea849..cfcc1e94 100755 --- a/bin/launch_instance +++ b/bin/launch_instance @@ -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,32 @@ 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) + print SCRIPTS + 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,7 +133,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", "--add-script", help="Pass in a script or a folder containing scripts to be run when the instance starts up, assumes cloud-init. If multiple scripts are specified, they are run in the order they were specified. If they are in a folder, they are run in alphabetical order", default=None, dest="scripts", action="append") + 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() @@ -159,6 +185,7 @@ 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 = [] @@ -178,9 +205,8 @@ if __name__ == "__main__": except OSError: scr_url = "file://%s" % scr_url try: - script_content = boto.utils.fetch_file(scr_url) - scriptuples.append((scr, script_content.read())) - except ValueError: + scriptuples.append((scr, add_script(scr_url))) + except Exception, e: pass user_data = boto.utils.write_mime_multipart(scriptuples, compress=False) |