This article is the third day of OpenSaaS Studio Advent Calendar 2019.
The former is a mono-repo configuration using Bazel in Golang and Python microservices. I wondered if Python could be mixed with the mono repo if I used Bazel, which supports multiple languages, but it was difficult, so I will leave a difficult point.
docker build
and pushes that imageI used only Python3 series in the service, but I needed Python2 series to push the created image. Since I used Container Registry as the image registry of the container, Bazel needs Google Cloud SDK to push the image and it is linked to it. I think that Python2 system was needed in the form of a sequel. Description of Google Cloud SDK
Recent versions of macOS include the appropriate version of Python required for the Google Cloud SDK. The Cloud SDK requires Python 2 with a release number of Python 2.7.9 or later. If you install an additional Python interpreter, it must not interfere with the installation of the Google Cloud SDK.
Bazel has a mechanism that allows you to specify the Python interpreter to use when building, so I used that.
The interpreter definition and build definition look like this.
load("@rules_python//python:defs.bzl", "py_runtime_pair")
py_runtime(
name = "py2_local_bin_runtime",
interpreter_path = "/usr/local/bin/python2",
python_version = "PY2",
)
py_runtime(
name = "py3_6_local_bin_runtime",
interpreter_path = "/usr/local/bin/python3.6",
python_version = "PY3",
)
py_runtime_pair(
name = "py_local_bin_runtime_pair",
py2_runtime = ":py2_local_bin_runtime",
py3_runtime = ":py3_6_local_bin_runtime",
)
toolchain(
name = "py_mac_toolchain",
exec_compatible_with = [
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
],
toolchain = "py_local_bin_runtime_pair",
toolchain_type = "@rules_python//python:toolchain_type",
)
register_toolchains(
"//:py_mac_toolchain",
"//:py_linux_toolchain",
)
py_binary(
name = "mysite",
srcs = ["manage.py"],
deps = [
"//mysite/mysite:py_default_library",
"//mysite/polls:py_default_library",
],
main = "manage.py",
python_version = "PY3",
)
When using Bazel rules for Python, it seems that the pip
associated with the python
command is executed when downloading the library. As I wrote above, the default python
was 2 series, so I could download the Python 3 series library.
The code is around here
Changed to python
-> python3.6
so that you can use 3 series by forking ..
However, when I checked the latest version, it was Pip3 compatible by default, so there may be no problem now.
Bazel has a default rule (py3_image
) for creating a Docker image of Python3 series, and when I built it in combination with python: 3.6-slim, the created image could not be started.
The build definition looks like this
container_pull(
name = "py3_base",
registry = "index.docker.io",
repository = "library/python",
tag = "3.6-slim",
)
py3_image(
name = "mysite_image",
srcs = ["manage.py"],
deps = [
"//mysite/mysite:py_default_library",
"//mysite/polls:py_default_library",
],
main = "manage.py",
base = "@py3_base//image",
)
Actually, the entry point of the image created by py3_image
is / usr / bin / python
by default, but in python: 3.6-slim, the python command is not set in that path and it can not be started. did.
The code is around here
I used this image because it started normally with python: 3.6
. (Image optimization is assumed to be performed later if necessary)
I had three Python services and the versions of Python I was using were all different from 3.5, 3.6 and 3.7. The method of using the Python interpreter described above can only separate the 2nd and 3rd systems, so this mechanism cannot handle it.
I decided to move all Python versions used in the service to 3.6. It took a long time to verify so far, and I thought that complicating the configuration around the build would deviate from the original policy, so I decided to reduce the number of things to manage. If you want to coexist with multiple versions of Python3, you may be able to separate WORKSPACE and specify the required Python interpreter for each. (unconfirmed)
├── service_for_python3.6
│ └── WORKSPACE <-Python3 here.Set up 6 interpreters
├── service_for_python3.7
│ └── WORKSPACE <-Python3 here.Set up 7 interpreters
└── WORKSPACE
__init__.py
is broken(Probably) The same event as this issue occurred in the google-cloud-XXX series library.
It seems that the problem was caused by the different directory structure when installing the library with pip install
and when building with Bazel.
The issue also has a comment, but it can now be executed by specifying the attribute legacy_create_init
.
I'm using tensorflow-gpu for some services and couldn't download the library because the build environment didn't have a GPU.
It is unsolved. It's been a while since I started the verification, so I rounded it up here. Bazel may be able to handle it by specifying a platform associated with the GPU (if it exists) or by building this service on a build farm that has a GPU.
Recommended Posts