-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServeRmore.py
287 lines (269 loc) · 14 KB
/
ServeRmore.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
import sys, time, srvl_config, VERSION
from signal import signal, SIGINT
from sys import exit
class srm:
def __init__(self):
self.cc = srvl_config.srvlConfig()
def lambda_list(self):
import boto3
client = boto3.client('lambda')
response = client.list_functions(
MaxItems=50
)
for res in response["Functions"]:
print(res["FunctionName"])
def lambda_create(self):
import boto3
client = boto3.client('lambda')
env = self.cc.settings["env"]
response = client.create_function(
FunctionName=self.cc.settings[env]["function"]["name"],
Runtime=self.cc.settings[env]["function"]["runtime"],
Role=self.cc.settings[env]["function"]["arn_role"],
Handler=self.cc.settings[env]["function"]["handler"],
Code={
'S3Bucket': self.cc.settings[env]["aws"]["s3_bucket"],
'S3Key': self.cc.settings[env]["aws"]["s3_key"]+"/"+self.cc.settings[env]["function"]["zip_file_name"]
},
Timeout=60,
MemorySize=3008,
Layers=[
self.cc.settings[env]["runtime_layer"]["arn"]
]
)
def lambda_invoke(self):
import boto3
client = boto3.client('lambda')
env = self.cc.settings["env"]
response = client.invoke(
FunctionName=self.cc.settings[env]["function"]["name"]
)
def lambda_update(self):
import boto3
client = boto3.client('lambda')
env = self.cc.settings["env"]
response = client.update_function_code(
FunctionName=self.cc.settings[env]["function"]["name"],
S3Bucket=self.cc.settings[env]["aws"]["s3_bucket"],
S3Key=self.cc.settings[env]["aws"]["s3_key"]+"/"+self.cc.settings[env]["function"]["zip_file_name"]
)
waiter = client.get_waiter('function_updated')
waiter.wait(
FunctionName=self.cc.settings[env]["function"]["name"],
WaiterConfig={
'Delay': 5,
'MaxAttempts': 60
}
)
if self.cc.settings[env]["additional_layer"]["name"]:
response = client.update_function_configuration(
FunctionName=self.cc.settings[env]["function"]["name"],
Role=self.cc.settings[env]["function"]["arn_role"],
Handler=self.cc.settings[env]["function"]["handler"],
Runtime=self.cc.settings[env]["function"]["runtime"],
Timeout=60,
MemorySize=3008,
Layers=[
self.cc.settings[env]["runtime_layer"]["arn"],
self.cc.settings[env]["additional_layer"]["arn"]
]
)
else:
response = client.update_function_configuration(
FunctionName=self.cc.settings[env]["function"]["name"],
Role=self.cc.settings[env]["function"]["arn_role"],
Handler=self.cc.settings[env]["function"]["handler"],
Runtime=self.cc.settings[env]["function"]["runtime"],
Timeout=60,
MemorySize=3008,
Layers=[
self.cc.settings[env]["runtime_layer"]["arn"]
]
)
def lambda_destroy(self):
import boto3
client = boto3.client('lambda')
env = self.cc.settings["env"]
response = client.delete_function(
FunctionName=self.cc.settings[env]["function"]["name"]
)
def create(self):
if not self.cc.settings["build_vm"]["instance_id"]:
import boto3
ec2 = boto3.resource('ec2')
self.name_str = 'srm_R_layer_'+str(VERSION.srm_VERSION)
instance = ec2.create_instances(
BlockDeviceMappings=[{
'DeviceName': '/dev/xvda',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': 50,
'VolumeType': 'gp2',
},
},
{
'DeviceName': '/dev/xvdcz',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': 50,
'VolumeType': 'gp2',
},
}],
SecurityGroupIds=[
str(self.cc.settings["build_vm"]["ssh_security_group"]),
str(self.cc.settings["build_vm"]["default_security_group"])
],
SubnetId=str(self.cc.settings["build_vm"]["subnet"]),
ImageId=str(self.cc.settings["build_vm"]["ami"]),
KeyName=str(self.cc.settings["build_vm"]["private_key"].replace('.pem', '')),
MinCount=1,
MaxCount=1,
InstanceType=str(self.cc.settings["build_vm"]["instance_type"]),
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key':'Name',
'Value': self.name_str
}
]
}
])
print("Please wait for instance bootup.")
time.sleep(5)
self.cc.set("build_vm", "instance_id", instance[0].instance_id)
print("New instance ID: " + self.cc.settings["build_vm"]["instance_id"])
state = 'pending'
while not state == 'running':
time.sleep(5)
instance = ec2.Instance(self.cc.settings["build_vm"]["instance_id"])
state = instance.state["Name"]
print("STATUS: "+state)
time.sleep(1)
self.cc.set("build_vm", "domain_name", instance.public_dns_name)
print("Proceeding with bootstrapping instance.")
self.vm_setup()
else:
print("Instance already exists.")
def deploy(self):
if self.cc.settings["build_vm"]["instance_id"]:
env = self.cc.settings["env"]
from pathlib import Path
if self.cc.settings[env]["runtime_layer"]["name"]:
cloud_connect = srvl_config.srvlConnect()
cloud_connect.initiate_ssh("ec2-user", self.cc.settings["build_vm"]["private_key"], self.cc.settings["build_vm"]["domain_name"])
print("Running build script for runtime Lambda Layer...")
cloud_connect.run_ssh("./runtime/build.sh " + self.cc.settings[env]["runtime_layer"]["r_version"] +" \""+ str(self.cc.settings[env]["runtime_layer"]["r_packages"]) + "\" 2>&1")
print("Running deploy script for runtime Lambda Layer...")
cloud_connect.run_ssh("./runtime/deploy.sh " + self.cc.settings[env]["runtime_layer"]["name"] + " " + self.cc.settings[env]["aws"]["s3_bucket"] + " " + self.cc.settings[env]["aws"]["s3_key"] + " 2>&1")
cloud_connect.terminate_ssh()
import boto3
client = boto3.client('lambda')
response = client.list_layer_versions(
LayerName=self.cc.settings[env]["runtime_layer"]["name"]
)
print("Updating Published Version for runtime Lambda Layer...")
self.cc.set("runtime_layer", "arn", response["LayerVersions"][0]["LayerVersionArn"])
if self.cc.settings[env]["additional_layer"]["name"]:
cloud_connect = srvl_config.srvlConnect()
cloud_connect.initiate_ssh("ec2-user", self.cc.settings["build_vm"]["private_key"], self.cc.settings["build_vm"]["domain_name"])
print("Running build script for additional Lambda Layer...")
cloud_connect.run_ssh("./additional/build.sh " + self.cc.settings[env]["runtime_layer"]["r_version"] +" \""+ str(self.cc.settings[env]["additional_layer"]["r_packages"]) + "\" 2>&1")
print("Running deploy script for additional Lambda Layer...")
cloud_connect.run_ssh("./additional/deploy.sh " + self.cc.settings[env]["additional_layer"]["name"] + " " + self.cc.settings[env]["aws"]["s3_bucket"] + " " + self.cc.settings[env]["aws"]["s3_key"] + " 2>&1")
cloud_connect.terminate_ssh()
import boto3
client = boto3.client('lambda')
response = client.list_layer_versions(
LayerName=self.cc.settings[env]["additional_layer"]["name"]
)
print("Updating Published Version for additional Lambda Layer...")
self.cc.set("additional_layer", "arn", response["LayerVersions"][0]["LayerVersionArn"])
else:
print("No additional layer named.")
else:
print("No runtime layer named.")
else:
print("No instance is allocated.")
def status(self):
if self.cc.settings["build_vm"]["instance_id"]:
import boto3
resource = boto3.resource('ec2')
instance = resource.Instance(self.cc.settings["build_vm"]["instance_id"])
state = instance.state["Name"]
print("Instance is "+ state + ".")
else:
print("No instance is allocated.")
def cpu(self):
if self.cc.settings["build_vm"]["instance_id"]:
cloud_connect = srvl_config.srvlConnect()
cloud_connect.initiate_ssh("ec2-user", self.cc.settings["build_vm"]["private_key"], self.cc.settings["build_vm"]["domain_name"])
cloud_connect.run_ssh(
"printf '\nCPU core usage:' && " \
"mpstat -P ALL 1 1 | awk '/Average:/ && $2 ~ /[0-9]/ {print $3\"%\"}' &&" \
"printf 'Memory used:' &&" \
"free -m | awk 'NR==2{print int($3*100/$2)\"%\" }' && " \
"printf 'Storage used:' &&" \
"df -h | awk '$NF==\"/\"{print $5}'")
cloud_connect.terminate_ssh()
else:
print("No instance is allocated.")
def terminate(self):
if input("are you sure? (y/n) ") == "y":
if self.cc.settings["build_vm"]["instance_id"]:
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.Instance(self.cc.settings["build_vm"]["instance_id"])
response = instance.terminate()
self.cc.set("build_vm", "instance_id", '')
self.cc.set("build_vm", "domain_name", '')
print("Instance has been terminated.")
else:
print("No instance is running.")
return False
def ssh(self):
if self.cc.settings["build_vm"]["instance_id"]:
cloud_connect = srvl_config.srvlConnect()
cloud_connect.evoke_ssh('ec2-user',self.cc.settings["build_vm"]["domain_name"])
else:
print("No instance is allocated.")
def vm_setup(self):
from pathlib import Path
print("Setting up VM for Lambda Layer Build Container...")
cloud_connect = srvl_config.srvlConnect()
cloud_connect.initiate_ssh("ec2-user", self.cc.settings["build_vm"]["private_key"], self.cc.settings["build_vm"]["domain_name"])
print("Updating the VM...")
cloud_connect.run_ssh("sudo yum -y update")
cloud_connect.run_ssh("sudo yum -y install nfs-utils sysstat python3-setuptools")
cloud_connect.run_ssh("pip3 install awscli --upgrade --user")
cloud_connect.run_ssh("sudo yum -y -q install git nano zip")
cloud_connect.upload_file_ssh(str(Path.home())+'/', '/home/ec2-user/', '.gitconfig')
cloud_connect.run_ssh("mkdir -p /home/ec2-user/.aws")
cloud_connect.upload_file_ssh(str(Path.home())+'/.aws/', '/home/ec2-user/.aws/', 'credentials')
cloud_connect.upload_file_ssh(str(Path.home())+'/.aws/', '/home/ec2-user/.aws/', 'config')
env = self.cc.settings["env"]
if self.cc.settings[env]["runtime_layer"]["name"]:
cloud_connect.run_ssh("mkdir -p /home/ec2-user/runtime")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/', '/home/ec2-user/runtime/', "Dockerfile")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/', '/home/ec2-user/runtime/', "build.sh")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/', '/home/ec2-user/runtime/', "deploy.sh")
cloud_connect.run_ssh("mkdir -p /home/ec2-user/runtime/src")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/src/', '/home/ec2-user/runtime/src/', "bootstrap")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/src/', '/home/ec2-user/runtime/src/', "bootstrap.R")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/runtime/src/', '/home/ec2-user/runtime/src/', "runtime.R")
cloud_connect.run_ssh("chmod -R ugo+x runtime")
if self.cc.settings[env]["additional_layer"]["name"]:
cloud_connect.run_ssh("mkdir -p /home/ec2-user/additional")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/additional/', '/home/ec2-user/additional/', "build.sh")
cloud_connect.upload_file_ssh(str(Path.home())+'/ServeRmore/layers/additional/', '/home/ec2-user/additional/', "deploy.sh")
cloud_connect.run_ssh("chmod -R ugo+x additional")
cloud_connect.terminate_ssh()
def restart_docker_service(self):
print("Restart Docker Service...")
cloud_connect = srvl_config.srvlConnect()
cloud_connect.initiate_ssh("ec2-user", self.cc.settings["build_vm"]["private_key"], self.cc.settings["build_vm"]["domain_name"])
cloud_connect.run_ssh("sudo service docker start")
cloud_connect.terminate_ssh()
print("Docker Service restarted...")