-
Notifications
You must be signed in to change notification settings - Fork 0
/
gari.py
executable file
·194 lines (158 loc) · 6.76 KB
/
gari.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
import ConfigParser
import argparse
import datetime
import help.vcenter as vcenter
import help.aws as aws
import logging
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Remove snapshots da AWS e do VCenter\nhttps://github.com/wvoliveira/gari.git""")
parser.add_argument('-d', '--datacenter', metavar='\b', help='vcenter or aws', required=True)
parser.add_argument('-c', '--config', metavar='\b', help='config file', required=True)
parser.add_argument('-w', '--whitelist', metavar='\b', help='whitelist file', required=True)
parser.add_argument('-f', '--force', help='force remove', action='store_true')
args = parser.parse_args()
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
def config_parser(section):
config = ConfigParser.ConfigParser()
config.read(args.config)
if config.has_section(section):
items_dict = dict(config.items(section))
return items_dict
else:
logging.error("Variavel '{0}' inexistente no arquivo de configuracao".format(section))
sys.exit(1)
def difference_date(date_):
date = datetime.datetime.strptime(str(date_), '%Y-%m-%d %H:%M:%S')
difference = abs(datetime.datetime.now() - date)
return difference.days
def vcenter_remove_snapshots():
try:
logging.info('Lendo VCenter section do arquivo de configuracao')
vcenter_args = config_parser('vcenter')
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info('Lendo arquivo whitelist')
with open(args.whitelist) as file:
whitelist = file.read().splitlines()
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info('Coletando variaveis')
host = vcenter_args['host']
user = vcenter_args['user']
pwd = vcenter_args['pwd']
snapshot_days = vcenter_args['snapshot_days']
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info("Tentando se conectar no VCenter '{0}'".format(vcenter_args['host']))
vcenter_conn = vcenter.VCenter(host, user, pwd)
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info('Coletando informacoes de todas as VMs')
vms = vcenter_conn.get_all_vms()
except Exception as error:
logging.error(error)
sys.exit(2)
logging.info('Numero de VMs: {0}'.format(len(vms)))
logging.info('Verificando se existe snapshot nas VMs. Pode demorar um pouco (muito)')
for index, vm in enumerate(vms, start=1):
snapshot_list = vcenter_conn.snapshot_list(vm['name'])
if len(snapshot_list) > 0:
for snapshot in snapshot_list:
create_time = snapshot['CreateTime'][:19]
snapshot_name = snapshot['Name']
vm_name = vm['name']
difference_days = difference_date(datetime.datetime.strptime(str(create_time), '%Y-%m-%d %H:%M:%S'))
if difference_days >= int(vcenter_args['snapshot_days']):
logging.info('Nome: {0}'.format(vm_name))
logging.info('Data da criacao: {0}'.format(create_time))
logging.info('Snapshot name: {0}'.format(snapshot_name))
if vm_name in whitelist:
logging.info('O snapshot existe ha mais de {0} dias, mas esta na whitelist'.format(snapshot_days))
continue
else:
if args.force:
logging.info("Removendo snapshot '{0}' da VM '{1}'".format(snapshot_name, vm_name))
try:
vcenter_conn.remove_snapshot(vm_name, snapshot_name)
except Exception as error:
logging.error(error)
else:
logging.info("Removeria o snapshot {0} da VM '{1}'".format(snapshot_name, vm_name))
def aws_remove_snapshots():
try:
logging.info('Lendo AWS section do arquivo de configuracao')
aws_args = config_parser('aws')
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info('Lendo arquivo whitelist')
with open(args.whitelist) as file:
whitelist = file.read().splitlines()
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info('Coletando variaveis')
region = aws_args['region']
key_id = aws_args['key_id']
access_key = aws_args['access_key']
owner_id = aws_args['owner_id']
snapshot_days = aws_args['snapshot_days']
except Exception as error:
logging.error(error)
sys.exit(2)
try:
logging.info("Tentando se conectar na regiao '{0}'... ".format(aws_args['region']))
aws_conn = aws.AWS(region, key_id, access_key, owner_id)
except Exception as error:
logging.error(error)
sys.exit(2)
logging.info('Coletando snapshots')
snapshots = aws_conn.snapshots()
logging.info('Numero de snapshots: {0}'.format(len(snapshots)))
for info in snapshots:
name = None
if info.has_key('Tags'):
for key in info['Tags']:
if key['Key'] == 'Name':
name = key['Value'].split(' - ')[0]
create_time = str(info['StartTime'])[:19]
snapshot_id = info['SnapshotId']
difference_days = difference_date(datetime.datetime.strptime(str(create_time), '%Y-%m-%d %H:%M:%S'))
if difference_days > int(snapshot_days):
logging.info('Nome: {0}'.format(name))
logging.info('Data da criacao: {0}'.format(create_time))
logging.info('Snapshot ID: {0}'.format(snapshot_id))
if snapshot_id in whitelist:
logging.info('O snapshot existe ha mais de {0} dias, mas esta na whitelist'.format(snapshot_days))
continue
else:
if args.force:
logging.info('Removendo snapshot {0}... '.format(snapshot_id))
try:
aws_conn.delete_snapshot(snapshot_id)
except Exception as error:
logging.error(error)
else:
logging.info('Removeria o snapshot {0}... '.format(snapshot_id))
def main():
if args.datacenter.lower() == 'vcenter':
vcenter_remove_snapshots()
elif args.datacenter.lower() == 'aws':
aws_remove_snapshots()
else:
parser.print_help()
if __name__ == '__main__':
main()