Skip to content

Commit

Permalink
move up setting username so it can be used by scripts in subshell pro…
Browse files Browse the repository at this point in the history
…cesses
  • Loading branch information
ridhwaans committed Mar 6, 2024
1 parent b3e5e22 commit eeb4942
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 133 deletions.
46 changes: 33 additions & 13 deletions src/base/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@ fi

start_time=$(date +%s)

source $(dirname $0)/scripts/_config.sh
source $(dirname $0)/scripts/_helper.sh

if [ $(uname) = Darwin ]; then
export ADJUSTED_ID="mac"
export ADJUSTED_ID="mac"
elif [ $(uname) = Linux ]; then
# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
. /etc/os-release
# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
. /etc/os-release

# Get an adjusted ID independent of distro variants
if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then
export ADJUSTED_ID="debian"
else
echo "Linux distro ${ID} not supported."
exit 1
fi
# Get an adjusted ID independent of distro variants
if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then
export ADJUSTED_ID="debian"
else
echo "Linux distro ${ID} not supported."
exit 1
fi
fi

source $(dirname $0)/scripts/_config.sh
source $(dirname $0)/scripts/_helper.sh
# If in automatic mode, determine if a user already exists, if not use vscode
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
if [ "$ADJUSTED_ID" = "mac" ]; then
FIRST_USER=$(dscl . -list /Users UniqueID | awk -v val=501 '$2 == val {print $1}')
else
FIRST_USER="$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"
fi
if id -u ${FIRST_USER} > /dev/null 2>&1; then
USERNAME=${FIRST_USER}
fi
if [ "${USERNAME}" = "" ]; then
USERNAME=vscode
fi
elif [ "${USERNAME}" = "none" ]; then
USERNAME=root
USER_UID=0
USER_GID=0
fi

scripts=(
common-utils.sh
Expand Down Expand Up @@ -58,6 +78,6 @@ done

end_time=$(date +%s)
elapsed=$(( end_time - start_time ))
echo -e "Install took $elapsed seconds"
echo -e "Install took $elapsed seconds."

exit $?
28 changes: 2 additions & 26 deletions src/base/scripts/_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,6 @@ updaterc() {
fi
}

get_target_user() {
local USERNAME

if [ "${1}" = "auto" ] || [ "${1}" = "automatic" ]; then
USERNAME=""
if [ "$ADJUSTED_ID" = "mac" ]; then
FIRST_USER=$(dscl . -list /Users UniqueID | awk -v val=501 '$2 == val {print $1}')
else
FIRST_USER="$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"
fi
if id -u ${FIRST_USER} > /dev/null 2>&1; then
USERNAME=${FIRST_USER}
fi
[ "${USERNAME}" = "" ] && USERNAME="root"
elif [ "${1}" = "none" ] || ! id -u "${1}" > /dev/null 2>&1; then
USERNAME="root"
else
USERNAME="${1}"
fi

echo "${USERNAME}"
}

run_brew_command_as_target_user() {
sudo -u $USERNAME brew "$@"
}
Expand All @@ -67,7 +44,7 @@ find_version_from_git_tags() {
local repository=$2
local prefix=${3:-"tags/v"}
local separator=${4:-"."}
local last_part_optional=${5:-"false"}
local last_part_optional=${5:-"false"}
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
local escaped_separator=${separator//./\\.}
local last_part
Expand All @@ -94,7 +71,6 @@ find_version_from_git_tags() {
}

export -f updaterc
export -f get_target_user
export -f find_version_from_git_tags
export -f run_brew_command_as_target_user
export -f conditional_grep
export -f conditional_grep
111 changes: 46 additions & 65 deletions src/base/scripts/common-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,58 @@ install_mac_packages() {
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

# Make sure we’re using the latest Homebrew
run_brew_command_as_target_user update
# Make sure we’re using the latest Homebrew
run_brew_command_as_target_user update

# Upgrade any already-installed formulae
run_brew_command_as_target_user upgrade
# Upgrade any already-installed formulae
run_brew_command_as_target_user upgrade

packages=(
dockutil
fontconfig
git
grep
jq
neofetch
tig
tree
)
run_brew_command_as_target_user install "${packages[@]}"

# Install Caskroom
run_brew_command_as_target_user tap homebrew/cask-versions

apps=(
beekeeper-studio
docker
discord
dropbox
figma
iterm2-nightly
kap
mounty
notion
postman
steam
visual-studio-code
)
packages=(
dockutil
fontconfig
git
grep
jq
neofetch
tig
tree
)
run_brew_command_as_target_user install "${packages[@]}"

# Install Caskroom
run_brew_command_as_target_user tap homebrew/cask-versions

apps=(
beekeeper-studio
docker
discord
dropbox
figma
iterm2-nightly
kap
mounty
notion
postman
steam
visual-studio-code
)

if [ ! -d "/Applications/Google Chrome.app" ]; then
apps+=(google-chrome)
fi
if [ ! -d "/Applications/Google Chrome.app" ]; then
apps+=(google-chrome)
fi

if [ ! -d "/Applications/Slack.app" ]; then
apps+=(slack)
fi
if [ ! -d "/Applications/Slack.app" ]; then
apps+=(slack)
fi

if [ ! -d "/Applications/zoom.us.app" ]; then
apps+=(zoom)
fi
if [ ! -d "/Applications/zoom.us.app" ]; then
apps+=(zoom)
fi

run_brew_command_as_target_user install --cask "${apps[@]}"
run_brew_command_as_target_user install --cask "${apps[@]}"

# Remove outdated versions from the cellar
run_brew_command_as_target_user cleanup
# Remove outdated versions from the cellar
run_brew_command_as_target_user cleanup

# Set Dock items
OLDIFS=$IFS
Expand Down Expand Up @@ -142,26 +142,6 @@ install_debian_packages() {
fi
}

# If in automatic mode, determine if a user already exists, if not use vscode
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
if [ "$ADJUSTED_ID" = "mac" ]; then
FIRST_USER=$(dscl . -list /Users UniqueID | awk -v val=501 '$2 == val {print $1}')
else
FIRST_USER="$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)"
fi
if id -u ${FIRST_USER} > /dev/null 2>&1; then
USERNAME=${FIRST_USER}
fi
if [ "${USERNAME}" = "" ]; then
USERNAME=vscode
fi
elif [ "${USERNAME}" = "none" ]; then
USERNAME=root
USER_UID=0
USER_GID=0
fi

if [ "$ADJUSTED_ID" = "mac" ]; then
dseditgroup -o edit -a $USERNAME -t user wheel
else
Expand Down Expand Up @@ -255,6 +235,7 @@ if [ "$ADJUSTED_ID" != "mac" ]; then
fi

zsh_rc_snippet=$(cat <<EOF
export LANG=en_US.UTF-8
export ZPLUG_HOME="$ZSHPLUG_PATH"
source \$ZPLUG_HOME/init.zsh
Expand Down
5 changes: 1 addition & 4 deletions src/base/scripts/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1
fi

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

if [ "$ADJUSTED_ID" = "mac" ]; then
packages=(
go
Expand Down Expand Up @@ -51,4 +48,4 @@ if [ "${UPDATE_RC}" = "true" ]; then
updaterc "bash" "${go_rc_snippet}"
fi

echo "Done!"
echo "Done!"
5 changes: 1 addition & 4 deletions src/base/scripts/java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export SDKMAN_DIR="${SDKMAN_PATH}"
# alongside JAVA_VERSION, but not set as default.
ADDITIONAL_VERSIONS="${JAVA_ADDITIONAL_VERSIONS:-""}"

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

# Use SDKMAN to install something using a partial version match
sdk_install() {
local install_type=$1
Expand Down Expand Up @@ -103,4 +100,4 @@ if [[ "${INSTALL_MAVEN}" = "true" ]] && ! mvn --version > /dev/null; then
sdk_install maven ${MAVEN_VERSION}
fi

echo "Done!"
echo "Done!"
5 changes: 1 addition & 4 deletions src/base/scripts/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export NVM_DIR="${NVM_PATH}"
# alongside NODE_VERSION, but not set as default.
ADDITIONAL_VERSIONS="${NODE_ADDITIONAL_VERSIONS:-""}"

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

if [ "$ADJUSTED_ID" != "mac" ]; then
# Create nvm group to the user's UID or GID to change while still allowing access to nvm
if ! cat /etc/group | grep -e "^nvm:" > /dev/null 2>&1; then
Expand Down Expand Up @@ -69,4 +66,4 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
IFS=$OLDIFS
fi

echo "Done!"
echo "Done!"
9 changes: 3 additions & 6 deletions src/base/scripts/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export PYENV_ROOT="${PYENV_PATH}"
# alongside PYTHON_VERSION, but not set as default.
ADDITIONAL_VERSIONS="${PYTHON_ADDITIONAL_VERSIONS:-""}"

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

# Mac OS packages
install_mac_packages() {
packages=(
Expand All @@ -33,7 +30,7 @@ install_debian_packages(){
# Ensure apt is in non-interactive to avoid prompts
export DEBIAN_FRONTEND=noninteractive

# General requirements
# General requirements
# https://stackoverflow.com/a/71347968/3577482
# https://stackoverflow.com/questions/50757647/e-gnupg-gnupg2-and-gnupg1-do-not-seem-to-be-installed-but-one-of-them-is-requ
# https://github.com/pyenv/pyenv/issues/677
Expand All @@ -60,7 +57,7 @@ if [ "$ADJUSTED_ID" != "mac" ]; then
umask 0002
[ ! -d ${PYENV_ROOT} ] && git clone https://github.com/pyenv/pyenv.git ${PYENV_ROOT}
chown -R "root:pyenv" ${PYENV_ROOT}
chmod -R g+rws "${PYENV_ROOT}"
chmod -R g+rws "${PYENV_ROOT}"
[ ! -d "${PYENV_ROOT}/plugins/pyenv-virtualenv" ] && git clone https://github.com/pyenv/pyenv-virtualenv.git ${PYENV_ROOT}/plugins/pyenv-virtualenv
fi

Expand Down Expand Up @@ -97,4 +94,4 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
IFS=$OLDIFS
fi

echo "Done!"
echo "Done!"
9 changes: 3 additions & 6 deletions src/base/scripts/ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export RBENV_ROOT="${RBENV_PATH}"
# alongside RUBY_VERSION, but not set as default.
ADDITIONAL_VERSIONS="${RUBY_ADDITIONAL_VERSIONS:-""}"

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

# Mac OS packages
install_mac_packages() {
packages=(
Expand All @@ -35,7 +32,7 @@ install_debian_packages(){
# Ensure apt is in non-interactive to avoid prompts
export DEBIAN_FRONTEND=noninteractive

# General requirements
# General requirements
# https://stackoverflow.com/a/9510209/3577482, https://github.com/rbenv/ruby-build/discussions/2118
apt install -y --no-install-recommends ca-certificates software-properties-common build-essential gnupg2 libreadline-dev \
procps dirmngr gawk autoconf automake bison libffi-dev libgdbm-dev libncurses5-dev \
Expand Down Expand Up @@ -69,7 +66,7 @@ if [ "$ADJUSTED_ID" != "mac" ]; then
umask 0002
[ ! -d ${RBENV_ROOT} ] && git clone https://github.com/rbenv/rbenv.git ${RBENV_ROOT}
chown -R "root:rbenv" ${RBENV_ROOT}
chmod -R g+rws "${RBENV_ROOT}"
chmod -R g+rws "${RBENV_ROOT}"
[ ! -d "${RBENV_ROOT}/plugins/ruby-build" ] && git clone https://github.com/rbenv/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build
[ ! -d "${RBENV_ROOT}/plugins/ruby-gemset" ] && git clone https://github.com/jf/rbenv-gemset.git ${RBENV_ROOT}/plugins/ruby-gemset
fi
Expand Down Expand Up @@ -105,4 +102,4 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
IFS=$OLDIFS
fi

echo "Done!"
echo "Done!"
7 changes: 2 additions & 5 deletions src/base/scripts/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1
fi

# Determine the appropriate non-root user
USERNAME=$(get_target_user $USERNAME)

if [ "$ADJUSTED_ID" = "mac" ]; then
run_brew_command_as_target_user tap aws/tap
run_brew_command_as_target_user tap hashicorp/tap
Expand All @@ -29,7 +26,7 @@ else
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null
apt update
apt update
apt install -y --no-install-recommends gh

# Install exercism-cli
Expand Down Expand Up @@ -80,4 +77,4 @@ else
fi
fi

echo "Done!"
echo "Done!"

0 comments on commit eeb4942

Please sign in to comment.