|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Start scrip for the Node Agent |
| 4 | +# |
| 5 | +# (C) 2014 INAETICS, <www.inaetics.org> - Apache License v2. |
| 6 | + |
| 7 | +#TODO the workdir should be removed when stopping the agent |
| 8 | + |
| 9 | +cd $(dirname $0) |
| 10 | + |
| 11 | +# |
| 12 | +# Config |
| 13 | +# |
| 14 | +PROVISIONING_NAMESPACE="/inaetics/node-provisioning-service" |
| 15 | +MAX_RETRY_ETCD_REPO=10 |
| 16 | +RETRY_ETCD_REPO_INTERVAL=5 |
| 17 | +UPDATE_INTERVAL=60 |
| 18 | +RETRY_INTERVAL=20 |
| 19 | +ETCD_TTL_INTERVALL=$((UPDATE_INTERVAL + 15)) |
| 20 | +LOG_DEBUG=true |
| 21 | + |
| 22 | +# |
| 23 | +# Libs |
| 24 | +# |
| 25 | +source etcdctl.sh |
| 26 | + |
| 27 | +# Wraps a function call to redirect or filter stdout/stderr |
| 28 | +# depending on the debug setting |
| 29 | +# args: $@ - the wrapped call |
| 30 | +# return: the wrapped call's return |
| 31 | +_call () { |
| 32 | + if [ "$LOG_DEBUG" != "true" ]; then |
| 33 | + $@ &> /dev/null |
| 34 | + return $? |
| 35 | + else |
| 36 | + $@ 2>&1 | awk '{print "[DEBUG] "$0}' >&2 |
| 37 | + return ${PIPESTATUS[0]} |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Echo a debug message to stderr, perpending each line |
| 42 | +# with a debug prefix. |
| 43 | +# args: $@ - the echo args |
| 44 | +_dbg() { |
| 45 | + if [ "$LOG_DEBUG" == "true" ]; then |
| 46 | + echo $@ | awk '{print "[DEBUG] "$0}' >&2 |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +# Echo a log message to stderr, perpending each line |
| 51 | +# with a info prefix. |
| 52 | +# args: $@ - the echo args |
| 53 | +_log() { |
| 54 | + echo $@ | awk '{print "[INFO] "$0}' >&2 |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +# |
| 59 | +# State |
| 60 | +# |
| 61 | +current_provisioning_service="" |
| 62 | +located_provisioning_service="" |
| 63 | +agent_pid="" |
| 64 | + |
| 65 | +# |
| 66 | +# Functions |
| 67 | +# |
| 68 | + |
| 69 | +# Locate the provisioning service in etcd. |
| 70 | +# args: $1 - <current service>, prefer if present |
| 71 | +# echo: <new service>, may be same as current |
| 72 | +# return: 0, if no errors |
| 73 | +# 1, if etcd lookup fails |
| 74 | +locate_provisioning_service () { |
| 75 | + located_provisioning_service="" |
| 76 | + local provisioning_services=($(etcd/values $PROVISIONING_NAMESPACE $ETCD_HOST)) |
| 77 | + if [ $? -ne 0 ]; then |
| 78 | + return 1 |
| 79 | + fi |
| 80 | + if [ "$current_provisioning_service" != "" ]; then |
| 81 | + for provisioning_service in ${provisioning_services[@]}; do |
| 82 | + if [ "$current_provisioning_service" == "$provisioning_service" ]; then |
| 83 | + located_provisioning_service=$current_provisioning_service |
| 84 | + return 0 |
| 85 | + fi |
| 86 | + done |
| 87 | + fi |
| 88 | + if [ ${#provisioning_services[@]} -gt 0 ]; then |
| 89 | + located_provisioning_service=${provisioning_services[0]} |
| 90 | + fi |
| 91 | + return 0 |
| 92 | +} |
| 93 | + |
| 94 | +start_agent () { |
| 95 | + DEPLOYMENT_ID=${agent_id} |
| 96 | + HOST_IP=${agent_ipv4} |
| 97 | + MAX_RETRY_ETCD_REPO=60 |
| 98 | + RETRY_ETCD_REPO_INTERVAL=5 |
| 99 | + DISCOVERY_PATH="org.apache.celix.discovery.etcd" |
| 100 | + |
| 101 | + local workdir="/tmp/workdir" |
| 102 | + mkdir -p ${workdir} |
| 103 | + |
| 104 | + cp /tmp/config.properties.base ${workdir}/config.properties |
| 105 | + echo "deployment_admin_identification=${agent_id}" >> ${workdir}/config.properties |
| 106 | + echo "deployment_admin_url=${current_provisioning_service}" >> ${workdir}/config.properties |
| 107 | + echo "RSA_IP=$agent_ipv4" >> ${workdir}/config.properties |
| 108 | + echo "DISCOVERY_ETCD_ROOT_PATH=inaetics/discovery" >> ${workdir}/config.properties |
| 109 | + echo "DISCOVERY_ETCD_SERVER_IP=`echo $ETCDCTL_PEERS | cut -d ':' -f 1`" >> ${workdir}/config.properties |
| 110 | + echo "DISCOVERY_ETCD_SERVER_PORT=`echo $ETCDCTL_PEERS | cut -d ':' -f 2`" >> ${workdir}/config.properties |
| 111 | + echo "DISCOVERY_CFG_SERVER_IP=$agent_ipv4" >> ${workdir}/config.properties |
| 112 | + |
| 113 | + cd ${workdir} |
| 114 | + local cmd="celix" |
| 115 | + |
| 116 | + _dbg $cmd |
| 117 | + $cmd & |
| 118 | + agent_pid=$! |
| 119 | +} |
| 120 | + |
| 121 | +function store_etcd_data(){ |
| 122 | + |
| 123 | + # check if provisioning is running |
| 124 | + if [ "$agent_pid" == "" ]; then |
| 125 | + _log "service not running, skipping store_etcd_data" |
| 126 | + return |
| 127 | + fi |
| 128 | + |
| 129 | + ETCD_PATH_FOUND=0 |
| 130 | + RETRY=1 |
| 131 | + while [ $RETRY -le $MAX_RETRY_ETCD_REPO ] && [ $ETCD_PATH_FOUND -eq 0 ] |
| 132 | + do |
| 133 | + etcd/putTtl "/inaetics/node-agent-service/$agent_id" "$agent_ipv4:$agent_port" "$ETCD_TTL_INTERVALL" |
| 134 | + |
| 135 | + if [ $? -ne 0 ]; then |
| 136 | + _log "Tentative $RETRY of storing agent to etcd failed. Retrying..." |
| 137 | + ((RETRY+=1)) |
| 138 | + sleep $RETRY_ETCD_REPO_INTERVAL |
| 139 | + else |
| 140 | + _log "Pair </inaetics/node-agent-service/$agent_id,$agent_ipv4:$agent_port> stored in etcd" |
| 141 | + ETCD_PATH_FOUND=1 |
| 142 | + fi |
| 143 | + done |
| 144 | + |
| 145 | + if [ $ETCD_PATH_FOUND -eq 0 ]; then |
| 146 | + _log "Cannot store pair </inaetics/node-agent-service/$agent_id,$agent_ipv4:$agent_port> in etcd" |
| 147 | + fi |
| 148 | + |
| 149 | +} |
| 150 | +stop_agent () { |
| 151 | + etcd/rm "/inaetics/node-agent-service/$agent_id" |
| 152 | + if [ "$agent_pid" != "" ]; then |
| 153 | + kill -SIGTERM $agent_pid |
| 154 | + wait $agent_pid |
| 155 | + agent_pid="" |
| 156 | + rm -fr /tmp/workdir |
| 157 | + fi |
| 158 | +} |
| 159 | + |
| 160 | +clean_up () { |
| 161 | + echo "Running cleanup.." |
| 162 | + stop_agent |
| 163 | + exit 0 |
| 164 | +} |
| 165 | + |
| 166 | +# |
| 167 | +# Main |
| 168 | +# |
| 169 | +trap clean_up SIGHUP SIGINT SIGTERM |
| 170 | + |
| 171 | +agent_id=$1 |
| 172 | +if [ "$agent_id" == "" ]; then |
| 173 | + # get id from env variable set by kubernetes pod config |
| 174 | + agent_id=$AGENT_NAME |
| 175 | + if [ "$agent_id" != "" ]; then |
| 176 | + # append ip |
| 177 | + agent_id=$agent_id-`hostname -i` |
| 178 | + fi |
| 179 | +fi |
| 180 | +if [ "$agent_id" == "" ]; then |
| 181 | + # get docker id |
| 182 | + agent_id=`cat /proc/self/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"` |
| 183 | +fi |
| 184 | +if [ "$agent_id" == "" ]; then |
| 185 | + echo "agent_id param required!" |
| 186 | + exit 1 |
| 187 | +fi |
| 188 | + |
| 189 | +agent_ipv4=$2 |
| 190 | +if [ "$agent_ipv4" == "" ]; then |
| 191 | + # get IP |
| 192 | + agent_ipv4=`hostname -i` |
| 193 | +fi |
| 194 | +if [ "$agent_ipv4" == "" ]; then |
| 195 | + echo "agent_ipv4 param required!" |
| 196 | + exit 1 |
| 197 | +fi |
| 198 | + |
| 199 | +# get port from env variable set by kubernetes pod config |
| 200 | +agent_port=$HOSTPORT |
| 201 | +if [ "$agent_port" == "" ]; then |
| 202 | + agent_port=8080 |
| 203 | +fi |
| 204 | + |
| 205 | +while true; do |
| 206 | + |
| 207 | + locate_provisioning_service |
| 208 | + if [ $? -ne 0 ]; then |
| 209 | + echo "Locating provisioning services in etcd failed. Keeping current state.." 1>&2 |
| 210 | + else |
| 211 | + if [ "$current_provisioning_service" != "$located_provisioning_service" ]; then |
| 212 | + echo "Provisioning service changed: $current_provisioning_service -> $located_provisioning_service" |
| 213 | + current_provisioning_service=$located_provisioning_service |
| 214 | + |
| 215 | + if [ "$current_provisioning_service" == "" ]; then |
| 216 | + if [ "$agent_pid" != "" ]; then |
| 217 | + echo "Stopping agent.." |
| 218 | + stop_agent |
| 219 | + fi |
| 220 | + else |
| 221 | + if [ "$agent_pid" != "" ]; then |
| 222 | + echo "Restarting agent..." |
| 223 | + stop_agent |
| 224 | + start_agent |
| 225 | + else |
| 226 | + echo "Starting agent..." |
| 227 | + start_agent |
| 228 | + fi |
| 229 | + fi |
| 230 | + fi |
| 231 | + fi |
| 232 | + |
| 233 | + if [ "$agent_pid" == "" ]; then |
| 234 | + echo "agent waiting for provisioning service.." |
| 235 | + echo "Will retry in $RETRY_INTERVAL seconds..." |
| 236 | + sleep $RETRY_INTERVAL & |
| 237 | + wait $! |
| 238 | + else |
| 239 | + echo "agent running with provisioning $current_provisioning_service" |
| 240 | + store_etcd_data |
| 241 | + echo "Will update in $UPDATE_INTERVAL seconds..." |
| 242 | + sleep $UPDATE_INTERVAL & |
| 243 | + wait $! |
| 244 | + fi |
| 245 | + |
| 246 | +done |
0 commit comments