Giter Club home page Giter Club logo

brewblox-boilerplate's Introduction

Boilerplate code for Brewblox service implementations

There 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 for Visual Studio Code.

Everything listed under Required Changes must be done before the package works as intended.

How to use

  • Install required dependencies (see below)
  • Fork this repository to your own Github account or project.
  • Follow all steps outlined under the various Required Changes.
  • Start coding your service =)
    • To test, run poetry run pytest

Install

Install Pyenv:

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 | bash

Bash on Debian / Ubuntu: to initialize pyenv on startup, press Ctrl-X, Ctrl-E in your terminal to open the batch command editor. Paste the code block below, and press Ctrl-X to save and run.

sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

If you use a different shell, see the relevant instructions at https://github.com/pyenv/pyenv.

To apply the changes, run:

exec $SHELL --login

Install Python 3.9:

pyenv install 3.9.9

Install Poetry

curl -sSL https://install.python-poetry.org | python3 -

exec $SHELL --login

Configure and install the environment used for this project.

Run in the root of your cloned project

pyenv shell 3.9.9
poetry env use 3.9.9
poetry install

During 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 shell

Install Docker

curl -sL get.docker.com | sh

sudo usermod -aG docker $USER

reboot

Files


The pyproject file contains all kinds of Python settings. For those more familiar with Python packaging: it replaces the following files:

  • setup.py
  • MANIFEST.in
  • requirements.txt

Required Changes:

  • Change the name field 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 the authors field to your name and email.

Developer tools such as Pytest, Flake8, and Autopep8 use this file to find configuration options.

Required Changes:

  • Change --cov=YOUR_PACKAGE to refer to your module name.
  • The --cov-fail-under=100 makes the build fail if code coverage is less than 100%. It is optional, but recommended. Remove the # comment character to enable it.

Project-specific environment variables can be stored here. By default, the name of the Docker repository (more on this below) is set here.

Required Changes:

  • Change DOCKER_REPO=you/your-package to match the name of your docker image.

This file contains EditorConfig configuration 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 .editorconfig files.


Your 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?

__main__.py, subscribe_example.py, http_example.py, publish_example.py

Your 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-package and your_package.
  • Change the import statements in .py files from YOUR_PACKAGE to your package name.

Shared 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 from YOUR_PACKAGE to your package name.

The 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 from YOUR_PACKAGE to your package name.

Docker builds can only access files in the same directory as the Dockerfile.

The before_build.sh copies the dependencies for the Docker build into the docker/ directory.


A 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):

# Enable the QEMU emulator, required for building ARM images on an AMD computer
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

# Remove previous builder
docker buildx rm bricklayer || true

# Create and use a new builder
docker buildx create --use --name bricklayer

# Bootstrap the newly created builder
docker buildx inspect --bootstrap

Build:

REPO=you/your-package
TAG=local

# Will build your Python package, and copy the results to the docker/ directory
bash docker/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 registry
docker buildx build \
    --tag $REPO:$TAG \
    --platform linux/amd64,linux/arm/v7,linux/arm64/v8 \
    --push \
    docker

While 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-package
TAG=local

# Will build your Python package, and copy the results to the docker/ directory
bash docker/before_build.sh

# Set image name
# Load image for local use
# This only builds for the current architecture (AMD64)
docker buildx build \
    --tag $REPO:$TAG \
    --load \
    docker

Required Changes:

  • Rename instances of YOUR-PACKAGE and YOUR_PACKAGE in the docker file to desired project and package names.

Azure can 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 a Docker Hub account, and register your image as a new repository.

Deployment

Other Brewblox services are published and used as Docker images. Setting this up is free and easy, and this repository includes the required configuration.

Docker Hub

First, we'll need a Docker Hub account and repository to store created images. Go to https://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 click create repository. Pick a name, and click create. You don't need to connect the repository.

You can now push images to user/repository.

Don't forget to set the DOCKER_REPO field in the .env file.

Azure Pipelines

To 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 using Travis, CircleCI, GitHub Actions, GitLab or any of the others.

Go to https://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 group brewblox.

Add two variables:

  • DOCKER_USER is your Docker Hub user name.
  • DOCKER_PASSWORD is 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 the azure-pipelines.yml file. 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 on https://dev.azure.com/

That's it. Happy coding!

brewblox-boilerplate's People

Contributors

steersbob avatar elcojacobs avatar nixon506e avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.