support multiple sets of airship files
[yaml_builds.git] / tools / cleanup.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.        #
4 #                                                                            #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may    #
6 # not use this file except in compliance with the License.                   #
7 #                                                                            #
8 # You may obtain a copy of the License at                                    #
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, WITHOUT  #
13 # 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
18 set -eux
19
20 log () {
21 printf "$(date)\t%s\n" "${1}"
22 }
23
24
25 TO_RM=(
26 "/etc/apt/apt.conf.d/20-proxy.conf"
27 "/etc/apt/sources.list.d/promenade-sources.list"
28 "/etc/cni"
29 "/etc/coredns"
30 "/etc/docker/daemon.json"
31 "/etc/etcd"
32 "/etc/genesis"
33 "/etc/kubernetes"
34 "/etc/logrotate.d/json-logrotate"
35 "/etc/systemd/system/kubelet.service"
36 "/etc/systemd/system/docker.service.d/http-proxy.conf"
37 "/home/ceph"
38 "/usr/local/bin/armada"
39 "/usr/local/bin/helm"
40 "/usr/local/bin/kubectl"
41 "/usr/local/bin/promenade-teardown"
42 "/var/lib/anchor/calico-etcd-bootstrap"
43 "/var/lib/etcd"
44 "/var/lib/kubelet/pods"
45 "/var/lib/openstack-helm"
46 "/var/log/armada"
47 "/var/log/containers"
48 "/var/log/pods"
49 )
50
51 TO_LEAVE=(
52 "/etc/hosts"
53 "/etc/resolv.conf"
54 )
55
56 prune_docker() {
57 log "Docker prune"
58 docker volume prune -f
59 docker system prune -a -f
60 }
61
62 remove_containers() {
63 log "Remove all Docker containers"
64 docker ps -aq 2> /dev/null | xargs --no-run-if-empty docker rm -fv
65 }
66
67 remove_files() {
68 for item in "${TO_RM[@]}"; do
69 log "Removing ${item}"
70 rm -rf "${item}"
71 done
72 }
73
74 leave_files() {
75 for item in "${TO_LEAVE[@]}"; do
76 log "WARNING: === ${item} === has been modified, but we didn't revert changes."
77 done
78 }
79
80 reset_docker() {
81 log "Remove all local Docker images"
82 docker images -qa | xargs --no-run-if-empty docker rmi -f
83
84 log "Remove remaining Docker files"
85 systemctl stop docker
86 if ! rm -rf /var/lib/docker/*; then
87 log "Failed to cleanup some files in /var/lib/docker"
88 find /var/lib/docker
89 fi
90 systemctl start docker
91 }
92
93 stop_kubelet() {
94 log "Stop Kubelet and clean pods"
95 systemctl stop kubelet || true
96
97 # Issue with orhan PODS
98 # https://github.com/kubernetes/kubernetes/issues/38498
99 find var/lib/kubelet/pods 2> dev/null | while read orphan_pod; do
100 if [[ ${orphan_pod} == io~secret ]] || [[ ${orphan_pod} == empty-dir ]]; then
101 umount "${orphan_pod}" || true
102 rm -rf "${orphan_pod}"
103 fi
104 done
105 }
106
107
108 FORCE=0
109 RESET_DOCKER=0
110
111 while getopts "fk" opt; do
112 case "${opt}" in
113 f)
114 FORCE=1
115 ;;
116 k)
117 RESET_DOCKER=1
118 ;;
119 *)
120 echo "Unknown option"
121 exit 1
122 ;;
123 esac
124 done
125
126 if [[ $FORCE == "0" ]]; then
127 echo Warning: This cleanup script is very aggressive. Run with -f to avoid this prompt.
128 while true; do
129 read -p "Are you sure you wish to proceed with aggressive cleanup?" yn
130 case $yn in
131 [Yy]*)
132 RESET_DOCKER=1
133 break
134 ;;
135 *)
136 echo Exiting.
137 exit 1
138 esac
139 done
140 fi
141
142 stop_kubelet
143 remove_containers
144 remove_files
145 prune_docker
146
147 systemctl daemon-reload
148
149 if [[ $RESET_DOCKER == "1" ]]; then
150 reset_docker
151 fi
152
153 leave_files