def studio_view(self, context=None): fragment = Fragment() fragment.add_content(Util.render_template("static/html/uc_rtc_studio.html")) fragment.add_css(Util.load_resource("static/css/uc_rtc.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_rtc_edit.js")) fragment.initialize_js("UcRtcXBlock") return fragment
def studio_view(self, context=None): fragment = Fragment() fragment.add_content(Util.render_template('static/html/uc_rtc_studio.html')) fragment.add_css(Util.load_resource("static/css/uc_rtc.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_rtc_edit.js")) fragment.initialize_js("UcRtcXBlock") return fragment
def message_view(self, title, message, context=None): context_dict = {"title": title, "message": message} fragment = Fragment() fragment.add_content(Util.render_template("static/html/uc_message.html", context_dict)) fragment.add_css(Util.load_resource("static/css/uc_rtc.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_null.js")) fragment.initialize_js("UcRtcXBlock") return fragment
def message_view(self, title, message, context=None): context_dict = { "title": title, "message": message } fragment = Fragment() fragment.add_content(Util.render_template('static/html/message_view.html', context_dict)) fragment.initialize_js("MyXBlock") return fragment
def message_view(self, title, message, context=None): context_dict = {"title": title, "message": message} fragment = Fragment() fragment.add_content( Util.render_template('static/html/uc_message.html', context_dict)) fragment.add_css(Util.load_resource("static/css/uc_docker.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_null.js")) fragment.initialize_js("UcDockerXBlock") return fragment
def student_view(self, context=None): """ The primary view of the MyXBlock, shown to students when viewing courses. """ # runtime error if not hasattr(self.runtime, "anonymous_student_id"): return self.message_view("Error (get anonymous student id)", "Cannot get anonymous_student_id in runtime", context) if self.runtime.anonymous_student_id == "student": context_dict = { "username": "", "email":"", "url":self.src } fragment = Fragment() fragment.add_content(Util.render_template('static/html/myxblock.html', context_dict)) fragment.add_css(Util.load_resource("static/css/myxblock.css")) fragment.add_javascript(Util.load_resource("static/js/src/myxblock.js")) fragment.initialize_js("MyXBlock") 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 if name == " ": name = username context_dict = { "username": username, "email":email, "url":self.src } fragment = Fragment() fragment.add_content(Util.render_template('static/html/myxblock.html', context_dict)) fragment.add_css(Util.load_resource("static/css/myxblock.css")) fragment.add_javascript(Util.load_resource("static/js/src/myxblock.js")) fragment.initialize_js("MyXBlock") return fragment
def studio_view(self, context=None): context_dict = { "title": "", "message": self.src } fragment = Fragment() fragment.add_content(Util.render_template('static/html/studio_view_new.html', context_dict)) fragment.add_css(Util.load_resource("static/css/studio.css")) fragment.add_javascript(Util.load_resource("static/js/src/myxblock.js")) fragment.initialize_js("MyXBlock") return fragment
class JennystartXBlock(XBlock): """ TO-DO: document what your XBlock does. """ # Fields are defined on the class. You can access them in your code as # self.<fieldname>. # TO-DO: delete count, and define your own fields. codeData = String(default="", scope=Scope.user_state, help="codeData") file_path=String(default="", scope=Scope.user_state, help="file_path") logger = Util .uc_logger() # TO-DO: change this view to display your data your own way. def student_view(self, context=None): """ The primary view of the JennystartXBlock, shown to students when viewing courses. """ student_id=self.runtime.anonymous_student_id base_path="/edx/var/edxapp/staticfiles/ucore/" real_user = self.runtime.get_real_user(student_id) username = real_user.username conn = pymongo.Connection('localhost', 27017) db = conn.test codeview = db.codeview result = user.find_one({"username":username}) conn.disconnect() if result: relative_path = result["view_file"] else: relative_path = "please enter file path" context_dict={"file":relative_path} fragment = Fragment() fragment.add_content(Util.render_template("static/html/jennystart.html",context_dict) ) fragment.add_css(Util.load_resource("static/css/jennystart.css")) fragment.add_css(Util.load_resource("static/css/codemirror.css")) fragment.add_css(Util.load_resource("static/css/fullscreen.css")) fragment.add_javascript(Util.load_resource("static/js/src/jennystart.js")) fragment.add_javascript(Util.load_resource("static/js/src/codemirror.js")) fragment.add_javascript(Util.load_resource("static/js/src/active-line.js")) fragment.add_javascript(Util.load_resource("static/js/src/clike.js")) fragment.add_javascript(Util.load_resource("static/js/src/matchbrackets.js")) fragment.add_javascript(Util.load_resource("static/js/src/fullscreen.js")) fragment.initialize_js('JennystartXBlock') return fragment
def student_view(self, context=None): # preview in studio if self.runtime.anonymous_student_id == "student": return self.message_view(title="This a real-time communication XBlock base-on WebRTC!", message="There is nothing to edit in studio view.") fragment = Fragment() fragment.add_content(Util.render_template('static/html/uc_rtc.html')) fragment.add_css(Util.load_resource("static/css/uc_rtc.css")) fragment.add_javascript(Util.load_resource("static/js/src/socket.io-1.3.2.js")) fragment.add_javascript(Util.load_resource("static/js/src/adapter.js")) fragment.add_javascript(Util.load_resource("static/js/src/static.js")) fragment.add_javascript(Util.load_resource("static/js/src/uc_rtc.js")) fragment.initialize_js("UcRtcXBlock") return fragment
def message_view(self, message, clusters): context_dict = { "dockers": clusters, "message": str(message), "report": "" } fragment = Fragment() fragment.add_content( Util.render_template('static/html/ibm_docker.html', context_dict)) fragment.add_css(Util.load_resource("static/css/ibm_docker.css")) fragment.add_javascript( Util.load_resource("static/js/src/ibm_docker.js")) fragment.initialize_js("IBMDockerTestXBlock") return fragment
def student_view(self, context=None): # preview in studio if self.runtime.anonymous_student_id == "student": return self.message_view( title="This a real-time communication XBlock base-on WebRTC!", message="There is nothing to edit in studio view.", ) fragment = Fragment() fragment.add_content(Util.render_template("static/html/uc_rtc.html")) fragment.add_css(Util.load_resource("static/css/uc_rtc.css")) fragment.add_javascript(Util.load_resource("static/js/src/socket.io-1.3.2.js")) fragment.add_javascript(Util.load_resource("static/js/src/adapter.js")) fragment.add_javascript(Util.load_resource("static/js/src/static.js")) fragment.add_javascript(Util.load_resource("static/js/src/uc_rtc.js")) fragment.initialize_js("UcRtcXBlock") return fragment
def student_view(self, context=None): """ The primary view of the JennystartXBlock, shown to students when viewing courses. """ student_id = self.runtime.anonymous_student_id base_path="/edx/var/edxapp/staticfiles/ucore/" real_user = self.runtime.get_real_user(student_id) username = real_user.username conn = pymongo.Connection('localhost', 27017) db = conn.test db.authenticate("secret","secret") codeview = db.codeview result = codeview.find_one({"username":username}) conn.disconnect() self.logger.info(username + " start edit") if result and "view_file" in result: relative_path = result["view_file"] self.lab = result["lab"] self.file_path = base_path + student_id + relative_path self.logger.info("file " + relative_path) output=open(self.file_path) self.codeData =output.read() output.close() else: relative_path = "please enter file path" self.file_path = relative_path context_dict={"file":relative_path,"codeData":self.codeData} fragment = Fragment() fragment.add_content(Util.render_template("static/html/jennystart.html",context_dict) ) fragment.add_css(Util.load_resource("static/css/jennystart.css")) fragment.add_css(Util.load_resource("static/css/codemirror.css")) fragment.add_css(Util.load_resource("static/css/fullscreen.css")) fragment.add_javascript(Util.load_resource("static/js/src/jennystart.js")) fragment.add_javascript(Util.load_resource("static/js/src/codemirror.js")) fragment.add_javascript(Util.load_resource("static/js/src/active-line.js")) fragment.add_javascript(Util.load_resource("static/js/src/clike.js")) fragment.add_javascript(Util.load_resource("static/js/src/matchbrackets.js")) fragment.add_javascript(Util.load_resource("static/js/src/fullscreen.js")) fragment.initialize_js('JennystartXBlock') return fragment
def student_view(self, context=None): """ The primary view of the JennystartXBlock, shown to students when viewing courses. """ student_id=self.runtime.anonymous_student_id base_path="/edx/var/edxapp/staticfiles/ucore/" real_user = self.runtime.get_real_user(student_id) username = real_user.username conn = pymongo.Connection('localhost', 27017) db = conn.test codeview = db.codeview result = codeview.find_one({"username":username}) conn.disconnect() self.logger.info(username + " start edit") if result: relative_path = result["view_file"] self.lab = result["lab"] self.file_path = base_path + student_id + relative_path self.logger.info("file " + relative_path) output=open(self.file_path) self.codeData =output.read() output.close() else: relative_path = "please enter file path" self.file_path = relative_path context_dict={"file":relative_path,"codeData":self.codeData} fragment = Fragment() fragment.add_content(Util.render_template("static/html/jennystart.html",context_dict) ) fragment.add_css(Util.load_resource("static/css/jennystart.css")) fragment.add_css(Util.load_resource("static/css/codemirror.css")) fragment.add_css(Util.load_resource("static/css/fullscreen.css")) fragment.add_javascript(Util.load_resource("static/js/src/jennystart.js")) fragment.add_javascript(Util.load_resource("static/js/src/codemirror.js")) fragment.add_javascript(Util.load_resource("static/js/src/active-line.js")) fragment.add_javascript(Util.load_resource("static/js/src/clike.js")) fragment.add_javascript(Util.load_resource("static/js/src/matchbrackets.js")) fragment.add_javascript(Util.load_resource("static/js/src/fullscreen.js")) fragment.initialize_js('JennystartXBlock') return fragment
def studio_view(self, context=None): # to add new lab context_dict = { "labs": self.labs, "docker_file": """FROM uclassroom/ucore-vnc-base MAINTAINER ggxx<*****@*****.**> RUN cd / && git clone https://github.com/chyyuu/ucore_lab.git my_lab && cd /my_lab/ && git remote remove origin ENTRYPOINT ["bash"] """, "message": "" } fragment = Fragment() fragment.add_content(Util.render_template('static/html/uc_lab_new.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
def studio_view(self, context=None): # to add new lab context_dict = { "labs": self.labs, "docker_file": """FROM uclassroom/ucore-vnc-base MAINTAINER ggxx<*****@*****.**> RUN cd / && git clone https://github.com/chyyuu/ucore_lab.git my_lab && cd /my_lab/ && git remote remove origin ENTRYPOINT ["bash"] """, "message": "" } fragment = Fragment() fragment.add_content( Util.render_template('static/html/uc_lab_new.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
def student_view(self, context=None): """ The primary view of the JennystartXBlock, shown to students when viewing courses. """ student_id=self.runtime.anonymous_student_id context_dict={"file":student_id} fragment = Fragment() fragment.add_content(Util.render_template("static/html/jennystart.html",context_dict) ) fragment.add_css(Util.load_resource("static/css/jennystart.css")) fragment.add_css(Util.load_resource("static/css/codemirror.css")) fragment.add_css(Util.load_resource("static/css/fullscreen.css")) fragment.add_javascript(Util.load_resource("static/js/src/jennystart.js")) fragment.add_javascript(Util.load_resource("static/js/src/codemirror.js")) fragment.add_javascript(Util.load_resource("static/js/src/active-line.js")) fragment.add_javascript(Util.load_resource("static/js/src/clike.js")) fragment.add_javascript(Util.load_resource("static/js/src/matchbrackets.js")) fragment.add_javascript(Util.load_resource("static/js/src/fullscreen.js")) fragment.initialize_js('JennystartXBlock') return fragment
student = self.runtime.get_real_user( self.runtime.anonymous_student_id) username = student.username print 'OK' context_dict = { "labs": self._get_available_labs(), "dockers": self.dockers, "password": self.git_password, "username": username, "message": "", "report": "" } fragment = Fragment() fragment.add_content( Util.render_template('static/html/uc_docker.html', context_dict)) fragment.add_css(Util.load_resource("static/css/uc_docker.css")) fragment.add_javascript( Util.load_resource("static/js/src/uc_docker.js")) fragment.initialize_js("UcDockerXBlock") return fragment def studio_view(self, context=None): # to add new lab context_dict = { "labs": self.labs, "docker_file": """FROM uclassroom/ucore-vnc-base MAINTAINER ggxx<*****@*****.**> RUN cd / && git clone https://github.com/chyyuu/ucore_lab.git my_lab && cd /my_lab/ && git remote remove origin ENTRYPOINT ["bash"]
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)
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)
class IBMDockerTestXBlock(XBlock): logger = Util.ibm_logger() is_new = Boolean(default=True, scope=Scope.user_state, help="is new") is_empty = Boolean(default=True, scope=Scope.user_state, help="do not have docker") private_key = String(default="", scope=Scope.user_state, help="SHH Private Key") public_key = String(default="", scope=Scope.user_state, help="SHH Public Key") cluster_id = String(default="", scope=Scope.user_state, help="user cluster id") git_id = Integer(default="", scope=Scope.user_state, help="Git id") #repo url may be different from username/repo.git,so get it from api git_name = String(default="", scope=Scope.user_state, help="git repo") ldap_name = String(default="", scope=Scope.user_state, help="ldap name") ldap_pwd = String(default="", scope=Scope.user_state, help="ldap password") #student only own one cluster clusters = List(default=[], scope=Scope.user_state, help="clusters") # config CONFIG = Config.CONFIG git_host = CONFIG["GIT"]["HOST"] git_port = CONFIG["GIT"]["PORT"] git_admin_token = CONFIG["GIT"]["ADMIN_TOKEN"] git_import_url = CONFIG["GIT"]["IMPORT_URL"] git_project_name = CONFIG["GIT"]["PROJECT_NAME"] git_teacher_token = CONFIG["GIT"]["TEACHER"]["TOKEN"] cluster_conf_id = CONFIG["CLUSTER"]["CLUSTER_CONFIG_ID"] cluster_num = CONFIG["CLUSTER"]["CLUSTER_NUM"] delete_time = CONFIG["CLUSTER"]["DELETE_TIME"] init_user = CONFIG["CLUSTER"]["INIT_USER"] init_pwd = CONFIG["CLUSTER"]["INIT_PWD"] ibm_username = CONFIG["IBM_ACCOUNT"]["ACCOUNT"] ibm_pwd = CONFIG["IBM_ACCOUNT"]["PASSWORD"] mongo_admin = CONFIG["MONGO"]["ADMIN"] mongo_pwd = CONFIG["MONGO"]["PASSWORD"] user_Authorization = CONFIG["AUTHORIZATION"]["USER"] cluster_Authorization = CONFIG["AUTHORIZATION"]["CLUSTER"] cluster_helper = ClusterHelper(cluster_conf_id, cluster_num, cluster_Authorization, ibm_username, ibm_pwd) docker_helper = DockerHelper(git_host, init_user, init_pwd) 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() context_dict = {"dockers": self.clusters, "message": "", "report": ""} fragment = Fragment() fragment.add_content( Util.render_template('static/html/ibm_docker.html', context_dict)) fragment.add_css(Util.load_resource("static/css/ibm_docker.css")) fragment.add_javascript( Util.load_resource("static/js/src/ibm_docker.js")) fragment.initialize_js("IBMDockerTestXBlock") return fragment
self.is_new = False else: # TODO update git account student = self.runtime.get_real_user(self.runtime.anonymous_student_id) username = student.username print 'OK' context_dict = { "labs": self._get_available_labs(), "dockers": self.dockers, "password": self.git_password, "username": username, "message": "" } fragment = Fragment() fragment.add_content(Util.render_template('static/html/uc_docker.html', context_dict)) fragment.add_css(Util.load_resource("static/css/uc_docker.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_docker.js")) fragment.initialize_js("UcDockerXBlock") return fragment def studio_view(self, context=None): # to add new lab context_dict = { "labs": self.labs, "docker_file": """FROM uclassroom/ucore-vnc-base MAINTAINER ggxx<*****@*****.**> RUN cd / && git clone https://github.com/chyyuu/ucore_lab.git my_lab && cd /my_lab/ && git remote remove origin ENTRYPOINT ["bash"] """,
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) 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)