def load_command(db=None, config=None, clear=True): """ After using init you can use this to load a config: m2sh load -db config.sqlite -config tests/sample_conf.py This will erase the previous config, but we'll make it safer later on. """ import imp if not (os.path.isfile(db) and os.access(db, os.R_OK)): print("Cannot access database file %s" % db) return try: model.begin(db, clear=clear) imp.load_source('mongrel2_config_main', config) commit_command(db=db, what="load_command", why=" ".join(sys.argv)) except OperationalError as exc: print("SQLite error: %s" % exc) except SyntaxError as exc: print("Syntax error: %s" % exc)
def dump_command(db=None): """ Simple dump of a config database: m2sh dump -db config.sqlite """ print "LOADING DB: ", db try: if not (os.path.isfile(db) and os.access(db, os.R_OK)): raise IOError store = model.begin(db) servers = store.find(model.Server) for server in servers: print server for host in server.hosts: print "\t", host for route in host.routes: print "\t\t", route except IOError: print "%s not readable" % db except OperationalError, exc: print "SQLite error: %s" % exc
def find_servers(db=None, uuid="", host="", name="", every=False): """ Finds all the servers which match the given uuid, host or name. If every is true all servers in the database will be returned. """ store = model.begin(db) servers = [] if every: servers = store.find(model.Server) elif uuid: servers = store.find(model.Server, model.Server.uuid == unicode(uuid)) elif host: servers = store.find(model.Server, model.Server.default_host == unicode(host)) elif name: servers = store.find(model.Server, model.Server.name == unicode(name)) if servers.count() > 1 and not every: print "Not sure which server to run, what I found:" print "NAME HOST UUID" print "--------------" for server in servers: print server.name, server.default_host, server.uuid print "* Use -every to run them all." return [] else: return servers
def dump_command(db=None): """ Simple dump of a config database: m2sh dump -db config.sqlite """ print("LOADING DB: ", db) try: if not (os.path.isfile(db) and os.access(db, os.R_OK)): raise IOError store = model.begin(db) servers = store.find(model.Server) for server in servers: print(server) for host in server.hosts: print("\t", host) for route in host.routes: print("\t\t", route) except IOError: print("%s not readable" % db) except OperationalError as exc: print("SQLite error: %s" % exc)
def find_servers(db=None, uuid="", host="", name="", every=False): """ Finds all the servers which match the given uuid, host or name. If every is true all servers in the database will be returned. """ store = model.begin(db) servers = [] if every: servers = store.find(model.Server) elif uuid: servers = store.find(model.Server, model.Server.uuid == str(uuid)) elif host: servers = store.find(model.Server, model.Server.default_host == str(host)) elif name: servers = store.find(model.Server, model.Server.name == str(name)) if servers.count() > 1 and not every: print("Not sure which server to run, what I found:") print("NAME HOST UUID") print("--------------") for server in servers: print(server.name, server.default_host, server.uuid) print("* Use -every to run them all.") return [] else: return servers
def load_command(db=None, config=None, clear=True): """ After using init you can use this to load a config: m2sh load -db config.sqlite -config tests/sample_conf.py This will erase the previous config, but we'll make it safer later on. """ import imp if not (os.path.isfile(db) and os.access(db, os.R_OK)): print "Cannot access database file %s" % db return try: model.begin(db, clear=clear) imp.load_source("mongrel2_config_main", config) commit_command(db=db, what="load_command", why=" ".join(sys.argv)) except OperationalError, exc: print "SQLite error: %s" % exc
def get_pub_sub(self, route, db=None): if db is None: db = 'config.sqlite' store = model.begin(db) routes = store.find(model.Route, model.Route.path == unicode(route)) if routes.count() == 1: target = routes.one().target try: return target.send_spec, target.recv_spec except AttributeError: raise Exception('route.target is not a mongrel2 handler: {0}' .format(target)) else: raise Exception('route is not unique: {0}'.format(', '.join()))
def hosts_command(db=None, uuid="", host="", name=""): """ List all the hosts in the given server identified by UUID or host. m2sh hosts -db config.sqlite -uuid f400bf85-4538-4f7a-8908-67e313d515c2 m2sh hosts -db config.sqlite -host localhost m2sh hosts -db config.sqlite -name test The -host parameter is the default_host for the server. """ if not (os.path.isfile(db) and os.access(db, os.R_OK)): print "Cannot read database file %s" % db return try: store = model.begin(db) results = None if uuid: results = store.find(model.Server, model.Server.uuid == unicode(uuid)) elif host: results = store.find(model.Server, model.Server.default_host == unicode(host)) elif name: results = store.find(model.Server, model.Server.name == unicode(name)) else: print "ERROR: Must give a -host or -uuid or -name." return if results.count(): server = results[0] hosts = store.find(model.Host, model.Host.server_id == server.id) for host in hosts: print "--------" print host, ":" for route in host.routes: print "\t", route.path, ':', route.target else: print "No servers found." except OperationalError, exc: print "SQLite error: %s" % exc
def servers_command(db=None): """ Lists the servers that are configured in this setup: m2sh servers -db config.sqlite """ if not os.path.isfile(db): print "ERROR: Cannot access database file %s" % db return try: store = model.begin(db) servers = store.find(model.Server) for server in servers: print "-------" print server.name, server.default_host, server.uuid for host in server.hosts: print "\t", host.id, ":", host.name except OperationalError, exc: print "SQLite error: %s" % exc
def servers_command(db=None): """ Lists the servers that are configured in this setup: m2sh servers -db config.sqlite """ if not os.path.isfile(db): print("ERROR: Cannot access database file %s" % db) return try: store = model.begin(db) servers = store.find(model.Server) for server in servers: print("-------") print(server.name, server.default_host, server.uuid) for host in server.hosts: print("\t", host.id, ':', host.name) except OperationalError as exc: print("SQLite error: %s" % exc)