Refactor folder structure.
[sim/o1-interface.git] / ntsimulator / scripts / clean.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # Copyright 2020 highstreet technologies GmbH and others
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9
10 #     http://www.apache.org/licenses/LICENSE-2.0
11
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ################################################################################
18
19 if [ "$#" -ne 1 ]; then
20     echo "ID Parameter missing."
21         echo "Usage: $0 id/all
22         
23 where id is the docker container id of the NTS Manager instance that we want to clean. Use 'all' if you want to clean all containers and all managers."
24         exit 1
25 fi
26
27 if [ "$1" = "all" ]; then
28         echo "Cleaning up all NTS simulated devices..."
29
30         mapfile -t NTS_containers < <( docker ps -a --filter "label=NTS_Manager" --format "{{.ID}}" )
31
32         CONTAINERS=""
33
34         if [ ${#NTS_containers[@]} -gt 0 ]
35         then
36
37                 for container in "${NTS_containers[@]}"
38                 do
39                         CONTAINERS="$CONTAINERS $container"
40                 done
41                 echo "Cleaning up containers: $CONTAINERS"
42                 docker kill $CONTAINERS > /dev/null 2>&1
43                 docker rm $CONTAINERS > /dev/null 2>&1
44         fi
45
46         echo "Cleaning up all NTS Managers..."
47
48         mapfile -t NTS_containers < <( docker ps -a --filter "label=NTS-manager" --format "{{.ID}}" )
49
50         CONTAINERS=""
51
52         if [ ${#NTS_containers[@]} -gt 0 ]
53         then
54
55                 for container in "${NTS_containers[@]}"
56                 do
57                         CONTAINERS="$CONTAINERS $container"
58                 done
59                 echo "Cleaning up containers: $CONTAINERS"
60                 docker kill $CONTAINERS > /dev/null 2>&1
61                 docker rm $CONTAINERS > /dev/null 2>&1
62         fi
63
64 else
65         echo "Cleaning up containers started by the NTS Manager $1..."
66
67         mapfile -t NTS_containers < <( docker ps -a --filter "label=NTS_Manager=$1" --format "{{.ID}}" )
68
69         CONTAINERS=""
70
71         if [ ${#NTS_containers[@]} -gt 0 ]
72         then
73
74                 for container in "${NTS_containers[@]}"
75                 do
76                         CONTAINERS="$CONTAINERS $container"
77                 done
78                 echo "Cleaning up containers: $CONTAINERS"
79                 docker kill $CONTAINERS > /dev/null 2>&1
80                 docker rm $CONTAINERS > /dev/null 2>&1
81         fi
82
83         echo "Cleaning NTS Manager $1..."
84         docker kill $1 > /dev/null 2>&1
85         docker rm $1 > /dev/null 2>&1
86 fi
87
88 echo "All cleaned up!"