Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Set the auto_boot policy according to the caller's wishes. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | |
| 10 | save_path_0 = sys.path[0] |
| 11 | del sys.path[0] |
| 12 | |
| 13 | from gen_print import * |
| 14 | from gen_valid import * |
| 15 | from gen_arg import * |
| 16 | from gen_misc import * |
| 17 | from gen_cmd import * |
| 18 | from gen_plug_in_utils import * |
| 19 | from gen_call_robot import * |
| 20 | |
| 21 | # Restore sys.path[0]. |
| 22 | sys.path.insert(0, save_path_0) |
| 23 | |
| 24 | # Set exit_on_error for gen_valid functions. |
| 25 | set_exit_on_error(True) |
| 26 | |
| 27 | parser = argparse.ArgumentParser( |
| 28 | usage='%(prog)s [OPTIONS]', |
| 29 | description="%(prog)s will set the auto_boot policy according to the" |
| 30 | + " user's wishes.", |
| 31 | formatter_class=argparse.RawTextHelpFormatter, |
| 32 | prefix_chars='-+') |
| 33 | |
| 34 | |
| 35 | # Populate stock_list with options we want. |
| 36 | stock_list = [("test_mode", get_plug_default("test_mode", 0)), |
| 37 | ("quiet", get_plug_default("quiet", 0)), |
| 38 | ("debug", get_plug_default("debug", 0))] |
| 39 | |
| 40 | AUTO_REBOOT_DISABLE = "1" |
| 41 | |
| 42 | def exit_function(signal_number=0, |
| 43 | frame=None): |
| 44 | r""" |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 45 | Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 46 | """ |
| 47 | |
| 48 | dprint_executing() |
| 49 | dprint_var(signal_number) |
| 50 | |
| 51 | # Your cleanup code here. |
| 52 | |
| 53 | qprint_pgm_footer() |
| 54 | |
| 55 | |
| 56 | def signal_handler(signal_number, |
| 57 | frame): |
| 58 | r""" |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 59 | Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately |
| 60 | with return code 143 and without calling our exit_function. |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 61 | """ |
| 62 | |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 63 | # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly |
| 64 | # call exit_function from here. |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 65 | |
| 66 | dprint_executing() |
| 67 | |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 68 | # Calling exit prevents us from returning to the code that was running when we received the signal. |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 69 | exit(0) |
| 70 | |
| 71 | |
| 72 | def validate_parms(): |
| 73 | |
| 74 | r""" |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 75 | Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly. |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 76 | """ |
| 77 | |
| 78 | get_plug_vars() |
| 79 | |
Michael Walsh | 2ea965c | 2019-08-01 16:14:25 -0500 | [diff] [blame] | 80 | valid_value(AUTOBOOT_OPENBMC_HOST) |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 81 | global AUTO_REBOOT_DISABLE |
| 82 | if pgm_name == "cp_cleanup": |
| 83 | AUTO_REBOOT_DISABLE = 0 |
| 84 | else: |
| 85 | valid_value(AUTO_REBOOT_DISABLE, valid_values=["0", "1"]) |
| 86 | AUTO_REBOOT_DISABLE = int(AUTO_REBOOT_DISABLE) |
| 87 | |
| 88 | gen_post_validation(exit_function, signal_handler) |
| 89 | |
| 90 | |
| 91 | def main(): |
| 92 | |
| 93 | gen_get_options(parser, stock_list) |
| 94 | |
| 95 | validate_parms() |
| 96 | |
| 97 | qprint_pgm_header() |
| 98 | |
Michael Walsh | 80caea0 | 2019-06-21 11:31:41 -0500 | [diff] [blame] | 99 | print_plug_in_header() |
| 100 | |
| 101 | if pgm_name == "cp_setup" or pgm_name == "cp_cleanup": |
| 102 | exit_not_master() |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 103 | |
| 104 | init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".") |
| 105 | |
| 106 | lib_file_path = init_robot_file_path("lib/utils.robot") |
| 107 | |
Michael Walsh | 318a4fe | 2019-10-17 10:38:56 -0500 | [diff] [blame] | 108 | enable_auto_reboot = 1 - AUTO_REBOOT_DISABLE |
| 109 | print_var(enable_auto_reboot) |
| 110 | keyword_string = "Set Auto Reboot ${%i}" % enable_auto_reboot |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 111 | |
Michael Walsh | 046fe22 | 2019-11-08 14:33:53 -0600 | [diff] [blame] | 112 | cmd_buf = create_robot_cmd_string("extended/run_keyword.robot", OPENBMC_HOST, SSH_PORT, HTTPS_PORT, |
| 113 | REST_USERNAME, REST_PASSWORD, keyword_string, lib_file_path, quiet, |
| 114 | test_mode, debug, outputdir, output, log, report) |
Michael Walsh | 0a3bdb4 | 2019-01-31 16:21:44 +0000 | [diff] [blame] | 115 | if not robot_cmd_fnc(cmd_buf): |
| 116 | print_error_report("Robot command execution failed.") |
| 117 | exit(1) |
| 118 | |
| 119 | |
| 120 | main() |