Clean up old datapath code in ACL plugin.
[vpp.git] / src / scripts / vppctl
1 #! /usr/bin/python
2 '''
3 Copyright 2016 Intel Corporation
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 '''
17
18 from cmd import Cmd
19 import os
20 import subprocess
21 import re
22 import sys
23 from optparse import OptionParser
24
25 try:
26         import readline
27 except ImportError:
28         readline = None
29
30 persishist = os.path.expanduser('~/.vpphistory')
31 persishist_size = 1000
32 if not persishist:
33         os.mknod(persishist, stat.S_IFREG)
34
35 class Vppctl(Cmd):
36
37         def __init__(self,api_prefix=None):
38                 Cmd.__init__(self)
39                 self.api_prefix = api_prefix
40
41         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'
51                 if ( self.api_prefix is None):
52                         command = ['vpp_api_test']
53                 else:
54                         command = ['vpp_api_test',"chroot prefix %s " % self.api_prefix]
55
56                 if os.geteuid() != 0:
57                     command = ['sudo'] + command
58
59                 vpp_process = subprocess.Popen(command,
60                         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()
84                 raise SystemExit
85
86         def emptyline(self):
87                 pass
88
89         def do_EOF(self,line):
90                 self.historyWrite()
91                 sys.stdout.write('\n')
92                 raise SystemExit
93
94         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
101 if __name__ == '__main__':
102         parser = OptionParser()
103         parser.add_option("-p","--prefix",action="store",type="string",dest="prefix")
104         (options,command_args) = parser.parse_args(sys.argv)
105
106         if not len(command_args) > 1:
107                 prompt = Vppctl(options.prefix)
108                 red_set = '\033[31m'
109                 norm_set = '\033[0m'
110                 if sys.stdout.isatty():
111                         if(options.prefix is None):
112                                 prompt.prompt = 'vpp# '
113                         else:
114                                 prompt.prompt = '%s# ' % options.prefix
115                         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:
124                                 prompt.cmdloop()
125                         except KeyboardInterrupt:
126                                 sys.stdout.write('\n')
127         else:
128                 del command_args[0]
129                 stdout_value = " ".join(command_args)
130                 VatAddress = Vppctl(options.prefix)
131                 VatAddress.runVat(stdout_value)
132
133
134