-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmac
executable file
·445 lines (378 loc) · 12.6 KB
/
mac
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/bash
# Welcome to the Kickstarter laptop script!
# Be prepared to turn your laptop (or desktop, no haters here)
# into an awesome development machine.
set -e
cat > ~/.ksr_functions.sh <<-"EOF"
# Helpful functions for from the kickstarter
# laptop script
# Print a status message with timestamp
print_status() {
printf "[%s] %s ... " "$(date +%H:%M:%S)" "$@"
}
# Print a check to complete the status message
print_done() {
printf "✅ \n"
}
# Print a happy green message
succeed() {
printf "\n$(tput bold)$(tput setaf 2)%s$(tput sgr0)" "$@"
printf "\n❤️ 💛 💚 💙 💜 \n"
}
# Print a warning message
print_warning() {
echo "⚠️ $(tput bold)$(tput setaf 1)$@$(tput sgr0) ⚠️ "
}
# Print a red error
print_error() {
printf "$(tput bold)$(tput setaf 1)%s$(tput sgr0)\n" "$@" >&2
}
# Install the required version of Ruby
check_ruby_version() {
print_status "Checking ruby version"
rbenv_path=$(which rbenv)
if ! $rbenv_path exec ruby -v > /dev/null 2>&1; then
# ensure we use homebrew's readline to avoid OSX's Editline version of readline
export RUBY_CONFIGURE_OPTS=--with-readline-dir=`brew --prefix readline`
$rbenv_path install "$(cat .ruby-version)"
fi
print_done
}
check_bundler_dependencies() {
print_status "Checking bundler dependencies"
# Install latest used version of bundler
BUNDLER_VERSION=$(tail -n 1 Gemfile.lock | tr -d ' ')
gem install -q bundler -v "=$BUNDLER_VERSION"
bundle config --global build.mysql2 --with-opt-dir="$(brew --prefix openssl)"
if ! bundle check >/dev/null
then
bundle install
fi
print_done
}
EOF
# shellcheck disable=SC1090
source ~/.ksr_functions.sh
tmp_output=$(mktemp -q /tmp/ksr.laptop.XXXXXXXXXXX)
exit_message() {
ret=$?
if [ $ret -ne 0 ]; then
if [ -e "$tmp_output" ]; then cat "$tmp_output"; fi
# shellcheck disable=SC1117
printf "\n\n"
print_error "Setup failed. 💔 "
print_error "Try running this command again, have a look at https://app.getguru.com/card/ig4eayoT/Help-bindev-is-not-working or paste this output in #dev-environments Slack channel"
fi
rm -f "$tmp_output"
exit $ret
}
# shellcheck disable=SC2154
trap exit_message EXIT
## Check for OS X >= 10.10, <= 10.15
if [ -n "$DANGER_ZONE" ]; then
print_warning "Skipping check of macOS/OS X version. 😎 DANGER ZONE"
else
if ! sw_vers -productVersion | grep -q "^10.1[0-5]"; then
print_error "Unsupported OS X version. Please use macOS Mojave, High Sierra, Sierra, El Capitan, or Yosemite."
print_error "Or set DANGER_ZONE=1 to proceed anyway."
exit 1
fi
fi
##
# Detect if you're in a tmux session
if [ -n "$TMUX" ]; then
print_error "👋 Hey, it looks like you're in a tmux session. Do you have reattach-to-user-namespace installed? If not, you're going to want to set that up before you proceed."
fi
## Make sure rvm isn't installed
# shellcheck disable=SC2230
if which rvm; then
print_error "Please uninstall rvm before continuing"
exit 1
fi
# XCode command-line tools
# From http://stackoverflow.com/a/15371967
while ! pkgutil --pkg-info=com.apple.pkg.CLTools_Executables > "$tmp_output" 2>&1; do
if [ -z "$xcode_cli_installing" ]
then
print_status "Please install XCode command line tools in the pop-up window"
xcode-select --install
print_status "Waiting for XCode command line tools"
xcode_cli_installing='yes'
else
sleep 1
fi
done
##
# Check that FileVault is enabled.
#
# NB: we test this script on a VirtualBox machine, where it's difficult/impossible to enable FileVault.
# If the user is 'devbootstrapper' (our vbox test user), skip the FileVault check.
if [ "$USER" == "devbootstrapper" ]; then
print_warning "Skipping FileVault check for bootstrapping tests."
else
print_status "Checking FileVault"
if ! fdesetup isactive > /dev/null; then
print_error "You must enable FileVault to continue."
exit 1
fi
print_done
fi
##
# Install & run Docker for Mac
if [ -n "$SKIP_DOCKER" ]; then
# Add flag for folks who wish to opt out of pow.
print_warning "Skipping Docker setup because 'SKIP_DOCKER' is set. YMMV."
else
# Unset old boot2docker config
unset DOCKER_CERT_PATH DOCKER_HOST DOCKER_IP DOCKER_TLS_VERIFY
if ! [ -d "/Applications/Docker.app" ]
then
print_status "Installing Docker"
docker_url=https://download.docker.com/mac/stable/Docker.dmg
docker_dmg=/tmp/Docker.dmg
docker_volume=/Volumes/Docker
curl -s -o $docker_dmg $docker_url
hdiutil attach $docker_dmg -nobrowse > /dev/null
cp -a $docker_volume/Docker.app /Applications/
hdiutil detach $docker_volume > /dev/null
rm -f $docker_dmg
print_done
fi
print_status "Ensuring Docker is running"
if ! /usr/local/bin/docker ps > "$tmp_output" 2>&1
then
open /Applications/Docker.app
fi
print_done
fi
##
# Install homebrew
if ! command -v brew >/dev/null; then
print_status "Installing Homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
print_done
fi
# Fix permission issue on /usr/local on OS 10.12 or less
# NOTE: this may be obsolete
if sw_vers -productVersion | grep -q "^10.1[0-3]" && [ ! -w "$(brew --prefix)/Cellar" ]; then
print_status "Fixing /usr/local permissions issue"
sudo chown -R "$(whoami)" "$(brew --prefix)"/*
print_done
fi
# At the time of writing (2018-01-09), the ‘n’ (node.js version manager) package doesn't
# automatically create the `/usr/local/n` directory automatically, so an error
# occurs when `n` is tried for the first time. So we'll manually create the
# directory ourselves.
if [ ! -d /usr/local/n ]; then
sudo mkdir /usr/local/n
sudo chown "$USER:admin" /usr/local/n
fi
print_status "Checking Homebrew formulae"
brew bundle --file=- > "$tmp_output" <<EOF
tap "homebrew/services" # For 'brew service'
tap "puma/puma" # For puma-dev
brew "awscli"
brew "git"
brew "jq"
brew "n"
brew "openssl"
brew "parallel"
brew "puma-dev"
brew "gmp"
brew "coreutils"
brew "rbenv"
brew "ruby-build"
EOF
print_done
function uninstall_homebrew_package {
if brew ls --versions "$1" > /dev/null; then
print_status "Uninstalling Homebrew package ‘$1’"
brew uninstall --force "$1" > /dev/null 2> "$tmp_output"
print_done
fi
}
# Uninstall problematic brew packages
uninstall_homebrew_package brew-cask
uninstall_homebrew_package node
uninstall_homebrew_package node4-tls
##
# Configure rbenv
if [ -n "$RBENV_ROOT" ] && [ "$RBENV_ROOT" != "${HOME}/.rbenv" ]; then
print_warning "Warning: RBENV_ROOT is an unexpected value (${RBENV_ROOT}). YMMV."
fi
print_status "Initializing rbenv"
eval "$(rbenv init -)"
print_done
##
# source ~/.ksr.rc from shell's rc file
cat > ~/.ksr.rc <<-"EOF"
# Kickstarter init commands provisioned by the laptop script.
# Changes will be overwritten.
# Initialize rbenv
eval "$(rbenv init -)"
if [[ ! "$PATH" =~ "/usr/local/bin" ]]; then
export PATH="/usr/local/bin:$PATH"
fi
# Set a higher ulimit to avoid
# capistrano issues deploying to many servers
LIMIT=2048
if [ $LIMIT -gt "$(ulimit -n)" ]; then
ulimit -n $LIMIT
fi
EOF
cmd='if [ -e ~/.ksr.rc ]; then source ~/.ksr.rc; fi # Provisioned by ksr laptop script'
case $(basename "$SHELL") in
bash )
profile="$HOME/.bashrc"
# Source bashrc from bash_profile
if ! grep -Fqs 'source ~/.bashrc' "$HOME/.bash_profile"; then
echo ''
echo 'source ~/.bashrc # Provisioned by ksr laptop script' >> "$HOME/.bash_profile"
fi
;;
zsh )
profile="$HOME/.zshrc"
;;
fish )
profile="$HOME/.config/fish/config.fish"
# Use a different command syntax
cmd='if test -e ~/.config/fish/ksr.fish; source ~/.config/fish/ksr.fish; end # Provisioned by ksr laptop script'
mkdir -p ~/.config/fish
cat > ~/.config/fish/ksr.fish <<-"EOF"
# Kickstarter init commands provisioned by the laptop script.
# Changes will be overwritten.
# Initialize rbenv
status --is-interactive; and . (rbenv init -|psub)
if not echo "$PATH" |grep -Fq "/usr/local/bin"
export PATH="/usr/local/bin:$PATH"
end
# Set a higher ulimit to avoid
# capistrano issues deploying to many servers
set LIMIT 2048
if test $LIMIT -gt (ulimit -n)
ulimit -n $LIMIT
end
EOF
;;
* )
echo 'Unrecognized shell'
;;
esac
if ! grep -Fqs "$cmd" "$profile"; then
print_status "Adding ksr config to $profile"
# shellcheck disable=SC2059,SC1117
printf "\n$cmd\n" >> "$profile"
print_done
fi
# Use osxkeychain for git https passwords
print_status "Configuring osxkeychain for git passwords"
git config --global credential.helper osxkeychain
print_done
## Puma-Dev Management
puma_certificate_file="${HOME}/Library/Application Support/io.puma.dev/cert.pem"
function puma_certificate_installed {
test -f "${puma_certificate_file}" && security verify-cert -c "${puma_certificate_file}" >> "$tmp_output" 2>&1
}
# Remove the Puma-dev TLS root cert, disable puma-dev dns resolution for .test domains
function deconfigure_puma_dev {
if command -v puma-dev >> "$tmp_output"; then
# Only attempt to disable .test DNS resolution if puma was actually running.
if puma-dev -stop >> "$tmp_output" 2>&1; then
puma-dev -uninstall -d test
fi
fi
if puma_certificate_installed; then
print_warning "You may need to remove HSTS settings for Chrome - chrome://net-internals/#hsts"
security remove-trusted-cert "${puma_certificate_file}"
fi
}
# Configure puma-dev, assumes that the puma-dev gem is installed
function configure_puma_dev {
if ! curl --fail -sH "Host: puma-dev" localhost/status >> "$tmp_output"; then
echo "Configuring puma-dev" # puma-dev is installed by homebrew
sudo puma-dev -setup -d test
puma-dev -install -d test
fi
if ! puma_certificate_installed; then
echo "Installing Certificate (password required)"
# Return value is pre-quoted with extra space, xargs clears it by echoing
login_keychain=$(security login-keychain|xargs)
# shellcheck disable=SC2086
security add-trusted-cert -k ${login_keychain} "${puma_certificate_file}"
fi
[[ $(curl --silent http://expectUnknownApp.test) == 'unknown app' ]]
}
# Uninstall pow, if installed
function uninstall_pow {
POW_CURRENT_PATH="${HOME}/Library/Application Support/Pow/Current"
if [ -d "$POW_CURRENT_PATH" ]; then
print_warning "Uninstalling POW"
curl -Ls https://raw.githubusercontent.com/ktheory/pow/fix-launchctl-bootstrap/uninstall.sh | bash
fi
}
# ----------- DEVELOPMENT_SERVER -----------
# Temporarily allow switching between pow and puma-dev with this flag.
export DEVELOPMENT_SERVER=${DEVELOPMENT_SERVER:=puma-dev}
print_status "Using DEVELOPMENT_SERVER=$(tput bold)$(tput setaf 7)${DEVELOPMENT_SERVER}$(tput sgr0)"
print_done
if [[ "$DEVELOPMENT_SERVER" != "puma-dev" ]]; then
export SKIP_PUMA_DEV=1
fi
if [[ "$DEVELOPMENT_SERVER" != "pow" ]]; then
export SKIP_POW=1
fi
# ----------- DEVELOPMENT_SERVER -----------
##
# Install Pow web server
# NB: we must install pow after the shell is configured with rbenv.
# See https://github.com/basecamp/pow/wiki/Troubleshooting#rbenv
if [ -n "$SKIP_POW" ]; then
# Add flag for folks who wish to opt out of pow.
if [[ "$DEVELOPMENT_SERVER" == "pow" ]]; then
print_warning "Skipping Pow setup because 'SKIP_POW' is set. YMMV."
fi
else
# ------------ TEMPORARILY UNINSTALL PUMA ------------------------
print_status "Disabling Puma-dev"
deconfigure_puma_dev
print_done
# ------------ TEMPORARILY UNINSTALL PUMA ------------------------
# Increase pow's default idle timeout to 1hr
echo "export POW_TIMEOUT=3600" > ~/.powconfig
echo "export POW_DOMAINS=test" >> ~/.powconfig
print_status "Checking Pow web server"
pow_install_needed=""
if ! curl --fail -sH host:pow "127.0.0.1/status.json" > /dev/null; then
pow_install_needed=true
fi
# Will pow resolve for .test domains?
# Puma-dev will 500 error on http://test, but Pow will succeed
if ! curl --fail --silent -I http://test > /dev/null 2>&1; then
pow_install_needed=true
fi
if [ -n "$pow_install_needed" ]; then
# Use custom install script until https://github.com/basecamp/pow/pull/505 is released
# curl -s get.pow.cx | sh
echo "Installing Pow"
curl -s https://raw.githubusercontent.com/kickstarter/pow/fix-launchctl-bootstrap/install.sh | sh
fi
print_done
fi
##
# Install puma-dev development server
if [ -n "$SKIP_PUMA_DEV" ]; then
if [[ "$DEVELOPMENT_SERVER" == "puma-dev" ]]; then
print_warning "Skipping puma-dev setup because 'SKIP_PUMA_DEV' is set."
fi
else
# ------------ TEMPORARILY UNINSTALL POW ------------------------
uninstall_pow
# ------------ TEMPORARILY UNINSTALL POW ------------------------
print_status "Checking puma-dev server"
echo > "$tmp_output"
configure_puma_dev
print_done
fi
if [ -z "$SKIP_LAPTOP_SUCCESS" ]; then
succeed "Laptop setup successful!"
fi