Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/local_arg_printer/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.2.1
6 changes: 6 additions & 0 deletions examples/local_arg_printer/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module(
name = "xmanager_local_arg_printer_example",
version = "1.0",
)

bazel_dep(name = "rules_cc", version = "0.1.2")
16 changes: 16 additions & 0 deletions examples/local_arg_printer/arg_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// A simple C++ binary that prints its command-line arguments to a file.
//
// The output file path is read from the `OUTPUT_PATH` environment variable.
// The first line of the output file will contain the argument count, followed
// by each argument on a new line. The program then sleeps for 5 seconds to
// simulate a long-running job before exiting.
// Usage:
// ```
// $ OUTPUT_PATH="/tmp/output.txt" bazel run //:arg_printer -- "arg1" "arg2"
// $ cat /tmp/output.txt
// 3
// /home/user/.cache/bazel/_bazel_user/b2b20ce9d34fd5389bf343c4cc37f48f/execroot/_main/bazel-out/k8-fastbuild/bin/arg_printer
// arg1
// arg2
// ```

#include <chrono>
#include <fstream>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion examples/local_arg_printer/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""XManager launcher that runs locally a binary built with Bazel.

One must `cd` into xmanager/examples/local_arg_printer/ in order to run this
example because Bazel needs to locate the WORKSPACE file.
example because Bazel needs to locate the `MODULE.bazel` file.
"""

from typing import Sequence
Expand Down
1 change: 1 addition & 0 deletions examples/local_container_links/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.2.1
53 changes: 28 additions & 25 deletions examples/local_container_links/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
# Copyright 2021 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("@python_deps//:requirements.bzl", "requirement")
load("@subpar//:subpar.bzl", "par_binary")
load("@aspect_rules_py//py:defs.bzl", "py_image_layer")
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

licenses(["notice"])

par_binary(
py_binary(
name = "server",
srcs = ["server.py"],
deps = [
requirement("absl-py"),
requirement("bottle"),
requirement("bottle-redis"),
"@pypi//absl_py",
"@pypi//flask",
"@pypi//redis",
],
)

container_image(
oci_image(
name = "server_image",
base = "@io_docker_index_library_python//image",
entrypoint = ["/server.par"],
files = [":server.par"],
base = "@docker_io_python",
entrypoint = ["/app/server"],
tars = py_image_layer(
name = "server_layer",
binary = ":server",
root = "/app/",
),
)

oci_load(
name = "server_image.load",
image = ":server_image",
repo_tags = ["local_container_links_server:latest"],
)

filegroup(
name = "server_image.tar",
srcs = [":server_image.load"],
output_group = "tarball",
)
35 changes: 35 additions & 0 deletions examples/local_container_links/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module(
name = "xmanager_local_container_links_bzlmod",
version = "1.0.0",
)

bazel_dep(name = "aspect_rules_py", version = "1.6.0")
bazel_dep(name = "rules_python", version = "1.5.0-rc2")
bazel_dep(name = "rules_oci", version = "2.2.6")
bazel_dep(name = "rules_pkg", version = "1.1.0")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")

python.toolchain(python_version = "3.13")

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pypi",
python_version = "3.13",
requirements_lock = "requirements.txt",
)
use_repo(pip, "pypi")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "docker_io_python",
digest = "sha256:034724ef64585eeb0e82385e9aabcbeabfe5f7cae2c2dcedb1da95114372b6d7",
image = "docker.io/library/python",
platforms = [
"linux/amd64",
"linux/arm64/v8",
],
tag = "3.13-slim",
)
use_repo(oci, "docker_io_python", "docker_io_python_linux_amd64", "docker_io_python_linux_arm64_v8")
2 changes: 1 addition & 1 deletion examples/local_container_links/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Description

launcher.py packages and executes (locally) two Docker containers:
`launcher.py` packages and executes (locally) two Docker containers:
[Redis](https://hub.docker.com/_/redis), a key-value database, and a
[Bottle](https://bottlepy.org/)-based HTTP server that listens on port 8080 and
responds to requests at `/increment` by running
Expand Down
60 changes: 0 additions & 60 deletions examples/local_container_links/WORKSPACE

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

absl-py
flask
redis
40 changes: 23 additions & 17 deletions examples/local_container_links/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Copyright 2021 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

absl-py
bottle
bottle-redis
# This file was autogenerated by uv via the following command:
# uv pip compile ./requirements.in -o ./requirements.txt
absl-py==2.3.0
# via -r ./requirements.in
blinker==1.9.0
# via flask
click==8.2.1
# via flask
flask==3.1.1
# via -r ./requirements.in
itsdangerous==2.2.0
# via flask
jinja2==3.1.6
# via flask
markupsafe==3.0.2
# via
# flask
# jinja2
# werkzeug
redis==6.2.0
# via -r ./requirements.in
werkzeug==3.1.3
# via flask
57 changes: 33 additions & 24 deletions examples/local_container_links/server.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
# Copyright 2021 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An HTTP server incrementing a value in Redis."""

import time
from typing import Sequence

from absl import app
from absl import flags
import bottle
import bottle.ext.redis
from flask import Flask
import redis

redis_host = flags.DEFINE_string('redis_host', None, "Redis' host.")
redis_host = flags.DEFINE_string("redis_host", None, "Address to Redis server.")

server = bottle.Bottle()
server = Flask(__name__)


@server.route('/increment')
def increment(rdb):
return str(rdb.incr('counter'))
@server.route("/increment")
def increment():
rdb = server.config["RDB"]
counter = rdb.incr("counter")
return f"{counter=}"


@server.route("/")
def index():
rdb = server.config["RDB"]
counter = int(rdb.get("counter"))
return f"{counter=}\nIncrement it by visiting `/increment`."


def main(argv: Sequence[str]) -> None:
del argv # Unused.
del argv

rdb = redis.Redis(host=redis_host.value, decode_responses=True)

while True:
print("Waiting for Redis to be available...")
try:
rdb.ping()
break
except redis.exceptions.ConnectionError:
time.sleep(1)

server.config["RDB"] = rdb
rdb.set("counter", 0)

server.install(bottle.ext.redis.RedisPlugin(host=redis_host.value))
bottle.run(server, host='0.0.0.0', port=8080, debug=True)
server.run(host="0.0.0.0", port=8080, debug=False)


if __name__ == '__main__':
if __name__ == "__main__":
app.run(main)