Skip to content

Commit

Permalink
Ongoing mac-setup.sh cleanup
Browse files Browse the repository at this point in the history
Summary:
* Can pass parameter whether to install apps or not
* Install rust
* Install mkcert
* Minor bug fixes
* Separate a few more things

Issue: https://khanacademy.atlassian.net/browse/INFRA-5864

Test Plan:
Startup clean VM and run mac-setup.sh. Ensure that it works
with no args, -a and -n.

Then finish all other setup.sh and startup KA. (There are
still a few known issues in this area.)

Reviewers: #devops, boris, davidbraley

Reviewed By: #devops, boris, davidbraley

Subscribers: davidbraley, boris, csilvers

Differential Revision: https://phabricator.khanacademy.org/D69082
  • Loading branch information
Eric (iCloud) Brown committed Feb 13, 2021
1 parent d78a225 commit 5167ede
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 53 deletions.
47 changes: 47 additions & 0 deletions bin/edit-system-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# History:
# * Functionality moved from setup.sh to edit-system-config.sh
# (called from linux-setup.sh and mac-setup-elevated.sh).

# Bail on any errors
set -e

# Install in $HOME by default, but can set an alternate destination via $1.
ROOT=${1-$HOME}
mkdir -p "$ROOT"

echo "Modifying system configs"

# This command avoids the spew when you deploy the Khan Academy
# appengine app:
# Cannot guess mime-type for XXX. Using application/octet-stream
line="application/octet-stream less eot ttf woff otf as fla sjs flash tmpl"
if [ -s /usr/local/etc/mime.types ]; then
# Replace any existing line with 'less' and 'eot' with the new line.
grep -v 'less eot' /usr/local/etc/mime.types | \
sudo sh -c "cat; echo '$line' > /usr/local/etc/mime.types"
else
sudo sh -c 'echo "$line" > /usr/local/etc/mime.types'
fi
sudo chmod a+r /usr/local/etc/mime.types

# If there is no ssh key, make one.
mkdir -p "$ROOT/.ssh"
if [ ! -e "$ROOT/.ssh/id_rsa" -a ! -e "$ROOT/.ssh/id_dsa" ]; then
ssh-keygen -q -N "" -t rsa -f "$ROOT/.ssh/id_rsa"
fi

# if the user does not have a global gitignore file configured, reference
# ours (or whatever is in the default location
if ! git config --global core.excludesfile > /dev/null; then
git config --global core.excludesfile ~/.gitignore
fi
# cleanup from previous versions: remove ~/.gitignore.khan symlink if exists
rm -f ~/.gitignore.khan

# Apple is very picky on permsions of files zsh loads
ZSHSHARE="/usr/local/share/zsh"
if [[ -d "${ZSHSHARE}" ]]; then
chmod -R 755 "${ZSHSHARE}"
fi
47 changes: 43 additions & 4 deletions bin/install-mac-apps.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
#!/bin/bash

# User may be prompted for password so brew can run sudo
# This script is normally called by mac-setup.sh (with relevant arguments)

# User may be prompted for password so brew can run sudo when installing
# - google-drive-file-stream (currently broken in homebrew 2/4/2021)
# - zoom

# Bail on any errors
set -e

SCRIPT=$(basename $0)

usage() {
cat <<EOF
usage: $SCRIPT [options]
-n, --none Install none of the optional apps some Khan DEVs like
-a, --all Install all apps
EOF
}

# Process command line arguments
while [[ "$1" != "" ]]; do
case $1 in
-n | --none)
# Install no apps (just exit)
exit 0
;;
-a | --all)
input="a"
;;
-h | --help)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
shift
done

# TODO(ericbrown): Remove by 6/1/2021 if no complaints (not needed anymore?)
# Likely need an alternate versions of Casks in order to install chrome-canary
# Required to install chrome-canary
Expand All @@ -28,9 +64,12 @@ install_mac_apps() {
)

mac_apps_str="${mac_apps[@]}"
echo "We recommend installing the following apps: ${mac_apps_str}. \n\n"

read -r -p "Would you like to install [a]ll, [n]one, or [s]ome of the apps? [a/n/s]: " input
if [[ -z "$input" ]]; then
echo "We recommend installing the following apps: ${mac_apps_str}."
echo
read -r -p "Would you like to install [a]ll, [n]one, or [s]ome of the apps? [a/n/s]: " input
fi

case "$input" in
[aA][lL][lL] | [aA])
Expand All @@ -54,7 +93,7 @@ install_mac_apps() {
for app in ${chosen_apps[@]}; do
if ! brew cask ls $app >/dev/null 2>&1; then
echo "$app is not installed, installing $app"
brew install --cask $app || warn "Failed to install $app, perhaps it is already installed."
brew install --cask $app || echo "Failed to install $app, perhaps it is already installed."
else
echo "$app already installed"
fi
Expand Down
22 changes: 22 additions & 0 deletions bin/install-mac-mkcert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3
"""Install mkcert and setup a CA.
This very simple script exists because we want it to be called along with all
the other scripts that require elevated permissions (sudo) and because it
requires a reboot after completion.
"""

import subprocess

result = subprocess.run(['which', 'mkcert'], capture_output=True)
if result.returncode != 0:
subprocess.run(['brew', 'install', 'mkcert'], check=True)
# The following will ask for your password
subprocess.run(['mkcert', '-install'], check=True)

print("""
You have installed mkcert (used to make khanacademy.dev work)
A CA has been added to your system and browser certificate trust stores.
You must REBOOT your machine for browsers to recognize new CA.
""")
12 changes: 12 additions & 0 deletions bin/install-mac-rust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3
"""Install rust & cargo on a mac."""

# TODO(ebrown): Make something that works on mac & linux
# TODO(ebrown): Install a specific version of rust/cargo
# TODO(ebrown): Tweak khan startup scripts instead of rustup-init doing it

import subprocess

subprocess.run(['brew', 'install', 'rustup-init'], check=True)
subprocess.run(['rustup-init', '-y', '-t', 'wasm32-wasi', '--no-modify-path'],
check=True)
2 changes: 2 additions & 0 deletions linux-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,6 @@ install_postgresql
# TODO (boris): Setup pyenv (see mac_setup:install_python_tools)
# https://opencafe.readthedocs.io/en/latest/getting_started/pyenv/

"$DEVTOOLS_DIR"/khan-dotfiles/bin/edit-system-config.sh

trap - EXIT
58 changes: 56 additions & 2 deletions mac-setup-elevated.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,50 @@
# Bail on any errors
set -e

SCRIPT=$(basename $0)

usage() {
cat << EOF
usage: $SCRIPT [options]
--root <dir> Use specified directory as root (instead of HOME).
--all Install all user apps.
--none Install no user apps.
EOF
}

# Install in $HOME by default, but can set an alternate destination via $1.
ROOT="${ROOT:-$HOME}"

APPS=

# Process command line arguments
while [[ "$1" != "" ]]; do
case $1 in
-r | --root)
shift
ROOT=$1
;;
-a | --all)
APPS="-a"
;;
-n | --none)
APPS="-n"
;;
-h | --help)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
shift
done

echo foobar

# The directory to which all repositories will be cloned.
ROOT=${1-$HOME}
REPOS_DIR="$ROOT/khan"

# Derived path location constants
Expand Down Expand Up @@ -37,8 +79,20 @@ install_protoc() {
echo "This setup script needs your password to install things as root."
sudo sh -c 'echo Thanks'

# Add github to known_hosts (one less prompt when QAing script)
mkdir -p ~/.ssh
grep -q github.com ~/.ssh/known_hosts 2>/dev/null || \
echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" \
>> ~/.ssh/known_hosts

"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-homebrew.py

# Other brew related installers that require sudo

"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-mkcert.py

"$DEVTOOLS_DIR"/khan-dotfiles/bin/edit-system-config.sh

# It used to be we needed to install xcode-tools, now homebrew does this for us
#"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-gcc.sh

Expand All @@ -48,4 +102,4 @@ install_protoc
# (as well as in khan-linter for linting those jobs)
install_mac_java

"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-apps.sh
"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-apps.sh "$APPS"
1 change: 1 addition & 0 deletions mac-setup-normal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ update_git
install_node
install_go

"$DEVTOOLS_DIR"/khan-dotfiles/bin/install-mac-rust.py
"$DEVTOOLS_DIR"/khan-dotfiles/bin/mac-setup-postgres.py

install_nginx
Expand Down
42 changes: 38 additions & 4 deletions mac-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,45 @@
# Bail on any errors
set -e

# TODO(ericbrown): Support --quiet command line argument
# TODO(ericbrown): Use python??
SCRIPT=$(basename $0)

usage() {
cat << EOF
usage: $SCRIPT [options]
--root <dir> Use specified directory as root (instead of HOME).
--all Install all user apps.
--none Install no user apps.
EOF
}

# Install in $HOME by default, but can set an alternate destination via $1.
ROOT="${ROOT:-$HOME}"

# Process command line arguments
while [[ "$1" != "" ]]; do
case $1 in
-r | --root)
shift
ROOT=$1
;;
-a | --all)
APPS="-a"
;;
-n | --none)
APPS="-n"
;;
-h | --help)
usage
exit 0
;;
*)
usage
exit 1
esac
shift
done

# The directory to which all repositories will be cloned.
ROOT=${1-$HOME}
REPOS_DIR="$ROOT/khan"

# Derived path location constants
Expand Down Expand Up @@ -43,7 +77,7 @@ read -p "Press enter to continue..."
# Note that ensure parsing arguments (above) doesn't hide anything

# Run setup that requires sudo access
"$DEVTOOLS_DIR"/khan-dotfiles/mac-setup-elevated.sh
"$DEVTOOLS_DIR"/khan-dotfiles/mac-setup-elevated.sh "$APPS"

# Run setup that does NOT require sudo access
"$DEVTOOLS_DIR"/khan-dotfiles/mac-setup-normal.sh
3 changes: 3 additions & 0 deletions profile.default
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
if [ -s ~/.profile.khan ]; then
. ~/.profile.khan
fi
if [ -s ~/.cargo/env ]; then
. ~/.cargo/env
fi
Loading

0 comments on commit 5167ede

Please sign in to comment.