Module: Seesaw::CommandBase

Included in:
Bounce, Configure, Switch
Defined in:
lib/seesaw/init.rb

Instance Method Summary collapse

Instance Method Details

#configureObject



89
90
91
92
93
# File 'lib/seesaw/init.rb', line 89

def configure
  options [
   ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/seesaw.yml"],
  ]
end

#log(message) ⇒ Object



13
14
15
# File 'lib/seesaw/init.rb', line 13

def log(message)
  puts message
end

#parse_optionsObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/seesaw/init.rb', line 71

def parse_options
  @options = YAML.load(File.read(@config_file))
  @config_path = @options["config_path"] || "/opt/local/etc/apache"
  @config_files = @options["config_files"] || {}
  @config_files[1] ||= "cluster_1.conf"
  @config_files[2] ||= "cluster_2.conf"
  @config_files["all"] ||= "cluster_all.conf"
  @config_symlink = @options["config_symlink"] || "cluster.conf"
  @config_symlink = File.join(@config_path, @config_symlink)
  @restart_cmd = @options["restart_cmd"] || "apachectl graceful"
  @symlink_cmd = @options["symlink_cmd"] || "ln -sf"
end

#restart_http(cluster = nil) ⇒ Object



45
46
47
48
# File 'lib/seesaw/init.rb', line 45

def restart_http(cluster=nil)
  log "webserver restart"
  system(@restart_cmd)
end

#restart_mongrels(cluster = nil) ⇒ Object



23
24
25
26
27
# File 'lib/seesaw/init.rb', line 23

def restart_mongrels(cluster=nil)
  log "restart mongrels #{cluster}"
  stop_mongrels(cluster)
  start_mongrels(cluster)
end

#shutdown_half_cluster(half) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/seesaw/init.rb', line 50

def shutdown_half_cluster(half)
  log "shutdown half #{half}"
  other_half = half == 1 ? 2 : 1
  symlink(other_half)
  restart_http
  stop_mongrels(half)
end

#start_clusterObject



65
66
67
68
69
# File 'lib/seesaw/init.rb', line 65

def start_cluster
  log "start cluster"
  symlink("all")
  restart_http
end

#start_mongrels(cluster = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/seesaw/init.rb', line 29

def start_mongrels(cluster=nil)
  log "start mongrels #{cluster}"
  cmd = "mongrel_rails seesaw::start"
  cmd << " --cluster #{cluster}" if cluster && cluster != "all"
  cmd << " -C #{@options["mongrel_config_file"]}" if @options["mongrel_config_file"]
  system(cmd)
end

#stop_mongrels(cluster = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/seesaw/init.rb', line 37

def stop_mongrels(cluster=nil)
  log "stop mongrels #{cluster}"
  cmd = "mongrel_rails seesaw::stop"
  cmd << " --cluster #{cluster}" if cluster
  cmd << " -C #{@options["mongrel_config_file"]}" if @options["mongrel_config_file"]
  system(cmd)
end

#switch_to_half_cluster(half) ⇒ Object



58
59
60
61
62
63
# File 'lib/seesaw/init.rb', line 58

def switch_to_half_cluster(half)
  log "switch_to_half_cluster #{half}"
  other_half = half == 1 ? 2 : 1
  shutdown_half_cluster(other_half)
  start_mongrels(other_half)
end


17
18
19
20
21
# File 'lib/seesaw/init.rb', line 17

def symlink(cluster)
  log "symlink to #{cluster}"
  file = File.join(@config_path, @config_files[cluster])
  system("#{@symlink_cmd} #{file} #{@config_symlink}")
end

#validateObject



84
85
86
87
# File 'lib/seesaw/init.rb', line 84

def validate
  valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails seesaw::configure.")
  return @valid
end