diff --git a/CHANGELOG.md b/CHANGELOG.md index e4c6267..4722677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/tutor/commands/cli.py b/tutor/commands/cli.py index 1935112..e5948c0 100755 --- a/tutor/commands/cli.py +++ b/tutor/commands/cli.py @@ -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) diff --git a/tutor/commands/dev.py b/tutor/commands/dev.py index 4a436e9..825f8c4 100644 --- a/tutor/commands/dev.py +++ b/tutor/commands/dev.py @@ -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 diff --git a/tutor/commands/images.py b/tutor/commands/images.py index 6d03efc..18d99cd 100644 --- a/tutor/commands/images.py +++ b/tutor/commands/images.py @@ -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) diff --git a/tutor/utils.py b/tutor/utils.py index a254700..c417121 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -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