-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_tasks.sh
executable file
·140 lines (116 loc) · 4.71 KB
/
final_tasks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
ONEDATA_STORAGE_PATH="/tmp/onedata"
ONE_ENV_DEPLOYMENT_DIR="/home/bamboo/.one-env"
ONE_ENV_ARTIFACTS_DIR="${PWD}/one_env/artifacts_dir"
DOCKER_CMD_TIMEOUT=10
CLEAR_DIRECTORIES_TIMEOUT=60
DELETE_HELM_RELEASE_TIMEOUT=60
DELETE_K8S_NAMESPACE_TIMEOUT=60
DELETE_K8S_ELEM_TIMEOUT=20
DELETE_LOOP_TIMEOUT=60
execute_with_timeout() {
TIMEOUT=$1
shift 1
CMD=$@
timeout --kill-after ${TIMEOUT} ${TIMEOUT} bash -c "${CMD}"
}
# clear spaces data and one-env deployment and artifacts dir
if [ -d ${ONE_ENV_ARTIFACTS_DIR} ]
then
echo "Clearing ${ONEDATA_STORAGE_PATH} and ${ONE_ENV_DEPLOYMENT_DIR} and ${ONE_ENV_ARTIFACTS_DIR}"
execute_with_timeout ${CLEAR_DIRECTORIES_TIMEOUT} docker run \
-v ${ONEDATA_STORAGE_PATH}:${ONEDATA_STORAGE_PATH} \
-v ${ONE_ENV_DEPLOYMENT_DIR}:${ONE_ENV_DEPLOYMENT_DIR} \
-v ${ONE_ENV_ARTIFACTS_DIR}:${ONE_ENV_ARTIFACTS_DIR} \
alpine sh -c \"rm -rf ${ONEDATA_STORAGE_PATH}/* ${ONE_ENV_DEPLOYMENT_DIR}/* ${ONE_ENV_ARTIFACTS_DIR}/*\"
else
echo "Clearing ${ONEDATA_STORAGE_PATH} and ${ONE_ENV_DEPLOYMENT_DIR}"
execute_with_timeout ${CLEAR_DIRECTORIES_TIMEOUT} docker run \
-v ${ONEDATA_STORAGE_PATH}:${ONEDATA_STORAGE_PATH} \
-v ${ONE_ENV_DEPLOYMENT_DIR}:${ONE_ENV_DEPLOYMENT_DIR} \
alpine sh -c \"rm -rf ${ONEDATA_STORAGE_PATH}/* ${ONE_ENV_DEPLOYMENT_DIR}/*\"
fi
# clear k8s
echo "Cleaning helm deployments"
HELM_RELEASES=$(helm ls --all --short)
for release in ${HELM_RELEASES}
do
execute_with_timeout ${DELETE_HELM_RELEASE_TIMEOUT} helm uninstall ${release}
done
echo "Cleanining pods"
NAMESPACES=$(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system)
for namespace in ${NAMESPACES};
do
execute_with_timeout ${DELETE_K8S_NAMESPACE_TIMEOUT} kubectl delete ns ${namespace}
done
# pv are not in any namespace so we have to delete them separately
echo "Cleaning kube persistant volumes"
PVS=$(kubectl get pv --no-headers -o custom-columns=":metadata.name")
for pv in ${PVS}
do
execute_with_timeout ${DELETE_K8S_ELEM_TIMEOUT} kubectl delete pv ${pv}
done
# sometimes deleting helm release / k8s namespace leaves some deployments
DEPLOYMENTS=$(kubectl get deployments --no-headers -o custom-columns=":metadata.name")
for deployment in ${DEPLOYMENTS}
do
execute_with_timeout ${DELETE_K8S_ELEM_TIMEOUT} kubectl delete deployment ${deployment}
done
PODS=$(kubectl get pods --no-headers -o custom-columns=":metadata.name")
for pod in ${PODS}
do
execute_with_timeout ${DELETE_K8S_ELEM_TIMEOUT} kubectl delete pod ${pod}
done
SERVICES=$(kubectl get services --no-headers -o custom-columns=":metadata.name" | grep -v kubernetes)
for service in ${SERVICES}
do
execute_with_timeout ${DELETE_K8S_ELEM_TIMEOUT} kubectl delete service ${service}
done
# Clean swift docker with special treatment as it hangs sometimes
echo "Cleaning dockswift container"
sudo /usr/local/sbin/clean-dockswift.sh
# clear docker
echo "Cleaning docker containers"
CONTAINERS=$(docker ps -qa)
CONTAINERS_TO_REMOVE=${CONTAINERS}
for container in ${CONTAINERS}
do
NAMESPACE=$(execute_with_timeout ${DOCKER_CMD_TIMEOUT} docker inspect --format '"{{ index .Config.Labels \"io.kubernetes.pod.namespace\"}}"' ${container})
NAME=$(execute_with_timeout ${DOCKER_CMD_TIMEOUT} docker inspect --format '"{{ index .Name}}"' ${container})
if [[ ${NAMESPACE} ]]
then
if [[ "${NAMESPACE}" == "kube-system" ]]
then
CONTAINERS_TO_REMOVE=( "${CONTAINERS_TO_REMOVE[@]/$container}" )
fi
fi
if [[ "${NAME}" == "/docuum" ]]
then
CONTAINERS_TO_REMOVE=( "${CONTAINERS_TO_REMOVE[@]/$container}" )
fi
done
echo "Stalled docker containers to remove: "
echo ${CONTAINERS_TO_REMOVE}
echo "Removing stalled docker containers"
for container in ${CONTAINERS_TO_REMOVE}
do
execute_with_timeout ${DOCKER_CMD_TIMEOUT} docker kill ${container}
execute_with_timeout ${DOCKER_CMD_TIMEOUT} docker rm -fv ${container}
done
STALLED_DOCKER_VOLUMES=$(docker volume ls -q)
echo "Stalled docker volumes to remove: "
echo ${STALLED_DOCKER_VOLUMES}
echo "Removing stalled docker volumes"
for volume in ${STALLED_DOCKER_VOLUMES}
do
execute_with_timeout ${DOCKER_CMD_TIMEOUT} docker volume rm ${volume}
done
# Remove loopdevices created in Onepanel's Ceph tests
echo "Removing stalled loopdevices"
# Run in docker to obtain root privileges.
# Use ubuntu 14.10 as newer versions don't have dmsetup.
# For unknown reasons does not work with execute_with_timeout.
timeout --kill-after ${DELETE_LOOP_TIMEOUT} ${DELETE_LOOP_TIMEOUT} \
docker run --rm --privileged ubuntu:14.10 sh -c \
'losetup -D; dmsetup ls | cut -f 1 | grep -F osd-- | xargs -tr -n 1 dmsetup remove;'
echo "Done"