forked from dell/dellemc-openmanage-ansible-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.py
255 lines (233 loc) · 10.5 KB
/
uninstall.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Dell EMC OpenManage Ansible Modules
# Version 2.0.8
# Copyright (C) 2019-2020 Dell Inc.
# GNU General Public License v3.0+ (see COPYING or
# https://www.gnu.org/licenses/gpl-3.0.txt)
# All rights reserved. Dell, EMC, and other trademarks are trademarks of
# Dell Inc. or its subsidiaries.
# Other trademarks may be trademarks of their respective owners.
#
"""
Un-installation of DellEMC OpenManage Ansible Module.
"""
import os
import re
import sys
import glob
import shutil
# print colors
FAIL = "\033[91m" # RED
SUCCESS = "\033[92m" # GREEN
NORMAL = "\033[00m" # DEFAULT
NO_ANSIBLE_MESSAGE = "\nDell EMC OpenManage Ansible Modules is not " \
"installed.\n"
# if any one of them are present proceeding with further process.
STARTED_MESSAGE = "Dell EMC OpenManage Ansible Modules uninstallation has " \
"started."
FOLDER_MESSAGE = "\n\tUninstalling Dell EMC OpenManage Ansible Modules " \
"specific folders and files...\n"
SUCCESS_MESSAGE = "SUCCESS: Dell EMC OpenManage Ansible Modules is " \
"uninstalled successfully."
FAILED_MESSAGE = "FAILED: Dell EMC OpenManage Ansible Modules uninstallation" \
" failed."
START_END = "-------------------------------------------------------------" \
"------------------------------------"
try:
import ansible
except ImportError:
print("\n" + START_END)
print(FAIL + "{0}".format(NO_ANSIBLE_MESSAGE) + NORMAL)
print(START_END + "\n")
sys.exit(1)
try:
# Ansible and dellemc installed location path.
if 'ANSIBLE_LIBRARY' in os.environ:
ANSIBLE_INSTALLED_PATH = os.environ['ANSIBLE_LIBRARY']
else:
ANSIBLE_INSTALLED_PATH = ansible.__path__[0]
ANSIBLE_VERSION = ansible.__version__
except (AttributeError, TypeError):
print("\n" + START_END)
print(FAIL + "{0}".format(NO_ANSIBLE_MESSAGE) + NORMAL)
print(START_END + "\n")
sys.exit(1)
# dellemc module path
DELLEMC_PATH = os.path.join(ANSIBLE_INSTALLED_PATH, "modules",
"remote_management", "dellemc")
DELLEMC_IDRAC_PATH = os.path.join(ANSIBLE_INSTALLED_PATH, "modules",
"remote_management", "dellemc", "idrac")
DELLEMC_OME_PATH = os.path.join(ANSIBLE_INSTALLED_PATH, "modules",
"remote_management", "dellemc", "ome")
DELLEMC_REDFISH_PATH = os.path.join(ANSIBLE_INSTALLED_PATH, "modules",
"remote_management", "dellemc", "redfish")
# dellemc util path
DELLEMC_UTIL_PATH = os.path.join(ANSIBLE_INSTALLED_PATH, "module_utils",
"remote_management", "dellemc")
OLD_UTIL_FILE = os.path.join(ANSIBLE_INSTALLED_PATH, "module_utils",
"dellemc_idrac.py")
# contributed module details for skipping if ansible 2.8 or more than exists.
CONTRIB_MODULE_FILES = {
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/idrac/"
"idrac_firmware.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/idrac"
"/idrac_server_config_profile.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/idrac"
"/__init__.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/"
"__init__.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/"
"ome_device_info.py"): "ansible 2.9.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/"
"__init__.py"): "ansible 2.8.4",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc/"
"idrac_firmware.py"): "ansible 2.8.4",
os.path.join(ANSIBLE_INSTALLED_PATH,
"modules/remote_management/dellemc"
"/idrac_server_config_profile.py"): "ansible 2.8.4",
}
CONTRIB_UTIL_FILES = {
os.path.join(ANSIBLE_INSTALLED_PATH,
"module_utils/remote_management/dellemc/"
"dellemc_idrac.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"module_utils/remote_management/dellemc/"
"__init__.py"): "ansible 2.8.0",
os.path.join(ANSIBLE_INSTALLED_PATH,
"module_utils/remote_management/dellemc/"
"ome.py"): "ansible 2.9.0",
}
# Any of the path is not present exit with message.
if not any(os.path.exists(exists) for exists in
(DELLEMC_PATH, DELLEMC_UTIL_PATH, OLD_UTIL_FILE)):
print("\n" + START_END)
print(FAIL + "{0}".format(NO_ANSIBLE_MESSAGE) + NORMAL)
print(START_END + "\n")
sys.exit(1)
def complete_remove(*args):
"""Completely removing folders, except contributed files or folders."""
for arg in args:
if os.path.isdir(arg):
shutil.rmtree(arg)
if os.path.isfile(arg):
os.remove(arg)
def check_ome_contributed():
"""Checks if contributed files present or not."""
is_contributed = False
if not os.path.exists(DELLEMC_IDRAC_PATH):
is_contributed = True
return is_contributed
def version_check(version):
"""Data type conversion of ansible version."""
return tuple(map(int, (version.split("."))))
def uninstall():
"""Uninstalling Dell EMC ansible modules from ansible core repository."""
version = re.match(r"(\d.\d)", ANSIBLE_VERSION).group()
# listing out all the installed modules from the location.
module_files = [f for f in glob.glob(os.path.join(DELLEMC_PATH, "*.py"))]
# listing out all the utility files from the installed location.
util_files = [f for f in
glob.glob(os.path.join(DELLEMC_UTIL_PATH, "*.py"))]
# Step 1: checking the installed ansible version,
# if it's more than 2.8 or equal skipping contributed modules.
if all(map(lambda x, y: x <= y, (2, 8),
tuple(map(int, version.split("."))))):
if len(module_files) > len(CONTRIB_MODULE_FILES):
# skipping contributed modules/utility files from installed
# location.
removed_module = list(
set(module_files) - set(CONTRIB_MODULE_FILES.keys()))
removed_util = list(
set(util_files) - set(CONTRIB_UTIL_FILES.keys()))
removed_module.extend(removed_util)
removed_module.append(DELLEMC_OME_PATH)
removed_module.append(DELLEMC_REDFISH_PATH)
if version_check(ANSIBLE_VERSION) < version_check("2.9.0"):
# remove ome modules if ansible version is lower than 2.9
remove_module_list, remove_modules_dict = [], {}
remove_modules_dict.update(CONTRIB_MODULE_FILES)
remove_modules_dict.update(CONTRIB_UTIL_FILES)
for key, val in remove_modules_dict.items():
if val == "ansible 2.9.0" or \
(val == "ansible 2.8.4" and
version_check(ANSIBLE_VERSION) <
version_check("2.8.4")) and \
"__init__" not in key:
remove_module_list.append(key)
removed_module.extend(remove_module_list)
print("\n" + START_END)
print(STARTED_MESSAGE)
print(FOLDER_MESSAGE)
# removing installed files.
complete_remove(*removed_module)
print(SUCCESS + "{0}".format(SUCCESS_MESSAGE) + NORMAL)
print(START_END + "\n")
elif os.path.exists(DELLEMC_IDRAC_PATH) and not os.path.exists(
DELLEMC_OME_PATH):
# Solve case
# when OMAM 1.1 to 2.1 upgrade is done with ansible version
# lesser than 2.8.4
# where idrac folder exists with contributed_file_lst.
# in this case upgrade is not removing other than contributed
# files inside idrac folder
# idrac_util_exists = os.path.exists(os.path.join(
# DELLEMC_UTIL_PATH, "dellemc_idrac.py"))
property_json = os.path.join(DELLEMC_PATH, "properties.json")
extras = os.path.join(ANSIBLE_INSTALLED_PATH, "modules", "extras")
old_ome_file = os.path.join(ANSIBLE_INSTALLED_PATH, "module_utils",
"remote_management", "dellemc",
"dellemc_ome.py")
contributed_file_lst = {DELLEMC_IDRAC_PATH + '/idrac_firmware.py',
DELLEMC_IDRAC_PATH +
'/idrac_server_config_profile.py'}
old_module_idrac_files = [f for f in glob.glob(
os.path.join(DELLEMC_IDRAC_PATH, "*.py"))]
removed_module = list(
set(old_module_idrac_files) - contributed_file_lst)
if len(old_module_idrac_files) == len(removed_module):
complete_remove(DELLEMC_IDRAC_PATH)
else:
complete_remove(*removed_module)
print("\n" + START_END)
print(STARTED_MESSAGE)
print(FOLDER_MESSAGE)
complete_remove(OLD_UTIL_FILE, property_json, extras, old_ome_file)
print(SUCCESS + "{0}".format(SUCCESS_MESSAGE) + NORMAL)
print(START_END + "\n")
else:
print("\n" + START_END)
print(FAIL + "{0}".format(NO_ANSIBLE_MESSAGE) + NORMAL)
print(START_END + "\n")
# Step 2: installed ansible version is less than 2.8,
# removing all the files from installed location.
elif any(map(lambda x, y: x <= y, tuple(map(int, version.split("."))),
(2, 8))):
print("\n" + START_END)
print(STARTED_MESSAGE)
print(FOLDER_MESSAGE)
complete_remove(DELLEMC_PATH, DELLEMC_UTIL_PATH, OLD_UTIL_FILE)
print(SUCCESS + "{0}".format(SUCCESS_MESSAGE) + NORMAL)
print(START_END + "\n")
# Step 3: If ansible is not installed showing error message.
else:
print("\n" + START_END)
print(FAIL + "{0}".format(NO_ANSIBLE_MESSAGE) + NORMAL)
print(START_END + "\n")
if __name__ == "__main__":
try:
uninstall()
except (IOError, OSError) as err:
print(str(err))
print("\n" + START_END)
print(FAIL + "{0}".format(FAILED_MESSAGE) + NORMAL)
print(START_END + "\n")
sys.exit(1)