mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 13:52:43 +08:00
RuntimeClient: make _init_user more robust (#3400)
This commit is contained in:
parent
3a77af8a22
commit
17c360c891
@ -121,6 +121,16 @@ class RuntimeClient:
|
||||
if username == 'root':
|
||||
return
|
||||
|
||||
# Check if the username already exists
|
||||
try:
|
||||
subprocess.run(
|
||||
f'id -u {username}', shell=True, check=True, capture_output=True
|
||||
)
|
||||
logger.debug(f'User {username} already exists. Skipping creation.')
|
||||
return
|
||||
except subprocess.CalledProcessError:
|
||||
pass # User does not exist, continue with creation
|
||||
|
||||
# Add sudoer
|
||||
sudoer_line = r"echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers"
|
||||
output = subprocess.run(sudoer_line, shell=True, capture_output=True)
|
||||
@ -128,30 +138,33 @@ class RuntimeClient:
|
||||
raise RuntimeError(f'Failed to add sudoer: {output.stderr.decode()}')
|
||||
logger.debug(f'Added sudoer successfully. Output: [{output.stdout.decode()}]')
|
||||
|
||||
# Add user and change ownership of the initial working directory if it doesn't exist
|
||||
command = (
|
||||
f'useradd -rm -d /home/{username} -s /bin/bash '
|
||||
f'-g root -G sudo -u {user_id} {username}'
|
||||
)
|
||||
|
||||
if not os.path.exists(self.initial_pwd):
|
||||
command += f' && mkdir -p {self.initial_pwd}'
|
||||
command += f' && chown -R {username}:root {self.initial_pwd}'
|
||||
command += f' && chmod g+s {self.initial_pwd}'
|
||||
|
||||
output = subprocess.run(
|
||||
command,
|
||||
shell=True,
|
||||
capture_output=True,
|
||||
)
|
||||
if output.returncode != 0:
|
||||
raise RuntimeError(
|
||||
f'Failed to create user {username}: {output.stderr.decode()}'
|
||||
# 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}'
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
f'Added user {username} successfully. Output: [{output.stdout.decode()}]'
|
||||
)
|
||||
if not os.path.exists(self.initial_pwd):
|
||||
command += f' && mkdir -p {self.initial_pwd}'
|
||||
command += f' && chown -R {username}:root {self.initial_pwd}'
|
||||
command += f' && chmod g+s {self.initial_pwd}'
|
||||
|
||||
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.stderr.decode()}'
|
||||
)
|
||||
|
||||
def _init_bash_shell(self, work_dir: str, username: str) -> None:
|
||||
self.shell = pexpect.spawn(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user