-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightScheduler.py
More file actions
167 lines (151 loc) · 5.64 KB
/
lightScheduler.py
File metadata and controls
167 lines (151 loc) · 5.64 KB
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
import json
#from __future__ import print_function
import boto3
import time
from decimal import Decimal
from datetime import datetime,timedelta
def lambda_handler(event, context):
srcFilename = 'test'
bucketstr = 'xxxtestbucket1'
trigName= 'CodePush'
n = 0
numrec = '0'
ticks = time.time()
localtime = time.asctime( time.localtime(ticks) )
trigTime = localtime
#debug skip
for record in event['Records']:
n = n+1
bucketstr = record['s3']['bucket']['name']
srcFilename = record['s3']['object']['key']
trigTime = record['eventTime']
trigName = record['eventName']
numrec = str(n)
#ticks = time.time()
#localtime = time.asctime( time.localtime(ticks) )+srcFilename
#dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
#table = dynamodb.Table('tblMission')
#response = table.put_item(
# Item={
# 'timePush': localtime,
# 'bucket': bucketstr,
# 'srcFilename': srcFilename,
# 'trigTime': trigTime,
# 'trigName': trigName,
# 'numlog': numrec
# }
#)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('tblTrans2')
#Check ahead to avoid redo again
try:
existRec = table.get_item(
Key={
'srcFilename': srcFilename
}
)
#print(existRec)
strIndi = 'Item'
if ('Item' in existRec):
print('exist')
existItem = existRec['Item']['bucketname']
existLog = existRec['Item']['numlog']+'+1'
response = table.update_item(
Key={
'srcFilename': srcFilename
},
UpdateExpression="set numlog = :a",
ExpressionAttributeValues={
':a': existLog
},
ReturnValues="UPDATED_NEW"
)
else:
print('null')
#======= append a trans log =================
response = table.put_item(
Item={
'srcFilename': srcFilename,
'timeBoarding': localtime,
'bucketname': bucketstr,
'trigTime': trigTime,
'trigName': trigName,
'numlog': numrec
}
)
spot_region = 'us-east-1'
wantInstaceType = 'm4.large'
ebs_device_name = '/dev/sda1'
ebs_volume_type = 'gp2'
ebs_volume_size = 10
amiIDstr = 'ami-02xxxxxxxx18bff94'
tmplID = 'lt-013xxxxxxxx700ff0'
tmplVer = '1'
#======= get price =================
ec2client = boto3.client('ec2',region_name = spot_region)
response = ec2client.describe_spot_price_history(
AvailabilityZone='us-east-1a',
StartTime=datetime(2019, 4, 25),
EndTime=datetime(2019, 4, 29),
InstanceTypes=[wantInstaceType],
MaxResults=1,
ProductDescriptions=['Linux/UNIX (Amazon VPC)']
)
i = 1
for query_spot_price in response["SpotPriceHistory"]:
#print('Spot Price ' + str(i))
#print(query_spot_price['AvailabilityZone'])
#print(query_spot_price['InstanceType'])
#print(query_spot_price['SpotPrice'])
#print(query_spot_price['Timestamp'])
#print('\r\n')
curPrice = float(query_spot_price['SpotPrice'])*2;
print('bidPrice:', curPrice)
bidPrice = str(curPrice)
i = i + 1
#======= spin a spot instance =================
ec2res = boto3.resource('ec2',region_name = spot_region)
response = ec2res.create_instances(
#DryRun=True,
#ImageId=amiIDstr,
InstanceType=wantInstaceType,
KeyName='myLabNV',
MinCount=1,
MaxCount=1,
#EbsOptimized=True
InstanceMarketOptions={
'MarketType': 'spot',
'SpotOptions': {
'MaxPrice': bidPrice,
'SpotInstanceType': 'one-time',
'BlockDurationMinutes': 60,
#'ValidUntil': datetime(2019, 5, 30),
'InstanceInterruptionBehavior': 'terminate'
}
},
LaunchTemplate={
'LaunchTemplateId': tmplID,
#'LaunchTemplateName': 'tmplMB3demo',
'Version': tmplVer
},
#UserData="IyEvYmluL2Jhc2gKc3VkbyBweXRob24gL2hvbWUvZWMyLXVzZXIvdGVzdC9qb2JydW4ucHk=",
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': 'testSpot-'+srcFilename
},
{
'Key': 'srcID',
'Value': srcFilename
}
]
}
]
)
print(response[0].id)
except:
print('error')
print('End')