RobL Vs

Background Robl

xterm-256color: unknown terminal type

Trying to run `top` on an Ubuntu server from OSX Mountain Lion results in the following.

1root@server:~# top
2'xterm-256color': unknown terminal type.

Thanks to Corentin Leclerc for the solution http://blog.corentinleclerc.com/terminal-sur-macos-x-lion-xterm-256color-unkn

Add the following to ~/.profile or ~/.bash_profile

1export TERM="xterm"

1export TERM="xterm"

Restart your terminal, or open a new tab. Magic.

 1root@server:~# top
 2
 3top - 10:52:46 up 31 days, 16:34,  1 user,  load average: 0.04, 0.02, 0.00
 4Tasks:  57 total,   1 running,  56 sleeping,   0 stopped,   0 zombie
 5Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
 6Mem:    786652k total,   772360k used,    14292k free,   181920k buffers
 7Swap:        0k total,        0k used,        0k free,    46000k cached
 8
 9  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                                              
1026479 rails     20   0 89604  57m 2428 S  0.3  7.5   0:04.08 ruby1.8                                                                                                                                                                                               
11    1 root      20   0  1952  508   72 S  0.0  0.1   0:13.04 init                                                                                                                                                                                                  
12    2 root      15  -5     0    0    0 S  0.0  0.0   0:00.12 kthreadd

Recover committed (deleted) file in Git

What if, just suppose, you deleted a file in error, made the commit to Git, then several commits later you realise you needed the file all along. How do you get it back?

Well, you find the last commit for that file and then checkout the file with the revision number. In just a few lines then…

1Robs-iMac:repo rl$ file='app/views/previously_deleted.html.haml'
2Robs-iMac:repo rl$ echo $file
3app/views/previously_deleted.html.haml
4Robs-iMac:repo rl$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Raspberry Pi - part 1

This year my main festive gift from the mighty Kat was a long awaited Raspberry Pi. Just never seemed to have the funds to nab one myself and I am pretty chuffed. If you’ve not heard of one then you’ve been living under a rock for the last year. The Raspberry Pi is basically just a very small, barebones computer. In fact its so minimal that it doesn’t even come with a case.

Raspberry Pi

You can see on the board that the mounted interfaces are a dual USB port, micro-USB power supply, HDMI output to monitor, audio out, video out, GPIO pins.

You might have enough bits and bobs lurking in your cupboard full of cables and old hardware to get going but alas I do not. So in order to get started I need some way of hooking this up to a monitor, keyboard and powering the thing.

USB Keyboard

No PS2 ports so its all USB, so a USB Keyboard is needed. Relatively cheap, I’ve spotted a great wireless one with a touch pad for £25 but for now this one will do.

Monitor

While the Raspberry Pi can be easily used with a modern television I can’t see myself sitting in the front room while I’m playing with it for the time being. You can use any monitor or television with an HDMI or DVI connection. So you’ll need a cable

My spare monitor is a VGA which won’t work without an adapter to convert the signal. £30 for an adapter is a bit much really. I’ve secured a DVI monitor from a friend for £10, so I’ve opted for the HDMI to DVI while I’m playing.

SD Card / Operating System

You’ll need an SD Card to install the operating system on. Fortunately we have an old 4Gb card from a Camera. So cost £0. Yay. average cost on Amazon about £6 so not bad.

Install it yourself

You can follow the tutorial on the Raspberry Pi Downloads page to copy an OS onto a card.

Pre-installed

You can get an 8Gb (and upwards) SD Card from ThePiHut with one of two distributions from £8.99.

  • Raspbian – pre-installed Raspian is an optimised version of Debian, containing LXDE, Midori, development tools and example source code for multimedia functions.
  • OpenElec / XBMC – pre-installed Open Embedded Linux Entertainment Center, or OpenELEC for short, is a small Linux distribution built from scratch as a platform to turn your computer into a complete XBMC media center.

That’s a start I’ve ordered what I need so now I have to sit and wait. I’ve got a few ideas about what I’d like to build.

  • MAME centre
  • Media centre
  • Spotify streaming straight into the HiFi

So I’ll do a bit of research and buy a book or two.

Resources

There’s plenty of resources out there as well as some physical publications to get started with.

More useful links…

Installing 'therubyracer' grrrr

 1Robs-iMac:testapp rl$ gem install therubyracer
 2Building native extensions.  This could take a while...
 3ERROR:  Error installing therubyracer:
 4        ERROR: Failed to build gem native extension.
 5
 6        /Users/rl/.rvm/rubies/ruby-1.8.7-p370/bin/ruby extconf.rb
 7*** extconf.rb failed ***
 8Could not create Makefile due to some reason, probably lack of
 9necessary libraries and/or headers.  Check the mkmf.log file for more
10details.  You may need configuration options.
11
12Provided configuration options:
13        --with-opt-dir
14        --without-opt-dir
15        --with-opt-include
16        --without-opt-include=${opt-dir}/include
17        --with-opt-lib
18        --without-opt-lib=${opt-dir}/lib
19        --with-make-prog
20        --without-make-prog
21        --srcdir=.
22        --curdir
23        --ruby=/Users/rl/.rvm/rubies/ruby-1.8.7-p370/bin/ruby
24extconf.rb:13: uninitialized constant Gem (NameError)
25Checking for Python...
26
27Gem files will remain installed in /Users/rl/.rvm/gems/ruby-1.8.7-p370@thebevy/gems/libv8-3.3.10.4 for inspection.
28Results logged to /Users/rl/.rvm/gems/ruby-1.8.7-p370@thebevy/gems/libv8-3.3.10.4/ext/libv8/gem_make.out

It seems that libv8 requires Gem to exist, however rubygems is not available by default in Ruby 1.8.7. Ruby 1.9.x it is.

Thanks to Olly Smith for the solution

 1Robs-iMac:thebevy rl$ RUBYOPT=-rrubygems gem install therubyracer
 2Building native extensions.  This could take a while...
 3Fetching: therubyracer-0.10.2.gem (100%)
 4Building native extensions.  This could take a while...
 5Successfully installed libv8-3.3.10.4
 6Successfully installed therubyracer-0.10.2
 72 gems installed
 8Installing ri documentation for libv8-3.3.10.4...
 9Installing ri documentation for therubyracer-0.10.2...
10Installing RDoc documentation for libv8-3.3.10.4...
11Installing RDoc documentation for therubyracer-0.10.2...

Apache Rewrite because I can never remember how to do it

I seem to use Apache less and less these days, so every year or so I have to try and remember the syntax for VirtualHost configs, redirects and the like.

 1<VirtualHost *:80>
 2  ServerName duncanwilkinson.com
 3  DocumentRoot /home/duncan/www.duncanwilkinson.com/
 4  RackEnv production
 5  RewriteEngine on
 6  RewriteCond %{HTTP_HOST} !^duncanwilkinson\.com
 7  RewriteRule ^/(.*)$ http://duncanwilkinson.com/$1 [R=permanent,L]
 8  CustomLog /var/log/apache2/loathsome-access.log combined
 9  ErrorLog  /var/log/apache2/loathsome-error.log
10</VirtualHost>
11<VirtualHost *:80>
12  ServerName www.duncanwilkinson.com
13  ServerAlias www.dna-labs.net dna-labs.net www.triplebinary.com triplebinary.com www.needleye.net needleye.net
14  RewriteEngine on
15  RedirectMatch (.*) http://duncanwilkinson.com
16</VirtualHost>
17<VirtualHost *:80>
18  ServerName www.collapse.co
19  ServerAlias collapse.co
20  DocumentRoot /home/duncan/www.collapse.co/
21</VirtualHost>

iptables reminder

Forgot all about iptables….

 1rails # iptables -A INPUT -s <naughty ip> -j DROP
 2 rails # iptables --list
 3Chain INPUT (policy ACCEPT)
 4target     prot opt source               destination         
 5DROP       all  --  bl16-108-238.dsl.telepac.pt  anywhere            
 6
 7Chain FORWARD (policy ACCEPT)
 8target     prot opt source               destination         
 9
10Chain OUTPUT (policy ACCEPT)
11target     prot opt source               destination

Resize multiple images in one bash script

Badoom……!!!

1#!/bin/bash
2for i in *.JPG; do echo $i; base=`basename "$i" .JPG`; convert "$i" -resize 50% "thumbs/$base.jpg"; done

Delete files created more than 14 days ago

When you’ve got lots of temporary files on a unix server and you never clean them out this might help. Remove files that were created more than 14 days ago like so…

1find . -type f -mtime +14 -exec rm {} \;

doing away with www.

“www.” for the most part is a pointless idea. We all know what a web page is. The “www.” prefix is outdated although necessary evil. I guess the same could be said of http:// and https:// for web requests …we all know what it means.

Here’s a quick snippet of my apache config to push all traffic from www.loathso.me to loathso.me

 1<VirtualHost *>
 2  ServerName www.loathso.me
 3  ServerAlias loathso.me
 4  DocumentRoot /var/www/loathsome/current/public/
 5  RackEnv production
 6  RewriteEngine on
 7  RewriteCond %{HTTP_HOST} ^www\.loathso\.me
 8  RewriteRule ^/(.*)$ http://loathso.me/$1 [R=permanent,L]
 9  CustomLog /var/log/apache2/loathsome-access.log common
10  ErrorLog  /var/log/apache2/loathsome-error.log
11</VirtualHost>

Rails Serializers and INET_NTOA

MySQL doesn’t have a built in type for an IP Address, PostgreSQL does though. You’ll find that ip addresses are often stored as an integer. You can translate between an integer and ip address and vice versa with a built in MySQL functions. In a recent piece of work we had to detect a user’s country code based on their incoming IP via against a range of IPs (stored as integers).

 1mysql> SELECT INET_ATON('192.168.0.1');
 2+--------------------------+
 3| INET_ATON('192.168.0.1') |
 4+--------------------------+
 5|               3232235521 |
 6+--------------------------+
 71 row in set (0.00 sec)
 8
 9mysql> SELECT INET_NTOA('3232235521');
10+-------------------------+
11| INET_NTOA('3232235521') |
12+-------------------------+
13| 192.168.0.1             |
14+-------------------------+
151 row in set (0.00 sec)

Wouldn’t it be nice to get a Rails model to accept an ip address and store it as an integer. Well its basically serializing the ip address and using Rails 3.1’s new serialization api we can do the following.

 1class IpEncoder
 2  #
 3  # Converts IP to number
 4  # inet_aton
 5  #
 6  def load(n)
 7    return unless n
 8    [n].pack("N").unpack("C*").join "."
 9  end
10
11  #
12  # Converts number to IP
13  # inet_ntoa
14  #
15  def dump(n)
16    n.split(/\./).map(&:to_i).pack("C*").unpack("N").first
17  end
18
19end

Basicially a class with two methods IpEncoder#load encodes its input, and IpEncoder#dump decodes it. Then you simply add the following to your model.

1require 'ip_encoder'
2class Log < ActiveRecord::Base
3  serialize :ip_address,  IpEncoder.new
4end

And there you have it.

1Rob-Laceys-MacBook-Pro:loathsome roblacey$ ./script/rails c
2Loading development environment (Rails 3.1.2)
3>> Log.create(:ip_address => '192.168.0.1')
4  SQL (0.5ms)  INSERT INTO "logs" ("ip_address") VALUES (?)  [["ip_address", 3232235521]]
5=> #<Log id: 1, ip_address: "192.168.0.1">

Github

Projects

  • PledgeMusic
  • Loathsome Records
  • Zombify
  • Metal T-Shirt Archive
  • Surrey Hills Bakery
  • GoreStore
  • The Bevy
  • Sphynx Cat Association
  • LCF Firstmove
  • Accounting and Business Admin Services