diff --git a/bin/mac-setup-postgres.py b/bin/mac-setup-postgres.py index f09ef86..a83f457 100755 --- a/bin/mac-setup-postgres.py +++ b/bin/mac-setup-postgres.py @@ -13,8 +13,6 @@ # may be useful to use homebrew to install python3 before running python3 # scripts. -# TODO(ericbrown): Why do we support anything other than postgresql@14 ? - import os import re import subprocess @@ -38,13 +36,6 @@ def get_brewname(): if result.returncode == 0: return POSTGRES_FORMULA - # TODO(ericbrown): Remove when sure this is no longer needed - # I believe this code is from when postgresql 11 was the current version - result = subprocess.run([BREW, 'ls', 'postgres', '--versions'], - capture_output=True, text=True) - if result.returncode == 0 and re.search(r'\s11\.\d', result.stdout): - return "postgresql" - # There is no postgresql installed return None @@ -71,8 +62,8 @@ def link_postgres_if_needed(brewname, force=False): def install_postgres() -> None: - subprocess.run(['BREW', 'install', 'postgresql@14'], check=True) - link_postgres_if_needed('postgresql@14', force=True) + subprocess.run(['BREW', 'install', POSTGRES_FORMULA], check=True) + link_postgres_if_needed(POSTGRES_FORMULA, force=True) def is_postgres_running(brewname: str) -> bool: @@ -116,6 +107,19 @@ def setup_postgres() -> None: brewname = POSTGRES_FORMULA install_postgres() else: + # The homebrew team only tests the latest versions of every package + # together so make sure we're on latest. + print(f'{SCRIPT}: {BREW} upgrade {brewname}') + result = subprocess.check_output([BREW, 'upgrade', POSTGRES_FORMULA]) + + # Initiate and wait for restart if an upgrade happened. Otherwise the + # service won't be ready in time for does_postgres_user_exist(). + if (re.search(r'Upgrading \d+ outdated package', + result.decode('utf-8'))): + print(f'{SCRIPT}: {BREW} services restart {brewname}') + subprocess.check_call([BREW, 'services', 'restart', + POSTGRES_FORMULA]) + # Sometimes postgresql gets unlinked if dev is tweaking their env # Force in case user has another version of postgresql installed too link_postgres_if_needed(brewname, force=True) diff --git a/mac-setup-normal.sh b/mac-setup-normal.sh index c49baa0..f5dc579 100755 --- a/mac-setup-normal.sh +++ b/mac-setup-normal.sh @@ -39,11 +39,10 @@ trap exit_warning EXIT # from shared-functions.sh 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.) + # We need /opt/homebrew/bin (/usr/local/bin on x86) 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 } @@ -153,22 +152,55 @@ update_git() { fi } +# install_or_upgrade_brew_formula ensures the latest version of the passed +# formula is installed as the homebrew team only tests the latest versions of +# every formula together. +install_or_upgrade_brew_formula() { + formulaName=$1 + + if brew ls --versions "$formulaName" >/dev/null ; then + info "Upgrading brew formula $formulaName\n" + brew upgrade "$formulaName" + else + info "Installing brew formula $formulaName\n" + brew install "$formulaName" + fi +} + install_node() { + # We need to uninstall the deprecated node@16 homebrew formula if it is + # installed so its dependencies don't conflict with the dependencies of the + # latest postgresql@14 homebrew formula. + if brew ls --versions node@16 >/dev/null ; then + brew uninstall node@16 + fi + + # Upgrade brew-installed node@20 if it is already installed. + if brew ls --versions node@20 >/dev/null ; then + install_or_upgrade_brew_formula node@20 + fi + + # Install node@20 homebrew formula if no node binary is found in $PATH. if ! which node >/dev/null 2>&1; then # Install node 20: It's LTS and the latest version supported on # appengine standard. - brew install node@20 + install_or_upgrade_brew_formula node@20 - # We need this because brew doesn't link /usr/local/bin/node - # by default when installing non-latest node. + # We need this because brew doesn't link /opt/homebrew/bin/node + # (/usr/local/bin/node on x86) by default when installing non-latest + # node. brew link --force --overwrite node@20 fi + + # At this point, users should have a node binary, whether it's from homebrew + # (preferred), NVM or standard install. + # We don't want to force usage of node v20, but we want to make clear we # don't support anything else. if ! node --version | grep "v20" >/dev/null ; then notice "Your version of node is $(node --version). We currently only support v20." if brew ls --versions node@20 >/dev/null ; then - notice "You do however have node 20 installed." + notice "You do however have node 20 installed via brew." notice "Consider running:" else notice "Consider running:" @@ -284,7 +316,7 @@ install_python_tools() { info "Installing python 3.11\n" brew install python@3.11 fi - # The python3 cask does not install `python` as a symlink, so we do. + # The python3 formula does not install `python` as a symlink, so we do. if ! [ -e /usr/local/bin/python ]; then ln -snf python3 /usr/local/bin/python fi