Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 2 | ''' |
| 3 | Copyright 2016 Intel Corporation |
Ed Warnicke | 33007f5 | 2016-04-04 14:37:21 -0700 | [diff] [blame] | 4 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 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 |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 23 | from optparse import OptionParser |
| 24 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 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 | |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 37 | def __init__(self,api_prefix=None): |
| 38 | Cmd.__init__(self) |
| 39 | self.api_prefix = api_prefix |
| 40 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 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' |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 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] |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 55 | |
Pierre Pfister | f524f0a | 2016-11-03 13:14:29 +0000 | [diff] [blame] | 56 | if os.geteuid() != 0: |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 57 | command = ['sudo'] + command |
Pierre Pfister | f524f0a | 2016-11-03 13:14:29 +0000 | [diff] [blame] | 58 | |
| 59 | vpp_process = subprocess.Popen(command, |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 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() |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 84 | raise SystemExit |
| 85 | |
| 86 | def emptyline(self): |
| 87 | pass |
| 88 | |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 89 | def do_EOF(self,line): |
| 90 | self.historyWrite() |
| 91 | sys.stdout.write('\n') |
| 92 | raise SystemExit |
| 93 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 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__': |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 102 | parser = OptionParser() |
| 103 | parser.add_option("-p","--prefix",action="store",type="string",dest="prefix") |
| 104 | (options,command_args) = parser.parse_args(sys.argv) |
Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 105 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 106 | if not len(command_args) > 1: |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 107 | prompt = Vppctl(options.prefix) |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 108 | red_set = '\033[31m' |
| 109 | norm_set = '\033[0m' |
| 110 | if sys.stdout.isatty(): |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 111 | if(options.prefix is None): |
| 112 | prompt.prompt = 'vpp# ' |
| 113 | else: |
| 114 | prompt.prompt = '%s# ' % options.prefix |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 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: |
Andrew Li | caffe09 | 2016-12-01 17:05:37 +0800 | [diff] [blame] | 124 | prompt.cmdloop() |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 125 | except KeyboardInterrupt: |
| 126 | sys.stdout.write('\n') |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 127 | else: |
| 128 | del command_args[0] |
| 129 | stdout_value = " ".join(command_args) |
Ed Warnicke | a2ac467 | 2017-01-23 22:11:47 +0000 | [diff] [blame^] | 130 | VatAddress = Vppctl(options.prefix) |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 131 | VatAddress.runVat(stdout_value) |
| 132 | |
Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 133 | |
| 134 | |