def cmd_do(self, argv, help): """Do stuff on the cluster (using fabric)""" parser = argparse.ArgumentParser( prog="aws do", description=help, add_help=False, ) parser.add_argument("server", nargs=1, metavar="server", help="Name of the instance or server from the config.", choices=list(self.ec2.all)) parser.add_argument("...", nargs=argparse.REMAINDER, help="Fabric options") if len(argv) < 2: parser.print_help() return old_sys_argv = sys.argv old_cwd = os.getcwd() import fabric_integration # this needs to be done before any other fabric module import fabric_integration.patch() import fabric.state import fabric.main hoststr = None try: fabric_integration.ec2 = self.ec2 fabric_integration.log = log hoststr = argv[0] server = self.ec2.all[hoststr] # prepare the connection fabric.state.env.reject_unknown_hosts = True fabric.state.env.disable_known_hosts = True fabfile = server.config.get('fabfile') if fabfile is None: log.error("No fabfile declared.") return newargv = ['fab', '-H', hoststr, '-r', '-D'] if fabfile is not None: newargv = newargv + ['-f', fabfile] sys.argv = newargv + argv[1:] # setup environment os.chdir(os.path.dirname(fabfile)) fabric.state.env.servers = self.ec2.all fabric.state.env.server = server known_hosts = self.ec2.known_hosts fabric.state.env.known_hosts = known_hosts class StdFilter(object): def __init__(self, org): self.org = org self.flush = self.org.flush def write(self, msg): lines = msg.split('\n') prefix = '[%s] ' % fabric.state.env.host_string for index, line in enumerate(lines): if line.startswith(prefix): lines[index] = line[len(prefix):] self.org.write('\n'.join(lines)) sys.stdout = StdFilter(sys.stdout) sys.stderr = StdFilter(sys.stderr) fabric.main.main() finally: if fabric.state.connections.opened(hoststr): fabric.state.connections[hoststr].close() sys.argv = old_sys_argv os.chdir(old_cwd)
except KeyError,e: log.error("Server not found %s", argv[0]) parser.parse_args([argv[0]]) return if tmpserver is not None: instance = tmpserver.instance if instance is None: return if instance.state != 'running': log.info("Instance state: %s", instance.state) return import fabric_integration # this needs to be done before any other fabric module import fabric_integration.patch() import fabric.state import fabric.main hoststr = None try: fabric_integration.ec2 = self.ec2 fabric_integration.log = log hoststr = argv[0] server = self.ec2.all[hoststr] # prepare the connection fabric.state.env.reject_unknown_hosts = True