Class: Seesaw::Configure

Inherits:
Object
  • Object
show all
Includes:
Mongrel::Command::Base, CommandBase
Defined in:
lib/seesaw/init.rb

Instance Method Summary collapse

Methods included from CommandBase

#log, #parse_options, #restart_http, #restart_mongrels, #shutdown_half_cluster, #start_cluster, #start_mongrels, #stop_mongrels, #switch_to_half_cluster, #symlink

Instance Method Details

#configureObject



141
142
143
144
145
146
147
148
149
# File 'lib/seesaw/init.rb', line 141

def configure 
  options [
    ['-N', '--num-servers INT', "Number of Mongrel servers", :@servers, 2],
    ['-C', '--config PATH', "Path to seesaw configuration file", :@config_file, 'config/seesaw.yml'],
    ['-M', '--mongrel PATH', "Path to mongrel cluster configuration file", :@mongrel_config_file, 'config/mongrel_cluster.yml'],
    ['', '--http_config PATH', "Path to the http server configuration includes", :@http_config_path, "config/http_cluster"],
    ['', '--server TYPE', "Webserver to generate example configuration for. TYPE can be nginx or apache. Default is apache.", :@server, 'apache']
  ]
end

#runObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/seesaw/init.rb', line 174

def run
  restart_cmds = {
    'nginx' => "sudo kill -HUP `cat /opt/local/var/log/nginx.pid`",
    'apache' => "sudo apachectl graceful"        
  }
  
  @options = {}
  
  @options["config_path"] = @http_config_path
  @options["restart_cmd"] = restart_cmds[@server]
  @options["symlink_cmd"] = "ln -sf"
  @options["config_symlink"] = "cluster.conf"
  @options["config_files"] = {}
  @options["config_files"]["all"] = "cluster_all.conf"
  @options["config_files"][1] = "cluster_1.conf"
  @options["config_files"][2] = "cluster_2.conf"      
  
  # write seesaw configuration
  log "Writing configuration file to #{@config_file}."
  File.open(@config_file,"w") {|f| f.write(@options.to_yaml)}
  
  # read mongrel cluster configuration to find number of servers
  mongrel_conf = YAML.load(File.read(@mongrel_config_file))
  
  # create cluster config directory if it doesn't exist
  Dir.mkdir(@http_config_path) unless File.exist?(@http_config_path)
  
  # write cluster configuration files for appropriate server
  start_port = mongrel_conf["port"].to_i 
  end_port = mongrel_conf["port"].to_i + mongrel_conf["servers"].to_i  - 1
  mid_port = ((end_port+start_port)/2.0).ceil
  
  # full cluster
  write_config_include(File.join(@http_config_path,@options["config_files"]["all"]), (start_port..end_port))
  
  # front half
  write_config_include(File.join(@http_config_path,@options["config_files"][1]), (start_port..mid_port-1))
  
  # back half
  write_config_include(File.join(@http_config_path,@options["config_files"][2]), (mid_port..end_port))
  
  # symlink
  system("#{@options["symlink_cmd"]} #{File.expand_path(File.join(@http_config_path,@options["config_files"]["all"]))} #{File.join(@http_config_path,@options["config_symlink"])}")
  
  log "Don't forget to include #{File.expand_path("#{@http_config_path}/cluster.conf")} in your webserver configuration."
end

#validateObject



151
152
153
154
155
156
157
# File 'lib/seesaw/init.rb', line 151

def validate
  valid?(['nginx','apache'].include?(@server), "Server must be nginx or apache")
  valid_dir? File.dirname(@config_file), "Path to config file not valid: #{@config_file}"
  valid?(File.exist?(@mongrel_config_file), "Path to mongrel cluster config file not valid: #{@mongrel_config_file}")
  
  return @valid
end

#write_config_include(file, ports) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/seesaw/init.rb', line 159

def write_config_include(file, ports)
  
  log "writing #{file}"
  
  ip = "127.0.0.1"
  server_lines = ""
  line_template = ERB.new(INCLUDES[@server][:server_line])
  ports.each do |port|
    server_lines << line_template.result(binding)
  end
  
  template = ERB.new(INCLUDES[@server][:template])
  File.open(file, "w") { |f| f.write(template.result(binding)) }
end