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 = "  WATCH = \"\#{watch}\"\n  START_CMD = \"\#{start_cmd}\"\n  STOP_CMD = \"\#{stop_cmd}\"\n  PORTS = [\#{ports.join(', ')}]\n\n"

  body = "  PORTS.each do |port|\n    God.watch do |w|\n      w.name = \"appscale-\#{WATCH}-\#{port}\"\n      w.group = WATCH\n      w.interval = 30.seconds # default      \n      w.start = START_CMD\n      w.stop = STOP_CMD\n      w.start_grace = 20.seconds\n      w.restart_grace = 20.seconds\n      w.log = \"/var/log/appscale/\#{WATCH}-\#{port}.log\"\n      w.pid_file = \"/var/appscale/\#{WATCH}-\#{port}.pid\"\n  \n      w.behavior(:clean_pid_file)\n\n      w.start_if do |start|\n        start.condition(:process_running) do |c|\n          c.running = false\n        end\n      end\n  \n      w.restart_if do |restart|\n        restart.condition(:memory_usage) do |c|\n          c.above = 150.megabytes\n          c.times = [3, 5] # 3 out of 5 intervals\n        end\n  \n        restart.condition(:cpu_usage) do |c|\n          c.above = 50.percent\n          c.times = 5\n        end\n      end\n  \n      # lifecycle\n      w.lifecycle do |on|\n        on.condition(:flapping) do |c|\n          c.to_state = [:start, :restart]\n          c.times = 5\n          c.within = 5.minute\n          c.transition = :unmonitored\n          c.retry_in = 10.minutes\n          c.retry_times = 5\n          c.retry_within = 2.hours\n        end\n      end\n"

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

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

    body += "\n      w.env = {\n        \#{env_vars_str}\n      }\n"
  end

  epilogue = "    end\n  end\n"

  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