
Untitled
By: a guest on Jan 28th, 2012 | syntax:
None | size: 1.13 KB | hits: 19 | expires: Never
desc "adds the necessary hosts to your local /etc/hosts file from current subdomains in db"
task :subdomains => :environment do
#
# emptyblog.localhost used as a locator for the line in /etc/hosts
tmp_file, changed = '/tmp/etc_hosts_copy', false
default, hosts = %w[skinnyboard.local www.skinnyboard.local], []
# add all the blog subdomains
Company.find(:all).each { |c| hosts << "#{c.subdomain}.skinnyboard.local" }
host_line = "127.0.0.1 " + hosts.sort.unshift(default).join(' ')
%x[cp /etc/hosts #{tmp_file}]
file = File.new(tmp_file)
lines = file.readlines
lines.each do |line|
changed = true if line.gsub!(/^127.0.0.1 skinnyboard\.local www\.skinnyboard\.local.+$/, host_line)
end
unless changed
lines += ["\n", host_line, "\n"]
end
file = File.new(tmp_file,'w')
lines.each do |line|
file.write(line)
end
file.close
%x[sudo -p "Password:" cp #{tmp_file} /etc/hosts]
puts "subdomains finished"
puts "They are available as;\n\n"
hosts.each do |host|
puts "http://#{host}"
end
puts "\n"
end