Esempio n. 1
0
def update_image():
    private_imagepath = fspath + "/global/images/private/"
    public_imagepath = fspath + "/global/images/public/"
    userdirs = os.listdir(private_imagepath)
    for userdir in userdirs:
        if os.path.isdir(private_imagepath + userdir + "/"):
            currentdir = private_imagepath + userdir + "/"
            images = os.listdir(currentdir)
            for image in images:
                if os.path.isdir(currentdir + image + "/"):
                    try:
                        sys_run(
                            "tar -cvf %s -C %s ." %
                            (currentdir + image + ".tz", currentdir + image))
                        #sys_run("rm -rf %s" % currentdir+image)
                    except Exception as e:
                        print(e)
    userdirs = os.listdir(public_imagepath)
    for userdir in userdirs:
        if os.path.isdir(public_imagepath + userdir + "/"):
            currentdir = public_imagepath + userdir + "/"
            images = os.listdir(currentdir)
            for image in images:
                if os.path.isdir(currentdir + image + "/"):
                    try:
                        sys_run(
                            "tar -cvf %s -C %s ." %
                            (currentdir + image + ".tz", currentdir + image))
                        #sys_run("rm -rf %s" % currentdir+image)
                    except Exception as e:
                        print(e)
Esempio n. 2
0
def update_image():
    private_imagepath = fspath + "/global/images/private/"
    public_imagepath = fspath + "/global/images/public/"
    userdirs = os.listdir(private_imagepath)
    for userdir in userdirs:
        if os.path.isdir(private_imagepath+userdir+"/"):
            currentdir = private_imagepath+userdir+"/"
            images = os.listdir(currentdir)
            for image in images:
                if os.path.isdir(currentdir+image+"/"):
                    try:
                        sys_run("tar -cvf %s -C %s ." % (currentdir+image+".tz",currentdir+image))
                        #sys_run("rm -rf %s" % currentdir+image)
                    except Exception as e:
                        print(e)
    userdirs = os.listdir(public_imagepath)
    for userdir in userdirs:
        if os.path.isdir(public_imagepath+userdir+"/"):
            currentdir = public_imagepath+userdir+"/"
            images = os.listdir(currentdir)
            for image in images:
                if os.path.isdir(currentdir+image+"/"):
                    try:
                        sys_run("tar -cvf %s -C %s ." % (currentdir+image+".tz",currentdir+image))
                        #sys_run("rm -rf %s" % currentdir+image)
                    except Exception as e:
                        print(e)
Esempio n. 3
0
 def stop_container(self, lxc_name):
     logger.info("stop container:%s" % lxc_name)
     #status = subprocess.call([self.libpath+"/lxc_control.sh", "stop", lxc_name])
     [success, status] = self.container_status(lxc_name)
     if not success:
         return [False, status]
     if status == "running":
         sys_run("lxc-stop -k -n %s" % lxc_name)
     [success, status] = self.container_status(lxc_name)
     if status == "running":
         logger.error("stop container %s failed" % lxc_name)
         return [False, "stop container failed"]
     else:
         logger.info("stop container %s success" % lxc_name)
         return [True, "stop container success"]
Esempio n. 4
0
 def stop_container(self, lxc_name):
     logger.info ("stop container:%s" % lxc_name)
     #status = subprocess.call([self.libpath+"/lxc_control.sh", "stop", lxc_name])
     [success, status] = self.container_status(lxc_name)
     if not success:
         return [False, status]
     if status == "running":
         sys_run("lxc-stop -k -n %s" % lxc_name)
     [success, status] = self.container_status(lxc_name)
     if status == "running":
         logger.error("stop container %s failed" % lxc_name)
         return [False, "stop container failed"]
     else:
         logger.info("stop container %s success" % lxc_name)
         return [True, "stop container success"]
Esempio n. 5
0
 def container_status(self, lxc_name):
     if not self.is_container(lxc_name):
         return [False, "container not found"]
     Ret = sys_run("lxc-info -n %s | grep RUNNING" % lxc_name)
     #status = subprocess.call([self.libpath+"/lxc_control.sh", "status", lxc_name])
     if Ret.returncode == 0:
         return [True, 'running']
     else:
         return [True, 'stopped']
Esempio n. 6
0
 def container_status(self, lxc_name):
     if not self.is_container(lxc_name):
         return [False, "container not found"]
     Ret = sys_run("lxc-info -n %s | grep RUNNING" % lxc_name)
     #status = subprocess.call([self.libpath+"/lxc_control.sh", "status", lxc_name])
     if Ret.returncode == 0:
         return [True, 'running']
     else:
         return [True, 'stopped']
Esempio n. 7
0
def enable_gluster_quota():
    conffile = open("../conf/docklet.conf", 'r')
    conf = conffile.readlines()
    conffile.close()
    enable = False
    volume_name = ""
    for line in conf:
        if line.startswith("DATA_QUOTA"):
            keyvalue = line.split("=")
            if len(keyvalue) < 2:
                continue
            key = keyvalue[0].strip()
            value = keyvalue[1].strip()
            if value == "YES":
                enable = True
                break
    for line in conf:
        if line.startswith("DATA_QUOTA_CMD"):
            keyvalue = line.split("=")
            if len(keyvalue) < 2:
                continue
            volume_name = keyvalue[1].strip()
    if not enable:
        print("don't need to enable the quota")
        return

    users = User.query.all()
    quotas = allquota()
    if quotaquery == None:
        print("quota info not found")
        return
    sys_run("gluster volume quota %s enable" % volume_name)
    for user in users:
        quota = quotaquery(user.user_group, quotas)
        nfs_quota = quota['data']
        if nfs_quota == None:
            print("data quota should be set")
            return
        nfspath = "/users/%s/data" % user.username
        sys_run("gluster volume quota %s limit-usage %s %sGB" %
                (volume_name, nfspath, nfs_quota))
Esempio n. 8
0
def enable_gluster_quota():
    conffile=open("../conf/docklet.conf",'r')
    conf=conffile.readlines()
    conffile.close()
    enable = False
    volume_name = ""
    for line in conf:
        if line.startswith("DATA_QUOTA"):
            keyvalue = line.split("=")
            if len(keyvalue) < 2:
                continue
            key = keyvalue[0].strip()
            value = keyvalue[1].strip()
            if value == "YES":
                enable = True
                break
    for line in conf:
        if line.startswith("DATA_QUOTA_CMD"):
            keyvalue = line.split("=")
            if len(keyvalue) < 2:
                continue
            volume_name = keyvalue[1].strip()
    if not enable:
        print("don't need to enable the quota")
        return
    
    users = User.query.all()
    quotas = allquota()
    if quotaquery == None:
        print("quota info not found")
        return
    sys_run("gluster volume quota %s enable" % volume_name)
    for user in users:
        quota = quotaquery(user.user_group, quotas)
        nfs_quota = quota['data']
        if nfs_quota == None:
            print("data quota should be set")
            return
        nfspath = "/users/%s/data" % user.username
        sys_run("gluster volume quota %s limit-usage %s %sGB" % (volume_name,nfspath,nfs_quota))
Esempio n. 9
0
    def create_container(self, lxc_name, username, setting, clustername, clusterid, containerid, hostname, ip, gateway, vlanid, image):
        logger.info("create container %s of %s for %s" %(lxc_name, clustername, username))
        try:
            setting = json.loads(setting)
            cpu = int(setting['cpu']) * 100000
            memory = setting["memory"]
            disk = setting["disk"]
            image = json.loads(image) 
            status = self.imgmgr.prepareFS(username,image,lxc_name,disk)
            if not status:
                return [False, "Create container failed when preparing filesystem, possibly insufficient space"]
            
            #Ret = subprocess.run([self.libpath+"/lxc_control.sh",
            #    "create", lxc_name, username, str(clusterid), hostname,
            #    ip, gateway, str(vlanid), str(cpu), str(memory)], stdout=subprocess.PIPE,
            #    stderr=subprocess.STDOUT,shell=False, check=True)
            
            rootfs = "/var/lib/lxc/%s/rootfs" % lxc_name
            
            if not os.path.isdir("%s/global/users/%s" % (self.fspath,username)):
                logger.error("user %s directory not found" % username)
                return [False, "user directory not found"]
            sys_run("mkdir -p /var/lib/lxc/%s" % lxc_name)
            logger.info("generate config file for %s" % lxc_name)
            
            def config_prepare(content):
                content = content.replace("%ROOTFS%",rootfs)
                content = content.replace("%HOSTNAME%",hostname)
                content = content.replace("%IP%",ip)
                content = content.replace("%GATEWAY%",gateway)
                content = content.replace("%CONTAINER_MEMORY%",str(memory))
                content = content.replace("%CONTAINER_CPU%",str(cpu))
                content = content.replace("%FS_PREFIX%",self.fspath)
                content = content.replace("%USERNAME%",username)
                content = content.replace("%CLUSTERID%",str(clusterid))
                content = content.replace("%LXCSCRIPT%",env.getenv("LXC_SCRIPT"))
                content = content.replace("%LXCNAME%",lxc_name)
                content = content.replace("%VLANID%",str(vlanid))
                content = content.replace("%CLUSTERNAME%", clustername)
                content = content.replace("%VETHPAIR%", str(clusterid)+'-'+str(containerid))
                return content

            conffile = open(self.confpath+"/container.conf", 'r')
            conftext = conffile.read()
            conffile.close()
            conftext = config_prepare(conftext)

            conffile = open("/var/lib/lxc/%s/config" % lxc_name,"w")
            conffile.write(conftext)
            conffile.close()

            if os.path.isfile(self.confpath+"/lxc.custom.conf"):
                conffile = open(self.confpath+"/lxc.custom.conf", 'r')
                conftext = conffile.read()
                conffile.close()
                conftext = config_prepare(conftext)
                conffile = open("/var/lib/lxc/%s/config" % lxc_name, 'a')
                conffile.write(conftext)
                conffile.close()

            #logger.debug(Ret.stdout.decode('utf-8'))
            logger.info("create container %s success" % lxc_name)

            # get AUTH COOKIE URL for jupyter
            [status, authurl] = self.etcd.getkey("web/authurl")
            if not status:
                [status, masterip] = self.etcd.getkey("service/master")
                if status:
                    webport = env.getenv("WEB_PORT")
                    authurl = "http://%s:%s/jupyter" % (masterip,
                            webport)
                else:
                    logger.error ("get AUTH COOKIE URL failed for jupyter")
                    authurl = "error"
            
            cookiename='docklet-jupyter-cookie'

            rundir = self.lxcpath+'/'+lxc_name+'/rootfs' + self.rundir

            logger.debug(rundir)

            if not os.path.exists(rundir):
                os.makedirs(rundir)
            else:
                if not os.path.isdir(rundir):
                    os.remove(rundir)
                    os.makedirs(rundir)

            jconfigpath = rundir + '/jupyter.config'
            config = open(jconfigpath, 'w')
            jconfigs="""USER=%s
PORT=%d
COOKIE_NAME=%s
BASE_URL=%s
HUB_PREFIX=%s
HUB_API_URL=%s
IP=%s
""" % (username, 10000, cookiename, '/go/'+username+'/'+clustername, '/jupyter',
        authurl, ip.split('/')[0])
            config.write(jconfigs)
            config.close()

        except subprocess.CalledProcessError as sube:
            logger.error('create container %s failed: %s' % (lxc_name,
                    sube.stdout.decode('utf-8')))
            return [False, "create container failed"]
        except Exception as e:
            logger.error(e)
            return [False, "create container failed"]
        self.historymgr.log(lxc_name,"Create")
        return [True, "create container success"]
Esempio n. 10
0
    def create_container(self, lxc_name, username, setting, clustername, clusterid, containerid, hostname, ip, gateway, vlanid, image, allocation_type):
        logger.info("create container %s of %s for %s" %(lxc_name, clustername, username))
        try:
            setting = json.loads(setting)
            cpu = int(setting['cpu']) * 100000
            memory = setting["memory"]
            disk = setting["disk"]

            image = json.loads(image) 
            status = self.imgmgr.prepareFS(username,image,lxc_name,disk)
            if not status:
                return [False, "Create container failed when preparing filesystem, possibly insufficient space"]
            
            #Ret = subprocess.run([self.libpath+"/lxc_control.sh",
            #    "create", lxc_name, username, str(clusterid), hostname,
            #    ip, gateway, str(vlanid), str(cpu), str(memory)], stdout=subprocess.PIPE,
            #    stderr=subprocess.STDOUT,shell=False, check=True)
            

            rootfs = "/var/lib/lxc/%s/rootfs" % lxc_name
            configuration['rootfs']=rootfs
            
            if not os.path.isdir("%s/global/users/%s" % (self.fspath,username)):
                logger.error("user %s directory not found" % username)
                return [False, "user directory not found"]
            sys_run("mkdir -p /var/lib/lxc/%s" % lxc_name)
            logger.info("generate config file for %s" % lxc_name)

            self.set_cgroup_settings(configuration)
            
            #logger.debug(Ret.stdout.decode('utf-8'))
            logger.info("create container %s success" % lxc_name)

            # get AUTH COOKIE URL for jupyter
            [status, authurl] = self.etcd.getkey("web/authurl")
            if not status:
                [status, masterip] = self.etcd.getkey("service/master")
                if status:
                    webport = env.getenv("WEB_PORT")
                    authurl = "http://%s:%s/jupyter" % (masterip,
                            webport)
                else:
                    logger.error ("get AUTH COOKIE URL failed for jupyter")
                    authurl = "error"
            
            cookiename='docklet-jupyter-cookie'

            rundir = self.lxcpath+'/'+lxc_name+'/rootfs' + self.rundir

            logger.debug(rundir)

            if not os.path.exists(rundir):
                os.makedirs(rundir)
            else:
                if not os.path.isdir(rundir):
                    os.remove(rundir)
                    os.makedirs(rundir)

            jconfigpath = rundir + '/jupyter.config'
            config = open(jconfigpath, 'w')
            jconfigs="""USER=%s
PORT=%d
COOKIE_NAME=%s
BASE_URL=%s
HUB_PREFIX=%s
HUB_API_URL=%s
IP=%s
""" % (username, 10000, cookiename, '/go/'+username+'/'+clustername, '/jupyter',
        authurl, ip.split('/')[0])
            config.write(jconfigs)
            config.close()

        except subprocess.CalledProcessError as sube:
            logger.error('create container %s failed: %s' % (lxc_name,
                    sube.stdout.decode('utf-8')))
            return [False, "create container failed"]
        except Exception as e:
            logger.error(e)
            return [False, "create container failed"]
        self.historymgr.log(lxc_name,"Create")
        return [True, "create container success"]
Esempio n. 11
0
    def create_container(self, lxc_name, username, user_info, clustername,
                         clusterid, containerid, hostname, ip, gateway, vlanid,
                         image):
        logger.info("create container %s of %s for %s" %
                    (lxc_name, clustername, username))
        try:
            user_info = json.loads(user_info)
            cpu = user_info["data"]["groupinfo"]["cpu"]
            memory = user_info["data"]["groupinfo"]["memory"]
            disk = user_info["data"]["groupinfo"]["disk"]
            image = json.loads(image)
            status = self.imgmgr.prepareFS(username, image, lxc_name, disk)
            if not status:
                return [
                    False,
                    "Create container failed when preparing filesystem, possibly insufficient space"
                ]

            #Ret = subprocess.run([self.libpath+"/lxc_control.sh",
            #    "create", lxc_name, username, str(clusterid), hostname,
            #    ip, gateway, str(vlanid), str(cpu), str(memory)], stdout=subprocess.PIPE,
            #    stderr=subprocess.STDOUT,shell=False, check=True)

            rootfs = "/var/lib/lxc/%s/rootfs" % lxc_name

            if not os.path.isdir("%s/global/users/%s" %
                                 (self.fspath, username)):
                logger.error("user %s directory not found" % username)
                return [False, "user directory not found"]
            sys_run("mkdir -p /var/lib/lxc/%s" % lxc_name)
            logger.info("generate config file for %s" % lxc_name)

            def config_prepare(content):
                content = content.replace("%ROOTFS%", rootfs)
                content = content.replace("%HOSTNAME%", hostname)
                content = content.replace("%IP%", ip)
                content = content.replace("%GATEWAY%", gateway)
                content = content.replace("%CONTAINER_MEMORY%", str(memory))
                content = content.replace("%CONTAINER_CPU%", str(cpu))
                content = content.replace("%FS_PREFIX%", self.fspath)
                content = content.replace("%USERNAME%", username)
                content = content.replace("%CLUSTERID%", str(clusterid))
                content = content.replace("%LXCSCRIPT%",
                                          env.getenv("LXC_SCRIPT"))
                content = content.replace("%LXCNAME%", lxc_name)
                content = content.replace("%VLANID%", str(vlanid))
                content = content.replace("%CLUSTERNAME%", clustername)
                content = content.replace(
                    "%VETHPAIR%",
                    str(clusterid) + '-' + str(containerid))
                return content

            conffile = open(self.confpath + "/container.conf", 'r')
            conftext = conffile.read()
            conffile.close()
            conftext = config_prepare(conftext)

            conffile = open("/var/lib/lxc/%s/config" % lxc_name, "w")
            conffile.write(conftext)
            conffile.close()

            if os.path.isfile(self.confpath + "/lxc.custom.conf"):
                conffile = open(self.confpath + "/lxc.custom.conf", 'r')
                conftext = conffile.read()
                conffile.close()
                conftext = config_prepare(conftext)
                conffile = open("/var/lib/lxc/%s/config" % lxc_name, 'a')
                conffile.write(conftext)
                conffile.close()

            #logger.debug(Ret.stdout.decode('utf-8'))
            logger.info("create container %s success" % lxc_name)

            # get AUTH COOKIE URL for jupyter
            [status, authurl] = self.etcd.getkey("web/authurl")
            if not status:
                [status, masterip] = self.etcd.getkey("service/master")
                if status:
                    webport = env.getenv("WEB_PORT")
                    authurl = "http://%s:%s/jupyter" % (masterip, webport)
                else:
                    logger.error("get AUTH COOKIE URL failed for jupyter")
                    authurl = "error"
            if (username == 'guest'):
                cookiename = 'guest-cookie'
            else:
                cookiename = 'docklet-jupyter-cookie'

            rundir = self.lxcpath + '/' + lxc_name + '/rootfs' + self.rundir

            logger.debug(rundir)

            if not os.path.exists(rundir):
                os.makedirs(rundir)
            else:
                if not os.path.isdir(rundir):
                    os.remove(rundir)
                    os.makedirs(rundir)

            jconfigpath = rundir + '/jupyter.config'
            config = open(jconfigpath, 'w')
            jconfigs = """USER=%s
PORT=%d
COOKIE_NAME=%s
BASE_URL=%s
HUB_PREFIX=%s
HUB_API_URL=%s
IP=%s
""" % (username, 10000, cookiename, '/go/' + username + '/' + clustername,
            '/jupyter', authurl, ip.split('/')[0])
            config.write(jconfigs)
            config.close()

        except subprocess.CalledProcessError as sube:
            logger.error('create container %s failed: %s' %
                         (lxc_name, sube.stdout.decode('utf-8')))
            return [False, "create container failed"]
        except Exception as e:
            logger.error(e)
            return [False, "create container failed"]
        return [True, "create container success"]
Esempio n. 12
0
    def create_container(self, configuration):
        lxc_name = configuration['lxc_name']
        username = configuration['username']
        user_info = configuration['user_info']
        clustername = configuration['clustername']
        clusterid = configuration['clusterid']
        containerid = configuration['containerid']
        hostname = configuration['hostname']
        ip = configuration['ip']
        gateway = configuration['gateway']
        vlanid = configuration['vlanid']
        image = configuration['image']

        logger.info("create container %s of %s for %s" %
                    (lxc_name, clustername, username))
        try:
            user_info = json.loads(user_info)
            #            cpu = int(user_info["data"]["groupinfo"]["cpu"]) * 100000
            #            memory = user_info["data"]["groupinfo"]["memory"]
            disk = user_info["data"]["groupinfo"]["disk"]
            image = json.loads(image)
            status = self.imgmgr.prepareFS(username, image, lxc_name, disk)
            if not status:
                return [
                    False,
                    "Create container failed when preparing filesystem, possibly insufficient space"
                ]

            #Ret = subprocess.run([self.libpath+"/lxc_control.sh",
            #    "create", lxc_name, username, str(clusterid), hostname,
            #    ip, gateway, str(vlanid), str(cpu), str(memory)], stdout=subprocess.PIPE,
            #    stderr=subprocess.STDOUT,shell=False, check=True)

            rootfs = "/var/lib/lxc/%s/rootfs" % lxc_name
            configuration['rootfs'] = rootfs

            if not os.path.isdir("%s/global/users/%s" %
                                 (self.fspath, username)):
                logger.error("user %s directory not found" % username)
                return [False, "user directory not found"]
            sys_run("mkdir -p /var/lib/lxc/%s" % lxc_name)
            logger.info("generate config file for %s" % lxc_name)

            self.set_cgroup_settings(configuration)

            #logger.debug(Ret.stdout.decode('utf-8'))
            logger.info("create container %s success" % lxc_name)

            # get AUTH COOKIE URL for jupyter
            [status, authurl] = self.etcd.getkey("web/authurl")
            if not status:
                [status, masterip] = self.etcd.getkey("service/master")
                if status:
                    webport = env.getenv("WEB_PORT")
                    authurl = "http://%s:%s/jupyter" % (masterip, webport)
                else:
                    logger.error("get AUTH COOKIE URL failed for jupyter")
                    authurl = "error"

            cookiename = 'docklet-jupyter-cookie'

            rundir = self.lxcpath + '/' + lxc_name + '/rootfs' + self.rundir

            logger.debug(rundir)

            if not os.path.exists(rundir):
                os.makedirs(rundir)
            else:
                if not os.path.isdir(rundir):
                    os.remove(rundir)
                    os.makedirs(rundir)

            jconfigpath = rundir + '/jupyter.config'
            config = open(jconfigpath, 'w')
            jconfigs = """USER=%s
PORT=%d
COOKIE_NAME=%s
BASE_URL=%s
HUB_PREFIX=%s
HUB_API_URL=%s
IP=%s
""" % (username, 10000, cookiename, '/go/' + username + '/' + clustername,
            '/jupyter', authurl, ip.split('/')[0])
            config.write(jconfigs)
            config.close()

        except subprocess.CalledProcessError as sube:
            logger.error('create container %s failed: %s' %
                         (lxc_name, sube.stdout.decode('utf-8')))
            return [False, "create container failed"]
        except Exception as e:
            logger.error(e)
            return [False, "create container failed"]
        return [True, "create container success"]