def get_ssh(): global _ssh if not _ssh: _ssh = paramiko.SSHClient() _ssh.load_system_host_keys() _ssh.connect(setting('replica')) return _ssh
def sqlquery(query, file): '''Run the query over the read-replica DB. Store the results in a file Known issues: * The query is passed over a Unix commandline in single quotes. We should clean this up, but for now, don't use single quotes. * This code is *intentionally* *not* *secure*. It presumes you have ssh access to the replica. It should *not* be co-opted for secure applications. ''' ssh = get_ssh() command = "echo '{query}' | {replica}".format(query=query, replica=setting('replica-sql')) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command) with open(file, "w") as f: for x in ssh_stdout: f.write(x)
def populate_tables(): for query in queries: sqlquery(queries[query], os.path.join(setting("userinfo"), query+".csv"))