#!/usr/bin/ruby require 'rubygems' require 'net/sftp' require 'net/ssh' if ARGV.empty? STDERR.puts "usage #{__FILE__} " Process.exit end HOST = ARGV.shift USER = ENV['USER'] config = "#{HOST}.yml" config = File.join(File.dirname(__FILE__), config) unless File.exist? config require 'yaml' config = YAML.load(open(config) {|file| file.read}) sections = %w{dirs packages gems gem_sources apache_conf apache_mod rsync} sections.each {|var| eval "$#{var}=(config[:#{var}] or [])" } def version string string.scan(/[\d.]+/).last.split('.').map {|n| n.to_i} end Net::SSH.start(HOST, USER) do |ssh| restart_apache = false installed = ssh.exec!('dpkg --get-selections').scan(/^\S+/) $packages.each do |package| unless installed.include? package puts "installing #{package}..." ssh.exec! "sudo aptitude -y install #{package}" end end Net::SFTP.start(HOST, USER) do |sftp| entries = sftp.dir.entries('.') $dirs.each do |dir| unless entries.any? {|entry| entry.name == dir} puts "mkdir #{dir}" sftp.mkdir dir end end entries = sftp.dir.entries('/etc/apache2/conf.d/') $apache_conf.each do |conf| unless entries.any? {|entry| entry.name == conf} puts "configure apache #{conf}" sftp.upload! "/etc/apache2/conf.d/#{conf}", "/tmp/#{conf}" ssh.exec! "sudo mv /tmp/#{conf} /etc/apache2/conf.d/#{conf}" restart_apache = true end end entries = sftp.dir.entries('/etc/apache2/mods-enabled/') $apache_mod.each do |mod| unless entries.any? {|entry| entry.name == "#{mod}.load"} puts "enable apache #{mod}" ssh.exec! "sudo a2enmod #{mod}" restart_apache = true end end end if restart_apache puts 'restart apache' ssh.exec! "sudo /etc/init.d/apache2 restart" end if $gems.include? 'rubygems-update' if -1 == (version(ssh.exec!('gem -v')) <=> version(`gem -v`)) puts 'installing rubygems-update...' ssh.exec! 'sudo gem install rubygems-update' ssh.exec! 'sudo /var/lib/gems/1.8/bin/update_rubygems' end $gems.delete 'rubygems-update' end gem_env = ssh.exec!('gem env remotesources').split("\n") $gem_sources.each do |source| unless gem_env.include? source puts "add remote gem source #{source}" ssh.exec! "sudo gem sources --add #{source}" end end installed = ssh.exec!('gem list').scan(/^\S+/) $gems.each do |gem| unless installed.include? gem puts "installing #{gem}..." ssh.exec! "sudo gem install #{gem}" end end end Dir.chdir ENV['HOME'] $rsync.each do |file| system "rsync #{file} #{HOST}:." end