-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathterminate.rb
145 lines (122 loc) · 4.55 KB
/
terminate.rb
1
2
3
4
5
6
7
8
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require 'find'
require 'pty'
$:.unshift File.join(File.dirname(__FILE__), "lib")
require 'fileutils'
APPSCALE_CONFIG_DIR = "/etc/appscale"
module TerminateHelper
# Erases all AppScale-related files (except database state) from the local
# filesystem. This is used when appscale is shutdown.
# TODO: Use FileUtils.rm_rf instead of backticks throughout this
# method.
def self.erase_appscale_state
`rm -f #{APPSCALE_CONFIG_DIR}/secret.key`
`rm -f /tmp/uploaded-apps`
`rm -f ~/.appscale_cookies`
`rm -f /etc/nginx/sites-enabled/appscale-*.conf`
`rm -f /etc/haproxy/sites-enabled/*.cfg`
`service nginx reload`
begin
PTY.spawn('appscale-stop-services') do |stdout, _, _|
begin
stdout.each { |line| print line }
rescue Errno::EIO
# The process has likely finished giving output.
end
end
rescue PTY::ChildExited
# The process has finished.
end
`rm -f /etc/monit/conf.d/appscale*.cfg`
`rm -f /etc/monit/conf.d/controller-17443.cfg`
# Stop datastore servers.
datastore_slice = '/sys/fs/cgroup/systemd/appscale.slice'\
'/appscale-datastore.slice'
begin
Find.find(datastore_slice) do |path|
next unless File.basename(path) == 'cgroup.procs'
File.readlines(path).each do |pid|
`kill #{pid}`
end
end
rescue Errno::ENOENT
# If there are no processes running, there is no need to stop them.
end
`rm -f /etc/logrotate.d/appscale-*`
# Let's make sure we restart any non-appscale service.
`service monit restart`
`service appscale-controller stop`
`rm -f #{APPSCALE_CONFIG_DIR}/port-*.txt`
# Remove location files.
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/all_ips")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/load_balancer_ips")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/login_ip")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/masters")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/memcache_ips")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/my_private_ip")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/my_public_ip")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/num_of_nodes")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/search_ip")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/slaves")
FileUtils.rm_f("#{APPSCALE_CONFIG_DIR}/taskqueue_nodes")
# TODO: Use the constant in djinn.rb (ZK_LOCATIONS_JSON_FILE)
`rm -rf #{APPSCALE_CONFIG_DIR}/zookeeper_locations.json`
`rm -rf #{APPSCALE_CONFIG_DIR}/zookeeper_locations`
`rm -f /opt/appscale/appcontroller-state.json`
`rm -f /opt/appscale/appserver-state.json`
print "OK"
end
# This functions does erase more of appscale state: used in combination
# with 'clean'.
def self.erase_appscale_full_state
# Delete logs.
`rm -rf /var/log/appscale/*`
# Restart rsyslog so that the combined app logs can be recreated.
`service rsyslog restart`
`rm -rf /var/log/rabbitmq/*`
`rm -rf /var/log/zookeeper/*`
`rm -rf /var/log/nginx/appscale-*`
# Delete running state.
`rm -rf /var/apps/`
`rm -rf #{APPSCALE_CONFIG_DIR}/*.pid`
`rm -rf /tmp/ec2/*`
`rm -rf /tmp/*started`
`rm -rf /etc/cron.d/appscale-*`
# Delete stored data.
`rm -rf /opt/appscale/cassandra`
`rm -rf /opt/appscale/zookeeper`
`rm -rf /opt/appscale/logserver/*`
`rm -rf /opt/appscale/apps`
`rm -rf /opt/appscale/solr`
`rm -rf /var/lib/rabbitmq/*`
`rm -rf /etc/appscale/celery/`
`rm -rf /opt/appscale/celery`
print "OK"
end
# Tells any services that persist data across AppScale runs to stop writing
# new data to the filesystem, since killing them is imminent.
#
# For right now, this is just Cassandra and ZooKeeper.
def self.disable_database_writes
# First, tell Cassandra that no more writes should be accepted on this node.
ifconfig = `ifconfig`
bound_addrs = ifconfig.scan(/inet addr:(\d+.\d+.\d+.\d+)/).flatten
bound_addrs.delete("127.0.0.1")
ip = bound_addrs[0]
# Make sure we have cassandra running, otherwise nodetool may get
# stuck.
if system("monit summary | grep cassandra | grep Running > /dev/null")
`/opt/cassandra/cassandra/bin/nodetool -h #{ip} -p 7199 drain`
end
# Next, stop ZooKeeper politely: we stop it with both new and old
# script to be sure.
`service zookeeper-server stop`
`service zookeeper stop`
end
end
if __FILE__ == $0
TerminateHelper.disable_database_writes
TerminateHelper.erase_appscale_state
if ARGV.length == 1 and ARGV[0] == "clean"
TerminateHelper.erase_appscale_full_state
end
end