Skip to content

Instantly share code, notes, and snippets.

@shaneog
Last active September 22, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaneog/f252f57152812c6df83408caf45666cd to your computer and use it in GitHub Desktop.
Save shaneog/f252f57152812c6df83408caf45666cd to your computer and use it in GitHub Desktop.
My custom strap.sh file
#!/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
echo "!!! If you're stuck: file an issue with debugging output at:" >&2
echo "!!! $STRAP_ISSUES_URL" >&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"
# Set by web/app.rb
# STRAP_GIT_NAME=
# STRAP_GIT_EMAIL=
# STRAP_GITHUB_USER=
# STRAP_GITHUB_TOKEN=
STRAP_ISSUES_URL="https://github.com/mikemcquaid/strap/issues/new"
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|12)" || {
abort "Run Strap on Mac OS X 10.9/10/11/12."
}
[ "$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
log "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)
if [ -z "$DEVELOPER_DIR" ] || ! [ -f "$DEVELOPER_DIR/usr/bin/git" ] \
|| ! [ -f "/usr/include/iconv.h" ]
then
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
fi
# 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" ] && [ "$(git config github.user)" != "$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 git credential-osxkeychain 2>&1 | grep $Q "git.credential-osxkeychain"
then
if [ "$(git config --global credential.helper)" != "osxkeychain" ]
then
git config --global credential.helper osxkeychain
fi
if [ -n "$STRAP_GITHUB_USER" ] && [ -n "$STRAP_GITHUB_TOKEN" ]
then
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
fi
logk
# Setup Homebrew directory and permissions.
logn "Installing Homebrew:"
HOMEBREW_PREFIX="$(brew --prefix 2>/dev/null || true)"
[ -n "$HOMEBREW_PREFIX" ] || HOMEBREW_PREFIX="/usr/local"
[ -d "$HOMEBREW_PREFIX" ] || sudo mkdir -p "$HOMEBREW_PREFIX"
sudo chown "root:wheel" "$HOMEBREW_PREFIX"
(
cd "$HOMEBREW_PREFIX"
sudo mkdir -p Cellar Frameworks bin etc include lib opt sbin share var
sudo chown -R "$USER:admin" Cellar Frameworks bin etc include lib opt sbin share var
)
HOMEBREW_REPOSITORY="$(brew --repository 2>/dev/null || true)"
[ -n "$HOMEBREW_REPOSITORY" ] || HOMEBREW_REPOSITORY="/usr/local/Homebrew"
[ -d "$HOMEBREW_REPOSITORY" ] || sudo mkdir -p "$HOMEBREW_REPOSITORY"
sudo chown -R "$USER:admin" "$HOMEBREW_REPOSITORY"
if [ $HOMEBREW_PREFIX != $HOMEBREW_REPOSITORY ]
then
ln -sf "$HOMEBREW_REPOSITORY/bin/brew" "$HOMEBREW_PREFIX/bin/brew"
fi
# Download Homebrew.
export GIT_DIR="$HOMEBREW_REPOSITORY/.git" GIT_WORK_TREE="$HOMEBREW_REPOSITORY"
[ -d "$GIT_DIR" ] && HOMEBREW_EXISTING="1"
git init $Q
git config remote.origin.url "https://github.com/Homebrew/brew"
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
if [ -n "$HOMEBREW_EXISTING" ]
then
git fetch $Q
else
git fetch $Q --no-tags --depth=1 --force --update-shallow
fi
git reset $Q --hard origin/master
unset GIT_DIR GIT_WORK_TREE HOMEBREW_EXISTING
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
# 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
# Setup Brewfile
if [ -n "$STRAP_GITHUB_USER" ] && ! [ -f "$HOME/.Brewfile" ]; then
HOMEBREW_BREWFILE_URL="https://github.com/$STRAP_GITHUB_USER/homebrew-brewfile"
if git ls-remote "$HOMEBREW_BREWFILE_URL" &>/dev/null; then
log "Fetching $STRAP_GITHUB_USER/homebrew-brewfile from GitHub:"
if [ ! -d "$HOME/.homebrew-brewfile" ]; then
log "Cloning to ~/.homebrew-brewfile:"
git clone $Q "$HOMEBREW_BREWFILE_URL" ~/.homebrew-brewfile
logk
else
(
cd ~/.homebrew-brewfile
git pull $Q
)
fi
ln -sf ~/.homebrew-brewfile/Brewfile ~/.Brewfile
logk
fi
fi
# Install from local Brewfile
if [ -f "$HOME/.Brewfile" ]; then
log "Installing from user Brewfile on GitHub:"
brew bundle --global
logk
fi
# Setup dotfiles
if [ -n "$STRAP_GITHUB_USER" ]; then
DOTFILES_URL="https://github.com/$STRAP_GITHUB_USER/dotfiles"
if git ls-remote "$DOTFILES_URL" &>/dev/null; then
log "Fetching $STRAP_GITHUB_USER/dotfiles from GitHub:"
if [ ! -d "$HOME/.dotfiles" ]; then
log "Cloning to ~/.dotfiles:"
git clone $Q "$DOTFILES_URL" ~/.dotfiles
fi
(
cd ~/.dotfiles
for i in script/setup script/bootstrap; do
if [ -f "$i" ] && [ -x "$i" ]; then
log "Running dotfiles $i:"
"$i" 2>/dev/null
break
fi
done
)
logk
fi
fi
STRAP_SUCCESS="1"
log "Your system is now Strap'd!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment