3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """VPP Configuration Main Entry"""
24 from vpplib.AutoConfig import AutoConfig
25 from vpplib.VPPUtil import VPPUtil
27 VPP_DRYRUNDIR = '/vpp/vpp-config/dryrun'
28 VPP_AUTO_CONFIGURATION_FILE = '/vpp/vpp-config/configs/auto-config.yaml'
29 VPP_HUGE_PAGE_FILE = '/vpp/vpp-config/dryrun/sysctl.d/80-vpp.conf'
30 VPP_STARTUP_FILE = '/vpp/vpp-config/dryrun/vpp/startup.conf'
31 VPP_GRUB_FILE = '/vpp/vpp-config/dryrun/default/grub'
32 VPP_REAL_HUGE_PAGE_FILE = '/etc/sysctl.d/80-vpp.conf'
33 VPP_REAL_STARTUP_FILE = '/etc/vpp/startup.conf'
34 VPP_REAL_GRUB_FILE = '/etc/default/grub'
39 def autoconfig_yn(question, default):
41 Ask the user a yes or no question.
43 :param question: The text of the question
44 :param default: Value to be returned if '\n' is entered
45 :type question: string
51 default = default.lower()
53 while not input_valid:
54 answer = raw_input(question)
57 if re.findall(r'[YyNn]', answer):
59 answer = answer[0].lower()
61 print "Please answer Y, N or Return."
66 def autoconfig_cp(node, src, dst):
68 Copies a file, saving the original if needed.
70 :param node: Node dictionary with cpuinfo.
71 :param src: Source File
72 :param dst: Destination file
76 :raises RuntimeError: If command fails
79 # If the destination file exist, create a copy if one does not already
82 (ret, stdout, stderr) = VPPUtil.exec_command('ls {}'.format(dst))
84 cmd = 'cp {} {}'.format(dst, ofile)
85 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
87 raise RuntimeError('{} failed on node {} {} {}'.
93 # Copy the source file
94 cmd = 'cp {} {}'.format(src, os.path.dirname(dst))
95 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
97 raise RuntimeError('{} failed on node {} {}'.
98 format(cmd, node['host'], stderr))
101 def autoconfig_diff(node, src, dst):
103 Returns the diffs of 2 files.
105 :param node: Node dictionary with cpuinfo.
106 :param src: Source File
107 :param dst: Destination file
113 :raises RuntimeError: If command fails
116 # Diff the files and return the output
117 cmd = "diff {} {}".format(src, dst)
118 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
120 raise RuntimeError('{} failed on node {} {} {}'.
129 def autoconfig_show_system():
131 Shows the system information.
135 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
142 def autoconfig_hugepage_apply(node, ask_questions=True):
144 Apply the huge page configuration.
145 :param node: The node structure
147 :param ask_questions: When True ask the user questions
148 :type ask_questions: bool
149 :returns: -1 if the caller should return, 0 if not
154 diffs = autoconfig_diff(node, VPP_REAL_HUGE_PAGE_FILE, rootdir + VPP_HUGE_PAGE_FILE)
156 print "These are the changes we will apply to"
157 print "the huge page file ({}).\n".format(VPP_REAL_HUGE_PAGE_FILE)
160 answer = autoconfig_yn("\nAre you sure you want to apply these changes [Y/n]? ", 'y')
165 autoconfig_cp(node, rootdir + VPP_HUGE_PAGE_FILE, VPP_REAL_HUGE_PAGE_FILE)
166 cmd = "sysctl -p {}".format(VPP_REAL_HUGE_PAGE_FILE)
167 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
169 raise RuntimeError('{} failed on node {} {} {}'.
170 format(cmd, node['host'], stdout, stderr))
172 print '\nThere are no changes to the huge page configuration.'
177 def autoconfig_vpp_apply(node, ask_questions=True):
179 Apply the vpp configuration.
181 :param node: The node structure
183 :param ask_questions: When True ask the user questions
184 :type ask_questions: bool
185 :returns: -1 if the caller should return, 0 if not
190 diffs = autoconfig_diff(node, VPP_REAL_STARTUP_FILE, rootdir + VPP_STARTUP_FILE)
192 print "These are the changes we will apply to"
193 print "the VPP startup file ({}).\n".format(VPP_REAL_STARTUP_FILE)
196 answer = autoconfig_yn("\nAre you sure you want to apply these changes [Y/n]? ", 'y')
200 # Copy the VPP startup
201 autoconfig_cp(node, rootdir + VPP_STARTUP_FILE, VPP_REAL_STARTUP_FILE)
203 print '\nThere are no changes to VPP startup.'
208 def autoconfig_grub_apply(node, ask_questions=True):
210 Apply the grub configuration.
212 :param node: The node structure
214 :param ask_questions: When True ask the user questions
215 :type ask_questions: bool
216 :returns: -1 if the caller should return, 0 if not
221 print "\nThe configured grub cmdline looks like this:"
222 configured_cmdline = node['grub']['default_cmdline']
223 current_cmdline = node['grub']['current_cmdline']
224 print configured_cmdline
225 print "\nThe current boot cmdline looks like this:"
226 print current_cmdline
228 question = "\nDo you want to keep the current boot cmdline [Y/n]? "
229 answer = autoconfig_yn(question, 'y')
233 node['grub']['keep_cmdline'] = False
236 diffs = autoconfig_diff(node, VPP_REAL_GRUB_FILE, rootdir + VPP_GRUB_FILE)
238 print "These are the changes we will apply to"
239 print "the GRUB file ({}).\n".format(VPP_REAL_GRUB_FILE)
242 answer = autoconfig_yn("\nAre you sure you want to apply these changes [y/N]? ", 'n')
246 # Copy and update grub
247 autoconfig_cp(node, rootdir + VPP_GRUB_FILE, VPP_REAL_GRUB_FILE)
248 distro = VPPUtil.get_linux_distro()
249 if distro[0] == 'Ubuntu':
252 cmd = "grub2-mkconfig -o /boot/grub2/grub.cfg"
254 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
256 raise RuntimeError('{} failed on node {} {} {}'.
257 format(cmd, node['host'], stdout, stderr))
259 print "There have been changes to the GRUB config a",
260 print "reboot will be required."
263 print '\nThere are no changes to the GRUB config.'
268 def autoconfig_apply(ask_questions=True):
270 Apply the configuration.
272 Show the diff of the dryrun file and the actual configuration file
273 Copy the files from the dryrun directory to the actual file.
274 Peform the system function
276 :param ask_questions: When true ask the user questions
277 :type ask_questions: bool
282 pkgs = vutil.get_installed_vpp_pkgs()
284 print "\nVPP is not installed, Install VPP with option 4."
287 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
290 print "\nWe are now going to configure your system(s).\n"
291 answer = autoconfig_yn("Are you sure you want to do this [Y/n]? ", 'y')
295 nodes = acfg.get_nodes()
296 for i in nodes.items():
299 # Check the system resources
300 if not acfg.min_system_resources(node):
307 ret = autoconfig_hugepage_apply(node, ask_questions)
312 ret = autoconfig_vpp_apply(node, ask_questions)
317 ret = autoconfig_grub_apply(node, ask_questions)
319 # We can still start VPP, even if we haven't configured grub
323 # Everything is configured start vpp
326 def autoconfig_dryrun(ask_questions=True):
328 Execute the dryrun function.
330 :param ask_questions: When true ask the user for paraameters
331 :type ask_questions: bool
336 pkgs = vutil.get_installed_vpp_pkgs()
338 print "\nVPP is not installed, please install VPP."
341 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE, clean=True)
343 # Stop VPP on each node
344 nodes = acfg.get_nodes()
345 for i in nodes.items():
352 # Check the system resources
353 nodes = acfg.get_nodes()
354 for i in nodes.items():
356 if not acfg.min_system_resources(node):
361 acfg.modify_devices()
363 acfg.update_interfaces_config()
366 acfg.modify_cpu(ask_questions)
368 # Calculate the cpu parameters
369 acfg.calculate_cpu_parameters()
371 # Acquire TCP stack parameters
373 acfg.acquire_tcp_params()
376 acfg.apply_vpp_startup()
378 # Apply the grub configuration
379 acfg.apply_grub_cmdline()
383 acfg.modify_huge_pages()
384 acfg.apply_huge_pages()
387 def autoconfig_install():
389 Install or Uninstall VPP.
393 # Since these commands will take a while, we
394 # want to see the progress
395 logger = logging.getLogger()
397 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
400 nodes = acfg.get_nodes()
401 for i in nodes.items():
404 pkgs = vutil.get_installed_vpp_pkgs()
407 print "\nThese packages are installed on node {}" \
408 .format(node['host'])
409 print "{:25} {}".format("Name", "Version")
412 print "{:25} {}".format(
413 pkg['name'], pkg['version'])
415 print "{}".format(pkg['name'])
417 question = "\nDo you want to uninstall these "
418 question += "packages [y/N]? "
419 answer = autoconfig_yn(question, 'n')
421 logger.setLevel(logging.INFO)
422 vutil.uninstall_vpp(node)
424 print "\nThere are no VPP packages on node {}." \
425 .format(node['host'])
426 question = "Do you want to install VPP [Y/n]? "
427 answer = autoconfig_yn(question, 'y')
429 logger.setLevel(logging.INFO)
430 vutil.install_vpp(node)
432 # Set the logging level back
433 logger.setLevel(logging.ERROR)
436 def autoconfig_patch_qemu():
438 Patch the correct qemu version that is needed for openstack
442 # Since these commands will take a while, we
443 # want to see the progress
444 logger = logging.getLogger()
446 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
448 nodes = acfg.get_nodes()
449 for i in nodes.items():
452 logger.setLevel(logging.INFO)
453 acfg.patch_qemu(node)
456 def autoconfig_ipv4_setup():
458 Setup IPv4 interfaces
462 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
463 acfg.ipv4_interface_setup()
466 def autoconfig_create_vm():
468 Setup IPv4 interfaces
472 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
473 acfg.create_and_bridge_virtual_interfaces()
476 def autoconfig_not_implemented():
478 This feature is not implemented
482 print "\nThis Feature is not implemented yet...."
485 def autoconfig_basic_test_menu():
487 The auto configuration basic test menu
491 basic_menu_text = '\nWhat would you like to do?\n\n\
492 1) List/Create Simple IPv4 Setup\n\
493 9 or q) Back to main menu.'
495 # 1) List/Create Simple IPv4 Setup\n\
496 # 2) List/Create Create VM and Connect to VPP interfaces\n\
497 # 9 or q) Back to main menu.'
500 print "{}".format(basic_menu_text)
504 while not input_valid:
505 answer = raw_input("\nCommand: ")
507 print "Please enter only 1 character."
509 if re.findall(r'[Qq1-29]', answer):
511 answer = answer[0].lower()
513 print "Please enter a character between 1 and 2 or 9."
521 def autoconfig_basic_test():
523 The auto configuration basic test menu
527 pkgs = vutil.get_installed_vpp_pkgs()
529 print "\nVPP is not installed, install VPP with option 4."
534 answer = autoconfig_basic_test_menu()
536 autoconfig_ipv4_setup()
537 # elif answer == '2':
538 # autoconfig_create_vm()
539 elif answer == '9' or answer == 'q':
542 autoconfig_not_implemented()
545 def autoconfig_main_menu():
547 The auto configuration main menu
551 main_menu_text = '\nWhat would you like to do?\n\n\
552 1) Show basic system information\n\
553 2) Dry Run (Will save the configuration files in {}/vpp/vpp-config/dryrun for inspection)\n\
554 and user input in {}/vpp/vpp-config/configs/auto-config.yaml\n\
555 3) Full configuration (WARNING: This will change the system configuration)\n\
556 4) List/Install/Uninstall VPP.\n\
557 5) Execute some basic tests.\n\
558 9 or q) Quit'.format(rootdir, rootdir)
560 # 5) Dry Run from {}/vpp/vpp-config/auto-config.yaml (will not ask questions).\n\
561 # 6) Install QEMU patch (Needed when running openstack).\n\
563 print "{}".format(main_menu_text)
567 while not input_valid:
568 answer = raw_input("\nCommand: ")
570 print "Please enter only 1 character."
572 if re.findall(r'[Qq1-79]', answer):
574 answer = answer[0].lower()
576 print "Please enter a character between 1 and 5 or 9."
583 def autoconfig_main():
585 The auto configuration main entry point
594 answer = autoconfig_main_menu()
596 autoconfig_show_system()
604 autoconfig_basic_test()
605 elif answer == '9' or answer == 'q':
608 autoconfig_not_implemented()
611 def autoconfig_setup(ask_questions=True):
613 The auto configuration setup function.
615 We will copy the configuration files to the dryrun directory.
621 logging.basicConfig(level=logging.ERROR)
623 distro = VPPUtil.get_linux_distro()
624 if distro[0] == 'Ubuntu':
625 rootdir = '/usr/local'
629 # If there is a system configuration file use that, if not use the initial auto-config file
630 filename = rootdir + VPP_AUTO_CONFIGURATION_FILE
631 if os.path.isfile(filename) is True:
632 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
634 raise RuntimeError('The Auto configuration file does not exist {}'.
638 print "\nWelcome to the VPP system configuration utility"
640 print "\nThese are the files we will modify:"
641 print " /etc/vpp/startup.conf"
642 print " /etc/sysctl.d/80-vpp.conf"
643 print " /etc/default/grub"
645 print "\nBefore we change them, we'll create working copies in {}".format(rootdir + VPP_DRYRUNDIR)
646 print "Please inspect them carefully before applying the actual configuration (option 3)!"
648 nodes = acfg.get_nodes()
649 for i in nodes.items():
652 if (os.path.isfile(rootdir + VPP_STARTUP_FILE) is not True) and \
653 (os.path.isfile(VPP_REAL_STARTUP_FILE) is True):
654 autoconfig_cp(node, VPP_REAL_STARTUP_FILE, '{}'.format(rootdir + VPP_STARTUP_FILE))
655 if (os.path.isfile(rootdir + VPP_HUGE_PAGE_FILE) is not True) and \
656 (os.path.isfile(VPP_REAL_HUGE_PAGE_FILE) is True):
657 autoconfig_cp(node, VPP_REAL_HUGE_PAGE_FILE, '{}'.format(rootdir + VPP_HUGE_PAGE_FILE))
658 if (os.path.isfile(rootdir + VPP_GRUB_FILE) is not True) and \
659 (os.path.isfile(VPP_REAL_GRUB_FILE) is True):
660 autoconfig_cp(node, VPP_REAL_GRUB_FILE, '{}'.format(rootdir + VPP_GRUB_FILE))
663 def execute_with_args(args):
665 Execute the configuration utility with agruments.
667 :param args: The Command line arguments
672 autoconfig_setup(ask_questions=False)
674 # Execute the command
676 autoconfig_show_system()
678 autoconfig_dryrun(ask_questions=False)
680 autoconfig_apply(ask_questions=False)
682 autoconfig_not_implemented()
687 The vpp configuration utility main entry point.
692 if not os.geteuid() == 0:
693 sys.exit('\nPlease run the VPP Configuration Utility as root.')
695 # If no arguments were entered, ask the user questions to get the main parameters
696 if len(sys.argv) == 1:
701 # There were arguments specified, so execute the utility using command line arguments
702 description = 'The VPP configuration utility allows the user to configure VPP in a simple and safe manner. \
703 The utility takes input from the user or the speficfied .yaml file. The user should then examine these files \
704 to be sure they are correct and then actually apply the configuration. When run without arguments the utility run \
705 in an interactive mode'
707 main_parser = argparse.ArgumentParser(prog='arg-test', description=description,
708 epilog='See "%(prog)s help COMMAND" for help on a specific command.')
709 main_parser.add_argument('--apply', '-a', action='store_true', help='Apply the cofiguration.')
710 main_parser.add_argument('--dry-run', '-dr', action='store_true', help='Create the dryrun configuration files.')
711 main_parser.add_argument('--show', '-s', action='store_true', help='Shows basic system information')
712 main_parser.add_argument('--debug', '-d', action='count', help='Print debug output (multiple levels)')
714 args = main_parser.parse_args()
716 return execute_with_args(args)
719 if __name__ == '__main__':