Skip to content

Instantly share code, notes, and snippets.

@whylom
whylom / gist:2322771
Created April 6, 2012 20:39
How to access a remote MySQL server via an SSH tunnel

For security reasons, many hosting providers restrict external access to the default MySQL port 3306. But if you have SSH access to the database server, you can access the MySQL server from your local machine by using an SSH tunnel. An SSH tunnel forwards bits back & forth between a port on your machine and a port on the remote machine, all lovingly encrypted with SSH.

Here's how you do it! But first, my assumptions:

  1. You already have configured SSH access to the remote server.
  2. You want to access port 3306 on the remote server.
  3. You want to access port 3307 on your local machine. This is simply because many devs (myself included) routinely have MySQL running on port 3307.

Here is the Unix command to open an SSH tunnel to hostname_or_ip with the user account username

@whylom
whylom / sync.rake
Last active March 22, 2016 19:44
Rake task to copy assets between S3 buckets. Requires the aws-s3 gem (http://amazon.rubyforge.org/)
# add a new "put_copy" method to the Amazon client's S3Object class
# to enable copying an object from 1 bucket to another
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
class AWS::S3::S3Object
def self.put_copy(source_key, target_key, source_bucket, target_bucket, options = {})
original = open(url_for(source_key, source_bucket))
default_options = { :content_type => original.content_type }
store(target_key, original, target_bucket, default_options.merge(options))
acl(target_key, target_bucket, acl(source_key, source_bucket))
end
@whylom
whylom / to_hash.rb
Last active December 25, 2015 20:19
Array#to_hash converts an array of instances into a hash using the provided pair of methods to determine the key & value of each row
# example class
class State < Struct.new(:name, :code)
def self.all
[
State.new('Alabama', 'AL'),
State.new('Alaska', 'AK'),
State.new('Arizona', 'AZ')
]
end
end
@whylom
whylom / .powrc
Created September 26, 2013 15:47
.powrc for RVM gemset
if [ -f "$rvm_path/scripts/rvm" ]; then
source "$rvm_path/scripts/rvm"
rvm use $(cat .ruby-version)@$(cat .ruby-gemset)
fi
@whylom
whylom / README.md
Last active December 17, 2015 19:49
Ruby script that enhances the standard `git branch` listing by organizing and color coding remotes (origin, root, etc).

This is a Ruby script that enhances the standard git branch listing by organizing and color coding remotes (origin, root, etc).

screenshot

  • Branches whose remote is origin (the default) are listed at the top.
  • Branches whose remote is not origin are listed next, with the remote displayed in magenta. This makes it easier to see branches that are shared with other developers.
  • A hardcoded list of staging branches are displayed at the bottom in gold.
@whylom
whylom / trick.rb
Created January 17, 2013 19:28
Neat Sinatra trick
set(:probability) { |value| condition { rand <= value } }
get '/win_a_car', :probability => 0.1 do
"You won!"
end
get '/win_a_car' do
"Sorry, you lost."
end
@whylom
whylom / gist:4383284
Created December 26, 2012 21:29
How many commits did we push to master in 2012?

Get the first commit of 2012

$ git log --pretty=format:'%H' --after="2011-12-31" | tail -1
d41d8cd98f00b204e9800998ecf8427e

Count the number of commits since then (inclusive)

$ git rev-list d41d8cd98f00b204e9800998ecf8427e^..HEAD | wc -l 
44555
@whylom
whylom / gist:3018099
Created June 29, 2012 13:58
git purge (delete unmerged branches starting with "_")

When using Git, I find myself doing a lot of what I call "exploratory merging" into a throwaway local branch. This is to preview what a scary merge might look like master, or to isolate a bug in a staging environment by selectively merging in certain of the branches that have been deployed.

The names of my throwaway branches always start with an underscore, so I can easily see them when I git branch. Below is a Bash one-liner (aliased as git purge) that deletes unmerged branches whose names start with "_".

[alias]
  purge   = !git branch --no-merged | tr -d "* " | grep ^_ | xargs git branch -D
@whylom
whylom / README.md
Created April 23, 2012 18:01
How to install Graphite on Vagrant with Chef

Here are some quick notes on installing Graphite on a Vagrant virtual machine using Chef recipes from GitHub. This more or less assumes you know what you're doing with Vagrant and Chef.

Notes

  • I used the "lucid32" Ubuntu base box you see in most Vagrant examples (http://files.vagrantup.com/lucid32.box)
  • Where possible, I used the cookbooks from Opscode. I was forced to use alternatives for Python (did not work for me) and for Graphite (no Opscode cookbook as of now).
  • The Graphite recipe depends on the Python, Apache2, and runit recipes.
  • I'm running the apt recipe first to update the Ubuntu package manager. The out-of-date manager that ships with the lucid32 box errored out for me when running the Python recipe.

Cookbooks Used

@whylom
whylom / gist:2432236
Created April 20, 2012 22:07
leggo: idea for a utility to kill a process that's using a specific port
$ leggo 3306
Port 3306 is being used by "mysqld"
process 66117 | started 4:53PM | up for 0:01.25
/usr/local/Cellar/mysql/5.5.20/bin/mysqld
--basedir=/usr/local/Cellar/mysql/5.5.20
--datadir=/usr/local/var/mysql
--plugin-dir=/usr/local/Cellar/mysql/5.5.20/lib/plugin