Mercurial > hg > thermostat-ng
view thermostat-ng.sh @ 5:c69fd8f97d50
Copy ssl example config prior starting agent.
Reviewed-by: ebaron
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024130.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Tue, 18 Jul 2017 18:46:20 +0200 |
parents | 8c2659d0cadf |
children | 0a17affdae81 |
line wrap: on
line source
#!/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() { config_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" if [ "${DO_BUILD}_" == "true_" ]; then local build_fn=$2 local error=0 local component=$(basename $dir) pushd "$dir" $build_fn; error=$? popd check_success_msg $error "Failed to build $component" else echo "Build of $dir is skipped." fi } mvn_build() { mvn clean verify } npm_build() { # setup environment variables export NODE_ENV=development export GATEWAY_URL npm install npm run build } build_web_gateway() { build "$WEB_GATEWAY_ROOT" mvn_build } config_agent() { local agent_conf_dir="$HOME/.thermostat/etc" local agent_conf="${agent_conf_dir}/agent.auth" if [ ! -d "$agent_conf" ]; then mkdir -p "$agent_conf_dir" fi echo "username=foo-agent-user" >> $agent_conf echo "password=agent-pwd" >> $agent_conf cp $AGENT_ROOT/distribution/target/image/etc/examples/ssl.properties.example $AGENT_ROOT/distribution/target/image/etc/ssl.properties } build_agent() { build "$AGENT_ROOT" mvn_build } build_web_client() { build "$WEB_CLIENT_ROOT" npm_build } # deploy web-client resources statically, more info at http://icedtea.classpath.org/pipermail/thermostat/2017-May/023164.html copy_web_client_to_web_gateway() { local dist_dir="${WEB_CLIENT_ROOT}/dist"; if [ -d "$dist_dir" ]; then cp -r $dist_dir $WEB_GATEWAY_ROOT/distribution/target/image/web-client; echo "Web client copied to web-gateway." else fail "Web client could not be copied to web-gateway because the directory web-client/dist/ does not exist. Make sure the web-client has been built correctly." fi } # 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" echo "The web client can be accessed at: $GATEWAY_URL/web-client/" echo "Use credentials 'test-user':'test-pass' to log in." 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" GATEWAY_URL="https://127.0.0.1:30000" 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 # Build the web client and copy the built distribution image into # web-gateway's web-client folder for static serving of the web client. build_web_client copy_web_client_to_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