-
Notifications
You must be signed in to change notification settings - Fork 1
/
doaws.py
216 lines (158 loc) · 5.66 KB
/
doaws.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
# -*- coding: utf-8 -*-
# switch == != to is , is not
# switch '' to ""
import UI
import worker
import sys
sys.path.insert(0, '/opt/boto/lib/python2.7/site-packages/boto-2.27.0-py2.7.egg')
import os
import boto.ec2
import getopt
import time
from UI import log, verbose
ins = None
user = "fedora"
dict_ins={'image-id': 'ami-3b361952', # Fedora as default
'instance-type': 't1.micro',
'key-name': 'stevens',
'security-groups': 'default'}
dict_aws={'key_id': "",
'secret_key': "",
'region': "us-east-1"}
def attach(volume_id=None, src_dir="/"):
if(verbose):
log.info("start attaching...")
"""
Attach volume
"""
global ins
conn = boto.ec2.connect_to_region(dict_aws['region'],
aws_access_key_id=dict_aws['key_id'],
aws_secret_access_key=dict_aws['secret_key']
)
log.info("Connect to region: " + str(conn))
reservations = conn.run_instances(
image_id=dict_ins['image-id'],
key_name=dict_ins['key-name'],
instance_type=dict_ins['instance-type'],
security_groups=[dict_ins['security-groups']]
)
ins = reservations.instances[0]
status = ins.update()
while status != 'running':
log.info("wating for running, current status: " + str(status))
time.sleep(10)
status = ins.update()
log.info("Running instance %s: key %s, group %s." %(str(ins.id), dict_ins['key-name'], dict_ins['security-groups']) )
if volume_id is None:
conv = 1024*1024*1024
sz = 2 * worker.get_size(src_dir)
sz = int((sz+conv-1)/conv)
vol = conn.create_volume(sz, ins.placement)
volume_id = vol.id
log.info("Create volume: " + str(volume_id))
status = vol.update()
while status != 'available':
log.info("wating for available, current status: " + str(status) )
time.sleep(5)
status = vol.update()
log.info("attach volume " + str(volume_id))
dest = "/dev/sdz"
print
if conn.attach_volume(volume_id, ins.id , dest) == False:
log.error("Attach failed.")
sys.exit(1)
log.info( "Attached volume %s to %s" %(volume_id, dest) )
return ins.ip_address, '/dev/xvdz', volume_id
def parse_config():
"""
parse EC2_CONFIG_FILE, retrive aws_access_key_id and aws_secret_acess_key
"""
log.info("parsing config ...")
try:
aws_conf = os.environ['AWS_CONFIG_FILE']
except KeyError:
log.info("Environment variable AWS_CONFIG_FILE not set.")
sys.exit(1)
aws_conf=worker.get_path(aws_conf)
with open(aws_conf) as f:
for line in f:
if line.startswith('aws_access_key_id'):
dict_aws['key_id'] = line[line.find('=')+1:].strip()
elif line.startswith('aws_secret_access_key'):
dict_aws['secret_key'] = line[line.find('=')+1:].strip()
elif line.startswith('region'):
dict_aws['region'] = line[line.find('=')+1:].strip()
else:
pass
def parse_AWS():
"""
parse EC2_BACKUP_FLAGS_AWS, which determines the details of instance
"""
log.info("parsing aws config ...")
try:
ins_conf = os.environ['EC2_BACKUP_FLAGS_AWS']
except KeyError:
log.error("Environment variable EC2_BACKUP_FLAGS_AWS not set.")
sys.exit(1)
opts, args = getopt.getopt(ins_conf.split(), "i:t:k:g:", ["image-id=","instance-type=", "key-name=", "security-groups="])
for o, a in opts:
if o in ("-k", "--key-name"):
dict_ins['key-name'] = a
elif o in ("-g", "--security-groups"):
dict_ins['security-groups'] = a
elif o in ("-i", "--image-id"):
dict_ins['image-id'] = a
elif o in ("-t", "--instance-type"):
dict_ins['instance-type'] = a
else:
assert False, "unhandled option: " + o
def parse_SSH():
"""
parse ssh option,get private key
"""
log.info("parsing ssh config ...")
try:
ssh_conf = os.environ['EC2_BACKUP_FLAGS_SSH']
except KeyError:
log.error("Environment variable EC2_BACKUP_FLAGS_SSH not set.")
sys.exit(1)
lt = ssh_conf.split()
if lt[0] == "-i":
return lt[1].strip()
else:
log.error("Unrecognized ssh option")
sys.exit(1)
def connect_attach(volume_id=None, src_dir="/"):
"""
perform ec2 connect, create volume and attach
"""
parse_AWS()
parse_config()
key = parse_SSH()
pub_ip, dest_dev, vol_id_p = attach(volume_id, src_dir)
return key, pub_ip, dest_dev, vol_id_p
if __name__ == '__main__':
new_volume=0
volume_id, method, src_dir = UI.interact()
if volume_id is None:
new_volume=1
key, pub_ip, dest_dev, vol_id_p = connect_attach(volume_id, src_dir)
if new_volume:
r=worker.mkfs_device(dest_dev, pub_ip, user, key)
if r!=0:
sys.exit(r)
mnt_path=worker.mount_device(dest_dev, pub_ip, user, key)
# print "mount path: ", mnt_path
# print "use method: " + method
if method == "dd":
r=worker.do_tarNdd(src_dir, mnt_path, pub_ip, user, key)
else:
r=worker.do_rsync(src_dir, mnt_path, pub_ip, user, key)
if r!=0:
sys.exit(r)
print vol_id_p
if ins is not None:
ins.terminate()
log.info("instance terminated")