changeset 3:d1b78dcca129

Initial script to boot thermostat.next() Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024124.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 13 Jul 2017 20:10:46 +0200
parents 7cd7c2bb65d0
children 8c2659d0cadf
files thermostat-ng.sh
diffstat 1 files changed, 237 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermostat-ng.sh	Thu Jul 13 20:10:46 2017 +0200
@@ -0,0 +1,237 @@
+#!/bin/bash
+#
+# Runs Thermostat.next() all-in-one locally
+#
+# Examples:
+# 
+# Don't build agent/gateway from source, just run it:
+#  $ BUILD=false ./thermostat-ng
+#
+# Build agent/gateway from source and run it:
+#  $ ./thermostat-ng
+#
+#set -xv
+#set -e
+
+script_directory() {
+  # Compute the parent directory. I.e. the (symlink-resolved) location of the
+  # currently executing code's directory. See
+  # http://stackoverflow.com/a/246128/3561275 for implementation details.
+  SOURCE="${BASH_SOURCE[0]}"
+  while [ -h "$SOURCE" ]; do
+    DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
+    SOURCE="$(readlink "$SOURCE")"
+    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
+  done
+  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
+  echo "$DIR"
+}
+
+check_sources_sane() {
+  local error=0
+  local base_hg_path="$(hg paths default)"
+  for dir in $WEB_GATEWAY_ROOT $AGENT_ROOT; do
+    if [ ! -e "${dir}" ]; then
+      sub_repo="$(basename ${dir})"
+      echo 2>&1 "${dir} does not exist. Try to clone it via: hg clone ${base_hg_path}/${sub_repo}"
+      error=$(( $error + 1 ))
+    fi
+  done
+  if [ $error -gt 0 ]; then
+    echo 2>&1 "Required repositories do not exist. Exiting."
+    exit 1
+  fi
+}
+
+check_success_msg() {
+  local retval=$1
+  local msg=$2
+  if [ $retval -ne 0 ]; then
+    echo 2>&1 "Error: $msg"
+    exit 1
+  fi
+}
+
+fail() {
+  local msg=$1
+  check_success_msg 1 "$msg"
+}
+
+wait_for_ready() {
+  local expected_msg="$1"
+  local log_file="$2"
+  local max_attempts=40
+  while [ $max_attempts -gt 0 ]; do
+    if grep -q "$expected_msg" "$log_file"; then
+       break
+    fi
+    sleep 1
+    max_attempts=$(( $max_attempts - 1 ))
+  done
+}
+
+# Start/Stop mongodb
+mongodb() {
+  local action="$1"  
+  case $action in
+    start)
+      ;& # fall-through
+    stop)
+      $WEB_GATEWAY_ROOT/distribution/target/image/bin/thermostat-mongodb.sh $action
+      check_success_msg $? "Failed to $action mongodb."
+      ;;
+    *)
+      fail "Unknown action '$action'"
+      ;;
+  esac
+}
+
+start_web_gateway() {
+  local error=0
+  echo "Starting web-gateway ..."
+  $WEB_GATEWAY_ROOT/distribution/target/image/bin/thermostat-web-gateway.sh > "$GW_LOG_FILE" 2>&1 &
+  error=$?
+  echo $! > "$GW_PID_FILE"
+  check_success_msg $error "Failed to start web-gateway"
+  wait_for_ready "Server:main: Started" "$GW_LOG_FILE"
+  echo "web-gateway started."
+  echo "PID file: $GW_PID_FILE"
+  echo "Log file: $GW_LOG_FILE"
+}
+
+stop_web_gateway() {
+  _stop "$GW_PID_FILE" "web-gateway does not appear to be running."
+}
+
+_stop() {
+  local pidFile="$1"
+  local errorMsg="$2"
+  if [ -f "$pidFile" ]; then
+    pid="$(cat "$pidFile")"
+    kill $pid
+  else
+    echo "$errorMsg"
+  fi
+}
+
+# Start/stop web-gateway
+web_gateway() {
+  local action="$1"  
+  case $action in
+    start)
+      start_web_gateway
+      ;;
+    stop)
+      stop_web_gateway
+      ;;
+    *)
+      fail "Unknown action '$action'"
+      ;;
+  esac
+}
+
+start_agent() {
+  echo "Starting agent ..."
+  $AGENT_ROOT/distribution/target/image/bin/thermostat -J-Dthermostat.agent.verbose=true -Tbg "$AGENT_PID_FILE" > "$AGENT_LOG_FILE" 2>&1 
+  check_success_msg $? "Failed to start agent"
+  wait_for_ready "Agent started" "$AGENT_LOG_FILE"
+  echo "agent started."
+  echo "PID file: $AGENT_PID_FILE"
+  echo "Log file: $AGENT_LOG_FILE"
+}
+
+stop_agent() {
+  _stop "$AGENT_PID_FILE" "agent does not appear to be running."
+}
+
+# Start/stop agent
+agent() {
+  local action="$1"  
+  case $action in
+    start)
+      start_agent
+      ;;
+    stop)
+      stop_agent
+      ;;
+    *)
+      fail "Unknown action '$action'"
+      ;;
+  esac
+}
+
+build() {
+  local dir="$1"
+  local error=0
+  local component=$(basename $dir)
+  if [ "${DO_BUILD}_" == "true_" ]; then
+    pushd "$dir"
+      mvn clean verify
+      error=$?
+    popd
+    check_success_msg $error "Failed to build $component"
+  fi
+}
+
+build_web_gateway() {
+  build "$WEB_GATEWAY_ROOT"
+}
+
+build_agent() {
+  build "$AGENT_ROOT"
+}
+
+# Shuts down everything in reverse order.
+# Called via trap
+stop_all() {
+  echo
+  echo "Shutting down everything..."
+  agent stop
+  web_gateway stop
+  mongodb stop
+  rm -rf "$RUN_DATA_DIR"
+  echo "Done. Bye now."
+  exit 0
+}
+
+# Idle loop so as to be able to receive signals
+idle() {
+  echo "Thermostat started. Stop with CTRL+C"
+  while true; do
+    sleep 60
+  done
+}
+
+TOP_DIR="$(script_directory)"
+WEB_GATEWAY_ROOT="${TOP_DIR}/web-gateway"
+AGENT_ROOT="${TOP_DIR}/agent"
+WEB_CLIENT_ROOT="${TOP_DIR}/web-client"
+
+DO_BUILD=${BUILD:-true}
+RUN_DATA_DIR="$TOP_DIR/data"
+LOG_DIR="$RUN_DATA_DIR/logs"
+PID_DIR="$RUN_DATA_DIR/pids"
+GW_LOG_FILE="$LOG_DIR/web-gateway.log"
+GW_PID_FILE="$PID_DIR/web-gateway.pid"
+AGENT_LOG_FILE="$LOG_DIR/agent.log"
+AGENT_PID_FILE="$PID_DIR/agent.pid"
+
+mkdir -p "$LOG_DIR" "$PID_DIR"
+
+# Be sure source tree looks OK.
+check_sources_sane
+
+# Build the agent and web-gateway
+build_agent
+build_web_gateway
+
+trap stop_all INT
+
+mongodb "start"
+web_gateway "start"
+agent "start"
+
+# Idle until interrupted with "CTRL + C"
+# The trap for INT will stop everything we've
+# started.
+idle