Skip to content

Instantly share code, notes, and snippets.

@gbarboza
Last active December 14, 2015 00:49
Show Gist options
  • Select an option

  • Save gbarboza/5001614 to your computer and use it in GitHub Desktop.

Select an option

Save gbarboza/5001614 to your computer and use it in GitHub Desktop.
home-setup
#!/usr/bin/env bash
### Begin configuration
HOST=$(hostname -s)
SSH_CONFIG=~/.ssh/config
KEY_SIZE=2048
BB_KEY=~/.ssh/keys/bitbucket
GH_KEY=~/.ssh/keys/github
### End configuration
send_github_key() {
local resp=$(curl --write-out %{http_code} \
--silent --output /dev/null \
-u "gbarboza" -X POST -H "X-GitHub-OTP: $1" \
-d "{ \"title\": \"$HOST\", \"key\": \"$(cat $GH_KEY.pub)\" }" \
https://api.github.com/user/keys)
if [[ $resp -eq 201 ]]; then
return 0
else
return 1
fi
}
send_bitbucket_key() {
local resp=$(curl --write-out %{http_code} --silent --output /dev/null \
-u "gbarboza" -X POST --data-urlencode label="$HOST" \
--data-urlencode key="$(cat $BB_KEY.pub)" \
https://bitbucket.org/api/1.0/users/gbarboza/ssh-keys)
if [[ $resp -eq 200 ]]; then
return 0
else
return 1
fi
}
github_key() {
echo "Doing github key"
while [[ ! -e $GH_KEY ]]; do
ssh-keygen -t rsa -b $KEY_SIZE -f $GH_KEY
done
echo -n "Enter 2FA: "
read tfa
send_github_key $tfa
while [[ $? -ne 0 ]]; do
echo "Invalid credentials, retrying"
send_github_key $tfa
done
if ! grep -q "github.com" $SSH_CONFIG; then
echo -e "Host github.com" > ~/.ssh/config
echo -e "\tHostname github.com" >> ~/.ssh/config
echo -e "\tIdentityFile ~/.ssh/keys/github" >> ~/.ssh/config
fi
echo "GH Key Done";
}
bitbucket_key() {
echo "Doing bitbucket key"
while [[ ! -e $BB_KEY ]]; do
ssh-keygen -t rsa -b $KEY_SIZE -f $BB_KEY
done
send_bitbucket_key
while [[ $? -ne 0 ]]; do
echo "Invalid credentials, retrying"
send_bitbucket_key
done
if ! grep -q "bitbucket.org" $SSH_CONFIG; then
echo -e "Host bitbucket.org" >> ~/.ssh/config
echo -e "\tHostname bitbucket.org" >> ~/.ssh/config
echo -e "\tIdentityFile ~/.ssh/keys/bitbucket" >> ~/.ssh/config
fi
echo "BB Key Done";
}
setup_home() {
rm -rf .home
cd ~
git clone git@bitbucket.org:gbarboza/home.git
cd ~/home
./setup.sh
echo "Done";
}
if [[ ! -e ~/.ssh/keys ]]; then
mkdir -p ~/.ssh/keys
fi
if [[ ! -e ~/.ssh/config ]]; then
touch ~/.ssh/config
fi
main() {
github_key
echo "Doing bitbucket key"
bitbucket_key
echo -n "Going to setup home now. Continue? [y/N] "
read ans
if [[ $ans == "y" ]]; then
setup_home
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment