Merge in util update
[awesomized/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <libtest/common.h>
23
24 #include <cassert>
25 #include <cerrno>
26 #include <cstdlib>
27 #include <iostream>
28
29 #include <algorithm>
30 #include <functional>
31 #include <locale>
32
33 // trim from end
34 static inline std::string &rtrim(std::string &s)
35 {
36 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
37 return s;
38 }
39
40 namespace libtest {
41
42 void server_startup_st::push_server(Server *arg)
43 {
44 servers.push_back(arg);
45
46 char port_str[NI_MAXSERV];
47 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
48
49 std::string server_config_string;
50 if (arg->has_socket())
51 {
52 server_config_string+= "--socket=";
53 server_config_string+= '"';
54 server_config_string+= arg->socket();
55 server_config_string+= '"';
56 server_config_string+= " ";
57 }
58 else
59 {
60 server_config_string+= "--server=";
61 server_config_string+= arg->hostname();
62 server_config_string+= ":";
63 server_config_string+= port_str;
64 server_config_string+= " ";
65 }
66
67 server_list+= server_config_string;
68
69 }
70
71 Server* server_startup_st::pop_server()
72 {
73 Server *tmp= servers.back();
74 servers.pop_back();
75 return tmp;
76 }
77
78 bool server_startup_st::shutdown(uint32_t number_of_host)
79 {
80 assert(servers.size() > number_of_host);
81 if (servers.size() > number_of_host)
82 {
83 Server* tmp= servers[number_of_host];
84
85 if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
86 { }
87 else
88 {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 void server_startup_st::shutdown_and_remove()
97 {
98 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
99 {
100 delete *iter;
101 }
102 servers.clear();
103 }
104
105 void server_startup_st::shutdown()
106 {
107 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
108 {
109 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
110 {
111 Error << "Unable to kill:" << *(*iter);
112 }
113 }
114 }
115
116 void server_startup_st::restart()
117 {
118 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
119 {
120 (*iter)->start();
121 }
122 }
123
124 server_startup_st::~server_startup_st()
125 {
126 shutdown_and_remove();
127 }
128
129 bool server_startup_st::is_debug() const
130 {
131 return bool(getenv("LIBTEST_MANUAL_GDB"));
132 }
133
134 bool server_startup_st::is_valgrind() const
135 {
136 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
137 }
138
139 bool server_startup_st::is_helgrind() const
140 {
141 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
142 }
143
144
145 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
146 {
147 Outn();
148 (void)try_port;
149
150 set_max_port(try_port);
151
152 // Look to see if we are being provided ports to use
153 {
154 char variable_buffer[1024];
155 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
156
157 char *var;
158 if ((var= getenv(variable_buffer)))
159 {
160 in_port_t tmp= in_port_t(atoi(var));
161
162 if (tmp > 0)
163 try_port= tmp;
164 }
165 }
166
167 libtest::Server *server= NULL;
168 if (0)
169 { }
170 else if (server_type.compare("gearmand") == 0)
171 {
172 if (GEARMAND_BINARY)
173 {
174 if (HAVE_LIBGEARMAN)
175 {
176 server= build_gearmand("localhost", try_port);
177 }
178 else
179 {
180 Error << "Libgearman was not found";
181 }
182 }
183 else
184 {
185 Error << "No gearmand binary is available";
186 }
187 }
188 else if (server_type.compare("blobslap_worker") == 0)
189 {
190 if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
191 {
192 if (HAVE_LIBGEARMAN)
193 {
194 server= build_blobslap_worker(try_port);
195 }
196 else
197 {
198 Error << "Libgearman was not found";
199 }
200 }
201 else
202 {
203 Error << "No gearmand binary is available";
204 }
205 }
206 else if (server_type.compare("memcached-sasl") == 0)
207 {
208 if (MEMCACHED_SASL_BINARY)
209 {
210 if (HAVE_LIBMEMCACHED)
211 {
212 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
213 }
214 else
215 {
216 Error << "Libmemcached was not found";
217 }
218 }
219 else
220 {
221 Error << "No memcached binary that was compiled with sasl is available";
222 }
223 }
224 else if (server_type.compare("memcached") == 0)
225 {
226 if (MEMCACHED_BINARY)
227 {
228 if (HAVE_LIBMEMCACHED)
229 {
230 server= build_memcached("localhost", try_port);
231 }
232 else
233 {
234 Error << "Libmemcached was not found";
235 }
236 }
237 else
238 {
239 Error << "No memcached binary is available";
240 }
241 }
242 else
243 {
244 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
245 }
246
247 if (server == NULL)
248 {
249 Error << "Failure occured while creating server: " << server_type;
250 return false;
251 }
252
253 /*
254 We will now cycle the server we have created.
255 */
256 if (not server->cycle())
257 {
258 Error << "Could not start up server " << *server;
259 delete server;
260 return false;
261 }
262
263 server->build(argc, argv);
264
265 if (construct.is_debug())
266 {
267 Out << "Pausing for startup, hit return when ready.";
268 std::string gdb_command= server->base_command();
269 std::string options;
270 Out << "run " << server->args(options);
271 getchar();
272 }
273 else if (not server->start())
274 {
275 Error << "Failed to start " << *server;
276 delete server;
277 return false;
278 }
279 else
280 {
281 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
282 }
283
284 construct.push_server(server);
285
286 if (default_port() == 0)
287 {
288 assert(server->has_port());
289 set_default_port(server->port());
290 }
291
292 Outn();
293
294 return true;
295 }
296
297 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
298 {
299 (void)try_port;
300 Outn();
301
302 Server *server= NULL;
303 if (0)
304 { }
305 else if (server_type.compare("gearmand") == 0)
306 {
307 Error << "Socket files are not supported for gearmand yet";
308 }
309 else if (server_type.compare("memcached-sasl") == 0)
310 {
311 if (MEMCACHED_SASL_BINARY)
312 {
313 if (HAVE_LIBMEMCACHED)
314 {
315 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
316 }
317 else
318 {
319 Error << "Libmemcached was not found";
320 }
321 }
322 else
323 {
324 Error << "No memcached binary is available";
325 }
326 }
327 else if (server_type.compare("memcached") == 0)
328 {
329 if (MEMCACHED_BINARY)
330 {
331 if (HAVE_LIBMEMCACHED)
332 {
333 server= build_memcached_socket("localhost", try_port);
334 }
335 else
336 {
337 Error << "Libmemcached was not found";
338 }
339 }
340 else
341 {
342 Error << "No memcached binary is available";
343 }
344 }
345 else
346 {
347 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
348 }
349
350 if (server == NULL)
351 {
352 Error << "Failure occured while creating server: " << server_type;
353 return false;
354 }
355
356 /*
357 We will now cycle the server we have created.
358 */
359 if (not server->cycle())
360 {
361 Error << "Could not start up server " << *server;
362 delete server;
363 return false;
364 }
365
366 server->build(argc, argv);
367
368 if (is_debug())
369 {
370 Out << "Pausing for startup, hit return when ready.";
371 std::string gdb_command= server->base_command();
372 std::string options;
373 Out << "run " << server->args(options);
374 getchar();
375 }
376 else if (not server->start())
377 {
378 Error << "Failed to start " << *server;
379 delete server;
380 return false;
381 }
382 else
383 {
384 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
385 }
386
387 push_server(server);
388
389 set_default_socket(server->socket().c_str());
390
391 Outn();
392
393 return true;
394 }
395
396 std::string server_startup_st::option_string() const
397 {
398 std::string temp= server_list;
399 rtrim(temp);
400 return temp;
401 }
402
403
404 } // namespace libtest