RarestBlog

Expose Local Port to Web

Since I always forget how to do this - here’s another cheatsheet to myself.

Assuming you have ssh access to some machine (and root rights or GatewayPorts) you can expose local port on your machine to the web.

First, turn on GatewayPorts for yourself:

ssh username@host
echo -e "\nMatch User `whoami`\n  GatewayPorts yes" | sudo tee -a /etc/ssh/sshd_config
sudo service ssh reload

Now, to expose the local port 3000 as hostname:3001 do:

ssh -R :3001:127.0.0.1:3000 user@hostname

When you open hostname:3001 you should see your local machine’s port 3000.

Most Popular Gems in Categories of Ruby Toolbox - March 2013

I always wanted to know most popular gems. Sadly Ruby-Toolbox does not provide the full rating (or I don’t see it). I’ve had half an hour and decided to scrape the categories. I didn’t scrape all gems, since that would have created thousands of unnecessary requests to Ruby-Toolbox, I’ve jus tmade 167 (the number of categories)

So, here they are Top-1000+ gems (only with categories)

JOIN Iterator in Ruby

I released a gem that implements something like a SQL join for sorted arrays in Ruby:

Array 1         Array 2
[ 1, 'a' ]      [ 1, 'b' ]
[ 2, 'c' ]      [ 5, 'd' ]
[ 5, 'e' ]

Result:

[ 1, ['a', 'b'] ]
[ 2, ['c', nil] ]
[ 5, ['e', 'd'] ]

Note that arrays must be sorted

Installation

gem install join-iterator

Source code

at GitHub

Simplest GitHub Hook

This allows you to pull the code into staging / production when somebody commits code to your git repo. Useful for teams, where not everybody has to have access to production machine.

Very compatible code (Ruby 1.8+), no dependencies on most modern Linux distros:

require 'webrick'

server = WEBrick::HTTPServer.new :Port => 8383

server.mount_proc '/deploy/123' do |req, res|
  res.body = `ls`
end

trap 'INT' do server.shutdown end

server.start

Add to crontab

@reboot bash -lc "cd where-your-script-is && ruby hook.rb"

Run

nohup ruby hook.rb &

Another solution with Sinatra

With Ruby’s sinatra it’s dead easy.

Create hook.rb

require 'sinatra'

post '/pull/1' do
  `cd ~/php/source && git pull`
end

Run:

gem install sinatra

Run the hook server:

screen ruby hook.rb -p 4378

Press Ctrl+a then d to leave screen.

Setup crontab -e to bring the hook back on reboot:

@reboot bash -lc "ruby hook.rb -p 4378"

Create GitHub webhook (your repo - Settings - Service hooks):

http://yoursite.com:4378/pull/1

It takes about 5 seconds from push to code update.

You can add more hooks to the code.

Redshift: PostgreSQL-like in the Cloud (Benchmark)

Amazon has opened it’s RedShift today.

image

Naturally I was curious about it’s performance. Basically it’s database with PostgreSQL-compatible protocol in the cloud

image

The full test results are under the cut, just a gist of it:

Insert 1M entries: local 8 seconds, RedShift 49 seconds.
Select by 2 rows (without indices): local 129ms seconds, RedShift ~54ms
Select by 1 row (without indices): local 44ms, RedShift ~448ms

(SELECT 1 was 100ms, so that's the round-trip time, I subtracted it 
from RedShift's values)

The test is VERY SYNTHETIC. Don’t take it as a comprehensive comparison.

Of course this does not prove anything. But it seems that 1M rows is not the use case for RedShift. I did not try JOINs, maybe it’s better with JOINs.

Afterall, they do say it’s for Business Intelligence systems. They also say “petabyte-scale”. So let’s try to scale up…

Egghead.io and AngularJS

So, have everybody already took a look at egghead.io ?

AngularJS seems to be really interesting stuff these days.

Basically you can have a collection of JavaScript objects that you can easily filter, sort, etc.. You can have <input> which will automatically search trough them, like this:

<input ng-model="search">

<ul ng-repeat="phone in phones | filter(search)">
  <li>{{ phone.brand }}</li>
</ul>

I don’t remember the exact syntax, sorry. But that’s the gist of it - no DOM manipulations, nothing. Just a templating, like you would do on server-side. And whenever the collection phones changes - the list is rebuilt. Really cool!

It was toogl.es that brought my attention to it.

Toogles is a minimal YouTube interface built as one page in AngularJS.

I mean - this app has models, routes and controllers inside of JavaScript. It works with in-page link, changes pages and works reeeeally fast.

It seems that the only problem is that you can’t be indexed with Google using that kind of technique (with routes), because Google requires that you have a static page behind the AJAX-built websites.

I think that’s going to change in the future. There is really no problem for Google to render the Javascript on server side. They even have the best browser (Chrome) and best JavaScript engine (V8) handy.

For now though you can use AngularJS for one-page rich applications (think GMail) and right now egghead.io is the best introduction I’ve seen (also donated some money to author).

It’s really cool - try it.

Laravel and YII

Working a lot with Rails - returning to PHP seems kind of daunting.

When you can do this in Rails:

@users.map{ |x| x.email }

Whereas you have to do this in PHP

$users = array_map(function($x) { return $x->email; }, $users);

(For which I can never remember the syntax - the array comes first or the lambda)

Yet there is a light at the end of the tunnel.

Two years ago I remember distinctly looking at all possible PHP frameworks and dismissing them one-by-one.

Everyone of them had something really bad that turned me off. Like it was way to heavy for small-time hacking. No safe templating. No migrations. Ugly ORMs. I still find Zend quite weird, because it’s a lot of great components and basically no good way to build a site out of it in minutes. (Maybe now it’s better, I don’t know).

My requirements were actually quite simple:

  • Good ORM (way to work with database, no more SQL injections) with a way to create custom SQL if you need to (preferably with Query Builder).
  • Safe templating (auto-escaping of everything, no more XSS)
  • Easy start, like having a simple CRUD blog in 5 minutes.
  • Easy to expand the project without hitting the limits of framework.

Mina for Rails Deployments

Mina is one of the cool projects I’ve recently discovered. It’s a deployment system, kind of like Capistrano.

I’ve been using Capistrano for some time now, but it always bugs me that after I do capify . I have to Google or search my projects for reasonable configuration.

Mina really solves that problem:

  1. You get sensible configuration by default right after mina init
  2. You get shared folders in default configuration
  3. You get local database.yml (something that really bugged me in Capistrano - it’s quite hard to create local database.yml)
  4. You get auto assets compilation only when assets change - out of box (something that you have to hack around to achieve in Capistrano)
  5. Mina is bloody fast! It creates bash file on server as opposed to Capistrano which does that on client.

It also kind of seems more nice-looking.

image

And yes, deployment is still as easy as mina deploy

I’m not sure whether multi-stage deployment is possible and also not sure how to remove the old releases, like keeping only 5 last releases. But overall - for my small “hack around” projects - Mina is a weapon of choice for now. Try it.

Foundation CSS

I’ve been working with Bootstrap for quite some time and it’s really nice.

But recently my eye kind of adapted to all that glossy look and it started to feel boring.

So, I’ve discovered Foundation. In my opinion it is more in current trend of Windows 8 / Google Mobile style of flat sqare buttons.

image

I did really like it and using it for current projects.

Create a User in PostgreSQL

I’ve got kind of tired remembering how to create users and databases in PostgreSQL.

Username: Database: Password:

bash

sudo -u postgres psql
CREATE ROLE username WITH CREATEDB LOGIN PASSWORD 'password';
CREATE DATABASE database OWNER username;

Note: the password has been randomly generated in your browser, refresh to update.

database.yml for rails

production:
  adapter: postgresql
  host: 127.0.0.1
  encoding: utf8
  database: database
  username: username
  password: password

MySQL

CREATE DATABASE database;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost' WITH GRANT OPTION;