Last active
September 22, 2023 01:59
-
-
Save rmkraus/1841e2ceb80555bce65e5dd3d377c038 to your computer and use it in GitHub Desktop.
A package manager for container and k8s tools that isn't a package manager.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
set -e | |
# credit: http://nparikh.org/notes/zshrc.txt | |
# Usage: extract <file> | |
# Description: extracts archived files / mounts disk images | |
# Note: .dmg/hdiutil is Mac OS X-specific. | |
extract () { | |
if [ -f $1 ]; then | |
case $1 in | |
*.tar.bz2) tar -jxvf $1 ;; | |
*.tar.gz) tar -zxvf $1 ;; | |
*.bz2) bunzip2 $1 ;; | |
*.dmg) hdiutil mount $1 ;; | |
*.gz) gunzip $1 ;; | |
*.tar) tar -xvf $1 ;; | |
*.tbz2) tar -jxvf $1 ;; | |
*.tgz) tar -zxvf $1 ;; | |
*.zip) unzip $1 ;; | |
*.ZIP) unzip $1 ;; | |
*.pax) cat $1 | pax -r ;; | |
*.pax.Z) uncompress $1 --stdout | pax -r ;; | |
*.Z) uncompress $1 ;; | |
*) echo "'$1' cannot be extracted/mounted via extract()" ;; | |
esac | |
else | |
echo "'$1' is not a valid file" | |
fi | |
} | |
function get_latest_gh_release() { | |
repo=$1 | |
asset=$2 | |
latest=$(gh release -R $repo list | grep -P '([^ ]+) *Latest' | awk -F'\t' '{ print $3 }') | |
# check if this version is already downloaded | |
if [ -d $TOOL_DIR/$repo/$latest ]; then | |
echo "Already at latest." | |
return 1 | |
fi | |
echo "Installing $repo @ $latest." | |
# make the working directory | |
cd $TOOL_DIR | |
mkdir -p $repo/$latest | |
cd $repo/$latest | |
# download the asset | |
gh release -R $repo download "$latest" -p $asset | |
# if its an rpm, install it | |
if [[ "$asset" == *.rpm ]]; then | |
sudo yum install -y $asset | |
return 0 | |
fi | |
# if its a deb, install it | |
if [[ "$asset" == *.deb ]]; then | |
sudo apt-get install -y $asset | |
return 0 | |
fi | |
# treat everything else as an archive | |
extract $asset | |
rm $asset | |
# install binary files in the archive | |
for e in $(find . -type f -executable); do | |
unlink $BIN_DIR/$(basename $e) &> /dev/null || true | |
ln -s $(pwd)/$e $BIN_DIR/$(basename $e) | |
echo "Installed $e" | |
done | |
cd - > /dev/null | |
return 0 | |
} | |
function rename_link() { | |
old_busted=$1 | |
new_hotness=$2 | |
ln -s $(readlink $BIN_DIR/$old_busted) $BIN_DIR/$new_hotness | |
unlink $BIN_DIR/$old_busted | |
} | |
function krew_install_latest() { | |
plugin=$1 | |
if kubectl $plugin &> /dev/null; then | |
kubectl krew upgrade $plugin || true | |
else | |
kubectl krew install $plugin | |
fi | |
} | |
# CONSTANTS | |
TOOL_DIR=~/.local/share/k8s-tools | |
BIN_DIR=~/.local/bin | |
OS_FAMILY=$(source /etc/os-release; echo $ID_LIKE | awk '{print $1}') | |
SHELL=$(basename $SHELL) | |
echo -e "\n\n\nSETUP HOME DIRECTORY" | |
mkdir -p $BIN_DIR $TOOL_DIR | |
# Install GitHub cli | |
echo -e "\n\n\nINSTALL GITHUB CLI" | |
if [ "$OS_FAMILY" == "rhel" ]; then | |
sudo dnf install -y 'dnf-command(config-manager)' wget curl | |
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo | |
sudo dnf install -y gh | |
else | |
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) | |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg | |
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg | |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null | |
sudo apt update | |
sudo apt install gh wget curl -y | |
fi | |
# ensure gh is logged in | |
if ! gh auth status; then | |
gh auth login | |
fi | |
# install k9s | |
echo -e "\n\n\nINSTALL K9S" | |
get_latest_gh_release "derailed/k9s" "k9s_Linux_amd64.tar.gz" || true | |
# install kbenv, helmenv, ocenv | |
echo -e "\n\n\nINSTALL KBENV, HELMENV, OCENV" | |
if get_latest_gh_release "little-angry-clouds/kubernetes-binaries-managers" "kubernetes-binaries-managers_*_linux_amd64.tar.gz"; then | |
rename_link kubectl-wrapper kubectl | |
latest=$(kbenv list remote | head -n 1) kbenv install $latest; kbenv use $latest | |
rename_link helm-wrapper helm | |
latest=$(helmenv list remote | head -n 1) helmenv install $latest; helmenv use $latest | |
rename_link oc-wrapper oc | |
latest=$(ocenv list remote | head -n 1) ocenv install $latest; ocenv use $latest | |
fi | |
mkdir -p ~/.bin | |
kbenv install 1.26.9 | |
kbenv use 1.26.9 | |
# install fuzzy finder | |
echo -e "\n\n\nINSTALLING FUZZY FINDER" | |
get_latest_gh_release "junegunn/fzf" "fzf-0.42.0-linux_amd64.tar.gz" || true | |
# install krew | |
echo -e "\n\n\nINSTALLING KREW" | |
if ! kubectl krew &> /dev/null; then | |
if get_latest_gh_release "kubernetes-sigs/krew" "krew-linux_amd64.tar.gz"; then | |
$BIN_DIR/krew* install krew | |
unlink $BIN_DIR/krew* | |
fi | |
echo 'export PATH=$PATH:~/.krew/bin' >> ~/.${SHELL}rc | |
else | |
kubectl krew update | |
kubectl krew upgrade krew || true | |
fi | |
# install kubectl plugins | |
echo -e "\n\n\nINSTALLING KUBECTL PLUGINS" | |
krew_install_latest ns | |
krew_install_latest ctx | |
krew_install_latest deprecations | |
krew_install_latest insider | |
krew_install_latest node-shell | |
# install k8s config sync | |
gh gist view bab4ec25b031057e7f5e320bc1974031 > $BIN_DIR/kubectl-sync | |
chmod +x $BIN_DIR/kubectl-sync |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment