From e521333b750abbf39f90c6011d693e3f6b15bdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Fri, 27 Mar 2020 09:59:11 +0100 Subject: [PATCH] Fix tls certificate generation in cron jobs tls renewal and generation was failing in cron jobs because of "The input device is not a TTY" errors. This is because the "-it" docker option does not work when a tty is not available. --- CHANGELOG.md | 4 ++++ tutor/commands/compose.py | 6 +++++- tutor/utils.py | 12 +++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f43c7eb..589b233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Note: Breaking changes between versions are indicated by "💥". +## Unreleased + +- [Bugfix] Fix tls certificate generation in cron jobs + ## v3.11.6 (2020-03-13) - [Bugfix] Fix "Unable to resolve dependency" error during forum initialisation diff --git a/tutor/commands/compose.py b/tutor/commands/compose.py index 5bb5bd5..e0f7b1e 100644 --- a/tutor/commands/compose.py +++ b/tutor/commands/compose.py @@ -3,6 +3,7 @@ import click from .. import config as tutor_config from .. import fmt from .. import scripts +from .. import utils class ScriptRunner(scripts.BaseRunner): @@ -90,7 +91,10 @@ def restart(context, services): @click.pass_obj def run(context, args): config = tutor_config.load(context.root) - context.docker_compose(context.root, config, "run", "--rm", *args) + command = ["run", "--rm"] + if not utils.is_a_tty(): + command.append("-T") + context.docker_compose(context.root, config, *command, *args) @click.command( diff --git a/tutor/utils.py b/tutor/utils.py index 979a652..03f73f2 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -4,6 +4,7 @@ import random import shutil import string import subprocess +import sys import click @@ -66,7 +67,10 @@ def walk_files(path): def docker_run(*command): - return docker("run", "--rm", "-it", *command) + args = ["run", "--rm"] + if is_a_tty(): + args.append("-it") + return docker(*args, *command) def docker(*command): @@ -92,6 +96,12 @@ def kubectl(*command): ) return execute("kubectl", *command) +def is_a_tty(): + """ + Return True if stdin is able to allocate a tty. Tty allocation sometimes cannot be + enabled, for instance in cron jobs + """ + return os.isatty(sys.stdin.fileno()) def execute(*command): click.echo(fmt.command(" ".join(command)))