-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrap.sh
executable file
·323 lines (290 loc) · 9.78 KB
/
strap.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
#!/bin/bash
#/ Usage: bin/strap.sh [--debug]
#/ Install development dependencies on Mac OS X.
set -e
# Keep sudo timestamp updated while Strap is running.
if [ "$1" = "--sudo-wait" ]; then
while true; do
mkdir -p "/var/db/sudo/$SUDO_USER"
touch "/var/db/sudo/$SUDO_USER"
sleep 1
done
exit 0
fi
[ "$1" = "--debug" ] && STRAP_DEBUG="1"
STRAP_SUCCESS=""
cleanup() {
set +e
if [ -n "$STRAP_SUDO_WAIT_PID" ]; then
sudo kill "$STRAP_SUDO_WAIT_PID"
fi
sudo -k
rm -f "$CLT_PLACEHOLDER"
if [ -z "$STRAP_SUCCESS" ]; then
if [ -n "$STRAP_STEP" ]; then
echo "!!! $STRAP_STEP FAILED" >&2
else
echo "!!! FAILED" >&2
fi
if [ -z "$STRAP_DEBUG" ]; then
echo "!!! Run '$0 --debug' for debugging output." >&2
fi
fi
}
trap "cleanup" EXIT
if [ -n "$STRAP_DEBUG" ]; then
set -x
else
STRAP_QUIET_FLAG="-q"
Q="$STRAP_QUIET_FLAG"
fi
STDIN_FILE_DESCRIPTOR="0"
[ -t "$STDIN_FILE_DESCRIPTOR" ] && STRAP_INTERACTIVE="1"
STRAP_GIT_NAME="David Goodlad"
STRAP_GIT_EMAIL="[email protected]"
STRAP_GITHUB_USER="dgoodlad"
STRAP_GITHUB_TOKEN=${STRAP_GITHUB_TOKEN:-}
STRAP_FULL_PATH="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
abort() { STRAP_STEP=""; echo "!!! $*" >&2; exit 1; }
log() { STRAP_STEP="$*"; echo "--> $*"; }
logn() { STRAP_STEP="$*"; printf -- "--> $* "; }
logk() { STRAP_STEP=""; echo "OK"; }
sw_vers -productVersion | grep $Q -E "^10.(9|10|11)" || {
abort "Run Strap on Mac OS X 10.9/10/11."
}
[ "$USER" = "root" ] && abort "Run Strap as yourself, not root."
groups | grep $Q admin || abort "Add $USER to the admin group."
# Initialise sudo now to save prompting later.
log "Enter your password (for sudo access):"
sudo -k
sudo /usr/bin/true
[ -f "$STRAP_FULL_PATH" ]
sudo bash "$STRAP_FULL_PATH" --sudo-wait &
STRAP_SUDO_WAIT_PID="$!"
ps -p "$STRAP_SUDO_WAIT_PID" &>/dev/null
logk
# Set some basic security settings.
logn "Configuring security settings:"
defaults write com.apple.Safari \
com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaEnabled \
-bool false
defaults write com.apple.Safari \
com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaEnabledForLocalFiles \
-bool false
defaults write com.apple.screensaver askForPassword -int 1
defaults write com.apple.screensaver askForPasswordDelay -int 0
sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1
sudo launchctl load /System/Library/LaunchDaemons/com.apple.alf.agent.plist 2>/dev/null
if [ -n "$STRAP_GIT_NAME" ] && [ -n "$STRAP_GIT_EMAIL" ]; then
sudo defaults write /Library/Preferences/com.apple.loginwindow \
LoginwindowText \
"Found this computer? Please contact $STRAP_GIT_NAME at $STRAP_GIT_EMAIL."
fi
logk
# Check and enable full-disk encryption.
logn "Checking full-disk encryption status:"
if fdesetup status | grep $Q -E "FileVault is (On|Off, but will be enabled after the next restart)."; then
logk
elif [ -n "$STRAP_CI" ]; then
echo
logn "Skipping full-disk encryption for CI"
elif [ -n "$STRAP_INTERACTIVE" ]; then
echo
logn "Enabling full-disk encryption on next reboot:"
sudo fdesetup enable -user "$USER" \
| tee ~/Desktop/"FileVault Recovery Key.txt"
logk
else
echo
abort "Run 'sudo fdesetup enable -user \"$USER\"' to enable full-disk encryption."
fi
# Install the Xcode Command Line Tools.
DEVELOPER_DIR=$("xcode-select" -print-path 2>/dev/null || true)
[ -z "$DEVELOPER_DIR" ] || ! [ -f "$DEVELOPER_DIR/usr/bin/git" ] \
|| ! [ -f "/usr/include/iconv.h" ] && {
log "Installing the Xcode Command Line Tools:"
CLT_PLACEHOLDER="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
sudo touch "$CLT_PLACEHOLDER"
CLT_PACKAGE=$(softwareupdate -l | \
grep -B 1 -E "Command Line (Developer|Tools)" | \
awk -F"*" '/^ +\*/ {print $2}' | sed 's/^ *//' | head -n1)
sudo softwareupdate -i "$CLT_PACKAGE"
sudo rm -f "$CLT_PLACEHOLDER"
if ! [ -f "/usr/include/iconv.h" ]; then
if [ -n "$STRAP_INTERACTIVE" ]; then
echo
logn "Requesting user install of Xcode Command Line Tools:"
xcode-select --install
else
echo
abort "Run 'xcode-select --install' to install the Xcode Command Line Tools."
fi
fi
logk
}
# Check if the Xcode license is agreed to and agree if not.
xcode_license() {
if /usr/bin/xcrun clang 2>&1 | grep $Q license; then
if [ -n "$STRAP_INTERACTIVE" ]; then
logn "Asking for Xcode license confirmation:"
sudo xcodebuild -license
logk
else
abort "Run 'sudo xcodebuild -license' to agree to the Xcode license."
fi
fi
}
xcode_license
# Setup Git configuration.
logn "Configuring Git:"
if [ -n "$STRAP_GIT_NAME" ] && ! git config user.name >/dev/null; then
git config --global user.name "$STRAP_GIT_NAME"
fi
if [ -n "$STRAP_GIT_EMAIL" ] && ! git config user.email >/dev/null; then
git config --global user.email "$STRAP_GIT_EMAIL"
fi
if [ -n "$STRAP_GITHUB_USER" ]; then
git config --global github.user "$STRAP_GITHUB_USER"
fi
# Squelch git 2.x warning message when pushing
if ! git config push.default >/dev/null; then
git config --global push.default simple
fi
# Setup GitHub HTTPS credentials.
if [ -n "$STRAP_GITHUB_USER" ] && [ -n "$STRAP_GITHUB_TOKEN" ] \
&& git credential-osxkeychain 2>&1 | grep $Q "git.credential-osxkeychain"
then
if [ "$(git config credential.helper)" != "osxkeychain" ]
then
git config --global credential.helper osxkeychain
fi
printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
printf "protocol=https\nhost=github.com\nusername=%s\npassword=%s\n" \
"$STRAP_GITHUB_USER" "$STRAP_GITHUB_TOKEN" \
| git credential-osxkeychain store
fi
logk
# Setup Homebrew directories and permissions.
logn "Installing Homebrew:"
HOMEBREW_PREFIX="/usr/local"
HOMEBREW_CACHE="/Library/Caches/Homebrew"
for dir in "$HOMEBREW_PREFIX" "$HOMEBREW_CACHE"; do
[ -d "$dir" ] || sudo mkdir -p "$dir"
sudo chown -R "$USER:admin" "$dir"
done
# Download Homebrew.
export GIT_DIR="$HOMEBREW_PREFIX/.git" GIT_WORK_TREE="$HOMEBREW_PREFIX"
git init $Q
git config remote.origin.url "https://github.com/Homebrew/brew"
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch $Q origin master:refs/remotes/origin/master \
--no-tags --depth=1 --force --update-shallow
git reset $Q --hard origin/master
sudo chmod g+rwx "$HOMEBREW_PREFIX"/* "$HOMEBREW_PREFIX"/.??*
unset GIT_DIR GIT_WORK_TREE
logk
# Update Homebrew.
export PATH="$HOMEBREW_PREFIX/bin:$PATH"
log "Updating Homebrew:"
brew update
logk
# Install Homebrew Bundle, Cask, Services and Versions tap.
log "Installing Homebrew taps and extensions:"
brew bundle --file=- <<EOF
tap 'caskroom/cask'
tap 'homebrew/core'
tap 'homebrew/services'
tap 'homebrew/versions'
EOF
logk
# Use pf packet filter to forward ports 80 and 443.
logn "Forwarding local ports 80 to 8080 and 443 to 8443:"
cat <<EOF | sudo tee /etc/pf.anchors/dev.strap >/dev/null
rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 8443
EOF
sudo rm -f /etc/pf.anchors/dev.github 2>/dev/null
sudo perl -pi \
-e 's/(rdr-anchor|load anchor) "dev\.(github|strap)"( from "\/etc\/pf.anchors\/dev\.(github|strap)")?\n//g;' \
-e 's/(rdr-anchor.*)/\1\nrdr-anchor "dev.strap"/g;' \
-e 's|(load anchor.*)|\1\nload anchor "dev.strap" from "/etc/pf.anchors/dev.strap"|g;' \
/etc/pf.conf
sudo launchctl unload /Library/LaunchDaemons/dev.strap.pf.plist 2>/dev/null || true
cat <<EOF | sudo tee /Library/LaunchDaemons/dev.strap.pf.plist >/dev/null
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple Computer/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>dev.strap.pf.plist</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>ipconfig waitall && /sbin/pfctl -e -f /etc/pf.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>FreeBSD Packet Filter (pf) daemon</string>
<key>StandardErrorPath</key>
<string>/var/log/pf.log</string>
<key>StandardOutPath</key>
<string>/var/log/pf.log</string>
</dict>
</plist>
EOF
sudo launchctl load -w /Library/LaunchDaemons/dev.strap.pf.plist 2>/dev/null
logk
# Check and install any remaining software updates.
logn "Checking for software updates:"
if softwareupdate -l 2>&1 | grep $Q "No new software available."; then
logk
else
echo
log "Installing software updates:"
if [ -z "$STRAP_CI" ]; then
sudo softwareupdate --install --all
xcode_license
else
echo "Skipping software updates for CI"
fi
logk
fi
if [ -n "$STRAP_GITHUB_USER" ] && [ -n "$STRAP_GITHUB_TOKEN" ]; then
REPO_URL="https://github.com/$STRAP_GITHUB_USER/homebrew-brewfile"
REPO_API_URL="https://api.github.com/repos/$STRAP_GITHUB_USER/homebrew-brewfile"
STATUS_CODE=$(curl -u "$STRAP_GITHUB_USER:$STRAP_GITHUB_TOKEN" --silent --write-out "%{http_code}" --output /dev/null $REPO_API_URL/contents/Brewfile)
if [ "$STATUS_CODE" -eq 200 ]; then
logn "Fetching user Brewfile from GitHub:"
if [ ! -d "$HOME/.homebrew-brewfile" ]; then
git clone $Q $REPO_URL ~/.homebrew-brewfile
else
cd ~/.homebrew-brewfile
git pull $Q
fi
ln -sf ~/.homebrew-brewfile/Brewfile ~/.Brewfile
logk
fi
fi
logn "Fetching dotfiles from GitHub:"
if [ ! -d "$HOME/.dotfiles" ]; then
git clone $Q https://github.com/$STRAP_GITHUB_USER/dotfiles $HOME/.dotfiles
else
cd ~/.dotfiles
git pull $Q
fi
logk
if [ -x "$HOME/.dotfiles/install.sh" ]; then
logn "Installing dotfiles:"
$HOME/.dotfiles/install.sh
logk
fi
# Install from local Brewfile
if [ -f "$HOME/.Brewfile" ]; then
log "Installing from user Brewfile on GitHub:"
brew bundle --global
logk
fi
STRAP_SUCCESS="1"
log "Your system is now Strap'd!"