-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_lambda.py
executable file
·379 lines (339 loc) · 11.9 KB
/
install_lambda.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
#
# Copyright (c) 2018, Centrica Hive Ltd.
#
# This file is part of chaim.
#
# chaim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# chaim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with chaim. If not, see <http://www.gnu.org/licenses/>.
#
"""
module to install/update lambda code
"""
import argparse
import boto3
import os
import subprocess
import sys
import tempfile
import yaml
# version module is at the top level of this repo (where
# this script lives)
import version
# from chalicelib.filesystem import DirNotFound
# from chalicelib.filesystem import FileNotFound
from chalicelib.filesystem import FileSystem
def getVer():
return [version.majorv, version.minorv, version.buildv]
def getVerstr():
xmajorv, xminorv, xbuildv = getVer()
return str(xmajorv) + '.' + str(xminorv) + '.' + str(xbuildv)
def updateBuild():
xmajorv, xminorv, xbuildv = getVer()
xbuildv += 1
with open(os.path.dirname(__file__) + "/version.py", "w") as vfn:
vfn.write("majorv = {}\n".format(xmajorv))
vfn.write("minorv = {}\n".format(xminorv))
vfn.write("buildv = {}\n".format(xbuildv))
vfn.write("verstr = str(majorv) + '.' + str(minorv) + '.' + str(buildv)")
vstr = str(xmajorv) + '.' + str(xminorv) + '.' + str(xbuildv)
cmd = "git add " + os.path.dirname(__file__) + "/version.py"
runcmd(cmd)
with open("version", "w") as vfn:
vfn.write(vstr)
cmd = "git add version"
runcmd(cmd)
cmsg = "updating chaim to {}".format(vstr)
print(cmsg)
cmd = 'git commit -m "' + cmsg + '"'
runcmd(cmd)
cmd = 'git push'
runcmd(cmd)
return vstr
def gitTag(tag):
bits = tag.split(".")
cn = len(bits)
if cn == 1:
majorv = bits[0]
minorv = "0"
buildv = "0"
elif cn == 2:
majorv = bits[0]
minorv = bits[1]
buildv = "0"
else:
majorv = bits[0]
minorv = bits[1]
buildv = bits[2]
with open(os.path.dirname(__file__) + "/version.py", "w") as vfn:
vfn.write("majorv = {}\n".format(majorv))
vfn.write("minorv = {}\n".format(minorv))
vfn.write("buildv = {}\n".format(buildv))
vfn.write("verstr = str(majorv) + '.' + str(minorv) + '.' + str(buildv)")
vstr = str(majorv) + '.' + str(minorv) + '.' + str(buildv)
cmd = "git add " + os.path.dirname(__file__) + "/version.py"
runcmd(cmd)
with open("version", "w") as vfn:
vfn.write(vstr)
cmd = "git add version"
runcmd(cmd)
cmsg = "tagging chaim at {}".format(vstr)
print(cmsg)
cmd = 'git commit -m "' + cmsg + '"'
runcmd(cmd)
cmd = 'git push'
runcmd(cmd)
print("tagging as " + vstr)
cmd = 'git tag ' + vstr
runcmd(cmd)
print("pushing tags")
cmd = "git push --tags"
runcmd(cmd)
return vstr
def runcmd(cmd):
return subprocess.check_call(cmd, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
def getFunctions():
client = boto3.client("lambda")
funcs = []
paginate = True
next = ""
while paginate:
if len(next) > 0:
resp = client.list_functions(Marker=next)
else:
resp = client.list_functions()
for func in resp["Functions"]:
funcs.append(func)
if "NextMarker" in resp:
next = resp["NextMarker"]
else:
next = ""
paginate = False
return funcs
def findFunction(allfuncs, fnname):
found = False
for func in allfuncs:
if func["FunctionName"] == fnname:
found = True
break
return found
def installRequirements(reqfn, tmpdir):
fs = FileSystem()
if fs.fileExists(reqfn):
cmd = "pip install -r " + reqfn + " -t " + tmpdir
return runcmd(cmd)
else:
print("{} does not exist.".format(reqfn))
return False
def prepareLambda(fname, vstr, wd, files, reqfn):
"""prepares the zip file for upload to lambda
wd is the working directory.
files is a list of files relative to the wd.
reqfn is the requirements file.
will make a 'package' directory under the wd
and place the resulting zip file in it.
returns the full path to the zip file or False on error.
"""
ret = False
fs = FileSystem()
packd = wd + "/package"
zipfn = packd + "/" + fname + "-" + vstr + ".zip"
with tempfile.TemporaryDirectory() as td:
for fn in files:
fnd = fs.dirname(fn)
if len(fnd) > 0:
tfnd = td + "/" + fnd
print("looking for dir: {}".format(tfnd))
if not fs.dirExists(tfnd):
print("making dir: {}".format(tfnd))
fs.makePath(tfnd)
fnd += "/"
dest = td + "/" + fnd + fs.basename(fn)
print("copying: {} to {}".format(fn, dest))
xdest = fs.copyfile(fn, dest)
print("copied {} to {}".format(fn, xdest))
installRequirements(wd + "/requirements.txt", td)
fs.makePath(packd)
os.chdir(td)
# ensure that the mode of all files in the zip is correct
print("zipping up")
cmd = "zip -r " + zipfn + " ."
runcmd(cmd)
# pz = PyZip(PyFolder("./", interpret=False))
# pz.save(zipfn)
os.chdir(packd)
ret = True
return zipfn if ret else None
def updateLambda(lname, config, zipfn, role=None):
try:
lc = boto3.client("lambda")
if lc is not None:
args = {
"FunctionName": lname,
"ZipFile": getZip(zipfn)
}
resp = lc.update_function_code(**args)
if "FunctionArn" in resp:
print("Updated function {} - arn: {}".format(lname, resp["FunctionArn"]))
else:
print("an error occurred updating function, no arn returned")
sys.exit(1)
xrole = config["role"] if role is None else role
tags = unpackList(config["tags"])
envar = unpackList(config["codeenv"])
args = {
"FunctionName": lname,
"Runtime": config["runtime"],
"Role": xrole,
"Handler": config["handler"],
"Description": config["description"],
"Timeout": config["timeout"],
"MemorySize": config["memory"],
"Environment": {"Variables": envar}
}
resp = lc.update_function_configuration(**args)
if "FunctionArn" in resp:
print("Updated function config {} - arn: {}".format(lname, resp["FunctionArn"]))
args = {
"Resource": resp["FunctionArn"],
"Tags": tags
}
lc.tag_resource(**args)
print("Re-tagged function")
else:
print("an error occurred updating function config, no arn returned")
sys.exit(1)
except Exception as e:
emsg = "update lambda: error: {}: {}".format(type(e).__name__, e)
print(emsg)
sys.exit(1)
def installLambda(lname, config, zipfn):
global VPC
try:
lc = boto3.client("lambda")
if lc is not None:
tags = unpackList(config["tags"])
envar = unpackList(config["codeenv"])
args = {
"FunctionName": lname,
"Runtime": config["runtime"],
"Role": config["role"],
"Handler": config["handler"],
"Code": {'ZipFile': getZip(zipfn)},
"Description": config["description"],
"Timeout": config["timeout"],
"MemorySize": config["memory"],
"Publish": True,
"Environment": {"Variables": envar},
"Tags": tags
}
if VPC:
vpc = unpackList(config["vpc"])
args["VpcConfig"] = {
'SubnetIds': vpc["subnets"],
'SecurityGroupIds': [vpc["securitygroup"]]
}
resp = lc.create_function(**args)
if "FunctionArn" in resp:
print("Created function {} - arn: {}".format(lname, resp["FunctionArn"]))
else:
print("an error occurred, no arn returned")
sys.exit(1)
else:
print("couldn't get a client")
sys.exit(1)
except Exception as e:
emsg = "install lambda: error: {}: {}".format(type(e).__name__, e)
print(emsg)
sys.exit(1)
def getZip(zipfn):
with open(zipfn, 'rb') as zfn:
bts = zfn.read()
return bts
def unpackList(carr):
v = {}
for ln in carr:
for k in ln.keys():
v[k] = ln[k]
return v
# ------------------
# Script starts here
# ------------------
parser = argparse.ArgumentParser(description="""Installs or updates the lambda functions for chaim.
Designed to be called from make files.""")
parser.add_argument("-b", "--build", action="store_true",
help="increment the build number and push to github.")
parser.add_argument("-c", "--clean", action="store_true",
help="remove the package/ directories (and their contents) and exit.")
parser.add_argument("-n", "--novpc", action="store_true",
help="Do not try and install into a VPC.")
parser.add_argument("-t", "--tag", help="set version and git tag it.")
parser.add_argument("environment", default="dev", choices=["dev", "prod", "test"],
help="environment to install/update (dev/prod etc).")
args = parser.parse_args()
env = args.environment
medir = os.getcwd()
fs = FileSystem()
VPC = INC = True
if args.tag:
gitTag(args.tag)
sys.exit(0)
if args.clean:
packd = medir + "/package/"
if fs.dirExists(packd):
os.chdir(packd)
print("cleaning package directory")
try:
cmd = "ls -1tr | head -n -1 |xargs rm"
runcmd(cmd)
except subprocess.CalledProcessError:
# ignore errors (if there are no files to clean)
pass
sys.exit(0)
if args.novpc:
VPC = False
if args.build:
updateBuild()
sys.exit(0)
me = os.path.basename(medir)
supp = "-dev" if env == "dev" else ""
yamlfn = medir + "/" + me + supp + ".yaml"
reqsfn = medir + "/requirements.txt"
if os.path.exists(yamlfn):
with open(yamlfn, "r") as yfs:
config = yaml.load(yfs, Loader=yaml.SafeLoader)
config["tags"][0]["environment"] = env
config["codeenv"][0]["environment"] = env
lambdaname = config["tags"][0]["Name"] + "-" + env
verstr = getVerstr()
fs.copyfile(os.path.dirname(__file__) + "/version", medir + "/version")
config["tags"][0]["version"] = verstr
packd = medir + "/package"
lzip = packd + "/" + me + "-" + verstr + ".zip"
if env not in ["dev"] and fs.fileExists(lzip):
zipfn = lzip
else:
zipfn = prepareLambda(me, verstr, medir, config["files"], "requirements.txt")
if zipfn is None:
print("failed to create zip file")
sys.exit(1)
allfuncs = getFunctions()
if findFunction(allfuncs, lambdaname):
print("\nupdating {} to {}\n".format(lambdaname, verstr))
updateLambda(lambdaname, config, zipfn)
else:
print("\nfunction {} doesn't exist, yet, installing {}\n".format(lambdaname, verstr))
installLambda(lambdaname, config, zipfn)
else:
print("no config file found: {}".format(yamlfn))
sys.exit(1)