Ejemplo n.º 1
0
    def student_view(self, context=None):

        # runtime error
        if not hasattr(self.runtime, "anonymous_student_id"):
            return self.message_view("Error in uc_docker (get anonymous student id)", "Cannot get anonymous_student_id in runtime", context)

        # preview in studio
        if self.runtime.anonymous_student_id == "student":

            result, message = GitLabUtil.get_user_projects(self.git_host, self.git_port, self.git_teacher_token)
            if not result:
                return self.message_view("Error in uc_docker (get git projects)", "Cannot get user's projects in git", context)

            context_dict = {
                "labs": self.labs,
                "message": ""
            }
            fragment = Fragment()
            fragment.add_content(Util.render_template('static/html/uc_lab.html', context_dict))
            fragment.add_css(Util.load_resource("static/css/uc_docker.css"))
            fragment.add_javascript(Util.load_resource("static/js/src/uc_lab.js"))
            fragment.initialize_js("UcDockerXBlock")
            return fragment

        # student view in open-edx
        if self.is_new:
            # create git account when first visiting
            student = self.runtime.get_real_user(self.runtime.anonymous_student_id)
            email = student.email
            name = student.first_name + " " + student.last_name
            username = student.username
            self.git_password = Util.create_random_password()
            # first_name, last_name are empty
            if name == " ":
                name = username
            self.logger.info("password is " + self.git_password)

            self.logger.info(self.git_host + "," + str(self.git_port) + "," + self.git_admin_token + "," + name + "," + username + "," + email + "," + self.git_password)
            result, message = GitLabUtil.create_account(self.git_host, self.git_port, self.git_admin_token, name, username, email, self.git_password)
            self.logger.info("create_account result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view("Error in uc_docker (create git account)", message, context)

            result, message = GitLabUtil.login(self.git_host, self.git_port, username, self.git_password)
            self.logger.info("login result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view("Error in uc_docker (login git account)", message, context)

            try:
                message = json.loads(message)
                self.git_id = message["id"]
                self.git_user_token = message["private_token"]
            except Exception, ex:
                return self.message_view("Error in uc_docker (load json string)", message, context)

            try:
                self.private_key, self.public_key = Util.gen_ssh_keys(email)
            except Exception, ex:
                return self.message_view("Error in uc_docker (gen ssh key)", ex, context)
Ejemplo n.º 2
0
    def student_view(self, context=None):

        # runtime error
        if not hasattr(self.runtime, "anonymous_student_id"):
            return self.message_view(
                "Error in ibm_docker (get anonymous student id)" +
                "Cannot get anonymous_student_id in runtime", None)

        # preview in studio
        if self.runtime.anonymous_student_id == "student":
            cluster_list = []

            conn = pymongo.Connection('localhost', 27017)
            db = conn.test
            db.authenticate(self.mongo_admin, self.mongo_pwd)
            cluster = db.cluster
            for i in cluster.find():
                new_cluster = Cluster()
                new_cluster.set_name(i["cluster_name"])
                new_cluster.set_user(i["username"])
                new_cluster.set_id(i["cluster_id"])
                new_cluster.set_creation_time(i["creation_time"])
                new_cluster.set_status(i["status"])
                new_cluster.set_ip(i["ip"])
                cluster_list.append(new_cluster.object_to_dict())

            context_dict = {"clusters": cluster_list, "message": ""}
            fragment = Fragment()
            fragment.add_content(
                Util.render_template('static/html/ibm_clusters.html',
                                     context_dict))
            fragment.add_css(Util.load_resource("static/css/ibm_docker.css"))
            #fragment.add_javascript(Util.load_resource("static/js/src/uc_lab.js"))
            fragment.initialize_js("IBMDockerTestXBlock")
            return fragment

        student = self.runtime.get_real_user(self.runtime.anonymous_student_id)
        email = student.email
        name = student.first_name + " " + student.last_name
        username = student.username

        # temporary code ,not used in next course
        conn = pymongo.Connection('localhost', 27017)
        db = conn.test
        db.authenticate(self.mongo_admin, self.mongo_pwd)
        token = db.token
        result = token.find_one({"username": username})
        conn.disconnect()

        #I dont now why the cluster cannot be shown in page directly
        temp_clusters = self.clusters

        if self.is_new and not result:
            # create git account when first visiting

            # get teacher id
            #result, message = GitLabUtil.get_user(self.git_host, self.git_port, self.git_teacher_token)
            #self.logger.info("get teacher id")
            #self.logger.info(result)
            #self.logger.info(message)
            #if not result:
            #    return self.message_view("Error in get teacher info")
            #try:
            #    message = json.loads(message)
            #    teacher_id = message["id"]
            #except Exception:
            #    return self.message_view("Error in uc_docker (load json string)", message, context)

            #add teacher to developer
            #result, message = GitLabUtil.add_project_developer(self.git_host, self.git_port, self.git_user_token, username, self.git_project_name, teacher_id)
            #self.logger.info("add developer result:")
            #self.logger.info(result)
            #self.logger.info(message)
            #if not result:
            #    return self.message_view("Error in uc_docker (add teacher to developer)", message, context)
            conn = pymongo.Connection('localhost', 27017)
            db = conn.test
            db.authenticate(self.mongo_admin, self.mongo_pwd)
            ibm = db.ibm
            result = ibm.find_one({"email": email})
            if not result:
                return self.message_view(
                    "Error in ibm_docker (get shibboleth account info). detail"
                    + message, temp_clusters)

            self.ldap_name = result["name"]
            self.ldap_pwd = result["password"]

            #get user id by email
            result, message = GitLabUtil.get_userid(self.git_host,
                                                    self.git_port,
                                                    self.git_admin_token,
                                                    self.ldap_name)
            self.logger.info("get user id result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in ibm_docker (get user id). detail" + message,
                    temp_clusters)
            self.git_id = message["id"]
            self.save()

            result, message = GitLabUtil.create_project(
                self.git_host, self.git_port, self.git_admin_token,
                self.git_import_url, self.git_project_name, self.git_id)
            self.logger.info("add project result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in ibm_docker (add project). detail:" + message,
                    temp_clusters)

            try:
                conn = pymongo.Connection('localhost', 27017)
                db = conn.test
                db.authenticate(self.mongo_admin, self.mongo_pwd)
                token = db.token
                result = token.find_one({"username": username})
                if not result:
                    self.private_key, self.public_key = Util.gen_ssh_keys(
                        email)
                    #self.logger.info("private_key:" + self.private_key)
                    self.save()
                    token.insert({
                        "username": username,
                        "private_key": self.private_key,
                        "public_key": self.public_key
                    })
                else:
                    self.private_key = result["private_key"]
                    self.public_key = result["public_key"]
                    self.save()
                conn.disconnect()

            except Exception, ex:
                return self.message_view(
                    "Error in ibm_docker (gen ssh key). detail:" + ex, context)

            result, message = GitLabUtil.add_ssh_key(
                self.git_host, self.git_port, self.git_admin_token,
                "ucore default", self.public_key, self.git_id)
            self.logger.info("add_ssh_key result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in ibm_docker (add git ssh key). detail:" + message,
                    temp_clusters)

            self.is_new = False
            self.save()
    def student_view(self, context=None):

        # runtime error
        if not hasattr(self.runtime, "anonymous_student_id"):
            return self.message_view(
                "Error in uc_docker (get anonymous student id)",
                "Cannot get anonymous_student_id in runtime", context)

        # preview in studio
        if self.runtime.anonymous_student_id == "student":

            result, message = GitLabUtil.get_user_projects(
                self.git_host, self.git_port, self.git_teacher_token)
            if not result:
                return self.message_view(
                    "Error in uc_docker (get git projects)",
                    "Cannot get user's projects in git", context)

            context_dict = {"labs": self.labs, "message": ""}
            fragment = Fragment()
            fragment.add_content(
                Util.render_template('static/html/uc_lab.html', context_dict))
            fragment.add_css(Util.load_resource("static/css/uc_docker.css"))
            fragment.add_javascript(
                Util.load_resource("static/js/src/uc_lab.js"))
            fragment.initialize_js("UcDockerXBlock")
            return fragment

        # student view in open-edx
        if self.is_new:
            # create git account when first visiting
            student = self.runtime.get_real_user(
                self.runtime.anonymous_student_id)
            email = student.email
            name = student.first_name + " " + student.last_name
            username = student.username
            self.git_password = Util.create_random_password()
            self.save()
            # first_name, last_name are empty
            if name == " ":
                name = username
            self.logger.info("password is " + self.git_password)

            # create ldap account
            l = ldap.initialize(self.ldap_url)
            l.bind(self.principal_name, self.ldap_password)
            dn = "uid=" + username + "," + self.base_dn

            attrs = {}
            attrs['objectclass'] = ['top', 'inetOrgPerson', 'eduPerson']
            attrs['cn'] = str(username)
            attrs['sn'] = str(username)
            attrs['givenName'] = str(username)
            attrs['uid'] = str(username)
            attrs['userPassword'] = str(self.git_password)
            attrs['description'] = 'ldap user for shibboleth'
            attrs['eduPersonPrincipalName'] = str(email)

            # Convert our dict to nice syntax for the add-function using modlist-module
            ldif = modlist.addModlist(attrs)
            l.add_s(dn, ldif)
            l.unbind_s()
            self.logger.info("create ldap account " + username + "," + dn)

            self.logger.info(self.git_host + "," + str(self.git_port) + "," +
                             self.git_admin_token + "," + name + "," +
                             username + "," + email + "," + self.git_password)
            result, message = GitLabUtil.create_account(
                self.git_host, self.git_port, self.git_admin_token, name,
                username, email, self.git_password)
            self.logger.info("create_account result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in uc_docker (create git account)", message,
                    context)

            result, message = GitLabUtil.login(self.git_host, self.git_port,
                                               username, self.git_password)
            self.logger.info("login result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in uc_docker (login git account)", message, context)

            try:
                message = json.loads(message)
                self.git_id = message["id"]
                self.git_user_token = message["private_token"]
                self.save()

            except Exception, ex:
                return self.message_view(
                    "Error in uc_docker (load json string)", message, context)

            try:
                self.private_key, self.public_key = Util.gen_ssh_keys(email)
                self.logger.info("private_key:" + self.private_key)
                self.save()
                conn = pymongo.Connection('localhost', 27017)
                db = conn.test
                token = db.token
                token.insert({
                    "username": username,
                    "token": message["private_token"],
                    "password": self.git_password,
                    "private_key": self.private_key,
                    "public_key": self.public_key
                })
                conn.disconnect()

            except Exception, ex:
                return self.message_view("Error in uc_docker (gen ssh key)",
                                         ex, context)
    def student_view(self, context=None):

        # runtime error
        if not hasattr(self.runtime, "anonymous_student_id"):
            return self.message_view("Error in uc_docker (get anonymous student id)", "Cannot get anonymous_student_id in runtime", context)

        # preview in studio
        if self.runtime.anonymous_student_id == "student":

            result, message = GitLabUtil.get_user_projects(self.git_host, self.git_port, self.git_teacher_token)
            if not result:
                return self.message_view("Error in uc_docker (get git projects)", "Cannot get user's projects in git", context)

            context_dict = {
                "labs": self.labs,
                "message": ""
            }
            fragment = Fragment()
            fragment.add_content(Util.render_template('static/html/uc_lab.html', context_dict))
            fragment.add_css(Util.load_resource("static/css/uc_docker.css"))
            fragment.add_javascript(Util.load_resource("static/js/src/uc_lab.js"))
            fragment.initialize_js("UcDockerXBlock")
            return fragment

        # student view in open-edx
        if self.is_new:
            # create git account when first visiting
            student = self.runtime.get_real_user(self.runtime.anonymous_student_id)
            email = student.email
            name = student.first_name + " " + student.last_name
            username = student.username
            self.git_password = Util.create_random_password()
            self.save()
            # first_name, last_name are empty
            if name == " ":
                name = username
            self.logger.info("password is " + self.git_password)

	    # create ldap account
	    l = ldap.initialize(self.ldap_url) 
	    l.bind(self.principal_name, self.ldap_password) 
	    dn= "uid=" + username + "," + self.base_dn

	    attrs = {}
	    attrs['objectclass'] = ['top','inetOrgPerson','eduPerson']
	    attrs['cn'] = str(username)
	    attrs['sn'] = str(username)
	    attrs['givenName'] = str(username)
	    attrs['uid'] = str(username)
	    attrs['userPassword'] = str(self.git_password)
	    attrs['description'] = 'ldap user for shibboleth'
	    attrs['eduPersonPrincipalName'] = str(email)

	    # Convert our dict to nice syntax for the add-function using modlist-module
	    ldif = modlist.addModlist(attrs)
	    l.add_s(dn,ldif)
	    l.unbind_s()
	    self.logger.info("create ldap account " + username + "," + dn)

            self.logger.info(self.git_host + "," + str(self.git_port) + "," + self.git_admin_token + "," + name + "," + username + "," + email + "," + self.git_password)
            result, message = GitLabUtil.create_account(self.git_host, self.git_port, self.git_admin_token, name, username, email, self.git_password)
            self.logger.info("create_account result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view("Error in uc_docker (create git account)", message, context)

            result, message = GitLabUtil.login(self.git_host, self.git_port, username, self.git_password)
            self.logger.info("login result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view("Error in uc_docker (login git account)", message, context)

            try:
                message = json.loads(message)
                self.git_id = message["id"]
                self.git_user_token = message["private_token"]
                self.save()
               
            except Exception, ex:
                return self.message_view("Error in uc_docker (load json string)", message, context)

            try:
                self.private_key, self.public_key = Util.gen_ssh_keys(email)
                self.logger.info("private_key:" + self.private_key)
                self.save()
                conn=pymongo.Connection('localhost', 27017)
                db = conn.test
                token=db.token
                token.insert({"username":username,"token":message["private_token"],"password":self.git_password,"private_key":self.private_key,"public_key":self.public_key})
                conn.disconnect()

            except Exception, ex:
                return self.message_view("Error in uc_docker (gen ssh key)", ex, context)
Ejemplo n.º 5
0
    def student_view(self, context=None):

        # runtime error
        if not hasattr(self.runtime, "anonymous_student_id"):
            return self.message_view(
                "Error in uc_docker (get anonymous student id)",
                "Cannot get anonymous_student_id in runtime", context)

        # preview in studio
        if self.runtime.anonymous_student_id == "student":

            result, message = GitLabUtil.get_user_projects(
                self.git_host, self.git_port, self.git_teacher_token)
            if not result:
                return self.message_view(
                    "Error in uc_docker (get git projects)",
                    "Cannot get user's projects in git", context)

            context_dict = {"labs": self.labs, "message": ""}
            fragment = Fragment()
            fragment.add_content(
                Util.render_template('static/html/uc_lab.html', context_dict))
            fragment.add_css(Util.load_resource("static/css/uc_docker.css"))
            fragment.add_javascript(
                Util.load_resource("static/js/src/uc_lab.js"))
            fragment.initialize_js("UcDockerXBlock")
            return fragment

        # student view in open-edx
        if self.is_new:
            # create git account when first visiting
            student = self.runtime.get_real_user(
                self.runtime.anonymous_student_id)
            email = student.email
            name = student.first_name + " " + student.last_name
            username = student.username
            self.git_password = Util.create_random_password()
            self.save()
            # first_name, last_name are empty
            if name == " ":
                name = username
            self.logger.info("password is " + self.git_password)

            self.logger.info(self.git_host + "," + str(self.git_port) + "," +
                             self.git_admin_token + "," + name + "," +
                             username + "," + email + "," + self.git_password)
            result, message = GitLabUtil.create_account(
                self.git_host, self.git_port, self.git_admin_token, name,
                username, email, self.git_password)
            self.logger.info("create_account result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in uc_docker (create git account)", message,
                    context)

            result, message = GitLabUtil.login(self.git_host, self.git_port,
                                               username, self.git_password)
            self.logger.info("login result:")
            self.logger.info(result)
            self.logger.info(message)
            if not result:
                return self.message_view(
                    "Error in uc_docker (login git account)", message, context)

            try:
                message = json.loads(message)
                self.git_id = message["id"]
                self.git_user_token = message["private_token"]
                self.save()

            except Exception, ex:
                return self.message_view(
                    "Error in uc_docker (load json string)", message, context)

            try:
                self.private_key, self.public_key = Util.gen_ssh_keys(email)
                self.logger.info("private_key:" + self.private_key)
                self.save()
                conn = pymongo.Connection('192.168.122.183', 27017)
                db = conn.test
                token = db.token
                token.insert({
                    "username": username,
                    "token": message["private_token"],
                    "password": self.git_password,
                    "private_key": self.private_key,
                    "public_key": self.public_key
                })
                conn.disconnect()

            except Exception, ex:
                return self.message_view("Error in uc_docker (gen ssh key)",
                                         ex, context)