summaryrefslogtreecommitdiffstats
path: root/tools/clean-genesis.sh
blob: 4d18f788041ba53ebf1e3725c137185c59a1075b (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash

set -x

log ()  {
    printf "$(date)\t%s\n" "${1}"
}

TO_RM=(
    "/etc/cni"
    "/etc/coredns"
    "/etc/etcd"
    "/etc/genesis"
    "/etc/kubernetes"
    "/etc/promenade"
    "/etc/systemd/system/kubelet.service"
    "/home/ceph"
    "/tmp/tmp.*"
    "/var/lib/etcd"
    "/var/lib/kubelet"
    "/var/lib/openstack-helm"
    "/var/log/containers"
    "/var/log/pods"
    "/var/log/armada"
    "/etc/modprobe.d/krbd_blacklist.conf"
    "/srv/elasticsearch-data"
    "/srv/elasticsearch-master"
    "/srv/prometheus-data"
)

prune_docker() {
    log "Docker prune"
    docker volume prune -f
    docker system prune -a -f
}

remove_containers() {
    log "Remove all Docker containers"
    docker ps -aq 2> /dev/null | xargs --no-run-if-empty docker rm -fv
    log "Remove all containerd pods"
    systemctl restart containerd || true
    sleep 60
    crictl rmp -a -f || true
    log "Remove any remaining containerd containers"
    crictl rm -a -f || true
    systemctl stop containerd || true
}

remove_files() {
    for item in "${TO_RM[@]}"; do
        log "Removing ${item}"
        rm -rf "${item}"
    done
}

reset_docker() {
    log "Remove all local Docker images"
    docker images -qa | xargs --no-run-if-empty docker rmi -f
    log "Remove remaining Docker files"
    systemctl stop docker
    if ! rm -rf /var/lib/docker/*; then
        log "Failed to cleanup some files in /var/lib/docker"
        find /var/lib/docker
    fi
    log "Remove all local containerd data"
    if ! rm -rf /var/lib/containerd/*; then
        log "Failed to cleanup some files in /var/lib/containerd/"
        find /var/lib/containerd
    fi
}

stop_kubelet() {
    log "Stop Kubelet and clean pods"
    systemctl stop kubelet || true
    # Issue with orhan PODS
    # https://github.com/kubernetes/kubernetes/issues/38498
    find /var/lib/kubelet/pods 2> /dev/null | while read orphan_pod; do
        if [[ ${orphan_pod} == *io~secret/* ]] || [[ ${orphan_pod} == *empty-dir/* ]]; then
            umount "${orphan_pod}" || true
            rm -rf "${orphan_pod}"
        fi
    done
}

wipe_disk() {
    CEPH_VG=$(vgs | tail -n +1 | awk '{print $1}' | grep ceph-vg- | paste -d " "  - -)

    if [[ x$CEPH_VG != 'x' ]]; then
        vgremove -f $CEPH_VG
    fi

    log "Wipe out CEPH disks"
    apt install --yes gdisk
    echo "====Earsing disk sdb===="
    sudo sgdisk -Z /dev/sdb
    sudo dd if=/dev/zero of=/dev/sdb bs=1M count=200
}

service_exists() {
    local n=$1
    if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | cut -f1 -d' ') == $n.service ]]; then
        return 0
    else
        return 1
    fi
}

FORCE=0
RESET_DOCKER=0
while getopts "fk" opt; do
    case "${opt}" in
        f)
            FORCE=1
            ;;
        k)
            RESET_DOCKER=1
            ;;
        *)
            echo "Unknown option"
            exit 1
            ;;
    esac
done

if [[ $FORCE == "0" ]]; then
    echo Warning:  This cleanup script is very aggresive.  Run with -f to avoid this prompt.
    while true; do
        read -p "Are you sure you wish to proceed with aggressive cleanup?" yn
        case $yn in
            [Yy]*)
                RESET_DOCKER=1
                break
                ;;
            *)
                echo Exitting.
                exit 1
        esac
    done
fi

if service_exists kubelet; then
    stop_kubelet
    remove_containers
    remove_files
    prune_docker
    systemctl daemon-reload
    systemctl start containerd.service
    if [[ $RESET_DOCKER == "1" ]]; then
        echo "hi"
        reset_docker
    fi
    systemctl start containerd
#sudo crictl pull docker.io/busybox:1.28.3
#sudo crictl pull docker.io/haproxy:1.8.19
    service docker restart
fi
wipe_disk