From 21266c13929d2691a5e8a6bf0b5d264186595832 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Fri, 1 Sep 2017 00:12:14 +0100 Subject: [PATCH] Add bootstrap-Ubuntu.sh usage: ./bootstrap-Ubuntu.sh [-h] [-d] Bootstrap a Ubuntu or Linux Mint instance with: * git - from apt-get * virtualenv - from pip * SSH key - from ssh-keygen * GitHub public key - with SSH key * conduit - configuration manager If any already exist they will not be reinstalled. optional arguments: -h show this help message and exit -d dry run, don't actuall install anything -n don't install pip packeges as --user --- bootstrap-Ubuntu.sh | 158 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 bootstrap-Ubuntu.sh diff --git a/bootstrap-Ubuntu.sh b/bootstrap-Ubuntu.sh new file mode 100755 index 0000000..adba7e6 --- /dev/null +++ b/bootstrap-Ubuntu.sh @@ -0,0 +1,158 @@ +#!/bin/bash + +set -e + +show_usage() { + echo "usage: $0 [-h] [-d]" +} + +show_help() { + show_usage + echo + echo "Bootstrap a Ubuntu or Linux Mint instance with:" + echo + echo "* Git - from apt-get" + 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" + echo " -n don't install pip packeges as --user" +} + +dry=0 +user='--user' +sudo='' + +while getopts 'hd' opt; do + case $opt in + h) + show_help + exit 0 + ;; + d) + dry=1 + ;; + u) + user='' + sudo='sudo' + ;; + *) + show_usage + exit 1 + ;; + esac +done + +if [ $dry -eq 1 ]; then + echo "OPERATING IN DRY RUN MODE!" +fi + +if [ "" != "$user" ]; then + export PATH=~/.local/bin:$PATH +fi + +# Install Git +if [ ! -f /usr/bin/git ]; then + if [ $dry -eq 0 ]; then + sudo apt-get install git + fi + echo "Installed: Git" +else + echo "Satisfied: Git" +fi + +# Install virtualenv +if [ ! -f ~/.local/bin/virtualenv ]; then + if [ $dry -eq 0 ]; then + $sudo pip install $user virtualenv &> /dev/null + echo "Installed: virtualenv" + fi +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 + $sudo pip install $user 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