Last active
February 22, 2023 18:10
-
-
Save Ari-E-S/bf604f611ca82a5121f4006c0bb073aa to your computer and use it in GitHub Desktop.
Bootstrap scripts
This file contains hidden or 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
| #!/usr/bin/env bash | |
| set -euf -o pipefail | |
| ## Fresh linux bootstrap | |
| # Copy this file to the new machine | |
| # Should only be used on "local" workstations. | |
| # Modify the parameters below | |
| HOSTNAME=`hostname` | |
| BOOTSTRAP_PB_BRANCH="main" | |
| BOOTSTRAP_PB_REPO="arielsalvo/bootstrap" | |
| BOOTSTRAP_KEY_TYPE="ed25519" | |
| BOOTSTRAP_KEY_LOC="$HOME/.ssh/bootstrap_id_${BOOTSTRAP_KEY_TYPE}_${HOSTNAME}${WSL_DISTRO_NAME:+_$WSL_DISTRO_NAME}" | |
| BOOTSTRAP_KEY_COMMENT="bootstrap@${HOSTNAME}${WSL_DISTRO_NAME:+_$WSL_DISTRO_NAME}" | |
| cat <<EOF | |
| This script bootstraps a local fresh linux system. | |
| The objective is to have a functional basic configuration using ansible | |
| for dotfile management. | |
| This must be run interactively and without sudo but it will ask for sudo | |
| escalation to upgrade the system. | |
| Ansible is responsible for all other customizations to the local system. | |
| -*- Tested on -*- | |
| - Ubuntu 18.04/WSL2 | |
| - Ubuntu 20.04/WSL2 | |
| - Ubuntu 20.04/Hyper-V | |
| - Ubuntu 22.04/WSL2 | |
| -*- Requirements -*- | |
| - Bash | |
| - Internet access | |
| - sudo access to the local system | |
| -*- Details -*- | |
| It installs the following packages: | |
| via apt: | |
| - (upgrade all to latest) | |
| - aptitude | |
| - python3-pip | |
| - git | |
| via git: | |
| - asdf --> ${HOME}/.asdf | |
| via asdf: | |
| - ansible | |
| Creates bootstraping cryptographic key pair: ${BOOTSTRAP_KEY_LOC} | |
| EOF | |
| if [[ $EUID -eq 0 ]]; then | |
| printf "This script must not be run as root\n" | |
| exit 1 | |
| fi | |
| ######################################## | |
| # Upgrade and install packages via apt | |
| ######################################## | |
| last_update=$(stat -c %Y /var/cache/apt/pkgcache.bin) | |
| now=$(date +%s) | |
| if [ $((now - last_update)) -gt 3600 ]; then | |
| sudo apt update | |
| else | |
| printf "INFO: Skipping repo update because it is fresh\n" | |
| fi | |
| sudo apt upgrade -y | |
| sudo apt install -y aptitude python3-pip python3-venv git | |
| ######################################## | |
| # Install asdf via git | |
| ######################################## | |
| if [ ! -e "${HOME}/.asdf" ]; then | |
| printf "\n\nINFO: Installing asdf in the user's environment\n" | |
| git clone https://github.com/asdf-vm/asdf.git ${HOME}/.asdf --branch v0.11.1 | |
| else | |
| printf "\n\nINFO: asdf directory already exists. Seems to be installed\n" | |
| fi | |
| printf "\n\nINFO: Activating asdf and updating\n" | |
| source "$HOME/.asdf/asdf.sh" | |
| asdf update | |
| asdf plugin-update --all | |
| ######################################## | |
| # Install ansible | |
| ######################################## | |
| printf "\n\nINFO: Installing ansible through asdf\n" | |
| if [ $( asdf plugin list | grep '^ansible-base$' | wc -l ) -eq 0 ]; then | |
| asdf plugin-add ansible-base https://github.com/amrox/asdf-pyapp.git | |
| fi | |
| asdf install ansible-base latest | |
| asdf global ansible-base latest | |
| ######################################## | |
| # Generate bootrapping key | |
| ######################################## | |
| if [ ! -f "$BOOTSTRAP_KEY_LOC" ]; then | |
| printf "INFO: No bootstrap key was found for this user. Generating....\n" | |
| ssh-keygen -t "$BOOTSTRAP_KEY_TYPE" -f "$BOOTSTRAP_KEY_LOC" -C "$BOOTSTRAP_KEY_COMMENT" -q -N "" | |
| fi | |
| if [ ! -f "${BOOTSTRAP_KEY_LOC}.pub" ]; then | |
| ssh-keygen -y -f "$BOOTSTRAP_KEY_LOC" > "${BOOTSTRAP_KEY_LOC}.pub" | |
| fi | |
| chmod go-rwx ${BOOTSTRAP_KEY_LOC} ${BOOTSTRAP_KEY_LOC}.pub | |
| cat <<EOF | |
| ---------------------------------------------- | |
| Make sure this key is published as read-only deploy key for github.com/${BOOTSTRAP_PB_REPO} | |
| DO NOT USE FOR ANYTHING ELSE!!!! | |
| ---------------------------------------------- | |
| EOF | |
| cat $BOOTSTRAP_KEY_LOC.pub | |
| printf "\n\nINFO: Attempting to connect to github.com (retries=100)\n" | |
| set +euf +o pipefail | |
| GIT_SSH_TEST=$(ssh -T -o IdentitiesOnly=yes -i ${BOOTSTRAP_KEY_LOC} -o ConnectionAttempts=100 git@github.com; echo $?) | |
| set -euf -o pipefail | |
| if [[ $GIT_SSH_TEST -eq 1 ]]; then | |
| echo "Success!!" | |
| else | |
| printf "Failed to authenticate with github.com\n" | |
| exit 1 | |
| fi | |
| printf "\n\nINFO: Initial bootstrap playbook run\n" | |
| ansible-pull \ | |
| --private-key "${BOOTSTRAP_KEY_LOC}" \ | |
| --ssh-common-args="-o IdentitiesOnly=yes" \ | |
| --url git@github.com:arielsalvo/bootstrap.git \ | |
| --checkout main \ | |
| -i hosts.yml \ | |
| bootstrap.yml | |
| printf "\n\nINFO: ALL DONE!\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment