22 #include <Properties.hpp>
23 #include <socket_io.h>
25 #include "APIService.hpp"
28 #include <OutputStream.hpp>
43 #define CPCD_CMD(name, fun, desc) \
46 ParserRow<CPCDAPISession>::Cmd, \
47 ParserRow<CPCDAPISession>::String, \
48 ParserRow<CPCDAPISession>::Optional, \
49 ParserRow<CPCDAPISession>::IgnoreMinMax, \
54 #define CPCD_ARG(name, type, opt, desc) \
57 ParserRow<CPCDAPISession>::Arg, \
58 ParserRow<CPCDAPISession>::type, \
59 ParserRow<CPCDAPISession>::opt, \
60 ParserRow<CPCDAPISession>::IgnoreMinMax, \
65 #define CPCD_ARG2(name, type, opt, min, max, desc) \
68 ParserRow<CPCDAPISession>::Arg, \
69 ParserRow<CPCDAPISession>::type, \
70 ParserRow<CPCDAPISession>::opt, \
71 ParserRow<CPCDAPISession>::IgnoreMinMax, \
79 ParserRow<CPCDAPISession>::Arg, \
80 ParserRow<CPCDAPISession>::Int, \
81 ParserRow<CPCDAPISession>::Optional, \
82 ParserRow<CPCDAPISession>::IgnoreMinMax, \
87 #define CPCD_CMD_ALIAS(name, realName, fun) \
90 ParserRow<CPCDAPISession>::CmdAlias, \
91 ParserRow<CPCDAPISession>::Int, \
92 ParserRow<CPCDAPISession>::Optional, \
93 ParserRow<CPCDAPISession>::IgnoreMinMax, \
98 #define CPCD_ARG_ALIAS(name, realName, fun) \
101 ParserRow<CPCDAPISession>::ArgAlias, \
102 ParserRow<CPCDAPISession>::Int, \
103 ParserRow<CPCDAPISession>::Optional, \
104 ParserRow<CPCDAPISession>::IgnoreMinMax, \
112 CPCD_CMD(
"define process" , &CPCDAPISession::defineProcess,
""),
113 CPCD_ARG(
"id", Int, Optional,
"Id of process."),
114 CPCD_ARG(
"name",
String, Mandatory,
"Name of process"),
115 CPCD_ARG(
"group",
String, Mandatory,
"Group of process"),
116 CPCD_ARG(
"env",
String, Optional,
"Environment variables for process"),
117 CPCD_ARG(
"path",
String, Mandatory,
"Path to binary"),
118 CPCD_ARG(
"args",
String, Optional,
"Arguments to process"),
119 CPCD_ARG(
"type",
String, Mandatory,
"Type of process"),
120 CPCD_ARG(
"cwd",
String, Mandatory,
"Working directory of process"),
121 CPCD_ARG(
"owner",
String, Mandatory,
"Owner of process"),
122 CPCD_ARG(
"runas",
String, Optional,
"Run as user"),
123 CPCD_ARG(
"stdout",
String, Optional,
"Redirection of stdout"),
124 CPCD_ARG(
"stderr",
String, Optional,
"Redirection of stderr"),
125 CPCD_ARG(
"stdin",
String, Optional,
"Redirection of stderr"),
126 CPCD_ARG(
"ulimit",
String, Optional,
"ulimit"),
127 CPCD_ARG(
"shutdown",
String, Optional,
"shutdown options"),
129 CPCD_CMD(
"undefine process", &CPCDAPISession::undefineProcess,
""),
130 CPCD_CMD_ALIAS(
"undef",
"undefine process", 0),
131 CPCD_ARG(
"id", Int, Mandatory,
"Id of process"),
132 CPCD_ARG_ALIAS(
"i",
"id", 0),
134 CPCD_CMD(
"start process", &CPCDAPISession::startProcess,
""),
135 CPCD_ARG(
"id", Int, Mandatory,
"Id of process"),
137 CPCD_CMD(
"stop process", &CPCDAPISession::stopProcess,
""),
138 CPCD_ARG(
"id", Int, Mandatory,
"Id of process"),
140 CPCD_CMD(
"list processes", &CPCDAPISession::listProcesses,
""),
142 CPCD_CMD(
"show version", &CPCDAPISession::showVersion,
""),
146 CPCDAPISession::CPCDAPISession(NDB_SOCKET_TYPE sock,
156 CPCDAPISession::CPCDAPISession(FILE * f,
CPCD & cpcd)
165 CPCDAPISession::~CPCDAPISession() {
173 CPCDAPISession::runSession(){
176 m_parser->
run(ctx, *
this);
177 if(ctx.m_currentToken == 0)
180 m_input->reset_timeout();
181 m_output->reset_timeout();
183 switch(ctx.m_status){
185 for(
size_t i = 0;
i<ctx.m_aliasUsed.size();
i++)
186 ndbout_c(
"Used alias: %s -> %s",
187 ctx.m_aliasUsed[
i]->name, ctx.m_aliasUsed[
i]->realName);
189 case Parser_t::NoLine:
190 case Parser_t::EmptyLine:
196 NDB_CLOSE_SOCKET(m_socket);
200 CPCDAPISession::stopSession(){
202 for(
size_t i = 0;
i<m_temporaryProcesses.size();
i++){
203 Uint32
id = m_temporaryProcesses[
i];
209 CPCDAPISession::loadFile(){
212 m_parser->
run(ctx, *
this);
213 if(ctx.m_currentToken == 0)
216 switch(ctx.m_status){
218 for(
size_t i = 0;
i<ctx.m_aliasUsed.size();
i++)
219 ndbout_c(
"Used alias: %s -> %s",
220 ctx.m_aliasUsed[
i]->name, ctx.m_aliasUsed[
i]->realName);
222 case Parser_t::NoLine:
223 case Parser_t::EmptyLine:
241 m_output->println(
"define process");
242 m_output->println(
"status: %d", rs.
getStatus());
244 m_output->println(
"id: %d", p->
m_id);
245 if(p->m_processType == TEMPORARY){
246 m_temporaryProcesses.push_back(p->
m_id);
249 m_output->println(
"errormessage: %s", rs.
getErrMsg());
251 m_output->println(
"%s",
"");
264 m_output->println(
"undefine process");
265 m_output->println(
"id: %d",
id);
266 m_output->println(
"status: %d", rs.
getStatus());
268 m_output->println(
"errormessage: %s", rs.
getErrMsg());
270 m_output->println(
"%s",
"");
283 m_output->println(
"start process");
284 m_output->println(
"id: %d",
id);
285 m_output->println(
"status: %d", rs.
getStatus());
287 m_output->println(
"errormessage: %s", rs.
getErrMsg());
288 m_output->println(
"%s",
"");
301 m_output->println(
"stop process");
302 m_output->println(
"id: %d",
id);
303 m_output->println(
"status: %d", rs.
getStatus());
305 m_output->println(
"errormessage: %s", rs.
getErrMsg());
307 m_output->println(
"%s",
"");
311 propToString(
Properties *prop,
const char *key) {
313 const char *retval = NULL;
316 prop->getTypeOf(key, &pt);
318 case PropertiesType_Uint32:
320 prop->get(key, &val);
324 case PropertiesType_char:
326 prop->get(key, &str);
337 CPCDAPISession::printProperty(
Properties *prop,
const char *key) {
338 m_output->println(
"%s: %s", key, propToString(prop, key));
347 m_output->println(
"start processes");
348 m_output->println(
"%s",
"");
351 for(
size_t i = 0;
i < proclist->size();
i++) {
354 m_output->println(
"process");
356 m_output->println(
"id: %d", p->
m_id);
361 m_output->println(
"cwd: %s", p->
m_cwd.
c_str());
362 m_output->println(
"env: %s", p->
m_env.
c_str());
373 m_output->println(
"status: stopped");
376 m_output->println(
"status: starting");
379 m_output->println(
"status: running");
382 m_output->println(
"status: stopping");
386 m_output->println(
"%s",
"");
390 m_output->println(
"end processes");
391 m_output->println(
"%s",
"");
401 m_output->println(
"show version");
402 m_output->println(
"compile time: %s %s", __DATE__, __TIME__);
404 m_output->println(
"%s",
"");