-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbootstrap.sh
More file actions
216 lines (182 loc) · 5.81 KB
/
bootstrap.sh
File metadata and controls
216 lines (182 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env bash
################################################################################
# Install homebrew and git if they dont' exist
################################################################################
function install_package_managers(){
#install or update homebrew
which -s brew
if [[ $? != 0 ]] ; then
# Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
else
brew update
fi
#install or update git
if brew list -1 | grep -q "^git\$"; then
brew upgrade git
else
brew install git
fi
}
################################################################################
# Create an SSH key for GitHub
################################################################################
function create_github_ssh(){
#Check if github SSH needs to be setup
pbcopy < ~/.ssh/id_rsa.pub
if [ $? -eq 1 ]
then
#use an empty paraphrase to silently create an key pair
echo -e 'y\n'|ssh-keygen -q -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
pbcopy < ~/.ssh/id_rsa.pub
# directions of what to do on GitHub.com
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
echo "Your public key has been copied to the clipboard. Next a browser will navigate"
echo "to your github SSH Key Settings page. There may be a login gate first."
echo "Create a 'New SSH key' with the contents of the clipboard when you get there."
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
read -n 1 -s -p "Press any key to open GitHub SSH Key Settings"
echo ""
echo ""
open "https://github.com/settings/keys"
echo ""
echo ""
read -n 1 -s -p "SSH Key created on GitHub? Press any key to continue"
fi
}
################################################################################
# Clone a git hub repo to a clean location following the tree structure of #
#
# $HOME
# -- Code
# -- [github org/user name]
# -- [github repo name]
################################################################################
function clone_repo(){
local codeDir=$1
local gitName=$2
#clear existing work and re-create folder structure
rm -rf "$codeDir/$gitName"
mkdir -p "$codeDir/$gitName"
#get a fresh copy
git clone git@github.com:${gitName}.git "$codeDir/$gitName"
#return the location of the repo
echo "$codeDir/$gitName"
}
################################################################################
# Execute .sh files within a directory #
################################################################################
function source_files(){
for file in "${1}"/*.sh; do
echo "Sourcing ${file}"
source $file
done;
unset file;
}
################################################################################
# Remove roadblocks for unattended execution #
################################################################################
function silence_sudo_approval(){
# Close any open System Preferences panes, to prevent them from overriding
# settings we’re about to change
osascript -e 'tell application "System Preferences" to quit'
# Ask for the administrator password upfront
sudo -v
# Keep-alive: update existing `sudo` time stamp until `faster_osx.sh` has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Set standby delay to 24 hours (default is 1 hour)
sudo pmset -a standbydelay 86400
}
################################################################################
# Bootstrap OSX with sane defaults for software development #
################################################################################
function bootstrap(){
local doPkgs=$1
local doPrefs=$2
local extras=$3
silence_sudo_approval
install_package_managers
create_github_ssh
local codeDir="$HOME/Code"
mkdir -p $codeDir
local gitDir=$(clone_repo $codeDir "PelotonTechIO/OSXBootstrap")
if $doPkgs ; then
source_files "${gitDir}/packages"
else
echo "No packages being installed."
fi;
if $doPrefs ; then
source_files "${gitDir}/preferences"
else
echo "No preferences being installed."
fi;
IFS=',' read -r -a array <<< "$extras"
for repo in "${array[@]}"
do
source_files $(clone_repo $codeDir $repo)
done
}
################################################################################
# Parse command line and set exectuion flags
################################################################################
force=false
prefs=true
pkgs=true
while [[ ${1} ]]; do
case "${1}" in
--force)
force=true
shift
;;
--f)
force=true
shift
;;
--noprefs)
prefs=false
shift
;;
--nopkgs)
pkgs=false
shift
;;
--extras)
extras=${2}
shift
if ! shift; then
echo 'Missing extras parameter argument.' >&2
exit 0
fi
;;
*)
echo "Unknown parameter: ${1}" >&2
exit 0
esac
done
################################################################################
# Inovke Execution #
################################################################################
#execution changes the path, so store the starting point to go back at the end
startingPWD=$pwd
if [ $force == false ]; then
read -p "This will overwrite some of your OSX preferences. Are you sure? (y/n) " -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
bootstrap $pkgs $prefs $extras;
fi;
else
bootstrap $pkgs $prefs $extras;
fi;
#return to the starting path
cd $startingPWD
unset bootstrap;
unset prefs;
unset pkgs;
unset extras;
unset force;