From 6190b4d1e88cf7e51724acdf186a0fdc4abd276d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 26 May 2024 16:09:34 -0700 Subject: [PATCH] Make all installers idempotent --- install/chrome.sh | 12 +++++++----- install/docker.sh | 20 +++++++++++--------- install/dotfiles.sh | 21 ++++++++++++++++----- install/fonts.sh | 18 ++++++++++-------- install/gh.sh | 12 +++++++----- install/lazydocker.sh | 16 +++++++++------- install/lazygit.sh | 16 +++++++++------- install/neovim.sh | 10 ++++++---- install/nodenv.sh | 25 +++++++++++++------------ install/ruby.sh | 12 +++++++----- install/typora.sh | 20 +++++++++++--------- install/ulauncher.sh | 10 ++++++---- 12 files changed, 112 insertions(+), 80 deletions(-) diff --git a/install/chrome.sh b/install/chrome.sh index 5ceda64..d545e2d 100644 --- a/install/chrome.sh +++ b/install/chrome.sh @@ -1,5 +1,7 @@ -cd ~/Downloads -wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -sudo dpkg -i google-chrome-stable_current_amd64.deb -rm google-chrome-stable_current_amd64.deb -cd - +if ! command -v google-chrome &>/dev/null; then + cd ~/Downloads + wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb + sudo dpkg -i google-chrome-stable_current_amd64.deb + rm google-chrome-stable_current_amd64.deb + cd - +fi diff --git a/install/docker.sh b/install/docker.sh index 56b0950..403be26 100644 --- a/install/docker.sh +++ b/install/docker.sh @@ -1,11 +1,13 @@ -DOCKER_COMPOSE_VERSION="2.27.0" -sudo apt install -y docker.io docker-buildx +if ! command -v docker &>/dev/null; then + DOCKER_COMPOSE_VERSION="2.27.0" + sudo apt install -y docker.io docker-buildx -sudo usermod -aG docker ${USER} -DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker} -mkdir -p $DOCKER_CONFIG/cli-plugins -curl -SL https://github.com/docker/compose/releases/download/v$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose -chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose + sudo usermod -aG docker ${USER} + DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker} + mkdir -p $DOCKER_CONFIG/cli-plugins + curl -SL https://github.com/docker/compose/releases/download/v$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose + chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose -sudo docker create -d --restart unless-stopped -p 3306:3306 --name=mysql8 -e MYSQL_ROOT_PASSWORD= -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:8 -sudo docker create -d --restart unless-stopped -p 6379:6379 --name=redis redis + sudo docker create -d --restart unless-stopped -p 3306:3306 --name=mysql8 -e MYSQL_ROOT_PASSWORD= -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:8 + sudo docker create -d --restart unless-stopped -p 6379:6379 --name=redis redis +fi diff --git a/install/dotfiles.sh b/install/dotfiles.sh index 19620dc..a219812 100644 --- a/install/dotfiles.sh +++ b/install/dotfiles.sh @@ -7,17 +7,28 @@ for entry in dotfiles/*; do # Any existing files will be renamed .bak if [ -f "$entry" ]; then target=~/."$(basename "$entry")" - [ -e "$target" ] && mv "$target" "$target.bak" - ln -s "$(pwd)/$entry" "$target" + + if [ -e "$target" ] && [ "$(readlink "$target")" != "$(pwd)/$entry" ]; then + mv "$target" "$target.bak" + fi + + if [ ! -e "$target" ]; then + ln -s "$(pwd)/$entry" "$target" + fi fi # Link all directories in ~/.config/ # Any existing directories will be renamed .bak if [ -d "$entry" ]; then target=~/.config/"$(basename "$entry")" - [ -e "$target" ] && mv "$target" "$target.bak" - ln -s "$(pwd)/$entry" "$target" + + if [ -e "$target" ] && [ "$(readlink "$target")" != "$(pwd)/$entry" ]; then + mv "$target" "$target.bak" + fi + + if [ ! -e "$target" ]; then + ln -s "$(pwd)/$entry" "$target" + fi fi done - unset entry diff --git a/install/fonts.sh b/install/fonts.sh index 3c826da..8d70df8 100644 --- a/install/fonts.sh +++ b/install/fonts.sh @@ -1,8 +1,10 @@ -cd ~/Downloads -wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/CascadiaMono.zip -unzip CascadiaMono.zip -d CascadiaFont -mkdir -p ~/.local/share/fonts -cp CascadiaFont/*.ttf ~/.local/share/fonts -rm -rf CascadiaMono.zip CascadiaFont -fc-cache -cd - +if ! [ -f "$HOME/.local/share/fonts/CaskaydiaMonoNerdFont-Regular.ttf" ]; then + cd ~/Downloads + wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/CascadiaMono.zip + unzip CascadiaMono.zip -d CascadiaFont + mkdir -p ~/.local/share/fonts + cp CascadiaFont/*.ttf ~/.local/share/fonts + rm -rf CascadiaMono.zip CascadiaFont + fc-cache + cd - +fi diff --git a/install/gh.sh b/install/gh.sh index c73d7de..9e9c627 100644 --- a/install/gh.sh +++ b/install/gh.sh @@ -1,5 +1,7 @@ -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 -y +if ! command -v gh &>/dev/null; then + 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 -y +fi diff --git a/install/lazydocker.sh b/install/lazydocker.sh index 4ca1086..b42da10 100644 --- a/install/lazydocker.sh +++ b/install/lazydocker.sh @@ -1,7 +1,9 @@ -cd ~/Downloads -LAZYDOCKER_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazydocker/releases/latest" | grep -Po '"tag_name": "v\K[^"]*') -curl -sLo lazydocker.tar.gz "https://github.com/jesseduffield/lazydocker/releases/latest/download/lazydocker_${LAZYDOCKER_VERSION}_Linux_x86_64.tar.gz" -tar -xf lazydocker.tar.gz lazydocker -sudo install lazydocker /usr/local/bin -rm lazydocker.tar.gz -cd - +if ! command -v lazydocker &>/dev/null; then + cd ~/Downloads + LAZYDOCKER_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazydocker/releases/latest" | grep -Po '"tag_name": "v\K[^"]*') + curl -sLo lazydocker.tar.gz "https://github.com/jesseduffield/lazydocker/releases/latest/download/lazydocker_${LAZYDOCKER_VERSION}_Linux_x86_64.tar.gz" + tar -xf lazydocker.tar.gz lazydocker + sudo install lazydocker /usr/local/bin + rm lazydocker.tar.gz + cd - +fi diff --git a/install/lazygit.sh b/install/lazygit.sh index aab5f21..dca7e7e 100644 --- a/install/lazygit.sh +++ b/install/lazygit.sh @@ -1,7 +1,9 @@ -cd ~/Downloads -LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*') -curl -sLo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz" -tar -xf lazygit.tar.gz lazygit -sudo install lazygit /usr/local/bin -rm lazygit.tar.gz -cd - +if ! command -v lazygit &>/dev/null; then + cd ~/Downloads + LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*') + curl -sLo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz" + tar -xf lazygit.tar.gz lazygit + sudo install lazygit /usr/local/bin + rm lazygit.tar.gz + cd - +fi diff --git a/install/neovim.sh b/install/neovim.sh index d981092..f842c6f 100644 --- a/install/neovim.sh +++ b/install/neovim.sh @@ -1,4 +1,6 @@ -sudo add-apt-repository -y ppa:neovim-ppa/stable -sudo apt update -y -sudo apt install -y neovim -git clone https://github.com/LazyVim/starter ~/.config/nvim +if ! command -v neovim &>/dev/null; then + sudo add-apt-repository -y ppa:neovim-ppa/stable + sudo apt update -y + sudo apt install -y neovim + git clone https://github.com/LazyVim/starter ~/.config/nvim +fi diff --git a/install/nodenv.sh b/install/nodenv.sh index 0e31237..22ac3ee 100644 --- a/install/nodenv.sh +++ b/install/nodenv.sh @@ -1,12 +1,13 @@ -git clone https://github.com/nodenv/nodenv.git ~/.nodenv -sudo ln -vs ~/.nodenv/bin/nodenv /usr/local/bin/nodenv -cd ~/.nodenv -src/configure && make -C src || true -cd ~/ -mkdir -p "$(nodenv root)"/plugins -git clone https://github.com/nodenv/node-build.git "$(nodenv root)"/plugins/node-build -git clone https://github.com/nodenv/nodenv-aliases.git $(nodenv root)/plugins/nodenv-aliases -nodenv install 20.11.1 -nodenv global 20.11.1 -sudo ln -vs $(nodenv root)/shims/* /usr/local/bin/ - +if ! command -v nodenv &>/dev/null; then + git clone https://github.com/nodenv/nodenv.git ~/.nodenv + sudo ln -vs ~/.nodenv/bin/nodenv /usr/local/bin/nodenv + cd ~/.nodenv + src/configure && make -C src || true + cd ~/ + mkdir -p "$(nodenv root)"/plugins + git clone https://github.com/nodenv/node-build.git "$(nodenv root)"/plugins/node-build + git clone https://github.com/nodenv/nodenv-aliases.git $(nodenv root)/plugins/nodenv-aliases + nodenv install 20.11.1 + nodenv global 20.11.1 + sudo ln -vs $(nodenv root)/shims/* /usr/local/bin/ +fi diff --git a/install/ruby.sh b/install/ruby.sh index 25f5b2b..eebe599 100644 --- a/install/ruby.sh +++ b/install/ruby.sh @@ -1,6 +1,8 @@ -DEFAULT_RUBY_VERSION="3.3.1" +if ! command -v rbenv &>/dev/null; then + DEFAULT_RUBY_VERSION="3.3.1" -sudo apt install -y rbenv -git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build -rbenv install $DEFAULT_RUBY_VERSION -rbenv global $DEFAULT_RUBY_VERSION + sudo apt install -y rbenv + git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build + rbenv install $DEFAULT_RUBY_VERSION + rbenv global $DEFAULT_RUBY_VERSION +fi diff --git a/install/typora.sh b/install/typora.sh index 7b6f55f..1a5fa5a 100644 --- a/install/typora.sh +++ b/install/typora.sh @@ -1,9 +1,11 @@ -sudo snap install typora -cd ~/Downloads -git clone https://github.com/dhh/ia_typora -mkdir -p ~/.local/share/fonts -cp ia_typora/fonts/iAWriterMonoS-* ~/.local/share/fonts/ -fc-cache -mkdir -p ~/snap/typora/88/.config/Typora/themes/ -cp ia_typora/ia_typora*.css ~/snap/typora/88/.config/Typora/themes/ -cd - +if ! command -v typora &>/dev/null; then + sudo snap install typora + cd ~/Downloads + git clone https://github.com/dhh/ia_typora + mkdir -p ~/.local/share/fonts + cp ia_typora/fonts/iAWriterMonoS-* ~/.local/share/fonts/ + fc-cache + mkdir -p ~/snap/typora/88/.config/Typora/themes/ + cp ia_typora/ia_typora*.css ~/snap/typora/88/.config/Typora/themes/ + cd - +fi diff --git a/install/ulauncher.sh b/install/ulauncher.sh index ba2f09d..3d3dd1d 100644 --- a/install/ulauncher.sh +++ b/install/ulauncher.sh @@ -1,4 +1,6 @@ -sudo add-apt-repository universe -y -sudo add-apt-repository ppa:agornostal/ulauncher -y -sudo apt update -y -sudo apt install -y ulauncher +if ! command -v ulauncher &>/dev/null; then + sudo add-apt-repository universe -y + sudo add-apt-repository ppa:agornostal/ulauncher -y + sudo apt update -y + sudo apt install -y ulauncher +fi