Coder Social home page Coder Social logo

tunneler's Introduction

tunneler: Connect to databases via ssh tunnel

A command tool and library to connect to databases with minimal fuss.

To install:

$ pip install tunneler

You will probably also need the database drivers for your preferred database. To install the postgres ones automatically when you install tunneler, do this:

$ pip install tunneler[pg]

To run:

$ tunneler name_of_task name_of_connection

As the above command shows, there are two concepts to understand when running tunneler, connections and tasks.

Define connections in a connections.yaml file in your current directory, as follows:

prod:
  remote_host: publicjumpboxservername
  ssh_username: username
  ssh_pkey: |
    -----BEGIN RSA PRIVATE KEY-----
    .......................................
    .......................................
    -----END RSA PRIVATE KEY-----
  private_dburl: postgres://username:password@privatedbservername/databasename
  local_port: 5433

dev:
  .....

Tunneler will use this information to set up a local tunneled post where you can access this database directly.

A connection URL to this local port will then be provided to each task.

There are three inbuilt tasks:

  • test_connection (run this one to check your config is correct)
  • do_full_pg_dump (assumes you have pg_dump installed)
  • do_schema_pg_dump (also assumes you have pg_dump installed)

So for example to dump the production schema we'd run the following command:

$ tunneler do_schema_pg_dump prod

Custom tasks

Want to define your own tasks? Simply create a tasks.py file (or module) in the current directory.

Define a top level method in this file that accepts a connection url, and that method name will be available as a task.

For instance, the test connection task could be re-implemented as follows:

from sqlbag import S

def test_connection_custom(dburl):
    with S(dburl) as s:
        s.execute('select 1')

You'd then run this as follows:

$ tunneler test_connection_custom prod

Pretty simple.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.