/
helper.py
62 lines (45 loc) · 1.49 KB
/
helper.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
#!/usr/bin/python
import sys
import imports
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from os import remove
ALL=100
PENDING=0
RUNNING=16
SHUTTING_DONW=32
TERMINATED=48
STOPPING = 64
STOPPED=80
#function to get the list instance to work upon
def getInstList(conn,filter):
""" This function will be returning the instances
The return is dictionary
"""
output ={PENDING:[],RUNNING:[],SHUTTING_DONW:[],TERMINATED:[],STOPPING:[],STOPPED:[]}
#get the list off all the instance
Instlist = conn.get_only_instances(filters=filter)
if len(Instlist) > 0:
for inst in Instlist :
instSt=inst._state
if instSt.code in output.keys():
output[instSt.code].append(inst)
return output
def uploadCallbackForS3(dataUploaded,totalSize):
print "\n%d of %d uploaded\n" % (dataUploaded,totalSize)
def uploadFileToS3(bucket,fileName,conf):
s3conn=S3Connection(conf.access_key_id, conf.access_key_sec)
try:
buck = s3conn.get_bucket(bucket)
except :
print "Failed to get the Bucket %s " % bucket
return False
try :
keyFile=Key(buck,fileName)
keyFile.set_contents_from_filename(fileName,replace=True,cb=uploadCallbackForS3,num_cb=10)
except:
print "Failed to Upload the file to S3"
print "%s" % str(sys.exc_info())
return False
remove(fileName)
return True