forked from leeroybrun/glacier-vault-remove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeVault.py
110 lines (88 loc) · 2.97 KB
/
removeVault.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import json
import time
import os.path
import logging
import boto.glacier
from socket import gethostbyname, gaierror
# Default logging config
logging.basicConfig(format='%(levelname)s : %(message)s', level=logging.INFO)
# Get arguments
if len(sys.argv) >= 3:
regionName = sys.argv[1]
vaultName = sys.argv[2]
else:
# If there is missing arguments, display usage example and exit
logging.error('Usage: %s REGION_NAME VAULT_NAME', sys.argv[0])
sys.exit(1)
# Get custom logging level
if len(sys.argv) == 4 and sys.argv[3] == 'DEBUG':
logging.info('Logging level set to DEBUG.')
logging.basicConfig(level=logging.DEBUG)
# Load credentials
f = open('credentials.json', 'r')
config = json.loads(f.read())
f.close()
try:
logging.info('Connecting to Amazon Glacier...')
glacier = boto.glacier.connect_to_region(regionName, aws_access_key_id=config['AWSAccessKeyId'], aws_secret_access_key=config['AWSSecretKey'])
except:
logging.error(sys.exc_info()[0])
sys.exit(1)
try:
logging.info('Getting selected vault...')
vault = glacier.get_vault(vaultName)
except:
logging.error(sys.exc_info()[0])
sys.exit(1)
logging.info('Getting jobs list...')
jobList = vault.list_jobs()
jobID = ''
# Check if a job already exists
for job in jobList:
if job.action == 'InventoryRetrieval':
logging.info('Found existing inventory retrieval job...')
jobID = job.id
if jobID == '':
logging.info('No existing job found, initiate inventory retrieval...')
try:
jobID = vault.retrieve_inventory(description='Python Amazon Glacier Removal Tool')
except Exception, e:
logging.error(e)
sys.exit(1)
logging.debug('Job ID : %s', jobID)
# Get job status
job = vault.get_job(jobID)
while job.status_code == 'InProgress':
logging.info('Inventory not ready, sleep for 30 mins...')
time.sleep(60*30)
job = vault.get_job(jobID)
if job.status_code == 'Succeeded':
logging.info('Inventory retrieved, parsing data...')
inventory = json.loads(job.get_output().read())
logging.info('Removing archives... please be patient, this may take some time...');
for archive in inventory['ArchiveList']:
if archive['ArchiveId'] != '':
logging.debug('Remove archive ID : %s', archive['ArchiveId'])
try:
vault.delete_archive(archive['ArchiveId'])
except Exception, e:
logging.error(e)
logging.info('Sleep 2 mins before retrying...')
time.sleep(60*2)
logging.info('Retry to remove archive ID : %s', archive['ArchiveId'])
try:
vault.delete_archive(archive['ArchiveId'])
logging.info('Successfully removed archive ID : %s', archive['ArchiveId'])
except:
logging.error('Cannot remove archive ID : %s', archive['ArchiveId'])
logging.info('Removing vault...')
try:
vault.delete()
logging.info('Vault removed.')
except:
logging.error("We can't remove the vault now. Please wait some time and try again. You can also remove it from the AWS console, now that all archives have been removed.")
else:
logging.info('Vault retrieval failed.')