Skip to content

fix: Refactor preinstall script to use useradd if adduser is not available #2858

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

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Changes from all 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
21 changes: 13 additions & 8 deletions preinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ set -eu

USER="coder"

# Add a Coder user to run as in systemd.
if ! id -u $USER >/dev/null 2>&1; then
if id -u $USER >/dev/null 2>&1; then
# If the coder user already exists, we don't need to add it.
exit 0
fi

if command -V useradd >/dev/null 2>&1; then
useradd \
--create-home \
--system \
--user-group \
--shell /bin/false \
$USER

# Add the Coder user to the Docker group.
# Coder is frequently used with Docker, so
# this prevents failures when building.
#
# It's fine if this fails!
usermod \
--append \
--groups docker \
$USER 2>/dev/null || true
$USER >/dev/null 2>&1 || true
elif command -V adduser >/dev/null 2>&1; then
# On alpine distributions useradd does not exist.
# This is a backup!
addgroup -S $USER
adduser -G coder -S coder
adduser coder docker >/dev/null 2>&1 || true
fi