-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathAuto_delete_instances.py
166 lines (125 loc) · 5.65 KB
/
Auto_delete_instances.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
# Oracle OCI - Auto Delete Instances
# Version: 1.8 22-November 2018
# Written by: [email protected]
# More info see: www.oc-blog.com
#
# This script will auto delete instances that do not have pre-assigned mandatory tags
#
import oci
import json
import shapes
import logging
# Script configuation ###################################################################################
configfile = "c:\\oci\\config" # Define config file to be used.
AllPredefinedTags = True # use only predefined tags from root compartment or include all compartment tags as well
# #######################################################################################################
#logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
class OCIobject(object):
"""__init__() functions as the class constructor"""
def __init__(self, Service=None, OCID=None, Region=None, Name=None):
self.Service = Service
self.OCID = OCID
self.Region = Region
self.Name = Name
deleteList = []
def EvaluateInstances(instances, compartmentName, instancetype, regionname):
print ("Checking " + instancetype + " instances in " + regionname + "-" + compartmentName)
for instance in instances:
tagtxt = ""
try:
namespaces = instance.defined_tags
Tag1 = namespaces["Mandatory__Tags"]["CSM_EMAIL"]
Tag2 = namespaces["Mandatory__Tags"]["CSM_COUNTRY"]
# print (" --> OK Instance: " + instance.display_name)
except:
item = OCIobject(instancetype, instance.id, regionname, instance.display_name)
deleteList.append(item)
customertags = []
config = oci.config.from_file(configfile)
identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
RootCompartmentID = user.compartment_id
print ("Logged in as: {} @ {}".format(user.description, config["region"]))
print ("Querying Enabled Regions:")
response = identity.list_region_subscriptions(config["tenancy"])
regions = response.data
for region in regions:
if region.is_home_region:
home = "Home region"
else:
home = ""
print ("- {} ({}) {}".format(region.region_name, region.status, home))
print ("Checking instances for missing Tags...")
#Retrieve all instances for all enabled regions.
for region in regions:
config = oci.config.from_file(configfile)
config["region"] = region.region_name
identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
RootCompartmentID = user.compartment_id
ComputeClient = oci.core.ComputeClient(config)
NetworkClient = oci.core.VirtualNetworkClient(config)
# Check instances for all the underlaying Compartments
response = oci.pagination.list_call_get_all_results(identity.list_compartments,RootCompartmentID,compartment_id_in_subtree=True)
compartments = response.data
# Insert (on top) the root compartment
RootCompartment = oci.identity.models.Compartment()
RootCompartment.id = RootCompartmentID
RootCompartment.name = "root"
RootCompartment.lifecycle_state = "ACTIVE"
compartments.insert(0, RootCompartment)
for compartment in compartments:
compartmentName = compartment.name
print ("Checking : " + compartment.name + " in " + region.region_name)
if compartment.lifecycle_state == "ACTIVE":
compartmentID = compartment.id
try:
response = oci.pagination.list_call_get_all_results(ComputeClient.list_instances,compartment_id=compartmentID)
if len(response.data) > 0:
EvaluateInstances(response.data, compartmentName, "Compute", region.region_name)
except:
donothing = 1
databaseClient = oci.database.DatabaseClient(config)
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_db_systems,compartment_id=compartmentID)
if len(response.data) > 0:
EvaluateInstances(response.data, compartmentName, "DB", region.region_name)
except:
donothing = 1
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_autonomous_data_warehouses,compartment_id=compartmentID)
if len(response.data) > 0:
EvaluateInstances(response.data, compartmentName, "ADW", region.region_name)
except:
donothing = 1
try:
response = oci.pagination.list_call_get_all_results(databaseClient.list_autonomous_databases,compartment_id=compartmentID)
if len(response.data) > 0:
EvaluateInstances(response.data, compartmentName, "ATP", region.region_name)
except:
donothing = 1
print (" ")
print ("Instances that are missing mandatory tags:")
for i in deleteList:
print (i.Name)
r = input ("Do you really want to delete these instances (y/n)?")
if r == "y" or r == "Y":
print ("Starting delete process")
for i in deleteList:
print ("Deleting " + i.Name)
config["region"] = i.Region
if i.Service == "Compute":
ComputeClient = oci.core.ComputeClient(config)
response = ComputeClient.terminate_instance(instance_id=i.OCID)
if i.Service == "DB":
databaseClient = oci.database.DatabaseClient(config)
response = databaseClient.terminate_db_system(db_system_id=i.OCID)
if i.Service == "ADW":
databaseClient = oci.database.DatabaseClient(config)
response = databaseClient.delete_autonomous_data_warehouse(autonomous_data_warehouse_id=i.OCID)
if i.Service == "ATP":
databaseClient = oci.database.DatabaseClient(config)
response = databaseClient.delete_autonomous_database(autonomous_database_id=i.OCID)
Print ("Cleaning process completed")
else:
print ("Cancelled")