6bb3b15c2e1578bf7925d578c39b39463ea6f45b
[icn.git] / env / ubuntu / bootloader-env / 02_clean_bootloader_package_req.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 source $(dirname $PWD)/../lib/common.sh
5 source $(dirname $PWD)/../lib/logging.sh
6
7 if [[ $EUID -ne 0 ]]; then
8     echo "This script must be run as root"
9     exit 1
10 fi
11
12 function autoremove {
13     rm -rf /etc/apt/sources.list.d/*
14 }
15
16 function clean_essential_packages {
17     apt-get update
18     for package in crudini curl dnsmasq figlet golang nmap patch psmisc \
19         python3-pip python3-requests python3-setuptools vim wget; do
20         apt-get remove $package -y
21     done
22     update-alternatives --remove python /usr/bin/python3
23     update-alternatives --remove pip /usr/bin/pip3
24
25     autoremove
26 }
27
28 function check_prerequisite {
29     if !(which pip); then
30         apt-get install python3-pip -y
31     fi
32     update-alternatives --install /usr/bin/python python /usr/bin/python3 1
33     update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
34
35     if !(which curl); then
36         apt-get install curl -y
37     fi
38
39     if !(which add-apt-repository); then
40         apt-get install software-properties-common -y
41     fi
42 }
43
44 function clean_ironic_packages {
45     for package in python3-ironicclient \
46         python3-ironic-inspector-client \
47         python3-openstackclient genisoimage; do
48         apt-get remove $package -y
49     done
50 }
51
52 function clean_docker_packages {
53     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
54     add-apt-repository \
55         "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
56         $(lsb_release -cs) \
57         stable"
58     apt-get update
59     docker rmi -f $(docker image ls -a -q)
60     apt-get remove docker-ce -y
61     apt-get remove -y docker \
62         docker-engine \
63         docker.io \
64         containerd \
65         runc \
66         docker-ce
67     apt-get purge docker-* -y --allow-change-held-packages
68     apt-get update
69 }
70
71 function clean_kubernetes_packages {
72     #Just to make sure kubernetes packages are removed during the download
73     curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
74     bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
75 deb https://apt.kubernetes.io/ kubernetes-xenial main
76 EOF'
77     apt-get update
78     apt-get remove kubelet kubeadm kubectl -y
79 }
80
81 function clean_apt_cache {
82     shopt -s extglob
83     pushd /var/cache/apt/archives
84
85     if [ $(ls -1q . | wc -l ) -ge 3 ]; then
86         $(rm !("lock"|"partial"))
87     fi
88     popd
89
90 }
91
92 if [ "$1" == "--only-packages" ]; then
93     check_prerequisite
94     clean_docker_packages || true
95     autoremove
96     exit 0
97 fi
98
99 check_prerequisite
100 clean_apt_cache
101 clean_kubernetes_packages
102 clean_docker_packages
103 clean_ironic_packages
104 clean_essential_packages