Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/idrac_lifecycle_controller_logs.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Module
6 # Version 10.0.1
7 # Copyright (C) 2018-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10
11
12 from __future__ import (absolute_import, division, print_function)
13 __metaclass__ = type
14
15 DOCUMENTATION = """
16 ---
17 module: idrac_lifecycle_controller_logs
18 short_description: Export Lifecycle Controller logs to a network share or local path.
19 version_added: "2.1.0"
20 description:
21 - Export Lifecycle Controller logs to a given network share or local path.
22 extends_documentation_fragment:
23 - dellemc.openmanage.idrac_auth_options
24 options:
25 share_name:
26 description:
27 - Network share or local path.
28 - CIFS, NFS network share types are supported.
29 type: str
30 required: true
31 share_user:
32 type: str
33 description: Network share user in the format 'user@domain' or 'domain\\user' if user is
34 part of a domain else 'user'. This option is mandatory for CIFS Network Share.
35 share_password:
36 type: str
37 description: Network share user password. This option is mandatory for CIFS Network Share.
38 aliases: ['share_pwd']
39 job_wait:
40 description: Whether to wait for the running job completion or not.
41 type: bool
42 default: true
43
44 requirements:
45 - "omsdk >= 1.2.488"
46 - "python >= 3.9.6"
47 author:
48 - "Rajeev Arakkal (@rajeevarakkal)"
49 - "Anooja Vardhineni (@anooja-vardhineni)"
50 - "Trisha Datta (@trisha-dell)"
51 notes:
52 - This module requires 'Administrator' privilege for I(idrac_user).
53 - Exporting data to a local share is supported only on iDRAC9-based PowerEdge Servers and later.
54 - Run this module from a system that has direct access to Dell iDRAC.
55 - This module supports both IPv4 and IPv6 address for I(idrac_ip).
56 - This module does not support C(check_mode).
57 - No job will be created when exporting data to a local share in iDRAC9 and iDRAC 10.
58 """
59
60 EXAMPLES = r'''
61 ---
62 - name: Export lifecycle controller logs to NFS share.
63 dellemc.openmanage.idrac_lifecycle_controller_logs:
64 idrac_ip: "190.168.0.1"
65 idrac_user: "user_name"
66 idrac_password: "user_password"
67 ca_path: "/path/to/ca_cert.pem"
68 share_name: "192.168.0.0:/nfsfileshare"
69
70 - name: Export lifecycle controller logs to CIFS share.
71 dellemc.openmanage.idrac_lifecycle_controller_logs:
72 idrac_ip: "190.168.0.1"
73 idrac_user: "user_name"
74 idrac_password: "user_password"
75 ca_path: "/path/to/ca_cert.pem"
76 share_name: "\\\\192.168.0.2\\share"
77 share_user: "share_user_name"
78 share_password: "share_user_pwd"
79
80 - name: Export lifecycle controller logs to LOCAL path.
81 dellemc.openmanage.idrac_lifecycle_controller_logs:
82 idrac_ip: "190.168.0.1"
83 idrac_user: "user_name"
84 idrac_password: "user_password"
85 ca_path: "/path/to/ca_cert.pem"
86 share_name: "/example/export_lc"
87 '''
88
89 RETURN = """
90 ---
91 msg:
92 type: str
93 description: Status of the export lifecycle controller logs job.
94 returned: always
95 sample: "Successfully exported the lifecycle controller logs."
96 lc_logs_status:
97 description: Status of the export operation along with job details and file path.
98 returned: success
99 type: dict
100 sample: {
101 "ElapsedTimeSinceCompletion": "0",
102 "InstanceID": "JID_274774785395",
103 "JobStartTime": "NA",
104 "JobStatus": "Completed",
105 "JobUntilTime": "NA",
106 "Message": "LCL Export was successful",
107 "MessageArguments": "NA",
108 "MessageID": "LC022",
109 "Name": "LC Export",
110 "PercentComplete": "100",
111 "Status": "Success",
112 "file": "192.168.0.0:/nfsfileshare/190.168.0.1_20210728_133437_LC_Log.log",
113 "retval": true
114 }
115 error_info:
116 description: Details of the HTTP Error.
117 returned: on HTTP error
118 type: dict
119 sample: {
120 "error": {
121 "code": "Base.1.0.GeneralError",
122 "message": "A general error has occurred. See ExtendedInfo for more information.",
123 "@Message.ExtendedInfo": [
124 {
125 "MessageId": "GEN1234",
126 "RelatedProperties": [],
127 "Message": "Unable to process the request because an error occurred.",
128 "MessageArgs": [],
129 "Severity": "Critical",
130 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
131 }
132 ]
133 }
134 }
135 """
136
137
138 import socket
139 import json
140 import copy
141 import datetime
142 from ansible_collections.dellemc.openmanage.plugins.module_utils.dellemc_idrac import iDRACConnection, idrac_auth_params
143 from ansible.module_utils.basic import AnsibleModule
144 from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI
145 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
146 from ansible.module_utils.urls import ConnectionError, SSLValidationError
147 from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_utils.\
148 idrac_lifecycle_controller_logs_utils import IDRACLifecycleControllerLogs
149 try:
150 from omsdk.sdkfile import file_share_manager
151 from omsdk.sdkcreds import UserCredentials
152 except ImportError:
153 pass
154 EXPORT_LC_LOGS = '/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellLCService/Actions/DellLCService.ExportLCLog'
155 SUCCESS_MSG = "Successfully exported the lifecycle controller logs."
156 SCHEDULE_MSG = "The export lifecycle controller log job is submitted successfully."
157 NO_CHANGES_FOUND_MSG = "No changes found to be applied."
158 CHANGES_FOUND_MSG = "Changes found to be applied."
159
160
161 def get_user_credentials(module):
162 share_username = module.params['share_user']
163 share_password = module.params['share_password']
164 work_group = None
165 if share_username is not None and "@" in share_username:
166 username_domain = share_username.split("@")
167 share_username = username_domain[0]
168 work_group = username_domain[1]
169 elif share_username is not None and "\\" in share_username:
170 username_domain = share_username.split("\\")
171 work_group = username_domain[0]
172 share_username = username_domain[1]
173 share = file_share_manager.create_share_obj(share_path=module.params['share_name'],
174 creds=UserCredentials(share_username, share_password,
175 work_group=work_group), isFolder=True)
176 return share
177
178
179 def run_export_lc_logs(idrac, module):
180 """
181 Export Lifecycle Controller Log to the given file share
182
183 Keyword arguments:
184 idrac -- iDRAC handle
185 module -- Ansible module
186 """
187
188 lclog_file_name_format = "%ip_%Y%m%d_%H%M%S_LC_Log.log"
189 share_username = module.params.get('share_user')
190 if (share_username is not None) and ("@" in share_username or "\\" in share_username):
191 myshare = get_user_credentials(module)
192 else:
193 myshare = file_share_manager.create_share_obj(share_path=module.params['share_name'],
194 creds=UserCredentials(module.params['share_user'],
195 module.params['share_password']),
196 isFolder=True)
197 data = socket.getaddrinfo(module.params["idrac_ip"], module.params["idrac_port"])
198 if "AF_INET6" == data[0][0]._name_:
199 lclog_file_name_format = get_file_name(module)
200 lc_log_file = myshare.new_file(lclog_file_name_format)
201 job_wait = module.params['job_wait']
202 msg = idrac.log_mgr.lclog_export(lc_log_file, job_wait)
203 return msg
204
205
206 def get_file_name(module):
207 file_name = None
208 ip = copy.deepcopy(module.params["idrac_ip"])
209 file_format = "{ip}_%Y%m%d_%H%M%S_LC_Log.log".format(ip=ip.replace(":", ".").replace("..", "."))
210 current_date = datetime.datetime.now()
211 current_date_str = current_date.strftime("%Y%m%d_%H%M%S")
212 file_name = file_format.replace("%Y%m%d_%H%M%S", current_date_str)
213 return file_name
214
215
216 # Main()
217 def main():
218 specs = {
219 "share_name": {"required": True, "type": 'str'},
220 "share_user": {"required": False, "type": 'str'},
221 "share_password": {"required": False, "type": 'str', "aliases": ['share_pwd'], "no_log": True},
222 "job_wait": {"required": False, "type": 'bool', "default": True},
223 }
224 specs.update(idrac_auth_params)
225 module = AnsibleModule(
226 argument_spec=specs,
227 supports_check_mode=False)
228
229 try:
230 with iDRACRedfishAPI(module.params) as idrac:
231 server_det = idrac.get_server_generation
232 server_hw_model = server_det[2]
233 if server_hw_model != "iDRAC 8":
234 lifecycle_controller_logs_obj = IDRACLifecycleControllerLogs(idrac)
235 msg, job_dict, changed = lifecycle_controller_logs_obj.lifecycle_controller_logs_operation(idrac, module)
236 module.exit_json(msg=msg, lc_logs_status=job_dict, changed=changed)
237 else:
238 with iDRACConnection(module.params) as idrac:
239 msg = run_export_lc_logs(idrac, module)
240 if msg.get("Status") in ["Failed", "Failure"] or msg.get("JobStatus") in ["Failed", "Failure"]:
241 msg.pop("file", None)
242 module.exit_json(
243 msg="Unable to export the lifecycle controller logs.",
244 lc_logs_status=msg, failed=True)
245 message = "Successfully exported the lifecycle controller logs."
246 if module.params['job_wait'] is False:
247 message = "The export lifecycle controller log job is submitted successfully."
248 module.exit_json(msg=message, lc_logs_status=msg)
249 except HTTPError as err:
250 module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
251 except URLError as err:
252 module.exit_json(msg=str(err), unreachable=True)
253 except (RuntimeError, SSLValidationError, ConnectionError, KeyError,
254 ImportError, ValueError, TypeError) as e:
255 module.exit_json(msg=str(e), failed=True)
256
257
258 if __name__ == '__main__':
259 main()