Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | CONTAINER_NAME=pnf-simulator |
| 6 | CONFIG_FILE_PATH=/config/body.json |
Pawel Kadlubanski | 8ad0213 | 2018-05-24 12:30:11 +0200 | [diff] [blame] | 7 | SIMULATOR_DOCKER_HUB=nexus3.onap.org:10003/onap |
Pawel Kadlubanski | 2120334 | 2018-06-07 15:43:32 +0200 | [diff] [blame^] | 8 | SIMULATOR_TAG=1.1.0-SNAPSHOT-latest |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 9 | |
| 10 | function main(){ |
| 11 | |
| 12 | COMMAND=${1:-"help"} |
| 13 | |
| 14 | case $COMMAND in |
| 15 | "build") |
| 16 | build_image;; |
| 17 | "start") |
| 18 | start_simulator $2 $CONFIG_FILE_PATH $SIMULATOR_DOCKER_HUB/pnf-simulator:$SIMULATOR_TAG;; |
| 19 | "start-dev") |
| 20 | start_simulator $2 $CONFIG_FILE_PATH pnf-simulator:$SIMULATOR_TAG;; |
| 21 | "stop") |
| 22 | stop_simulator;; |
| 23 | "status") |
| 24 | print_status;; |
| 25 | "logs") |
| 26 | get_logs;; |
| 27 | "help") |
| 28 | print_help;; |
| 29 | *) |
| 30 | print_help;; |
| 31 | esac |
| 32 | } |
| 33 | |
| 34 | function build_image(){ |
| 35 | if [ -f pom.xml ]; then |
| 36 | mvn clean package |
| 37 | else |
| 38 | echo "pom.xml file not found" |
| 39 | exit 1 |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | function start_simulator(){ |
| 44 | |
| 45 | stop_and_remove_container || true |
| 46 | |
| 47 | if [ $(docker run -d --name $CONTAINER_NAME -v $(pwd):/config -e VES_ADDRESS=$1 -e CONFIG_FILE_PATH=$2 $3) > /dev/null ]; then |
| 48 | echo "Simulator started" |
| 49 | else |
| 50 | echo "Failed to start simulator" |
| 51 | fi |
| 52 | } |
| 53 | |
| 54 | function stop_and_remove_container(){ |
| 55 | docker rm -f $CONTAINER_NAME 1> /dev/null |
| 56 | } |
| 57 | |
| 58 | function stop_simulator(){ |
| 59 | if [ $(docker kill $CONTAINER_NAME) > /dev/null ]; then |
| 60 | echo "Simulator stopped" |
| 61 | else |
| 62 | echo "Failed to stop simulator" |
| 63 | fi |
| 64 | |
| 65 | } |
| 66 | |
| 67 | function print_status(){ |
| 68 | cat << EndOfMessage |
| 69 | |
| 70 | Simulator container status: |
| 71 | |
| 72 | $(docker ps -a -f name=$CONTAINER_NAME) |
| 73 | |
| 74 | EndOfMessage |
| 75 | } |
| 76 | |
| 77 | function print_help(){ |
| 78 | cat << EndOfMessage |
| 79 | |
| 80 | Available options: |
| 81 | build - locally builds simulator image from existing code |
| 82 | start <ves-url> - starts simulator using remote docker image and connects to given VES server |
| 83 | start-dev <ves-url> - starts simulator using local docker image and connects to given VES server |
| 84 | stop - stops simulator |
| 85 | status - prints container status |
| 86 | logs - prints logs |
| 87 | help - prints this message |
| 88 | |
| 89 | Starting simulation: |
| 90 | Use "./simulator.sh start". It will download required docker image from the internet and start simulator using body.json file |
| 91 | |
| 92 | To stop simulation use "./simulator.sh stop" command. To check simulator's status use "./simulator.sh status". |
| 93 | If you want to change message parameters simply edit body.json file then run simulator again. |
| 94 | |
| 95 | FOR DEVELOPERS |
| 96 | 1. Build local simulator image using "./simulator.sh build" |
| 97 | 2. Run simulation with "./simulator.sh start-dev" |
| 98 | |
| 99 | If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start-dev" again |
| 100 | |
| 101 | EndOfMessage |
| 102 | } |
| 103 | |
| 104 | function get_logs(){ |
| 105 | docker logs --tail all $CONTAINER_NAME |
| 106 | } |
| 107 | |
| 108 | main $@ |