Initial Commit

main
Drew Bednar 11 months ago
commit be8ace4b7e

@ -0,0 +1,6 @@
[run]
source = learn_otel
omit = test*
[report]
show_missing = True

@ -0,0 +1,14 @@
./tests
./scripts
.ruff_cache
.coveragerc
.dockerignore
.git
.gitignore
.pre-commit-config.yaml
dev-requirements.in
dev-requirements.txt
.profile
Dockerfile
requirements.in
tasks.py

@ -0,0 +1,44 @@
kind: pipeline
type: docker
name: CI Test/Lint Pipeline
steps:
- name: Unit Tests and Linters
# Bullseye because drone runner host OS is using older libseccomp2 causing issues
# with thread allocation. See: https://github.com/docker-library/python/issues/835
image: python:3.11-bullseye
commands:
- bash -xc './scripts/run_linters.sh'
- bash -xc './scripts/run_unit_tests.sh'
group: test-lint
trigger:
event:
- pull_request
- push
# Secrets used to pull private images
image_pull_secrets:
- dockerconfigjson
---
kind: pipeline
type: docker
name: Build Production Image
steps:
- name: Build learn_otel Container Image
image: plugins/docker
settings:
username: automate
password:
from_secret: automate_password
dockerfile: Dockerfile
registry: registry.runcible.io
repo: registry.runcible.io/learn_otel
tags:
- ${DRONE_COMMIT_SHA}
when:
branch:
- master
event:
- push

62
.gitignore vendored

@ -0,0 +1,62 @@
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Pycharm
.idea

@ -0,0 +1,40 @@
default_stages: [commit, push]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
args: [--allow-multiple-documents]
- id: check-added-large-files
- id: debug-statements
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.5
hooks:
- id: shellcheck
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
args: ["learn_otel", "./tests/"]
types: [ python ]
- repo: local
hooks:
- id: isort
name: isort
entry: isort
language: system
types: [python]
require_serial: true
- id: ruff
name: ruff
entry: ruff
language: system
types: [python]
args: [
# '-rn', # Only display messages
# '-sn', # Don't display the score
# '--disable=C,R,W0511', # Disable C and R type messages, and TODO fixme warning
]
require_serial: true

@ -0,0 +1,55 @@
# syntax = docker/dockerfile:1.4
# Best practice: Choose a stable base image and tag.
FROM python:3.11-slim-bookworm
# Install security updates, and some useful packages.
#
# Best practices:
# * Make sure apt-get doesn't run in interactive mode.
# * Update system packages.
# * Pre-install some useful tools.
# * Minimize system package installation.
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get -y upgrade && \
apt-get install -y --no-install-recommends tini procps net-tools && \
apt-get -y clean && \
rm -rf /var/lib/apt/lists/*
# Install dependencies.
#
# Best practices:
# * `COPY` in files only when needed.
# * Reduce disk usage from `pip` installs.
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Create a new user to run as.
#
# Best practices: Don't run as root.
RUN useradd --create-home appuser
USER appuser
WORKDIR /home/appuser
# Copy in the code.
#
# Best practices: Avoid extra chowns.
COPY --chown=appuser . .
# Best practices: Prepare for C crashes.
ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=0
ARG COMMIT_SHA
LABEL io.runcible.repo-sha="${COMMIT_SHA}"
# Run the code when the image is run:
#
# Best practices:
# * Add an `init` process.
# * Make sure images shut down correctly (via ENTRYPOINT [] syntax).
# * '-g' option means killing the container kills all processes, not just the
# entrypoint shell.
ENTRYPOINT ["tini", "-g", "--", "./entrypoint.sh"]

@ -0,0 +1,10 @@
MIT License
Copyright (c) 2024, Drew Bednar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -0,0 +1,26 @@
# learn_otel
Learning OTEL
## First time setup
Create a virtual environment and activate it. Now from the root project directory run `./scripts/bootstrap`. This will install `pip-tools` and sync any dependencies for the first time.
## Dependency management
Dependencies are managed via [pip-tools].
### Adding a dependency
To add a dependency, edit `requirements.in` (or `dev-requirements.in`
for dev dependencies) and add your dependency then run `pip-compile
requirements.in`.
### Syncing dependencies
Run `pip-sync requirements.txt dev_requirements.txt`.
## Testing
Run the tests by invoking `py.test` in the project root. Make sure you
run any pending migrations beforehand.

@ -0,0 +1,12 @@
-c ./requirements.txt
black
build
invoke
isort<=5.12.0
pip-tools
pre-commit
pytest
pytest-cov
shellcheck-py==0.9.0.5
ruff

@ -0,0 +1,78 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile dev-requirements.in
#
black==24.2.0
# via -r dev-requirements.in
build==1.0.3
# via
# -r dev-requirements.in
# pip-tools
cfgv==3.4.0
# via pre-commit
click==8.1.7
# via
# -c ./requirements.txt
# black
# pip-tools
coverage[toml]==7.4.1
# via
# coverage
# pytest-cov
distlib==0.3.8
# via virtualenv
filelock==3.13.1
# via virtualenv
identify==2.5.34
# via pre-commit
iniconfig==2.0.0
# via pytest
invoke==2.2.0
# via -r dev-requirements.in
isort==5.12.0
# via -r dev-requirements.in
mypy-extensions==1.0.0
# via black
nodeenv==1.8.0
# via pre-commit
packaging==23.2
# via
# black
# build
# pytest
pathspec==0.12.1
# via black
pip-tools==7.3.0
# via -r dev-requirements.in
platformdirs==4.2.0
# via
# black
# virtualenv
pluggy==1.4.0
# via pytest
pre-commit==3.6.1
# via -r dev-requirements.in
pyproject-hooks==1.0.0
# via build
pytest==8.0.0
# via
# -r dev-requirements.in
# pytest-cov
pytest-cov==4.1.0
# via -r dev-requirements.in
pyyaml==6.0.1
# via pre-commit
ruff==0.2.1
# via -r dev-requirements.in
shellcheck-py==0.9.0.5
# via -r dev-requirements.in
virtualenv==20.25.0
# via pre-commit
wheel==0.42.0
# via pip-tools
# The following packages are considered to be unsafe in a requirements file:
# pip
# setuptools

@ -0,0 +1,8 @@
#!/bin/bash
# Best practice: Bash strict mode.
set -euo pipefail
# Best practice: Make sure the image shuts down correctly by using `exec` in
# entry point shell scripts.
exec "$@"

@ -0,0 +1,5 @@
class Example:
"""An example class"""
def __init__(self, name):
self.name = name

@ -0,0 +1,59 @@
[project]
name = "learn_otel"
version = "0.1.0"
authors = [
{name = "Drew Bednar", email = "drew@runcible.io"},
]
description = "Learning OTEL"
requires-python = ">=3.10"
license = {text = "MIT"}
classifiers = [
"Programming Language :: Python :: 3",
]
dynamic = ["readme", "dependencies"]
[tool.setuptools.dynamic]
readme = {file = ["README.md"], content-type = "text/markdown"}
dependencies = {file = ["requirements.txt"]}
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[tool.isort]
profile = "black"
line_length = 120
force_single_line = true
filter_files = true
[tool.black]
line-length = 120
skip-string-normalization = true
exclude = "(^/\\.git|^/env/|^/venv/|^/node_modules/)"
[tool.ruff]
line-length = 120
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

@ -0,0 +1,2 @@
flask<3
werkzeug<3

@ -0,0 +1,24 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile requirements.in
#
blinker==1.7.0
# via flask
click==8.1.7
# via flask
flask==2.3.3
# via -r requirements.in
itsdangerous==2.1.2
# via flask
jinja2==3.1.3
# via flask
markupsafe==2.1.5
# via
# jinja2
# werkzeug
werkzeug==2.3.8
# via
# -r requirements.in
# flask

@ -0,0 +1,10 @@
#!/usr/bin/env bash
function _setup_env() {
# build a virtual env
if [ -z "${VIRTUAL_ENV}" ]; then
python3 -m venv env
# shellcheck source=/dev/null
source env/bin/activate
fi
}

@ -0,0 +1,47 @@
#!/usr/bin/env bash
# setting -e to exit immediately on a command failure.
# set -o pipefail sets the exit code of a pipeline to
# that of the rightmost command to exit with a non-zero status,
# or to zero if all commands of the pipeline exit successfully.
# set -u affects variables. When set, a reference to any variable
# you haven't previously defined - with the exceptions of $* and $@ -
# is an error, and causes the program to immediately exit
set -eo pipefail
IFS=$'\n\t'
# shellcheck source=/dev/null
source "$(dirname "$0")/_common.sh"
function project_bootstrap() {
_setup_env
pip install -U pip pip-tools=='7.3.0'
# Check if requirements.txt exists, if not, compile it from requirements.in
if [ ! -f requirements.txt ]; then
pip-compile requirements.in
fi
# Check if dev-requirements.txt exists, if not, compile it from dev-requirements.in
if [ ! -f dev-requirements.txt ]; then
pip-compile dev-requirements.in
fi
pip-sync requirements.txt dev-requirements.txt
}
function install_precommit_hooks() {
if [ ! -d ".git" ]; then
git init
fi
if [ -z "$VIRTUAL_ENV" ]; then
echo "warning: you are not in a virtualenv"
exit 1
fi
pre-commit install
}
project_bootstrap
install_precommit_hooks

@ -0,0 +1,28 @@
#! /usr/bin/env bash
# shellcheck source=/dev/null
source "$(dirname "$0")/_common.sh"
if [ "${DRONE}" == "true" ]; then
_setup_env
pip install -r requirements.txt -r dev-requirements.txt
fi
# Run linting commands and capture their return codes
"${VIRTUAL_ENV}/bin/python3" -m isort --check ./speech_collect ./tests ./tasks.py
ISORT_EXIT_CODE=$?
"${VIRTUAL_ENV}/bin/python3" -m black --check ./speech_collect ./tests ./tasks.py
BLACK_EXIT_CODE=$?
"${VIRTUAL_ENV}/bin/python3" -m ruff ./speech_collect ./tests ./tasks.py
RUFF_EXIT_CODE=$?
# Check if any linting command failed
if [ $ISORT_EXIT_CODE -ne 0 ] || [ $BLACK_EXIT_CODE -ne 0 ] || [ $RUFF_EXIT_CODE -ne 0 ]; then
echo "Some linting checks failed"
# Exit with a non-zero status, you can choose which error code to return
exit 1
fi
echo "All linting checks passed"

@ -0,0 +1,12 @@
#! /usr/bin/env bash
set -ex
# shellcheck source=/dev/null
source "$(dirname "$0")/_common.sh"
if [ "${DRONE}" == "true" ]; then
_setup_env
pip install -r requirements.txt -r dev-requirements.txt
fi
exec "${VIRTUAL_ENV}/bin/python3" -m pytest -vv --cov "$@"

@ -0,0 +1,52 @@
import os
from invoke import task
IMAGE_RESPOSITORY = os.environ.get("IMAGE_RESPOSITORY", "registry.runcible.io/learn_otel")
@task
def update_deps(c):
"""Updates depenencies"""
c.run("pip-compile requirements.in", pty=True)
c.run("pip-compile dev-requirements.in", pty=True)
@task
def sync_deps(c):
"""Syncs local dependencies"""
c.run("pip-sync requirements.txt dev-requirements.txt")
@task
def lint(c):
"""Runs all linters against the project."""
c.run("./scripts/run_linters.sh", pty=True)
@task
def delint(c):
"""Applies automated linters to project"""
c.run("isort ./learn_otel ./tests ./tasks.py", pty=True)
c.run("black ./learn_otel ./tests ./tasks.py", pty=True)
@task
def build(c):
"""Builds the project as a Python package."""
c.run("python3 -m build")
@task
def build_image(c, dev=True, registry_user=None, registry_token=None, push=False, login=False):
"""Builds the learn_otel container image."""
context_dir = c.run("pwd", hide=True).stdout.strip()
commit_sha = c.run("git rev-parse --short HEAD", hide=True).stdout.strip()
image_name = f"{IMAGE_RESPOSITORY}:{commit_sha}{'-dev' if dev else ''}"
c.run(f"docker build --build-arg='COMMIT_SHA={commit_sha}' -t {image_name} {context_dir}")
if login:
if registry_user is None or registry_token is None:
raise ValueError("--registry_user and --registry_token must be provided if using --login parameter")
c.run(f"docker login -u {registry_user} -p {registry_token}")
if push:
c.run(f"docker push {image_name}")

@ -0,0 +1,6 @@
from learn_otel.example import Example
def test_example():
my_example = Example(name="dirp")
assert my_example.name == "dirp"
Loading…
Cancel
Save