Exemplo n.º 1
0
 def openLog(cls, nodename, host, user=None):
     '''
 Opens the log file associated with the given node in a new terminal.
 @param nodename: the name of the node (with name space)
 @type nodename: C{str}
 @param host: the host name or ip where the log file are
 @type host: C{str}
 @return: C{True}, if a log file was found
 @rtype: C{bool}
 @raise Exception: on errors while resolving host
 @see: L{node_manager_fkie.is_local()}
 '''
     rospy.loginfo("show log for '%s' on '%s'", str(nodename), str(host))
     title_opt = 'LOG %s on %s' % (nodename, host)
     if nm.is_local(host):
         found = False
         screenLog = nm.screen().getScreenLogFile(node=nodename)
         if os.path.isfile(screenLog):
             cmd = nm.settings().terminal_cmd(
                 [nm.settings().log_viewer, screenLog], title_opt)
             rospy.loginfo("open log: %s", cmd)
             SupervisedPopen(shlex.split(cmd),
                             id="Open log",
                             description="Open log for '%s' on '%s'" %
                             (str(nodename), str(host)))
             found = True
         #open roslog file
         roslog = nm.screen().getROSLogFile(nodename)
         if os.path.isfile(roslog):
             title_opt = title_opt.replace('LOG', 'ROSLOG')
             cmd = nm.settings().terminal_cmd(
                 [nm.settings().log_viewer, roslog], title_opt)
             rospy.loginfo("open ROS log: %s", cmd)
             SupervisedPopen(shlex.split(cmd),
                             id="Open log",
                             description="Open log for '%s' on '%s'" %
                             (str(nodename), str(host)))
             found = True
         return found
     else:
         ps = nm.ssh().ssh_x11_exec(host, [
             nm.settings().start_remote_script, '--show_screen_log',
             nodename
         ], title_opt, user)
         ps = nm.ssh().ssh_x11_exec(host, [
             nm.settings().start_remote_script, '--show_ros_log', nodename
         ], title_opt.replace('LOG', 'ROSLOG'), user)
     return False
Exemplo n.º 2
0
 def openScreenTerminal(cls, host, screen_name, nodename, user=None):
     '''
 Open the screen output in a new terminal.
 @param host: the host name or ip where the screen is running.
 @type host: C{str}
 @param screen_name: the name of the screen to show
 @type screen_name: C{str}
 @param nodename: the name of the node is used for the title of the terminal
 @type nodename: C{str}
 @raise Exception: on errors while resolving host
 @see: L{node_manager_fkie.is_local()}
 '''
     #create a title of the terminal
     #    pid, session_name = cls.splitSessionName(screen_name)
     title_opt = 'SCREEN %s on %s' % (nodename, host)
     if nm.is_local(host):
         cmd = nm.settings().terminal_cmd([cls.SCREEN, '-x', screen_name],
                                          title_opt)
         rospy.loginfo("Open screen terminal: %s", cmd)
         SupervisedPopen(shlex.split(cmd),
                         object_id=title_opt,
                         description="Open screen terminal: %s" % title_opt)
     else:
         ps = nm.ssh().ssh_x11_exec(host, [cls.SCREEN, '-x', screen_name],
                                    title_opt, user)
         rospy.loginfo("Open remote screen terminal: %s", ps)
Exemplo n.º 3
0
 def killScreens(cls, node, host, auto_ok_request=True, user=None, pw=None):
     '''
     Searches for the screen associated with the given node and kill this screens.
     @param node: the name of the node those screen output to show
     @type node: C{str}
     @param host: the host name or ip where the screen is running
     @type host: C{str}
     '''
     if node is None or len(node) == 0:
         return False
     try:
         # get the available screens
         screens = cls._getActiveScreens(host,
                                         cls.createSessionName(node),
                                         auto_ok_request,
                                         user=user,
                                         pwd=pw)  # user=user, pwd=pwd
         if screens:
             do_kill = True
             if auto_ok_request:
                 try:
                     from python_qt_binding.QtGui import QMessageBox
                 except:
                     from python_qt_binding.QtWidgets import QMessageBox
                 result = QMessageBox.question(
                     None, "Kill SCREENs?", '\n'.join(screens),
                     QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok)
                 if result & QMessageBox.Ok:
                     do_kill = True
             if do_kill:
                 for s in screens:
                     pid, _, _ = s.partition('.')
                     if pid:
                         try:
                             nm.starter()._kill_wo(host, int(pid),
                                                   auto_ok_request, user,
                                                   pw)
                         except:
                             import traceback
                             rospy.logwarn(
                                 "Error while kill screen (PID: %s) on host '%s': %s",
                                 str(pid), str(host),
                                 traceback.format_exc(1))
                 if nm.is_local(host):
                     SupervisedPopen(
                         [cls.SCREEN, '-wipe'],
                         object_id='screen -wipe',
                         description="screen: clean up the socket with -wipe"
                     )
                 else:
                     nm.ssh().ssh_exec(host, [cls.SCREEN, '-wipe'],
                                       close_stdin=True,
                                       close_stdout=True,
                                       close_stderr=True)
     except nm.AuthenticationRequest as e:
         raise nm.InteractionNeededError(e, cls.killScreens,
                                         (node, host, auto_ok_request))
Exemplo n.º 4
0
 def _prepareROSMaster(cls, masteruri):
     if not masteruri:
         masteruri = roslib.rosenv.get_master_uri()
     #start roscore, if needed
     try:
         if not os.path.isdir(nm.ScreenHandler.LOG_PATH):
             os.makedirs(nm.ScreenHandler.LOG_PATH)
         socket.setdefaulttimeout(3)
         master = xmlrpclib.ServerProxy(masteruri)
         master.getUri(rospy.get_name())
     except:
         #      socket.setdefaulttimeout(None)
         #      import traceback
         #      print traceback.format_exc(1)
         # run a roscore
         from urlparse import urlparse
         master_host = urlparse(masteruri).hostname
         if nm.is_local(master_host, True):
             print "Start ROS-Master with", masteruri, "..."
             master_port = urlparse(masteruri).port
             new_env = dict(os.environ)
             new_env['ROS_MASTER_URI'] = masteruri
             cmd_args = '%s roscore --port %d' % (
                 nm.ScreenHandler.getSceenCmd(
                     '/roscore--%d' % master_port), master_port)
             print "    %s" % cmd_args
             try:
                 SupervisedPopen(shlex.split(cmd_args),
                                 env=new_env,
                                 id="ROSCORE",
                                 description="Start roscore")
                 # wait for roscore to avoid connection problems while init_node
                 result = -1
                 count = 1
                 while result == -1 and count < 11:
                     try:
                         print "  retry connect to ROS master", count, '/', 10
                         master = xmlrpclib.ServerProxy(masteruri)
                         result, _, _ = master.getUri(
                             rospy.get_name())  #_:=uri, msg
                     except:
                         time.sleep(1)
                         count += 1
                 if count >= 11:
                     raise StartException(
                         'Cannot connect to the ROS-Master: ' +
                         str(masteruri))
             except Exception as e:
                 import sys
                 print >> sys.stderr, e
                 raise
         else:
             raise Exception("ROS master '%s' is not reachable" % masteruri)
     finally:
         socket.setdefaulttimeout(None)
Exemplo n.º 5
0
 def getLocalOutput(cls, cmd):
     '''
     This method is used to read the output of the command executed in a terminal.
     @param cmd: the command to execute in a terminal
     @type cmd: C{str}
     @return: the output generated by the execution of the command.
     @rtype: output
     '''
     ps = SupervisedPopen(cmd, stdout=subprocess.PIPE)
     result = ps.stdout.read()
     return result
Exemplo n.º 6
0
 def ssh_x11_exec(self, host, cmd, title=None, user=None):
     '''
     Executes a command on remote host using a terminal with X11 forwarding.
     @todo: establish connection using paramiko SSH library.
     @param host: the host
     @type host: C{str}
     @param cmd: the list with command and arguments
     @type cmd: C{[str,...]}
     @param title: the title of the new opened terminal, if it is None, no new terminal will be created
     @type title: C{str} or C{None}
     @param user: user name
     @return: the result of C{subprocess.Popen(command)}
     @see: U{http://docs.python.org/library/subprocess.html?highlight=subproces#subprocess}
     '''
     with self.mutex:
         try:
             # workaround: use ssh in a terminal with X11 forward
             user = nm.settings().host_user(host) if user is None else user
             if host in self.SSH_AUTH:
                 user = self.SSH_AUTH[host]
             # generate string for SSH command
             ssh_str = ' '.join([
                 '/usr/bin/ssh', '-aqtx', '-oClearAllForwardings=yes',
                 '-oConnectTimeout=5', '-oStrictHostKeyChecking=no',
                 '-oVerifyHostKeyDNS=no', '-oCheckHostIP=no',
                 ''.join([user, '@', host])
             ])
             if title is not None:
                 cmd_str = nm.settings().terminal_cmd(
                     [ssh_str, ' '.join(cmd)], title)
             else:
                 cmd_str = str(' '.join([ssh_str, ' '.join(cmd)]))
             rospy.loginfo("REMOTE x11 execute on %s: %s", host, cmd_str)
             return SupervisedPopen(
                 shlex.split(cmd_str),
                 object_id=str(title),
                 description="REMOTE x11 execute on %s: %s" %
                 (host, cmd_str))
         except:
             raise
Exemplo n.º 7
0
    def runNode(cls,
                node,
                launch_config,
                force2host=None,
                masteruri=None,
                auto_pw_request=False,
                user=None,
                pw=None,
                item=None):
        '''
    Start the node with given name from the given configuration.
    @param node: the name of the node (with name space)
    @type node: C{str}
    @param launch_config: the configuration containing the node
    @type launch_config: L{LaunchConfig} 
    @param force2host: start the node on given host.
    @type force2host: L{str} 
    @param masteruri: force the masteruri.
    @type masteruri: L{str} 
    @param auto_pw_request: opens question dialog directly, use True only if the method is called from the main GUI thread
    @type auto_pw_request: bool
    @raise StartException: if the screen is not available on host.
    @raise Exception: on errors while resolving host
    @see: L{node_manager_fkie.is_local()}
    '''
        #'print "RUN node", node, time.time()
        n = launch_config.getNode(node)
        if n is None:
            raise StartException(''.join(["Node '", node, "' not found!"]))

        env = list(n.env_args)
        if n.respawn:
            # set the respawn environment variables
            respawn_params = cls._get_respawn_params(
                rospy.names.ns_join(n.namespace, n.name),
                launch_config.Roscfg.params)
            if respawn_params['max'] > 0:
                env.append(('RESPAWN_MAX', '%d' % respawn_params['max']))
            if respawn_params['min_runtime'] > 0:
                env.append(('RESPAWN_MIN_RUNTIME',
                            '%d' % respawn_params['min_runtime']))
            if respawn_params['delay'] > 0:
                env.append(('RESPAWN_DELAY', '%d' % respawn_params['delay']))
        prefix = n.launch_prefix if not n.launch_prefix is None else ''
        if prefix.lower() == 'screen' or prefix.lower().find('screen ') != -1:
            rospy.loginfo("SCREEN prefix removed before start!")
            prefix = ''
        args = [
            ''.join(['__ns:=', n.namespace.rstrip(rospy.names.SEP)]),
            ''.join(['__name:=', n.name])
        ]
        if not (n.cwd is None):
            args.append(''.join(['__cwd:=', n.cwd]))

        # add remaps
        for remap in n.remap_args:
            args.append(''.join([remap[0], ':=', remap[1]]))

        # get host of the node
        host = launch_config.hostname
        env_loader = ''
        if n.machine_name:
            machine = launch_config.Roscfg.machines[n.machine_name]
            if not machine.address in ['localhost', '127.0.0.1']:
                host = machine.address
                if masteruri is None:
                    masteruri = nm.nameres().masteruri(n.machine_name)
            #TODO: env-loader support?
#      if hasattr(machine, "env_loader") and machine.env_loader:
#        env_loader = machine.env_loader
# set the host to the given host
        if not force2host is None:
            host = force2host

        # set the ROS_MASTER_URI
        if masteruri is None:
            masteruri = masteruri_from_ros()
            env.append(('ROS_MASTER_URI', masteruri))

        abs_paths = list()  # tuples of (parameter name, old value, new value)
        not_found_packages = list()  # package names
        # set the global parameter
        if not masteruri is None and not masteruri in launch_config.global_param_done:
            global_node_names = cls.getGlobalParams(launch_config.Roscfg)
            rospy.loginfo(
                "Register global parameter:\n  %s", '\n  '.join(
                    "%s%s" % (str(v)[:80], '...' if len(str(v)) > 80 else '')
                    for v in global_node_names.values()))
            abs_paths[len(abs_paths):], not_found_packages[
                len(not_found_packages):] = cls._load_parameters(
                    masteruri, global_node_names, [], user, pw,
                    auto_pw_request)
            launch_config.global_param_done.append(masteruri)

        # add params
        if not masteruri is None:
            nodens = ''.join([n.namespace, n.name, rospy.names.SEP])
            params = dict()
            for param, value in launch_config.Roscfg.params.items():
                if param.startswith(nodens):
                    params[param] = value
            clear_params = []
            for cparam in launch_config.Roscfg.clear_params:
                if cparam.startswith(nodens):
                    clear_params.append(cparam)
            rospy.loginfo("Delete parameter:\n  %s", '\n  '.join(clear_params))
            rospy.loginfo(
                "Register parameter:\n  %s", '\n  '.join(
                    "%s%s" % (str(v)[:80], '...' if len(str(v)) > 80 else '')
                    for v in params.values()))
            abs_paths[len(abs_paths):], not_found_packages[
                len(not_found_packages):] = cls._load_parameters(
                    masteruri, params, clear_params, user, pw, auto_pw_request)
        #'print "RUN prepared", node, time.time()

        if nm.is_local(host):
            nm.screen().testScreen()
            if item:
                cmd_type = item
            else:
                try:
                    cmd = roslib.packages.find_node(n.package, n.type)
                except (Exception, roslib.packages.ROSPkgException) as e:
                    # multiple nodes, invalid package
                    raise StartException(''.join(
                        ["Can't find resource: ",
                         str(e)]))
                # handle diferent result types str or array of string
                if isinstance(cmd, types.StringTypes):
                    cmd = [cmd]
                cmd_type = ''
                if cmd is None or len(cmd) == 0:
                    raise StartException(' '.join([
                        n.type, 'in package [', n.package,
                        '] not found!\n\nThe package was created?\nIs the binary executable?\n'
                    ]))
                if len(cmd) > 1:
                    if auto_pw_request:
                        # Open selection for executables, only if the method is called from the main GUI thread
                        try:
                            from python_qt_binding import QtGui
                            item, result = QtGui.QInputDialog.getItem(
                                None, ' '.join([
                                    'Multiple executables', n.type, 'in',
                                    n.package
                                ]), 'Select an executable', cmd, 0, False)
                            if result:
                                #open the selected screen
                                cmd_type = item
                            else:
                                return
                        except:
                            raise StartException(
                                'Multiple executables with same name in package found!'
                            )
                    else:
                        err = BinarySelectionRequest(cmd,
                                                     'Multiple executables')
                        raise nm.InteractionNeededError(
                            err, cls.runNode,
                            (node, launch_config, force2host, masteruri,
                             auto_pw_request, user, pw))
                else:
                    cmd_type = cmd[0]
            # determine the current working path, Default: the package of the node
            cwd = get_ros_home()
            if not (n.cwd is None):
                if n.cwd == 'ROS_HOME':
                    cwd = get_ros_home()
                elif n.cwd == 'node':
                    cwd = os.path.dirname(cmd_type)
            cls._prepareROSMaster(masteruri)
            node_cmd = [
                nm.settings().respawn_script if n.respawn else '', prefix,
                cmd_type
            ]
            cmd_args = [nm.screen().getSceenCmd(node)]
            cmd_args[len(cmd_args):] = node_cmd
            cmd_args.append(str(n.args))
            cmd_args[len(cmd_args):] = args
            rospy.loginfo("RUN: %s", ' '.join(cmd_args))
            new_env = dict(os.environ)
            new_env['ROS_MASTER_URI'] = masteruri
            # add the namespace environment parameter to handle some cases, e.g. rqt_cpp plugins
            if n.namespace:
                new_env['ROS_NAMESPACE'] = n.namespace
            for k, v in env:
                new_env[k] = v
            SupervisedPopen(shlex.split(str(' '.join(cmd_args))),
                            cwd=cwd,
                            env=new_env,
                            id="Run node",
                            description="Run node [%s]%s" %
                            (str(n.package), str(n.type)))
        else:
            #'print "RUN REMOTE", node, time.time()
            # start remote
            if launch_config.PackageName is None:
                raise StartException(''.join(
                    ["Can't run remote without a valid package name!"]))
            # thus the prefix parameters while the transfer are not separated
            if prefix:
                prefix = ''.join(['"', prefix, '"'])
            # setup environment
            env_command = ''
            if env_loader:
                rospy.logwarn(
                    "env_loader in machine tag currently not supported")
                raise StartException(
                    "env_loader in machine tag currently not supported")
            if env:
                new_env = dict()
                try:
                    for (k, v) in env:
                        v_value, is_abs_path, found, package = cls._resolve_abs_paths(
                            v, host, user, pw, auto_pw_request)
                        new_env[k] = v_value
                        if is_abs_path:
                            abs_paths.append(('ENV', "%s=%s" % (k, v),
                                              "%s=%s" % (k, v_value)))
                            if not found and package:
                                not_found_packages.append(package)
                    env_command = "env " + ' '.join(
                        ["%s=%s" % (k, v) for (k, v) in new_env.items()])
                except nm.AuthenticationRequest as e:
                    raise nm.InteractionNeededError(
                        e, cls.runNode, (node, launch_config, force2host,
                                         masteruri, auto_pw_request))

            startcmd = [
                env_command,
                nm.settings().start_remote_script, '--package',
                str(n.package), '--node_type',
                str(n.type), '--node_name',
                str(node), '--node_respawn true' if n.respawn else ''
            ]
            if not masteruri is None:
                startcmd.append('--masteruri')
                startcmd.append(masteruri)
            if prefix:
                startcmd[len(startcmd):] = ['--prefix', prefix]

            #rename the absolute paths in the args of the node
            node_args = []
            try:
                for a in n.args.split():
                    a_value, is_abs_path, found, package = cls._resolve_abs_paths(
                        a, host, user, pw, auto_pw_request)
                    node_args.append(a_value)
                    if is_abs_path:
                        abs_paths.append(('ARGS', a, a_value))
                        if not found and package:
                            not_found_packages.append(package)

                startcmd[len(startcmd):] = node_args
                startcmd[len(startcmd):] = args
                rospy.loginfo("Run remote on %s: %s", host,
                              str(' '.join(startcmd)))
                #'print "RUN CALL", node, time.time()
                _, stdout, stderr, ok = nm.ssh().ssh_exec(host,
                                                          startcmd,
                                                          user,
                                                          pw,
                                                          auto_pw_request,
                                                          close_stdin=True)
                output = stdout.read()
                error = stderr.read()
                stdout.close()
                stderr.close()
                #'print "RUN CALLOK", node, time.time()
            except nm.AuthenticationRequest as e:
                raise nm.InteractionNeededError(
                    e, cls.runNode, (node, launch_config, force2host,
                                     masteruri, auto_pw_request))

            if ok:
                if error:
                    rospy.logwarn("ERROR while start '%s': %s", node, error)
                    raise StartException(
                        str(''.join(
                            ['The host "', host, '" reports:\n', error])))
                if output:
                    rospy.loginfo("STDOUT while start '%s': %s", node, output)
            # inform about absolute paths in parameter value
            if len(abs_paths) > 0:
                rospy.loginfo(
                    "Absolute paths found while start:\n%s",
                    str('\n'.join([
                        ''.join([p, '\n  OLD: ', ov, '\n  NEW: ', nv])
                        for p, ov, nv in abs_paths
                    ])))

            if len(not_found_packages) > 0:
                packages = '\n'.join(not_found_packages)
                raise StartException(
                    str('\n'.join([
                        'Some absolute paths are not renamed because following packages are not found on remote host:',
                        packages
                    ])))
Exemplo n.º 8
0
    def runNodeWithoutConfig(cls,
                             host,
                             package,
                             binary,
                             name,
                             args=[],
                             masteruri=None,
                             auto_pw_request=False,
                             user=None,
                             pw=None):
        '''
    Start a node with using a launch configuration.
    @param host: the host or ip to run the node
    @type host: C{str} 
    @param package: the ROS package containing the binary
    @type package: C{str} 
    @param binary: the binary of the node to execute
    @type binary: C{str} 
    @param name: the ROS name of the node (with name space)
    @type name: C{str} 
    @param args: the list with arguments passed to the binary
    @type args: C{[str, ...]} 
    @param auto_pw_request: opens question dialog directly, use True only if the method is called from the main GUI thread
    @type auto_pw_request: bool
    @raise Exception: on errors while resolving host
    @see: L{node_manager_fkie.is_local()}
    '''
        # create the name with namespace
        args2 = list(args)
        fullname = roslib.names.ns_join(roslib.names.SEP, name)
        namespace = ''
        for a in args:
            if a.startswith('__ns:='):
                namespace = a.replace('__ns:=', '')
                fullname = roslib.names.ns_join(namespace, name)
        args2.append(''.join(['__name:=', name]))
        # run on local host
        if nm.is_local(host):
            try:
                cmd = roslib.packages.find_node(package, binary)
            except roslib.packages.ROSPkgException as e:
                # multiple nodes, invalid package
                raise StartException(str(e))
            # handle different result types str or array of string
#      import types
            if isinstance(cmd, types.StringTypes):
                cmd = [cmd]
            cmd_type = ''
            if cmd is None or len(cmd) == 0:
                raise StartException(' '.join(
                    [binary, 'in package [', package, '] not found!']))
            if len(cmd) > 1:
                # Open selection for executables
                #        try:
                #          from python_qt_binding import QtGui
                #          item, result = QtGui.QInputDialog.getItem(None, ' '.join(['Multiple executables', type, 'in', package]),
                #                                            'Select an executable',
                #                                            cmd, 0, False)
                #          if result:
                #            #open the selected screen
                #            cmd_type = item
                #          else:
                #            return
                #        except:
                err = [
                    ''.join([
                        'Multiple executables with same name in package [',
                        package, ']  found:'
                    ])
                ]
                err.extend(cmd)
                raise StartException('\n'.join(err))
            else:
                cmd_type = cmd[0]
            cmd_str = str(' '.join(
                [nm.screen().getSceenCmd(fullname), cmd_type,
                 ' '.join(args2)]))
            rospy.loginfo("Run without config: %s", cmd_str)
            ps = None
            new_env = dict(os.environ)
            if namespace:
                new_env['ROS_NAMESPACE'] = namespace
            if not masteruri is None:
                cls._prepareROSMaster(masteruri)
                new_env['ROS_MASTER_URI'] = masteruri
            SupervisedPopen(shlex.split(cmd_str),
                            env=new_env,
                            id="Run without config",
                            description="Run without config [%s]%s" %
                            (str(package), str(binary)))
        else:
            # run on a remote machine
            startcmd = [
                nm.settings().start_remote_script, '--package',
                str(package), '--node_type',
                str(binary), '--node_name',
                str(fullname)
            ]
            startcmd[len(startcmd):] = args2
            if not masteruri is None:
                startcmd.append('--masteruri')
                startcmd.append(masteruri)
            rospy.loginfo("Run remote on %s: %s", host, ' '.join(startcmd))
            try:
                _, stdout, stderr, ok = nm.ssh().ssh_exec(host,
                                                          startcmd,
                                                          user,
                                                          pw,
                                                          auto_pw_request,
                                                          close_stdin=True)
                if ok:
                    output = stdout.read()
                    error = stderr.read()
                    stdout.close()
                    stderr.close()
                    if error:
                        rospy.logwarn("ERROR while start '%s': %s", name,
                                      error)
                        raise StartException(''.join(
                            ['The host "', host, '" reports:\n', error]))

    #          from python_qt_binding import QtGui
    #          QtGui.QMessageBox.warning(None, 'Error while remote start %s'%str(name),
    #                                      str(''.join(['The host "', host, '" reports:\n', error])),
    #                                      QtGui.QMessageBox.Ok)
                    if output:
                        rospy.logdebug("STDOUT while start '%s': %s", name,
                                       output)
                else:
                    if error:
                        rospy.logwarn("ERROR while start '%s': %s", name,
                                      error)
                        raise StartException(''.join(
                            ['The host "', host, '" reports:\n', error]))
            except nm.AuthenticationRequest as e:
                raise nm.InteractionNeededError(
                    e, cls.runNodeWithoutConfig,
                    (host, package, binary, name, args, masteruri,
                     auto_pw_request))