forked from claranet/terraform-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
143 lines (115 loc) · 3.58 KB
/
build.py
File metadata and controls
143 lines (115 loc) · 3.58 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
# Builds a zip file from the source_dir or source_file.
# Installs dependencies with pip automatically.
import hash
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
from contextlib import contextmanager
@contextmanager
def cd(path):
"""
Changes the working directory.
"""
cwd = os.getcwd()
print('cd', path)
try:
os.chdir(path)
yield
finally:
os.chdir(cwd)
def format_command(command):
"""
Formats a command for displaying on screen.
"""
args = []
for arg in command:
if ' ' in arg:
args.append('"' + arg + '"')
else:
args.append(arg)
return ' '.join(args)
def list_files(top_path):
"""
Returns a sorted list of all files in a directory.
"""
results = []
for root, dirs, files in os.walk(top_path):
for file_name in files:
file_path = os.path.join(root, file_name)
relative_path = os.path.relpath(file_path, top_path)
results.append(relative_path)
results.sort()
return results
@contextmanager
def tempdir():
"""
Creates a temporary directory and then deletes it afterwards.
"""
print('mktemp -d')
path = tempfile.mkdtemp(prefix='terraform-aws-lambda-', dir='/tmp')
print(path)
try:
yield path
finally:
shutil.rmtree(path)
def dequote(value):
"""
Handles quotes around values in a shell-compatible fashion.
"""
return ' '.join(shlex.split(value))
filename = dequote(sys.argv[1])
runtime = dequote(sys.argv[2])
source_path = dequote(sys.argv[3])
absolute_filename = os.path.abspath(filename)
if os.path.isdir(source_path):
source_dir = source_path
source_files = list_files(source_path)
else:
source_dir = os.path.dirname(source_path)
source_files = [os.path.basename(source_path)]
cmd = [
'docker',
'run',
'--rm',
'-t',
'-v', '%s:/src' % source_dir,
'-v', '%s:/out' % os.path.abspath(hash.DIRNAME_BUILDS),
'public.ecr.aws/sam/build-%s:latest' % runtime,
# 'lambci/lambda:build-%s' % runtime,
'bash', '-c',
''' cp -r /src /build &&
cd /build &&
yum install -y llvm llvm-dev &&
pip3 install --upgrade pip &&
pip3 install --progress-bar off -r requirements.txt --target . &&
find . -name \\*\\.pyc -exec mv '{}' . \\; &&
find . -name \\*\\.so -exec strip '{}' \\; &&
find . -type d -name \\*-info -prune -exec rm -rdf '{}' \\; &&
find . -type d -name tests -prune -exec rm -rdf '{}' \\; &&
find . -type d -name boto3 -prune -exec rm -rdf '{}' \\; &&
find . -type d -name botocore -prune -exec rm -rdf '{}' \\; &&
find . -type d -name docutils -prune -exec rm -rdf '{}' \\; &&
find . -type d -name dateutil -prune -exec rm -rdf '{}' \\; &&
find . -type d -name jmespath -prune -exec rm -rdf '{}' \\; &&
find . -type d -name s3transfer -prune -exec rm -rdf '{}' \\; &&
find . -type d -name doc -prune -exec rm -rdf '{}' \\; &&
chmod -R 755 . &&
zip -r /out/%s . &&
chown $(stat -c '%%u:%%g' /out) /out/%s
''' % (os.path.basename(filename), os.path.basename(filename))
]
print(cmd)
try:
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('Error running docker command')
print(e.cmd)
print('Output:')
print(e.output)
print('raising ...')
raise(e)
print('out dir %s' % os.path.abspath(hash.DIRNAME_BUILDS))
print('Created {}'.format(filename))