Compare commits

...

4 Commits

Author SHA1 Message Date
a8b9e6bfe2 Add bootstrap-Windows.cmd
usage: bootstrap-Windows.cmd [/?]

Bootstrap a macOS instance with:

* Windows SDK - from MSDN
* Chocolatey - package manager
* Git - from Chocolatey
* Python - from Chocolatey
* virtualenv - from pip
* SSH key - from ssh-keygen
* GitHub public key - with SSH key
* GitLab public key - with SSH key
* BitBucket Cloud public key - with SSH key
* Gogs Cloud public key - with SSH key
* conduit - configuration manager

optional arguments:
        /?              show this help message and exit
2018-01-04 18:41:28 +00:00
d8c63058c3 Add bootstrap-Debian.sh
usage: ./bootstrap-Debian.sh [-h] [-y]

Bootstrap a Debian based distribution with:

* update apt cache
* upgrade apt packages
* git - from apt
* python - from apt
* python-pip - from apt
* virtualenv - from pip
* SSH key - from ssh-keygen
* GitHub public key - with SSH key
* GitLab public key - with SSH key
* BitBucket Cloud public key - with SSH key
* Gogs Cloud 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
        -y              assume yes when prompted
2018-01-04 18:40:49 +00:00
8c11e15a6c Add bootstrap-macOS.sh
usage: bootstrap-macOS.sh [-h] [-y]

Bootstrap a macOS instance with:

* Xcode command line developer tools
* Homebrew - package manager
* python - from Homebrew
* virtualenv - from pip
* SSH key - from ssh-keygen
* GitHub public key - with SSH key
* GitLab public key - with SSH key
* BitBucket Cloud public key - with SSH key
* Gogs Cloud 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
        -y              assume yes when prompted
2018-01-04 18:39:05 +00:00
e158eb4d62 Add python package to setup SSH keys
Supports setting SSH keys for the following services:

* GitHub
* GitLab
* BitBucket Cloud
* Gogs
2018-01-04 18:39:05 +00:00
5 changed files with 487 additions and 0 deletions

105
bootstrap-Debian.sh Executable file
View File

@ -0,0 +1,105 @@
#!/bin/bash
set -e
show_usage() {
echo "usage: $0 [-h] [-y]"
}
show_help() {
show_usage
echo
echo "Bootstrap a Debian based distribution with:"
echo
echo "* update apt cache"
echo "* upgrade apt packages"
echo "* git - from apt"
echo "* python - from apt"
echo "* python-pip - from apt"
echo "* virtualenv - from pip"
echo "* SSH key - from ssh-keygen"
echo "* GitHub public key - with SSH key"
echo "* GitLab public key - with SSH key"
echo "* BitBucket Cloud public key - with SSH key"
echo "* Gogs Cloud 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 " -y assume yes when prompted"
}
yes=0
while getopts 'hy' opt; do
case $opt in
h) show_help; exit 0 ;;
y) yes=1 ;;
*) show_usage; exit 1 ;;
esac
done
missing() {
which $1 &> /dev/null && return 1 || return 0
}
agree() {
local check=^[Nn]$
[ $yes -eq 1 ] && [[ ! "$2" =~ $check ]] && return 0
[[ "$2" =~ $check ]] && local default="[y/N]" || local default="[Y/n]"
read -p "$1 $default? " answer
case "$answer" in
y|Y|yes) return 0 ;;
n|N|no) return 1 ;;
'') [[ "$2" =~ $check ]] && return 1 || return 0 ;;
*) echo "invalid input: $answer" && return `agree "$1"` ;;
esac
}
apt_install() {
sudo apt-get install --yes --install-recommends $1 > /dev/null
}
pip_install() {
pip install --user $1 > /dev/null
}
export PATH=~/.local/bin:$PATH
agree "Update apt cache" && sudo apt-get update > /dev/null
agree "Upgrade apt packages" "N" && sudo apt-get upgrade > /dev/null
missing git && agree "Install git" && apt_install git
if missing pip; then
agree "Install python-pip" && apt_install python-pip
agree "Upgrade pip with pip" && sudo -H pip install --upgrade pip > /dev/null
fi
missing virtualenv && agree "Install virtualenv" && pip_install virtualenv
if [ ! -f ~/.ssh/id_rsa ] && agree "Generate SSH key"; then
read -rp "SSH email: " email
[ ! -d ~/.ssh ] && mkdir -p ~/.ssh
ssh-keygen -t rsa -b 4096 -C "$email" -N "" -f ~/.ssh/id_rsa
fi
if ! missing virtualenv && agree "Set SSH keys on remote Git servers"; then
env=$(mktemp -d)
virtualenv $env > /dev/null
source $env/bin/activate
pip install git+https://code.infektor.net/config/bootstrap.git > /dev/null
python -c 'import bootstrap; bootstrap.set_ssh_keys()'
deactivate
rm -r $env
fi
missing conduit && agree "Install conduit" && \
pip_install git+ssh://git@github.com/kbenzie/conduit.git
echo "To use installed pip packages update your PATH:"
echo 'export PATH=~/.local/bin:$PATH'
[ -f $0 ] && agree "Remove $0" "N" && rm $0 || exit 0

80
bootstrap-Windows.cmd Normal file
View File

@ -0,0 +1,80 @@
@echo off
if [%1]==[/?] goto :help
echo %* | find "/?" > nul
if errorlevel 1 goto :main
:help
echo usage: %0 [/?]
echo
echo Bootstrap a macOS instance with:
echo
echo * Windows SDK - from MSDN
echo * Chocolatey - package manager
echo * Git - from Chocolatey
echo * Python - from Chocolatey
echo * virtualenv - from pip
echo * SSH key - from ssh-keygen
echo * GitHub public key - with SSH key
echo * GitLab public key - with SSH key
echo * BitBucket Cloud public key - with SSH key
echo * Gogs Cloud public key - with SSH key
echo * conduit - configuration manager
echo
echo optional arguments:
echo /? show this help message and exit
goto end
:main
:: Check for admin permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if errorlevel 1 (
echo Administrator privilages required!
exit /B 1
)
:: Install Windows SDK
bitsadmin.exe /transfer "Download Windows SDK" https://go.microsoft.com/fwlink/p/?linkid=845298 %~dp0\winsdksetup.exe
if errorlevel 1 exit /B 1
echo Installing: Windows SDK
%~dp0\winsdksetup.exe /features + /q
if errorlevel 1 exit /B 1
del %~dp0\winsdksetup.exe
echo Installed: Windows SDK
:: Install Chocolatey
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
if errorlevel 1 exit /B 1
echo Installed: Chocolatey
:: Install Git
choco install cmder --yes
if errorlevel 1 exit /B 1
:: Install Python
choco install python2 --yes
if errorlevel 1 exit /B 1
:: Install virtualenv
"C:\Python27\Scripts\pip.exe" install virtualenv
if errorlevel 1 exit /B 1
:: Generate SSH key
mkdir %USERPROFILE%\.ssh
set /P email="SSH email: "
"C:\tools\cmder\vendor\git-for-windows\usr\bin\ssh-keygen.exe" -t rsa -b 4096 -C "%email%" -N "" -f %USERPROFILE%\.ssh\id_rsa
if errorlevel 1 exit /B 1
:: Set SSH keys on remote Git servers
"C:\Python27\Scripts\virtualenv.exe" %~dp0\bootstrap_env
if errorlevel 1 exit /B 1
"%~dp0\bootstrap_env\Scripts\pip.exe" install git+https://code.infektor.net/config/bootstrap.git
if errorlevel 1 exit /B 1
"%~dp0\bootstrap_env\Scripts\python.exe" -c "import bootstrap; bootstrap.set_ssh_keys()"
if errorlevel 1 exit /B 1
rmdir /Q /S "%~dp0\bootstrap_env"
echo "Completed: %0 will now be removed"
start /b "" cmd /c del "%~f0"&exit /b
:end

110
bootstrap-macOS.sh Executable file
View File

@ -0,0 +1,110 @@
#!/bin/bash
set -e
show_usage() {
echo "usage: $0 [-h] [-y]"
}
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 "* GitLab public key - with SSH key"
echo "* BitBucket Cloud public key - with SSH key"
echo "* Gogs Cloud 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 " -y assume yes when prompted"
}
yes=0
while getopts 'hy' opt; do
case $opt in
h) show_help; exit 0 ;;
y) yes=1 ;;
*) show_usage; exit 1 ;;
esac
done
missing() {
which $1 &> /dev/null && return 1 || return 0
}
agree() {
local check=^[Nn]$
[ $yes -eq 1 ] && [[ ! "$2" =~ $check ]] && return 0
[[ "$2" =~ $check ]] && local default="[y/N]" || local default="[Y/n]"
read -p "$1 $default? " answer
case "$answer" in
y|Y|yes) return 0 ;;
n|N|no) return 1 ;;
'') [[ "$2" =~ $check ]] && return 1 || return 0 ;;
*) echo "invalid input: $answer" && return `agree "$1"` ;;
esac
}
brew_install() {
brew install $1 > /dev/null
}
pip_install() {
pip install --user $1 > /dev/null
}
export PATH=~/.local/bin:$PATH
export PYTHONUSERBASE=~/.local
if ! xcode-select --print-path &> /dev/null; then
agree "Install Xcode command line developer tools" && xcode-select --install
fi
missing brew && agree "Intalll Homebrew" && /usr/bin/ruby -e \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
agree "Update Homebrew packages" && brew update > /dev/null
if missing /usr/local/bin/python2 && agree "Install Homebrew python"; then
brew_install python
export PATH=/usr/local/opt/python/libexec/bin:$PATH
echo "To use Homebrew python update your PATH:"
echo 'export PATH=/usr/local/opt/python/libexec/bin:$PATH'
fi
missing virtualenv && agree "Install virtualenv" && pip_install virtualenv
if [ ! -f ~/.ssh/id_rsa ] && agree "Generate SSH key"; then
read -rp "SSH email: " email
[ ! -d ~/.ssh ] && mkdir -p ~/.ssh
ssh-keygen -t rsa -b 4096 -C "$email" -N "" -f ~/.ssh/id_rsa
fi
if ! missing virtualenv && agree "Set SSH keys on remote Git servers"; then
env=$(mktemp -d)
virtualenv $env &> /dev/null
source $env/bin/activate
pip install git+https://code.infektor.net/config/bootstrap.git > /dev/null
python -c 'import bootstrap; bootstrap.set_ssh_keys()'
deactivate
rm -r $env
fi
! missing pip && missing conduit && agree "Install conduit" && \
pip_install git+ssh://git@github.com/kbenzie/conduit.git
echo "To use installed pip packages update your PATH and PYTHONUSERBASE:"
echo 'export PATH=~/.local/bin:$PATH && export PYTHONUSERBASE=~/.local'
[ -f $0 ] && agree "Remove $0" "N" && rm $0 || exit 0

181
bootstrap/__init__.py Normal file
View File

@ -0,0 +1,181 @@
"""Interactively set SSH keys on remote Git servers."""
from __future__ import print_function
from getpass import getpass
from os import environ
from os.path import join
from platform import node
from requests import ConnectionError, get, post
from requests.auth import HTTPBasicAuth
from requests.compat import urlparse
try:
input = raw_input
except NameError:
pass
class BootstrapError(Exception):
"""Bootstrap Exception."""
pass
def bootstrap_error(response):
"""Create a BootstrapError from a Response."""
return BootstrapError('%s %s' % (response.status_code, response.reason))
def agree(question, default='Y'):
"""Prompt user to answer a yes/no question."""
valid = {
'y': True,
'Y': True,
'yes': True,
'n': False,
'N': False,
'no': False,
'': {'Y': True,
'N': False}[default]
}
answer = input('%s [%s]? ' % (question, {'Y': 'Y/n', 'N': 'y/N'}[default]))
try:
return valid[answer]
except KeyError:
print('invalid input: %s' % answer)
return agree(question)
def get_url(service, default):
"""Get URL."""
url = input('%s URL%s: ' % (service, ' (%s)' % default if default else ''))
if url == '':
if default:
return default
else:
print('invalid input: %s' % url)
return get_url(service, default)
return url
def get_username_password(service):
"""Get username/password."""
username = input('%s username: ' % service)
password = getpass('%s password: ' % service)
return (username, password)
def get_local_key():
"""Get local SSH key."""
with open(join(environ['HOME'], '.ssh', 'id_rsa.pub'), 'r') as key_file:
return key_file.read().rstrip()
def key_exists(keys, local_key):
"""Check if local SSH key is already set."""
for key in keys:
if local_key.startswith(key['key']):
return True
return False
def set_github_ssh_key():
"""Set GitHub SSH key."""
url = urlparse(get_url('GitHub', 'https://github.com'))
keys_url = '%s://api.%s/user/keys' % (url.scheme, url.netloc)
username, password = get_username_password('GitHub')
auth = HTTPBasicAuth(username, password)
response = get(keys_url, auth=auth)
if response.status_code != 200:
raise bootstrap_error(response)
keys = response.json()
local_key = get_local_key()
if not key_exists(keys, local_key):
response = post(
keys_url, auth=auth, json={'title': node(),
'key': local_key})
if response.status_code != 201:
raise bootstrap_error(response)
def set_gitlab_ssh_key():
"""Set GitLab SSH key."""
api_url = '%s/api/v4' % get_url('GitLab', 'https://gitlab.com')
session_url = '%s/session' % api_url
keys_url = '%s/user/keys' % api_url
username, password = get_username_password('GitLab')
response = post(session_url, {'login': username, 'password': password})
if response.status_code != 201:
raise bootstrap_error(response)
auth = {'Private-Token': response.json()['private_token']}
response = get(keys_url, headers=auth)
if response.status_code != 200:
raise bootstrap_error(response)
keys = response.json()
local_key = get_local_key()
if not key_exists(keys, local_key):
response = post(
keys_url, headers=auth, json={'title': node(),
'key': local_key})
if response.status_code != 201:
raise bootstrap_error(response)
def set_bitbucket_cloud_ssh_key():
"""Set BitBucket Cloud SSH key."""
api_url = 'https://api.bitbucket.org/1.0'
username, password = get_username_password('BitBucket Cloud')
auth = HTTPBasicAuth(username, password)
keys_url = '%s/users/%s/ssh-keys' % (api_url, username)
response = get(keys_url, auth=auth)
if response.status_code != 200:
raise bootstrap_error(response)
keys = response.json()
local_key = get_local_key()
if not key_exists(keys, local_key):
response = post(
keys_url, auth=auth, data={'label': node(),
'key': local_key})
if response.status_code != 200:
raise bootstrap_error(response)
def set_gogs_ssh_key():
"""Set Gogs SSH key."""
keys_url = '%s/api/v1/user/keys' % get_url('Gogs', None)
username, password = get_username_password('Gogs')
auth = HTTPBasicAuth(username, password)
response = get(keys_url, auth=auth)
if response.status_code != 200:
raise bootstrap_error(response)
keys = response.json()
local_key = get_local_key()
if not key_exists(keys, local_key):
response = post(
keys_url, auth=auth, json={'title': node(),
'key': local_key})
if response.status_code != 201:
raise bootstrap_error(response)
def set_ssh_keys():
"""Interactively set SSH keys on remote Git servers."""
try:
for service in ['GitHub', 'GitLab', 'BitBucket Cloud', 'Gogs']:
question = 'Set %s SSH key' % service
default = 'Y'
while agree(question, default):
try:
{
'GitHub': set_github_ssh_key,
'GitLab': set_gitlab_ssh_key,
'BitBucket Cloud': set_bitbucket_cloud_ssh_key,
'Gogs': set_gogs_ssh_key,
}[service]()
if service == 'BitBucket Cloud':
break
question = 'Set another %s SSH key' % service
default = 'N'
except (BootstrapError, ConnectionError) as error:
print('error: %s' % error.message)
except KeyboardInterrupt:
exit(130)

11
setup.py Normal file
View File

@ -0,0 +1,11 @@
"""Setup bootstrap package."""
from setuptools import find_packages, setup
setup(
name='bootstrap',
version='0.1.0',
packages=find_packages(),
install_requires=[
'requests',
])