This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
etcd.py
163 lines (133 loc) · 4.9 KB
/
etcd.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
# project aquarium's backend
# Copyright (C) 2021 SUSE, LLC.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import asyncio
import multiprocessing
import shlex
from logging import Logger
from pathlib import Path
from typing import List, Optional
from fastapi.logger import logger as fastapi_logger
from gravel.controllers.errors import GravelError
from gravel.controllers.gstate import GlobalState
class ContainerFetchError(GravelError):
pass
logger: Logger = fastapi_logger
# We need to rely on "spawn" because otherwise the subprocess' signal
# handler will play nasty tricks with uvicorn's and fastapi's signal
# handlers.
# For future reference,
# - uvicorn issue: https://github.com/encode/uvicorn/issues/548
# - python bug report: https://bugs.python.org/issue43064
# - somewhat of a solution, using "spawn" for multiprocessing:
# https://github.com/tiangolo/fastapi/issues/1487#issuecomment-657290725
#
# And because we need to rely on spawn, which will create a new python
# interpreter to execute what we are specifying, the function we're passing
# needs to be pickled. And nested functions, apparently, can't be pickled. Thus,
# we need to have the functions at the top-level scope.
#
def _bootstrap_etcd_process(cmd: List[str]):
async def _run_etcd():
process = await asyncio.create_subprocess_exec(*cmd)
await process.wait()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(_run_etcd())
except KeyboardInterrupt:
pass
async def spawn_etcd(
gstate: GlobalState,
new: bool,
token: Optional[str],
hostname: str,
address: str,
initial_cluster: Optional[str] = None,
) -> None:
assert hostname
assert address
data_dir: Path = Path(gstate.config.options.etcd.data_dir)
if not data_dir.exists():
data_dir.mkdir(0o700)
logger.info(f"starting etcd, hostname: {hostname}, addr: {address}")
def _get_etcd_args() -> List[str]:
client_url: str = f"http://{address}:2379"
peer_url: str = f"http://{address}:2380"
nonlocal initial_cluster
if not initial_cluster:
initial_cluster = f"{hostname}={peer_url}"
args: List[str] = [
"--name",
hostname,
"--initial-advertise-peer-urls",
peer_url,
"--listen-peer-urls",
peer_url,
"--listen-client-urls",
f"{client_url},http://127.0.0.1:2379",
"--advertise-client-urls",
client_url,
"--initial-cluster",
initial_cluster,
"--data-dir",
str(data_dir),
]
if new:
assert token
args += ["--initial-cluster-state", "new"]
args += ["--initial-cluster-token", token]
else:
args += ["--initial-cluster-state", "existing"]
return args
def _get_container_cmd() -> List[str]:
registry = gstate.config.options.etcd.registry
version = gstate.config.options.etcd.version
return [
"podman",
"run",
"--rm",
"--replace",
"--net=host",
"--entrypoint",
"/usr/local/bin/etcd",
"-v",
f"{data_dir}:{data_dir}",
"--name",
f"etcd.{hostname}",
f"{registry}:{version}",
] + _get_etcd_args()
cmd = _get_container_cmd()
ctx = multiprocessing.get_context("spawn")
logger.debug("spawn etcd: " + shlex.join(cmd))
process = ctx.Process(target=_bootstrap_etcd_process, args=(cmd,))
process.start()
logger.info(f"started etcd process pid = {process.pid}")
await gstate.init_store()
async def etcd_pull_image(gstate: GlobalState) -> None:
registry = gstate.config.options.etcd.registry
version = gstate.config.options.etcd.version
logger.debug(f"fetching etcd image from {registry}:{version}")
cmd = shlex.split(f"podman pull {registry}:{version}")
process = await asyncio.create_subprocess_exec(
*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
try:
# wait for 5 minutes
retcode = await asyncio.wait_for(process.wait(), 300)
if retcode != 0:
stderr = "unknown error"
if process.stderr:
stderr = (await process.stderr.read()).decode("utf-8")
raise ContainerFetchError(stderr)
except TimeoutError:
raise ContainerFetchError("timed out")