Skip to content

Commit e07fd09

Browse files
committed
modelbox-tool: move shell API command from modelbox to modelbox-tool
1 parent b61dd83 commit e07fd09

File tree

10 files changed

+198
-175
lines changed

10 files changed

+198
-175
lines changed

src/modelbox/common/include/modelbox/common/utils.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <sstream>
2626
#include <string>
2727

28+
#include "modelbox/base/status.h"
29+
2830
namespace modelbox {
2931

3032
constexpr const char *MODELBOX_ROOT_VAR = "${MODELBOX_ROOT}";
@@ -37,9 +39,9 @@ const std::string &modelbox_root_dir();
3739

3840
/**
3941
* @brief Get modelbox full path
40-
*
41-
* @param path
42-
* @return std::string
42+
*
43+
* @param path
44+
* @return std::string
4345
*/
4446
std::string modelbox_full_path(const std::string &path);
4547

@@ -69,6 +71,15 @@ int modelbox_sig_register(const int sig_list[], int sig_num,
6971
*/
7072
int modelbox_cpu_register_data(char *buf, int buf_size, ucontext_t *ucontext);
7173

74+
/**
75+
* @brief Split ip address and port from string
76+
* @param host host string
77+
* @param ip output ip address
78+
* @param port output port
79+
* @return result.
80+
*/
81+
Status SplitIPPort(const std::string &host, std::string &ip, std::string &port);
82+
7283
/**
7384
* @brief Custom stream
7485
*/

src/modelbox/common/utils.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,39 @@ int modelbox_cpu_register_data(char *buf, int buf_size, ucontext_t *ucontext) {
244244
return 0;
245245
}
246246

247+
Status SplitIPPort(const std::string &host, std::string &ip,
248+
std::string &port) {
249+
auto pos = host.find_last_of(':');
250+
251+
if (pos == std::string::npos) {
252+
const auto *msg = "invalid ip address, please try ip:port";
253+
return {STATUS_INVALID, msg};
254+
}
255+
256+
port = host.substr(pos + 1, host.length());
257+
int n_port = atol(port.c_str());
258+
if (n_port <= 0 || n_port > 65535) {
259+
const auto *msg = "invalid port";
260+
return {STATUS_INVALID, msg};
261+
}
262+
263+
ip = host.substr(0, pos);
264+
/* process ipv6 format */
265+
pos = ip.find_first_of('[');
266+
if (pos != std::string::npos) {
267+
ip = ip.substr(pos + 1, ip.length());
268+
}
269+
270+
pos = ip.find_first_of(']');
271+
if (pos != std::string::npos) {
272+
ip = ip.substr(0, pos);
273+
}
274+
275+
if (ip == "") {
276+
ip = "0.0.0.0";
277+
};
278+
279+
return STATUS_OK;
280+
}
281+
247282
} // namespace modelbox

src/modelbox/manager/etc/init.d/modelbox-manager.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ case $1 in
144144
restart)
145145
"$0" stop && sleep 1 && "$0" start
146146
;;
147+
reload)
148+
if [ ! -f "$PIDFILE" ]; then
149+
echo "modelbox-manager service is stopped."
150+
exit 0
151+
fi
152+
PID="$(cat $PIDFILE 2>/dev/null)"
153+
if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
154+
echo "modelbox-manager service is stopped"
155+
exit 0
156+
fi
157+
158+
kill -HUP "$PID"
159+
if [ $? -ne 0 ]; then
160+
echo "reload modelbox-manager service failed."
161+
exit 1;
162+
fi
163+
echo "reload modelbox-manager service success."
164+
;;
147165
status)
148166
PID="$(cat "$PIDFILE" 2>/dev/null)"
149167
if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then

src/modelbox/manager/etc/manager.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ logfile @MODELBOX_ROOT_VAR@/var/log/modelbox/manager.log
88
# watchdog-timeout 90
99

1010
#
11-
# app -name "appname" -pidfile "@MODELBOX_ROOT_VAR@/run/app.pid" -check-alive -check-alive-time [90] -heartbeat-interval [5] -- run command list here
11+
# app -name "appname" -pidfile "@MODELBOX_ROOT_VAR@/run/app.pid" -check-alive -check-alive-time [90] -heartbeat-interval [5] \
12+
# --kill-cmd "/path/to/script/to/kill" \
13+
# -- run command list here
1214
#
1315
app -name "modelbox" -check-alive -pidfile "@MODELBOX_ROOT_VAR@/@CMAKE_INSTALL_RUNSTATEDIR@/modelbox/modelbox.pid" -- @MODELBOX_ROOT_VAR@/etc/init.d/modelbox start
1416

src/modelbox/server/bin/develop

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ MODELBOX_USER_HOME="$HOME/modelbox-service"
2121
MODELBOX_SYS_FLOW_DIR="${MODELBOX_ROOT}/usr/local/etc/modelbox/graph/"
2222
MODELBOX_DEFAULT_PORT="1104"
2323
MODELBOX_BIN="${MODELBOX_ROOT}/usr/local/bin/modelbox"
24+
MODELBOX_TOOL_BIN="${MODELBOX_ROOT}/usr/local/bin/modelbox-tool"
2425

2526
setup_env() {
2627
MODELBOX_FLOW_DIR="$MODELBOX_USER_HOME/graph"
@@ -61,7 +62,7 @@ debug_mode_info() {
6162
checkportbind() {
6263
IP=$1
6364
PORT=$2
64-
${MODELBOX_ROOT}/usr/local/bin/modelbox --check-port $IP:$PORT
65+
${MODELBOX_TOOL_BIN} server --check-port $IP:$PORT
6566
return $?
6667
}
6768

@@ -248,8 +249,8 @@ develop_status() {
248249
return 1
249250
fi
250251

251-
MODELBOX_USER_SERVER_IP="`${MODELBOX_BIN} -c $MODELBOX_USER_CONF_FILE --get-conf-value server.ip`"
252-
MODELBOX_USER_SERVER_PORT="`${MODELBOX_BIN} -c $MODELBOX_USER_CONF_FILE --get-conf-value server.port`"
252+
MODELBOX_USER_SERVER_IP="`${MODELBOX_TOOL_BIN} server -conf $MODELBOX_USER_CONF_FILE --get-conf-value server.ip`"
253+
MODELBOX_USER_SERVER_PORT="`${MODELBOX_TOOL_BIN} server -conf $MODELBOX_USER_CONF_FILE --get-conf-value server.port`"
253254

254255
debug_mode_info
255256
MSG=`$MODELBOX_RUN_SCRIPT status 2>/dev/null`

src/modelbox/server/include/modelbox/server/utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class IPACL {
6464
std::vector<std::pair<uint32_t, uint32_t>> ipv4_acl_;
6565
};
6666

67-
Status SplitIPPort(const std::string &host, std::string &ip, std::string &port);
68-
6967
} // namespace modelbox
7068

7169
#endif // MODELBOX_SERVER_UTILS_H_

src/modelbox/server/main.cc

Lines changed: 23 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,6 @@ static int g_sig_num = sizeof(g_sig_list) / sizeof(g_sig_list[0]);
5151
static bool kVerbose = false;
5252
static bool kForground = false;
5353

54-
enum MODELBOX_SERVER_ARG {
55-
MODELBOX_SERVER_ARG_CHECKPORT,
56-
MODELBOX_SERVER_ARG_GETCONF,
57-
MODELBOX_SERVER_ARG_GET_MODELBOX_ROOT,
58-
};
59-
60-
static int option_flag = 0;
61-
static struct option options[] = {
62-
/* internal command for develop mode */
63-
{"check-port", 1, &option_flag, MODELBOX_SERVER_ARG_CHECKPORT},
64-
{"get-conf-value", 1, &option_flag, MODELBOX_SERVER_ARG_GETCONF},
65-
{"get-modelbox-root", 0, &option_flag,
66-
MODELBOX_SERVER_ARG_GET_MODELBOX_ROOT},
67-
{nullptr, 0, nullptr, 0},
68-
};
69-
7054
static void showhelp() {
7155
/* clang-format off */
7256
char help[] = ""
@@ -253,75 +237,6 @@ int modelbox_run(const std::shared_ptr<modelbox::Server> &server) {
253237
return 0;
254238
}
255239

256-
int GetConfig(const std::string &key) {
257-
if (modelbox::LoadConfig(modelbox::kConfigPath) == false) {
258-
fprintf(stderr, "can not load configuration : %s \n",
259-
modelbox::kConfigPath.c_str());
260-
return 1;
261-
}
262-
263-
auto values = modelbox::kConfig->GetStrings(key);
264-
if (values.size() <= 0) {
265-
fprintf(stderr, "Not found key %s\n", key.c_str());
266-
return 1;
267-
}
268-
269-
for (const auto &value : values) {
270-
std::cout << value << std::endl;
271-
}
272-
273-
return 0;
274-
}
275-
276-
int CheckPort(const std::string &host) {
277-
struct addrinfo hints;
278-
struct addrinfo *result = nullptr;
279-
280-
memset_s(&hints, sizeof(hints), 0, sizeof(hints));
281-
hints.ai_family = AF_UNSPEC;
282-
283-
std::string ip;
284-
std::string port;
285-
286-
auto ret_val = modelbox::SplitIPPort(host, ip, port);
287-
if (!ret_val) {
288-
std::cerr << ret_val.Errormsg() << std::endl;
289-
return 1;
290-
}
291-
292-
auto ret = getaddrinfo(ip.c_str(), port.c_str(), &hints, &result);
293-
if (ret != 0) {
294-
std::cerr << "check port failed, " << gai_strerror(ret) << std::endl;
295-
return 1;
296-
}
297-
298-
Defer { freeaddrinfo(result); };
299-
300-
int sock = socket(result->ai_family, SOCK_STREAM, 0);
301-
if (sock < 0) {
302-
std::cerr << "create socket failed\n";
303-
return 1;
304-
}
305-
Defer { close(sock); };
306-
307-
if (bind(sock, result->ai_addr, result->ai_addrlen) != 0) {
308-
if (errno == EADDRINUSE) {
309-
/* in use */
310-
return 2;
311-
}
312-
313-
if (errno == EACCES) {
314-
/* no permission */
315-
return 3;
316-
}
317-
318-
std::cerr << "check failed, errno is " << errno << "\n";
319-
return 1;
320-
}
321-
322-
return 0;
323-
}
324-
325240
static void onexit() {}
326241

327242
#ifdef BUILD_TEST
@@ -332,56 +247,34 @@ int main(int argc, char *argv[])
332247
{
333248
std::string pidfile = MODELBOX_SERVER_PID_FILE;
334249
int cmdtype = 0;
335-
std::string get_conf_key;
336250

337-
MODELBOX_COMMAND_GETOPT_SHORT_BEGIN(cmdtype, "hc:Vvfp:n:k:K", options)
251+
MODELBOX_COMMAND_GETOPT_SHORT_BEGIN(cmdtype, "hc:Vvfp:n:k:K", nullptr)
338252
switch (cmdtype) {
339-
case 0: {
340-
switch (option_flag) {
341-
case MODELBOX_SERVER_ARG_CHECKPORT:
342-
return CheckPort(optarg);
343-
case MODELBOX_SERVER_ARG_GETCONF:
344-
get_conf_key = optarg;
345-
break;
346-
case MODELBOX_SERVER_ARG_GET_MODELBOX_ROOT:
347-
printf("%s\n", modelbox::modelbox_root_dir().c_str());
348-
return 0;
349-
break;
350-
default:
351-
printf("Try %s -h for more information.\n", argv[0]);
352-
return 1;
353-
break;
354-
}
355-
case 'p':
356-
pidfile = modelbox::modelbox_full_path(optarg);
357-
break;
358-
case 'V':
359-
kVerbose = true;
360-
break;
361-
case 'f':
362-
kForground = true;
363-
break;
364-
case 'h':
365-
showhelp();
366-
return 1;
367-
case 'c':
368-
modelbox::kConfigPath = modelbox::modelbox_full_path(optarg);
369-
break;
370-
case 'v':
371-
printf("modelbox-server %s\n", modelbox::GetModelBoxVersion());
372-
return 0;
373-
default:
374-
printf("Try %s -h for more information.\n", argv[0]);
375-
return 1;
376-
break;
377-
}
253+
case 'p':
254+
pidfile = modelbox::modelbox_full_path(optarg);
255+
break;
256+
case 'V':
257+
kVerbose = true;
258+
break;
259+
case 'f':
260+
kForground = true;
261+
break;
262+
case 'h':
263+
showhelp();
264+
return 1;
265+
case 'c':
266+
modelbox::kConfigPath = modelbox::modelbox_full_path(optarg);
267+
break;
268+
case 'v':
269+
printf("modelbox-server %s\n", modelbox::GetModelBoxVersion());
270+
return 0;
271+
default:
272+
printf("Try %s -h for more information.\n", argv[0]);
273+
return 1;
274+
break;
378275
}
379276
MODELBOX_COMMAND_GETOPT_END()
380277

381-
if (get_conf_key.length()) {
382-
return GetConfig(get_conf_key);
383-
}
384-
385278
if (modelbox::LoadConfig(modelbox::kConfigPath) == false) {
386279
fprintf(stderr, "can not load configuration : %s \n",
387280
modelbox::kConfigPath.c_str());

src/modelbox/server/utils.cc

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,39 +105,4 @@ std::shared_ptr<struct addrinfo> IPACL::GetAddrInfo(const std::string &host) {
105105
return addrinfo;
106106
}
107107

108-
Status SplitIPPort(const std::string &host, std::string &ip,
109-
std::string &port) {
110-
auto pos = host.find_last_of(':');
111-
112-
if (pos == std::string::npos) {
113-
const auto *msg = "invalid ip address, please try ip:port";
114-
return {STATUS_INVALID, msg};
115-
}
116-
117-
port = host.substr(pos + 1, host.length());
118-
int n_port = atol(port.c_str());
119-
if (n_port <= 0 || n_port > 65535) {
120-
const auto *msg = "invalid port";
121-
return {STATUS_INVALID, msg};
122-
}
123-
124-
ip = host.substr(0, pos);
125-
/* process ipv6 format */
126-
pos = ip.find_first_of('[');
127-
if (pos != std::string::npos) {
128-
ip = ip.substr(pos + 1, ip.length());
129-
}
130-
131-
pos = ip.find_first_of(']');
132-
if (pos != std::string::npos) {
133-
ip = ip.substr(0, pos);
134-
}
135-
136-
if (ip == "") {
137-
ip = "0.0.0.0";
138-
};
139-
140-
return STATUS_OK;
141-
}
142-
143108
} // namespace modelbox

src/modelbox/tool/main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum MODELBOX_TOOL_COMMAND {
5454
MODELBOX_TOOL_COMMAND_VERBOSE,
5555
MODELBOX_TOOL_COMMAND_LOG_LEVEL,
5656
MODELBOX_TOOL_COMMAND_LOG_PATH,
57+
MODELBOX_TOOL_COMMAND_GET_MODELBOX_ROOT,
5758
MODELBOX_TOOL_COMMAND_HELP,
5859
MODELBOX_TOOL_SHOW_VERSION,
5960
};
@@ -62,6 +63,7 @@ static struct option options[] = {
6263
{"verbose", 0, nullptr, MODELBOX_TOOL_COMMAND_VERBOSE},
6364
{"log-level", 1, nullptr, MODELBOX_TOOL_COMMAND_LOG_LEVEL},
6465
{"log-path", 1, nullptr, MODELBOX_TOOL_COMMAND_LOG_PATH},
66+
{"get-modelbox-root", 1, nullptr, MODELBOX_TOOL_COMMAND_GET_MODELBOX_ROOT},
6567
{"h", 0, nullptr, MODELBOX_TOOL_COMMAND_HELP},
6668
{"v", 0, nullptr, MODELBOX_TOOL_SHOW_VERSION},
6769
{nullptr, 0, nullptr, 0},
@@ -217,6 +219,10 @@ int main(int argc, char *argv[])
217219
case MODELBOX_TOOL_COMMAND_LOG_PATH:
218220
kLogFile = modelbox::modelbox_full_path(optarg);
219221
break;
222+
case MODELBOX_TOOL_COMMAND_GET_MODELBOX_ROOT:
223+
printf("%s\n", modelbox::modelbox_root_dir().c_str());
224+
return 0;
225+
break;
220226
case MODELBOX_TOOL_COMMAND_HELP:
221227
showhelp();
222228
return 0;

0 commit comments

Comments
 (0)