From 6f2e29a067cbeaf88b8ea478bb7c24fe8772180a Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 20 Nov 2017 16:07:43 +0530 Subject: Multiple Changes: - Add python-decouple package to requirements - use sane defaults and python-decouple in yaksh.settings and online_test.settings module - Replace hardcoded localhost domain name referring to code server with dynamic - Move Dockerfile to dedicated directory - Add dynamic run-as-admin commands to the base command depending on OS - Replace linux specific commands like cp with Python functions --- online_test/.env.sample | 16 +++++++++++++++ online_test/settings.py | 13 ++++++------ tasks.py | 54 ++++++++++++++++++++++++++++++------------------- yaksh/settings.py | 2 +- yaksh/views.py | 2 +- 5 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 online_test/.env.sample diff --git a/online_test/.env.sample b/online_test/.env.sample new file mode 100644 index 0000000..908ec72 --- /dev/null +++ b/online_test/.env.sample @@ -0,0 +1,16 @@ +# Django settings + +SECRET_KEY=dUmMy_s3cR3t_k3y +DB_ENGINE=sqlite3 +#DB_NAME=name_of_db +#DB_USER=name_of_db_user +#DB_PASSWORD=db_password +#DB_HOST=db_hostname +#DB_PORT=8180 + +# Yaksh settings + +#N_CODE_SERVERS=5 +#SERVER_POOL_PORT=55555 +#SERVER_HOST_NAME=http://localhost +#SERVER_TIMEOUT=4 diff --git a/online_test/settings.py b/online_test/settings.py index 3cd36a4..e64a791 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -23,7 +23,7 @@ OUTPUT_DIR = os.path.join(BASE_DIR, "yaksh_data", "output") # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = config('SECRET_KEY', default='dUmMy_s3cR3t_k3y')#'0=fsi3g5dw*7ze1cyh441_e^5^$2ay@&z(5(n7mhir0xb267=6' +SECRET_KEY = config('SECRET_KEY', default='dUmMy_s3cR3t_k3y') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -65,16 +65,17 @@ WSGI_APPLICATION = 'online_test.wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases - DATABASES = { 'default': { - #'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.{0}'.format( config('DB_ENGINE', default='sqlite3') ), - 'NAME': config('DB_NAME', default=os.path.join(BASE_DIR, 'db.sqlite3')) - #os.path.join(BASE_DIR, 'db.sqlite3'), - + 'NAME': config('DB_NAME', default=os.path.join(BASE_DIR, 'db.sqlite3')), + # The following settings are not used with sqlite3: + 'USER': config('DB_USER', default=''), + 'PASSWORD': config('DB_PASSWORD', default=''), + 'HOST': config('DB_HOST', default='localhost'), # Empty for localhost through domain sockets or '1$ + 'PORT': config('DB_PORT', default=''), }, } diff --git a/tasks.py b/tasks.py index 04a2ef5..47edb20 100644 --- a/tasks.py +++ b/tasks.py @@ -1,7 +1,13 @@ from __future__ import print_function +import os +import sys +import shutil +from distutils.dir_util import copy_tree +from distutils.file_util import copy_file + import invoke from invoke import task -import os + from yaksh.settings import SERVER_POOL_PORT SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) @@ -52,7 +58,10 @@ def clean(ctx): @task def getimage(ctx, image=SRC_IMAGE_NAME): try: - result = ctx.run("sudo docker inspect {0}".format(image), hide=True) + base_cmd = "docker inspect {0}".format(image) + run_as_cmd = run_as(OS_NAME) + cmd = get_cmd(run_as_cmd, base_cmd) + ctx.run(cmd, hide=True) except invoke.exceptions.Failure: print("The docker image {0} does not exist locally".format(image)) print("\n** Pulling latest image <{0}> from docker hub **".format(image)) @@ -92,47 +101,50 @@ def start(ctx, ports=SERVER_POOL_PORT, image=SRC_IMAGE_NAME, unsafe=False, create_dir(os.path.join(SCRIPT_DIR, 'yaksh_data', 'data')) create_dir(os.path.join(SCRIPT_DIR, 'yaksh_data', 'output')) - ctx.run('cp -r {0} {1}'.format( - os.path.join(SCRIPT_DIR, 'yaksh'), - os.path.join(SCRIPT_DIR, 'yaksh_data') - ) + copy_tree( + os.path.join(SCRIPT_DIR, 'yaksh'), + os.path.join(SCRIPT_DIR, 'yaksh_data', 'yaksh') ) - ctx.run('cp {0} {1}'.format( - os.path.join(SCRIPT_DIR, 'requirements', 'requirements-codeserver.txt'), - os.path.join(SCRIPT_DIR, 'yaksh_data') - ) + + copy_file( + os.path.join(SCRIPT_DIR, 'requirements', 'requirements-codeserver.txt'), + os.path.join(SCRIPT_DIR, 'yaksh_data') ) print("** Initializing code server within docker container **") - ctx.run( - "sudo docker run \ + base_cmd = "docker run \ -dp {ports}:{ports} --name={name} \ -v {vol_mount}:{vol_mount} \ -w {vol_mount} \ {image} {command}".format(**cmd_params) - ) + run_as_cmd = run_as(OS_NAME) + cmd = get_cmd(run_as_cmd, base_cmd) + ctx.run(cmd) while not os.path.isfile(CHECK_FILE_PATH): print("** Checking code server status. Press Ctrl-C to exit **\r", end="") - print("** Code server is up and running successfully **") + print("\n** Code server is up and running successfully **") @task def stop(ctx, container=TARGET_CONTAINER_NAME, hide=True): - result = ctx.run("sudo docker ps -q --filter='name={0}'".format(container)) + base_filter_cmd = "docker ps -q --filter='name={0}'".format(container) + run_as_cmd = run_as(OS_NAME) + cmd = get_cmd(run_as_cmd, base_filter_cmd) + result = ctx.run(cmd) + remove_check_file(CHECK_FILE_PATH) if result.stdout: print ("** Stopping the docker container <{0}>".format(container)) - base_cmd = "docker stop {0}".format(container) - run_as_cmd = run_as(OS_NAME) - cmd = get_cmd(run_as_cmd, base_cmd) + base_stop_cmd = "docker stop {0}".format(container) + cmd = get_cmd(run_as_cmd, base_stop_cmd) ctx.run(cmd) print ("** Done! Stopped the docker container <{0}>".format(container)) print ("** Discarding the docker container <{0}>".format(container)) - base_cmd = "docker rm {0}".format(container) - run_as_cmd = run_as(OS_NAME) - cmd = get_cmd(run_as_cmd, base_cmd) + base_rm_cmd = "docker rm {0}".format(container) + cmd = get_cmd(run_as_cmd, base_rm_cmd) + ctx.run(cmd) print ("** Done! Discarded the docker container <{0}>".format(container)) else: print("** Docker container <{0}> not found **".format(container)) diff --git a/yaksh/settings.py b/yaksh/settings.py index e414e75..d895d19 100644 --- a/yaksh/settings.py +++ b/yaksh/settings.py @@ -10,7 +10,7 @@ N_CODE_SERVERS = config('N_CODE_SERVERS', default=5, cast=int) # The server pool port. This is the server which returns available server # ports so as to minimize load. This is some random number where no other # service is running. It should be > 1024 and less < 65535 though. -SERVER_POOL_PORT = config('EMAIL_PORT', default=55555, cast=int) +SERVER_POOL_PORT = config('SERVER_POOL_PORT', default=55555, cast=int) SERVER_HOST_NAME = config('SERVER_HOST_NAME', default='http://localhost') #'localhost' diff --git a/yaksh/views.py b/yaksh/views.py index a001ffd..8fe4523 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -35,7 +35,7 @@ except ImportError: from io import BytesIO as string_io import re # Local imports. -from yaksh.code_server import get_result as get_result_from_code_server, +from yaksh.code_server import get_result as get_result_from_code_server from yaksh.models import ( Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, FloatTestCase, HookTestCase, IntegerTestCase, McqTestCase, Profile, -- cgit