forked from oracle/oci-ansible-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.py
executable file
·108 lines (81 loc) · 3.07 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
#!/usr/bin/env python
# Copyright (c) 2018, 2019 Oracle and/or its affiliates.
# This software is made available to you under the terms of the GPL 3.0 license or the Apache 2.0 license.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Apache License v2.0
# See LICENSE.TXT for details.
"""
Oracle Cloud Infrastructure(OCI) Ansible Modules Uninstaller Script
===================================================================
This script deletes OCI Ansible modules, Oracle docs fragments and Oracle Ansible utility file from the ansible path.
To uninstall OCI Ansible modules, execute:
$ ./uninstall.py
To execute the script with debug messages, execute:
$ ./uninstall.py --debug
author: "Rohit Chaware (@rohitChaware)"
"""
from __future__ import print_function
import argparse
import os.path
import shutil
import sys
try:
import ansible
ANSIBLE_IS_INSTALLED = True
except ImportError:
ANSIBLE_IS_INSTALLED = False
debug = False
def parse_cli_args():
parser = argparse.ArgumentParser(description="Script to uninstall oci-ansible-role")
parser.add_argument(
"--debug",
action="store_true",
default=False,
help="Send debug messages to STDERR",
)
return parser.parse_args()
def log(*args, **kwargs):
if debug:
print(*args, file=sys.stderr, **kwargs)
def main():
if not ANSIBLE_IS_INSTALLED:
print("Could not load ansible module.")
sys.exit(1)
global debug
args = parse_cli_args()
if args.debug:
debug = True
ansible_path = os.path.dirname(os.path.abspath(os.path.realpath(ansible.__file__)))
log("Ansible path: {}".format(ansible_path))
module_utils_path = os.path.join(ansible_path, "module_utils", "oracle")
log("Module utilities path: {}".format(module_utils_path))
document_fragments_path_old = os.path.join(
ansible_path, "utils", "module_docs_fragments"
)
document_fragments_path_new = os.path.join(ansible_path, "plugins", "doc_fragments")
if os.path.exists(document_fragments_path_new):
document_fragments_path = document_fragments_path_new
else:
document_fragments_path = document_fragments_path_old
log("Documentation fragments path: {}".format(document_fragments_path))
delete(module_utils_path)
oci_docs_fragments = []
for filename in os.listdir(document_fragments_path):
if filename.startswith("oracle"):
oci_docs_fragments.append(os.path.join(document_fragments_path, filename))
delete(oci_docs_fragments)
oracle_module_dir_path = os.path.join(ansible_path, "modules", "cloud", "oracle")
delete(oracle_module_dir_path)
print("Uninstalled OCI Ansible modules successfully.")
def delete(paths):
if type(paths) is not list:
paths = [paths]
for path in paths:
if os.path.isdir(path):
print("Deleting directory {}".format(path))
shutil.rmtree(path)
elif os.path.isfile(path):
print("Deleting {}".format(path))
os.remove(path)
if __name__ == "__main__":
main()