Skip to content

Commit dd009f2

Browse files
committed
Fix remarks reported by Sunil Arora such as : 1) use temp dir, 2) check os, hw and exists if non supported, 3) add prereq - curl, tar, if kubebuilder folder is empty
1 parent 4658025 commit dd009f2

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

scripts/install.sh

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
#
44
# This file will be fetched as: curl -L https://git.io/getLatestKubebuilder | sh -
@@ -8,13 +8,13 @@
88
# It lets users to do curl -L https://git.io//getLatestKubebuilder | KUBEBUILDER_VERSION=1.0.5 sh -
99
# for instance to change the version fetched.
1010

11-
OS="$(uname)"
12-
if [ "x${OS}" = "xDarwin" ] ; then
13-
OSEXT="darwin"
14-
else
15-
OSEXT="linux"
16-
fi
17-
ARCH=amd64
11+
# Check if the program is installed, otherwise exit
12+
function command_exists () {
13+
if ! [ -x "$(command -v $1)" ]; then
14+
echo "Error: $1 program is not installed." >&2
15+
exit 1
16+
fi
17+
}
1818

1919
if [ "x${KUBEBUILDER_VERSION}" = "x" ] ; then
2020
KUBEBUILDER_VERSION=$(curl -L -s https://api.github.com/repos/kubernetes-sigs/kubebuilder/releases/latest | \
@@ -23,18 +23,56 @@ fi
2323

2424
KUBEBUILDER_VERSION=${KUBEBUILDER_VERSION#"v"}
2525
KUBEBUILDER_VERSION_NAME="kubebuilder_${KUBEBUILDER_VERSION}"
26+
KUBEBUILDER_DIR=/usr/local/kubebuilder
27+
28+
# Check if folder containing kubebuilder executable exists and is not empty
29+
if [ -d "$KUBEBUILDER_DIR" ]; then
30+
if [ "$(ls -A $KUBEBUILDER_DIR)" ]; then
31+
echo "\n/usr/local/kubebuilder folder is not empty. Please delete or backup it before to install ${KUBEBUILDER_VERSION_NAME}"
32+
exit 1
33+
fi
34+
fi
35+
36+
# Check if curl, tar commands/programs exist
37+
command_exists curl
38+
command_exists tar
39+
40+
# Determine OS
41+
OS="$(uname)"
42+
case $OS in
43+
Darwin)
44+
OSEXT="darwin"
45+
;;
46+
Linux)
47+
OSEXT="linux"
48+
;;
49+
*)
50+
echo "Only OSX and Linux OS are supported !"
51+
exit 1
52+
;;
53+
esac
54+
55+
HW=$(uname -m)
56+
case $HW in
57+
x86_64)
58+
ARCH=amd64 ;;
59+
*)
60+
echo "Only x86_64 machines are supported !"
61+
exit 1
62+
;;
63+
esac
64+
65+
TMP_DIR=$(mktemp -d)
66+
67+
# Downloading Kuberbuilder compressed file using curl program
2668
URL="https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${KUBEBUILDER_VERSION}/${KUBEBUILDER_VERSION_NAME}_${OSEXT}_${ARCH}.tar.gz"
27-
echo "Downloading ${KUBEBUILDER_VERSION_NAME} from $URL ..."
28-
curl -L "$URL" | tar xz
29-
30-
echo "Downloaded these executable files into $NAME: "
31-
ls "${KUBEBUILDER_VERSION_NAME}_${OSEXT}_${ARCH}/bin"
32-
mv ${KUBEBUILDER_VERSION_NAME}_${OSEXT}_${ARCH} kubebuilder && sudo mv -f kubebuilder /usr/local/
33-
RETVAL=$?
34-
35-
if [ $RETVAL -eq 0 ]; then
36-
echo "Add kubebuilder to your path; e.g copy paste in your shell and/or ~/.profile:"
37-
echo "export PATH=\$PATH:/usr/local/kubebuilder/bin"
38-
else
39-
echo "\n/usr/local/kubebuilder folder is not empty. Please delete or backup it before to install ${KUBEBUILDER_VERSION_NAME}"
40-
fi
69+
echo "Downloading ${KUBEBUILDER_VERSION_NAME}\nfrom $URL\n"
70+
curl -L "$URL"| tar xz -C $TMP_DIR
71+
72+
echo "Downloaded executable files"
73+
ls "$TMP_DIR/${KUBEBUILDER_VERSION_NAME}_${OSEXT}_${ARCH}/bin"
74+
75+
echo "Moving files to $KUBEBUILDER_DIR folder\n"
76+
mv $TMP_DIR/${KUBEBUILDER_VERSION_NAME}_${OSEXT}_${ARCH} $TMP_DIR/kubebuilder && sudo mv -f $TMP_DIR/kubebuilder /usr/local/
77+
78+
rm -rf $TMP_DIR

0 commit comments

Comments
 (0)