-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathopenwhisk.py
377 lines (337 loc) · 14.4 KB
/
openwhisk.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
import os
import subprocess
from typing import cast, Dict, List, Optional, Tuple, Type
import docker
from sebs.benchmark import Benchmark
from sebs.cache import Cache
from sebs.faas import System, PersistentStorage
from sebs.faas.function import Function, ExecutionResult, Trigger
from sebs.openwhisk.container import OpenWhiskContainer
from sebs.openwhisk.storage import Minio
from sebs.openwhisk.triggers import LibraryTrigger, HTTPTrigger
from sebs.utils import LoggingHandlers
from .config import OpenWhiskConfig
from .function import OpenWhiskFunction, OpenWhiskFunctionConfig
from ..config import SeBSConfig
class OpenWhisk(System):
_config: OpenWhiskConfig
def __init__(
self,
system_config: SeBSConfig,
config: OpenWhiskConfig,
cache_client: Cache,
docker_client: docker.client,
logger_handlers: LoggingHandlers,
):
super().__init__(system_config, cache_client, docker_client)
self._config = config
self.logging_handlers = logger_handlers
self.container_client = OpenWhiskContainer(
self.system_config, self.config, self.docker_client, self.config.experimentalManifest
)
if self.config.resources.docker_username:
if self.config.resources.docker_registry:
docker_client.login(
username=self.config.resources.docker_username,
password=self.config.resources.docker_password,
registry=self.config.resources.docker_registry,
)
else:
docker_client.login(
username=self.config.resources.docker_username,
password=self.config.resources.docker_password,
)
def initialize(self, config: Dict[str, str] = {}, resource_prefix: Optional[str] = None):
self.initialize_resources(select_prefix=resource_prefix)
@property
def config(self) -> OpenWhiskConfig:
return self._config
def get_storage(self, replace_existing: bool = False) -> PersistentStorage:
if not hasattr(self, "storage"):
if not self.config.resources.storage_config:
raise RuntimeError(
"OpenWhisk is missing the configuration of pre-allocated storage!"
)
self.storage = Minio.deserialize(
self.config.resources.storage_config, self.cache_client, self.config.resources
)
self.storage.logging_handlers = self.logging_handlers
else:
self.storage.replace_existing = replace_existing
return self.storage
def shutdown(self) -> None:
if hasattr(self, "storage") and self.config.shutdownStorage:
self.storage.stop()
if self.config.removeCluster:
from tools.openwhisk_preparation import delete_cluster # type: ignore
delete_cluster()
super().shutdown()
@staticmethod
def name() -> str:
return "openwhisk"
@staticmethod
def typename():
return "OpenWhisk"
@staticmethod
def function_type() -> "Type[Function]":
return OpenWhiskFunction
def get_wsk_cmd(self) -> List[str]:
cmd = [self.config.wsk_exec]
if self.config.wsk_bypass_security:
cmd.append("-i")
return cmd
def package_code(
self,
directory: str,
language_name: str,
language_version: str,
architecture: str,
benchmark: str,
is_cached: bool,
container_deployment: bool,
) -> Tuple[str, int, str]:
# Regardless of Docker image status, we need to create .zip file
# to allow registration of function with OpenWhisk
_, image_uri = self.container_client.build_base_image(
directory, language_name, language_version, architecture, benchmark, is_cached
)
# We deploy Minio config in code package since this depends on local
# deployment - it cannnot be a part of Docker image
CONFIG_FILES = {
"python": ["__main__.py"],
"nodejs": ["index.js"],
}
package_config = CONFIG_FILES[language_name]
benchmark_archive = os.path.join(directory, f"{benchmark}.zip")
subprocess.run(
["zip", benchmark_archive] + package_config, stdout=subprocess.DEVNULL, cwd=directory
)
self.logging.info(f"Created {benchmark_archive} archive")
bytes_size = os.path.getsize(benchmark_archive)
self.logging.info("Zip archive size {:2f} MB".format(bytes_size / 1024.0 / 1024.0))
return benchmark_archive, bytes_size, image_uri
def storage_arguments(self) -> List[str]:
storage = cast(Minio, self.get_storage())
return [
"-p",
"MINIO_STORAGE_SECRET_KEY",
storage.config.secret_key,
"-p",
"MINIO_STORAGE_ACCESS_KEY",
storage.config.access_key,
"-p",
"MINIO_STORAGE_CONNECTION_URL",
storage.config.address,
]
def create_function(
self,
code_package: Benchmark,
func_name: str,
container_deployment: bool,
container_uri: str,
) -> "OpenWhiskFunction":
self.logging.info("Creating function as an action in OpenWhisk.")
try:
actions = subprocess.run(
[*self.get_wsk_cmd(), "action", "list"],
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE,
)
function_found = False
docker_image = ""
for line in actions.stdout.decode().split("\n"):
if line and func_name in line.split()[0]:
function_found = True
break
function_cfg = OpenWhiskFunctionConfig.from_benchmark(code_package)
function_cfg.storage = cast(Minio, self.get_storage()).config
if function_found:
# docker image is overwritten by the update
res = OpenWhiskFunction(
func_name, code_package.benchmark, code_package.hash, function_cfg
)
# Update function - we don't know what version is stored
self.logging.info(f"Retrieved existing OpenWhisk action {func_name}.")
self.update_function(res, code_package, container_deployment, container_uri)
else:
try:
self.logging.info(f"Creating new OpenWhisk action {func_name}")
docker_image = self.system_config.benchmark_image_name(
self.name(),
code_package.benchmark,
code_package.language_name,
code_package.language_version,
code_package.architecture,
)
subprocess.run(
[
*self.get_wsk_cmd(),
"action",
"create",
func_name,
"--web",
"true",
"--docker",
docker_image,
"--memory",
str(code_package.benchmark_config.memory),
"--timeout",
str(code_package.benchmark_config.timeout * 1000),
*self.storage_arguments(),
code_package.code_location,
],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
)
function_cfg.docker_image = docker_image
res = OpenWhiskFunction(
func_name, code_package.benchmark, code_package.hash, function_cfg
)
except subprocess.CalledProcessError as e:
self.logging.error(f"Cannot create action {func_name}.")
self.logging.error(f"Output: {e.stderr.decode('utf-8')}")
raise RuntimeError(e)
except FileNotFoundError:
self.logging.error("Could not retrieve OpenWhisk functions - is path to wsk correct?")
raise RuntimeError("Failed to access wsk binary")
# Add LibraryTrigger to a new function
trigger = LibraryTrigger(func_name, self.get_wsk_cmd())
trigger.logging_handlers = self.logging_handlers
res.add_trigger(trigger)
return res
def update_function(
self,
function: Function,
code_package: Benchmark,
container_deployment: bool,
container_uri: str,
):
self.logging.info(f"Update an existing OpenWhisk action {function.name}.")
function = cast(OpenWhiskFunction, function)
docker_image = self.system_config.benchmark_image_name(
self.name(),
code_package.benchmark,
code_package.language_name,
code_package.language_version,
code_package.architecture,
)
try:
subprocess.run(
[
*self.get_wsk_cmd(),
"action",
"update",
function.name,
"--web",
"true",
"--docker",
docker_image,
"--memory",
str(code_package.benchmark_config.memory),
"--timeout",
str(code_package.benchmark_config.timeout * 1000),
*self.storage_arguments(),
code_package.code_location,
],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
)
function.config.docker_image = docker_image
except FileNotFoundError as e:
self.logging.error("Could not update OpenWhisk function - is path to wsk correct?")
raise RuntimeError(e)
except subprocess.CalledProcessError as e:
self.logging.error(f"Unknown error when running function update: {e}!")
self.logging.error("Make sure to remove SeBS cache after restarting OpenWhisk!")
self.logging.error(f"Output: {e.stderr.decode('utf-8')}")
raise RuntimeError(e)
def update_function_configuration(self, function: Function, code_package: Benchmark):
self.logging.info(f"Update configuration of an existing OpenWhisk action {function.name}.")
try:
subprocess.run(
[
*self.get_wsk_cmd(),
"action",
"update",
function.name,
"--memory",
str(code_package.benchmark_config.memory),
"--timeout",
str(code_package.benchmark_config.timeout * 1000),
*self.storage_arguments(),
],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
)
except FileNotFoundError as e:
self.logging.error("Could not update OpenWhisk function - is path to wsk correct?")
raise RuntimeError(e)
except subprocess.CalledProcessError as e:
self.logging.error(f"Unknown error when running function update: {e}!")
self.logging.error("Make sure to remove SeBS cache after restarting OpenWhisk!")
self.logging.error(f"Output: {e.stderr.decode('utf-8')}")
raise RuntimeError(e)
def is_configuration_changed(self, cached_function: Function, benchmark: Benchmark) -> bool:
changed = super().is_configuration_changed(cached_function, benchmark)
storage = cast(Minio, self.get_storage())
function = cast(OpenWhiskFunction, cached_function)
# check if now we're using a new storage
if function.config.storage != storage.config:
self.logging.info(
"Updating function configuration due to changed storage configuration."
)
changed = True
function.config.storage = storage.config
return changed
def default_function_name(self, code_package: Benchmark) -> str:
return (
f"{code_package.benchmark}-{code_package.language_name}-"
f"{code_package.language_version}"
)
def enforce_cold_start(self, functions: List[Function], code_package: Benchmark):
raise NotImplementedError()
def download_metrics(
self,
function_name: str,
start_time: int,
end_time: int,
requests: Dict[str, ExecutionResult],
metrics: dict,
):
pass
def create_trigger(self, function: Function, trigger_type: Trigger.TriggerType) -> Trigger:
if trigger_type == Trigger.TriggerType.LIBRARY:
return function.triggers(Trigger.TriggerType.LIBRARY)[0]
elif trigger_type == Trigger.TriggerType.HTTP:
try:
response = subprocess.run(
[*self.get_wsk_cmd(), "action", "get", function.name, "--url"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=True,
)
except FileNotFoundError as e:
self.logging.error(
"Could not retrieve OpenWhisk configuration - is path to wsk correct?"
)
raise RuntimeError(e)
stdout = response.stdout.decode("utf-8")
url = stdout.strip().split("\n")[-1] + ".json"
trigger = HTTPTrigger(function.name, url)
trigger.logging_handlers = self.logging_handlers
function.add_trigger(trigger)
self.cache_client.update_function(function)
return trigger
else:
raise RuntimeError("Not supported!")
def cached_function(self, function: Function):
for trigger in function.triggers(Trigger.TriggerType.LIBRARY):
trigger.logging_handlers = self.logging_handlers
cast(LibraryTrigger, trigger).wsk_cmd = self.get_wsk_cmd()
for trigger in function.triggers(Trigger.TriggerType.HTTP):
trigger.logging_handlers = self.logging_handlers
def disable_rich_output(self):
self.container_client.disable_rich_output = True