Use script to execute command

This commit is contained in:
zhuyj17 2018-07-30 17:16:26 +08:00
parent 82c59a9812
commit eae99bf275
2 changed files with 27 additions and 15 deletions

View File

@ -10,7 +10,7 @@ def run():
channel = grpc.insecure_channel('localhost:50051') channel = grpc.insecure_channel('localhost:50051')
stub = rpc_pb2_grpc.WorkerStub(channel) stub = rpc_pb2_grpc.WorkerStub(channel)
comm = rpc_pb2.Command(commandLine=r"echo \"s\" | awk '{print \"test\n\\\"\"}' > test.txt;cat test.txt", packagePath="/root", envVars={'test1':'10','test2':'20'}) # | awk '{print \"test\\\"\\n\"}' comm = rpc_pb2.Command(commandLine="echo \"s\" | awk '{print \"test\\n\\\"\"}' > test.txt;cat test.txt", packagePath="/root", envVars={'test1':'10','test2':'20'}) # | awk '{print \"test\\\"\\n\"}'
paras = rpc_pb2.Parameters(command=comm, stderrRedirectPath="", stdoutRedirectPath="") paras = rpc_pb2.Parameters(command=comm, stderrRedirectPath="", stdoutRedirectPath="")
img = rpc_pb2.Image(name="base", type=rpc_pb2.Image.BASE, owner="docklet") img = rpc_pb2.Image(name="base", type=rpc_pb2.Image.BASE, owner="docklet")
@ -18,7 +18,7 @@ def run():
mnt = rpc_pb2.Mount(localPath="",remotePath="") mnt = rpc_pb2.Mount(localPath="",remotePath="")
clu = rpc_pb2.Cluster(image=img, instance=inst, mount=[mnt]) clu = rpc_pb2.Cluster(image=img, instance=inst, mount=[mnt])
task = rpc_pb2.Task(id="test",username="root",instanceid=0,instanceCount=1,maxRetryCount=1,parameters=paras,cluster=clu,timeout=10) task = rpc_pb2.TaskInfo(id="test",username="root",instanceid=0,instanceCount=1,maxRetryCount=1,parameters=paras,cluster=clu,timeout=10,token="test")
response = stub.process_task(task) response = stub.process_task(task)
print("Batch client received: " + str(response.status)+" "+response.message) print("Batch client received: " + str(response.status)+" "+response.message)

View File

@ -14,7 +14,7 @@ from concurrent import futures
import grpc import grpc
#from utils.log import logger #from utils.log import logger
#from utils import env #from utils import env
import json,lxc,subprocess,threading,os,time import json,lxc,subprocess,threading,os,time,traceback
from utils import imagemgr from utils import imagemgr
from protos import rpc_pb2, rpc_pb2_grpc from protos import rpc_pb2, rpc_pb2_grpc
@ -152,19 +152,31 @@ class TaskController(rpc_pb2_grpc.WorkerServicer):
return rpc_pb2.Reply(status=rpc_pb2.Reply.ACCEPTED,message="") return rpc_pb2.Reply(status=rpc_pb2.Reply.ACCEPTED,message="")
def excute_task(self,taskid,instanceid,envs,lxcname,pkgpath,command,ip): def excute_task(self,taskid,instanceid,envs,lxcname,pkgpath,command,ip):
cmd = "lxc-attach -n " + lxcname lxcfspath = "/var/lib/lxc/"+lxcname+"/rootfs"
for envkey,envval in envs.items(): scriptname = "batch_job.sh"
cmd = cmd + " -v %s=%s" % (envkey,envval) try:
cmd = cmd + " -- /bin/bash -c " + "\"cd " + pkgpath + ";" + command + "\"" scriptfile = open(lxcfspath+"/root/"+scriptname,"w")
logger.info('run task with command - %s' % cmd) scriptfile.write("#!/bin/bash\n")
ret = subprocess.run(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True) scriptfile.write("cd "+str(pkgpath)+"\n")
logger.info(ret) scriptfile.write(command)
if ret.returncode == 0: scriptfile.close()
#call master rpc function to tell the taskmgr except Exception as err:
pass logger.error(traceback.format_exc())
logger.error("Fail to write script file with taskid(%s) instanceid(%s)" % (str(taskid),str(instanceid)))
else: else:
#call master rpc function to tell the wrong cmd = "lxc-attach -n " + lxcname
pass for envkey,envval in envs.items():
cmd = cmd + " -v %s=%s" % (envkey,envval)
cmd = cmd + " -- /bin/bash \"" + "/root/" + scriptname + "\""
logger.info('run task with command - %s' % cmd)
ret = subprocess.run(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
logger.info(ret)
if ret.returncode == 0:
#call master rpc function to tell the taskmgr
pass
else:
#call master rpc function to tell the wrong
pass
#umount oss here #umount oss here