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)
    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)
Exemple #4
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)