1#!/usr/bin/env python3
2
3r"""
4Set the auto_boot policy according to the caller's wishes.
5"""
6
7import os
8import sys
9import time
10
11save_dir_path = sys.path.pop(0)
12
13modules = [
14    "gen_arg",
15    "gen_print",
16    "gen_valid",
17    "gen_misc",
18    "gen_cmd",
19    "gen_plug_in_utils",
20    "gen_call_robot",
21]
22for module in modules:
23    exec("from " + module + " import *")
24
25sys.path.insert(0, save_dir_path)
26
27
28# Set exit_on_error for gen_valid functions.
29set_exit_on_error(True)
30
31parser = argparse.ArgumentParser(
32    usage="%(prog)s [OPTIONS]",
33    description=(
34        "%(prog)s will set the auto_boot policy according to the user's"
35        " wishes."
36    ),
37    formatter_class=argparse.RawTextHelpFormatter,
38    prefix_chars="-+",
39)
40
41
42# Populate stock_list with options we want.
43stock_list = [
44    ("test_mode", get_plug_default("test_mode", 0)),
45    ("quiet", get_plug_default("quiet", 0)),
46    ("debug", get_plug_default("debug", 0)),
47]
48
49AUTO_REBOOT_DISABLE = "1"
50
51
52def validate_parms():
53    r"""
54    Validate program parameters, etc.  Return True or False (i.e. pass/fail) accordingly.
55    """
56
57    get_plug_vars()
58
59    valid_value(AUTOBOOT_OPENBMC_HOST)
60    global AUTO_REBOOT_DISABLE
61    if pgm_name == "cp_cleanup":
62        AUTO_REBOOT_DISABLE = 0
63    else:
64        valid_value(AUTO_REBOOT_DISABLE, valid_values=["0", "1"])
65        AUTO_REBOOT_DISABLE = int(AUTO_REBOOT_DISABLE)
66
67
68def main():
69    gen_setup()
70
71    set_term_options(term_requests="children")
72
73    print_plug_in_header()
74
75    if pgm_name == "cp_setup" or pgm_name == "cp_cleanup":
76        exit_not_master()
77
78    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
79
80    lib_file_path = init_robot_file_path("lib/utils.robot")
81
82    REDFISH_SUPPORT_TRANS_STATE = int(
83        os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0)
84    ) or int(os.environ.get("AUTOBOOT_REDFISH_SUPPORT_TRANS_STATE", 0))
85
86    enable_auto_reboot = 1 - AUTO_REBOOT_DISABLE
87    print_var(enable_auto_reboot)
88    keyword_string = "Set Auto Reboot Setting  ${%i}" % enable_auto_reboot
89
90    cmd_buf = create_robot_cmd_string(
91        "extended/run_keyword.robot",
92        OPENBMC_HOST,
93        SSH_PORT,
94        HTTPS_PORT,
95        REST_USERNAME,
96        REST_PASSWORD,
97        OPENBMC_USERNAME,
98        OPENBMC_PASSWORD,
99        IPMI_USERNAME,
100        IPMI_PASSWORD,
101        REDFISH_SUPPORT_TRANS_STATE,
102        keyword_string,
103        lib_file_path,
104        quiet,
105        test_mode,
106        debug,
107        outputdir,
108        output,
109        log,
110        report,
111    )
112
113    retry_count = 3
114    while not robot_cmd_fnc(cmd_buf):
115        retry_count -= 1
116        if retry_count == 0:
117            print_error_report("Robot command execution failed.")
118            exit(1)
119        time.sleep(30)
120    return
121
122
123main()
124