-
Notifications
You must be signed in to change notification settings - Fork 7
/
make.jl
134 lines (108 loc) · 3.69 KB
/
make.jl
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
module MakeLatexLambda
using JSON
using AWSCore
using AWSS3
using AWSIAM
using AWSLambda: create_lambda, update_lambda, lambda_configuration,
invoke_lambda, create_lambda_role
using InfoZIP
function all()
download()
build()
zip()
deploy()
test()
end
# Download TeXLive installer.
function download()
run(`wget
http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz`)
end
# Build docker image from Dockerfile.
function build()
run(`docker build -t octech/lambdalatex .`)
end
# Create Lambda deployment .ZIP file from docker image.
function zip()
rm("latexlambda.zip", force=true)
run(`docker run --rm -it -v $(pwd()):/var/host octech/lambdalatex zip --symlinks -r -9 /var/host/latexlambda.zip .`)
end
# Deploy .ZIP file to Lambda.
function deploy()
AWSCore.set_debug_level(2)
aws = AWSCore.default_aws_config()
n = AWSCore.aws_account_number(aws)
aws[:lambda_bucket] = "octech.latexlambda.deploy.$n"
s3_create_bucket(aws[:lambda_bucket])
s3_put(aws[:lambda_bucket], "latexlambda.zip", read("latexlambda.zip"))
role = create_lambda_role(aws, "latex", """{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "*"
}
]
}""")
if lambda_configuration("latex") == nothing
create_lambda(aws, "latex"; Runtime="python3.6",
S3Key="latexlambda.zip",
MemorySize=1536,
Role=role)
else
update_lambda(aws, "latex"; S3Key="latexlambda.zip", Role=role)
end
end
# Docker image interactive shell.
function shell()
run(`docker run --rm -it -v $(pwd()):/var/host octech/lambdalatex bash`)
end
test_zip = base64encode(create_zip("document.tex" =>
readstring("test_input.tex")))
# Test latex in local docker image.
function localtest()
write("test_input.zip.base64", test_zip)
pycmd = """
import lambda_function
import json
import base64
out = lambda_function.lambda_handler(
{'input': open('/var/host/test_input.zip.base64').read()}, {})
with open('/var/host/test_output.json', 'w') as f:
f.write(json.dumps(out))
"""
run(`docker run --security-opt seccomp:unconfined --rm -v $(pwd()):/var/host octech/lambdalatex strace -f -t -e trace=file -o /var/host/strace.out python3 -c $pycmd`)
out = JSON.parse(readstring("test_output.json"))
write("test_output_local.pdf", base64decode(out["output"]))
write("test_output_local.stdout", out["stdout"])
end
# Test latex on Lambda.
function test()
@time out = invoke_lambda("latex"; input=test_zip)
write("test_output_lambda.pdf", base64decode(out[:output]))
write("test_output_lambda.stdout", out[:stdout])
end
# Test latex on Lambda.
function s3test()
@time out = invoke_lambda("latex"; input_bucket="cinchtmp.tex",
input_key="Archive.zip",
output_bucket="cinchtmp.pdf",
output_key="test.pdf")
write("test_output_lambda.stdout", out[:stdout])
end
# Remove intermediate files.
function clean()
rm("test_output.json", force=true)
rm("test_output_local.stdout", force=true)
rm("test_output_local.pdf", force=true)
rm("test_output_lambda.stdout", force=true)
rm("test_output_lambda.pdf", force=true)
rm("latexlambda.zip", force=true)
end
if length(ARGS) == 0
all()
else
include_string("$(ARGS[1])()")
end
end # module MakeLatexLambda