-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitbackup.py
99 lines (68 loc) · 2.23 KB
/
gitbackup.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import our dependencies
import boto
from boto import ec2
from boto.ec2 import connection, connect_to_region
import sys
import os
import uuid
class AMICreation(object):
def __init__(self):
#os.environ['AWS_ACCESS_KEY_ID']
#os.environ['AWS_SECRET_ACCESS_KEY']
#os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
self.connection = connect_to_region('us-west-2')
self.instances = [i for r in
self.connection.get_all_instances() for i in
r.instances]
self.action = 'failure'
self.has_error = 'no'
self.instance = None
def create_image_id(
self,
instance=None,
description=None,
no_reboot=None,
ami_name=None,
):
image_id = instance.create_image(ami_name,
description=description, no_reboot=no_reboot)
if 'ami' in image_id:
print image_id
return 'success'
else:
return 'failure'
def find_instance_id_and_create(
self,
servername,
descritption,
no_reboot,
):
for i in self.instances:
if 'Name' in i.tags:
state = i.state
name = i.tags['Name']
instance_id = str(i.id)
print name, state, instance_id
if name.lower() == servername.lower():
ami_name = servername.lower() + '-' \
+ str(uuid.uuid4().fields[-1])[:5]
status = self.create_image_id(i, str(descritption),
str(no_reboot), str(ami_name))
if status == 'success':
self.has_error = 'no'
return self
else:
self.has_error = 'yes'
return self
else:
self.has_error = 'no instances named %s' \
% servername.lower()
AMICreator = AMICreation()
AMICreator.find_instance_id_and_create('gitserver',
'this is a git server backup base image', 'False')
if str(AMICreator.has_error) == 'no':
print 'success'
else:
print AMICreator.has_error