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")
예제 #2
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)
         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)
         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)
     if err_output != '':
         return 7
     docker.container_id = std_output
     return 0
예제 #3
0
    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 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(user_token + project_name)
            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(user_token + " "+project_name)
        #    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)
	self.logger.info(cmd)
        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