-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
274 lines (260 loc) · 9.39 KB
/
main.py
File metadata and controls
274 lines (260 loc) · 9.39 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
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
import subprocess
import traceback
import os
import tempfile
import shutil
from rfctl.client import LambdaClient
import yaml
def lambda_handler(event: dict, ctx):
try:
_call_method = event.get("_call_method")
if _call_method == "deploy-function":
return deploy_func(event, ctx)
if _call_method == "invoke-function":
return invoke_func(event, ctx)
if _call_method == "delete-function":
return delete_func(event, ctx)
if _call_method == "get-function":
return get_func(event, ctx)
except Exception as e:
return {
"code": 1,
"message": "error",
"result": "{}".format(e)
}
return {
"code": 1,
"message": "error",
"result": "call method error"
}
def deploy_func(event: dict, ctx):
name, namespace = event.get("name"), event.get("namespace")
manifest, src_code = event.get("manifest"), event.get("src_code")
if not os.getenv("S3_SYNC_URI"):
raise Exception("S3_SYNC_URI error")
func_info: dict = yaml.load(manifest, yaml.FullLoader)
# write source file
workdir = "{}/{}/{}".format(os.path.abspath(os.getenv("WORKSPACE_DIR")), namespace, name)
if os.path.exists(workdir):
shutil.rmtree(workdir)
rc, _, _ = run_cmd(["mkdir", "-p", workdir])
if rc != 0:
raise Exception("mkdir workspace error")
source_items = {
"{}/lambda.yaml".format(workdir): manifest
}
for path, content in src_code.items():
source_items.update({"{}/{}".format(workdir, path): content})
for path, content in source_items.items():
directory = os.path.dirname(path)
if directory and not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
with open(path, 'w', encoding="utf-8") as f:
f.write(content)
# sync files to s3
s3_sync = ["juicefs", "sync", "--check-all", "--delete-dst"]
s3_sync.extend(["{}/".format(workdir), "{}/{}/{}/".format(os.getenv("S3_SYNC_URI"), namespace, name)])
rc, _, _ = run_cmd(s3_sync, cwd=workdir)
if rc != 0:
raise Exception("save files to s3 error")
# create or update func code
subcmd = "update-code"
rc, get_stdout, get_stderr = run_cmd(["rfctl", "get"], cwd=workdir)
if "ResourceNotFoundException" in get_stdout or "ResourceNotFoundException" in get_stderr:
subcmd = "create"
rc, code_stdout, code_stderr = run_cmd(["rfctl", subcmd], cwd=workdir)
if rc != 0:
return {
"code": 1,
"message": "error",
"result": "code deploy:\n{}\n{}".format(code_stdout, code_stderr)
}
# update func config
if subcmd != "create":
rc, cfg_stdout, cfg_stderr = run_cmd(["rfctl", "update-config"], cwd=workdir)
if rc != 0:
return {
"code": 1,
"message": "error",
"result": "code deploy:\n{}\n{}\nupdate-config:\n{}\n{}".format(code_stdout, code_stderr, cfg_stdout, cfg_stderr)
}
# update func url
exist_rc, _, _ = run_cmd(["rfctl", "get-url"], cwd=workdir)
exist_url = exist_rc == 0
if func_info.get("spec", {}).get("enable_url") and not exist_url:
rc, _, _ = run_cmd(["rfctl", "create-url"], cwd=workdir)
if rc != 0:
return {
"code": 1,
"message": "error",
"result": "enable func url call error"
}
if not func_info.get("spec", {}).get("enable_url") and exist_url:
rc, _, _ = run_cmd(["rfctl", "delete-url"], cwd=workdir)
if rc != 0:
return {
"code": 1,
"message": "error",
"result": "disable func url call error"
}
return {
"code": 0,
"message": "success"
}
def invoke_func(event: dict, ctx):
client = LambdaClient(event.get("namespace"))
rsp = client.invoke_function(event.get("name"), event.get("payload"), get_log=True)
return rsp
def delete_func(event: dict, ctx):
name, namespace = event.get("name"), event.get("namespace")
if not os.getenv("S3_SYNC_URI"):
raise Exception("S3_SYNC_URI error")
client = LambdaClient(namespace)
client.lambda_client.get_function(FunctionName=name)
# delete funcdef
workdir = tempfile.mkdtemp()
manifest = "{}/lambda.yaml".format(workdir)
content = """metadata:
name: {}
namespace: {}
# placeholder spec
spec:
build:
source: .
manifest: requirements.txt
language: python
architecture: x86_64
handler: main.lambda_handler
timeout: 120
runtime: "python3.10"
""".format(name, namespace)
with open(manifest, 'w', encoding="utf-8") as f:
f.write(content)
rc, del_stdout, del_stderr = run_cmd(["rfctl", "delete"], cwd=workdir)
if rc != 0:
return {
"code": 1,
"message": "error",
"result": "delete function:\n{}\n{}".format(del_stdout, del_stderr)
}
shutil.rmtree(workdir)
# fetch&backup code
workdir = os.path.abspath(tempfile.mkdtemp())
s3_sync = ["juicefs", "sync", "--check-all", "--delete-dst"]
s3_sync.extend(["{}/{}/{}/".format(os.getenv("S3_SYNC_URI"), namespace, name), "{}/".format(workdir)])
rc, _, _ = run_cmd(s3_sync, cwd=workdir)
if rc != 0:
raise Exception("sync files from s3 error")
s3_sync = ["juicefs", "sync", "--check-all", "--delete-dst"]
s3_sync.extend(["{}/".format(workdir), "{}/deleted/{}/{}/".format(os.getenv("S3_SYNC_URI"), namespace, name)])
rc, _, _ = run_cmd(s3_sync, cwd=workdir)
if rc != 0:
raise Exception("save backup files to s3 error")
shutil.rmtree(workdir)
# delete func code
workdir = os.path.abspath(tempfile.mkdtemp())
s3_sync = ["juicefs", "sync", "--check-all", "--delete-dst"]
s3_sync.extend(["{}/".format(workdir), "{}/{}/{}/".format(os.getenv("S3_SYNC_URI"), namespace, name)])
rc, _, _ = run_cmd(s3_sync, cwd=workdir)
if rc != 0:
raise Exception("delete files from s3 error")
shutil.rmtree(workdir)
return {
"code": 0,
"message": "success"
}
def get_func(event: dict, ctx):
# check func exist
name, namespace = event.get("name"), event.get("namespace")
if not os.getenv("S3_SYNC_URI"):
raise Exception("S3_SYNC_URI error")
client = LambdaClient(namespace)
client.lambda_client.get_function(FunctionName=name)
workdir = "{}/{}/{}".format(os.path.abspath(os.getenv("WORKSPACE_DIR")), namespace, name)
os.makedirs(workdir, exist_ok=True)
# sync files from s3
s3_sync = ["juicefs", "sync", "--check-all", "--delete-dst"]
s3_sync.extend(["{}/{}/{}/".format(os.getenv("S3_SYNC_URI"), namespace, name), "{}/".format(workdir)])
rc, _, _ = run_cmd(s3_sync, cwd=workdir)
if rc != 0:
raise Exception("sync files from s3 error")
# build sources
manifest, src_code = "", {}
for root, _, files in os.walk(workdir):
for file in files:
file_path = os.path.join(root, file)
if file_path.endswith("lambda.yaml") and not manifest:
with open(file_path, 'r', encoding='utf-8') as f:
manifest = f.read()
continue
try:
with open(file_path, 'r', encoding='utf-8') as f:
_path = file_path.removeprefix(workdir).lstrip("/")
src_code[_path] = f.read()
except Exception as e:
print(f"can not read file {file_path}: {e}")
return {
"code": 0,
"result": {
"manifest": manifest,
"src_code": src_code
}
}
def run_cmd(cmd, cwd=None, env: dict = None):
try:
if not env:
env = os.environ.copy()
stdout, stderr = "", ""
with subprocess.Popen(cmd, cwd=cwd, env=env, stderr=subprocess.PIPE, stdout=subprocess.PIPE) as process:
stdout, stderr = process.communicate()
rc = process.wait()
if isinstance(stdout, bytes):
stdout = stdout.decode()
if isinstance(stderr, bytes):
stderr = stderr.decode()
return rc, stdout, stderr
except Exception as e:
print(traceback.format_exc())
return 1, None, None
if __name__ == "__main__":
os.environ.update({"WORKSPACE_DIR": "./temp"})
_manifest = '''metadata:
name: demo-py310
namespace: default
spec:
description: |
A demo python func
func's args
```
{"a":1}
这是一个函数
```
build:
source: .
manifest: requirements.txt
language: "python"
architecture: "x86_64"
enable_url: true
handler: "main.lambda_handler"
timeout: 120
runtime: "python3.10"'''
_src_code = {
"main.py": '''
def lambda_handler(event, ctx):
ctx.log("this is log")
return {
"msg": "hello py3.10"
}
''',
"requirements.txt": "",
"tests/test.py": "print('test')"
}
print("create==========>")
print(deploy_func({"namespace": "default", "name": "demo-py310", "manifest": _manifest, "src_code": _src_code}, {}))
print("invoke==========>")
_client = LambdaClient("default")
print(_client.invoke_function("demo-py310", {}, get_log=True))
print("get==========>")
print(get_func({"namespace": "default", "name": "demo-py310"}, {}))
print("delete==========>")
print(delete_func({"namespace": "default", "name": "demo-py310"}, {}))