blob: 0136918950378e5785bf123536b03b4d1d18acb8 [file] [log] [blame]
Padraig Connollyfba8a952016-11-14 11:37:37 +00001#! /usr/bin/python
Padraigb21b6762016-09-21 14:59:02 +01002'''
3Copyright 2016 Intel Corporation
Ed Warnicke33007f52016-04-04 14:37:21 -07004
Padraigb21b6762016-09-21 14:59:02 +01005Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16'''
17
18from cmd import Cmd
19import os
20import subprocess
21import re
22import sys
Ed Warnickea2ac4672017-01-23 22:11:47 +000023from optparse import OptionParser
24
Padraigb21b6762016-09-21 14:59:02 +010025try:
26 import readline
27except ImportError:
28 readline = None
29
30persishist = os.path.expanduser('~/.vpphistory')
31persishist_size = 1000
32if not persishist:
33 os.mknod(persishist, stat.S_IFREG)
34
35class Vppctl(Cmd):
36
Ed Warnickea2ac4672017-01-23 22:11:47 +000037 def __init__(self,api_prefix=None):
38 Cmd.__init__(self)
39 self.api_prefix = api_prefix
40
Padraigb21b6762016-09-21 14:59:02 +010041 def historyWrite(self):
42 if readline:
43 readline.set_history_length(persishist_size)
44 readline.write_history_file(persishist)
45
46 def runVat(self, line):
47 input_prefix = "exec "
48 input_command = input_prefix + line
49 line_remove = '^load_one_plugin:'
50 s = '\n'
Ed Warnickea2ac4672017-01-23 22:11:47 +000051 if ( self.api_prefix is None):
52 command = ['vpp_api_test']
53 else:
54 command = ['vpp_api_test',"chroot prefix %s " % self.api_prefix]
Padraigb21b6762016-09-21 14:59:02 +010055
Pierre Pfisterf524f0a2016-11-03 13:14:29 +000056 if os.geteuid() != 0:
Ed Warnickea2ac4672017-01-23 22:11:47 +000057 command = ['sudo'] + command
Pierre Pfisterf524f0a2016-11-03 13:14:29 +000058
59 vpp_process = subprocess.Popen(command,
Padraigb21b6762016-09-21 14:59:02 +010060 stderr=subprocess.PIPE,
61 stdin=subprocess.PIPE,
62 stdout=subprocess.PIPE)
63 stdout_value = vpp_process.communicate(input_command)[0]
64
65 buffer_stdout = stdout_value.splitlines()
66
67 buffer_stdout[:] = [b for b in buffer_stdout
68 if line_remove not in b]
69
70 for i, num in enumerate(buffer_stdout):
71 buffer_stdout[i] = num.replace('vat# ','')
72
73 stdout_value = s.join(buffer_stdout)
74 print stdout_value
75
76 def do_help(self, line):
77 self.runVat("help")
78
79 def default(self, line):
80 self.runVat(line)
81
82 def do_exit(self, line):
83 self.historyWrite()
Padraigb21b6762016-09-21 14:59:02 +010084 raise SystemExit
85
86 def emptyline(self):
87 pass
88
Padraig Connolly1f417aa2016-11-28 10:21:19 +000089 def do_EOF(self,line):
90 self.historyWrite()
91 sys.stdout.write('\n')
92 raise SystemExit
93
Padraigb21b6762016-09-21 14:59:02 +010094 def preloop(self):
95 if readline and os.path.exists(persishist):
96 readline.read_history_file(persishist)
97
98 def postcmd(self, stop, line):
99 self.historyWrite()
100
101if __name__ == '__main__':
Ed Warnickea2ac4672017-01-23 22:11:47 +0000102 parser = OptionParser()
103 parser.add_option("-p","--prefix",action="store",type="string",dest="prefix")
104 (options,command_args) = parser.parse_args(sys.argv)
Padraig Connollyfba8a952016-11-14 11:37:37 +0000105
Padraigb21b6762016-09-21 14:59:02 +0100106 if not len(command_args) > 1:
Ed Warnickea2ac4672017-01-23 22:11:47 +0000107 prompt = Vppctl(options.prefix)
Padraig Connolly1f417aa2016-11-28 10:21:19 +0000108 red_set = '\033[31m'
109 norm_set = '\033[0m'
110 if sys.stdout.isatty():
Ed Warnickea2ac4672017-01-23 22:11:47 +0000111 if(options.prefix is None):
112 prompt.prompt = 'vpp# '
113 else:
114 prompt.prompt = '%s# ' % options.prefix
Padraig Connolly1f417aa2016-11-28 10:21:19 +0000115 try:
116 prompt.cmdloop(red_set + " _______ _ " + norm_set + " _ _____ ___ \n" +
117 red_set + " __/ __/ _ \ (_)__ " + norm_set + " | | / / _ \/ _ \\\n" +
118 red_set + " _/ _// // / / / _ \\" + norm_set + " | |/ / ___/ ___/\n" +
119 red_set + " /_/ /____(_)_/\___/ " + norm_set + "|___/_/ /_/ \n")
120 except KeyboardInterrupt:
121 sys.stdout.write('\n')
122 else:
123 try:
Andrew Licaffe092016-12-01 17:05:37 +0800124 prompt.cmdloop()
Padraig Connolly1f417aa2016-11-28 10:21:19 +0000125 except KeyboardInterrupt:
126 sys.stdout.write('\n')
Padraigb21b6762016-09-21 14:59:02 +0100127 else:
128 del command_args[0]
129 stdout_value = " ".join(command_args)
Ed Warnickea2ac4672017-01-23 22:11:47 +0000130 VatAddress = Vppctl(options.prefix)
Padraigb21b6762016-09-21 14:59:02 +0100131 VatAddress.runVat(stdout_value)
132
Padraig Connollyfba8a952016-11-14 11:37:37 +0000133
134