Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 1 | load("@pydeps//:requirements.bzl", "requirement") |
| 2 | load("@rules_python//python:defs.bzl", "py_binary") |
| 3 | load("@io_bazel_rules_docker//python:image.bzl", "py_layer") |
| 4 | load("@io_bazel_rules_docker//python3:image.bzl", "py3_image") |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 5 | load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer") |
| 6 | load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit_layer", "container_run_and_extract") |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 7 | load("@io_bazel_rules_docker//docker/package_managers:download_pkgs.bzl", "download_pkgs") |
| 8 | load("@io_bazel_rules_docker//docker/package_managers:install_pkgs.bzl", "install_pkgs") |
| 9 | |
| 10 | # - - base docker stuff - - |
| 11 | |
| 12 | download_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 | |
| 21 | install_pkgs( |
| 22 | name = "base_image", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 23 | 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 Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 26 | output_image_name = "base_image", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | BASE_IMAGE = ":base_image" |
| 30 | |
| 31 | # overkill rube goldberg setup to build static files begins |
| 32 | # - - - - |
| 33 | |
| 34 | container_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 | |
| 43 | container_image( |
| 44 | name = "static_build_image", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 45 | base = BASE_IMAGE, |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 46 | layers = [":static_build_layer"], |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 47 | ) |
| 48 | |
| 49 | # this will also contain .pyc files, but the python binary will be the same |
| 50 | # on prod, so it's fine |
| 51 | container_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 Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 59 | docker_run_flags = ["--entrypoint="], |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 60 | image = ":build_container.tar", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 61 | ) |
| 62 | |
| 63 | py3_image( |
| 64 | name = "build_container", |
| 65 | srcs = [":manage"], |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 66 | base = ":build_tools_container", |
| 67 | layers = [":deps_layer"], |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 68 | main = "manage.py", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 69 | # this doesn't work for some reason - this is always rebuilt, unless |
| 70 | # you pass --nostamp globally |
| 71 | stamp = 0, |
| 72 | ) |
| 73 | |
| 74 | download_pkgs( |
| 75 | name = "build_tools", |
| 76 | image_tar = "@python-debian//image", |
| 77 | packages = [ |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 78 | "gettext", |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 79 | "sassc", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 80 | ], |
| 81 | ) |
| 82 | |
| 83 | install_pkgs( |
| 84 | name = "build_tools_container", |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 85 | image_tar = BASE_IMAGE + ".tar", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 86 | installables_tar = ":build_tools.tar", |
| 87 | installation_cleanup_commands = "rm -rf /var/lib/apt/lists/* /usr/share/doc", |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 88 | output_image_name = "build_tools_container", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 89 | ) |
| 90 | |
| 91 | # - - - - |
| 92 | # overkill rube goldberg setup to build static files ends |
| 93 | |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 94 | # - - python stuff - - |
| 95 | |
| 96 | # this is purely a build optimization - put the pip deps into a separate layer |
| 97 | py_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 | |
| 108 | py_library( |
| 109 | name = "django_base", |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 110 | srcs = [ |
| 111 | "settings.py", |
| 112 | "urls.py", |
| 113 | ] + glob([ |
| 114 | "upstream_settings/*.py", |
| 115 | ]), |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 116 | deps = [ |
| 117 | requirement("Django"), |
| 118 | requirement("postorius"), |
| 119 | requirement("hyperkitty"), |
| 120 | requirement("gunicorn"), |
| 121 | requirement("psycopg2-binary"), |
| 122 | ], |
| 123 | ) |
| 124 | |
| 125 | py_binary( |
| 126 | name = "manage", |
| 127 | srcs = ["manage.py"], |
| 128 | deps = [":django_base"], |
| 129 | ) |
| 130 | |
| 131 | py_binary( |
| 132 | name = "serve", |
| 133 | srcs = ["serve.py"], |
| 134 | deps = [":django_base"], |
| 135 | ) |
| 136 | |
| 137 | # prod docker image |
| 138 | |
| 139 | py3_image( |
| 140 | name = "mailman-web", |
| 141 | srcs = ["container_main.py"], |
Serge Bazanski | dacb778 | 2024-01-15 12:53:30 +0000 | [diff] [blame] | 142 | #base = ":base_container" |
| 143 | base = ":static_build_image", |
| 144 | layers = [ |
| 145 | ":deps_layer", |
| 146 | ], |
| 147 | main = "container_main.py", |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 148 | deps = [ |
| 149 | ":django_base", |
| 150 | ":manage", |
| 151 | ":serve", |
| 152 | ], |
Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 153 | ) |