Resuming scp after an interruption

It is not possible to make scp resume copying after an interrupted transmission. The good news is you can use rsync over SSH:

rsync --rsh='ssh' -av --progress --partial source destination
Posted in system administration | Leave a comment

How to change the disabled property in jQuery

If you are using jQuery 1.6 or greater, there is a simple way to change the disabled property in an input field:

$("input").prop('disabled', true);
$("input").prop('disabled', false);
Posted in javascript | Leave a comment

Files that have been modified between two git branches

Before merging two branches that have been separated for a while, it is useful to know which files have been modified:

$ git diff --name-status branch1..branch2
Posted in git | Leave a comment

How to install PostgreSQL on Ubuntu 12.04

Update your system with this PPA (Personal Package Archives) so you can install an up-to-date version of PostgreSQL:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:pitti/postgresql
$ sudo apt-get update

And now install PostgreSQL and the dev packages:

$ sudo apt-get install postgresql libpq-dev

That’s all. You can check your new PostgreSQL version:

$ psql -V
psql (PostgreSQL) 9.1.6
contains support for command-line editing
Posted in system administration | Leave a comment

How to install express on Ubuntu 12.04

Express is a node.js web application framework. Before installing express we need to install node.js and npm (Node Packaged Modules). I recommend you to update your system with this PPA (Personal Package Archives) so you can install an up-to-date version of node.js:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs npm
$ node -v
v0.8.15

Express is a node.js module, so we can:

$ npm install express

To check that express is already installed:

$ npm ls
/home/jvp
└─┬ express@3.0.3
  ├── commander@0.6.1
  ├─┬ connect@2.7.0
  │ ├── bytes@0.1.0
  │ ├── formidable@1.0.11
  │ ├── pause@0.0.1
  │ └── qs@0.5.1
  ├── cookie@0.0.5
  ├── cookie-signature@0.0.1
  ├── crc@0.2.0
  ├── debug@0.7.0
  ├── fresh@0.1.0
  ├── methods@0.0.1
  ├── mkdirp@0.3.3
  ├── range-parser@0.0.4
  └─┬ send@0.1.0
    └── mime@1.2.6

Easy, isn’t it?

Posted in javascript | 2 Comments

Skipping asset precompilation in capistrano if the assets did not change

The default capistrano task for compiling the assets compiles them every time, regardless of whether any assets were changed in the set of commits that you are deploying. This is the code snippet I use to check if the assets have changed:

namespace :deploy do
  namespace :assets do
 
    def not_first_deploy?
      'true' ==  capture("if [ -e #{current_path}/REVISION ]; then echo 'true'; fi").strip
    end
 
    desc "Run the asset precompilation rake task only if there are changes."
    task :precompile, :roles => :web, :except => { :no_release => true } do
      if not_first_deploy?
        from = source.next_revision(current_revision)
        if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
          run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
        else
          logger.info "Skipping asset pre-compilation because there were no asset changes"
        end
      end
    end
 
  end
end

It’s based in this other snippet. You have to load the file that contains the snippet after loading the default capistrano asset tasks. For example, if the above snippet is in lib/deploy/assets.rb, you should write in your main deploy.rb file something like:

load 'deploy/assets'
load 'lib/deploy/assets'
Posted in rails | Leave a comment

On-the-fly tar

If you want to copy a directory recursively and preserve exact file timestamps, ownerships, and permissions you can do an on-the-fly tar:

cd source-directory
tar cf - . | (cd destination-directory; tar xf -)
Posted in system administration | Leave a comment

New feed

I’m going to delete the FeedBurner RSS feed of this blog, so if you want to keep subscribed you will have to re-subscribe to the new feed http://javiervidal.net/feed. Sorry for the inconvenience.

Posted in personal | Leave a comment

Rake task with arguments and :environment

Just a quick tip:

desc "Rake task with arguments and :environment"
task :task_name, [:arg1, :arg2] => :environment do |t, args|
  args.with_defaults(:arg1 => "Foo", :arg2 => "Bar")
  puts "Hello, #{args.arg1}. Bye, #{args.arg2}."
end

Notes:

  • You may omit the #with_defaults call.
  • You have to use an Array for your arguments, even if there is only one.

So you will be able to pass arguments to your rake task:

$ rake task_name["Moon","Sun"]
Hello, Moon. Bye, Sun.
Posted in rails | 1 Comment

Timestamps with Paperclip and S3

I’m using Paperclip (2.3.11) to upload images to S3 and, as some other people have pointed out, if the content of a file changes but its name remains the same (for example, if you recrop the image), the timestamp added by Paperclip to the end of image URL won’t change. Consequently, the browser thinks the image hasn’t changed, and will display the old version.

Being image a Paperclip::Attachment, the url method will return something like:

> image.class
=> Paperclip::Attachment
> image.url
=> "http://domain/filename?1305625852"

If the image’s content changes, but not the name, the timestamp won’t change. To fix this issue, I’ve added a new and simple processor:

# config/initializers/paperclip_timestamper.rb
 
module Paperclip
  class TimeStamper < Processor
 
    def initialize(file, options={}, attachment=nil)
      super(file,options,attachment)
      timestamp_filename
    end
 
    def timestamp_filename
      attachment.instance_write(:updated_at, Time.now.to_i)
    end
 
    def make
      @file
    end
 
  end
end

Custom processors are currently not being loaded correctly in Rails 3 from lib/paperclip_processors, so a quick hack is to define it as a regular initializer. Don’t forget to specify the :processors option to has_attached_file:

has_couch_attached_file :image, ..., :processors => [:time_stamper]
Posted in ruby | Leave a comment