summaryrefslogtreecommitdiff
path: root/tasks.py
diff options
context:
space:
mode:
authorankitjavalkar2017-11-17 17:13:26 +0530
committerankitjavalkar2018-01-04 11:37:20 +0530
commit2c057133d1b131a1573c93f0d0dbb548dcc8223e (patch)
tree351a7cd0b250de047307dcae2029b64d149ed4ba /tasks.py
parent9a871cee5d6836b82cc906ad74bc6731bb8e72f7 (diff)
downloadonline_test-2c057133d1b131a1573c93f0d0dbb548dcc8223e.tar.gz
online_test-2c057133d1b131a1573c93f0d0dbb548dcc8223e.tar.bz2
online_test-2c057133d1b131a1573c93f0d0dbb548dcc8223e.zip
Add dynamic run-as-admin commands to the base command depending on OS
Diffstat (limited to 'tasks.py')
-rw-r--r--tasks.py38
1 files changed, 32 insertions, 6 deletions
diff --git a/tasks.py b/tasks.py
index 68d9967..04a2ef5 100644
--- a/tasks.py
+++ b/tasks.py
@@ -9,6 +9,7 @@ TARGET_CONTAINER_NAME = 'yaksh_code_server'
SRC_IMAGE_NAME = 'fossee/yaksh_codeserver'
CHECK_FILE = 'server_running.txt'
CHECK_FILE_PATH = os.path.join(SCRIPT_DIR, 'yaksh_data', CHECK_FILE)
+OS_NAME = sys.platform
def create_dir(path):
@@ -19,6 +20,18 @@ def remove_check_file(path):
if os.path.isfile(path):
os.remove(path)
+def run_as(os_name):
+ if os_name.startswith('linux') or os_name == 'darwin':
+ return 'sudo'
+ elif os_name.startswith('Win32'):
+ return None
+
+def get_cmd(run_as_cmd, base_cmd):
+ if run_as_cmd:
+ return '{0} {1}'.format(run_as_cmd, base_cmd)
+ else:
+ return base_cmd
+
@task
def setupdb(ctx):
print("** Setting up & migrating database **")
@@ -43,7 +56,10 @@ def getimage(ctx, image=SRC_IMAGE_NAME):
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))
- ctx.run("sudo docker pull {0}".format(image))
+ base_cmd = "docker pull {0}".format(image)
+ run_as_cmd = run_as(OS_NAME)
+ cmd = get_cmd(run_as_cmd, base_cmd)
+ ctx.run(cmd)
print("\n** Done! Successfully pulled latest image <{0}> **".format(image))
@task
@@ -52,10 +68,12 @@ def start(ctx, ports=SERVER_POOL_PORT, image=SRC_IMAGE_NAME, unsafe=False,
if unsafe:
with ctx.cd(SCRIPT_DIR):
print("** Initializing local code server **")
- ctx.run("sudo python{0} -m yaksh.code_server".format(
- version
- )
+ base_cmd = "python{0} -m yaksh.code_server".format(
+ version
)
+ run_as_cmd = run_as(OS_NAME)
+ cmd = get_cmd(run_as_cmd, base_cmd)
+ ctx.run(cmd)
else:
cmd_params = {'ports': ports,
'image': SRC_IMAGE_NAME,
@@ -104,9 +122,17 @@ def stop(ctx, container=TARGET_CONTAINER_NAME, hide=True):
result = ctx.run("sudo docker ps -q --filter='name={0}'".format(container))
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)
+ ctx.run(cmd)
+ print ("** Done! Stopped the docker container <{0}>".format(container))
+
print ("** Discarding the docker container <{0}>".format(container))
- ctx.run("sudo docker stop {0}".format(container))
- ctx.run("sudo docker rm {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)
print ("** Done! Discarded the docker container <{0}>".format(container))
else:
print("** Docker container <{0}> not found **".format(container))