Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 5 | COMPOSE_FILE_NAME=docker-compose.yml |
| 6 | DEV_COMPOSE_FILE_NAME=docker-compose.dev.yml |
| 7 | NETOPEER_CONTAINER_NAME=netopeer |
| 8 | SIMULATOR_CONTAINER_NAME=pnf-simulator |
| 9 | SIMULATOR_PORT=5000 |
| 10 | SIMULATOR_START_URL=http://localhost:$SIMULATOR_PORT/simulator/start |
| 11 | SIMULATOR_STOP_URL=http://localhost:$SIMULATOR_PORT/simulator/stop |
| 12 | SIMULATOR_STATUS_URL=http://localhost:$SIMULATOR_PORT/simulator/status |
| 13 | RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 14 | |
| 15 | function main(){ |
| 16 | |
| 17 | COMMAND=${1:-"help"} |
| 18 | |
| 19 | case $COMMAND in |
| 20 | "build") |
| 21 | build_image;; |
| 22 | "start") |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 23 | start $COMPOSE_FILE_NAME;; |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 24 | "start-dev") |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 25 | start $DEV_COMPOSE_FILE_NAME;; |
| 26 | "start-debug") |
| 27 | start_netconf_server $DEV_COMPOSE_FILE_NAME;; |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 28 | "stop") |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 29 | stop;; |
| 30 | "run-simulator") |
| 31 | run_simulator;; |
| 32 | "stop-simulator") |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 33 | stop_simulator;; |
| 34 | "status") |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 35 | get_status;; |
| 36 | "clear-logs") |
| 37 | clear_logs;; |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 38 | *) |
| 39 | print_help;; |
| 40 | esac |
| 41 | } |
| 42 | |
| 43 | function build_image(){ |
| 44 | if [ -f pom.xml ]; then |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 45 | mvn clean package |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 46 | else |
| 47 | echo "pom.xml file not found" |
| 48 | exit 1 |
| 49 | fi |
| 50 | } |
| 51 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 52 | function start_netconf_server() { |
| 53 | docker-compose -f $1 up -d $NETOPEER_CONTAINER_NAME |
| 54 | echo |
| 55 | echo "NETCONF server container's logs:" |
| 56 | docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepoctl --install --yang=/netconf/\$NETCONF_MODEL.yang --owner=netconf:nogroup --permissions=777" |
| 57 | docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepocfg --import=/netconf/\$NETCONF_MODEL.data.xml --datastore=startup --format=xml --level=3 \$NETCONF_MODEL" |
| 58 | docker exec -d $NETOPEER_CONTAINER_NAME /bin/bash -c "/opt/dev/sysrepo/build/examples/application_example \$NETCONF_MODEL" |
| 59 | echo |
| 60 | } |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 61 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 62 | function start(){ |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 63 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 64 | if [[ $(running_containers) ]]; then |
| 65 | echo "Simulator containers are already up" |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 66 | else |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 67 | echo "Starting simulator containers using netconf model specified in config/netconf.env" |
| 68 | |
| 69 | archive_logs |
| 70 | start_netconf_server $1 |
| 71 | docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME |
| 72 | RUNNING_COMPOSE_CONFIG=$1 |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 73 | fi |
| 74 | } |
| 75 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 76 | function running_containers(){ |
| 77 | docker-compose -f $COMPOSE_FILE_NAME ps -q && docker-compose -f $DEV_COMPOSE_FILE_NAME ps -q |
| 78 | } |
| 79 | |
| 80 | function stop(){ |
| 81 | |
| 82 | if [[ $(running_containers) ]]; then |
| 83 | docker-compose -f $RUNNING_COMPOSE_CONFIG down |
| 84 | else |
| 85 | echo "Simulator containers are already down" |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | function run_simulator(){ |
| 90 | cat << EndOfMessage |
| 91 | Simulator response: |
| 92 | $(curl -s -X POST -H "Content-Type: application/json" -d @config/config.json $SIMULATOR_START_URL | json_pp) |
| 93 | EndOfMessage |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | function stop_simulator(){ |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 97 | cat << EndOfMessage |
| 98 | Simulator response: |
| 99 | $(curl -s -X POST $SIMULATOR_STOP_URL | json_pp) |
| 100 | EndOfMessage |
| 101 | } |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 102 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 103 | function get_status(){ |
| 104 | |
| 105 | if [[ $(running_containers) ]]; then |
| 106 | print_status |
| 107 | else |
| 108 | echo "Simulator containers are down" |
| 109 | fi |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | function print_status(){ |
| 113 | cat << EndOfMessage |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 114 | $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps) |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 115 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 116 | Simulator response: |
| 117 | $(curl -s -X GET $SIMULATOR_STATUS_URL | json_pp) |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 118 | EndOfMessage |
| 119 | } |
| 120 | |
| 121 | function print_help(){ |
| 122 | cat << EndOfMessage |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 123 | Available options: |
| 124 | build - locally builds simulator image from existing code |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 125 | start - starts simulator and netopeer2 containers using remote simulator image and specified model name |
| 126 | start-dev - starts simulator and netopeer2 containers using remote simulator image |
| 127 | start-debug - starts only netopeer2 container |
| 128 | run-simulator - starts sending PNF registration messages with parameters specified in config.json |
| 129 | stop-simulator - stop sending PNF registration messages |
| 130 | stop - stops both containers |
| 131 | status - prints simulator status |
| 132 | clear-logs - deletes log folder |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 133 | |
| 134 | Starting simulation: |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 135 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 136 | - Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine |
| 137 | - To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json |
| 138 | |
| 139 | To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status". |
| 140 | If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again |
| 141 | Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 142 | |
| 143 | FOR DEVELOPERS |
| 144 | 1. Build local simulator image using "./simulator.sh build" |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 145 | 2. Run containers with "./simulator.sh start-dev" |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 146 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 147 | If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 148 | EndOfMessage |
| 149 | } |
| 150 | |
Pawel Kadlubanski | 4326965 | 2018-06-15 16:13:45 +0200 | [diff] [blame] | 151 | function archive_logs(){ |
| 152 | |
| 153 | if [ -d logs ]; then |
| 154 | echo "Moving log file to archive" |
| 155 | DIR_PATH=logs/archive/simulator[$(timestamp)] |
| 156 | mkdir -p $DIR_PATH |
| 157 | if [ -f logs/pnfsimulator.log ]; then |
| 158 | mv logs/pnfsimulator.log $DIR_PATH |
| 159 | fi |
| 160 | |
| 161 | if [ -f logs/*.xml ]; then |
| 162 | mv logs/*.xml $DIR_PATH |
| 163 | fi |
| 164 | |
| 165 | else |
| 166 | mkdir logs |
| 167 | fi |
| 168 | } |
| 169 | |
| 170 | function clear_logs(){ |
| 171 | |
| 172 | if [[ $(running_containers) ]]; then |
| 173 | echo "Cannot delete logs when simulator is running" |
| 174 | else |
| 175 | rm -rf logs |
| 176 | fi |
| 177 | } |
| 178 | |
| 179 | function timestamp(){ |
| 180 | date "+%Y-%m-%d_%T" |
Pawel Kadlubanski | 8d0066e | 2018-05-11 14:35:43 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | main $@ |