diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 241c883..013d5d0 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -66,3 +66,62 @@ container_deps() load("@io_bazel_rules_docker//python3:image.bzl", _py_image_repos = "repositories") _py_image_repos() + +load( + "@io_bazel_rules_docker//container:container.bzl", + "container_pull", +) + +container_pull( + name = "debian_base", + # docker manifest inspect index.docker.io/debian:bullseye + digest = "sha256:c11d2593cb741ae8a36d0de9cd240d13518e95f50bccfa8d00a668c006db181e", + registry = "index.docker.io", + repository = "library/debian", +) + +# http_archive( +# name = "com_github_google_rules_install", +# urls = ["https://github.com/google/bazel_rules_install/releases/download/0.3/bazel_rules_install-0.3.tar.gz"], +# sha256 = "ea2a9f94fed090859589ac851af3a1c6034c5f333804f044f8f094257c33bdb3", +# strip_prefix = "bazel_rules_install-0.3", +# ) + +# load("@com_github_google_rules_install//:deps.bzl", "install_rules_dependencies") + +# install_rules_dependencies() + +# load("@com_github_google_rules_install//:setup.bzl", "install_rules_setup") + +# install_rules_setup() + + +### Custom rule + +# def _local_deploy_impl(ctx): +# target = ctx.attr.target +# shell_commands = "" + +# for s in ctx.files.srcs: +# shell_commands += "echo Copying %s to %s\n" % (s.short_path, target) +# shell_commands += "sudo cp %s %s\n" % (s.short_path, target) + +# ctx.actions.write( +# output = ctx.outputs.executable, +# is_executable = True, +# content = shell_commands, +# ) +# runfiles = ctx.runfiles(files = ctx.files.srcs) +# return DefaultInfo( +# executable = ctx.outputs.executable, +# runfiles = runfiles, +# ) + +# local_deploy = rule( +# executable = True, +# implementation = _local_deploy_impl, +# attrs = { +# "srcs": attr.label_list(allow_files = True), +# "target": attr.string(default = "/usr/local/bin", doc = "Deployment target directory"), +# }, +# ) diff --git a/projects/python_web/BUILD.bazel b/projects/python_web/BUILD.bazel index 3ee6190..cb34b10 100644 --- a/projects/python_web/BUILD.bazel +++ b/projects/python_web/BUILD.bazel @@ -1,6 +1,13 @@ load("@pip//:requirements.bzl", "requirement") load("@io_bazel_rules_docker//python3:image.bzl", "py3_image") -# load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push") + + +# load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit") +# load("@io_bazel_rules_docker//container:container.bzl", "container_push") + +# load("@com_github_google_rules_install//installer:def.bzl", "installer") +# load("//rules:local-deploy.bzl", "local_deploy") +load("@io_bazel_rules_docker//container:container.bzl", "container_image") py_library( name = "python_web_lib", @@ -18,23 +25,125 @@ py_binary( deps = [":python_web_lib"], ) -py_test( - name = "python_web_test", - size = "small", +py_library( + name = "python_web_test_lib", srcs = ["python_web_test.py"], deps = [ + requirement('Flask'), "//projects/calculator:calculator", - "//projects/python_web:python_web_lib", ], ) +py_binary( + name = "python_web_test", + srcs = ["python_web_test.py"], + main = "python_web_test.py", + deps = [":python_web_lib"], +) + +container_image( + name = "python_web_docker", + base = "@debian_base//image", + directory = "/app", + # entrypoint = + # "chmod +x /model_server.sh; \ + # bin/bash /model_server.sh ", + files = [ + ":python_web_image", + ":python_web_image_integ" + ], +) + +# py_test( +# name = "python_web_test", +# size = "small", +# srcs = ["python_web_test.py"], +# deps = [ +# "//projects/calculator:calculator", +# "//projects/python_web:python_web_lib", +# ], +# ) + +# TODO: add another image rule along with test inside + + py3_image( name = "python_web_image", - srcs = ["python_web.py"], + srcs = [ + "python_web.py", + ], main = "python_web.py", deps = [":python_web_lib"], ) +container_image( + name = "python_web_common", + testonly = True, + base = ":python_web_image", + files = [":python_web_test"], +) + +py3_image( + name = "python_web_image_integ", + srcs = [ + "python_web_test.py", + ], + main = "python_web_test.py", + deps = [":python_web_lib"], +) + + +# The following defines our base builder image for the dataform repo. +# container_run_and_commit( +# name = "builder", +# commands = [ +# # Install apt packages. +# "apt update", +# "apt install -yq build-essential cmake git ca-certificates apt-transport-https gnupg2 lsb-release wget curl python2 python3.9 default-jre default-jdk", +# # Update certificates. +# "update-ca-certificates", +# # Install docker. +# "curl -fsSL https://get.docker.com -o get-docker.sh", +# "chmod +x get-docker.sh", +# "./get-docker.sh", +# # Install bazelisk. +# "wget https://github.com/bazelbuild/bazelisk/releases/download/v1.2.1/bazelisk-linux-amd64 -O /usr/bin/bazel", +# "chmod +x /usr/bin/bazel", +# # Set python versions. +# "ln -sf /usr/bin/python2.7 /usr/bin/python", +# "ln -sf /usr/bin/python3.9 /usr/bin/python3" +# ], +# image = "@debian_base//image", +# tags = [ +# "no-remote", +# ], +# ) + +# container_push( +# name = "builder.push", +# format = "Docker", +# image = ":builder_commit.tar", +# # registry = "gcr.io", +# # repository = "dataform-public/dataform-builder", +# # tag = "latest", +# ) + + +############################################ + +# installer( +# name = "copy_python_web", +# data = [":python_web"], +# ) + + +# local_deploy( +# name = "copy_python_web", +# srcs = [":python_web_image"], +# target = "//project/python_web/", +# ) + + # container_image( # name = "python_web_container", # files = [ diff --git a/projects/python_web/python_web.garden.yaml b/projects/python_web/python_web.garden.yaml index 7afae26..f090b9a 100644 --- a/projects/python_web/python_web.garden.yaml +++ b/projects/python_web/python_web.garden.yaml @@ -118,6 +118,13 @@ tests: args: - echo - ok + - name: python-web-integ-2 + dependencies: + - re-tag-integ-image + command: + - /usr/bin/python + args: + - /app/projects/python_web/python_web_image_integ.binary ## devMode doesn't work as expected # ⚠ python-web → Error scanning sync target, path python_web_image.binary: invalid symbolic link: target is absolute @@ -128,3 +135,82 @@ tests: # sync: # - source: ../../bazel-bin/projects/python_web/python_web.runfiles # target: /app/projects/python_web/ + +--- +kind: Module +type: exec +local: true +name: python-web-image-integ +description: Build docker image +include: + - ./* +build: + command: ["bazel", "run", "//projects/python_web:python_web_image_integ", "--", "--norun"] + +--- +kind: Module +type: exec +local: true +name: python-web-integ-tag-image +include: [] +description: Tag docker image +build: + dependencies: + - name: python-web-image-integ +tasks: + - name: re-tag-integ-image + command: ["docker", "tag", "bazel/projects/python_web:python_web_image_integ", "python_web_integ:${modules.python-web-image-integ.version}"] +--- +kind: Module +name: python-web-integ +type: kubernetes +include: [] +build: + dependencies: + - python-web-image-integ +dependencies: + - re-tag-integ-image +serviceResource: + kind: Deployment + name: python-web-integ +manifests: +# Deployment + - apiVersion: apps/v1 + kind: Deployment + metadata: + name: python-web-integ + labels: + app: python-web-integ + spec: + replicas: 1 + selector: + matchLabels: + app: python-web-integ + template: + metadata: + labels: + app: python-web-integ + spec: + containers: + - name: python-web-integ + image: "python_web_integ:${modules.python-web-image-integ.version}" + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 5000 + protocol: TCP + resources: + requests: + cpu: 250m + memory: 512Mi + limits: + cpu: 500m + memory: 1G +tests: + - name: python-web-integ + dependencies: + - re-tag-integ-image + command: + - /usr/bin/python + args: + - /app/projects/python_web/python_web_image_integ.binary diff --git a/projects/python_web/python_web_test.py b/projects/python_web/python_web_test.py index 41d483e..f9ff724 100644 --- a/projects/python_web/python_web_test.py +++ b/projects/python_web/python_web_test.py @@ -11,9 +11,7 @@ def setUp(self): self.calculator = Calculator() def test_hello(self): - print("testing") response = self.client.get('/') - print(response) data = response.get_json() num1 = data['num1'] num2 = data['num2']