add explicit non-interactive option to configure.py

This commit is contained in:
j3d1 2024-10-12 16:02:41 +02:00
parent 8d64a3c528
commit 9acf5a97e2

View file

@ -8,9 +8,18 @@ import dotenv
from django.db import transaction, IntegrityError from django.db import transaction, IntegrityError
def yesno(prompt, default=False): class CmdCtx:
if not sys.stdin.isatty():
def __init__(self, args):
self.args = args
def yesno(self, prompt, default=False):
if not sys.stdin.isatty() or self.args.noninteractive:
return default return default
elif self.args.yes:
return True
elif self.args.no:
return False
yes = {'yes', 'y', 'ye'} yes = {'yes', 'y', 'ye'}
no = {'no', 'n'} no = {'no', 'n'}
@ -31,9 +40,9 @@ def yesno(prompt, default=False):
print('Please respond with "yes" or "no"') print('Please respond with "yes" or "no"')
def configure(): def configure(ctx):
if not os.path.exists('.env'): if not os.path.exists('.env'):
if not yesno("the .env file does not exist, do you want to create it?", default=True): if not ctx.yesno("the .env file does not exist, do you want to create it?", default=True):
print('Aborting') print('Aborting')
exit(0) exit(0)
if not os.path.exists('.env.dist'): if not os.path.exists('.env.dist'):
@ -56,7 +65,7 @@ def configure():
current_hosts = os.getenv('ALLOWED_HOSTS') current_hosts = os.getenv('ALLOWED_HOSTS')
print('Current ALLOWED_HOSTS: {}'.format(current_hosts)) print('Current ALLOWED_HOSTS: {}'.format(current_hosts))
if yesno("Do you want to add ALLOWED_HOSTS?"): if ctx.yesno("Do you want to add ALLOWED_HOSTS?"):
hosts = input("Enter a comma-separated list of allowed hosts: ") hosts = input("Enter a comma-separated list of allowed hosts: ")
joined_hosts = current_hosts + ',' + hosts if current_hosts else hosts joined_hosts = current_hosts + ',' + hosts if current_hosts else hosts
dotenv.set_key('.env', 'ALLOWED_HOSTS', joined_hosts) dotenv.set_key('.env', 'ALLOWED_HOSTS', joined_hosts)
@ -67,20 +76,21 @@ def configure():
django.setup() django.setup()
if not os.path.exists('db.sqlite3'): if not os.path.exists('db.sqlite3'):
if not yesno("No database found, do you want to create one?", default=True): if not ctx.yesno("No database found, do you want to create one?", default=True):
print('Aborting') print('Aborting')
exit(0) exit(0)
from django.core.management import call_command from django.core.management import call_command
call_command('migrate') call_command('migrate')
if yesno("Do you want to create a superuser?"): if ctx.yesno("Do you want to create a superuser?"):
from django.core.management import call_command from django.core.management import call_command
call_command('createsuperuser') call_command('createsuperuser')
call_command('collectstatic', '--no-input') call_command('collectstatic', '--no-input')
if yesno("Do you want to import all categories, properties and tags contained in this repository?", default=True): if ctx.yesno("Do you want to import all categories, properties and tags contained in this repository?",
default=True):
from hostadmin.serializers import CategorySerializer, PropertySerializer, TagSerializer from hostadmin.serializers import CategorySerializer, PropertySerializer, TagSerializer
from hostadmin.models import ImportedIdentifierSets from hostadmin.models import ImportedIdentifierSets
from hashlib import sha256 from hashlib import sha256
@ -196,6 +206,7 @@ def main():
parser = ArgumentParser(description='Toolshed Server Configuration') parser = ArgumentParser(description='Toolshed Server Configuration')
parser.add_argument('--yes', '-y', help='Answer yes to all questions', action='store_true') parser.add_argument('--yes', '-y', help='Answer yes to all questions', action='store_true')
parser.add_argument('--no', '-n', help='Answer no to all questions', action='store_true') parser.add_argument('--no', '-n', help='Answer no to all questions', action='store_true')
parser.add_argument('--noninteractive', '-x', help="Run in noninteractive mode", action='store_true')
parser.add_argument('cmd', help='Command', default='configure', nargs='?') parser.add_argument('cmd', help='Command', default='configure', nargs='?')
args = parser.parse_args() args = parser.parse_args()
@ -203,8 +214,10 @@ def main():
print('Error: --yes and --no are mutually exclusive') print('Error: --yes and --no are mutually exclusive')
exit(1) exit(1)
ctx = CmdCtx(args)
if args.cmd == 'configure': if args.cmd == 'configure':
configure() configure(ctx)
elif args.cmd == 'reset': elif args.cmd == 'reset':
reset() reset()
elif args.cmd == 'testdata': elif args.cmd == 'testdata':