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