te·los  - An ultimate object or aim.

Resolving "no route to host" with OSX VPN Subnet Conflict

This is an example of resolving routing to a host via the VPN. Both networks 10.0.0.0/24. ppp0 is the VPN interface while en0 is the local network interface.

$ netstat -nr

                                                                                                             
Routing tables
Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
...
10.0.0.209/32      en0               USc             1        0   en0
$ ifconfig

        
en0: flags=8863 mtu 1500
...
ppp0: flags=8051 mtu 1396
    inet 10.0.0.90 --> 10.0.0.133 netmask 0xffffff00

$ route add -net 10.0.0.209 -interface ppp0

$ netstat -nr

                                                                                                    
Routing tables
Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
...
10.0.0.209/32      ppp0               USc             1        0    ppp0


OpenSSL Notes

View

  • .cer
    openssl x509 -in cert.cer -text -noout

Transform

  • .cer -> .der
    openssl x509 -in cert.cer -outform der -out cert.der
  • .der -> .pem
    openssl x509 -in cert.der -inform der -outform pem -out cert.pem
  • [encrypted].key -> [unencrypted].key
    openssl rsa -in encrypted.key -out unencrypted.key

Extract

  • [private].pem -> [public].pem.pub
ssh-keygen -y -f cert.pem > cert.pem.pub
openssl rsa -in cert.pem -pubout > cert.pem.pub

Misc

  • self signed wildcard ssl - *.domain.com for the "Common Name"
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/private/telos.co.nz.key -out /etc/nginx/ssl/certs/telos.co.nz.crt

Runit and Unicorn

Your service run file should look something like this:

#!/bin/bash
# /etc/service/unicorn/run
exec 2>&1
exec chpst -u vagrant:vagrant \
  /home/vagrant/.rbenv/shims/unicorn \
    -c /home/vagrant/app/current/unicorn.conf.rb \
    /home/vagrant/app/current/config.ru

You can get zero downtime restarts with:

sv hup unicorn

Nginx 403

Nginx needs execute permission on directories and read permission on files.

namei -om app/current/public

Look at line 6.

f: app/current/public
 drwxrwxr-x vagrant vagrant app
 lrwxrwxrwx vagrant vagrant current -> /home/vagrant/app/releases/1389923971
   dr-xr-xr-x root    root    /
   drwxr-xr-x root    root    home
   drwx------ vagrant vagrant vagrant
   drwxrwxr-x vagrant vagrant app
   drwxrwxr-x vagrant vagrant releases
   drwxrwxr-x vagrant vagrant 1389923971
 drwxrwxr-x vagrant vagrant public

Dynamic DNS with Digital Ocean and Cron

curl https://api.digitalocean.com/domains/$DOMAIN/records/$RECORD/edit\?client_id\=$CLIENT\&api_key\=$API\&data\=$(dig +short myip.opendns.com @resolver1.opendns.com)

We can make it better by updating only when we need to:

[ $(host example.com | awk -F' ' '{print $4}') != $(dig +short myip.opendns.com @resolver1.opendns.com) ] && curl ...

Photo Backups

# e.g. ~/Pictures/Photos
DIRECTORY=$1

# e.g. s3://backup/media/
BUCKET=$2

# resize.
find . -iname "*.jpg" -exec mogrify -resize "2048x2048>" -quality 75 {} \;

# sync.
s3cmd sync --recursive --exclude '*.MTS' --include '*.jpg' --include '*.JPG' $DIRECTORY $BUCKET

Optimise .gif with Image Magick

convert -layers Optimize original.gif optimised.gif

Nebula-JS


Jenkins Slave Via SSH Proxy

Option 1:

ssh -o "ProxyCommand=ssh <proxy.address> nc -w 1 <slave.address> 22" <slave.address>

Option 2: add the following to master's .ssh/config:

Host <slave.name>
  ProxyCommand ssh <proxy.address> nc -w 1 <slave.address> 22

Now you can configure the node as "Launch slave via execution of command on the Master":

ssh <command/slave.name> 'curl http://<master.address>/jnlpJars/slave.jar -o slave.jar; java -jar slave.jar'

Alias Method Chain Debugging

module Marshal
  class << self
    def dump_with_debugger(o)
      dump_without_debugger(o)
    rescue Exception => e
      binding.pry
    end

    alias_method :dump_without_debugger, :dump
    alias_method :dump, :dump_with_debugger
  end
end

Chef Override Template

Here is an example of me overriding the pg_hba.conf template.

include_recipe "postgresql::server"

begin
  r = resources(:template => "#{node['postgresql']['dir']}/pg_hba.conf")
  r.cookbook "webapp-postgresql"
rescue Chef::Exceptions::ResourceNotFound
  Chef::Log.warn "could not find template to override!"
end

Fix Centos/Vagrant 5 second DNS resolve

config.vm.provision :shell, inline: "if [ ! $(grep single-request-reopen /etc/resolv.conf) ]; then echo 'options single-request-reopen' >> /etc/resolv.conf && service network restart; fi"

Qt4.8 on Centos 6.4 for Capybara Webkit

# Place this file in your /etc/yum.repos.d/ directory

[epel-qt48]
name=Software Collection for Qt 4.8
baseurl=http://repos.fedorapeople.org/repos/sic/qt48/epel-$releasever/$basearch/
enabled=1
skip_if_unavailable=1
gpgcheck=0

[epel-qt48-source]
name=Software Collection for Qt 4.8 - Source
baseurl=http://repos.fedorapeople.org/repos/sic/qt48/epel-$releasever/SRPMS
enabled=0
skip_if_unavailable=1
gpgcheck=0

yum install qt48-qt-webkit-devel

ln -s /opt/rh/qt48/root/usr/include/QtCore/qconfig-64.h  /opt/rh/qt48/root/usr/include/QtCore/qconfig-x86_64.h

source /opt/rh/qt48/enable

export PATH=/opt/rh/qt48/root/usr/lib64/qt4/bin/${PATH:+:${PATH}}

gem install capybara-webkit -v "1.0.0"

Database is being accessed by other users [Postgres 9.2]

namespace :db do
  desc 'kills connections to postgres database'
  task :kill_postgres_connections => :environment do
    database_config = Rails.configuration.database_configuration[Rails.env]

    if database_config
      database_name = database_config['database']
      sql = <<-SQL
        SELECT pid, pg_terminate_backend(pid) AS terminated
        FROM pg_stat_activity
        WHERE pid <> pg_backend_pid() 
        AND datname = '#{database_name}';
      SQL
      cmd = %(psql -c "#{sql}"  -d '#{database_name}')
      puts "WARN: killing connections to #{database_name}"
      system(cmd)
    else
      fail(ArgumentError, "Could not find db config entry for env (#{Rails.env})")
    end
  end
end

task 'db:drop' => 'db:kill_postgres_connections'

Example of Soap XML with Slim

Envelope

doctype xml
soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  soapenv:Header
    soapenv:Action mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"
  soapenv:Body
    == yield

Body

upr:ProfileRetrieveReq TargetBranch=target_branch xmlns:upr="http://www.travelport.com/schema/uprofile_v13_0" xmlns:com="http://www.travelport.com/schema/common_v19_0"
  com:BillingPointOfSaleInfo OriginApplication="UAPI"
  upr:ProfileID= id
  upr:ProfileDataFilter
    upr:ProfileDataCategory= filter

Render

def default_render_locals
  {}
end

def default_render_options
  {format: :xhtml, attr_quote: '"'}
end

def template_path(name)
  File.expand_path(File.join("templates", name), File.dirname(__FILE__))
end

def render(template_name, locals, options = default_render_options)
  Tilt.new(template_path("layout.xml.slim")).render do
    template = Tilt.new(template_path(template_name), 0, options)
    template.render(Object.new, default_render_locals.merge(locals))
  end
end

Break Time


AngularJS Mock External Services

You should (just like I should have) read the following documentation.

The $httpBackend used in production, always responds to requests with responses asynchronously. If we preserved this behavior in unit testing, we'd have to create async unit tests, which are hard to write, follow and maintain. At the same time the testing mock, can't respond synchronously because that would change the execution of the code under test. For this reason the mock $httpBackend has a flush() method, which allows the test to explicitly flush pending requests and thus preserving the async api of the backend, while allowing the test to execute synchronously.

Mock

var $httpBackend;
beforeEach(inject(function ($injector)) {
  $httpBackend = $injector.get('$httpBackend');
  $httpBackend
     .whenJSONP(/ajax.googleapis.com/)
     .respond({});  
}));

Test

it("does something...", function () {
  // do something.
  $httpBackend.flush();
  // expect something.
});

Export iTunes .m3u8 Playlist Files Script

Ruby

#!/usr/bin/env ruby
# ./export_m3u8.rb path/to/playlist.m3u8

require 'rubygems'
require 'pathname'
require 'fileutils'

if not Pathname.new(ARGV[0]).exist?
  puts "m3u8 file not found!"
  puts "usage: #{__FILE__} path/to/playlist.m3u8"
  exit(1)
end

playlist_path   = ARGV[0]
playlist_name   = File.basename(playlist_path, ".*")

folder_path     = File.join(Dir.pwd, playlist_name)
FileUtils.mkdir_p(folder_path)

IO.read(playlist_path).split("\r").each_with_index do |file_path, index|
  next unless Pathname.new(file_path).exist?

  file_name = "#{index} " + File.basename(file_path)
  puts "Copying... #{file_name}"
  FileUtils.cp(file_path, File.join(folder_path, file_name))
end

puts "Finished."

Python

#!/usr/bin/env python
# ./export_m3u8.py path/to/playlist.m3u8

import sys
import os
import shutil

if not os.path.exists(sys.argv[1]):
  print "m3u8 file not found!"
  print "usage: %s path/to/playlist.m3u8" % sys.argv[0]
  sys.exit(1)

playlist_path = sys.argv[1]
playlist_name = os.path.splitext(os.path.basename(playlist_path))[0]

folder_path   = os.path.join(os.getcwd(), playlist_name)
if not os.path.exists(folder_path):
  os.makedirs(folder_path)

playlist_file = open(playlist_path).read().split("\r")
for index, file_path in enumerate(playlist_file):
  if not os.path.exists(file_path):
    continue

  file_name = "%d %s " % (index, os.path.basename(file_path))
  print "Copying... %s" % file_name
  shutil.copy(file_path, os.path.join(folder_path, file_name))

print "Finished."

RaspberryPi 3-Step SD-Card Format for OSX

Please make sure you identify the correct disk. You can download officially supported distros here.

First

$ diskutil list                                                                                                                         [master][ruby-1.9.3-p392] 
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *4.0 GB     disk1
   1:                 DOS_FAT_32 UNTITLED                4.0 GB     disk1s1

Second

$ sudo diskutil unmountDisk disk1                                                                                                        
Unmount of all volumes on disk1 was successful

Third

$ sudo dd if=/Users/james/Downloads/2013-02-09-wheezy-raspbian.img of=/dev/disk1 

Finding A Ruby Memory Leak

The following code is helpful finding obvious changes in Ruby's ObjectSpace and tracking leaks.

object_counts = Hash.new(0)
ObjectSpace.each_object {|object| object_counts[object.class] += 1}
puts object_counts.inspect
# => [[Image, 67], [Array, 2474], [String, 59407], ...

Nelson Elhage has a great post on finding more sinister memory leaks here.


Inconsistent indentation with haml textarea

Sometimes you don’t want Haml to indent all your text. For example, tags like pre and textarea are whitespace-sensitive; indenting the text makes them render wrong. - Haml Documentation

Sinatra

set :haml, {:ugly => true}

Haml Template Filter

:preserve

Scorched Earth in Ruby

I wanted to build the simplest game with destructible terrain represented by an image bitmap. I had a lot of fun building this. You can find the code on github here.

img

Random Terrain

Texplay made it easy to draw to the terrain image.

# http://banisterfiend.wordpress.com/2008/08/23/texplay-an-image-manipulation-tool-for-ruby-and-gosu/
width, height = $window.width, $window.height
self.image ||= ::TexPlay.create_image($window, width, height)

# bounds
x1, y1  = 0, height/1.5
x2, y2  = width, height

# paint terrain
image.paint do
  # clear
  rect(0, 0, x2, y2, :fill => true, :color => Color::EMPTY)

  # rand seed
  cycles = rand(10)

  # draw
  x1 = 0.0
  while(x1 < $window.width) do
    y1 = (Math.sin(x1 / width * cycles) * 100) + height / 1.5
    line(x1, y1, x1, y2, :fill => true, :texture => Gosu::Image["terrain.png"])
    pixel(x1, y1, :color => Color::SURFACE)
    x1 += 1.0
  end
end

Angle against Terrain

It samples points underneath the player and takes the average angle.

self.angle = 270 + (angles.inject(:+) / angles.size) unless angles.size == 0

Player Colors

The Singleton ColorIterator class worked well providing predefined colors to filter over the player's image.

self.color = ColorIterator.instance.next

Projectile Angle

I love how simply the following code sets the projectile angle.

self.angle = Gosu.angle(x, y, x + velocity_x, y + velocity_y)

img


It's that time again.

You can find the code for my new blog using Sinatra and Sequel on github here.