Statistics
| Branch: | Tag: | Revision:

mininet / mininet / term.py @ 149a1f56

History | View | Annotate | Download (1.91 KB)

1 15b482e3 Brandon Heller
"""
2
Terminal creation and cleanup.
3
Utility functions to run a term (connected via screen(1)) on each host.
4

5
Requires GNU screen(1) and xterm(1).
6
Optionally uses gnome-terminal.
7
"""
8
9
import re
10
from subprocess import Popen
11
12
from mininet.log import error
13
from mininet.util import quietRun
14
15 9ab2cba8 Bob Lantz
def quoteArg( arg ):
16
    "Quote an argument if it contains spaces."
17
    return repr( arg ) if ' ' in arg else arg
18 82b72072 Bob Lantz
19 f583900d Bob Lantz
def makeTerm( node, title='Node', term='xterm' ):
20 de901ef0 Bob Lantz
    """Run screen on a node, and hook up a terminal.
21 15b482e3 Brandon Heller
       node: Node object
22
       title: base title
23 de901ef0 Bob Lantz
       term: 'xterm' or 'gterm'
24 15b482e3 Brandon Heller
       returns: process created"""
25
    title += ': ' + node.name
26
    if not node.inNamespace:
27
        title += ' (root)'
28 de901ef0 Bob Lantz
    cmds = {
29
        'xterm': [ 'xterm', '-title', title, '-e' ],
30
        'gterm': [ 'gnome-terminal', '--title', title, '-e' ]
31
    }
32
    if term not in cmds:
33 15b482e3 Brandon Heller
        error( 'invalid terminal type: %s' % term )
34
        return
35
    if not node.execed:
36 4a67fc9a Bob Lantz
        node.cmd( 'screen -dmS ' + 'mininet.' + node.name)
37 de901ef0 Bob Lantz
        args = [ 'screen', '-D', '-RR', '-S', 'mininet.' + node.name ]
38
    else:
39
        args = [ 'sh', '-c', 'exec tail -f /tmp/' + node.name + '*.log' ]
40
    if term == 'gterm':
41 82b72072 Bob Lantz
        # Compress these for gnome-terminal, which expects one token
42 9ab2cba8 Bob Lantz
        # to follow the -e option
43 82b72072 Bob Lantz
        args = [ ' '.join( [ quoteArg( arg ) for arg in args ] ) ]
44 de901ef0 Bob Lantz
    return Popen( cmds[ term ] + args )
45 15b482e3 Brandon Heller
46
def cleanUpScreens():
47
    "Remove moldy old screen sessions."
48 388f54e1 Bob Lantz
    r = r'(\d+\.mininet\.[hsc]\d+)'
49 15b482e3 Brandon Heller
    output = quietRun( 'screen -ls' ).split( '\n' )
50
    for line in output:
51
        m = re.search( r, line )
52
        if m:
53
            quietRun( 'screen -S ' + m.group( 1 ) + ' -X quit' )
54
55 f583900d Bob Lantz
def makeTerms( nodes, title='Node', term='xterm' ):
56 15b482e3 Brandon Heller
    """Create terminals.
57
       nodes: list of Node objects
58
       title: base title for each
59
       returns: list of created terminal processes"""
60
    return [ makeTerm( node, title, term ) for node in nodes ]