Module: GodInterface

Defined in:
lib/godinterface.rb

Class Method Summary collapse

Class Method Details

.remove(watch, remote_ip = nil, remote_key = nil) ⇒ Object



129
130
131
# File 'lib/godinterface.rb', line 129

def self.remove(watch, remote_ip=nil, remote_key=nil)
  self.run_god_command("god remove #{watch}", remote_ip, remote_key)
end

.shutdown(remote_ip = nil, remote_key = nil) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/godinterface.rb', line 133

def self.shutdown(remote_ip=nil, remote_key=nil)
  %w{ uaserver pbserver memcached blobstore monitr loadbalancer }.each { |service|
    self.run_god_command("god stop #{service}", remote_ip, remote_key)
  }

  self.run_god_command("god terminate", remote_ip, remote_key)
end

.start(watch, start_cmd, stop_cmd, ports, env_vars = nil, remote_ip = nil, remote_key = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/godinterface.rb', line 9

def self.start(watch, start_cmd, stop_cmd, ports, env_vars=nil, remote_ip=nil, remote_key=nil)

  ports = [ports] unless ports.class == Array

  prologue = <<BOO
  WATCH = "#{watch}"
  START_CMD = "#{start_cmd}"
  STOP_CMD = "#{stop_cmd}"
  PORTS = [#{ports.join(', ')}]

BOO

  body = <<'BAZ'
  PORTS.each do |port|
    God.watch do |w|
      w.name = "appscale-#{WATCH}-#{port}"
      w.group = WATCH
      w.interval = 30.seconds # default      
      w.start = START_CMD
      w.stop = STOP_CMD
      w.start_grace = 20.seconds
      w.restart_grace = 20.seconds
      w.log = "/var/log/appscale/#{WATCH}-#{port}.log"
      w.pid_file = "/var/appscale/#{WATCH}-#{port}.pid"
  
      w.behavior(:clean_pid_file)

      w.start_if do |start|
        start.condition(:process_running) do |c|
          c.running = false
        end
      end
  
      w.restart_if do |restart|
        restart.condition(:memory_usage) do |c|
          c.above = 150.megabytes
          c.times = [3, 5] # 3 out of 5 intervals
        end
  
        restart.condition(:cpu_usage) do |c|
          c.above = 50.percent
          c.times = 5
        end
      end
  
      # lifecycle
      w.lifecycle do |on|
        on.condition(:flapping) do |c|
          c.to_state = [:start, :restart]
          c.times = 5
          c.within = 5.minute
          c.transition = :unmonitored
          c.retry_in = 10.minutes
          c.retry_times = 5
          c.retry_within = 2.hours
        end
      end
BAZ

  if !env_vars.nil? and !env_vars.empty?
    env_vars_str = ""

    env_vars.each { |k, v|
      env_vars_str += "          \"" + k + "\" => \"" + v + "\",\n"
    }

    body += <<BOO

      w.env = {
        #{env_vars_str}
      }
BOO
  end

  epilogue = <<BAZ
    end
  end
BAZ

  config_file = prologue + body + epilogue
  tempfile = "/tmp/god-#{rand(10000)}.god"

  CommonFunctions.write_file(tempfile, config_file)

  if remote_ip
    CommonFunctions.scp_file(tempfile, tempfile, remote_ip, remote_key)
  end

  if remote_ip
    ip = remote_ip
  else
    ip = CommonFunctions.local_ip
  end

  #unless CommonFunctions.is_port_open?(ip, GOD_PORT, use_ssl=false)
  #  self.run_god_command("god", remote_ip, remote_key)
  #  sleep(5)
  #end

  self.run_god_command("god load #{tempfile}", remote_ip, remote_key)

  sleep(5)

  FileUtils.rm_f(tempfile)
  if remote_ip
    remove = "rm -rf #{tempfile}"
    CommonFunctions.run_remote_command(ip, remove, remote_key, false)
  end

  #god_info = "Starting #{watch} on ip #{ip}, port #{ports.join(', ')}" +
  #  " with start command [#{start_cmd}] and stop command [#{stop_cmd}]"
  #puts god_info

  self.run_god_command("god start #{watch}", remote_ip, remote_key)
end

.start_god(remote_ip, remote_key) ⇒ Object



5
6
7
# File 'lib/godinterface.rb', line 5

def self.start_god(remote_ip, remote_key)
  self.run_god_command("god &", remote_ip, remote_key)
end

.stop(watch, remote_ip = nil, remote_key = nil) ⇒ Object



125
126
127
# File 'lib/godinterface.rb', line 125

def self.stop(watch, remote_ip=nil, remote_key=nil)
  self.run_god_command("god stop #{watch}", remote_ip, remote_key)
end