Capify - public key deployment

written by Jerod on July 11th, 2008 @ 09:12 AM

I’ve been playing with Capistrano a lot lately and loving it. Here is an example of how easy it is to write tasks and use them on multiple remote servers.

This task installs your SSH public key on the remote machine to allow key-based authentication:

   1  set :key_file do
   2      Capistrano::CLI.ui.ask "enter public key to push: "
   3    end
   4    
   5    desc "configures key-based SSH administration"
   6    task :push_key, :roles  => :all do
   7      key_location = File.expand_path(key_file)
   8      unless File.exist?(key_location) and key_file.match(/\.pub$/)
   9        puts "Couldn't locate public key. Try again"
  10        exit
  11      end
  12      key_file_name = File.basename(key_location)
  13      upload key_location, "/tmp/#{key_file_name}"
  14      run "if [ ! -e ~/.ssh ];then mkdir ~/.ssh; fi"
  15      run "cat /tmp/#{key_file_name} >> ~/.ssh/authorized_keys"
  16      run "rm /tmp/#{key_file_name}"
  17      run "chmod 600 ~/.ssh/authorized_keys"
  18    end

Silky smooth.

Post a comment

Options:

Size

Colors

Clicky Web Analytics