예제 #1
0
    def run(self, args):
        if self.mgr.is_started():
            print "experiment already running"
            return

        if len(args) != 2:
            print "error: {} requires two parameter".format(self.get_keyword())
            print self.help_msg()
            return

        group = args[0]
        user_name = args[1]

        if not re.search("^[a-zA-Z0-9_]+$", user_name):
            logging.info("invalid username used: %s", user_name)
            print "error: name must not contain characters other than letters, numbers and _"
            return

        if not self.mgr.has_group(group):
            logging.info("wrong group name used: %s", group)
            print "error: group %s not defined" % group
            return

        ## raises exception if group is invalid
        tasks = self.mgr.get_tasks_for_group(group)
        experiment = Experiment(group, user_name, tasks)

        logging.info("starting new experiment for %s, group: %s", user_name,
                     group)
        print "starting new experiment for user: {}, group: {}".format(
            user_name, group)
        # ensure we have access to the local display
        self.exec_cmd("xhost +local:", silent=True)

        print
        print "starting new editor container..."

        # start container as root, we will switch witin the init script
        #docker_cmd = "docker run -d"
        docker_cmd = "docker create"
        docker_cmd += " --name exp_%s_%s" % (group, user_name)
        # volume mount for X11 socket, if local X-display is used
        docker_cmd += " -v /tmp/.X11-unix:/tmp/.X11-unix"

        # ssh forwarded connection require the xauth mechanism
        xauth_file = "%s/.Xauthority" % os.environ['HOME']
        if os.path.isfile(xauth_file):
            docker_cmd += " -v %s:/tmp/.xauth" % xauth_file

        docker_cmd += " -e DISPLAY=%s" % os.environ['DISPLAY']
        # if we use a X11 display over network (ssh)
        docker_cmd += " --net=\"host\""

        if self.mgr.devmode:
            docker_cmd += " -v %s:/home/user/src" % os.path.abspath(
                os.path.join(os.path.dirname(__file__), "../../experiments"))
        else:
            docker_cmd += " -v /home/user/src"

        docker_cmd += " " + EDITOR_CNT_IMAGE

        # run the docker_cmd command
        out, ret = self.exec_cmd(docker_cmd)
        cnt_id = out.strip()
        if ret == 0:
            logging.debug("got editor container id: %s", cnt_id)
            if self.mgr.devmode:
                print "id: %s" % cnt_id

            # on CentOS, the xauth file has selinux label attached, thus the
            # container is not able to read it, therefore, try to copy it into
            # the container directly
            # Note: this work only for docker > 1.8
            if os.path.isfile(xauth_file):
                out, ret = self.exec_cmd(
                    "docker cp %s %s:/home/user/.Xauthority" %
                    (xauth_file, cnt_id),
                    silent=True)

            out, ret = self.exec_cmd("docker start %s" % cnt_id)
            if ret != 0:
                logging.error("error starting editor container")
                return False

            # everything worked as expected, set container id and experiment
            experiment.cnt_id = cnt_id
            logging.debug("about to start experiment")
            self.mgr.start_experiment(experiment)

            return True
        else:
            logging.error("could not start experiment editor container")
            print "could not start editor container"
            print "maybe you have to choose a different 'user name'"

        return False