|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2020 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +KV_STORAGE="kv_storage" |
| 18 | +ENCLAVE_MANAGER_KME="${TCF_HOME}/enclave_manager/avalon_enclave_manager/kme/kme_enclave_manager.py" |
| 19 | +ENCLAVE_MANAGER_WPE="${TCF_HOME}/enclave_manager/avalon_enclave_manager/wpe/wpe_enclave_manager.py" |
| 20 | +LISTENER="avalon_listener" |
| 21 | +VERSION="$(cat ${TCF_HOME}/VERSION)" |
| 22 | + |
| 23 | +# Default values |
| 24 | +COMPONENTS="$ENCLAVE_MANAGER_KME $ENCLAVE_MANAGER_WPE" # #KV_STORAGE added if -s passed |
| 25 | +START_STOP_AVALON_SERVICES=0 # default if -s not passed |
| 26 | +LMDB_URL="http://localhost:9090" # -l default |
| 27 | +LISTENER_URL="http://localhost:1947" |
| 28 | +ENCLAVE_ZMQ_URL="tcp://localhost:5555" |
| 29 | + |
| 30 | +#Variables |
| 31 | +COUNT=1 |
| 32 | +PORT=1948 |
| 33 | +WORKER_ID=0 |
| 34 | +declare -A WORKERPOOL |
| 35 | + |
| 36 | +is_sync_mode() |
| 37 | +{ |
| 38 | + return grep "sync_workload_execution" ${TCF_HOME}/listener/listener_config.toml | awk -F'=' '{print $2}' |
| 39 | +} |
| 40 | + |
| 41 | +start_avalon_components() |
| 42 | +{ |
| 43 | + if [ $START_STOP_AVALON_SERVICES = 1 ] ; then |
| 44 | + echo "Starting Avalon KV Storage $VERSION ..." |
| 45 | + $KV_STORAGE --bind $LMDB_URL & |
| 46 | + echo "Avalon KV Storage started" |
| 47 | + fi |
| 48 | + |
| 49 | + if [ $START_STOP_AVALON_SERVICES = 1 ] ; then |
| 50 | + echo "Starting Avalon Listener $VERSION ..." |
| 51 | + is_sync_mode |
| 52 | + is_sync_mode_on=$? |
| 53 | + if [ "$is_sync_mode_on" -eq "1" ]; then |
| 54 | + $LISTENER --bind $LISTENER_URL --lmdb_url $LMDB_URL --zmq_url $ENCLAVE_ZMQ_URL & |
| 55 | + else |
| 56 | + $LISTENER --bind $LISTENER_URL --lmdb_url $LMDB_URL & |
| 57 | + fi |
| 58 | + echo "Avalon Listener started" |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +start_kme() |
| 63 | +{ |
| 64 | + # Incrementing PORT number and workerid for New kme |
| 65 | + ((PORT++)) |
| 66 | + ((WORKER_ID++)) |
| 67 | + WORKER="kme-worker-"$WORKER_ID |
| 68 | + KME_URL="http://localhost:"$PORT |
| 69 | + |
| 70 | + # START_STOP_AVALON_SERVICES doesn't control enclave manager. It will be |
| 71 | + # once enclave manager runs as separate container. |
| 72 | + echo "Starting Avalon KME ..." |
| 73 | + python3 $ENCLAVE_MANAGER_KME --lmdb_url $LMDB_URL --bind $KME_URL --worker_id $WORKER & |
| 74 | + echo "Avalon KME started at $KME_URL" |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +start_wpe() |
| 79 | +{ |
| 80 | + WORKER="kme-worker-"$WORKER_ID |
| 81 | + echo $WORKER |
| 82 | + # START_STOP_AVALON_SERVICES doesn't control enclave manager. It will be |
| 83 | + # once enclave manager runs as separate container. |
| 84 | + echo "Starting Avalon WPE ..." |
| 85 | + python3 $ENCLAVE_MANAGER_WPE --lmdb_url $LMDB_URL --kme_listener_url $KME_URL --worker_id $WORKER & |
| 86 | + echo "Avalon WPE started" |
| 87 | +} |
| 88 | + |
| 89 | +build_wpe() |
| 90 | +{ |
| 91 | + |
| 92 | + # Remove any previously generated mrenclave text file |
| 93 | + rm -f $TCF_HOME/wpe_mr_enclave.txt &> /dev/null |
| 94 | + |
| 95 | + # Reset the config file to original version |
| 96 | + FILE=$TCF_HOME/config/wpe_config.toml.b |
| 97 | + if test -f "$FILE"; then |
| 98 | + echo "--------Reseting the config file-----------" |
| 99 | + mv $TCF_HOME/config/wpe_config.toml{.b,} |
| 100 | + fi |
| 101 | + |
| 102 | + # Changing library name in wpe_config.toml file to support multiple Workerpool |
| 103 | + # Saving the original file with .b extension and rewriting the new library name |
| 104 | + echo "Writing to config file" |
| 105 | + cp $TCF_HOME/config/wpe_config.toml $TCF_HOME/config/wpe_config.toml.b |
| 106 | + # Check line by line and replace enclave lib file name appended with integer COUNT |
| 107 | + while read a; do |
| 108 | + echo ${a//libavalon-wpe-enclave.signed.so/libavalon-wpe-enclave-$COUNT.signed.so} |
| 109 | + done < $TCF_HOME/config/wpe_config.toml > $TCF_HOME/config/wpe_config.toml.t |
| 110 | + mv $TCF_HOME/config/wpe_config.toml{.t,} |
| 111 | + |
| 112 | + # Building the code |
| 113 | + cd $TCF_HOME/tools/build |
| 114 | + export ENCLAVE_TYPE=wpe |
| 115 | + export WORKLOADS=$1 |
| 116 | + echo "Building WPE with WORKLOADS: $1 " |
| 117 | + make &> /dev/null |
| 118 | + |
| 119 | + # Renaming the enclave lib file as per the wpe_config.toml file |
| 120 | + echo "Renaming to lib file name" |
| 121 | + mv $TCF_HOME/tc/sgx/trusted_worker_manager/enclave/build/lib/libavalon-wpe-enclave{,-$COUNT}.signed.so |
| 122 | + mv $TCF_HOME/tc/sgx/trusted_worker_manager/enclave/build/lib/libavalon-wpe-enclave{,-$COUNT}.so |
| 123 | + mv $TCF_HOME/tc/sgx/trusted_worker_manager/enclave/build/lib/libavalon-wpe-enclave{,-$COUNT}.signed.so.meta |
| 124 | + |
| 125 | + # Increment the count for next workerpool |
| 126 | + ((COUNT++)) |
| 127 | +} |
| 128 | + |
| 129 | +reset_config_file() |
| 130 | +{ |
| 131 | + echo "--------Reseting the config file-----------" |
| 132 | + mv $TCF_HOME/config/wpe_config.toml{.b,} &> /dev/null |
| 133 | + sleep 10 |
| 134 | +} |
| 135 | + |
| 136 | +check_file() |
| 137 | +{ |
| 138 | + while(true) |
| 139 | + do |
| 140 | + FILE=$TCF_HOME/wpe_mr_enclave.txt |
| 141 | + if test -f "$FILE"; then |
| 142 | + echo "------------------------------------------------$FILE exists." |
| 143 | + cat $TCF_HOME/wpe_mr_enclave.txt |
| 144 | + return |
| 145 | + fi |
| 146 | + done |
| 147 | +} |
| 148 | + |
| 149 | +stop_avalon_components() |
| 150 | +{ |
| 151 | + for i in $COMPONENTS ; do |
| 152 | + pkill -f "$i" |
| 153 | + done |
| 154 | + echo "Hyperledger Avalon successfully ended." |
| 155 | + pkill -f "$ENCLAVE_MANAGER_KME" |
| 156 | + pkill -f "$ENCLAVE_MANAGER_WPE" |
| 157 | + if [ $START_STOP_AVALON_SERVICES = 1 ] ; then |
| 158 | + pkill -f "$KV_STORAGE" |
| 159 | + pkill -f "$LISTENER" |
| 160 | + fi |
| 161 | + mv $TCF_HOME/config/wpe_config.toml{.b,} &> /dev/null |
| 162 | + exit |
| 163 | +} |
| 164 | + |
| 165 | +stop_avalon_components_forcefully() |
| 166 | +{ |
| 167 | + ps -ef | grep bin/$LISTENER | grep -v grep | awk '{print $2}' | xargs -r kill -9 ; |
| 168 | + ps -ef | grep bin/$KV_STORAGE | grep -v grep | awk '{print $2}' | xargs -r kill -9; |
| 169 | + ps -ef | grep $ENCLAVE_MANAGER_KME | grep -v grep | awk '{print $2}' | xargs -r kill -9; |
| 170 | + ps -ef | grep $ENCLAVE_MANAGER_WPE | grep -v grep | awk '{print $2}' | xargs -r kill -9; |
| 171 | + |
| 172 | + allPorts=("bind zmq_url remote_storage_url") |
| 173 | + for i in $allPorts ; do |
| 174 | + # Port number of listener, zmq and kv storage is picked from listener toml file. |
| 175 | + # grep command reads the line as string from toml file which contails the url.Eg: bind = "http://localhost:1947". |
| 176 | + # awk command separates the string into 3 parts, based on ":" such as "http,//localhost,1947". |
| 177 | + # sed truncates the last char of the string. eg, " is removed from the 3rd part i.e 1947". |
| 178 | + # Hence the PORT stores the port value of the url. eg PORT=1947 |
| 179 | + |
| 180 | + PORT=$(grep $i ${TCF_HOME}/listener/listener_config.toml | awk -F':' '{print $3}' | sed 's/.$//i') |
| 181 | + |
| 182 | + #Below command kills the PID which occupied port. lsof command lists the PIDs holding the $PORT. |
| 183 | + echo $PORT | lsof -t -i | xargs -r kill -9; |
| 184 | + done |
| 185 | + mv $TCF_HOME/config/wpe_config.toml{.b,} &> /dev/null |
| 186 | + echo "Hyperledger Avalon forcefully terminated" |
| 187 | + exit |
| 188 | +} |
| 189 | + |
| 190 | +start_program() |
| 191 | +{ |
| 192 | + |
| 193 | + echo "******************Welcome to Hyperledger Avalon*****************\n" |
| 194 | + read -p 'Number of Worker Pool (KME) required: ' kme_count |
| 195 | + |
| 196 | + for (( i=1; i<=$kme_count; i++ )) |
| 197 | + do |
| 198 | + echo "Hyperledger Avalon currently supports the following service: |
| 199 | + 1. Echo-result code: er |
| 200 | + 2. Heart-disease-eval code: hd |
| 201 | + 3. Inside-out-eval code: io |
| 202 | + 4. Simple-wallet code: sw |
| 203 | + 5. All services code: all" |
| 204 | + read -p "Workload that need to be support by worker pool $i: " -a arr |
| 205 | + read -p "Number of Workerorder Processing Enclave WPE required for WorkerPool $i: " wpe_count |
| 206 | + WORKERPOOL[$i, 1]=$wpe_count |
| 207 | + for wpes in "${arr[@]}"; do |
| 208 | + case $wpes in |
| 209 | + er ) |
| 210 | + WPE="echo-result;$WPE" |
| 211 | + |
| 212 | + ;; |
| 213 | + hd) |
| 214 | + WPE="heart-disease-eval;$WPE" |
| 215 | + ;; |
| 216 | + io) |
| 217 | + WPE="inside-out-eval;$WPE" |
| 218 | + ;; |
| 219 | + sw) |
| 220 | + WPE="simple-wallet;$WPE" |
| 221 | + ;; |
| 222 | + all) |
| 223 | + WPE="echo-result;heart-disease-eval;inside-out-eval;simple-wallet" |
| 224 | + ;; |
| 225 | + esac |
| 226 | + done |
| 227 | + WORKERPOOL[$i, 2]=$WPE |
| 228 | + |
| 229 | + done |
| 230 | + echo "Received Input :" |
| 231 | + echo ${WORKERPOOL[*]} |
| 232 | + |
| 233 | + echo "Initializing the build process" |
| 234 | + cd $TCF_HOME/tools/build |
| 235 | + export ENCLAVE_TYPE=kme |
| 236 | + make clean &> /dev/null |
| 237 | + echo "Building KME" |
| 238 | + make &> /dev/null |
| 239 | + start_avalon_components |
| 240 | + |
| 241 | + for (( i=1; i<=$kme_count; i++ )) |
| 242 | + do |
| 243 | + |
| 244 | + build_wpe ${WORKERPOOL[$i, 2]} |
| 245 | + check_file |
| 246 | + sleep 20 |
| 247 | + start_kme |
| 248 | + for (( j=1; j<=${WORKERPOOL[$i, 1]}; j++ )) |
| 249 | + do |
| 250 | + echo "Waiting for 20 seconds for registration" |
| 251 | + sleep 20 |
| 252 | + echo "Starting WPE" |
| 253 | + start_wpe |
| 254 | + sleep 20 |
| 255 | + |
| 256 | + done |
| 257 | + done |
| 258 | + |
| 259 | + if [ "$YES" != "1" ] ; then |
| 260 | + while true; do |
| 261 | + echo "If you wish to exit the program, press y and enter" |
| 262 | + read -t 5 yn |
| 263 | + case $yn in |
| 264 | + y ) stop_avalon_components;; |
| 265 | + * ) echo " ";; |
| 266 | + esac |
| 267 | + done |
| 268 | + fi |
| 269 | + |
| 270 | +} |
| 271 | + |
| 272 | +while getopts "l:styhf" OPTCHAR ; do |
| 273 | + case $OPTCHAR in |
| 274 | + s ) |
| 275 | + START_STOP_AVALON_SERVICES=1 |
| 276 | + COMPONENTS="$COMPONENTS $KV_STORAGE $LISTENER" |
| 277 | + ;; |
| 278 | + l ) |
| 279 | + LMDB_URL=$OPTARG |
| 280 | + ;; |
| 281 | + y ) |
| 282 | + YES=1 |
| 283 | + ;; |
| 284 | + t ) |
| 285 | + stop_avalon_components |
| 286 | + ;; |
| 287 | + f ) |
| 288 | + stop_avalon_components_forcefully |
| 289 | + ;; |
| 290 | + \?|h ) |
| 291 | + BN=$(basename $0) |
| 292 | + echo "$BN: Start or Stop Hyperledger Avalon" 1>&2 |
| 293 | + echo "Usage: $BN [-l|-s|-t|-y|-h|-?]" 1>&2 |
| 294 | + echo "Where:" 1>&2 |
| 295 | + echo " -l LMDB server URL. Default is $LMDB_URL" 1>&2 |
| 296 | + echo " -t terminate the program gracefully" 1>&2 |
| 297 | + echo " -y do not prompt to end program" 1>&2 |
| 298 | + echo " -s also start or stop KV storage component" 1>&2 |
| 299 | + echo " -f forcefully kill avalon" 1>&2 |
| 300 | + echo " -? or -h print usage information" 1>&2 |
| 301 | + echo "Examples:" 1>&2 |
| 302 | + echo " $BN -s" 1>&2 |
| 303 | + echo " $BN -t -s" 1>&2 |
| 304 | + echo " $BN -y -l http://avalon-lmdb:9090" 1>&2 |
| 305 | + exit 2 |
| 306 | + ;; |
| 307 | + esac |
| 308 | +done |
| 309 | +shift `expr $OPTIND - 1` |
| 310 | + |
| 311 | +start_program |
0 commit comments