3DS Server Control Script (3dsctl.sh)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
#
# 3DS Server control script.
 
usage()
{
cat <<EOF
 
Usage: $0 <options> <command>
 
This script allows to control 3DS Server instances (i.e. start/stop them).
 
OPTIONS:
 
   -n      The node ID (defaults to 1)
           The node ID is used to select:
            - the server configuration in the configuration home directory (e.g. node-1.properties)
            - the extract directory (e.g. node-1)
            - the startup log file to write in the out directory (e.g. catalina.out)
            - the PID file to write in the pid directory (e.g. node.pid)
 
   -c      The configuration home directory
 
   -j      The Java options to set (optional). See examples below...
 
   -f      Keep the started java process in the foreground (optional).
 
   -h      Show this message
 
COMMANDS:
 
   start          Starts an 3DS Server node
   stop           Stops an 3DS Server node
   configtest     Validates the 3DS Server configuration without actually reloading it
   configreload   Reloads the 3DS Server configuration (only if valid)
   licensetest    Validates the 3DS Server license without actually reloading it
   licensereload  Reloads the 3DS Server license (only if valid)
 
 
Java option examples:
 
   Set the maximum heap space for the 3DS Server to 1024 MB and the maximum PermGen space to 256 MB:
     -Xmx1024m -XX:MaxPermSize=256m
 
   Enable JSSE (Java Secure Socket Extension) debug output
   (Check http://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#Debug for details):
     -Djavax.net.debug=all
 
EOF
}
 
# Set defaults for command line arguments
THREEDS_HOME=$(cd $(dirname "$0")/..; pwd)
THREEDS_CONFIG_HOME="$THREEDS_HOME/conf/"
NODE_ID="1"
JAVA_OPTS=""
DAEMON_MODE="true"
NODE_PROPERTIES="$THREEDS_CONFIG_HOME/node"
APPLICATION_PROPERTIES="$THREEDS_CONFIG_HOME/application.properties"
NODE_HOST="127.0.0.1"
NODE_PORT=""
CONFIGURATION_RELOAD_PATH="reload/config"
LICENSE_RELOAD_PATH="reload/license"
DRY_RUN_PARAM="?dryRun=true"
 
# Read command line arguments
while getopts "fn:c:j:h" OPTION
do
  case $OPTION in
    c)
      THREEDS_CONFIG_HOME=$OPTARG
      ;;
    j)
      JAVA_OPTS=$OPTARG
      ;;
    f)
      DAEMON_MODE="false"
      ;;
    n)
      NODE_ID=$OPTARG
      ;;
    h)
      usage
      exit
      ;;
    \?)
      echo "Unknown option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Missing option argument for -$OPTARG" >&2
      exit 1
      ;;
    *)
      echo "Unimplemented option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done
shift $(($OPTIND-1))
 
# Set variables
# if THREEDS_CONFIG_HOME is a directory, add / here
if [[ -d $THREEDS_CONFIG_HOME ]]; then
  config_length=${#THREEDS_CONFIG_HOME}
  last_char=${THREEDS_CONFIG_HOME:config_length-1:1}
  [ $last_char != "/" ] && THREEDS_CONFIG_HOME="$THREEDS_CONFIG_HOME/";
else
  echo "$THREEDS_CONFIG_HOME is not a directory";
  exit 1;
fi
NODE_DIR="node-$NODE_ID"
THREEDS_JAR="$THREEDS_HOME/lib/nca-3ds-server.jar"
PID_FILE="$THREEDS_HOME/$NODE_DIR/pid/node.pid"
CATALINA_OUT="$THREEDS_HOME/$NODE_DIR/out/catalina.out"
CURRENT_DATE=`date +"%Y-%m-%d"`
 
# Check files and directories
assert_file_exists_and_is_readable()
{
  if [[ ! -f $1 || ! -r $1 ]]; then
    echo "File $1 doesn't exist or is not readable" >&2
    exit 1
  fi
}
assert_directory_exists_and_is_writable()
{
  if [[ ! -d $1 || ! -w $1 ]]; then
    echo "Directory $1 doesn't exist or is not writable" >&2
    exit 1
  fi
}
create_directory_if_not_exists()
{
  if [[ ! -d $1 ]]; then
    mkdir $1
  fi
}
 
# Read node port and hostname
function read_node_port() {
    NODE_PROPERTIES="$NODE_PROPERTIES-$NODE_ID.properties"
    if [ -f $NODE_PROPERTIES ]
        then
            NODE_PORT=$(grep "^server.port" $NODE_PROPERTIES|cut -d'=' -f2)
    fi
    if [[ $NODE_PORT = "" ]]
        then
            NODE_PORT=$(grep "^server.port" $APPLICATION_PROPERTIES|cut -d'=' -f2)
    fi
    if [[ $NODE_PORT = "" ]]
        then
            echo "could not find server port for node $NODE_ID"
            exit 1
    fi
}
 
assert_file_exists_and_is_readable $THREEDS_JAR
assert_directory_exists_and_is_writable "$THREEDS_HOME"
create_directory_if_not_exists "$THREEDS_HOME/$NODE_DIR/"
create_directory_if_not_exists "$THREEDS_HOME/$NODE_DIR/pid/"
create_directory_if_not_exists "$THREEDS_HOME/$NODE_DIR/out/"
 
# Execute command
COMMAND=$1
case $COMMAND in
  start)
    if [[ -e $PID_FILE ]]; then
      PID=$(<$PID_FILE)
      CURRENT_PID=`ps aux | awk '{print $2}'  | grep "^$PID$"`
        if [[ "$CURRENT_PID" -eq "$PID" ]]; then
        echo "3DS Server node seems to be already started (there is a active PID for this node)"
        exit 0
      else
        rm $PID_FILE
      fi
    fi
    echo "Starting 3DS Server on node $NODE_ID"
    cd $THREEDS_HOME/$NODE_DIR/
 
    if [[ $DAEMON_MODE = "true" ]]; then
      java $JAVA_OPTS -jar $THREEDS_JAR --spring.config.name=application,node-$NODE_ID --spring.config.additional-location=$THREEDS_CONFIG_HOME > $CATALINA_OUT 2>&1 &
      echo $! > $PID_FILE
    else
      java $JAVA_OPTS -jar $THREEDS_JAR --spring.config.name=application,node-$NODE_ID --spring.config.additional-location=$THREEDS_CONFIG_HOME
    fi
    ;;
 
  stop)
    if [[ ! -e $PID_FILE ]]; then
      echo "3DS Server node seems not to be started (there is no PID file for this node)"
      exit 0
    fi
    PID=$(<$PID_FILE)
    echo "Stopping 3DS Server on node $NODE_ID"
    kill $PID
    rm $PID_FILE
    echo "3DS Server node is stopped"
    ;;
 
  configtest)
    read_node_port
    curl -X POST https://$NODE_HOST:$NODE_PORT/$CONFIGURATION_RELOAD_PATH$DRY_RUN_PARAM --header "Accept:application/json" -k
    ;;
 
  configreload)
    read_node_port
    curl -X POST https://$NODE_HOST:$NODE_PORT/$CONFIGURATION_RELOAD_PATH --header "Accept:application/json" -k
    ;;
 
  licensetest)
    read_node_port
    curl -X POST https://$NODE_HOST:$NODE_PORT/$LICENSE_RELOAD_PATH$DRY_RUN_PARAM --header "Accept:application/json" -k
    ;;
 
  licensereload)
    read_node_port
    curl -X POST https://$NODE_HOST:$NODE_PORT/$LICENSE_RELOAD_PATH --header "Accept:application/json" -k
    ;;
 
  *)
    usage
    exit 1
    ;;
esac