|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Script called from the discovery_datatool init script that runs a new |
| 4 | +# datatool in the background, storing the pid and piping the output |
| 5 | +# to a log file. |
| 6 | +# |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +# |
| 11 | +# Sanity check input variables |
| 12 | +# |
| 13 | + |
| 14 | +if [ -z "$RELEASE_DIR" ]; then |
| 15 | + echo "RELEASE_DIR must be set" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | +if [ ! -d "$RELEASE_DIR" ]; then |
| 19 | + echo "RELEASE_DIR must be a directory: $RELEASE_DIR" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | +if [ -z "$DATATOOL_DIR" ]; then |
| 23 | + echo "DATATOOL_DIR must be set" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | +if [ ! -d "$DATATOOL_DIR" ]; then |
| 27 | + echo "DATATOOL_DIR must be a directory: $DATATOOL_DIR" >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# rotate logs |
| 32 | +function log_rotate { |
| 33 | + LOG_NAME=$1 |
| 34 | + LOG_MAX=${2:- 30} |
| 35 | + LOG_NUM=$LOG_MAX |
| 36 | + while [ $LOG_NUM -ge 0 ]; do |
| 37 | + if [ $LOG_NUM -eq 0 ]; then |
| 38 | + SOURCE="$LOG_NAME" |
| 39 | + else |
| 40 | + SOURCE="$LOG_NAME.$LOG_NUM" |
| 41 | + fi |
| 42 | + if [ $LOG_NUM -eq $LOG_MAX ]; then |
| 43 | + TARGET= |
| 44 | + else |
| 45 | + TARGET="$LOG_NAME.$((LOG_NUM + 1))" |
| 46 | + fi |
| 47 | + if [ -f "$SOURCE" ]; then |
| 48 | + if [ -z "$TARGET" ]; then |
| 49 | + rm -f "$SOURCE" |
| 50 | + else |
| 51 | + mv -f "$SOURCE" "$TARGET" |
| 52 | + fi |
| 53 | + fi |
| 54 | + LOG_NUM=$((LOG_NUM - 1)) |
| 55 | + done |
| 56 | + unset LOG_NAME LOG_MAX LOG_NUM |
| 57 | +} |
| 58 | + |
| 59 | +# obtain discovery_datatool property values |
| 60 | +function get_property() { |
| 61 | + if [ $(grep -c "^ *$1 *=" datatool.properties ) -gt 0 ]; then |
| 62 | + grep "^ *$1 *=" datatool.properties | sed "s/^ *$1 *= *\([^ ]*\)/\1/" |
| 63 | + fi |
| 64 | +} |
| 65 | +# |
| 66 | +# Enter the datatool working directory and remain there for the |
| 67 | +# rest of this script. |
| 68 | +# |
| 69 | + |
| 70 | +cd "$DATATOOL_DIR" |
| 71 | + |
| 72 | +if [ ! -f datatool.properties ]; then |
| 73 | + echo "The datatool.properties file is missing from $(pwd)" >&2 |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | + |
| 77 | +# |
| 78 | +# Before we continue, make sure the datatool isn't already running |
| 79 | +# |
| 80 | + |
| 81 | +if [ -f datatool.pid ]; then |
| 82 | + CMD=$(ps "$(cat datatool.pid)" | tail -n +2 | awk '{print $5}') |
| 83 | + if [ "$CMD" = "java" ]; then |
| 84 | + # There is a process with our last recorded pid that looks like |
| 85 | + # a java process, this is likely our datatool. |
| 86 | + echo "The datatool is already running with the pid: $(cat datatool.pid)" >&2 |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + unset CMD |
| 90 | +fi |
| 91 | + |
| 92 | +# |
| 93 | +# Set some sensible defaults |
| 94 | +# |
| 95 | + |
| 96 | +# jvm.cmd JVM to use |
| 97 | +JVM_CMD="$(get_property jvm\\.cmd)" |
| 98 | +if [ -z "$JAVA_CMD" ]; then |
| 99 | + JVM_CMD="java" |
| 100 | +fi |
| 101 | + |
| 102 | +# jvm.args Arguments to pass to the jvm |
| 103 | +JVM_ARGS="$(get_property jvm\\.args)" |
| 104 | +if [ -z "$JVM_ARGS" ]; then |
| 105 | + JVM_ARGS="-showversion" |
| 106 | +fi |
| 107 | + |
| 108 | +LOGGING_ARGS="$(get_property jvm\\.logging)" |
| 109 | +if [ -z "$LOGGING_ARGS" ]; then |
| 110 | + LOGGING_PROPERTIES="$RELEASE_DIR"/logging.properties |
| 111 | + if [ -f "$DATATOOL_DIR"/logging.properties ]; then |
| 112 | + LOGGING_PROPERTIES="$DATATOOL_DIR"/logging.properties |
| 113 | + fi |
| 114 | + LOGGING_ARGS="-Djava.util.logging.config.file=$LOGGING_PROPERTIES" |
| 115 | + unset LOGGING_PROPERTIES |
| 116 | +fi |
| 117 | + |
| 118 | +# jvm.memory = Maximum amount of memory (in MB) the engine can use |
| 119 | +JVM_MEMORY="$(get_property jvm\\.memory)" |
| 120 | +if [ -n "$JVM_MEMORY" ]; then |
| 121 | + JVM_ARGS="$JVM_ARGS -Xmx${JVM_MEMORY}m" |
| 122 | +fi |
| 123 | +unset JVM_MEMORY |
| 124 | + |
| 125 | + |
| 126 | +datatool_http_port=$(get_property http_port) |
| 127 | +datatool_bind_address=$(get_property bind_address) |
| 128 | +datatool_https_port=$(get_property https_port) |
| 129 | +datatool_keystore_file=$(get_property keystore_file) |
| 130 | +datatool_keystore_pass=$(get_property keystore_pass) |
| 131 | +datatool_key_pass=$(get_property key_pass) |
| 132 | +datatool_truststore_file=$(get_property truststore_file) |
| 133 | +datatool_truststore_pass=$(get_property truststore_pass) |
| 134 | + |
| 135 | +# Build command line arguments |
| 136 | +if [ -n "$datatool_bind_address" ]; then |
| 137 | + DATATOOL_ARGS="$DATATOOL_ARGS --bind-address $datatool_bind_address" |
| 138 | +fi |
| 139 | +if [ -n "$datatool_http_port" ]; then |
| 140 | + DATATOOL_ARGS="$DATATOOL_ARGS --port $datatool_http_port" |
| 141 | +fi |
| 142 | +if [ -n "$datatool_https_port" ]; then |
| 143 | + DATATOOL_ARGS="$DATATOOL_ARGS --https-port $datatool_https_port" |
| 144 | +fi |
| 145 | +if [ -n "$datatool_keystore_file" ]; then |
| 146 | + DATATOOL_ARGS="$DATATOOL_ARGS --keystore-file $datatool_keystore_file" |
| 147 | +fi |
| 148 | +if [ -n "$datatool_keystore_pass" ]; then |
| 149 | + DATATOOL_ARGS="$DATATOOL_ARGS --keystore-pass $datatool_keystore_pass" |
| 150 | +fi |
| 151 | +if [ -n "$datatool_key_pass" ]; then |
| 152 | + DATATOOL_ARGS="$DATATOOL_ARGS --key-pass $datatool_key_pass" |
| 153 | +fi |
| 154 | +if [ -n "$datatool_truststore_file" ]; then |
| 155 | + DATATOOL_ARGS="$DATATOOL_ARGS --truststore-file $datatool_truststore_file" |
| 156 | +fi |
| 157 | +if [ -n "$datatool_truststore_pass" ]; then |
| 158 | + DATATOOL_ARGS="$DATATOOL_ARGS --truststore-pass $datatool_truststore_pass" |
| 159 | +fi |
| 160 | + |
| 161 | + |
| 162 | +mkdir -p log |
| 163 | + |
| 164 | +log_rotate log/datatool.out |
| 165 | + |
| 166 | +# |
| 167 | +# Finally run the datatool, storing the pid |
| 168 | +# |
| 169 | + |
| 170 | +rm -f datatool.pid datatool.state |
| 171 | +"$JVM_CMD" $JVM_ARGS $JAVA_OPTIONS $LOGGING_ARGS \ |
| 172 | + -jar "$RELEASE_DIR"/discovery_datatool_standalone.jar $DATATOOL_ARGS \ |
| 173 | + >& log/datatool.out & |
| 174 | +echo $! >datatool.pid |
0 commit comments