Compare commits
2 Commits
6a3d187a7b
...
f7c0db1d00
Author | SHA1 | Date | |
---|---|---|---|
f7c0db1d00 | |||
d17909b455 |
151
bootstrap-Ubuntu.sh
Executable file
151
bootstrap-Ubuntu.sh
Executable file
@ -0,0 +1,151 @@
|
||||
#!/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 "* 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 "* 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 agree "GitHub public key"; then
|
||||
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(130)
|
||||
|
||||
EOF
|
||||
)
|
||||
deactivate
|
||||
rm -r $env
|
||||
fi
|
||||
|
||||
# TODO: Gogs SSH key
|
||||
|
||||
if [ -f ~/.ssh/id_rsa.pub ]; then
|
||||
echo "----------------------------------------------------------------------"
|
||||
cat ~/.ssh/id_rsa.pub
|
||||
echo "----------------------------------------------------------------------"
|
||||
read -rp "Set other SSH key recipients, press ENTER to continue: "
|
||||
fi
|
||||
|
||||
missing conduit && agree "Install conduit" && \
|
||||
pip_install git+ssh://git@github.com/kbenzie/conduit.git
|
||||
|
||||
echo "To use install pip packages update your PATH:"
|
||||
echo 'export PATH=~/.local/bin:$PATH'
|
||||
|
||||
[ -f $0 ] && agree "Remove $0" "N" && rm $0
|
146
bootstrap-Windows.cmd
Normal file
146
bootstrap-Windows.cmd
Normal file
@ -0,0 +1,146 @@
|
||||
@echo off
|
||||
rem = """
|
||||
|
||||
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 * 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
|
||||
goto github
|
||||
|
||||
:cleanup
|
||||
if errorlevel 1 exit /B 1
|
||||
:: These commands are exectued after the embedded python code below, this is
|
||||
:: required because commands following the embedded python code result in
|
||||
:: python syntax errors.
|
||||
rmdir /Q /S "%~dp0\bootstrap_env
|
||||
echo "Completed: %0 will now be removed"
|
||||
start /b "" cmd /c del "%~f0"&exit /b
|
||||
goto end
|
||||
|
||||
:: GitHub public key
|
||||
:github
|
||||
"C:\Python27\Scripts\virtualenv.exe" %~dp0\bootstrap_env
|
||||
if errorlevel 1 exit /B 1
|
||||
"%~dp0\bootstrap_env\Scripts\pip.exe" install githubpy
|
||||
if errorlevel 1 exit /B 1
|
||||
"%~dp0\bootstrap_env\Scripts\python.exe" -x "%~f0"
|
||||
goto cleanup """
|
||||
|
||||
from os import environ
|
||||
from os.path import join
|
||||
from getpass import getpass
|
||||
from sys import stdout
|
||||
from subprocess import call
|
||||
from github import GitHub
|
||||
|
||||
SEP = '----------------------------------------------------------------------'
|
||||
|
||||
|
||||
def getinput(prompt):
|
||||
try:
|
||||
return raw_input(prompt)
|
||||
except NameError:
|
||||
return input(prompt)
|
||||
|
||||
|
||||
def set_github_key():
|
||||
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['USERPROFILE'], '.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']):
|
||||
stdout.write('Satisfied: GitHub public key\n')
|
||||
return
|
||||
title = getinput('GitHub key name: ')
|
||||
github.user.keys().post(title=title, key=local_key)
|
||||
stdout.write('Installed: GitHub public key\n')
|
||||
|
||||
|
||||
def show_public_key():
|
||||
stdout.write('%s\n' % SEP)
|
||||
with open(join(environ['USERPROFILE'], '.ssh', 'id_rsa.pub')) as key:
|
||||
stdout.write('%s\n' % key.read())
|
||||
stdout.write('%s\n' % SEP)
|
||||
getinput('Set other SSH key recipients, press ENTER to continue: ')
|
||||
|
||||
|
||||
def install_conduit():
|
||||
path = environ['PATH'].split(';')
|
||||
path.append(r'C:\tools\cmder\vendor\git-for-windows\bin')
|
||||
environ['PATH'] = ';'.join(path)
|
||||
call([r'C:\Python27\Scripts\pip.exe', 'install',
|
||||
'git+ssh://git@github.com:kbenzie/conduit.git'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
set_github_key()
|
||||
show_public_key()
|
||||
install_conduit()
|
||||
except KeyboardInterrupt:
|
||||
exit(1)
|
||||
|
||||
rem = """
|
||||
:end """
|
Loading…
x
Reference in New Issue
Block a user