Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to install as $INSTALL_USER #789

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ If you want to run the Homebrew installer non-interactively without prompting fo
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

If you want to run the Homebrew installer as root whithout prompting for passwords, while installing for a non-root user, you can use:
berikv marked this conversation as resolved.
Show resolved Hide resolved

```bash
INSTALL_USER=<user> /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

## Uninstall Homebrew

```bash
Expand Down
93 changes: 57 additions & 36 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ then
export USER
fi

# Allow delegating installation to a different user (useful for installs by root)
INSTALL_USER="${INSTALL_USER-${USER}}"
INSTALL_HOME=$(eval echo ~"${INSTALL_USER}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will quietly fail if the user does not exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a validation for the INSTALL_USER to exist


# First check OS.
OS="$(uname)"
if [[ "${OS}" == "Linux" ]]
Expand Down Expand Up @@ -136,7 +140,7 @@ then
HOMEBREW_PREFIX="/usr/local"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
fi
HOMEBREW_CACHE="${HOME}/Library/Caches/Homebrew"
HOMEBREW_CACHE="${INSTALL_HOME}/Library/Caches/Homebrew"

STAT_PRINTF=("stat" "-f")
PERMISSION_FORMAT="%A"
Expand All @@ -151,15 +155,15 @@ else
# On Linux, this script installs to /home/linuxbrew/.linuxbrew only
HOMEBREW_PREFIX="/home/linuxbrew/.linuxbrew"
HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}/Homebrew"
HOMEBREW_CACHE="${HOME}/.cache/Homebrew"
HOMEBREW_CACHE="${INSTALL_HOME}/.cache/Homebrew"

STAT_PRINTF=("stat" "--printf")
PERMISSION_FORMAT="%a"
CHOWN=("/bin/chown")
CHGRP=("/bin/chgrp")
GROUP="$(id -gn)"
TOUCH=("/bin/touch")
INSTALL=("/usr/bin/install" -d -o "${USER}" -g "${GROUP}" -m "0755")
INSTALL=("/usr/bin/install" -d -o "${INSTALL_USER}" -g "${GROUP}" -m "0755")
fi
CHMOD=("/bin/chmod")
MKDIR=("/bin/mkdir" "-p")
Expand Down Expand Up @@ -238,9 +242,21 @@ execute() {
fi
}

execute_user() {
local -a args=("$@")
if [[ "${INSTALL_USER}" == "${USER}" ]]
then
ohai "${args[@]}"
execute "${args[@]}"
else
ohai "/usr/bin/sudo" "-Hu" "${INSTALL_USER}" "${args[@]}"
execute "/usr/bin/sudo" "-Hu" "${INSTALL_USER}" "${args[@]}"
fi
}

execute_sudo() {
local -a args=("$@")
if have_sudo_access
if [[ "${EUID:-${UID}}" != "0" ]] && have_sudo_access
then
if [[ -n "${SUDO_ASKPASS-}" ]]
then
Expand Down Expand Up @@ -300,14 +316,19 @@ version_lt() {
}

check_run_command_as_root() {
[[ "${EUID:-${UID}}" == "0" ]] || return
INSTALL_USER_UID=$(id -u "${INSTALL_USER}")
if [[ "${INSTALL_USER_UID}" != "0" ]]
then
ohai "Installing homebrew for user ${INSTALL_USER}"
return
fi

# Allow Azure Pipelines/GitHub Actions/Docker/Concourse/Kubernetes to do everything as root (as it's normal there)
[[ -f /.dockerenv ]] && return
[[ -f /run/.containerenv ]] && return
[[ -f /proc/1/cgroup ]] && grep -E "azpl_job|actions_job|docker|garden|kubepods" -q /proc/1/cgroup && return

abort "Don't run this as root!"
abort "Refusing install as root. Define INSTALL_USER=<user> to delegate installation to a user"
}

should_install_command_line_tools() {
Expand Down Expand Up @@ -350,7 +371,7 @@ get_group() {
}

file_not_grpowned() {
[[ " $(id -G "${USER}") " != *" $(get_group "$1") "* ]]
[[ " $(id -G "${INSTALL_USER}") " != *" $(get_group "$1") "* ]]
}

# Please sync with 'test_ruby()' in 'Library/Homebrew/utils/ruby.sh' from the Homebrew/brew repository.
Expand Down Expand Up @@ -668,7 +689,7 @@ then
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
ohai "The following existing directories will have their owner set to ${tty_underline}${USER}${tty_reset}:"
ohai "The following existing directories will have their owner set to ${tty_underline}${INSTALL_USER}${tty_reset}:"
printf "%s\n" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
Expand Down Expand Up @@ -733,7 +754,7 @@ then
fi
if [[ "${#chowns[@]}" -gt 0 ]]
then
execute_sudo "${CHOWN[@]}" "${USER}" "${chowns[@]}"
execute_sudo "${CHOWN[@]}" "${INSTALL_USER}" "${chowns[@]}"
fi
if [[ "${#chgrps[@]}" -gt 0 ]]
then
Expand All @@ -751,23 +772,23 @@ then
then
execute_sudo "${CHMOD[@]}" "go-w" "${mkdirs_user_only[@]}"
fi
execute_sudo "${CHOWN[@]}" "${USER}" "${mkdirs[@]}"
execute_sudo "${CHOWN[@]}" "${INSTALL_USER}" "${mkdirs[@]}"
execute_sudo "${CHGRP[@]}" "${GROUP}" "${mkdirs[@]}"
fi

if ! [[ -d "${HOMEBREW_REPOSITORY}" ]]
then
execute_sudo "${MKDIR[@]}" "${HOMEBREW_REPOSITORY}"
fi
execute_sudo "${CHOWN[@]}" "-R" "${USER}:${GROUP}" "${HOMEBREW_REPOSITORY}"
execute_sudo "${CHOWN[@]}" "-R" "${INSTALL_USER}:${GROUP}" "${HOMEBREW_REPOSITORY}"

if ! [[ -d "${HOMEBREW_CACHE}" ]]
then
if [[ -n "${HOMEBREW_ON_MACOS-}" ]]
then
execute_sudo "${MKDIR[@]}" "${HOMEBREW_CACHE}"
else
execute "${MKDIR[@]}" "${HOMEBREW_CACHE}"
execute_user "${MKDIR[@]}" "${HOMEBREW_CACHE}"
fi
fi
if exists_but_not_writable "${HOMEBREW_CACHE}"
Expand All @@ -776,15 +797,15 @@ then
fi
if file_not_owned "${HOMEBREW_CACHE}"
then
execute_sudo "${CHOWN[@]}" "-R" "${USER}" "${HOMEBREW_CACHE}"
execute_sudo "${CHOWN[@]}" "-R" "${INSTALL_USER}" "${HOMEBREW_CACHE}"
fi
if file_not_grpowned "${HOMEBREW_CACHE}"
then
execute_sudo "${CHGRP[@]}" "-R" "${GROUP}" "${HOMEBREW_CACHE}"
fi
if [[ -d "${HOMEBREW_CACHE}" ]]
then
execute "${TOUCH[@]}" "${HOMEBREW_CACHE}/.cleaned"
execute_user "${TOUCH[@]}" "${HOMEBREW_CACHE}/.cleaned"
fi

if should_install_command_line_tools && version_ge "${macos_version}" "10.13"
Expand Down Expand Up @@ -815,7 +836,7 @@ fi
if should_install_command_line_tools && test -t 0
then
ohai "Installing the Command Line Tools (expect a GUI popup):"
execute "/usr/bin/xcode-select" "--install"
execute_user "/usr/bin/xcode-select" "--install"
echo "Press any key when the installation has completed."
getc
execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools"
Expand Down Expand Up @@ -900,28 +921,28 @@ ohai "Downloading and installing Homebrew..."
cd "${HOMEBREW_REPOSITORY}" >/dev/null || return

# we do it in four steps to avoid merge errors when reinstalling
execute "${USABLE_GIT}" "-c" "init.defaultBranch=master" "init" "--quiet"
execute_user "${USABLE_GIT}" "-c" "init.defaultBranch=master" "init" "--quiet"

# "git remote add" will fail if the remote is defined in the global config
execute "${USABLE_GIT}" "config" "remote.origin.url" "${HOMEBREW_BREW_GIT_REMOTE}"
execute "${USABLE_GIT}" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
execute_user "${USABLE_GIT}" "config" "remote.origin.url" "${HOMEBREW_BREW_GIT_REMOTE}"
execute_user "${USABLE_GIT}" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"

# ensure we don't munge line endings on checkout
execute "${USABLE_GIT}" "config" "--bool" "core.autocrlf" "false"
execute_user "${USABLE_GIT}" "config" "--bool" "core.autocrlf" "false"

# make sure symlinks are saved as-is
execute "${USABLE_GIT}" "config" "--bool" "core.symlinks" "true"
execute_user "${USABLE_GIT}" "config" "--bool" "core.symlinks" "true"

execute "${USABLE_GIT}" "fetch" "--force" "origin"
execute "${USABLE_GIT}" "fetch" "--force" "--tags" "origin"
execute_user "${USABLE_GIT}" "fetch" "--force" "origin"
execute_user "${USABLE_GIT}" "fetch" "--force" "--tags" "origin"

execute "${USABLE_GIT}" "reset" "--hard" "origin/master"
execute_user "${USABLE_GIT}" "reset" "--hard" "origin/master"

if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_PREFIX}" ]]
then
if [[ "${HOMEBREW_REPOSITORY}" == "${HOMEBREW_PREFIX}/Homebrew" ]]
then
execute "ln" "-sf" "../Homebrew/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
execute_user "ln" "-sf" "../Homebrew/bin/brew" "${HOMEBREW_PREFIX}/bin/brew"
else
abort "The Homebrew/brew repository should be placed in the Homebrew prefix directory."
fi
Expand All @@ -933,23 +954,23 @@ ohai "Downloading and installing Homebrew..."
# shellcheck disable=SC2016
ohai 'Tapping homebrew/core because `$HOMEBREW_NO_INSTALL_FROM_API` is set.'
(
execute "${MKDIR[@]}" "${HOMEBREW_CORE}"
execute_user "${MKDIR[@]}" "${HOMEBREW_CORE}"
cd "${HOMEBREW_CORE}" >/dev/null || return

execute "${USABLE_GIT}" "-c" "init.defaultBranch=master" "init" "--quiet"
execute "${USABLE_GIT}" "config" "remote.origin.url" "${HOMEBREW_CORE_GIT_REMOTE}"
execute "${USABLE_GIT}" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
execute "${USABLE_GIT}" "config" "--bool" "core.autocrlf" "false"
execute "${USABLE_GIT}" "config" "--bool" "core.symlinks" "true"
execute "${USABLE_GIT}" "fetch" "--force" "origin" "refs/heads/master:refs/remotes/origin/master"
execute "${USABLE_GIT}" "remote" "set-head" "origin" "--auto" >/dev/null
execute "${USABLE_GIT}" "reset" "--hard" "origin/master"
execute_user "${USABLE_GIT}" "-c" "init.defaultBranch=master" "init" "--quiet"
execute_user "${USABLE_GIT}" "config" "remote.origin.url" "${HOMEBREW_CORE_GIT_REMOTE}"
execute_user "${USABLE_GIT}" "config" "remote.origin.fetch" "+refs/heads/*:refs/remotes/origin/*"
execute_user "${USABLE_GIT}" "config" "--bool" "core.autocrlf" "false"
execute_user "${USABLE_GIT}" "config" "--bool" "core.symlinks" "true"
execute_user "${USABLE_GIT}" "fetch" "--force" "origin" "refs/heads/master:refs/remotes/origin/master"
execute_user "${USABLE_GIT}" "remote" "set-head" "origin" "--auto" >/dev/null
execute_user "${USABLE_GIT}" "reset" "--hard" "origin/master"

cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
) || exit 1
fi

execute "${HOMEBREW_PREFIX}/bin/brew" "update" "--force" "--quiet"
execute_user "${HOMEBREW_PREFIX}/bin/brew" "update" "--force" "--quiet"
) || exit 1

if [[ ":${PATH}:" != *":${HOMEBREW_PREFIX}/bin:"* ]]
Expand Down Expand Up @@ -985,8 +1006,8 @@ EOS

(
cd "${HOMEBREW_REPOSITORY}" >/dev/null || return
execute "${USABLE_GIT}" "config" "--replace-all" "homebrew.analyticsmessage" "true"
execute "${USABLE_GIT}" "config" "--replace-all" "homebrew.caskanalyticsmessage" "true"
execute_user "${USABLE_GIT}" "config" "--replace-all" "homebrew.analyticsmessage" "true"
execute_user "${USABLE_GIT}" "config" "--replace-all" "homebrew.caskanalyticsmessage" "true"
) || exit 1

ohai "Next steps:"
Expand Down