1
1
<?php
2
2
3
3
4
- // restarts all servers (shuts down and starts)
5
- function restart_servers ($ pdo , $ output = true )
6
- {
7
- global $ SERVER_IP ;
8
-
9
- // find servers
10
- $ servers = servers_select ($ pdo );
11
-
12
- // output
13
- if ($ output === true ) {
14
- output ('Restarting all active servers... ' );
15
- }
16
-
17
- // shut down and restart all servers
18
- foreach ($ servers as $ server ) {
19
- if ($ server ->address == $ SERVER_IP ) {
20
- restart_server (PR2_ROOT . '/pr2.php ' , $ server ->address , $ server ->port , $ server ->salt , $ server ->server_id );
21
- } else {
22
- output ("ERROR: Server # $ server ->server_id 's address doesn't match the server IP. " );
23
- continue ;
24
- }
25
- }
26
-
27
- // output
28
- if ($ output === true ) {
29
- output ('All server restart operations complete. ' );
30
- }
31
- }
32
-
33
-
34
- // restart a server
35
- function restart_server ($ script , $ address , $ port , $ salt , $ server_id , $ verbose = false )
36
- {
37
- $ pid = read_pid ($ port );
38
- shut_down_server ($ pid , $ address , $ port , $ salt );
39
- sleep (1 );
40
- start_server ($ script , $ port , $ server_id , $ verbose );
41
- }
42
-
43
-
44
- // test all servers and start the ones that aren't running
45
- function check_servers ($ pdo )
46
- {
47
- global $ SERVER_IP , $ COMM_PASS ;
48
-
49
- // test the policy server
50
- test_server (ROOT_DIR . '/policy_server/run_policy.php ' , 'localhost ' , 843 , $ COMM_PASS , 0 );
51
-
52
- // load all active servers
53
- $ servers = servers_select ($ pdo );
54
-
55
- // test all active servers at this address
56
- foreach ($ servers as $ server ) {
57
- if ($ server ->address == $ SERVER_IP ) {
58
- output ("Testing $ server ->server_name (ID # $ server ->server_id )... " );
59
- test_server (PR2_ROOT . '/pr2.php ' , 'localhost ' , $ server ->port , $ server ->salt , $ server ->server_id );
60
- }
61
- }
62
-
63
- // tell it to the world
64
- output ('All servers tested. ' );
65
- return $ servers ;
66
- }
67
-
68
-
69
- // starts a server if it is not running
70
- function test_server ($ script , $ address , $ port , $ salt , $ server_id )
71
- {
72
- // tell the world
73
- output ("Beginning test for server # $ server_id at $ address: $ port ( $ script) " );
74
-
75
- // connect
76
- $ result = connect_to_server ($ address , $ port , $ salt );
77
-
78
- // test the server if able to connect
79
- if ($ result !== false ) {
80
- output ("GOOD - Server # $ server_id is running. " );
81
- } else {
82
- output ("BAD - Bad/No response from server # $ server_id. " );
83
- restart_server ($ script , $ address , $ port , $ salt , $ server_id );
84
- }
85
-
86
- // tell the world
87
- output ("Test of server # $ server_id at $ address: $ port complete. \n" );
88
- }
89
-
90
-
91
- // starts a server
92
- function start_server ($ script , $ port , $ server_id , $ verbose = false , $ silent = false )
93
- {
94
- if (!$ silent ) {
95
- output ("STARTING SERVER | Script: $ script | Port: $ port " );
96
- }
97
-
98
- $ log = ROOT_DIR . '/../pr2/log/ ' . $ port . '- ' . date ("Fj-Y-g:ia " ) . '.log ' ;
99
- $ verbose = $ verbose === true ? 'true ' : '' ;
100
- $ command = "nohup php $ script $ server_id $ verbose > $ log & echo $! " ;
101
- if (!$ silent ) {
102
- output ("Executing command: $ command " );
103
- }
104
- $ pid = exec ($ command );
105
-
106
- write_pid ($ pid , $ port , $ silent );
107
- return $ pid ;
108
- }
109
-
110
-
111
4
// tests server connectivity
112
5
function connect_to_server ($ address , $ port , $ salt )
113
6
{
@@ -117,87 +10,6 @@ function connect_to_server($address, $port, $salt)
117
10
}
118
11
119
12
120
- // graceful shutdown
121
- function shut_down_server ($ pid , $ address , $ port , $ salt )
122
- {
123
- $ result = talk_to_server ($ address , $ port , $ salt , 'shut_down` ' , true );
124
-
125
- // make sure the port is dead
126
- if (!$ result ) {
127
- $ kill_res = kill_pid ($ pid );
128
- }
129
-
130
- // tell the world
131
- if (@$ kill_res === true ) {
132
- output ("Shutdown of server running at $ address: $ port successful. " );
133
- } else {
134
- output ("A new server can now be started on port $ port. " );
135
- }
136
- }
137
-
138
-
139
- // gets the pid file
140
- function get_pid_file ($ port )
141
- {
142
- $ pid_file = ROOT_DIR . '/../pr2/pid/ ' . $ port . '.txt ' ;
143
- return $ pid_file ;
144
- }
145
-
146
-
147
- // write pid to a file
148
- function write_pid ($ pid , $ port , $ silent = false )
149
- {
150
- $ pid_file = get_pid_file ($ port );
151
- if (!$ silent ) {
152
- output ("Writing PID for port $ port to $ pid_file: $ pid... " );
153
- }
154
- $ handle = fopen ($ pid_file , 'w ' );
155
- if ($ handle ) {
156
- fwrite ($ handle , $ pid );
157
- fclose ($ handle );
158
- }
159
- if (!$ silent ) {
160
- output ("Write operation concluded. " );
161
- }
162
- }
163
-
164
-
165
- // read pid from file
166
- function read_pid ($ port )
167
- {
168
- $ pid_file = get_pid_file ($ port );
169
- $ pid = 0 ;
170
- output ("Reading the PID from $ pid_file... " );
171
- $ handle = fopen ($ pid_file , 'r ' );
172
- if ($ handle !== false ) {
173
- $ pid = fread ($ handle , 999 );
174
- fclose ($ handle );
175
- output ("PID is: $ pid " );
176
- } else {
177
- output ("The PID file does not exist. " );
178
- }
179
- return $ pid ;
180
- }
181
-
182
-
183
- // kills a port
184
- function kill_pid ($ pid )
185
- {
186
- if ($ pid != null && $ pid != 0 && $ pid != '' && $ pid != false ) {
187
- system ("kill " . $ pid , $ k );
188
- $ pid = null ;
189
- if (!$ k ) {
190
- return true ;
191
- } else {
192
- return false ;
193
- }
194
- } else {
195
- output ('There is no PID to kill. ' );
196
- return true ;
197
- }
198
- }
199
-
200
-
201
13
// send a command to the server
202
14
function talk_to_server ($ address , $ port , $ salt , $ process_function , $ receive = true , $ output = true )
203
15
{
0 commit comments