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/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 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 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): """ 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
class ClusterHelper(object): logger = Util.ibm_logger() base_url = "https://crl.ptopenlab.com:8800/restful/supernova" def __init__(self, conf_id, num, token, username, pwd): #self.logger.info("ClusterHelper.__init__") self._conf_id = conf_id self._num = num #self._token = token #self._username = username #self._pwd = pwd self._headers = { "Content-Type": "application/json", "Authorization": token, "username": username, "password": pwd } def cluster_create(self): url = self.base_url + "/cluster/create" body = json.dumps({ "cluster_config_id": self._conf_id, "cluster_number": self._num, }) ret = requests.post(url, headers=self._headers, data=body) ret = json.loads(ret.text) return ret def cluster_list(self): url = self.base_url + "/cluster/list" ret = requests.get(url, headers=self._headers) ret = json.loads(ret.text) return ret def cluster_delete(self, cluster_id): url = self.base_url + "/cluster/delete/{0}".format(cluster_id) ret = requests.delete(url, headers=self._headers) ret = json.loads(ret.text) return ret def cluster_show(self, cluster_id): #url = self.base_url + "/cluster/show/{0}".format(cluster_id) url = self.base_url + "/cluster/show/" + cluster_id ret = requests.get(url, headers=self._headers) ret = json.loads(ret.text) return ret def get_webshell(self, user_Authorization, ip): headers = { "Content-Type": "application/json", "Authorization": user_Authorization } url = "https://dashboard.ptopenlab.com:443/restful/supernova/web_shell/get/Beijing/" + ip ret = requests.get(url, headers=headers) ret = json.loads(ret.text) return ret
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 _create_tmp_dockerfile(self, docker_file_text): tmp_path = "/tmp/uc_docker/" + Util.random_string(12) tmp_file = tmp_path + "/Dockerfile" mkdir(tmp_path) f=open(tmp_file, 'w') f.write(docker_file_text) f.flush() f.close() return tmp_path
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 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 UcCodemirrorXBlock, shown to students when viewing courses. """ fragment = Fragment() fragment.add_content(Util.load_resource("static/html/uc_codemirror.html")) self.logger.info("load uc_codemirror.html") fragment.add_css(Util.load_resource("static/css/uc_codemirror.css")) self.logger.info("load uc_codemirror.css") fragment.add_css(Util.load_resource("static/css/codemirror.css")) self.logger.info("load codemirror.css") fragment.add_javascript(Util.load_resource("static/js/src/codemirror.js")) self.logger.info("load codemirror.js") fragment.add_javascript(Util.load_resource("static/js/src/uc_codemirror.js")) self.logger.info("load uc_codemirror.js") fragment.initialize_js('UcCodemirrorXBlock') self.logger.info("initialize_js(UcCodemirrorXBlock)") 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
class DockerHelper(object): logger = Util.ibm_logger() def __init__(self, git_host, init_user, init_pwd): self._git_host = git_host self._init_user = init_user self._init_pwd = init_pwd #create user for student def init_user(self, host, user_name, user_pwd): self.logger.info("init_user " + host + " " + user_name + " " + user_pwd) cmd = "./myuseradd-ibm " + user_name + " " + user_pwd print cmd ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=self._init_user, password=self._init_pwd) stdin, stdout, stderr = ssh.exec_command(cmd) print stdout.read() ssh.close() #initialize git config def init_git(self, host, user_name, user_pwd, email, git_name, private_key, public_key): self.logger.info("init docker git" + " " + email + " " + user_name) cmd = "umask 0002 ; mkdir .ssh ; " cmd += "echo -ne '" + private_key + "' >~/.ssh/id_rsa ; " cmd += "echo '" + public_key + "' >~/.ssh/id_rsa.pub ; " cmd += "chmod 0600 ~/.ssh/id_rsa ; " cmd += "eval 'ssh-agent -s' ; " cmd += "ssh-add ~/.ssh/id_rsa ; " cmd += "git config --global user.name '" + user_name + "' ; " cmd += "git config --global user.email " + email + " ; " #cmd += "sudo echo -ne 'StrictHostKeyChecking no\\nUserKnownHostsFile /dev/null\\n' >> /etc/ssh/ssh_config && " cmd += "mkdir ucore_lab ; cd ./ucore_lab && git init ; " #cmd += "git remote add origin_edx " + repo_url +"; " cmd += "git remote add origin_edx git@" + self._git_host + ":" + git_name + "/ucore_lab.git ; " cmd += "git pull origin_edx master" self.logger.info("cmd" + cmd) print cmd ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user_name, password=user_pwd) stdin, stdout, stderr = ssh.exec_command(cmd) print stdout.read() ssh.close()
class GitRepo(): logger = Util.custom_logger({ 'logFile': Config.logFile, 'logFmt': Config.logFmt, 'logName': 'GitRepoLog' }) def __init__(self, localRepoDir): self.localRepoDir = localRepoDir def execInShell(self, cmd): out = subprocess.check_output(cmd, shell=True) self.logger.debug('check_output [cmd=%s] [out=%s]' % (cmd, out)) return out def checkUser(self, user): findEmail = self.execInShell('git config -l | grep user.email; exit 0') if not findEmail: self.execInShell('git config --global user.email "%s"' % user['email']) findName = self.execInShell('git config -l | grep user.name; exit 0') if not findName: self.execInShell('git config --global user.name "%s"' % user['name']) def pull(self): p = Popen(['git', 'pull'], cwd=self.localRepoDir, stdout=PIPE) return p.communicate() def status(self): p = Popen(['git', 'status'], cwd=self.localRepoDir, stdout=PIPE) return p.communicate() def commit(self, commit): p = Popen(['git', 'commit', '-m', commit], cwd=self.localRepoDir) return p.communicate() def add(self, fileRgx='--all'): p = Popen(['git', 'add', fileRgx], cwd=self.localRepoDir, stdout=PIPE) return p.communicate() def push(self): p = Popen(['git', 'push'], cwd=self.localRepoDir) return p.communicate()
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 UcCodemirrorXBlock, shown to students when viewing courses. """ fragment = Fragment() fragment.add_content(Util.load_resource("static/html/uc_codemirror.html")) fragment.add_css(Util.load_resource("static/css/uc_codemirror.css")) fragment.add_css(Util.load_resource("static/jstree/dist/themes/default/style.min.css")) fragment.add_javascript(Util.load_resource("static/js/src/uc_codemirror.js")) fragment.add_javascript(Util.load_resource("static/js/src/jquery.js")) fragment.add_javascript(Util.load_resource("static/js/src/jstree.min.js")) fragment.initialize_js('UcCodemirrorXBlock') 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 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
class DockerHelper(object): logger = Util.uc_logger() def __init__(self, host, url, ca, cert, key, version): self.logger.info("__init__") self._host = host tls_config = docker.tls.TLSConfig(client_cert=(cert, key), ca_cert=ca, verify=True) self._client = docker.Client(base_url=url, tls=tls_config, version=version) def build_lab_docker(self, image_name, docker_file_text): self.logger.info("DockerHelper.build_lab_docker") f = BytesIO(docker_file_text.encode('utf-8')) response = [ line for line in self._client.build(tag=image_name, rm=True, fileobj=f) ] self.logger.info("docker.build:") for line in response: self.logger.info(line) self.logger.info("DockerHelper.build_lab_docker.complete") def build_student_docker(self, image_name, docker_obj, private_key, public_key, user_name, user_psw, user_email, user_token, git_host, git_port, teacher_token, docker_namespace): self.logger.info("DockerHelper.build_student_docker") result, message = GitLabUtil.get_user(git_host, git_port, teacher_token) if not result: self.logger.info(message) return teacher_id = json.loads(message)["id"] teacher_name = json.loads(message)["username"] project_name = docker_obj.name docker_file_text = self._create_ucore_docker_file( docker_obj, private_key, public_key, user_name, user_psw, user_email, git_host, git_port, docker_namespace, teacher_name) docker_file = BytesIO(docker_file_text.encode('utf-8')) result, message = GitLabUtil.create_private_project( git_host, git_port, user_token, project_name) if not result: self.logger.info(message) return result, message = GitLabUtil.add_project_developer( git_host, git_port, user_token, user_name, project_name, teacher_id) if not result: self.logger.info(message) return response = [ line for line in self._client.build( tag=image_name, rm=True, fileobj=docker_file) ] self.logger.info("build docker result:") for line in response: self.logger.info(line) container = self._client.create_container(image=image_name, ports=[8080]) self.logger.info("docker.create_container:") self.logger.info(container) docker_obj.container_id = container["Id"] self.logger.info("DockerHelper.build_student_docker.complete") def start_student_docker(self, docker_obj): print "DockerHelper.start_student_docker" self._client.start(docker_obj.container_id, port_bindings={ 6080: ("0.0.0.0", ), 8080: ("0.0.0.0", ) }) port = self._client.port(docker_obj.container_id, 8080) vnc = self._client.port(docker_obj.container_id, 6080) docker_obj.host = self._host docker_obj.port = port[0]["HostPort"] docker_obj.vnc = vnc[0]["HostPort"] docker_obj.last_start_time = datetime.datetime.strftime( datetime.datetime.today(), "%Y-%m-%d %H:%M:%S") self.logger.info("DockerHelper.start_student_docker.complete") def stop_student_docker(self, docker_obj): print "DockerHelper.stop_student_docker" self._client.stop(docker_obj.container_id) print "DockerHelper.stop_student_docker.complete" def _create_ucore_docker_file(self, docker_obj, private_key, public_key, user_name, user_pwd, user_email, git_host, git_port, docker_namespace, teacher_name): text = ( 'FROM ' + docker_namespace + '/' + docker_obj.lab.name + '\nMAINTAINER ggxx<*****@*****.**>' + '\n' + '\nRUN echo -ne "' + private_key.replace("\n", "\\n") + '" > /root/.ssh/id_rsa;\\' + '\n echo "' + public_key + '" > /root/.ssh/id_rsa.pub;\\' + '\n chmod 0600 /root/.ssh/id_rsa ;\\' + '\n echo -ne "' + self.startup_shell.replace("\n", "\\n") + '" > /startup.sh;\\' + '\n chmod +x /startup.sh;\\' + '\n echo -ne "' + self.tty_config.format(user_name, user_pwd).replace("\n", "\\n") + '" > /opt/ttyjs/ttyjs-config.json;\\' + '\n echo ' + user_pwd + ' | echo $(vncpasswd -f) > /root/.vnc/passwd;\\' + '\n chmod 0600 /root/.vnc/passwd;\\' + '\n git config --global user.name "' + user_name + '" ;\\' + '\n git config --global user.email "' + user_email + '" ;\\' + '\n echo -ne "StrictHostKeyChecking no\\nUserKnownHostsFile /dev/null\\n" >> /etc/ssh/ssh_config ;\\' + '\n cd /my_lab/ ;\\' + '\n git remote add origin git@' + git_host + ':' + user_name + '/' + docker_obj.name + '.git; \\' '\n git push -u origin master' + '\n' + '\nEXPOSE 6080' + '\nEXPOSE 8080' + '\nENTRYPOINT ["/startup.sh"]', ) self.logger.info(text[0]) return text[0] startup_shell = """#!/usr/bin/env bash (vncserver && /opt/noVNC/utils/launch.sh --vnc localhost:5901) & tty.js --config /opt/ttyjs/ttyjs-config.json""" tty_config = """{{
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
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"]
class ExerciseMdfXBlock(XBlock): """ 功能: 1. 向题库增加题目 2. 修改现有题目 """ display_name = String(display_name="Display Name", default=u'题库编辑', scope=Scope.settings) logger = Util.custom_logger({ 'logFile': Config.logFile, 'logFmt': Config.logFmt, 'logName': 'ExerciseMdfXBlockLog' }) # Fields are defined on the class. You can access them in your code as # self.<fieldname>. def resource_string(self, path): """Handy helper for getting resources from our kit.""" data = pkg_resources.resource_string(__name__, path) return data.decode("utf8") def student_view(self, context=None): """ The primary view of the ExerciseMdfXBlock, shown to students when viewing courses. """ HTML_FILE = "static/index.html" html = self.resource_string(HTML_FILE) frag = Fragment(html.format(self=self)) frag.add_javascript(self.resource_string("static/index.js")) frag.initialize_js('ExerciseMdfXBlock') return frag @XBlock.json_handler def getQuestionJson(self, data, suffix=''): q_number = int(data['q_number']) url = Config.questionJsonUrl % { 'qDir': ((q_number - 1) / 100) + 1, 'qNo': q_number, } try: #req = urllib2.Request(url) res_data = urllib2.urlopen(url) res = res_data.read() res = json.loads(res) if 'content' in res: content = json.loads(base64.b64decode(res['content'])) self.logger.info('getQuestionJson [qNo=%d] [url=%s]' % (q_number, url)) return {'code': 0, 'desc': 'ok', 'res': content} elif res['message'] == 'Not Found': self.logger.info( 'ERROR getQuestionJson [qNo=%d] [msg=%s] [url=%s]' % (q_number, res['message'], url)) return { 'code': 1, 'type': 'error', 'desc': u'题号为%d的题目不存在' % q_number } else: self.logger.info( 'ERROR getQuestionJson [qNo=%d] [msg=%s] [url=%s]' % (q_number, res['message'], url)) return { 'code': 1, 'error': 'error', 'dese': 'Error occurs when loading %d.json message: %s' % (q_number, res['message']) } # except urllib2.HTTPError as e: # self.logger.exception('ERROR getQuestionJson [qNo=%d] [status=%d] [url=%s]' % (q_number, e.code, url)) # if (e.code == 404): # return { # 'code': 1, # 'type': 'error', # 'desc': u'题号为%d的题目不存在' % q_number # } except Exception as e: self.logger.exception( 'ERROR getQuestionJson [qNo=%d] [desc=%s] [url=%s]' % (q_number, str(e), url)) return { 'code': 1, 'type': 'error', 'desc': str(e), } @XBlock.json_handler def setQuestionJson(self, data, suffix=''): try: repo = ExerciseRepo(Config.localRepoDir) repo.setUser({ 'email': Config.commitEmail, 'name': Config.commitName }) # 简单检查题目是否合理 lenOfAnswer = len(data['answer'].strip()) questionType = data['type'] if lenOfAnswer == 0: return {'code': 2, 'type': 'warning', 'desc': '答案不能为空'} if questionType == 'single_answer' and lenOfAnswer != 1: return {'code': 2, 'type': 'warning', 'desc': '单选题的答案个数仅有一个'} data['status'] = 'ok' if not data['q_number']: data['q_number'] = repo.getMaxQNo() + 1 repo.setExercise(data) self.logger.info('setQuestionJson [qNo=%d] [%s]' % (data['q_number'], json.dumps(data))) return {'code': 0, 'q_number': data['q_number']} except Exception as e: self.logger.exception( 'ERROR setQuestionJson [qNo=%d] [desc=%s] [%s]' % (data['q_number'], str(e), json.dumps(data))) return {'code': 1, 'type': 'error', 'desc': '发生错误, %s' % str(e)} # TO-DO: change this to create the scenarios you'd like to see in the # workbench while developing your XBlock. @staticmethod def workbench_scenarios(): """A canned scenario for display in the workbench.""" return [ ("ExerciseMdfXBlock", """<exercisemdf/> """), ("Multiple ExerciseMdfXBlock", """<vertical_demo> <exercisemdf/> <exercisemdf/> <exercisemdf/> </vertical_demo> """), ]
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 DockerRawHelper(object): logger = Util.uc_logger() def __init__(self, host, port, ca, cert, key): self.logger.info("DockerRawHelper.__init__") self._host = host self._port = port self._ca = ca self._cert = cert self._key = key def build_lab_docker(self, image_name, dockerfile_text): dockerfile_path = self._create_tmp_dockerfile(dockerfile_text) cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} build --rm -t {5} {6}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, image_name, dockerfile_path) self.logger.info(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=60*10) if err_output != '': return 1 return 0 def build_student_docker(self, image_name, docker, private_key, public_key, user_name, user_psw, user_email, user_token, git_host, git_port, teacher_token, docker_namespace, mem_limit='256m'): result, message = GitLabUtil.get_user(git_host, git_port, teacher_token) if not result: self.logger.info(message) return 2 try: teacher_id = json.loads(message)["id"] teacher_name = json.loads(message)["username"] except Exception: return 3 project_name = docker.name dockerfile_text = self._format_ucore_dockerfile_text(docker, private_key, public_key, user_name, user_psw, user_email, git_host, docker_namespace) dockerfile_path = self._create_tmp_dockerfile(dockerfile_text) result, message = GitLabUtil.create_private_project(git_host, git_port, user_token, project_name) if not result: self.logger.info(message) self.logger.info("hhhhhhhhhhhhh1") return 4 result, message = GitLabUtil.add_project_developer(git_host, git_port, user_token, user_name, project_name, teacher_id) if not result: self.logger.info(message) self.logger.info("hhhhhhhhhhhhhh2") return 5 cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} build --rm -t {5} {6}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, image_name, dockerfile_path) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=60*5) if err_output != '': return 6 cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} create -p :8080 -p :6080 -m {5} {6}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, mem_limit, image_name) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=15) docker.container_id = std_output if err_output != '': return 7 docker.container_id = std_output return 0 def start_student_docker(self, docker): cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} start {5}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, docker.container_id) self.logger.info(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=15) if err_output != '': return 1 cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} port {5}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, docker.container_id) self.logger.info(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=15) if err_output != '': return 2 docker.last_start_time = datetime.datetime.strftime(datetime.datetime.today(), "%Y-%m-%d %H:%M:%S") docker.host = self._host # std_output is 6080/tcp -> 0.0.0.0:49100\n8080/tcp -> 0.0.0.0:49101 ports = std_output.split('\n') for i in range(0, len(ports)): port_docker = ports[i].split("/")[0] if port_docker == "6080": docker.port = ports[i].split(":")[1] elif port_docker == "8080": docker.vnc = ports[i].split(":")[1] return 0 def stop_student_docker(self, docker): cmd = "docker --tlsverify --tlscacert={0} --tlscert={1} --tlskey={2} -H={3}:{4} stop {5}" cmd = cmd.format(self._ca, self._cert, self._key, self._host, self._port, docker.container_id) self.logger.info(cmd) process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (std_output, err_output) = process.communicate()#timeout=15) if err_output != '': return 1 return 0 def _create_tmp_dockerfile(self, docker_file_text): tmp_path = "/tmp/uc_docker/" + Util.random_string(12) tmp_file = tmp_path + "/Dockerfile" mkdir(tmp_path) f=open(tmp_file, 'w') f.write(docker_file_text) f.flush() f.close() return tmp_path def _format_ucore_dockerfile_text(self, docker, private_key, public_key, user_name, user_pwd, user_email, git_host, docker_namespace): text = ( 'FROM ' + docker_namespace + '/' + docker.lab.name + '\nMAINTAINER ggxx<*****@*****.**>' + '\n' + '\nRUN echo -ne "' + private_key.replace("\n", "\\n") + '" > /root/.ssh/id_rsa;\\' + '\n echo "' + public_key + '" > /root/.ssh/id_rsa.pub;\\' + '\n chmod 0600 /root/.ssh/id_rsa ;\\' + '\n echo -ne "' + self._startup_shell.replace("\n", "\\n") + '" > /startup.sh;\\' + '\n chmod +x /startup.sh;\\' + '\n echo -ne "' + self._tty_config.format(user_name, user_pwd).replace("\n", "\\n") + '" > /opt/ttyjs/ttyjs-config.json;\\' + '\n echo ' + user_pwd + ' | echo $(vncpasswd -f) > /root/.vnc/passwd;\\' + '\n chmod 0600 /root/.vnc/passwd;\\' + '\n git config --global user.name "' + user_name + '" ;\\' + '\n git config --global user.email "' + user_email + '" ;\\' + '\n echo -ne "StrictHostKeyChecking no\\nUserKnownHostsFile /dev/null\\n" >> /etc/ssh/ssh_config ;\\' + '\n cd /my_lab/ ;\\' + '\n git remote add origin git@' + git_host + ':' + user_name + '/' + docker.name + '.git; \\' '\n git push -u origin master' + '\n' + '\nEXPOSE 6080' + '\nEXPOSE 8080' + '\nENTRYPOINT ["/startup.sh"]',) self.logger.info(text[0]) return text[0] _startup_shell = """#!/usr/bin/env bash vncserver -kill :1 (vncserver && /opt/noVNC/utils/launch.sh --vnc localhost:5901) & tty.js --config /opt/ttyjs/ttyjs-config.json""" _tty_config = """{{
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)
class UcDockerXBlock(XBlock): logger = Util.uc_logger() is_new = Boolean(default=True, scope=Scope.user_state, help="is new") private_key = String(default="", scope=Scope.user_state, help="SHH Private Key") public_key = String(default="", scope=Scope.user_state, help="SHH Public Key") git_password = String(default="", scope=Scope.user_state, help="Git password") git_id = Integer(default="", scope=Scope.user_state, help="Git id") git_user_token = String(default="", scope=Scope.user_state, help="Git private token") dockers = List(default=[], scope=Scope.user_state, help="dockers") labs = List(default=[], scope=Scope.content, help="labs") # config CONFIG = Config.CONFIG git_host = CONFIG["GIT"]["HOST"] git_port = CONFIG["GIT"]["PORT"] git_admin_token = CONFIG["GIT"]["ADMIN_TOKEN"] docker_host = CONFIG["DOCKER"]["HOST"] docker_url = CONFIG["DOCKER"]["REMOTE_API"]["URL"] docker_namespace = CONFIG["DOCKER"]["NAMESPACE"] docker_mem = CONFIG["DOCKER"]["MEM_LIMIT"] ca = CONFIG["DOCKER"]["REMOTE_API"]["CA"] cert = CONFIG["DOCKER"]["REMOTE_API"]["CERT"] key = CONFIG["DOCKER"]["REMOTE_API"]["KEY"] version = CONFIG["DOCKER"]["REMOTE_API"]["VERSION"] git_teacher_token = CONFIG["GIT"]["TEACHER"]["TOKEN"] principal_name = CONFIG["LDAP"]["PRINCIPAL_NAME"] ldap_password = CONFIG["LDAP"]["PASSWORD"] ldap_url = CONFIG["LDAP"]["LDAP_URL"] base_dn = CONFIG["LDAP"]["BASE_DN"] docker_helper = DockerRawHelper(docker_host, docker_url, ca, cert, key) 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) result, message = GitLabUtil.add_ssh_key(self.git_host, self.git_port, self.git_user_token, "uClassroom default", self.public_key) self.logger.info("add_ssh_key result:") self.logger.info(result) self.logger.info(message) if not result: return self.message_view( "Error in uc_docker (add git ssh key)", message, context) self.is_new = False self.save()
class UcDockerXBlock(XBlock): logger = Util.uc_logger() is_new = Boolean(default=True, scope=Scope.user_state, help="is new") private_key = String(default="", scope=Scope.user_state, help="SHH Private Key") public_key = String(default="", scope=Scope.user_state, help="SHH Public Key") git_password = String(default="", scope=Scope.user_state, help="Git password") git_id = Integer(default="", scope=Scope.user_state, help="Git id") git_user_token = String(default="", scope=Scope.user_state, help="Git private token") dockers = List(default=[], scope=Scope.user_state, help="dockers") labs = List(default=[], scope=Scope.content, help="labs") # config CONFIG = Config.CONFIG git_host = CONFIG["GIT"]["HOST"] git_port = CONFIG["GIT"]["PORT"] git_admin_token = CONFIG["GIT"]["ADMIN_TOKEN"] docker_host = CONFIG["DOCKER"]["HOST"] docker_url = CONFIG["DOCKER"]["REMOTE_API"]["URL"] docker_namespace = CONFIG["DOCKER"]["NAMESPACE"] docker_mem = CONFIG["DOCKER"]["MEM_LIMIT"] ca = CONFIG["DOCKER"]["REMOTE_API"]["CA"] cert = CONFIG["DOCKER"]["REMOTE_API"]["CERT"] key = CONFIG["DOCKER"]["REMOTE_API"]["KEY"] version = CONFIG["DOCKER"]["REMOTE_API"]["VERSION"] git_teacher_token = CONFIG["GIT"]["TEACHER"]["TOKEN"] docker_helper = DockerRawHelper(docker_host, docker_url, ca, cert, key) 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) result, message = GitLabUtil.add_ssh_key(self.git_host, self.git_port, self.git_user_token, "uClassroom default", self.public_key) self.logger.info("add_ssh_key result:") self.logger.info(result) self.logger.info(message) if not result: return self.message_view( "Error in uc_docker (add git ssh key)", message, context) 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 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)
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"] """,