Fix run as user support in Windows and add fall-back for created user folders

This commit is contained in:
allegroai 2020-03-22 19:16:11 +02:00
parent b3418e4496
commit 61232d05dd
2 changed files with 15 additions and 5 deletions

View File

@ -17,7 +17,7 @@ from datetime import datetime
from distutils.spawn import find_executable
from functools import partial
from itertools import chain
from tempfile import mkdtemp
from tempfile import mkdtemp, gettempdir
from time import sleep, time
from typing import Text, Optional, Any, Tuple
@ -2066,14 +2066,20 @@ class Worker(ServiceCommandSection):
os.setuid(self.uid)
# create a home folder for our user
trains_agent_home = 'trains_agent_home{}'.format('.'+str(Singleton.get_slot()) if Singleton.get_slot() else '')
try:
home_folder = '/trains_agent_home'
home_folder = (Path('/') / trains_agent_home).absolute().as_posix()
rm_tree(home_folder)
Path(home_folder).mkdir(parents=True, exist_ok=True)
except:
home_folder = '/home/trains_agent_home'
rm_tree(home_folder)
Path(home_folder).mkdir(parents=True, exist_ok=True)
try:
home_folder = (Path.home().parent / trains_agent_home).absolute().as_posix()
rm_tree(home_folder)
Path(home_folder).mkdir(parents=True, exist_ok=True)
except:
home_folder = (Path(gettempdir()) / trains_agent_home).absolute().as_posix()
rm_tree(home_folder)
Path(home_folder).mkdir(parents=True, exist_ok=True)
# move our entire venv into the new home
venv_folder = venv_folder.as_posix()

View File

@ -137,3 +137,7 @@ class Singleton(object):
if os.environ.get(ENV_K8S_HOST_MOUNT):
return os.environ.get(ENV_K8S_HOST_MOUNT).split(':')[-1]
return gettempdir()
@classmethod
def get_slot(cls):
return cls.instance_slot or 0