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