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") |
| 5 | load("@io_bazel_rules_docker//container:container.bzl", "container_layer", "container_image") |
| 6 | load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_extract", "container_run_and_commit_layer") |
| 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", |
| 23 | output_image_name = "base_image", |
| 24 | image_tar = "@python-debian//image", |
| 25 | installables_tar = ":apt_py_is_py3.tar", |
| 26 | installation_cleanup_commands = "rm -rf /var/lib/apt/lists/* /usr/share/doc && apt remove -y libbluetooth3 mariadb-common tk && apt autoremove -y", |
| 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", |
| 45 | layers = [":static_build_layer"], |
| 46 | base = BASE_IMAGE, |
| 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 | ], |
| 59 | image = ":build_container.tar", |
| 60 | docker_run_flags = ["--entrypoint="], |
| 61 | ) |
| 62 | |
| 63 | py3_image( |
| 64 | name = "build_container", |
| 65 | srcs = [":manage"], |
| 66 | main = "manage.py", |
| 67 | base = ":build_tools_container", |
| 68 | layers = [":deps_layer"], |
| 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 = [ |
| 78 | "sassc", |
| 79 | "gettext", |
| 80 | ], |
| 81 | ) |
| 82 | |
| 83 | install_pkgs( |
| 84 | name = "build_tools_container", |
| 85 | output_image_name = "build_tools_container", |
| 86 | image_tar = BASE_IMAGE + '.tar', |
| 87 | installables_tar = ":build_tools.tar", |
| 88 | installation_cleanup_commands = "rm -rf /var/lib/apt/lists/* /usr/share/doc", |
| 89 | ) |
| 90 | |
| 91 | # - - - - |
| 92 | # overkill rube goldberg setup to build static files ends |
| 93 | |
| 94 | |
| 95 | # - - python stuff - - |
| 96 | |
| 97 | # this is purely a build optimization - put the pip deps into a separate layer |
| 98 | py_layer( |
| 99 | name = "deps_layer", |
| 100 | deps = [ |
| 101 | requirement("Django"), |
| 102 | requirement("postorius"), |
| 103 | requirement("hyperkitty"), |
| 104 | requirement("gunicorn"), |
| 105 | requirement("psycopg2-binary"), |
| 106 | ], |
| 107 | ) |
| 108 | |
| 109 | py_library( |
| 110 | name = "django_base", |
| 111 | srcs = ["settings.py", "urls.py"] |
| 112 | + glob(["upstream_settings/*.py"]), |
| 113 | deps = [ |
| 114 | requirement("Django"), |
| 115 | requirement("postorius"), |
| 116 | requirement("hyperkitty"), |
| 117 | requirement("gunicorn"), |
| 118 | requirement("psycopg2-binary"), |
| 119 | ], |
| 120 | ) |
| 121 | |
| 122 | py_binary( |
| 123 | name = "manage", |
| 124 | srcs = ["manage.py"], |
| 125 | deps = [":django_base"], |
| 126 | ) |
| 127 | |
| 128 | py_binary( |
| 129 | name = "serve", |
| 130 | srcs = ["serve.py"], |
| 131 | deps = [":django_base"], |
| 132 | ) |
| 133 | |
| 134 | # prod docker image |
| 135 | |
| 136 | py3_image( |
| 137 | name = "mailman-web", |
| 138 | srcs = ["container_main.py"], |
| 139 | deps = [ |
| 140 | ":django_base", |
| 141 | ":manage", |
| 142 | ":serve", |
| 143 | ], |
| 144 | layers = [ |
| 145 | ":deps_layer", |
| 146 | ], |
| 147 | main = "container_main.py", |
| 148 | #base = ":base_container" |
| 149 | base = ":static_build_image", |
| 150 | ) |