-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate_dockerfiles.py
189 lines (130 loc) · 3.97 KB
/
generate_dockerfiles.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
from pathlib import Path
RHASSPY_VERSION = "2.5.11"
BUILD_DOCS = """
FROM python:3.7
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app/docs
COPY docs/requirements.txt ./
RUN pip3 install --no-cache-dir -r requirements.txt
COPY docs .
RUN mkdocs build
"""
BUILD_FRONTEND = """
FROM node:lts
WORKDIR /usr/src/app/ui/frontend
COPY ui/frontend/package.json ui/frontend/package-lock.json ./
RUN npm ci
COPY ui/frontend .
RUN npm run build
"""
BUILD_VENV = """
FROM python:3.7
WORKDIR /usr/src/app
ENV LANG=C.UTF-8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PATH="/venv/bin:$PATH"
RUN python -m venv /venv --copies
COPY requirements.txt ./
RUN pip3 install -U pip setuptools wheel
RUN pip3 install --no-cache-dir -r requirements.txt
"""
FINAL_BULD = """
FROM {build_image}
WORKDIR /usr/src/app
ENV PYTHONUNBUFFERED=1
ENV PATH="/venv/bin:$PATH"
{extra}
COPY {venv_location} /venv
COPY {docs_location} ./docs/site
COPY setup setup
COPY home_intent home_intent
COPY ui ui
COPY {frontend_location} ./ui/frontend/build
ENTRYPOINT {entrypoint}
"""
WARNING = """### NOTE: THIS FILE IS AUTOGENERATED FROM generate_dockerfiles.py ###"""
def main():
make_local_dockerfile()
make_gh_actions_dockerfiles()
make_development_dockerfiles()
def write_dockerfile(filename: str, contents: str):
file = Path(filename)
if file.is_file():
file.chmod(0o644)
file.write_text(contents)
file.chmod(0o444)
def make_local_dockerfile():
final_build = FINAL_BULD.format(
build_image=f"rhasspy/rhasspy:{RHASSPY_VERSION}",
venv_location="--from=1 /venv",
docs_location="--from=0 /usr/src/app/docs/site",
frontend_location="--from=2 /usr/src/app/ui/frontend/build",
extra="",
entrypoint='[ "bash", "/usr/src/app/setup/setup.sh" ]',
)
dockerfile = f"""
{WARNING}
{BUILD_DOCS}
{BUILD_VENV}
{BUILD_FRONTEND}
{final_build}
"""
write_dockerfile("Dockerfile", dockerfile)
def make_gh_actions_dockerfiles():
make_static_dockerfile()
make_rhasspy_external_dockerfile()
make_gh_build_dockerfile()
def make_static_dockerfile():
dockerfile = f"""
{WARNING}
{BUILD_DOCS}
{BUILD_FRONTEND}
FROM scratch
COPY --from=0 /usr/src/app/docs/site /docs/site
COPY --from=1 /usr/src/app/ui/frontend/build /ui/frontend/build
"""
write_dockerfile("Dockerfile.static", dockerfile)
def make_rhasspy_external_dockerfile():
final_build = FINAL_BULD.format(
build_image="python:3.7-slim",
venv_location="--from=0 /venv",
docs_location="tmp/static/docs/site",
frontend_location="tmp/static/ui/frontend/build",
extra="RUN pip3 install --no-cache-dir supervisor",
entrypoint='[ "supervisord", "--configuration", "/usr/src/app/setup/supervisord.conf" ]',
)
dockerfile = f"""
{WARNING}
{BUILD_VENV}
{final_build}
"""
write_dockerfile("Dockerfile.rhasspy-external", dockerfile)
def make_gh_build_dockerfile():
final_build = FINAL_BULD.format(
build_image=f"rhasspy/rhasspy:{RHASSPY_VERSION}",
venv_location="--from=0 /venv",
docs_location="tmp/static/docs/site",
frontend_location="tmp/static/ui/frontend/build",
extra="",
entrypoint='[ "bash", "/usr/src/app/setup/setup.sh" ]',
)
dockerfile = f"""
{WARNING}
{BUILD_VENV}
{final_build}
"""
write_dockerfile("Dockerfile.gh-build", dockerfile)
def make_development_dockerfiles():
make_development_docs_dockerfile()
make_development_python_dockerfile()
make_development_frontend_dockerfiles()
def make_development_docs_dockerfile():
write_dockerfile("./development-env/Dockerfile.docs", f"{WARNING}\n\n{BUILD_DOCS}")
def make_development_python_dockerfile():
write_dockerfile("./development-env/Dockerfile.python", f"{WARNING}\n\n{BUILD_VENV}")
def make_development_frontend_dockerfiles():
write_dockerfile("./development-env/Dockerfile.frontend", f"{WARNING}\n\n{BUILD_FRONTEND}")
if __name__ == "__main__":
main()