def setup_boot_items(): boot_items = "\n".join([ '', '[program:cassandra]', 'command=/home/cstar/fab/cassandra/bin/cassandra -f', 'priority=1', 'user=cstar', 'autostart=true', 'autorestart=false', 'redirect_stderr=true', '', '[program:cstar_perf_notifications]', 'command=cstar_perf_notifications -F', 'priority=1', 'user=cstar', 'autostart=true', 'autorestart=true', 'startretries=30', 'redirect_stderr=true', '', '[program:cstar_perf_server]', 'command=cstar_perf_server', 'priority=2', 'user=cstar', 'environment=HOME=/home/cstar', 'autostart=true', 'startretries=30', 'autorestart=true', 'redirect_stderr=true', '' ]) fab_append("/supervisord.conf", boot_items)
def run_python_script(script): fab.run("rm -f ~/pyscript.py") fab_append("pyscript.py", textwrap.dedent(script)) output = fab.run("python pyscript.py") fab.run("rm ~/pyscript.py") return output
def write_template(filename, context, tpl_str=None, tpl_file=None, remote_tpl_file=None, append=False, mark=None, use_sudo=False): """ Render template and write output to file @param filename: File to write @param template: Name of template @param context: Dictionary for the template context (usually just env) @param append: bool, True if you want to append content to file instead of overwriting. @param mark: str, If append is true and mark is defined, then the rendered template will replace any previous appened version. """ # If we're in local mode, interpret remote_tpl_file as it was a local file. if is_local() and remote_tpl_file: tpl_file = remote_tpl_file remote_tpl_file = None if remote_tpl_file: f = StringIO() get(remote_tpl_file, f) tpl = env.jinja.from_string(f.getvalue()) f.close() elif tpl_file: try: tpl = env.jinja.get_template(tpl_file) except TemplateNotFound: jinja = Environment(loader=FileSystemLoader([os.path.dirname(tpl_file)])) tpl = jinja.get_template(os.path.basename(tpl_file)) elif tpl_str: tpl = env.jinja.from_string(tpl_str) content = tpl.render(context) if append and mark is not None: start_mark = "### START: %s ###" % mark end_mark = "### END: %s ###" % mark marked_content = """%s\n%s\n%s\n""" % (start_mark, content, end_mark) file_content = [] skip = False added = False if is_local(): f = open(filename, 'r') else: f = StringIO() get(filename, f) for line in f: if line == start_mark: skip = True added = True file_content.append(marked_content) elif line == end_mark: skip = False else: if not skip: file_content.append(line) f.close() if added: append = False content = "\n".join(file_content) else: content = marked_content if is_local(): f = open(filename, 'a' if append else 'w') f.write(content.encode('utf8')) f.close() else: if append: fab_append(filename, content.encode('utf8'), use_sudo=use_sudo) else: put(StringIO(content.encode('utf8')), filename, use_sudo=use_sudo)