3DS Server Admin Control Script (3dssadminuictl.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
#!/bin/bash
#
# 3DS Server Admin UI control script.
 
usage()
{
cat <<EOF
 
Usage: $0 <options> <command>
 
This script allows to control 3DS Server Admin UI instance (i.e. start/stop it).
 
OPTIONS:
 
   -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 a 3DS Server Admin UI node
   stop           Stops a 3DS Server Admin UI node
   licensetest    Validates the 3DS Server Admin UI license without actually reloading it
   licensereload  Reloads the 3DS Server Admin UI license (only if valid)
 
Java option examples:
 
   Set the maximum heap space for the 3DS Server Admin UI 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_ADMIN_UI_HOME=$(cd $(dirname "$0")/..; pwd)
THREEDS_ADMIN_UI_CONFIG_HOME="$THREEDS_ADMIN_UI_HOME/conf/"
JAVA_OPTS=""
DAEMON_MODE="true"
APPLICATION_PROPERTIES="$THREEDS_ADMIN_UI_CONFIG_HOME/application.properties"
APP_HOST="127.0.0.1"
APP_PORT=""
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_ADMIN_UI_CONFIG_HOME=$OPTARG
      ;;
    j)
      JAVA_OPTS=$OPTARG
      ;;
    f)
      DAEMON_MODE="false"
      ;;
    h)
      usage
      exit
      ;;
    n)
      echo "Ignored option -n."
      ;;
    \?)
      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
THREEDS_ADMIN_UI_JAR="$THREEDS_ADMIN_UI_HOME/lib/nca-3dss-admin-ui.jar"
PID_FILE="$THREEDS_ADMIN_UI_HOME/pid/node.pid"
CURRENT_DATE=`date +"%Y-%m-%d"`
CATALINA_OUT="$THREEDS_ADMIN_UI_HOME/out/catalina.out"
 
# 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 server port
function read_server_port() {
    APP_PORT=$(grep "^server.port" $APPLICATION_PROPERTIES|cut -d'=' -f2)
    if [[ $APP_PORT = "" ]]
        then
            APP_PORT=$(grep "^server.port" $APPLICATION_PROPERTIES|cut -d'=' -f2)
    fi
    if [[ $APP_PORT = "" ]]
        then
            echo "could not find server port for Admin UI App"
            exit 1
    fi
}
 
assert_file_exists_and_is_readable $THREEDS_ADMIN_UI_JAR
assert_directory_exists_and_is_writable "$THREEDS_ADMIN_UI_HOME"
create_directory_if_not_exists "$THREEDS_ADMIN_UI_HOME/pid/"
create_directory_if_not_exists "$THREEDS_ADMIN_UI_HOME/out/"
 
# Execute command
COMMAND=$1
case $COMMAND in
  start)
    if [[ -e $PID_FILE ]]; then
      PID=$(<$PID_FILE)
      CURRENT_PID=`ps aux | awk '{print $1}'  | grep "^$PID$"`
        if [[ "$CURRENT_PID" -eq "$PID" ]]; then
        echo "3DS Server Admin UI node seems to be already started (there is a active PID for this node)"
        exit 0
      else
        rm $PID_FILE
      fi
    fi
    cd $THREEDS_ADMIN_UI_HOME
 
    if [[ $DAEMON_MODE = "true" ]]; then
      java $JAVA_OPTS -jar $THREEDS_ADMIN_UI_JAR --spring.config.name=application --spring.config.additional-location=$THREEDS_ADMIN_UI_CONFIG_HOME > $CATALINA_OUT 2>&1 &
      echo $! > $PID_FILE
    else
      java $JAVA_OPTS -jar $THREEDS_ADMIN_UI_JAR --spring.config.name=application --spring.config.additional-location=$THREEDS_ADMIN_UI_CONFIG_HOME
    fi
    ;;
  stop)
    if [[ ! -e $PID_FILE ]]; then
      echo "3DS Server Admin UI node seems not to be started (there is no PID file for this node)"
      exit 0
    fi
    PID=$(<$PID_FILE)
    kill $PID
    rm $PID_FILE
    ;;
 
  licensetest)
    read_server_port
    curl -X POST https://$APP_HOST:$APP_PORT/$LICENSE_RELOAD_PATH$DRY_RUN_PARAM --header "Accept:application/json" -k
    ;;
 
  licensereload)
    read_server_port
    curl -X POST https://$APP_HOST:$APP_PORT/$LICENSE_RELOAD_PATH --header "Accept:application/json" -k
    ;;
 
  *)
    usage
    exit 1
    ;;
esac