Register now and start sharing your code snippets.
-->

Allowing URL's in a route.

Ruby posted 4 months ago by marko

I wanted to create a route that could accept an URL as a parameter. The problem was that dots and slashes were interpreted as separators for the route. Luckily I managed to find this post that explained how it could be bypassed.

   1  map.connect ':scale/:text.:format', :controller => 'barcode', :requirements => { :text => /.*/ }

With this route set up I now could catch parameters like this: http://localhost:3000/200×200/http://aktagon.com.png, where 200×200 is :scale, http://aktagon.com is :text and .png is :format.

Tagged rails routes, url in route

Installing ImageMagick from sources and using xstow to handle the installation.

Shell Script (Bash) posted 4 months ago by marko

I really hate installing software outside of the package manager, because that is not the way of a stable system. Xstow helps the agony those times that sourceball installations are necessary (e.g for new features). Here’s how to install imagemagick with xstow. If you haven’t installed xstow yet, then follow the relevant instructions in this snippet

   1  sudo apt-get install libmagick9-dev # needed for sane image support
   2  cd /tmp
   3  wget ftp://ftp.sunet.se/pub/multimedia/graphics/ImageMagick/ImageMagick-6.4.2-7.tar.bz2
   4  tar xjvf ImageMagick-6.4.2-7.tar.bz2
   5  sudo mv ImageMagick-6.4.2 /usr/src
   6  cd /usr/src
   7  sudo ln -s ImageMagick-6.4.2 imagemagick
   8  cd imagemagick
   9  ./configure --prefix=/usr/local/stow/imagemagick
  10  make
  11  sudo make install
  12  cd /usr/local/stow
  13  sudo xstow imagemagick

Tagged xstow, imagemagick

Using expect for automation of bulk scp copying.

Shell Script (Bash) posted 5 months ago by marko

Expect can come in handy when you can’t configure ssh public key authentication on the servers :) (and the system “architect” hasn’t yet realized the wonderfulness of a log host).

   1  #!/bin/bash
   2  #
   3  # Usage: script <username> <password> <build>
   4  #
   5  # Example ./copy_logs_from_production.sh marko hubbabubba current
   6  #
   7  
   8  username=$1
   9  password=$2
  10  build=$3
  11  mkdir $build
  12  
  13  instance_1_server=10.0.0.1
  14  instance_2_server=10.0.0.1
  15  instance_3_server=10.0.0.2
  16  instance_4_server=10.0.0.2
  17  instance_5_server=10.0.0.3
  18  instance_6_server=10.0.0.3
  19  instance_7_server=10.0.0.4
  20  instance_8_server=10.0.0.4
  21  instance_9_server=10.0.0.5
  22  
  23  servers=("$instance_1_server" "$instance_2_server" "$instance_3_server" "$instance_4_server" "$instance_5_server" "$instance_6_server" "$instance_7_server" "$instance_8_server" "$instance_9_server" )
  24  i=1
  25  for server in ${servers[@]}; do
  26    expect -c "
  27              # exp_internal 1 # uncomment for debugging
  28              spawn /usr/bin/scp $username@$server:/var/logs/application/$build/server${i}/error.log $build/error-${i}.log
  29              expect { 
  30                "*password:*" { send $password\r\n; interact } 
  31                eof { exit }
  32              }
  33              exit
  34              "
  35    let "i=i+1"
  36  done
  37  

Tagged expect, bash, automation, scp, bulk

Time ago in words (minutes, hours, days, weeks, months ago in words)

Ruby posted 5 months ago by christian

   1  def minutes_in_words(timestamp)
   2      minutes = (((Time.now - timestamp).abs)/60).round
   3      
   4      return nil if minutes < 0
   5      
   6      case minutes
   7        when 0..4            then '&lt; 5 minutes'
   8        when 5..14           then '&lt; 15 minutes'
   9        when 15..29          then '&lt; 30 minutes'
  10        when 30..59          then '&gt; 30 minutes'
  11        when 60..119         then '&gt; 1 hour'
  12        when 120..239        then '&gt; 2 hours'
  13        when 240..479        then '&gt; 4 hours'
  14        when 480..719        then '&gt; 8 hours'
  15        when 720..1439       then '&gt; 12 hours'
  16        when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
  17        when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
  18        when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
  19        else                      '&gt; ' << pluralize((minutes/525600).floor, 'år')
  20      end
  21    end

There are also similar implementations:
  1. http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
  2. http://timeago.yarp.com/
Tagged month, week, hour, minutes, words, rails, ruby

"updating paths is incompatible with switching branches/forcing" error when using git branches with Vlad

Plain Text posted 5 months ago by christian

I ended up writing this snippet after I found out that deploying a git branch with vlad wasn’t as easy as it should’ve been…

I assumed the Vlad documentation was right, so I put the branch name in the revision variable (located in config/deploy.rb):

   1  set :revision,              'branch_name'

But then I got this error when executing rake vlad:deploy:

   1  git checkout: updating paths is incompatible with switching branches/forcing
   2  Did you intend to checkout 'branch_name' which can not be resolved as commit?

I managed to solve the problem by using the SHA1 hash of the branch instead of the branch name, but that only worked for the current revision:

   1  set :revision,              'd34360870be6536992e6d45bb0aa72eca31e14443'

So after some googling I found this post at scie.nti.st, which made my day because it has the following examples of how to deploy tags and branches with git and Vlad:

   1  # Deploy the latest code, this the default
   2  set :revision, "HEAD" 
   3  
   4  # Deploy branch "origin/branch_name"
   5  set :revision, "origin/branch_name"
   6   
   7  # Deploy tag "1.0"
   8  set :revision, "1.0"

Note that you have to push the branch to the remote server before running rake vlad:deploy:

   1  git push origin branch_name

Tagged vlad, git, branch, deploy, revisions, tags