From aad5ef5a17c6be49b4a7273f30d5f799d5c2b7c2 Mon Sep 17 00:00:00 2001 From: zhuyj17 Date: Mon, 27 May 2019 01:06:09 +0800 Subject: [PATCH] remove env variable in lxc conf hook to support 2.0.11 lxc version. --- conf/container.batch.conf | 6 +++--- conf/container.conf | 6 +++--- conf/lxc-script/lxc-ifdown | 2 ++ conf/lxc-script/lxc-ifup | 2 ++ conf/lxc-script/lxc-prestart | 4 +++- src/worker/container.py | 18 +++++++++++++++++- src/worker/taskworker.py | 17 +++++++++++++++-- 7 files changed, 45 insertions(+), 10 deletions(-) diff --git a/conf/container.batch.conf b/conf/container.batch.conf index 1746f18..fe7d086 100644 --- a/conf/container.batch.conf +++ b/conf/container.batch.conf @@ -25,8 +25,8 @@ lxc.network.type = veth lxc.network.name = eth0 # veth.pair is limited in 16 bytes lxc.network.veth.pair = %VETHPAIR% -lxc.network.script.up = Bridge=%BRNAME% %LXCSCRIPT%/lxc-ifup -lxc.network.script.down = Bridge=%BRNAME% %LXCSCRIPT%/lxc-ifdown +lxc.network.script.up = %LXCSCRIPT%/lxc-ifup +lxc.network.script.down = %LXCSCRIPT%/lxc-ifdown lxc.network.ipv4 = %IP% lxc.network.ipv4.gateway = %GATEWAY% lxc.network.flags = up @@ -50,7 +50,7 @@ lxc.mount.entry = %FS_PREFIX%/global/users/%USERNAME%/ssh %ROOTFS%/root/.ssh non lxc.mount.entry = %FS_PREFIX%/local/temp/%LXCNAME%/ %ROOTFS%/tmp none bind,rw,create=dir 0 0 # setting hostname -lxc.hook.pre-start = HNAME=%HOSTNAME% %LXCSCRIPT%/lxc-prestart +lxc.hook.pre-start = %LXCSCRIPT%/lxc-prestart # setting nfs softlink #lxc.hook.mount = %LXCSCRIPT%/lxc-mount diff --git a/conf/container.conf b/conf/container.conf index ab1a7a8..2ab8cd9 100644 --- a/conf/container.conf +++ b/conf/container.conf @@ -25,8 +25,8 @@ lxc.network.type = veth lxc.network.name = eth0 # veth.pair is limited in 16 bytes lxc.network.veth.pair = %VETHPAIR% -lxc.network.script.up = Bridge=docklet-br-%UserID% %LXCSCRIPT%/lxc-ifup -lxc.network.script.down = Bridge=docklet-br-%UserID% %LXCSCRIPT%/lxc-ifdown +lxc.network.script.up = %LXCSCRIPT%/lxc-ifup +lxc.network.script.down = %LXCSCRIPT%/lxc-ifdown lxc.network.ipv4 = %IP% lxc.network.ipv4.gateway = %GATEWAY% lxc.network.flags = up @@ -50,7 +50,7 @@ lxc.mount.entry = %FS_PREFIX%/global/users/%USERNAME%/ssh %ROOTFS%/root/.ssh non lxc.mount.entry = %FS_PREFIX%/local/temp/%LXCNAME%/ %ROOTFS%/tmp none bind,rw,create=dir 0 0 # setting hostname -lxc.hook.pre-start = HNAME=%HOSTNAME% %LXCSCRIPT%/lxc-prestart +lxc.hook.pre-start = %LXCSCRIPT%/lxc-prestart # setting nfs softlink #lxc.hook.mount = %LXCSCRIPT%/lxc-mount diff --git a/conf/lxc-script/lxc-ifdown b/conf/lxc-script/lxc-ifdown index 927c6d5..e9cb0ee 100755 --- a/conf/lxc-script/lxc-ifdown +++ b/conf/lxc-script/lxc-ifdown @@ -6,6 +6,8 @@ # $4 : network type, for example, veth # $5 : value of lxc.network.veth.pair +. $LXC_ROOTFS_PATH/../env.conf + ovs-vsctl --if-exists del-port $Bridge $5 cnt=$(ovs-vsctl list-ports ${Bridge} | wc -l) if [ "$cnt" = "1" ]; then diff --git a/conf/lxc-script/lxc-ifup b/conf/lxc-script/lxc-ifup index 17dacf7..06a514d 100755 --- a/conf/lxc-script/lxc-ifup +++ b/conf/lxc-script/lxc-ifup @@ -7,5 +7,7 @@ # $4 : network type, for example, veth # $5 : value of lxc.network.veth.pair +. $LXC_ROOTFS_PATH/../env.conf + ovs-vsctl --may-exist add-br $Bridge ovs-vsctl --may-exist add-port $Bridge $5 diff --git a/conf/lxc-script/lxc-prestart b/conf/lxc-script/lxc-prestart index 3e34c86..971d0db 100755 --- a/conf/lxc-script/lxc-prestart +++ b/conf/lxc-script/lxc-prestart @@ -10,7 +10,9 @@ # $LXC_ROOTFS_MOUNT: the path to the mounted root filesystem. # $LXC_CONFIG_FILE: the path to the container configuration file. # $LXC_SRC_NAME: in the case of the clone hook, this is the original container's name. -# $LXC_ROOTFS_PATH: this is the lxc.rootfs entry for the container. +# $LXC_ROOTFS_PATH: this is the lxc.rootfs entry for the container. # Note this is likely not where the mounted rootfs is to be found, use LXC_ROOTFS_MOUNT for that. +. $LXC_ROOTFS_PATH/../env.conf + echo $HNAME > $LXC_ROOTFS_PATH/etc/hostname diff --git a/src/worker/container.py b/src/worker/container.py index af891f4..71eec79 100755 --- a/src/worker/container.py +++ b/src/worker/container.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -import subprocess, os, json +import subprocess, os, json, traceback from utils.log import logger from utils import env, imagemgr from utils.lvmtool import sys_run, check_volume @@ -24,6 +24,17 @@ class Container(object): self.imgmgr = imagemgr.ImageMgr() self.historymgr = History_Manager() + def prepare_hook_conf(self, conf_path, env_dict): + try: + confile = open(conf_path, "w") + for k,v in env_dict.items(): + confile.write("%s=%s\n"%(k,v)) + confile.close() + except Exception as e: + logger.error(traceback.format_exc()) + return [False, e] + return [True, ""] + def create_container(self, lxc_name, proxy_server_ip, username, uid, setting, clustername, clusterid, containerid, hostname, ip, gateway, image): logger.info("create container %s of %s for %s" %(lxc_name, clustername, username)) try: @@ -85,6 +96,11 @@ class Container(object): conffile.write(conftext) conffile.close() + hook_env = {} + hook_env['Bridge'] = "docklet-br-%d" % uid + hook_env['HNAME'] = hostname + self.prepare_hook_conf(rootfs+"/../env.conf",hook_env) + #logger.debug(Ret.stdout.decode('utf-8')) logger.info("create container %s success" % lxc_name) diff --git a/src/worker/taskworker.py b/src/worker/taskworker.py index f0fa3f3..fb53a18 100755 --- a/src/worker/taskworker.py +++ b/src/worker/taskworker.py @@ -324,6 +324,16 @@ class TaskWorker(rpc_pb2_grpc.WorkerServicer): return rpc_pb2.Reply(status=rpc_pb2.Reply.ACCEPTED,message="") + def prepare_hook_conf(self, conf_path, env_dict): + try: + confile = open(conf_path, "w") + for k,v in env_dict.items(): + confile.write("%s=%s\n"%(k,v)) + confile.close() + except Exception as e: + logger.error(traceback.format_exc()) + return [False, e] + return [True, ""] #accquire ip and create a container def create_container(self,taskid,vnodeid,username,image,lxcname,quota,ipaddr,gateway,brname,hostname): @@ -353,7 +363,6 @@ class TaskWorker(rpc_pb2_grpc.WorkerServicer): content = content.replace("%LXCNAME%",lxcname) content = content.replace("%VETHPAIR%",str(taskid)+"-"+str(vnodeid)) content = content.replace("%IP%",ipaddr) - content = content.replace("%BRNAME%",brname) content = content.replace("%GATEWAY%",gateway) return content @@ -367,7 +376,11 @@ class TaskWorker(rpc_pb2_grpc.WorkerServicer): conffile = open("/var/lib/lxc/%s/config" % lxcname, 'w') conffile.write(conftext) conffile.close() - return [True, ""] + + hook_env = {} + hook_env['Bridge'] = brname + hook_env['HNAME'] = hostname + return self.prepare_hook_conf(rootfs+"/../env.conf",hook_env) def write_output(self,lxcname,tmplogpath,filepath): cmd = "lxc-attach -n " + lxcname + " -- mv %s %s"