Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Last active November 30, 2021 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomdavidson/b2348327f1015835fcfa58ae84a56f09 to your computer and use it in GitHub Desktop.
Save tomdavidson/b2348327f1015835fcfa58ae84a56f09 to your computer and use it in GitHub Desktop.
bootstrap script for setting up toms new computer
#!/usr/bin/env bash
# promts for sudo and can be installed via:
# wget -O - https://gist.githubusercontent.com/tomdavidson/64828803dbd4718514a465be1d7e908a/raw/75ec0521175b3d8b7d92c188d3dc0d251dbf5469/install-node-zx.sh | bash
! type sudo >/dev/null 2>&1 && apt install sudo && usermod -aG sudo "$(whoami)"
export CLOUDSDK_CORE_PROJECT='dots-333103'
# install NodeJS & zx if not already installed
function installNodeJS() {
# nodejs
if ! type -P node >/dev/null 2>&1; then
sudo apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -
sudo apt install -y nodejs
fi
# non-root npm global
npm config set prefix "$HOME/.local"
export NPM_PACKAGES="$HOME/.local"
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"
echo "node installed now check for zx"
! type -P zx >/dev/null 2>&1 && sudo npm install --global zx
}
function installgcloudCLI() {
type -P gcloud >/dev/null 2>&1 && echo 'gcloud cli allready installed' && return 0
sudo apt-get install apt-transport-https ca-certificates gnupg
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" |
tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg |
sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update
sudo apt-get install -y google-cloud-sdk
}
# create new secret
# gcloud --project 'dots-333103' secrets create 'default-ssh-key' --replication-policy='automatic'
#
# new secret version
# gcloud secrets create secret-id --data-file="/path/to/file.txt"
# gcloud --project dots-333103 secrets versions add default-ssh-key --data-file="$HOME/.ssh/id_rsa"
function getDefaultSSHkey() {
targetKey="${HOME}/.ssh/id_rsa"
fetchedKey="${targetKey}_fetched"
gcloud secrets versions access 'latest' --secret='default-ssh-key' >"$fetchedKey"
if [[ -f $HOME/.ssh/id_rsa ]]; then
if ! cmp -s "$fetchedKey" "$targetKey"; then
echo "ERROR: existing id_rsa, but it is different than fetched"
echo " fetched key is written to ${fetchedKey}"
echo " remove the existing key or update the secrect manager key."
exit 1
fi
else
mv "$fetchedKey" "$HOME/.ssh/id_rsa"
fi
chmod 600 "$HOME/.ssh/id_rsa"
}
function gitDots() {
echo "checking if get is installed"
! type -P git >/dev/null 2>&1 && sudo apt install -y git
if [ -d "$HOME/dots/.git" ]; then
echo "dots/.git exsist"
(
cd "$HOME/dots" || exit
git checkout main && git pull --rebase
)
else
echo " getting dots from github"
git clone git@github.com:tomdavidson/dots.git "$HOME/dots"
fi
}
function main() {
installgcloudCLI
gcloud auth login --activate --brief
getDefaultSSHkey
installNodeJS
echo "calling getDots()"
gitDots
}
if [[ -n $1 ]]; then
"$@"
else
main
(
cd "$HOME/dots" || exit
./install.sh
./configure.sh
)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment