using wstool in server control scripts srvctrl

2017-03-06

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Mar 2017 15:43:28 +0100 (2017-03-06)
branch
srvctrl
changeset 175
9823770ba4ee
parent 173
63b8d52db390
child 176
f2268fcbe487

using wstool in server control scripts

adds server config lib
wstool can read server.conf now
changes wstool usage

make/install.mk file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/server/Makefile file | annotate | diff | comparison | revisions
src/tools/Makefile file | annotate | diff | comparison | revisions
src/tools/wstool.c file | annotate | diff | comparison | revisions
src/tools/wstool.h file | annotate | diff | comparison | revisions
templates/bin/reconfig.template file | annotate | diff | comparison | revisions
templates/bin/startserv.template file | annotate | diff | comparison | revisions
templates/bin/stopserv.template file | annotate | diff | comparison | revisions
--- a/make/install.mk	Sun Feb 19 11:56:39 2017 +0100
+++ b/make/install.mk	Mon Mar 06 15:43:28 2017 +0100
@@ -46,7 +46,9 @@
 	sed s:%%WS_HOST%%:$(HOST):g ../templates/config/server.template > $(INSTALL_DIR)/config/server.conf
 	@echo "copy binaries"
 	cp ../build/bin/webservd$(APP_EXT) $(INSTALL_DIR)/bin/
+	cp ../build/bin/wstool$(APP_EXT) $(INSTALL_DIR)/bin/
 	cp ../build/lib/libucx$(LIB_EXT) $(INSTALL_DIR)/lib/
+	cp ../build/lib/libwscfg$(LIB_EXT) $(INSTALL_DIR)/lib/
 	@echo "copy includes"
 	cp ../src/server/public/nsapi.h $(INSTALL_DIR)/include/nsapi.h
 	cp ../src/server/public/auth.h $(INSTALL_DIR)/include/auth.h
--- a/src/Makefile	Sun Feb 19 11:56:39 2017 +0100
+++ b/src/Makefile	Mon Mar 06 15:43:28 2017 +0100
@@ -37,7 +37,7 @@
 server: ucx .FORCE
 	cd server; $(MAKE) 
 
-tools: .FORCE
+tools: server .FORCE
 	cd tools; $(MAKE)
 
 .FORCE:
--- a/src/server/Makefile	Sun Feb 19 11:56:39 2017 +0100
+++ b/src/server/Makefile	Mon Mar 06 15:43:28 2017 +0100
@@ -33,7 +33,9 @@
 
 MAIN_TARGET = $(BUILD_ROOT)/build/bin/webservd
 
-all: preparation $(MAIN_TARGET) $(PLUGINS)
+LIB_WSCFG = $(BUILD_ROOT)/build/lib/libwscfg$(LIB_EXT)
+
+all: preparation $(MAIN_TARGET) $(LIB_WSCFG) $(PLUGINS)
 
 include util/objs.mk
 include safs/objs.mk
@@ -65,6 +67,9 @@
 $(MAIN_TARGET): $(MAINOBJS)
 	$(CXX) -o $(MAIN_TARGET) $(MAINOBJS) -L$(BUILD_ROOT)/build/lib $(LDFLAGS)
 
+$(LIB_WSCFG): $(CONFOBJS)
+	$(CC) $(SHLIB_LDFLAGS) -o $@ $(CONFOBJS)
+
 
 ../../build/server/ucx/%.o: %.c
 	$(CC) -o $@ -c $(CFLAGS) $<
--- a/src/tools/Makefile	Sun Feb 19 11:56:39 2017 +0100
+++ b/src/tools/Makefile	Mon Mar 06 15:43:28 2017 +0100
@@ -30,6 +30,9 @@
 
 include $(BUILD_ROOT)/config.mk
 
+CFLAGS += -I..
+LDFLAGS += -L../../build/lib -lucx -lwscfg
+
 # list of source files
 WSTOOL_SRC = wstool.c
 WSTOOL_SRC += srvctrlsocket.c
--- a/src/tools/wstool.c	Sun Feb 19 11:56:39 2017 +0100
+++ b/src/tools/wstool.c	Mon Mar 06 15:43:28 2017 +0100
@@ -30,26 +30,66 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include "../server/config/serverconf.h"
 
 #include "srvctrlsocket.h"
 
 static void print_info(char *cmd) {
-    fprintf(stderr, "usage: %s -s <socketpath> <command>\n\n");
+    fprintf(stderr, "usage:\n");
+    fprintf(stderr, "%s -t <srvconfigfile>\n", cmd);
+    fprintf(stderr, "%s -s <socketpath> <command>\n", cmd);
     fprintf(stderr, "Commands: reconfig, shutdown, stat, log\n");
 }
 
 int main(int argc, char **argv) {
-    if(argc < 3) {
-        print_info(argv[0]);
-        return -2;
+    if(argc > 2) {
+        if(!strcmp(argv[1], "-t")) {
+            return tool_get_tmpdir(argv[2]);
+        } else if(!strcmp(argv[1], "-s")) {
+            if(argc != 4) {
+                print_info(argv[0]);
+                return -2;
+            }
+            return tool_srvctrl(argv[2], argv[3]);
+        }
     }
     
-    SrvConnection *srv = srvctrl_connet(argv[1]);
+    print_info(argv[0]);
+    return -2;
+}
+
+int tool_get_tmpdir(char *configfile) {
+    ServerConfig *serverconf = load_server_config(configfile);
+    UcxList *list = ucx_map_sstr_get(serverconf->objects, sstrn("Runtime", 7));
+    if(!list) {
+        fprintf(stderr, "Error: No Runtime element in %s\n", configfile);
+        return -1;
+    }
+    if(ucx_list_size(list) != 1) {
+        fprintf(stderr, "Error: Multiple Runtime elements in %s\n", configfile);
+        return -1;
+    }
+    ServerConfigObject *runtime = list->data;
+    sstr_t tmp = cfg_directivelist_get_str(runtime->directives, sstr("Temp"));
+    if(!tmp.ptr) {
+        fprintf(stderr, "Error: No Temp directive in Runtime Object\n");
+        return -1;
+    }
+    
+    printf("%.*s\n", (int)tmp.length, tmp.ptr);
+    
+    return 0;
+}
+
+int tool_srvctrl(char *socketfile, char *cmd) {
+    SrvConnection *srv = srvctrl_connet(socketfile);
     if(!srv) {
         return -1;
     }
     
-    fprintf(srv->stream, "%s\n", argv[2]);
+    fprintf(srv->stream, "%s\n", cmd);
     fflush(srv->stream);
     
     SrvMsg msg;
@@ -68,3 +108,16 @@
     return 0;
 }
 
+
+
+int log_ereport(int degree, const char *format, ...) {
+    va_list args;
+    va_start(args, format);
+    int ret = log_ereport_v(degree, format, args);
+    va_end(args);
+    return ret;
+}
+
+int log_ereport_v(int degree, const char *format, va_list args) { 
+    return 0;
+}
--- a/src/tools/wstool.h	Sun Feb 19 11:56:39 2017 +0100
+++ b/src/tools/wstool.h	Mon Mar 06 15:43:28 2017 +0100
@@ -29,12 +29,18 @@
 #ifndef WSTOOL_H
 #define WSTOOL_H
 
+#include <stdarg.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+int tool_get_tmpdir(char *configfile);
 
+int tool_srvctrl(char *socketfile, char *cmd);
 
+int log_ereport(int degree, const char *format, ...);
+int log_ereport_v(int degree, const char *format, va_list args);
 
 #ifdef __cplusplus
 }
--- a/templates/bin/reconfig.template	Sun Feb 19 11:56:39 2017 +0100
+++ b/templates/bin/reconfig.template	Mon Mar 06 15:43:28 2017 +0100
@@ -1,6 +1,57 @@
 #!/bin/sh
 
-PID=`cat /tmp/webserver-rw6pgl8b/pid`
+#
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+#
+# Copyright 2017 Olaf Wintermann. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+#   1. Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above copyright
+#      notice, this list of conditions and the following disclaimer in the
+#      documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
 
-kill -USR1 $PID
+WS_INSTALL_DIR=%%WS_INSTALL_DIR%%
+
+cd $WS_INSTALL_DIR
+
+WS_TMP_DIR=`bin/wstool -t config/server.conf`
+if [ $? -ne 0 ]; then
+	exit 1
+fi
 
+WS_PID=`cat $WS_TMP_DIR/pid 2> /dev/null`
+if [ $? -ne 0 ]; then
+	echo "cannot get server pid"
+	exit 1
+fi
+if [ -z $WS_PID ]; then
+	echo "cannot get server pid"
+	exit 1
+fi
+
+kill -0 $WS_PID 2> /dev/null
+if [ $? -ne 0 ]; then
+	echo "server not running"
+	exit 1
+fi
+
+bin/wstool -s $WS_TMP_DIR/private/srvctrl.sock reconfig
+
--- a/templates/bin/startserv.template	Sun Feb 19 11:56:39 2017 +0100
+++ b/templates/bin/startserv.template	Mon Mar 06 15:43:28 2017 +0100
@@ -3,7 +3,7 @@
 #
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 #
-# Copyright 2011 Olaf Wintermann. All rights reserved.
+# Copyright 2017 Olaf Wintermann. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are met:
--- a/templates/bin/stopserv.template	Sun Feb 19 11:56:39 2017 +0100
+++ b/templates/bin/stopserv.template	Mon Mar 06 15:43:28 2017 +0100
@@ -1,6 +1,56 @@
 #!/bin/sh
 
-PID=`cat /tmp/webserver-rw6pgl8b/pid`
+#
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+#
+# Copyright 2017 Olaf Wintermann. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+#   1. Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above copyright
+#      notice, this list of conditions and the following disclaimer in the
+#      documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
 
-kill -TERM $PID
+WS_INSTALL_DIR=%%WS_INSTALL_DIR%%
+
+cd $WS_INSTALL_DIR
+
+WS_TMP_DIR=`bin/wstool -t config/server.conf`
+if [ $? -ne 0 ]; then
+	exit 1
+fi
 
+WS_PID=`cat $WS_TMP_DIR/pid 2> /dev/null`
+if [ $? -ne 0 ]; then
+	echo "cannot get server pid"
+	exit 1
+fi
+if [ -z $WS_PID ]; then
+	echo "cannot get server pid"
+	exit 1
+fi
+
+kill -0 $WS_PID 2> /dev/null
+if [ $? -ne 0 ]; then
+	echo "server not running"
+	exit 1
+fi
+
+bin/wstool -s $WS_TMP_DIR/private/srvctrl.sock shutdown

mercurial