Fix user ID checking under windows

This should make tutor portable on Windows. Well, if the `id -u` command
is supported at least...

See: https://github.com/overhangio/tutor/issues/381
This commit is contained in:
Régis Behmo 2020-11-07 17:48:20 +01:00
parent 998dec7149
commit 6c4d9619fd
5 changed files with 19 additions and 9 deletions

View File

@ -4,6 +4,7 @@ Note: Breaking changes between versions are indicated by "💥".
## Unreleased
- [Bugfix] Fix user ID checking under Windows.
- [Bugfix] Fix template rendering for Windows users.
- [Improvement] Switch to `bcrypt` for htpasswd password generation, for better portability on Windows.
- [Improvement] In the openedx production docker image, add some jitter to the gunicorn worker restart process to prevent all workers from restarting at the same time.

View File

@ -1,5 +1,4 @@
#! /usr/bin/env python3
import os
import sys
import appdirs
@ -19,6 +18,7 @@ from .webui import webui
from ..__about__ import __version__
from .. import exceptions
from .. import fmt
from .. import utils
def main():
@ -54,9 +54,11 @@ def main():
)
@click.pass_context
def cli(context, root):
if os.getuid() == 0:
if utils.get_user_id() == 0:
fmt.echo_alert(
"You are running Tutor as root. This is strongly not recommended. If you are doing this in order to access the Docker daemon, you should instead add your user to the 'docker' group. (see https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user)"
"You are running Tutor as root. This is strongly not recommended. If you are doing this in order to access"
" the Docker daemon, you should instead add your user to the 'docker' group. (see https://docs.docker.com"
"/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user)"
)
context.obj = Context(root)

View File

@ -9,6 +9,7 @@ from .. import env as tutor_env
from .. import fmt
from .. import utils
# pylint: disable=too-few-public-methods
class DevContext(Context):
@staticmethod

View File

@ -1,11 +1,10 @@
import subprocess
import click
from .. import config as tutor_config
from .. import env as tutor_env
from .. import images
from .. import plugins
from .. import utils
BASE_IMAGE_NAMES = ["openedx", "forum", "android"]
DEV_IMAGE_NAMES = ["openedx-dev"]
@ -93,8 +92,7 @@ def build_image(root, config, image, *args):
)
# Build dev images with user id argument
user_id = subprocess.check_output(["id", "-u"]).strip().decode()
dev_build_arg = ["--build-arg", "USERID={}".format(user_id)]
dev_build_arg = ["--build-arg", "USERID={}".format(utils.get_user_id())]
for img, tag in iter_images(config, image, DEV_IMAGE_NAMES):
images.build(tutor_env.pathjoin(root, "build", img), tag, *dev_build_arg, *args)

View File

@ -126,6 +126,15 @@ def walk_files(path):
yield os.path.join(dirpath, filename)
def get_user_id():
"""
Portable way to get user ID. Note: I have no idea if it actually works on windows...
"""
if sys.platform == "windows":
return int(check_output("id", "-u").decode())
return os.getuid()
def docker_run(*command):
args = ["run", "--rm"]
if is_a_tty():
@ -190,6 +199,5 @@ def check_output(*command):
click.echo(fmt.command(" ".join(command)))
try:
return subprocess.check_output(command)
except:
finally:
fmt.echo_error("Command failed: {}".format(" ".join(command)))
raise