Skip to content

Commit

Permalink
adopting poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
usrbinkat committed Nov 3, 2024
1 parent 8431e81 commit 6e374b1
Show file tree
Hide file tree
Showing 44 changed files with 3,524 additions and 1,885 deletions.
50 changes: 38 additions & 12 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@

FROM ghcr.io/containercraft/devcontainer:latest

# Install additional dependencies by extending the base image
#ARG DEBIAN_FRONTEND=noninteractive
#ARG APT_PACKAGES="\
#"
#
#RUN set -ex \
# && apt-get update \
# && apt-get install -y $APT_PACKAGES \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/* \
# && echo

##################################################################################
# Install AWS CLI v2

Expand All @@ -41,6 +29,44 @@ RUN echo \
&& echo
USER vscode

##################################################################################
# Install Poetry for Python dependency management
#USER root
#ENV POETRY_HOME="/usr/local" \
# PATH="${POETRY_HOME}/bin:${PATH}"
#
#RUN PYTHON_VERSION=$(python3 --version | cut -d' ' -f2 | cut -d'.' -f1,2) \
# && NAME="poetry" \
# && echo "---------------------------------------------------------" \
# && echo "INFO[${NAME}] Installing:" \
# && echo "INFO[${NAME}] Command: ${NAME}" \
# && echo "INFO[${NAME}] Python Version: ${PYTHON_VERSION}" \
# && echo "---------------------------------------------------------" \
# && curl -sSL https://install.python-poetry.org | python3 - \
# && chmod +x /usr/local/bin/poetry \
# && poetry config virtualenvs.in-project true \
# && poetry --version \
# && mkdir -p /home/vscode/.config/pypoetry \
# && chown -R vscode:vscode /home/vscode/.config \
# && echo
#
#USER vscode
#RUN poetry config virtualenvs.in-project true \
# && poetry config virtualenvs.create true

###################################################################################
# Install additional dependencies by extending the base image
#ARG DEBIAN_FRONTEND=noninteractive
#ARG APT_PACKAGES="\
#"
#
#RUN set -ex \
# && apt-get update \
# && apt-get install -y $APT_PACKAGES \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/* \
# && echo

###################################################################################
## Install Google Cloud SDK
#
Expand Down
39 changes: 21 additions & 18 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
source .tmpenv # 2>/dev/null || true
##################################################################################
# bash bin
export PATH=$PATH:.github/bin
#!/usr/bin/env bash

# Platform Architecture
#export ARCH=$(uname -m | awk '{ if ($$1 == "x86_64") print "amd64"; else if ($$1 == "aarch64" || $$1 == "arm64") print "arm64"; else print "unknown" }')
# Source environment variables from .env and temporary environment
source_env .env 2>/dev/null || true
source .tmpenv 2>/dev/null || true

##################################################################################
# Basic Config Variables
# Add local bin directories to PATH
PATH_add .venv/bin
PATH_add .github/bin

# Platform Architecture Detection
export ARCH=$(uname -m | awk '{ if ($1 == "x86_64") print "amd64"; else if ($1 == "aarch64" || $1 == "arm64") print "arm64"; else print "unknown" }')

# Basic Configuration
export BROWSER=echo
export KUBECONFIG=$PWD/.kube/config
export TALOSCONFIG=$PWD/.talos/manifest/talosconfig
#export OMNICONFIG=.talos/omniconfig

##################################################################################
# Pulumi Environment Variables
# - https://www.pulumi.com/docs/cli/environment-variables
# Pulumi Configuration
# See: https://www.pulumi.com/docs/cli/environment-variables
export PULUMI_SKIP_UPDATE_CHECK=true
export PULUMI_SKIP_CONFIRMATIONS=true
export PULUMI_AUTOMATION_API_SKIP_VERSION_CHECK=true
#export PULUMI_K8S_DELETE_UNREACHABLE=true
#export PULUMI_HOME=$PWD/.pulumi

##################################################################################
# Optional Pulumi Environment Variables
# Useful for CI testing
# Uncomment to use local backend instead of Pulumi Cloud
# Optional Configuration (commented out by default)
#export OMNICONFIG=.talos/omniconfig

# Optional Pulumi Backend Configuration
# Uncomment for local backend instead of Pulumi Cloud
#export PULUMI_BACKEND_URL=${PULUMI_BACKEND_URL:-file://$PWD/.pulumi}
#export PULUMI_CONFIG_PASSPHRASE=${PULUMI_CONFIG_PASSPHRASE:-foobarbaz}
#export PULUMI_HOME=$PWD/.pulumi
#export PULUMI_K8S_DELETE_UNREACHABLE=true
69 changes: 69 additions & 0 deletions .github/bin/cloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash
set -euo pipefail

# Script to count non-comment, non-docstring lines of Python code in Pulumi IaC
# Usage: ./cloc
# Description: Analyzes Python code in the ./pulumi directory, excluding:
# - Comments (lines starting with #)
# - Empty lines and whitespace
# - Docstrings (multi-line strings between """ or ''')
# - Inline comments (# after code)

# Ensure we're in the root directory
cd "$(dirname "$0")/../.." || exit 1

# Verify pulumi directory exists
if [[ ! -d "./pulumi" ]]; then
echo "Error: ./pulumi directory not found!"
exit 1
fi

echo "Analyzing Python code in Pulumi Infrastructure as Code (IaC) directory..."
echo "Excluding comments, docstrings, and blank lines..."

# Count lines across all Python files
total_lines=$(find ./pulumi -name "*.py" -type f -exec awk '
BEGIN {
in_docstring = 0
count = 0
}
{
# Store original line for inline comment handling
original = $0
# Trim leading/trailing whitespace
line = $0
gsub(/^[ \t]+|[ \t]+$/, "", line)
# Skip empty lines
if (line == "") { next }
# Skip full comment lines
if (line ~ /^#/) { next }
# Handle triple-quoted docstrings (both single and double quotes)
if (line ~ /^"""/ || line ~ /"""$/ || line ~ /^'\'''\'''\''/ || line ~ /'\'''\'''\''$/) {
in_docstring = !in_docstring
next
}
if (in_docstring) { next }
# Remove inline comments
sub(/#.*$/, "", original)
# Trim whitespace after removing inline comment
gsub(/^[ \t]+|[ \t]+$/, "", original)
# Skip if line becomes empty after removing inline comment
if (original == "") { next }
# Count this line
count++
}
END { print count }
' {} \; | awk '{total += $1} END {print total}')

# Verify we got a valid number
if [[ ! "$total_lines" =~ ^[0-9]+$ ]]; then
echo "Error: Failed to count lines properly!"
exit 1
fi

echo "Total lines of Python code: ${total_lines}"
9 changes: 5 additions & 4 deletions Pulumi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ name: konductor
description: DevOps Platform IaC Template Repository
main: ./pulumi
stackConfigDir: ./pulumi/stacks
options:
refresh: always
runtime:
name: python
# options:
# virtualenv: venv
options:
toolchain: poetry
typechecker: pyright
options:
refresh: always
75 changes: 64 additions & 11 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ vars:
echo "unknown"
fi
# New Python-specific vars
python_version: "3.8"
poetry_version: "1.7.1"

tasks:
##################################################################################
# Meta & Utility Tasks
Expand All @@ -39,6 +43,7 @@ tasks:
default:
desc: "Run all tasks to set up and configure a Kargo Kubevirt Kubernetes Platform."
cmds:
- task: dev-setup
- task: deploy

# Task to print environment variables, useful for debugging
Expand Down Expand Up @@ -141,21 +146,22 @@ tasks:
configure:
desc: "Configure Pulumi stack settings."
cmds:
- source .envrc && pulumi stack select --create {{.pulumi_stack_identifier}}
- source .envrc && pulumi config set --path kubernetes.context admin@{{.cluster_name}}
- source .envrc && pulumi config set --path kubernetes.kubeconfig {{.kube_config_file}}
- source .envrc && pulumi config set --path kubernetes.distribution talos
- source .envrc && pulumi config set --path cilium.enabled false
- source .envrc && pulumi config set --path vm.enabled false
- source .envrc && poetry run pulumi stack select --create {{.pulumi_stack_identifier}}
- source .envrc && poetry run pulumi config set --path kubernetes.context admin@{{.cluster_name}}
- source .envrc && poetry run pulumi config set --path kubernetes.kubeconfig {{.kube_config_file}}
- source .envrc && poetry run pulumi config set --path kubernetes.distribution talos
- source .envrc && poetry run pulumi config set --path cilium.enabled false
- source .envrc && poetry run pulumi config set --path vm.enabled false

# Task to deploy Pulumi infrastructure
iac-deploy:
desc: "Deploy Pulumi infrastructure."
deps: [type-check]
cmds:
- task: iac-cancel
- source .envrc && pulumi up --yes --skip-preview --refresh --continue-on-error --stack {{.pulumi_stack_identifier}} || true
- source .envrc && poetry run pulumi up --yes --skip-preview --refresh --continue-on-error --stack {{.pulumi_stack_identifier}} || true
- task: all-pods-ready
- source .envrc && pulumi up --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}}
- source .envrc && poetry run pulumi up --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}}
- task: all-pods-ready

# Task to destroy Pulumi infrastructure
Expand All @@ -165,14 +171,14 @@ tasks:
- task: iac-cancel
- |
source .envrc
pulumi down --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}} || true
pulumi down --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}}
poetry run pulumi down --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}} || true
poetry run pulumi down --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}}
# Task to cancel the Pulumi update process
iac-cancel:
desc: "Cancel the Pulumi update."
cmds:
- source .envrc && pulumi cancel --yes --stack {{.pulumi_stack_identifier}} 2>/dev/null || true
- source .envrc && poetry run pulumi cancel --yes --stack {{.pulumi_stack_identifier}} 2>/dev/null || true

# Task to clean up all Pulumi resources
iac-clean:
Expand All @@ -181,6 +187,15 @@ tasks:
- task: iac-cancel
- source .envrc && pulumi down --yes --skip-preview --refresh --stack {{.pulumi_stack_identifier}} 2>/dev/null || true

# Add pre-commit hook task
pre-commit:
desc: "Run pre-commit checks"
cmds:
- task: type-check
- task: format
- task: lint
- task: test

##################################################################################
# Talos Tasks
##################################################################################
Expand Down Expand Up @@ -236,3 +251,41 @@ tasks:
- task: kubernetes-deploy
- task: kubernetes-ready
- task: all-pods-ready

##################################################################################
# Python Development Tasks
##################################################################################

poetry-update:
desc: "Update Poetry dependencies"
cmds:
- poetry update
- poetry install

type-check:
desc: "Run Pyright type checking"
cmds:
- poetry run pyright pulumi/

format:
desc: "Format Python code with Black"
cmds:
- poetry run black pulumi/

lint:
desc: "Run Pylint"
cmds:
- poetry run pylint pulumi/

test:
desc: "Run Python tests"
cmds:
- poetry run pytest

dev-setup:
desc: "Complete development environment setup"
cmds:
- task: type-check
- task: format
- task: lint
- task: configure
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Before you begin, ensure you have the following installed:
- **Kubectl**: For interacting with Kubernetes clusters.
- **sudo**: For executing administrative commands.

> NOTE: All dependencies are automatically supplied in the [ghcr.io/containercraft/devcontainer](https://github.com/containercraft/devcontainer) image powering the VSCode Dev Container included in this repository by the [.devcontainer/devcontainer.json](.devcontainer/devcontainer.json) and [.devcontainer/Dockerfile](.devcontainer/Dockerfile).
## Steps to Recreate

Follow the steps below to set up your environment:
Expand Down
Loading

0 comments on commit 6e374b1

Please sign in to comment.