-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2.py
More file actions
112 lines (86 loc) · 2.94 KB
/
ec2.py
File metadata and controls
112 lines (86 loc) · 2.94 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
#!/usr/bin/env python
from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()
import json
import os
import boto3
from botocore.exceptions import ClientError
from flask import Flask
from flask import request
from flask import make_response
# Flask app should start in global layout
def Start_Instance(req):
print ("Getting Request")
result = req.get("result")
parameters = result.get("parameters")
instance_action = parameters.get("instance_action")
instance_id = "i-0a656796258609de1"
print ("Starting EC2 Management")
result = manageEC2instance(instance_action,instance_id,"eu-west-1")
print("EC2 Managed")
print (result)
print(json.dumps(result, indent=4))
print("Formatting Results")
res = makeWebhookResult(instance_action, result)
print("Results Formatted")
return res
def manageEC2instance(instance_action, instance_id, _region_name):
ec2 = boto3.client('ec2', _region_name)
print("boto3 client created for action=" + instance_action)
if instance_action == 'ON':
# Do a dryrun first to verify permissions
try:
ec2.start_instances(InstanceIds=[instance_id], DryRun=True)
except Exception as e:
if 'DryRunOperation' not in str(e):
raise
# Dry run succeeded, run start_instances without dryrun
try:
print("executing action")
response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
return(response)
except ClientError as e:
print(e)
else:
# Do a dryrun first to verify permissions
try:
print("executing action")
ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
except ClientError as e:
if 'DryRunOperation' not in str(e):
raise
# Dry run succeeded, call stop_instances witout dryrun
try:
response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
return (response)
except ClientError as e:
print(e)
return
def makeWebhookResult(instance_action, result):
root = 'StoppingInstances'
if instance_action == 'ON':
root = 'StartingInstances'
jroot = result.get(root)
if jroot is None:
return {}
InstanceId = jroot[0].get('InstanceId')
if InstanceId is None:
return {}
PreviousState = jroot[0].get('PreviousState')
if PreviousState is None:
return {}
CurrentState = jroot[0].get('CurrentState')
if CurrentState is None:
return {}
print("Creating speech")
speech = "The server was " + PreviousState.get('Name') + " and now is " + CurrentState.get('Name') + "."
print("Response:")
print(speech)
return {
"speech": speech,
"displayText": speech,
# "data": data,
# "contextOut": [],
"source": "apiai-ec2-webhook"
}