Method: WebServerConfigGenerator::Generator#add_ghost_entries

Defined in:
lib/web_server_config_generator/generator.rb

#add_ghost_entriesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/web_server_config_generator/generator.rb', line 68

def add_ghost_entries
  current_hosts = Host.list
  already_correct = []
  added = []
  present_but_incorrect = []
  server_names.each do |server_name|
    if host = current_hosts.detect { |h| h.name == server_name }
      if host.ip == "127.0.0.1"
        already_correct << host
      else
        present_but_incorrect << host
      end
    else
      if $TEST_MODE
        puts "would have added #{server_name} -> 127.0.0.1"
      else
        added << Host.add(server_name)
        global_config[:hosts] ||= []
        global_config[:hosts] << server_name
        save_global_config(global_config)
      end
    end
  end
  
  if already_correct.size > 0
    puts "\n#{already_correct.size} hosts were already setup correctly"
    puts
  end
  
  if added.size > 0
    puts "The following hostnames were added for 127.0.0.1:"
    puts added.map { |h| "  #{h.name}\n" }
    puts
  end
  
  if present_but_incorrect.size > 0
    puts "The following hostnames were present, but didn't map to 127.0.0.1:"
    pad = present_but_incorrect.max{|a,b| a.to_s.length <=> b.to_s.length }.to_s.length
    puts present_but_incorrect.map { |h| "#{h.name.rjust(pad+2)} -> #{h.ip}\n" }
    puts
  end
end