-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathone_time_boot.py
More file actions
executable file
·123 lines (121 loc) · 7.17 KB
/
one_time_boot.py
File metadata and controls
executable file
·123 lines (121 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""EZIMM -
Use This Wizard to Create Terraform HCL configuration from Question and Answer or the IMM Transition Tool.
It uses argparse to take in the following CLI arguments:
-a or --intersight-api-key-id: The Intersight API key id for HTTP signature scheme.
-d or --dir: Base Directory to use for creation of the YAML Configuration Files.
-dl or --debug-level: The Debug Level to Run for Script Output
1. Shows the api request response status code
5. Shows URL String + Lower Options
6. Adds Results + Lower Options
7. Adds json payload + Lower Options
Note: payload shows as pretty and straight to check for stray object types like Dotmap and numpy
-f or --intersight-fqdn: The Intersight hostname for the API endpoint. The default is intersight.com.
-i or --ignore-tls: Ignore TLS server-side certificate verification. Default is False.
-j or --json_file: IMM Transition JSON export to convert to HCL.
-l or --load-config Flag to Load Previously Saved YAML Configuration Files.
-k or --intersight-secret-key: Name of the file containing The Intersight secret key for the HTTP signature scheme.
-t or --deployment-method: Deployment Method. Values are: Intersight or Terraform
-v or --api-key-v3: Flag for API Key Version 3.
"""
#=============================================================================
# Source Modules
#=============================================================================
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
import os, sys
script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
sys.path.insert(0, f'{script_path}{os.sep}classes')
try:
from classes import ezfunctions, isight, pcolor
from copy import deepcopy
from dotmap import DotMap
import argparse, json, os, re, requests, time, urllib3, yaml
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
except ImportError as e:
prRed(f'EZIMM - !!! ERROR !!!\n{e.__class__.__name__}')
prRed(f" Module {e.name} is required to run this script")
prRed(f" Install the module using the following: `pip install {e.name}`")
sys.exit(1)
#=============================================================================
# Function: Parse Arguments
#=============================================================================
def cli_arguments():
parser = argparse.ArgumentParser(description ='Intersight Easy IMM Deployment Module')
parser = ezfunctions.base_arguments(parser)
parser = ezfunctions.base_arguments_ezimm_sensitive_variables(parser)
parser.add_argument(
'-iso', '--mapping-iso', default ='',
help = 'Name of the ISO file to mapping:')
return DotMap(args = parser.parse_args())
#=============================================================================
# Function: Parse Arguments
#=============================================================================
def hosts(kwargs):
#=========================================================================
# Import YAML Data
#=========================================================================
yaml_file = open(os.path.join(kwargs.args.yaml_file), 'r')
ydata = DotMap(yaml.safe_load(yaml_file))
yaml_file.close()
#=========================================================================
# Patch Virtual Media
#=========================================================================
kwargs.org = ydata.vmedia.organization
kwargs = isight.api('organization').all_organizations(kwargs)
kwargs = kwargs | DotMap(method = 'get', names = [ydata.vmedia.name], uri = 'vmedia/Policies')
kwargs = isight.api('virtual_media').calls(kwargs)
api_body = {
"Mappings": [{
'AuthenticationProtocol': 'none', 'DeviceType': 'cdd',
'FileLocation': f'{ydata.vmedia.url}', 'MountOptions': 'noauto', 'MountProtocol': 'https'
}]
}
kwargs = kwargs | DotMap(api_body = api_body, method = 'patch', pmoid = kwargs.pmoids[ydata.vmedia.name].moid, uri = 'vmedia/Policies')
kwargs = isight.api('virtual_media').calls(kwargs)
pcolor.Cyan(f'\n{"-"*108}\n\n !!! Virtual Media Patch Complete. Sleeping for Profile Validation !!!\n\n{"-"*108}\n')
time.sleep(30)
#=========================================================================
# Setup Variables
#=========================================================================
profiles = []
for e in ydata.profiles:
profiles.append(DotMap(action = 'Deploy', name = e.name, serial_number = e.serial_number))
#=========================================================================
# Deploy Server Profiles
#=========================================================================
kwargs.org = ydata.profiles_organization
kwargs = isight.imm('profiles.server').profiles_chassis_server_deploy(profiles, kwargs)
#=========================================================================
# Set One Time Boot and Reboot
#=========================================================================
physical_servers = DotMap()
kwargs = kwargs | DotMap(method = 'get', names = [e.name for e in profiles], uri = 'server/Profiles')
kwargs = isight.api('server').calls(kwargs)
for e in kwargs.results: physical_servers[e.AssignedServer.Moid] = DotMap(name = e.Name, object_type = e.AssignedServer.ObjectType)
kwargs = kwargs | DotMap(method = 'get', names = [k for k,v in physical_servers.items()], uri = 'compute/ServerSettings')
kwargs = isight.api('ancestors').calls(kwargs)
for e in kwargs.results: physical_servers[e.Ancestors[0].Moid].server_settings_moid = e.Moid
pcolor.Cyan(f'\n{"-"*108}\n')
for k, v in physical_servers.items():
api_body = {'AdminPowerState': "PowerCycle", 'OneTimeBootDevice': ydata.boot_device}
kwargs = kwargs | DotMap(api_body = api_body, method = 'post_by_moid', pmoid = v.server_settings_moid)
kwargs = isight.api('servers').calls(kwargs)
pcolor.Cyan(f' * One Time Boot Set for Server: {v.name} - Moid: {k}')
pcolor.Cyan(f'\n{"-"*108}\n')
#=============================================================================
# Function: Main Script
#=============================================================================
def main():
#=========================================================================
# Configure Base Module Setup
#=========================================================================
kwargs = cli_arguments()
kwargs = ezfunctions.base_script_settings(kwargs)
#=========================================================================
# Set Virtual Media and Reboot
#=========================================================================
kwargs = hosts(kwargs)
pcolor.Cyan(f'\n{"-"*108}\n\n !!! Procedures Complete !!!\n Closing Environment and Exiting Script...\n\n{"-"*108}\n')
sys.exit(0)
if __name__ == '__main__':
main()