Class: WebServerConfigGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/web_server_config_generator/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/web_server_config_generator/generator.rb', line 3

def initialize(options)
  options.each do |opt, val|
    instance_variable_set("@#{opt}", val)
  end
  
  @starting_port = options[:starting_port] || 40000
  @port_pool_size = options[:port_pool_size] || 10000
  
  @environment_map = Hash.new(options[:environments]) if options[:environments]

  setup_config_dir
  
  @project_or_projects_dir ||= projects_dir
end

Instance Method Details

#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

#app_projectsObject



152
153
154
155
156
157
158
# File 'lib/web_server_config_generator/generator.rb', line 152

def app_projects
  @app_projects ||=
    begin
      find_project_and_symlink_dirs
      @app_projects
    end
end

#check_nginx_confObject



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/web_server_config_generator/generator.rb', line 194

def check_nginx_conf
  unless nginx_conf =~ /include.*#{web_server_vhost_nginx_conf}/
    puts "\nWarning: You'll need to make sure this line is in your nginx config, in the http block:"
    puts "  include #{web_server_vhost_nginx_conf};"
  end

  unless nginx_conf =~ /server_names_hash_bucket_size.*128/
    puts "\nWarning: Couldn't find the following line in your nginx conf.  It should be in the http block."
    puts "  server_names_hash_bucket_size 128;"
  end
end


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/web_server_config_generator/generator.rb', line 42

def create_sub_uri_links_dir(sub_uri_project)
  env = sub_uri_project.env
  
  web_server_sub_uri_apps_dir.mkdir unless web_server_sub_uri_apps_dir.exist?
  links_dir = web_server_sub_uri_apps_dir + sub_uri_project.server_name
  links_dir.mkdir unless links_dir.exist?
  sub_uri_project.projects.each do |p|
    symlink p.expand_path + "public", File.join(links_dir, p.relative_root_url_for_env(env))
    if p.relative_root_url_root_for_env(env)
      symlink p.expand_path + "public", File.join(links_dir, "root")
    end
  end
end

#delete_ghost_entriesObject



60
61
62
63
64
65
66
# File 'lib/web_server_config_generator/generator.rb', line 60

def delete_ghost_entries
  global_config[:hosts].each do |host|
    Host.delete host
  end
  global_config[:hosts] = []
  save_global_config(global_config)
end

#environmentsObject



186
187
188
# File 'lib/web_server_config_generator/generator.rb', line 186

def environments
  @environments ||= environment_map.values.flatten.uniq
end

#projectsObject



182
183
184
# File 'lib/web_server_config_generator/generator.rb', line 182

def projects
  app_projects + sub_uri_projects
end

#projects_dirObject



220
221
222
# File 'lib/web_server_config_generator/generator.rb', line 220

def projects_dir
  WebServerConfigGenerator::Pathname.new(global_config[:projects_dirs].first).expand_path
end

#prompt_to_restart_nginxObject



206
207
208
209
210
211
212
213
214
# File 'lib/web_server_config_generator/generator.rb', line 206

def prompt_to_restart_nginx
  puts
  if $RESTART_NGINX || ($RESTART_NGINX.nil? && agree("Restart nginx? [Y/n]") { |q| q.default = "Y"})
    puts "Restarting nginx..."
    cmd = "sudo #{nginx} -s quit; sleep 1 && sudo #{nginx}"
    puts "running: #{cmd}"
    system cmd
  end
end

#server_namesObject



56
57
58
# File 'lib/web_server_config_generator/generator.rb', line 56

def server_names
  projects.map { |p| p.server_names }.flatten.uniq
end

#setup_config_dirObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/web_server_config_generator/generator.rb', line 111

def setup_config_dir
  unless File.exist?(config_dir)
    unless dir = $PROJECTS_DIRECTORY
      puts "It looks like this is the first time you've run me."
      puts 
      dir = @project_or_projects_dir || FileUtils.pwd
      unless agree("setup #{dir} as your projects dir? [Y/n]") { |q| q.default = "Y"}
        puts "for your first run you'll need to supply your projects directory"
        exit
      end
      puts "setting up config dir"
    end
    
    FileUtils.mkdir_p config_dir
    initial_config = { :projects_dirs => [File.expand_path(dir)]}
    save_global_config(initial_config)
  end
end


144
145
146
147
148
149
150
# File 'lib/web_server_config_generator/generator.rb', line 144

def setup_webserver_links_dir
  web_server_links_dir.mkpath
  environments.each do |e|
    link_name = web_server_links_dir + e
    symlink(projects_dir, link_name)
  end
end

#sub_uri_projectsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/web_server_config_generator/generator.rb', line 160

def sub_uri_projects
  return @sub_uri_projects if @sub_uri_projects
  server_name_env_project_map = {}
  app_projects.each do |p|
    p.server_name_env_pairs.each do |server_name, env|
      server_name_env_project_map[server_name] ||= {}
      server_name_env_project_map[server_name][:env] ||= env
      raise "can't have one hostname map to multiple envs" if server_name_env_project_map[server_name][:env] != env
      server_name_env_project_map[server_name][:projects] ||= []
      server_name_env_project_map[server_name][:projects] << p
    end
  end
  server_name_env_project_map = server_name_env_project_map.select do |server_name, env_and_projects|
    # more than one project with the same server_name
    # don't count projects that evaluate to the same path
    env_and_projects[:projects].map { |p| p.realpath }.uniq.size > 1
  end
  @sub_uri_projects = server_name_env_project_map.map do |server_name, env_and_projects|
    WebServerConfigGenerator::SubUriProject.new(server_name, env_and_projects, self)
  end
end


130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/web_server_config_generator/generator.rb', line 130

def symlink(link_target, link_name)
  if File.exist?(link_name)
    if FileTest.symlink?(link_name)
      unless Pathname.new(link_name).realpath == Pathname.new(link_target).realpath
        puts "symlink '#{link_name}' exists, but doesn't appear to link to the correct place"
      end
    else
      puts "couldn't make symlink '#{link_name}', something's already there"
    end
  else
    File.symlink link_target, link_name
  end
end


216
217
218
# File 'lib/web_server_config_generator/generator.rb', line 216

def web_server_links_dir
  config_dir + "links"
end

#web_server_sub_uri_apps_dirObject



224
225
226
# File 'lib/web_server_config_generator/generator.rb', line 224

def web_server_sub_uri_apps_dir
  config_dir + "sub_uri_apps"
end

#web_server_vhost_nginx_confObject



190
191
192
# File 'lib/web_server_config_generator/generator.rb', line 190

def web_server_vhost_nginx_conf
  web_server_vhost_nginx_dir + "projects.conf"
end

#write_conf_filesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/web_server_config_generator/generator.rb', line 18

def write_conf_files
  FileUtils.cd projects_dir do
    # generate files for each proj/env
    list_of_conf_files = []
    app_projects.each do |p|
      environment_map[p].each do |env|
        list_of_conf_files << write_conf_file(p, env)
      end
    end
    
    sub_uri_projects.each do |p|
      create_sub_uri_links_dir p
      list_of_conf_files << write_conf_file(p, p.env)
    end

    current_lines = []
    web_server_vhost_nginx_conf.read.each_line { |l| current_lines << l }
    new_lines = current_lines + list_of_conf_files.map { |p| "include #{p};\n" }
    new_lines.uniq!
    new_lines.sort!
    web_server_vhost_nginx_conf.write(new_lines.join)
  end
end