Package: brewblox-spark-api
Boilerplate code for Brewblox service implementationsThere is some boilerplate code involved when creating a Brewblox service.
This repository can be forked to avoid having to do the boring configuration.You're free to use whatever editor or IDE you like, but we preconfigured some useful settings forVisual Studio Code.Everything listed underRequired Changesmust be done before the package works as intended.How to useInstall required dependencies (see below)Fork this repository to your own Github account or project.Follow all steps outlined under the variousRequired Changes.Start coding your service =)To test, runpoetry run pytestInstallInstallPyenv:sudo apt-get update -y && sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git python3-venv

curl https://pyenv.run | bashAfter installing, it may suggest to add initialization code to ~/.bashrc. Do that.To apply the changes to ~/.bashrc (or ~/.zshrc), run:exec $SHELL --loginInstall Python 3.7:pyenv install 3.7.7InstallPoetrycurl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

exec $SHELL --loginConfigure and install the environment used for this project.Run in the root of your cloned projectpoetry run pip install --upgrade pip
poetry installDuring development, you need to have your environment activated.
When it is activated, your terminal prompt is prefixed with(.venv).Visual Studio code with suggested settings does this automatically whenever you open a .py file.
If you prefer using a different editor, you can do it manually by running:poetry shellInstallDockercurl -sL get.docker.com | sh

sudo usermod -aG docker $USER

rebootFilespyproject.tomlThepyprojectfile contains all kinds of Python settings.
For those more familiar with Python packaging: it replaces the following files:setup.pyMANIFEST.inrequirements.txtRequired Changes:Change thenamefield to your project name. This is generally the same as the repository name. This name is used when installing the package through Pip.It is common for this name to equal the package name, but using "-" as separator instead of "_".Change theauthorsfield to your name and email.tox.iniDeveloper tools such asPytest,Flake8, andAutopep8use this file to find configuration options.Required Changes:Change--cov=YOUR_PACKAGEto refer to your module name.The--cov-fail-under=100makes the build fail if code coverage is less than 100%. It is optional, but recommended. Remove the#comment character to enable it..envProject-specific environment variables can be stored here. By default, the name of the Docker repository (more on this below) is set here.Required Changes:ChangeDOCKER_REPO=you/your-packageto match the name of your docker image..editorconfigThis file containsEditorConfigconfiguration for this project.Among other things, it describes per file type whether it uses tabs or spaces.For a basic service, you do not need to change anything in this file.
However, it is recommended to use an editor that recognizes and uses.editorconfigfiles.README.mdYour module readme (this file). It will automatically be displayed in Github.Required Changes:Add all important info about your package here. What does your package do? How do you use it? What is your favorite color?YOUR_PACKAGE/__main__.py,subscribe_example.py,http_example.py,publish_example.pyYour module. The directory name is used when importing your code in Python.You can find examples for common service actions here.Required Changes:Rename to the desired module name. This name can't include "-" characters.It is common for single-module projects to use "-" as a separator for the project name, and "_" for the module.For example:your-packageandyour_package.Change the import statements in .py files fromYOUR_PACKAGEto your package name.test/conftest.pyShared pytest fixtures for all your tests are defined here.
The other test files provide examples on how to use the fixtures.Required Changes:Change the import fromYOUR_PACKAGEto your package name.test/test_http_example.py/test/test_publish_example.py/test/test_subscribe_example.pyThe test code shows how to test the functionality added by the various examples.
This includes multiple tricks for testing async code with pytest.
You can remove the files if you no longer need them.Required Changes:Change the import fromYOUR_PACKAGEto your package name.docker/before_build.shDocker builds can only access files in the same directory as theDockerfile.Thebefore_build.shcopies the dependencies for the Docker build into the docker/ directory.docker/DockerfileA docker file for running your package. To build the image for both desktop computers (AMD64), Raspberry Pi (ARM32), and Raspberry Pi 64-bit (ARM64):Prepare the builder (run once per shell):# Buildx is an experimental featureexportDOCKER_CLI_EXPERIMENTAL=enabled# Enable the QEMU emulator, required for building ARM images on an AMD computerdockerrun--rm--privilegedmultiarch/qemu-user-static--reset-pyes# Remove previous builderdockerbuildxrmbricklayer||true# Create and use a new builderdockerbuildxcreate--use--namebricklayer# Bootstrap the newly created builderdockerbuildxinspect--bootstrapBuild:REPO=you/your-packageTAG=local# Will build your Python package, and copy the results to the docker/ directorybashdocker/before_build.sh# Set image name# Build the image for multiple architectures# - AMD64 -> linux/amd64# - ARM32 -> linux/arm/v7# - ARM64 -> linux/arm64/v8# Push the image to the docker registrydockerbuildxbuild\--tag$REPO:$TAG\--platformlinux/amd64,linux/arm/v7,linux/arm64/v8\--push\dockerWhile you are in the same shell, you don't need to repeat the build preparation.If you only want to use the image locally, run the build commands like this:REPO=you/your-packageTAG=local# Will build your Python package, and copy the results to the docker/ directorybashdocker/before_build.sh# Set image name# Load image for local use# This only builds for the current architecture (AMD64)dockerbuildxbuild\--tag$REPO:$TAG\--load\dockerRequired Changes:Rename instances ofYOUR-PACKAGEandYOUR_PACKAGEin the docker file to desired project and package names.azure-pipelines.ymlAzurecan automatically test and deploy all commits you push to GitHub.
If you haven't enabled Azure Pipelines for your repository: don't worry, it won't do anything.To deploy your software, you will also need to create aDocker Hubaccount,
and register your image as a new repository.DeploymentOther Brewblox services are published and used as Docker images.
Setting this up is free and easy, and this repository includes the required configuration.Docker HubFirst, we'll need a Docker Hub account and repository to store created images.
Go tohttps://hub.docker.com/and create an account.After this is done: log in, click on the fingerprint icon, and go to "Account Settings" -> "Security".
Generate an access token. We'll be using this to log in during CI builds.Now, go back to the main page by clicking on the Docker Hub logo, and clickcreate repository.
Pick a name, and clickcreate. You don't need to connect the repository.You can now push images touser/repository.Don't forget to set the DOCKER_REPO field in the .env file.Azure PipelinesTo automatically build and push those images, you'll need a Continuous Integration (CI) server.
Here we'll set up Azure Pipelines as CI service, but you can do the same thing usingTravis,CircleCI,GitHub Actions,GitLabor any of the others.Go tohttps://azure.microsoft.com/en-us/services/devops/and click "Start free with GitHub".
You can then connect your GitHub account to Azure.After logging in, create a new project. The name does not matter.In the side bar, go to Pipelines, click on Library, and create a new variable group.
Call this groupbrewblox.Add two variables:DOCKER_USERis your Docker Hub user name.DOCKER_PASSWORDis the access token you generated earlier. Make the value secret by clicking the lock icon.Save to confirm the group. These variables are now used during CI builds.Again in the side bar, go to Pipelines, and create a new Pipeline. Choose GitHub as source, and select your repository.Azure will automatically detect theazure-pipelines.ymlfile. Click "Run" to initialize it.
It will ask you for permission to link Azure to your GitHub repository.When this is done, it will start its first build. You can view the build results onhttps://dev.azure.com/That's it. Happy coding!