#!/bin/bash set -e show_usage() { echo "usage: $0 [-h] [-d]" } show_help() { show_usage echo echo "Bootstrap a macOS instance with:" echo echo "* Xcode command line developer tools" echo "* Homebrew - package manager" echo "* Python - from Homebrew" echo "* virtualenv - from pip" echo "* SSH key - from ssh-keygen" echo "* GitHub public key - with SSH key" echo "* conduit - configuration manager" echo echo "If any already exist they will not be reinstalled." echo echo "optional arguments:" echo " -h show this help message and exit" echo " -d dry run, don't actuall install anything" } dry=0 while getopts 'hd' opt; do case $opt in h) show_help exit 0 ;; d) dry=1 ;; *) show_usage exit 1 ;; esac done if [ $dry -eq 1 ]; then echo "OPERATING IN DRY RUN MODE!" fi # Install Xcode command line developer tools if ! xcode-select --print-path &> /dev/null; then if [ $dry -eq 1 ]; then xcode-select --install fi echo "Installed: Xcode command line developer tools" else echo "Satisfied: Xcode command line developer tools" fi # Install Homebrew if ! brew help &> /dev/null; then if [ $dry -eq 0 ]; then /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" fi echo "Installed: Homebrew" else echo "Satisfied: Homebrew" fi # Install Python if [ ! -f /usr/local/bin/python ]; then if [ $dry -eq 0 ]; then brew install python fi echo "Installed: Python" else echo "Satisfied: Python" fi # Install virtualenv if [ ! -f /usr/local/bin/virtualenv ]; then if [ $dry -eq 0 ]; then pip install virtualenv &> /dev/null fi echo "Installed: virtualenv" else echo "Satisfied: virtualenv" fi # Generate SSH key if [ ! -f ~/.ssh/id_rsa ]; then read -rp "SSH email: " email mkdir ~/.ssh if [ $dry -eq 0 ]; then ssh-keygen -t rsa -b 4096 -C "$email" -N "" -f ~/.ssh/id_rsa fi echo "Generated: SSH key" else echo "Satisfied: SSH key" fi # GitHub public key env=$(mktemp -d) virtualenv $env &> /dev/null source $env/bin/activate pip install githubpy &> /dev/null python <(cat << EOF from __future__ import print_function from os import environ from os.path import join from getpass import getpass from github import GitHub def getinput(prompt): try: return raw_input(prompt) except NameError: return input(prompt) def main(): username = getinput('GitHub username: ') password = getpass('GitHub password: ') github = GitHub(username=username, password=password) keys = github.user.keys().get() id_rsa_pub_path = join(environ['HOME'], '.ssh', 'id_rsa.pub') with open(id_rsa_pub_path, 'r') as id_rsa_pub_file: local_key = id_rsa_pub_file.read() for key in keys: if local_key.startswith(key['key']): print('Satisfied: GitHub public key') exit(0) title = getinput('GitHub key name: ') github.user.keys().post(title=title, key=local_key) print('Installed: GitHub public key') exit(0) if __name__ == '__main__': try: main() except KeyboardInterrupt: exit(1) EOF ) deactivate rm -r $env # Prompt user to setup other SSH key recipients echo '----------------------------------------------------------------------' cat ~/.ssh/id_rsa.pub echo '----------------------------------------------------------------------' read -rp "Set other SSH key recipients, press ENTER to continue: " # Install conduit if ! python -c 'import conduit' &> /dev/null; then if [ $dry -eq 0 ]; then pip install git+ssh://git@github.com:kbenzie/conduit.git fi echo "Installed: conduit" else echo "Satisfied: conduit" fi echo "Completed: $0 will now be removed" if [ -f $0 ]; then rm $0; fi