Ejemplo n.º 1
0
 def startup_script(self, debug=False):
     startup_script_path = self.config.get('startup_script', None)
     if startup_script_path is None:
         return ''
     startup_script = template.Template(
         startup_script_path['path'],
         pre_filter=template.strip_hashcomments,
     )
     options = self.config.copy()
     options.update(dict(
         servers=self.ec2.all,
     ))
     result = startup_script(**options)
     if startup_script_path.get('gzip', False):
         result = "\n".join([
             "#!/bin/bash",
             "tail -n+4 $0 | gunzip -c | bash",
             "exit $?",
             gzip_string(result)
         ])
     if len(result) >= 16*1024:
         log.error("Startup script too big.")
         if not debug:
             sys.exit(1)
     return result
Ejemplo n.º 2
0
 def __call__(self, **kwargs):
     options = {}
     if callable(self.pre_filter):
         body = self.pre_filter(self.template.get_payload())
     else:
         body = self.template.get_payload()
     for key, value in self.template.items():
         commands, value = value.rsplit(None, 1)
         for cmd in commands.split(','):
             if cmd == 'file':
                 path = value
                 if not os.path.isabs(path):
                     path = os.path.join(os.path.dirname(self.path), path)
                 value = open(path).read()
             elif cmd == 'base64':
                 value = value.encode("base64")
             elif cmd == 'format':
                 value = value.format(**kwargs)
             elif cmd == 'template':
                 path = value
                 if not os.path.isabs(path):
                     path = os.path.join(os.path.dirname(self.path), path)
                 value = Template(path)(**kwargs)
             elif cmd == 'gzip':
                 value = gzip_string(value)
             elif cmd == 'escape_eol':
                 value = value.replace('\n', '\\n')
             else:
                 raise ValueError("Unknown command '%s' for option '%s' in startup script '%s'." % (cmd, key, self.path))
         options[key] = value
     for key in kwargs:
         options[key] = kwargs[key]
     result = body.format(**options)
     if callable(self.post_filter):
         result = self.post_filter(result)
     return result