forked from csilvers/khan-dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac-setup.sh
executable file
·358 lines (321 loc) · 11.2 KB
/
mac-setup.sh
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/sh
# Bail on any errors
set -e
tty_bold=`tput bold`
tty_normal=`tput sgr0`
# for printing standard echoish messages
notice () {
printf " $1\n"
}
# for printing logging messages that *may* be replaced by
# a success/warn/error message
info () {
printf " [ \033[00;34m..\033[0m ] $1"
}
# for printing prompts that expect user input and will be
# replaced by a success/warn/error message
user () {
printf "\r [ \033[0;33m??\033[0m ] $1 "
}
# for replacing previous input prompts with success messages
success () {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] $1\n"
}
# for replacing previous input prompts with warnings
warn () {
printf "\r\033[2K [\033[0;33mWARN\033[0m] $1\n"
}
# for replacing previous prompts with errors
error () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
}
update_path() {
# We need /usr/local/bin to come before /usr/bin on the path, to
# pick up brew files we install. To do this, we just source
# .profile.khan, which does this for us (and the new user).
# (This assumes you're running mac-setup.sh from the khan-dotfiles
# directory.)
. .profile.khan
}
maybe_generate_ssh_keys () {
# Create a public key if need be.
info "Checking for ssh keys"
mkdir -p ~/.ssh
if [ -e ~/.ssh/id_[rd]sa ]
then
success "Found existing ssh keys"
else
ssh-keygen -q -N "" -t rsa -f ~/.ssh/id_rsa
success "Generated an rsa ssh key at ~/.ssh/id_rsa"
fi
return 0
}
copy_ssh_key () {
if [ -e ~/.ssh/id_rsa ]
then
pbcopy < ~/.ssh/id_rsa.pub
elif [ -e ~/.ssh/id_dsa ]
then
pbcopy < ~/.ssh/id_dsa.pub
else
error "no ssh public keys found"
exit
fi
}
register_ssh_keys() {
success "Registering your ssh keys with github\n"
verify_ssh_auth
}
# checks to see that ssh keys are registered with github
# $1: "true"|"false" to end the auth cycle
verify_ssh_auth () {
ssh_host="[email protected]"
webpage_url="https://github.com/settings/ssh"
instruction="Click 'Add SSH Key', paste into the box, and hit 'Add key'"
info "Checking for GitHub ssh auth"
if ! ssh -T -v $ssh_host 2>&1 >/dev/null | grep \
-q -e "Authentication succeeded (publickey)"
then
if [ "$2" == "false" ] # error if auth fails twice in a row
then
error "Still no luck with GitHub ssh auth. Ask a dev!"
ssh_auth_loop $webpage_url "false"
else
# otherwise prompt to upload keys
success "GitHub's ssh auth didn't seem to work\n"
notice "Let's add your public key to GitHub"
info "${tty_bold}${instruction}${tty_normal}\n"
ssh_auth_loop $webpage_url "true"
fi
else
success "GitHub ssh auth succeeded!"
fi
}
ssh_auth_loop() {
# a convenience function which lets you copy your public key to your clipboard
# open the webpage for the site you're pasting the key into or just bailing
# $1 = ssh key registration url
service_url=$1
first_run=$2
if [ "$first_run" == "true" ]
then
notice "1. hit ${tty_bold}o${tty_normal} to open GitHub on the web"
notice "2. hit ${tty_bold}c${tty_normal} to copy your public key to your clipboard"
notice "3. hit ${tty_bold}t${tty_normal} to test ssh auth for GitHub"
notice "☢. hit ${tty_bold}s${tty_normal} to skip ssh setup for GitHub"
ssh_auth_loop $1 "false"
else
user "o|c|t|s) "
read -n1 ssh_option
case $ssh_option in
o|O )
success "opening GitHub's webpage to register your key!"
open $service_url
ssh_auth_loop $service_url "false"
;;
c|C )
success "copying your ssh key to your clipboard"
copy_ssh_key
ssh_auth_loop $service_url "false"
;;
t|T )
printf "\r"
verify_ssh_auth "false"
;;
s|S )
warn "skipping GitHub ssh registration"
;;
esac
fi
}
install_gcc() {
info "\nChecking for apple command line developer tools..."
if ! gcc --version >/dev/null 2>&1 || [ ! -s /usr/include/stdio.h ]; then
if sw_vers -productVersion | grep -e '^10\.[0-8]$' -e '^10\.[0-8]\.'; then
warn "Command line tools are *probably available* for your Mac's OS, but..."
info "why not upgrade your OS right now?\n"
info "Otherwise, you can always visit developer.apple.com and grab 'em there.\n"
exit 1
else
success "Installing command line developer tools"
# Also, how did you get this dotfiles repo in 10.9 without
# git auto-triggering the command line tools install process??
xcode-select --install
warn "The dotfile setup is stopping now."
warn "When the install finishes, rerun ${tty_bold}make${tty_normal} to continue. (sorry)"
exit 1
fi
# If this doesn't work for you, you can find the most recent
# version here: https://developer.apple.com/downloads
else
success "Great, found gcc! (assuming we also have other recent devtools)"
fi
}
install_slack() {
info "Checking for Slack..."
if ! open -R -g -a Slack > /dev/null; then
success "Didn't find Slack."
info "Installing Slack to ~/Applications\n"
brew cask install slack
else
success "Great! Slack already installed!"
fi
}
install_homebrew() {
info "Checking for mac homebrew"
# If homebrew is already installed, don't do it again.
if ! brew --help >/dev/null 2>&1; then
success "Brew not found. Installing!"
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
success "Great! Mac homebrew already installed!"
info "Verifying homebrew is in a good state...\n"
if ! brew doctor; then
warn "Oh no! 'brew doctor' reported some warnings."
info "These warnings may cause you trouble, but they are likely harmless.\n"
read -r -p "Onward? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
exit 1
;;
esac
fi
fi
success "Updating (but not upgrading) Homebrew"
brew update > /dev/null
# Install homebrew-cask, so we can use it manage installing binary/GUI apps
brew tap caskroom/cask
# Make sure everything is ok. We don't care if we're using an
# obsolete gcc, so instead of looking at the exit code for 'brew
# doctor', we look at its output. The last 'grep', combined with
# the ! at the beginning of this command, causes the overall
# command to fail -- and thus the script to exit -- if brew doctor
# has any errors or warnings after we grep out the stuff we don't
# care about.
## Commented out for now: too many legit setups have warnings (cf chris).
## ! brew doctor 2>&1 \
## | grep -v -e 'A newer Command Line Tools' \
## | grep -v -e 'Your Homebrew is not installed to /usr/local' \
## | grep -C1000 -e ^Error -e ^Warning
}
update_git() {
if ! git --version | grep -q -e 'version 1.[89]' \
-e 'version 2'; then
echo "Installing an updated version of git using Homebrew"
echo "Current version is `git --version`"
if brew ls git >/dev/null 2>&1; then
# If git is already installed via brew, update it
brew upgrade git || true
else
# Otherwise, install via brew
brew install git || true
fi
# Check git version again
if ! git --version | grep -q -e 'version 1.[89]' \
-e 'version 2'; then
echo "Error installing git via brew; download and install manually via http://git-scm.com/download/mac. "
read -p "Press enter to continue..."
fi
fi
# Some code, such as tools/diagnose_js_packages.py, uses pcre-grep.
# (Search for `perl_regexp` in webapp to see a complete list.)
if ! git grep -P -l . >/dev/null; then
echo "Updating git to be able to use PCRE."
brew reinstall --with-pcre git
fi
}
install_node() {
if ! brew ls node >/dev/null 2>&1; then
brew install node 2>&1
fi
}
install_phantomjs() {
info "Checking for phantomjs\n"
if ! type phantomjs >/dev/null 2>&1 || ! expr `phantomjs --version` : 2 >/dev/null; then
brew uninstall --force phantomjs
brew install phantomjs
else
success "phantomjs 2.x already installed"
fi
}
install_nginx() {
info "Checking for nginx\n"
if ! type nginx >/dev/null 2>&1; then
info "Installing nginx\n"
brew install nginx
else
success "nginx already installed"
fi
}
install_image_utils() {
info "Checking for imagemagick\n"
if ! brew ls imagemagick >/dev/null 2>&1; then
info "Installing imagemagick\n"
brew install imagemagick
else
success "imagemagick already installed"
fi
}
install_helpful_tools() {
# This installs gtimeout, among a ton of other tools, which we use
# some in our deploy pipeline.
if ! brew ls coreutils >/dev/null 2>&1; then
info "Installing coreutils\n"
brew install coreutils
else
success "coreutils already installed"
fi
}
install_java() {
# We use java for our google cloud dataflow jobs that live in webapp
# (as well as in khan-linter for linting those jobs)
brew cask install java
}
install_protoc() {
# We use protocol buffers in webapp's event log stream infrastructure. This
# installs the protocol buffer compiler (which generates python & java code
# from the protocol buffer definitions), as well as a go-based compiler
# plugin that allows us to generate bigquery schemas as well.
if ! brew ls protobuf >/dev/null 2>&1; then
info "Installing protoc\n"
brew install protobuf
else
success "protoc already installed"
fi
if ! brew ls go >/dev/null 2>&1; then
info "Installing go\n"
brew install go
else
success "go already installed"
fi
go get github.com/GoogleCloudPlatform/protoc-gen-bq-schema
}
echo "\n"
success "Running Khan Installation Script 1.1\n"
warn "Warning: This is only tested up to macOS 10.12 (Sierra).\n"
notice "If you find that this works on a newer version of macOS, "
notice "please update this message.\n"
notice "After each statement, either something will open for you to"
notice "interact with, or a script will run for you to use\n"
notice "Press enter when a download/install is completed to go to"
notice "the next step (including this one)"
read -p "Press enter to continue..."
# Run sudo once at the beginning to get the necessary permissions.
notice "This setup script needs your password to install things as root."
sudo sh -c 'echo Thanks'
update_path
maybe_generate_ssh_keys
register_ssh_keys
install_gcc
install_homebrew
install_slack
update_git
install_node
install_phantomjs
install_nginx
install_image_utils
install_helpful_tools
install_java
install_protoc
notice "You might be done! \n\n \
You should open a new shell to pick up any changes."