1#!/usr/bin/env python
2
3r"""
4Set the auto_boot policy according to the caller's wishes.
5"""
6
7import os
8import sys
9
10save_path_0 = sys.path[0]
11del sys.path[0]
12
13from gen_print import *
14from gen_valid import *
15from gen_arg import *
16from gen_misc import *
17from gen_cmd import *
18from gen_plug_in_utils import *
19from gen_call_robot import *
20
21# Restore sys.path[0].
22sys.path.insert(0, save_path_0)
23
24# Set exit_on_error for gen_valid functions.
25set_exit_on_error(True)
26
27parser = 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.
36stock_list = [("test_mode", get_plug_default("test_mode", 0)),
37              ("quiet", get_plug_default("quiet", 0)),
38              ("debug", get_plug_default("debug", 0))]
39
40AUTO_REBOOT_DISABLE = "1"
41
42def exit_function(signal_number=0,
43                  frame=None):
44    r"""
45    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
46    """
47
48    dprint_executing()
49    dprint_var(signal_number)
50
51    # Your cleanup code here.
52
53    qprint_pgm_footer()
54
55
56def signal_handler(signal_number,
57                   frame):
58    r"""
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.
61    """
62
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.
65
66    dprint_executing()
67
68    # Calling exit prevents us from returning to the code that was running when we received the signal.
69    exit(0)
70
71
72def validate_parms():
73
74    r"""
75    Validate program parameters, etc.  Return True or False (i.e. pass/fail) accordingly.
76    """
77
78    get_plug_vars()
79
80    valid_value(AUTOBOOT_OPENBMC_HOST)
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
91def main():
92
93    gen_get_options(parser, stock_list)
94
95    validate_parms()
96
97    qprint_pgm_header()
98
99    print_plug_in_header()
100
101    if pgm_name == "cp_setup" or pgm_name == "cp_cleanup":
102        exit_not_master()
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
108    enable_auto_reboot = 1 - AUTO_REBOOT_DISABLE
109    print_var(enable_auto_reboot)
110    keyword_string = "Set Auto Reboot  ${%i}" % enable_auto_reboot
111
112    cmd_buf = create_robot_cmd_string("extended/run_keyword.robot",
113                                      OPENBMC_HOST, REST_USERNAME,
114                                      REST_PASSWORD, keyword_string,
115                                      lib_file_path, quiet, test_mode, debug,
116                                      outputdir, output, log, report)
117    if not robot_cmd_fnc(cmd_buf):
118        print_error_report("Robot command execution failed.")
119        exit(1)
120
121
122main()
123