-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
175 lines (134 loc) · 4.39 KB
/
main.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
import boto3
import botocore
import click
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
def filter_instances(project):
instances = []
if project:
filters = [{'Name':'tag:Project', 'Values':[project]}]
instances = ec2.instances.filter(Filters=filters)
else:
instances = ec2.instances.all()
return instances
def has_pending_snapshot(volume):
snapshots = list(volume.snapshots.all())
return snapshots and snapshots[0].state == 'pending'
@click.group()
def cli():
"""Shotty manages snapshots"""
@cli.group('snapshots')
def snapshots():
"""Commands for snapshots"""
@snapshots.command('list')
@click.option('--project', default=None,
help="Only snapshots for project (tag Project:<name>)")
@click.option('--all', 'list_all', default=False, is_flag=True,
help="List all snapshots for each volume, not just the most recent")
def list_snapshots(project, list_all):
"List EC2 snapshots"
instances = filter_instances(project)
for i in instances:
for v in i.volumes.all():
for s in v.snapshots.all():
print(", ".join((
s.id,
v.id,
i.id,
s.state,
s.progress,
s.start_time.strftime("%c")
)))
if s.state == 'completed' and not list_all: break
return
@cli.group('volumes')
def volumes():
"""Commands for volumes"""
@volumes.command('list')
@click.option('--project', default=None,
help="Only volumes for project (tag Project:<name>)")
def list_volumes(project):
"List EC2 volumes"
instances = filter_instances(project)
for i in instances:
for v in i.volumes.all():
print(", ".join((
v.id,
i.id,
v.state,
str(v.size) + "GiB",
v.encrypted and "Encrypted" or "Not Encrypted"
)))
return
@cli.group('instances')
def instances():
"""Commands for instances"""
@instances.command('snapshot',
help="Create snapshots of all volumes")
@click.option('--project', default=None,
help="Only instances for project (tag Project:<name>)")
def create_snapshots(project):
"Create snapshots for EC2 instances"
instances = filter_instances(project)
for i in instances:
print("Stopping {0}...".format(i.id))
i.stop()
i.wait_until_stopped()
for v in i.volumes.all():
if has_pending_snapshot(v):
print(" Skipping {0}, snapshot already in progress".format(v.id))
continue
print(" Creating snapshot of {0}".format(v.id))
v.create_snapshot(Description="Created by SnapshotAlyzer 30000")
print("Starting {0}...".format(i.id))
i.start()
i.wait_until_running()
print("Job's done!")
return
@instances.command('list')
@click.option('--project', default=None,
help="Only instances for project (tag Project:<name>)")
def list_instances(project):
"List EC2 instances"
instances = filter_instances(project)
for i in instances:
tags = { t['Key']: t['Value'] for t in i.tags or [] }
print(', '.join((
i.id,
i.instance_type,
i.placement['AvailabilityZone'],
i.state['Name'],
i.public_dns_name,
tags.get('Project', '<no project>')
)))
return
@instances.command('stop')
@click.option('--project', default=None,
help='Only instances for project')
def stop_instances(project):
"Stop EC2 instances"
instances = filter_instances(project)
for i in instances:
print("Stopping {0}...".format(i.id))
try:
i.stop()
except botocore.exceptions.ClientError as e:
print(" Could not stop {0}. ".format(i.id) + str(e))
continue
return
@instances.command('start')
@click.option('--project', default=None,
help='Only instances for project')
def start_instances(project):
"Start EC2 instances"
instances = filter_instances(project)
for i in instances:
print("Starting {0}...".format(i.id))
try:
i.start()
except botocore.exceptions.ClientError as e:
print(" Could not start {0}. ".format(i.id) + str(e))
continue
return
if __name__ == '__main__':
cli()