blob: 451ff39c50bcfb09c47b487184a0495d525d2534 [file] [log] [blame]
Bartosz Stebelf5b1a212023-02-04 23:47:44 +01001load("@pydeps//:requirements.bzl", "requirement")
2load("@rules_python//python:defs.bzl", "py_binary")
3load("@io_bazel_rules_docker//python:image.bzl", "py_layer")
4load("@io_bazel_rules_docker//python3:image.bzl", "py3_image")
Serge Bazanskidacb7782024-01-15 12:53:30 +00005load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
6load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit_layer", "container_run_and_extract")
Bartosz Stebelf5b1a212023-02-04 23:47:44 +01007load("@io_bazel_rules_docker//docker/package_managers:download_pkgs.bzl", "download_pkgs")
8load("@io_bazel_rules_docker//docker/package_managers:install_pkgs.bzl", "install_pkgs")
9
10# - - base docker stuff - -
11
12download_pkgs(
13 name = "apt_py_is_py3",
14 image_tar = "@python-debian//image",
15 packages = [
16 # rules_docker python wants /usr/bin/python
17 "python-is-python3",
18 ],
19)
20
21install_pkgs(
22 name = "base_image",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010023 image_tar = "@python-debian//image",
24 installables_tar = ":apt_py_is_py3.tar",
25 installation_cleanup_commands = "rm -rf /var/lib/apt/lists/* /usr/share/doc && apt remove -y libbluetooth3 mariadb-common tk && apt autoremove -y",
Serge Bazanskidacb7782024-01-15 12:53:30 +000026 output_image_name = "base_image",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010027)
28
29BASE_IMAGE = ":base_image"
30
31# overkill rube goldberg setup to build static files begins
32# - - - -
33
34container_run_and_extract(
35 name = "static_pack",
36 commands = [
37 "tar cpJvf /out.tar.xz -C /opt/mailman/web/static ./",
38 ],
39 extract_file = "/out.tar.xz",
40 image = ":static_build_image.tar",
41)
42
43container_image(
44 name = "static_build_image",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010045 base = BASE_IMAGE,
Serge Bazanskidacb7782024-01-15 12:53:30 +000046 layers = [":static_build_layer"],
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010047)
48
49# this will also contain .pyc files, but the python binary will be the same
50# on prod, so it's fine
51container_run_and_commit_layer(
52 name = "static_build_layer",
53 commands = [
54 "./app/mailman-web/manage collectstatic",
55 "./app/mailman-web/manage compress",
56 # gettext is cursed, TODO make this work
57 #"./app/mailman-web/manage compilemessages",
58 ],
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010059 docker_run_flags = ["--entrypoint="],
Serge Bazanskidacb7782024-01-15 12:53:30 +000060 image = ":build_container.tar",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010061)
62
63py3_image(
64 name = "build_container",
65 srcs = [":manage"],
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010066 base = ":build_tools_container",
67 layers = [":deps_layer"],
Serge Bazanskidacb7782024-01-15 12:53:30 +000068 main = "manage.py",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010069 # this doesn't work for some reason - this is always rebuilt, unless
70 # you pass --nostamp globally
71 stamp = 0,
72)
73
74download_pkgs(
75 name = "build_tools",
76 image_tar = "@python-debian//image",
77 packages = [
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010078 "gettext",
Serge Bazanskidacb7782024-01-15 12:53:30 +000079 "sassc",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010080 ],
81)
82
83install_pkgs(
84 name = "build_tools_container",
Serge Bazanskidacb7782024-01-15 12:53:30 +000085 image_tar = BASE_IMAGE + ".tar",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010086 installables_tar = ":build_tools.tar",
87 installation_cleanup_commands = "rm -rf /var/lib/apt/lists/* /usr/share/doc",
Serge Bazanskidacb7782024-01-15 12:53:30 +000088 output_image_name = "build_tools_container",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010089)
90
91# - - - -
92# overkill rube goldberg setup to build static files ends
93
Bartosz Stebelf5b1a212023-02-04 23:47:44 +010094# - - python stuff - -
95
96# this is purely a build optimization - put the pip deps into a separate layer
97py_layer(
98 name = "deps_layer",
99 deps = [
100 requirement("Django"),
101 requirement("postorius"),
102 requirement("hyperkitty"),
103 requirement("gunicorn"),
104 requirement("psycopg2-binary"),
105 ],
106)
107
108py_library(
109 name = "django_base",
Serge Bazanskidacb7782024-01-15 12:53:30 +0000110 srcs = [
111 "settings.py",
112 "urls.py",
113 ] + glob([
114 "upstream_settings/*.py",
115 ]),
Bartosz Stebelf5b1a212023-02-04 23:47:44 +0100116 deps = [
117 requirement("Django"),
118 requirement("postorius"),
119 requirement("hyperkitty"),
120 requirement("gunicorn"),
121 requirement("psycopg2-binary"),
122 ],
123)
124
125py_binary(
126 name = "manage",
127 srcs = ["manage.py"],
128 deps = [":django_base"],
129)
130
131py_binary(
132 name = "serve",
133 srcs = ["serve.py"],
134 deps = [":django_base"],
135)
136
137# prod docker image
138
139py3_image(
140 name = "mailman-web",
141 srcs = ["container_main.py"],
Serge Bazanskidacb7782024-01-15 12:53:30 +0000142 #base = ":base_container"
143 base = ":static_build_image",
144 layers = [
145 ":deps_layer",
146 ],
147 main = "container_main.py",
Bartosz Stebelf5b1a212023-02-04 23:47:44 +0100148 deps = [
149 ":django_base",
150 ":manage",
151 ":serve",
152 ],
Bartosz Stebelf5b1a212023-02-04 23:47:44 +0100153)