Fix UID management for ubuntu users (#3937)

This commit is contained in:
Robert Brennan
2024-09-18 12:29:39 -04:00
committed by GitHub
parent a7f1bca586
commit c864715b43
2 changed files with 15 additions and 20 deletions

View File

@@ -195,27 +195,19 @@ class RuntimeClient:
else:
logger.debug('Added sudoer successfully.')
# Attempt to add the user, retrying with incremented user_id if necessary
while True:
command = (
f'useradd -rm -d /home/{username} -s /bin/bash '
f'-g root -G sudo -u {user_id} {username}'
command = (
f'useradd -rm -d /home/{username} -s /bin/bash '
f'-g root -G sudo -u {user_id} {username}'
)
output = subprocess.run(command, shell=True, capture_output=True)
if output.returncode == 0:
logger.debug(
f'Added user `{username}` successfully with UID {user_id}. Output: [{output.stdout.decode()}]'
)
else:
raise RuntimeError(
f'Failed to create user `{username}` with UID {user_id}. Output: [{output.stderr.decode()}]'
)
output = subprocess.run(command, shell=True, capture_output=True)
if output.returncode == 0:
logger.debug(
f'Added user `{username}` successfully with UID {user_id}. Output: [{output.stdout.decode()}]'
)
break
elif f'UID {user_id} is not unique' in output.stderr.decode():
logger.warning(
f'UID {user_id} is not unique. Incrementing UID and retrying...'
)
user_id += 1
else:
raise RuntimeError(
f'Failed to create user `{username}`! Output: [{output.stderr.decode()}]'
)
def _init_bash_shell(self, work_dir: str, username: str) -> None:
self.shell = pexpect.spawn(

View File

@@ -18,6 +18,9 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Remove UID 1000 if it's called pn--this fixes the nikolaik image for ubuntu users
RUN if getent passwd 1000 | grep -q pn; then userdel pn; fi
# Create necessary directories
RUN mkdir -p /openhands && \
mkdir -p /openhands/logs && \