-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconf.py
236 lines (195 loc) · 8.05 KB
/
conf.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
import logging
import os
import re
from enum import Enum
from os.path import isfile, join, exists, abspath, isdir
from pathlib import Path
from subprocess import check_output
from typing import NewType, Optional, List
from os import environ
from pydantic import BaseSettings, Field
logger = logging.getLogger(__name__)
Url = NewType("Url", str)
class DnsResolver(str, Enum):
resolv_conf = "resolv.conf" # Simply copy from /etc/resolv.conf
resolvectl = "resolvectl" # Systemd-resolved, common on Ubuntu
def etc_resolv_conf_dns_servers():
with open("/etc/resolv.conf", "r") as resolv_file:
for line in resolv_file.readlines():
ip = re.findall(r"^nameserver\s+([\w.]+)$", line)
if ip:
yield ip[0]
def systemd_resolved_dns_servers(interface):
# Example output format from systemd-resolve --status {interface}:
# Link 2 (enp7s0)
# Current Scopes: DNS
# DefaultRoute setting: yes
# LLMNR setting: yes
# MulticastDNS setting: no
# DNSOverTLS setting: no
# DNSSEC setting: no
# DNSSEC supported: no
# Current DNS Server: 213.133.100.100
# DNS Servers: 213.133.100.100
# 213.133.98.98
# 213.133.99.99
# 2a01:4f8:0:1::add:9898
# 2a01:4f8:0:1::add:1010
# 2a01:4f8:0:1::add:9999
output = check_output(["/usr/bin/systemd-resolve", "--status", interface])
nameserver_line = False
for line in output.split(b"\n"):
if b"DNS Servers" in line:
nameserver_line = True
_, ip = line.decode().split(":", 1)
yield ip.strip()
elif nameserver_line:
ip = line.decode().strip()
if ip:
yield ip
class Settings(BaseSettings):
SUPERVISOR_HOST = "127.0.0.1"
SUPERVISOR_PORT: int = 4020
# Public domain name
DOMAIN_NAME: Optional[str] = Field(
default="localhost",
description="Default public domain name",
)
START_ID_INDEX: int = 4
PREALLOC_VM_COUNT: int = 0
REUSE_TIMEOUT: float = 60 * 60.0
WATCH_FOR_MESSAGES = True
WATCH_FOR_UPDATES = True
API_SERVER = "https://official.aleph.cloud"
USE_JAILER = True
# System logs make boot ~2x slower
PRINT_SYSTEM_LOGS = False
DEBUG_ASYNCIO = False
# Networking does not work inside Docker/Podman
ALLOW_VM_NETWORKING = True
NETWORK_INTERFACE = "eth0"
IPV4_ADDRESS_POOL = "172.16.0.0/12"
IPV4_NETWORK_SIZE = 24
DNS_RESOLUTION: Optional[DnsResolver] = DnsResolver.resolv_conf
DNS_NAMESERVERS: Optional[List[str]] = None
FIRECRACKER_PATH = "/opt/firecracker/firecracker"
JAILER_PATH = "/opt/firecracker/jailer"
LINUX_PATH = "/opt/firecracker/vmlinux.bin"
INIT_TIMEOUT: float = 20.0
CONNECTOR_URL = Url("http://localhost:4021")
CACHE_ROOT = Path("/var/cache/aleph/vm")
MESSAGE_CACHE = CACHE_ROOT / "message"
CODE_CACHE = CACHE_ROOT / "code"
RUNTIME_CACHE = CACHE_ROOT / "runtime"
DATA_CACHE = CACHE_ROOT / "data"
EXECUTION_ROOT = Path("/var/lib/aleph/vm")
EXECUTION_DATABASE = EXECUTION_ROOT / "executions.sqlite3"
EXECUTION_LOG_ENABLED = False
EXECUTION_LOG_DIRECTORY = EXECUTION_ROOT / "executions"
PERSISTENT_VOLUMES_DIR = EXECUTION_ROOT / "volumes" / "persistent"
MAX_PROGRAM_ARCHIVE_SIZE = 10_000_000 # 10 MB
MAX_DATA_ARCHIVE_SIZE = 10_000_000 # 10 MB
# hashlib.sha256(b"secret-token").hexdigest()
ALLOCATION_TOKEN_HASH = (
"151ba92f2eb90bce67e912af2f7a5c17d8654b3d29895b042107ea312a7eebda"
)
FAKE_DATA_PROGRAM: Optional[Path] = None
BENCHMARK_FAKE_DATA_PROGRAM = Path(
environ.get("BENCHMARK_FAKE_DATA_PROGRAM")
or abspath(join(__file__, "../../examples/example_fastapi"))
)
FAKE_DATA_MESSAGE = Path(
environ.get("FAKE_DATA_MESSAGE")
or abspath(join(__file__, "../../examples/message_from_aleph.json"))
)
FAKE_DATA_DATA: Optional[Path] = Path(
environ.get("FAKE_DATA_DATA")
or abspath(join(__file__, "../../examples/data/"))
)
FAKE_DATA_RUNTIME = Path(
environ.get("FAKE_DATA_RUNTIME")
or abspath(join(__file__, "../../runtimes/aleph-debian-11-python/rootfs.squashfs"))
)
FAKE_DATA_VOLUME: Optional[Path] = Path(
environ.get("FAKE_DATA_VOLUME")
or abspath(join(__file__, "../../examples/volumes/volume-venv.squashfs"))
)
CHECK_FASTAPI_VM_ID = (
"67705389842a0a1b95eaa408b009741027964edc805997475e95c505d642edd8"
)
SENTRY_DSN: Optional[str] = None
# Fields
SENSITIVE_FIELDS: List[str] = Field(
default=["SENTRY_DSN"],
description="Sensitive fields, redacted from `--print-settings`.",
)
def update(self, **kwargs):
for key, value in kwargs.items():
if key != key.upper():
logger.warning(f"Setting {key} is not uppercase")
if hasattr(self, key):
setattr(self, key, value)
else:
raise ValueError(f"Unknown setting '{key}'")
def check(self):
assert isfile(self.FIRECRACKER_PATH), f"File not found {self.FIRECRACKER_PATH}"
assert isfile(self.JAILER_PATH), f"File not found {self.JAILER_PATH}"
assert isfile(self.LINUX_PATH), f"File not found {self.LINUX_PATH}"
assert self.CONNECTOR_URL.startswith(
"http://"
) or self.CONNECTOR_URL.startswith("https://")
if self.ALLOW_VM_NETWORKING:
assert exists(
f"/sys/class/net/{self.NETWORK_INTERFACE}"
), f"Network interface {self.NETWORK_INTERFACE} does not exist"
if self.FAKE_DATA_PROGRAM:
assert isdir(
self.FAKE_DATA_PROGRAM
), "Local fake program directory is missing"
assert isfile(self.FAKE_DATA_MESSAGE), "Local fake message is missing"
assert isdir(self.FAKE_DATA_DATA), "Local fake data directory is missing"
assert isfile(
self.FAKE_DATA_RUNTIME
), "Local runtime .squashfs build is missing"
if "," in str(self.FAKE_DATA_VOLUME): # allow multiple volumes with format "host_path:mountpoint,host_path:mountpoint"
for volume_bind in str(self.FAKE_DATA_VOLUME).split(","):
assert isfile(
volume_bind.split(":")[0]
), f"Local data volume {volume_bind.split(':')[0]} is missing"
else:
assert isfile(
self.FAKE_DATA_VOLUME
), f"Local data volume {volume_bind.split(':')[0]} is missing"
def setup(self):
os.makedirs(self.MESSAGE_CACHE, exist_ok=True)
os.makedirs(self.CODE_CACHE, exist_ok=True)
os.makedirs(self.RUNTIME_CACHE, exist_ok=True)
os.makedirs(self.DATA_CACHE, exist_ok=True)
if self.DNS_NAMESERVERS is None and self.DNS_RESOLUTION:
if self.DNS_RESOLUTION == DnsResolver.resolv_conf:
self.DNS_NAMESERVERS = list(etc_resolv_conf_dns_servers())
elif self.DNS_RESOLUTION == DnsResolver.resolvectl:
self.DNS_NAMESERVERS = list(
systemd_resolved_dns_servers(interface=self.NETWORK_INTERFACE)
)
else:
assert "This should never happen"
def display(self) -> str:
attributes: Dict[str, Any] = {}
for attr in self.__dict__.keys():
if attr != attr.upper():
# Settings are expected to be ALL_UPPERCASE, other attributes snake_case or CamelCase
continue
if getattr(self, attr) and attr in self.SENSITIVE_FIELDS:
attributes[attr] = "<REDACTED>"
else:
attributes[attr] = getattr(self, attr)
return "\n".join(
f"{attribute:<27} = {value}" for attribute, value in attributes.items()
)
class Config:
env_prefix = "ALEPH_VM_"
case_sensitive = False
env_file = ".env"
# Settings singleton
settings = Settings()