Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 1.13 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. desc "adds the necessary hosts to your local /etc/hosts file from current subdomains in db"
  2.   task :subdomains => :environment do
  3.     #
  4.     #  emptyblog.localhost used as a locator for the line in /etc/hosts
  5.     tmp_file, changed = '/tmp/etc_hosts_copy', false
  6.     default, hosts = %w[skinnyboard.local www.skinnyboard.local], []
  7.  
  8.     # add all the blog subdomains
  9.     Company.find(:all).each { |c| hosts << "#{c.subdomain}.skinnyboard.local" }
  10.  
  11.     host_line = "127.0.0.1 " + hosts.sort.unshift(default).join(' ')
  12.  
  13.     %x[cp /etc/hosts #{tmp_file}]
  14.  
  15.     file = File.new(tmp_file)
  16.     lines = file.readlines
  17.     lines.each do |line|
  18.       changed = true if line.gsub!(/^127.0.0.1 skinnyboard\.local www\.skinnyboard\.local.+$/, host_line)
  19.     end
  20.  
  21.     unless changed
  22.       lines += ["\n", host_line, "\n"]
  23.     end
  24.  
  25.     file = File.new(tmp_file,'w')
  26.     lines.each do |line|
  27.       file.write(line)
  28.     end
  29.     file.close
  30.  
  31.     %x[sudo -p "Password:" cp #{tmp_file} /etc/hosts]
  32.     puts "subdomains finished"
  33.     puts "They are available as;\n\n"
  34.     hosts.each do |host|
  35.       puts "http://#{host}"
  36.     end
  37.     puts "\n"
  38.   end