diff --git a/.github/workflows/ansible-deploy.yml b/.github/workflows/ansible-deploy.yml new file mode 100644 index 0000000000..cc74d130ed --- /dev/null +++ b/.github/workflows/ansible-deploy.yml @@ -0,0 +1,90 @@ +name: Ansible Deployment + +on: + push: + branches: [ master, lab11 ] + + pull_request: + branches: [ master ] + paths: + - 'app_python/ansible/**' + +jobs: + + lint: + name: Ansible Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + pip install ansible ansible-lint + + - name: Run ansible-lint + run: | + cd app_python/ansible + ansible-lint playbooks/*.yml + + + deploy: + name: Deploy Application + needs: lint + runs-on: ubuntu-latest + + steps: + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install Ansible + run: | + pip install ansible + + - name: Setup SSH + run: | + mkdir -p ~/.ssh + echo Store the private key + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/devops-terraform-passwordless + chmod 600 ~/.ssh/devops-terraform-passwordless + echo check key validity + ssh-keygen -y -f ~/.ssh/devops-terraform-passwordless + echo Append vm ip to known hosts + ssh-keyscan "${{ secrets.VM_HOST }}" >> ~/.ssh/known_hosts + + - name: Recreate group_vars/all.yml + run: | + mkdir -p app_python/ansible/group_vars + echo "${{ secrets.ANSIBLE_GROUP_VARS_ALL }}" | base64 --decode > app_python/ansible/group_vars/all.yml + + - name: Run Ansible Playbook + run: | + ssh -i ~/.ssh/devops-terraform-passwordless ubuntu@${{ secrets.VM_HOST }} "echo connected" + + echo '${{ secrets.ANSIBLE_VAULT_PASSWORD }}' > /tmp/vault_pass + cd app_python/ansible + + ansible-playbook playbooks/deploy.yml \ + -i inventory/hosts.ini \ + --vault-password-file /tmp/vault_pass \ + --extra-vars @./group_vars/all.yml + + rm /tmp/vault_pass + + - name: Verify Deployment + run: | + sleep 10 + curl -f http://${{ secrets.VM_HOST }}:1999 || exit 1 + curl -f http://${{ secrets.VM_HOST }}:1999/health || exit 1 \ No newline at end of file diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml new file mode 100644 index 0000000000..e9bb430f24 --- /dev/null +++ b/.github/workflows/python-ci.yml @@ -0,0 +1,69 @@ +name: My FLask App Testing + +on: [push, pull_request] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + cache: 'pip' + cache-dependency-path: './app_python/requirements.txt' + - name: Install dependencies + run: | + pip install -r ./app_python/requirements.txt + pip install pytest flake8 + - name: Lint + run: flake8 ./app_python/app.py + - name: Test + run: pytest ./app_python/tests/ + docker: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Generate version + run: echo "VERSION=$(date +%Y.%m.%d)" >> $GITHUB_ENV + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: ./app_python + push: true + platforms: linux/amd64,linux/arm64 + tags: | + ${{ secrets.DOCKER_USERNAME }}/my-app:latest + ${{ secrets.DOCKER_USERNAME }}/my-app:${{ env.VERSION }} + ${{ secrets.DOCKER_USERNAME }}/my-app:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + security: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + cache: 'pip' + cache-dependency-path: './app_python/requirements.txt' + - name: Install dependencies + run: | + pip install -r app_python/requirements.txt + - name: Install Snyk CLI + run: npm install -g snyk + - name: Run Snyk + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + run: | + snyk test app_python \ + --severity-threshold=high diff --git a/.gitignore b/.gitignore index 30d74d2584..e0d7cccaf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,14 @@ -test \ No newline at end of file +# OS +.DS_Store + +# Vault +app_python/ansible/group_vars/all.yml + +# File for me +app_python/reminder.md + +.vscode/settings.json + +app_python/monitoring/.env + +argocd_pass \ No newline at end of file diff --git a/.wrangler/cache/wrangler-account.json b/.wrangler/cache/wrangler-account.json new file mode 100644 index 0000000000..16741862e0 --- /dev/null +++ b/.wrangler/cache/wrangler-account.json @@ -0,0 +1,6 @@ +{ + "account": { + "id": "7a19bada09a97582626ff1817cab49ed", + "name": "V.levasheva@innopolis.university's Account" + } +} \ No newline at end of file diff --git a/app_python/.dockerignore b/app_python/.dockerignore new file mode 100644 index 0000000000..889bdb36b4 --- /dev/null +++ b/app_python/.dockerignore @@ -0,0 +1,29 @@ +# Python +__pycache__/ +*.py[cod] +venv/ +*.log +.venv/ + +# Version control +.git +.gitignore + +# IDE +.vscode/ +.idea/ + +# OS +.DS_Store + +# Secrets +.env +*.pem +secrets/ + +# Documentation +*.md +docs/ + +# Tests +tests/ \ No newline at end of file diff --git a/app_python/.gitignore b/app_python/.gitignore new file mode 100644 index 0000000000..65b17f162c --- /dev/null +++ b/app_python/.gitignore @@ -0,0 +1,16 @@ +# Python +__pycache__/ +*.py[cod] +venv/ +*.log + +# IDE +.vscode/ +.idea/ + +# OS +.DS_Store + +# Vault +./ansible/group_vars/all.yml +~/.vault_pass.txt \ No newline at end of file diff --git a/app_python/.python-version b/app_python/.python-version new file mode 100644 index 0000000000..e18b651909 --- /dev/null +++ b/app_python/.python-version @@ -0,0 +1 @@ +devops diff --git a/app_python/Dockerfile b/app_python/Dockerfile new file mode 100644 index 0000000000..22740e5e88 --- /dev/null +++ b/app_python/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.13-slim AS builder +WORKDIR /app_python +COPY requirements.txt . +RUN pip install --upgrade pip && \ + pip install --no-cache-dir -r requirements.txt +COPY app.py . + +FROM python:3.13-slim +ENV PORT="12345" +EXPOSE 12345 +RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/* +RUN useradd --create-home --shell /bin/bash newuser +WORKDIR /app +RUN mkdir -p /app/data && chown -R newuser:newuser /app +COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages +COPY --from=builder ./app_python . +USER newuser +CMD ["python", "app.py"] \ No newline at end of file diff --git a/app_python/README.md b/app_python/README.md new file mode 100644 index 0000000000..14c94fc1da --- /dev/null +++ b/app_python/README.md @@ -0,0 +1,109 @@ +# User-facing documentation + +## Overview + +When acessed, this web service answers with information either about itself (path "/") or with its health status ("/health). The information includes fields with the details of the service, system, runtime, request and enpoints. + +## Prerequisites + +### Python version + +Pyenv is used for the virtual environment managing. To install it refer to [this website](https://akrabat.com/creating-virtual-environments-with-pyenv/) for instructions. + +Python version used: 3.12.9 + + +### Python libraries: +- jsonify imported from Flask library +- request imported from Flask library +- platform +- socket +- datetime imported from datetime +- os +- logging + +### Installation + +```bash +pyenv virtualenv 3.12.9 webapp +pyenv activate webapp +pip install -r requirements.txt + ``` +### Running the Application +```bash +python app.py +PORT=8080 python app.py # if you want to use a custom port (the default is 5000) +``` + +### API Endpoints ### +- `GET /` - Service and system information +- `GET /health` - Health check +- `GET /visits` - See the number of times root directory was accessed +- `GET /ready` - Check if the application is ready to take requests +- `GET /health` - Check if the application is dead and should be reloaded + +### Configuration ### + +| № | Var name | Description +| ---| ----- | --------------------| +| #1 | HOST | Name of the host (defaults to socket.hostname first if available) | +| #2 | PORT | Port for the application access | +| #3 | DEBUG | Debug status | + +### Docker container ### + +#### Building the image locally ### + +Use the command in format: +```bash +docker build -t your-docker-username/image-name:image-tag directory-with-dockerfile +``` +Example: +```bash +docker build -t fountainer/my-app:1.1.0 . +``` +#### Running a container ### + +Use the command in format: +```bash +docker run -p host-port:container-port image-name:tag +``` +Example: +```bash +docker run -p 12345:12345 fountainer/my-app:1.1.0 +``` +#### Pulling from Docker Hub ### + +```bash +docker image pull image-name:image-tag +``` + +Example: +```bash +docker image pull app:1.0.0 +``` + +### Testing ### + +#### Workflow Badge + +[![My FLask App Testing](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/python-ci.yml/badge.svg?branch=master)](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/python-ci.yml) + +#### Unit testing #### + +To test the ./ endpoint: + +```bash +pytest ./app_python/tests/test_home_endpoint.py +``` + +To test the ./health endpoint: + + +```bash +pytest ./app_python/tests/test_health_endpoint.py +``` + +## Ansible deployment + +[![Ansible Deployment](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/ansible-deploy.yml/badge.svg)](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/ansible-deploy.yml) \ No newline at end of file diff --git a/app_python/ansible/.vault_pass.txt b/app_python/ansible/.vault_pass.txt new file mode 100644 index 0000000000..d9d37937a0 --- /dev/null +++ b/app_python/ansible/.vault_pass.txt @@ -0,0 +1 @@ +1967 diff --git a/app_python/ansible/ansible.cfg b/app_python/ansible/ansible.cfg new file mode 100644 index 0000000000..18d1667515 --- /dev/null +++ b/app_python/ansible/ansible.cfg @@ -0,0 +1,12 @@ +[defaults] +inventory = inventory/hosts.ini +roles_path = roles +host_key_checking = False +remote_user = ubuntu +retry_files_enabled = False +vault_password_file = .vault_pass.txt + +[privilege_escalation] +become = True +become_method = sudo +become_user = root \ No newline at end of file diff --git a/app_python/ansible/docs/LAB05.md b/app_python/ansible/docs/LAB05.md new file mode 100644 index 0000000000..082dcec5d2 --- /dev/null +++ b/app_python/ansible/docs/LAB05.md @@ -0,0 +1 @@ +!!!!!! real LAB05.md documentation lies in the app_python/docs/LAB05.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/app_python/ansible/docs/LAB06.md b/app_python/ansible/docs/LAB06.md new file mode 100644 index 0000000000..72d34032cc --- /dev/null +++ b/app_python/ansible/docs/LAB06.md @@ -0,0 +1 @@ +!!!!!! real LAB06.md documentation lies in the app_python/docs/LAB06.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/app_python/ansible/inventory/hosts.ini b/app_python/ansible/inventory/hosts.ini new file mode 100644 index 0000000000..af7b76d3fc --- /dev/null +++ b/app_python/ansible/inventory/hosts.ini @@ -0,0 +1,7 @@ +[webservers] +terraform ansible_host=93.77.189.231 + +[webservers:vars] +ansible_user=ubuntu +ansible_python_interpreter=/usr/bin/python3 +ansible_ssh_private_key_file=~/.ssh/devops-terraform-passwordless \ No newline at end of file diff --git a/app_python/ansible/playbooks/deploy.yml b/app_python/ansible/playbooks/deploy.yml new file mode 100644 index 0000000000..95174b9e0e --- /dev/null +++ b/app_python/ansible/playbooks/deploy.yml @@ -0,0 +1,7 @@ +--- +- name: Deploy application + hosts: webservers + become: true + + roles: + - web_app diff --git a/app_python/ansible/playbooks/provision.yml b/app_python/ansible/playbooks/provision.yml new file mode 100644 index 0000000000..6334c412cc --- /dev/null +++ b/app_python/ansible/playbooks/provision.yml @@ -0,0 +1,12 @@ +--- +- name: Provision web servers + hosts: webservers + become: true + + roles: + - role: common + tags: + - common + - role: docker + tags: + - docker diff --git a/app_python/ansible/roles/common/defaults/main.yml b/app_python/ansible/roles/common/defaults/main.yml new file mode 100644 index 0000000000..ec12cf9268 --- /dev/null +++ b/app_python/ansible/roles/common/defaults/main.yml @@ -0,0 +1,12 @@ +--- +common_packages: + - vim + - git + - curl + - python3-pip + - htop + +common_timezone_name: "Europe/Moscow" + +common_users: + - ubuntu diff --git a/app_python/ansible/roles/common/tasks/main.yml b/app_python/ansible/roles/common/tasks/main.yml new file mode 100644 index 0000000000..72042655d6 --- /dev/null +++ b/app_python/ansible/roles/common/tasks/main.yml @@ -0,0 +1,47 @@ +--- +# the first block with package installation tasks +- name: Install dependencies + become: true + tags: + - packages + block: + - name: Update apt cache + ansible.builtin.apt: + update_cache: true + cache_valid_time: 3600 + - name: Install common_packages + ansible.builtin.apt: + name: "{{ common_packages }}" + state: present + rescue: + - name: Handle installation failure + ansible.builtin.apt: + update_cache: true + force_apt_get: true + always: + - name: Write log file + ansible.builtin.copy: + content: "Common role has finished" + dest: /tmp/common.log + mode: "0644" + # The second block with user creation +- name: Manage users + become: true + tags: + - users + block: + - name: Create common users + ansible.builtin.user: + name: "{{ item }}" + state: present + create_home: true + loop: "{{ common_users }}" + # the third block with timezone set up +- name: Ensure timezone is set + become: true + tags: + - timezone + block: + - name: Set timezome + community.general.timezone: + name: "{{ common_timezone_name }}" diff --git a/app_python/ansible/roles/docker/defaults/main.yml b/app_python/ansible/roles/docker/defaults/main.yml new file mode 100644 index 0000000000..5638745c1e --- /dev/null +++ b/app_python/ansible/roles/docker/defaults/main.yml @@ -0,0 +1,8 @@ +--- +docker_packages: + - docker-ce + - docker-ce-cli + - containerd.io +docker_user: "ubuntu" +docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" diff --git a/app_python/ansible/roles/docker/handlers/main.yml b/app_python/ansible/roles/docker/handlers/main.yml new file mode 100644 index 0000000000..07aa0eb290 --- /dev/null +++ b/app_python/ansible/roles/docker/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Restart docker + ansible.builtin.service: + name: docker + state: restarted diff --git a/app_python/ansible/roles/docker/tasks/main.yml b/app_python/ansible/roles/docker/tasks/main.yml new file mode 100644 index 0000000000..58c0a6981d --- /dev/null +++ b/app_python/ansible/roles/docker/tasks/main.yml @@ -0,0 +1,66 @@ +--- +- name: Docker installation + tags: + - docker_install + become: true + # block with docker installation and set up + block: + - name: Install prerequisites + ansible.builtin.apt: + name: + - apt-transport-https + - ca-certificates + - curl + - gnupg + - lsb-release + state: present + update_cache: true + force_apt_get: true + - name: Add Docker GPG key + ansible.builtin.apt_key: + url: "{{ docker_gpg_url }}" + state: present + - name: Add Docker APT repository + ansible.builtin.apt_repository: + repo: "{{ docker_repo }}" + state: present + filename: docker + - name: Install Docker packages + ansible.builtin.apt: + name: "{{ docker_packages }}" + state: present + update_cache: true + force_apt_get: true + notify: Restart docker + - name: Install python3-docker for Ansible Docker modules + ansible.builtin.apt: + name: python3-docker + state: present + rescue: + - name: Wait before retry + ansible.builtin.pause: + seconds: 10 + - name: Retry apt update + ansible.builtin.apt: + update_cache: true + - name: Retry adding Docker GPG key + ansible.builtin.apt_key: + url: "{{ docker_gpg_url }}" + state: present + always: + - name: Ensure docker service is enabled + ansible.builtin.service: + name: docker + state: started + enabled: true +- name: Configure docker + tags: + - docker_config + become: true + # block with docker configuration + block: + - name: Add user to Docker group + ansible.builtin.user: + name: "{{ docker_user }}" + groups: docker + append: true diff --git a/app_python/ansible/roles/web_app/defaults/main.yml b/app_python/ansible/roles/web_app/defaults/main.yml new file mode 100644 index 0000000000..fd4cb65878 --- /dev/null +++ b/app_python/ansible/roles/web_app/defaults/main.yml @@ -0,0 +1,17 @@ +--- +web_app_container_port: "12345" +web_app_restart_policy: unless-stopped +web_app_compose_project_dir: "/opt/{{ app_name }}" +web_app_docker_image: "{{ dockerhub_username }}/{{ app_name }}" +web_app_docker_tag: latest + +web_app_env: + PORT: "{{ web_app_container_port }}" + +# Wipe Logic Control +web_app_wipe: false # Default: do not wipe + +# Usage documentation: +# Set to true to remove application completely +# Wipe only: ansible-playbook deploy.yml -e "web_app_wipe=true" --tags web_app_wipe +# Clean install: ansible-playbook deploy.yml -e "web_app_wipe=true" diff --git a/app_python/ansible/roles/web_app/handlers/main.yml b/app_python/ansible/roles/web_app/handlers/main.yml new file mode 100644 index 0000000000..ad70fe1fa6 --- /dev/null +++ b/app_python/ansible/roles/web_app/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: Restart app container + community.docker.docker_container: + name: "{{ app_container_name }}" + state: started + restart: false diff --git a/app_python/ansible/roles/web_app/meta/main.yml b/app_python/ansible/roles/web_app/meta/main.yml new file mode 100644 index 0000000000..cb7d8e0460 --- /dev/null +++ b/app_python/ansible/roles/web_app/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - role: docker diff --git a/app_python/ansible/roles/web_app/tasks/main.yml b/app_python/ansible/roles/web_app/tasks/main.yml new file mode 100644 index 0000000000..1536ed6da0 --- /dev/null +++ b/app_python/ansible/roles/web_app/tasks/main.yml @@ -0,0 +1,44 @@ +--- +- name: Deploy application with Docker Compose + tags: + - app_deploy + - compose + # block for app deployment with docker compose and possible wipe logic + block: + - name: Include wipe tasks + ansible.builtin.include_tasks: wipe.yml + tags: + - web_app_wipe + + - name: Create application directory + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}" + state: directory + owner: root + group: root + mode: "0755" + + - name: Template docker-compose.yml + ansible.builtin.template: + src: docker-compose.yml.j2 + dest: "{{ web_app_compose_project_dir }}/docker-compose.yml" + mode: "0644" + + - name: Deploy with Docker Compose + community.docker.docker_compose_v2: + project_src: "{{ web_app_compose_project_dir }}" + pull: missing + state: present + recreate: auto + + rescue: + - name: Handle deployment failure + ansible.builtin.debug: + msg: "Docker Compose deployment failed. Check logs and network." + + always: + - name: Log deployment attempt + ansible.builtin.copy: + content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + dest: "/tmp/{{ app_name }}-compose.log" + mode: "0644" diff --git a/app_python/ansible/roles/web_app/tasks/wipe.yml b/app_python/ansible/roles/web_app/tasks/wipe.yml new file mode 100644 index 0000000000..0db3164ba0 --- /dev/null +++ b/app_python/ansible/roles/web_app/tasks/wipe.yml @@ -0,0 +1,27 @@ +--- +- name: Wipe web application + when: web_app_wipe | bool + tags: + - web_app_wipe + block: + - name: Stop and remove containers + community.docker.docker_compose_v2: + project_src: "{{ web_app_compose_project_dir }}" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Remove docker-compose file + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}/docker-compose.yml" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Remove application directory + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Log wipe completion + ansible.builtin.debug: + msg: "Application {{ app_name }} wiped successfully" diff --git a/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 b/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 new file mode 100644 index 0000000000..e373f7d510 --- /dev/null +++ b/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 @@ -0,0 +1,22 @@ +version: '{{ docker_compose_version }}' + +services: + {{ app_name }}: + image: {{ web_app_docker_image }}:{{ web_app_docker_tag }} + + ports: + - "{{ app_port }}:{{ app_internal_port }}" + + restart: unless-stopped + + environment: +{% for key, value in web_app_env.items() %} + {{ key }}: "{{ value }}" +{% endfor %} + + networks: + - {{ app_name }}_network + +networks: + {{ app_name }}_network: + driver: bridge \ No newline at end of file diff --git a/app_python/app.py b/app_python/app.py new file mode 100644 index 0000000000..b0f3ff3816 --- /dev/null +++ b/app_python/app.py @@ -0,0 +1,348 @@ +""" +DevOps Info Service +Lab 1. Veronika Levasheva +""" + +from flask import Flask, jsonify +from flask import request +import platform +from datetime import datetime +import socket +import os +import logging +from pythonjsonlogger.json import JsonFormatter +from prometheus_client import Counter, Histogram, Gauge, generate_latest +import time +from flask import g, Response +from threading import Lock + +app = Flask(__name__) # creating an instance of Flask + + +logger = logging.getLogger(__name__) + + +# Log important events: startup, HTTP requests, errors +# Include context: method, path, status code, client IP + +logHandler = logging.StreamHandler() +formatter = JsonFormatter( + "levelname, asctime, message", + style=",", + rename_fields=( + {"levelname": "LEVEL", "asctime": "TIMESTAMP", "message": "MESSAGE"} + ), +) + +logHandler.setFormatter(formatter) +logger.addHandler(logHandler) + +logger.setLevel(logging.DEBUG) + +# Prometheus metrics + +http_requests_total = Counter( + "http_requests_total", "Total HTTP requests", + ["method", "endpoint", "status_code"] +) + +http_request_duration_seconds = Histogram( + "http_request_duration_seconds", + "HTTP request duration", ["method", "endpoint"] +) + +http_requests_in_progress = Gauge( + "http_requests_in_progress", + "HTTP requests currently being processed" +) + +endpoint_calls = Counter("devops_info_endpoint_calls", + "Endpoint calls", ["endpoint"]) + +system_info_duration = Histogram( + "devops_info_system_collection_seconds", + "System info collection time" +) + +DATA_DIR = os.getenv("DATA_DIR", "/tmp/data") +VISITS_FILE = os.path.join(DATA_DIR, "visits") + +visits_lock = Lock() +visits_count = 0 + + +def load_visits(): + global visits_count + + os.makedirs(DATA_DIR, exist_ok=True) + + if not os.path.exists(VISITS_FILE): + visits_count = 0 + return + + try: + with open(VISITS_FILE, "r") as f: + visits_count = int(f.read().strip()) + except Exception: + visits_count = 0 + + +def increment_visits(): + global visits_count + + with visits_lock: + visits_count += 1 + with open(VISITS_FILE, "w") as f: + f.write(str(visits_count)) + + return visits_count + + +def get_visits(): + with visits_lock: + return visits_count + + +@app.before_request +def before_request(): + g.start_time = time.time() + http_requests_in_progress.inc() + + +@app.after_request +def after_request(response): + duration = time.time() - g.start_time + + endpoint = request.path + + if endpoint == "/metrics": + return response + + if endpoint.startswith("/user/"): + endpoint = "/user/{id}" + + http_requests_total.labels( + method=request.method, endpoint=endpoint, + status_code=response.status_code + ).inc() + + http_request_duration_seconds.labels( + method=request.method, endpoint=endpoint + ).observe(duration) + + http_requests_in_progress.dec() + + return response + + +# variable names +platform_name = platform.system() +architecture = platform.machine() +python_version = platform.python_version() +hostname = socket.gethostname() + +# env variables +ADDRESS = os.getenv("ADDRESS", "0.0.0.0") +PORT = int(os.getenv("PORT", 1999)) +DEBUG = os.getenv("DEBUG", "True").lower() == "true" + + +@app.route("/metrics") +def metrics(): + return Response(generate_latest(), mimetype="text/plain") + + +# decorator for / path +@app.route("/", methods=["GET"]) +def get_endpoint(): + start = time.time() + endpoint_calls.labels(endpoint="/").inc() + current_visits = increment_visits() + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify({ + "message": message, + "visits": current_visits + }) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +@app.route("/visits", methods=["GET"]) +def visits(): + start = time.time() + endpoint_calls.labels(endpoint="/visits").inc() + + count = get_visits() + + response = jsonify({"visits": count}) + response.status_code = 200 + + system_info_duration.observe(time.time() - start) + return response + + +@app.route("/ready") +def ready(): + start = time.time() + endpoint_calls.labels(endpoint="/ready").inc() + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify({ + "status": "ready", + "timestamp": datetime.now().isoformat() + }) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +# decorator for /health path +@app.route("/health") +def health(): + start = time.time() + endpoint_calls.labels(endpoint="/health").inc() + # extra: status code, client ip + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify( + { + "status": "healthy", + "timestamp": datetime.now().isoformat(), + "uptime_seconds": get_uptime()["seconds"], + } + ) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +# error handling + + +@app.errorhandler(404) +def not_found(error): + logger.error( + {"MESSAGE": "Endpoint does not exist"}, + extra={ + "ERROR": "Not Found", + "STATUS_CODE": 404, + "CLIENT_IP": request.remote_addr, + "METHOD": request.method, + "PATH": request.path, + }, + ) + return ( + jsonify(({"error": "Not Found", + "MESSAGE": "Endpoint does not exist"})), + 404, + ) + + +@app.errorhandler(500) +def internal_error(error): + logger.error( + {"MESSAGE": "Internal Server Error"}, + extra={ + "ERROR": "Not Found", + "STATUS_CODE": 500, + "CLIENT_IP": request.remote_addr, + "METHOD": request.method, + "PATH": request.path, + }, + ) + return ( + jsonify( + { + "error": "Internal Server Error", + "MESSAGE": "An unexpected error occurred", + } + ), + 500, + ) + + +start_time = datetime.now() + + +def get_uptime(): + delta = datetime.now() - start_time + seconds = int(delta.total_seconds()) + hours = seconds // 3600 + minutes = (seconds % 3600) // 60 + return {"seconds": seconds, "human": f"{hours} hours, {minutes} minutes"} + + +# json message with system and environment info +message = { + "service": { + "name": "devops-info-service", + "version": "1.0.0", + "description": "DevOps course info service", + "framework": "Flask", + "debug status": DEBUG, + }, + "system": { + "hostname": hostname, + "platform": platform_name, + "platform_version": "Ubuntu 24.04", + "architecture": architecture, + "cpu_count": 8, + "python_version": python_version, + }, + "runtime": { + "uptime_seconds": get_uptime(), + "uptime_human": "1 hour, 0 minutes", + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + }, + "request": { + "client_ip": "127.0.0.1", + "port": PORT, + "user_agent": "curl/7.81.0", + "method": "GET", + "path": "/", + }, + "endpoints": [ + {"path": "/", "method": "GET", "description": "Service information"}, + {"path": "/health", "method": "GET", "description": "Health check"}, + ], +} + +logger.info( + { + "MESSAGE": f"Application starting on port {PORT}...", + } +) + +load_visits() +if __name__ == "__main__": + app.run(host=ADDRESS, port=PORT, debug=DEBUG) diff --git a/app_python/data/visits b/app_python/data/visits new file mode 100644 index 0000000000..86ee83a4a2 --- /dev/null +++ b/app_python/data/visits @@ -0,0 +1 @@ +40 \ No newline at end of file diff --git a/app_python/docker-compose.yml b/app_python/docker-compose.yml new file mode 100644 index 0000000000..eb623d9169 --- /dev/null +++ b/app_python/docker-compose.yml @@ -0,0 +1,13 @@ +version: "3.9" + +services: + app: + build: . + ports: + - "1999:12345" + environment: + - PORT=12345 + - ADDRESS=0.0.0.0 + - DATA_DIR=/app/data + volumes: + - ./data:/app/data \ No newline at end of file diff --git a/app_python/docs/LAB01.md b/app_python/docs/LAB01.md new file mode 100644 index 0000000000..159dae34a3 --- /dev/null +++ b/app_python/docs/LAB01.md @@ -0,0 +1,134 @@ +# Documentation + +## Framework Selection + +I chose Flask because it is quite good for the small projects and easy enough for the beginners, as I didn't have an experience with API before. It also has a lot of community support since it was released long ago. + +| Framework | Features | +|---------|------------| +| Flask | Easy to learn, a lot of documentation | +| FastAPI | Harder to learn, more suitable for big projects (but from reddit comments people seem to like it a lot)| +| Django | Also too heavy for a small project | + +## Best Practices Applied + +- Informative comments + +```python +# decorator for / path +# decorator for /health path +# error handling +``` +- PEP8 standarts with auto linting from black formatter (example is app.py itself) + +- clear structure + +- error handling + +```python +@app.errorhandler(404) +def not_found(error): + return jsonify({"error": "Not Found", "message": "Endpoint does not exist"}), 404 + + +@app.errorhandler(500) +def internal_error(error): + return ( + jsonify( + { + "error": "Internal Server Error", + "message": "An unexpected error occurred", + } + ), + 500, + ) +``` + +- logging +```python +logging.basicConfig( + level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" +) + +logger.info("Application starting...") + +logger.debug(f"Request: {request.method} {request.path}") # inside decorators +``` +- requirements.txt and .gitignore + +## API Documentation + +- The web app can be accesed by this link: http://127.0.0.1:5000 + +### Request/response examples + +- Request Examples: + - GET / HTTP/1.1 + - GET /health HTTP/1.1 +- Response examples + - json and 200 OK code + - {"error":"Not Found","message":"Endpoint does not exist"} 404 not found error + - {"error":"Internal Server Error","message":"An unexpected error occurred"} 500 internal server error + +### Testing commands + +- test urls + - http://127.0.0.1:5000 + - http://127.0.0.1:5000/health + - http://127.0.0.1:5000/unknown +- curl + - curl http://127.0.0.1:5000/unknown + - curl http://127.0.0.1:5000/health + - curl http://127.0.0.1:5000/ + +## Testing Evidence + +### Screenshots + +Screenshots can be found in app/python/docs/screenshots. + +### Terminal output + +#### Curl + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/unknown +{"error":"Not Found","message":"Endpoint does not exist"} + +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/health +{"status":"healthy","timestamp":"2026-01-28T22:32:13.607128","uptime_seconds":352} + +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/ +{"message":{"endpoints":[{"description":"Service information","method":"GET","path":"/"},{"description":"Health check","method":"GET","path":"/health"}],"request":{"client_ip":"127.0.0.1","method":"GET","path":"/","port":5000,"user_agent":"curl/7.81.0"},"runtime":{"current_time":"2026-01-07T14:30:00.000Z","timezone":"UTC","uptime_human":"1 hour, 0 minutes","uptime_seconds":{"human":"0 hours, 0 minutes","seconds":0}},"service":{"debug status":true,"description":"DevOps course info service","framework":"Flask","name":"devops-info-service","version":"1.0.0"},"system":{"architecture":"arm64","cpu_count":8,"hostname":"Veronicas-MacBook-Air.local","platform":"Darwin","platform_version":"Ubuntu 24.04","python_version":"3.12.9"}}} +``` +#### App started + +```bash +devops) fountainer@Veronicas-MacBook-Air app_python % python app.py +2026-01-28 22:36:05,289 - __main__ - INFO - Application starting... + * Serving Flask app 'app' + * Debug mode: off +2026-01-28 22:36:05,299 - werkzeug - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on http://127.0.0.1:5000 +2026-01-28 22:36:05,299 - werkzeug - INFO - Press CTRL+C to quit +2026-01-28 22:36:09,089 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:09] "GET /unknown HTTP/1.1" 404 - +2026-01-28 22:36:12,152 - __main__ - DEBUG - Request: GET / +2026-01-28 22:36:12,153 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:12] "GET / HTTP/1.1" 200 - +2026-01-28 22:36:18,600 - __main__ - DEBUG - Request: GET /health +2026-01-28 22:36:18,607 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:18] "GET /health HTTP/1.1" 200 - +2026-01-28 22:36:20,246 - __main__ - DEBUG - Request: GET /health +2026-01-28 22:36:20,247 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:20] "GET /health HTTP/1.1" 200 - +``` + +## Challenges & Solutions + +- It was quite hard for me to understand the structure since I didn't work with Flask and API in general before. I got some info from stackoverflow, documentations, etc. + +- I have encountered a problem with a jsonify library since I thought it wasn't comming from Flask library. Terminal errors helped to understand it eventually. + +- It took some time to understand how methods from request library work. + +## GitHub Community + +- starring repositories support open-source development and small but promising projects +- following developers lead to the opportunities of communication, exchanging experience, and making connections in the field. diff --git a/app_python/docs/LAB02.md b/app_python/docs/LAB02.md new file mode 100644 index 0000000000..9ae3741628 --- /dev/null +++ b/app_python/docs/LAB02.md @@ -0,0 +1,84 @@ +# Documentation + +## Docker Best Practices Applied + +- COPY commands for copying requirements before copying application code. This is a good practice because this way if we change the application code we won't have to rebuild all dependencies. + +```bash +COPY requirements.txt . +COPY app.py . +``` +- Multi-stage image. Firstly we build an image, then we run commands. It helps to reduce the final image size since all build tools won't be included. Example is the whole Dockerfile. + +- Small base image (python:3.13-slim), this way image builds faster since we don't need to install a giant package. + +- Creating and setting a non-root user. This is good for security since running as root is vulnerable. + +```bash +RUN useradd --create-home --shell /bin/bash newuser +USER newuser +``` +- .dockerignore file. We explicitly exclude files that should not be leaked to the public image (for security). + +- COPY only those files that are really needed (application code and dependencies). + +```bash +COPY requirements.txt . +COPY app.py . +``` +- EXPOSE port for documentation purposes. + +```bash +EXPOSE 12345 +``` + +## Image Information & Decisions + +- I chose python:3.13-slim because it is really small compared to the full version and was released not so long ago (I thought about using a distroless image, but the last available version was 3.12). + +- Final image is 158MB, it is very light (compressed size in dockerhub is even smaller: 47.32 MB) + +- Layer structure: I use multi-stage Dockerfile, so I have two parts: for building and for running. In the build stage I copy all dependencies and then application code, in the run-time stage I set an environment variable for the port, document it, add a new user and create a directory, then copy files with dependencies and application code, and then run commands. This way the time for building is utilised in an effective manner due to the small image, copy commands order, and multi-stage dockerfile. + +## Build & Run Process + +![Build](./screenshots/lab02-shots/build.png) + +![Running](./screenshots/lab02-shots/run.png) + +![Testing](./screenshots/lab02-shots/test.png) + +![Push](./screenshots/lab02-shots/push.png) + +![Pull](./screenshots/lab02-shots/pull.png) + +[Link to the image](https://hub.docker.com/repository/docker/fountainer/my-app/general) + +## Technical Analysis + +### Why does your Dockerfile work the way it does? + +Because Dockerfile provides a clear structure that can be easily followed and suited for your particular needs. It works the way it does because it replicates how the script is running on the host machine. + +### What would happen if you changed the layer order? + +If I change the COPY commands order after I make changes in application code all dependencies will be rebuilt too. If I remove the multi-stage the size of the image will increase. + +### What security considerations did you implement? + +Non-root user, .dockerignore, copying specific files. + +### How does .dockerignore improve your build? + +It excludes the possibility to copy files that contain private information (but we still can forget to add some files so it is better to only copy what we actually need). + +### Challenges & Solutions + +- I faced a problem with port mapping but docker documentation helped. Now I know how to set up the communication between host and container. While I was struggling I tried to debug what processes blocked the 5000 port (and it seemed to be docker itself). + + + + + + + diff --git a/app_python/docs/LAB03.md b/app_python/docs/LAB03.md new file mode 100644 index 0000000000..0585052aa9 --- /dev/null +++ b/app_python/docs/LAB03.md @@ -0,0 +1,97 @@ +# Documentation + +## Overview + +### Choose a Testing Framework + +Since the project is quite simple, it is not a big difference between frameworks regarding their more complex features. I chose pytest because it is easy to use as a beginner and it provides all needed functionality. + +### Test coverage & trigger configuration + +- I check the structure of a json response for ./ and ./health requests, and additionally review error cases (response.status code is 404 or 500). Also, I test the type of the value for importand fields. + +- The pipeline is triggered on push and pull requests. It is useful for me since I can check the pipeline during working on the lab (pushes) and then on pull request for the submission. + +### Versioning Strategy + +- latest tag + +- I chose to use Calendar Versioning since it is quite indicative for the university course (I can easily determine the image corresponding to the particular lab (by the date)). + +- Also I tag by commit SHA for needed image detection + +## Workflow Evidence + +### Tests terminal output + +![Passed tests](./screenshots/lab03-shots/unit%20test%20output.png) + +### Pipeline test with GitHub actions + +[Successful run](https://github.com/ffountainer/DevOps-Core-Course/actions/runs/21885090766) + +![](./screenshots/lab03-shots/pipeline%20success.png) + +### GitHub Image + +[Image](https://hub.docker.com/layers/fountainer/my-app/latest/images/sha256:ec4a12a2a6e91d464be4f1a908f23a3646ed05233d2ae82101357ea1e23bd677?uuid=8c4ce238-1b75-4c64-ba2e-b07167c9cb11%0A) + +## Best Practices Implemented + +- check on pull request to see the status before merging +- secrets for sensitive data +- docker image layer caching for quicker builds +- job dependencies (so docker build and push do not run if unit tests have not passed) +- add status badge to immediately see the current pipeline status +- dependency caching for quicker unit testing +- security scanning with Snyk for detecting vulnerabilities + +![Improvements](./screenshots/lab03-shots/improved%20perf.png) + +### Caching implementation and speed improvement metrics + +Caching is enabled by actions/setup-python@v5 and Docker layer caching, reducing dependency installation and image rebuild times. This decreased average workflow time from ~2-1 min to ~1-0.4 min (~40–60% faster on subsequent runs). + +![DockerHub images](./screenshots/lab03-shots/images%20with%20tags%20docker%20hub.png) + +### Snyk integration + +Initially there was a vulnerability with outdated flask version, so I upgraded it. + +## Key Decisions + +### Versioning Strategy + +- I chose to use Calendar Versioning since it is quite indicative for the university course (I can easily determine the image corresponding to the particular lab (by the date)). + +### Docker Tags + +- CI creates CalVer, latest, and SHA tags. + +### Workflow Triggers + +- The pipeline is triggered on push and pull requests. It is useful for me since I can check the pipeline during working on the lab (pushes) and then on pull request for the submission. + +### Test Coverage + +I check the structure of a json response for ./ and ./health requests, and additionally review error cases (response.status code is 404 or 500). Also, I test the type of the value for importand fields. + +What is not tested: + - if env variables provide correct values + +### Chosen actions + +- actions/checkout@v4: to check-out my repository under $GITHUB_WORKSPACE, so my workflow can access it + +- actions/setup-python@v5: to install python and add it to path and to allow caching + +- docker/login-action@v3: to login into docker + +- docker/setup-buildx-action@v3: to enable layer caching + +- docker/build-push-action@v5: to build and push image + +## Challenges + +- didn't work with unit testing and assert command before +- snyk didn't find my files so I stopped using snyk action and configured job manually \ No newline at end of file diff --git a/app_python/docs/LAB04.md b/app_python/docs/LAB04.md new file mode 100644 index 0000000000..ab38156561 --- /dev/null +++ b/app_python/docs/LAB04.md @@ -0,0 +1,198 @@ +# Documentation + +## Cloud provider and infrastructure + +### Cloud provider chosen and rationale + +- I chose Yandex Cloud since it provides solid resource provision with a lot of RAM and storage space, also it has a free trial period. + +### Instance type/size and why + +- 2 cpu cores and 2 gb of ram because the project is really small and we don't need a lot of resources for it. + +### Region/zone selected + +- zone ru-central1-a + +### Resources created (list all) + +- service account + +- boot disk with ubuntu image + +- vm (2 cores, 2 memory) + +- network + +- subnet (zone ru-central1-a) + +- security group + +## Task 1 (Terraform implementation) + +### Terraform version used + +Terraform v1.14.5 on darwin_arm64 + +### Project structure explanation + +``` +app_python/ +├── pulumi/ +│ ├── venv/ # Python virtual environment +│ ├── Pulumi.yaml # Pulumi project metadata +│ ├── Pulumi.dev.yaml # Stack config (gitignored) +│ ├── requirements.txt # Python dependencies +│ ├── __main__.py # Pulumi infrastructure code +| ├── .gitignore # Ignore state, credentials +│ └── README.md # Pulumi setup instructions +└── terraform/ + ├── .gitignore # Ignore state, credentials + ├── main.tf # Main resources + ├── variables.tf # Input variables + ├── outputs.tf # Output values + ├── terraform.tfvars # Variable values (gitignored) + └── README.md # Terraform setup instructions +``` + +### Key configuration decisions + +- connect with pair of SSH keys +- configure security group rules +- expose only necessary ports +- do not expose credentials + +### Challenges encountered + +Getting accustomed to the Yandex Cloud wasn't easy, and setting up SSH connection also was a little challanging. + +### Public IP address of created VM + +```bash +external_ip_address_vm = "62.84.117.91" +``` + +### SSH connection command + +```bash +ssh -i /home-directory/.ssh/terraform-vm-key ubuntu@62.84.117.91 +``` + +### Terminal output from terraform plan and terraform apply + +![Plan](./screenshots/lab04-shots/terraform%20plan.png) + +![Apply-1](./screenshots/lab04-shots/terraform%20apply-1.png) + +![Apply-2](./screenshots/lab04-shots/terraform%20apply-2.png) + +### Proof of SSH access to VM + +![SSH](./screenshots/lab04-shots/ssh%20output%20terraform.png) + +## Task 2 (Pulumi Implementation) + +### Pulumi version and language used + +- pulumi==3.222.0, python + +### Terraform destroy output + +![](./screenshots/lab04-shots/terraform%20destroy.png) + +### How code differs from Terraform + +- Palumi supports many programming language for configuration, while terraform only lets to use its default config language. + +### Advantages you discovered + +- It is easier to write in a familiar language in Palumi, but to be honest, I enjoyed writing terraform config more. + +### Challenges encountered + +- Had problems with SSH as well, also it wasn't so easy to find documentation and guides. + +### Terminal output + +- pulumi preview + +![](./screenshots/lab04-shots/pulumi%20preview.png) + +- pulumi up +![](./screenshots/lab04-shots/pulumi%20up.png) + +- SSH connection to VM + +![](./screenshots/lab04-shots/pulumi%20ssh.png) + +### Public IP of Pulumi-created VM + +```bash +(venv) fountainer@Veronicas-MacBook-Air pulumi % pulumi stack output + +Enter your passphrase to unlock config/secrets + (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember): +Enter your passphrase to unlock config/secrets +Current stack outputs (2): + OUTPUT VALUE + external_ip 93.77.185.195 + internal_ip 192.168.10.5 +``` +### SSH connection + +(I reused the key from the terraform config) + +```bash +ssh -i ~/.ssh/terraform-vm-key ubuntu@93.77.185.195 +``` + +![](./screenshots/lab04-shots/pulumi%20ssh.png) + +## Terraform vs Pulumi Comparison + +### Ease of Learning: Which was easier to learn and why? + +- Documentation for Terraform was easier to find (for me), but Polumi was more familiar to work with. I also felt like Polumni's config is less complex. + +### Code Readability: Which is more readable for you? + +- Terraform + +### Debugging: Which was easier to debug when things went wrong? + +- Terraform, plan command was really useful + +### Documentation: Which has better docs and examples? + +- Terraform + +### Use Case: When would you use Terraform? When Pulumi? + +- Pulumi is good for projects where you want to employ features that only conplex programming languages can provide. Terraform is good for standardized infrastructure. + +### Code differences (HCL vs Python/TypeScript) + +- Terraform uses declarative HCL with blocks and attributes, while Pulumi uses imperative Python/TypeScript with function calls, objects, and full programming language features for resource creation and logic. + +### Which tool you prefer and why + +- I liked Terraform more (partially because on this step I was creating a vm in the cloud for the first time, but doing the same with Palumi was quite boring), I like the declarative languages, and they seem to be intuitively understandable. + +## Lab 5 Preparation & Cleanup + +### Are you keeping your VM for Lab 5? (Yes/No) + +- Yes + +### If yes: Which VM (Terraform or Pulumi created)? + +- Palumi, but I kind of contemplating on returning to Terraform... + +### VM Status + +![](./screenshots/lab04-shots/vm%20status.png) + + + + + diff --git a/app_python/docs/LAB05.md b/app_python/docs/LAB05.md new file mode 100644 index 0000000000..f18d4db760 --- /dev/null +++ b/app_python/docs/LAB05.md @@ -0,0 +1,306 @@ +# Documentation + +## Architecture Overview + +### Ansible version used + +core 2.20.2 + +### Target VM OS and version + +linux, ubuntu-2204-lts + +### Role structure diagram or explanation + +``` +roles/ +├─ web_app/ # deploying the application +│ ├─ defaults/main.yml # default variables: container_port, restart_policy, app_env, etc +│ ├─ handlers/main.yml # handlers, for example, restart container +│ └─ tasks/main.yml # deployment tasks +│ +├─ docker/ # docker setup/configuration +│ ├─ defaults/main.yml # defaults for docker config +│ ├─ handlers/main.yml # docker handlers +│ └─ tasks/main.yml # tasks to install and configure docker +│ +├─ common/ # general tasks +│ ├─ defaults/main.yml # default variables for common tasks +│ ├─ handlers/main.yml # handlers for common tasks +│ └─ tasks/main.yml # general-purpose tasks shared across roles +``` + +### Why roles instead of monolithic playbooks? + +- Roles can be used across different projects, thay are easy to configure and control, as well as to change if needed. + +## Roles Documentation + +### Common + +- Purpose: installs common packages and sets system timezone +- Variables: common_packages (default: vim, git, curl, python3-pip, htop), timezone_name (default: Europe/Moscow) +- Handlers: doesn't have +- Dependencies: doesn't have + +### Docker + +- Purpose: installs and configures docker engine, ensures service is running, adds user to docker group, and sets up python for ansible +- Variables: docker_packages (docker-ce, docker-ce-cli, containerd.io), docker_user (default: ubuntu), docker_gpg_url, docker_repo +- Handlers: restart docker – restarts docker service when notified +- Dependencies: doesn't have + +### web_app + +- Purpose: deploys the application container: logs in to Docker Hub, pulls the image, stops/removes old container, runs new container, waits for it to be ready, and verifies health +- Variables: Vaulted: dockerhub_username, dockerhub_password, app_name, docker_image_tag, app_port, app_container_name; Defaults: container_port, restart_policy, app_env +- Handlers: restart app container – restarts the application container when notified +- Dependencies: depends on Docker being installed and configured (accomplished with docker role) + +## Idempotency Demonstration + +### Terminal output from FIRST provision.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml + +PLAY [Provision web servers] ********************************************************************************************************************************************************* + +TASK [Gathering Facts] *************************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Update apt cache] ***************************************************************************************************************************************************** +changed: [terraform] + +TASK [common : Install common packages] ********************************************************************************************************************************************** +changed: [terraform] + +TASK [common : Ensure timezone is set] *********************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Install prerequisites] ************************************************************************************************************************************************ +ok: [terraform] + +TASK [docker : Add Docker GPG key] *************************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Add Docker APT repository] ******************************************************************************************************************************************** +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +TASK [docker : Install Docker packages] ********************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Ensure Docker service is running] ************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] ********************************************************************************************************************************************* +changed: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ******************************************************************************************************************** +changed: [terraform] + +RUNNING HANDLER [docker : restart docker] ******************************************************************************************************************************************** +changed: [terraform] + +PLAY RECAP *************************************************************************************************************************************************************************** +terraform : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Terminal output from SECOND provision.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml + +PLAY [Provision web servers] ********************************************************************************************************************************************************* + +TASK [Gathering Facts] *************************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Update apt cache] ***************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Install common packages] ********************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Ensure timezone is set] *********************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ************************************************************************************************************************************************ +ok: [terraform] + +TASK [docker : Add Docker GPG key] *************************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ******************************************************************************************************************************************** +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] ********************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Ensure Docker service is running] ************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] ********************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ******************************************************************************************************************** +ok: [terraform] + +PLAY RECAP *************************************************************************************************************************************************************************** +terraform : ok=11 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Analysis: What changed first time? What didn't change second time? + +- first run made actual changes to install and configure packages/services; second run found everything already in desired state, so no changes + +### What makes your roles idempotent? + +- all tasks use Ansible modules that check state before making changes, ensuring applying the same playbook multiple times does not alter already-correct configuration + +## Ansible Vault Usage + +### How you store credentials securely + +- secrets like dockerhub username/token etc are saved in encrypted files (group_vars/all.yml) using ansible-vault create + +### Vault password management strategy + +- use a dedicated vault password file (~/.vault_pass.txt) or just type password manually with --ask-vault-pass + +### Example of encrypted file (show it's encrypted!) + +![](./screenshots/lab05-shots/encrypted.png) + +### Why Ansible Vault is important + +- keeps sensitive data safe, prevents accidental exposure in repos or logs, allows secure automation + + +## Deployment Verification + +### Terminal output from deploy.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] **************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Show Docker password] ************************************************************************************************* +ok: [terraform] => { + "container_port": "12345" +} + +TASK [web_app : Log in to Docker Hub] ************************************************************************************************* +ok: [terraform] + +TASK [web_app : Pull Docker image] **************************************************************************************************** +ok: [terraform] + +TASK [web_app : Stop existing container (if running)] ********************************************************************************* +changed: [terraform] + +TASK [web_app : Remove old container (if exists)] ************************************************************************************* +changed: [terraform] + +TASK [web_app : Run new container] **************************************************************************************************** +changed: [terraform] + +TASK [web_app : Wait for application to be ready] ************************************************************************************* +ok: [terraform] + +TASK [web_app : Verify health endpoint] *********************************************************************************************** +ok: [terraform] + +RUNNING HANDLER [web_app : restart app container] ************************************************************************************* +ok: [terraform] + +PLAY RECAP ******************************************************************************************************************************* +terraform : ok=10 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +``` + +### Container status: docker ps output + +```bash +Last login: Thu Feb 26 23:56:36 2026 from 45.12.151.45 +ubuntu@fhmebroid75qocec3dc3:~$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +bf2d264719af fountainer/my-app:latest "python app.py" About a minute ago Up About a minute 0.0.0.0:8080->12345/tcp my-app +ubuntu@fhmebroid75qocec3dc3:~$ +``` + +### Health check verification: curl outputs + +```bash +ubuntu@fhmebroid75qocec3dc3:~$ curl http://127.0.0.1:8080/health +{ + "status": "healthy", + "timestamp": "2026-02-26T20:58:46.335570", + "uptime_seconds": 134 +} +``` + +### Handler execution (if any) + +```bash +RUNNING HANDLER [web_app : restart app container] ************************************************************************************* +ok: [terraform] +``` + +## Key Decisions + +### Why use roles instead of plain playbooks? + +- roles can be used across different projects, thay are easy to configure and control, as well as to change if needed + +### How do roles improve reusability? + +- roles define tasks, defaults, and handlers so they can be reused across multiple playbooks or projects + +### What makes a task idempotent? + +- changes are made only when a system is not already in a needed state + +### How do handlers improve efficiency? + +- handlers run only when notified, avoiding repeated or unnecessary actions like service restarts + +### Why is Ansible Vault necessary? + +- vault encrypts sensitive data (passwords, tokens) from ending up in playbooks, logs, or git + +## Challenges + +- it was really hard for me to configure the internet access from my pulumi VM on yandex cloud. turned out it was kinda impossible so I returned to my terraform VM + +- while trying to run ```bash ansible-playbook playbooks/deploy.yml --ask-vault-pass``` ansible didn't see my vaulted variables \ No newline at end of file diff --git a/app_python/docs/LAB06.md b/app_python/docs/LAB06.md new file mode 100644 index 0000000000..6d86869cca --- /dev/null +++ b/app_python/docs/LAB06.md @@ -0,0 +1,868 @@ +# Documentation + +## Overview (What you accomplished and technologies used) + +- My stack consisted of Ansible core 2.20.2, Docker Compose 2.39.2, Jinja 3.1.6, python 3.11.9. +- I've accomplished refactoring the roles for using with docker-compose, implemented a wipe logic, and set up a new CI/CD worflow. + +## Blocks & Tags (Block usage in each role, tag strategy, execution examples with screenshots) + +All tags are made for quick understanding of the block usage. + +### Common role + +- block with package installation tasks (tag: packages) +- block with user creation (tag: users) +- block with timezone set up (tag: timezone) + +### Docker role + +- block with docker installation and set up (tag: docker_install) +- block with docker configuration (tag: docker_config) + +### Web-app role + +- block for app deployment with docker compose and possible wipe logic (from wipe.yaml with tag web_app_wipe), (tags: app_deploy, compose) + +## Screenshots and terminal outputs + +### Test provision with only docker +![](./screenshots/lab06-shots/Test%20provision%20with%20only%20docker.png) + +### Skip common role +![](./screenshots/lab06-shots/Skip%20common%20role.png) + +### Install packages only across all roles +![](./screenshots/lab06-shots/Install%20packages%20only%20across%20all%20roles.png) + +### Check mode to see what would run +![](./screenshots/lab06-shots/Check%20mode%20to%20see%20what%20would%20run.png) + +### Run only docker installation tasks +![](./screenshots/lab06-shots/Run%20only%20docker%20installation%20tasks.png) + +### Error handling with rescue block triggered + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml --tags "docker_install" + +PLAY [Provision web servers] ******************************************************************************************************************************************** + +TASK [Gathering Facts] ************************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] *********************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ************************************************************************************************************************************** +[ERROR]: Task failed: Module failed: unknown url type: 'blabla' +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/tasks/main.yml:14:7 + +12 state: present +13 update_cache: yes +14 - name: Add Docker GPG key + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "unknown url type: 'blabla'", "status": -1, "url": "blabla"} + +TASK [docker : Wait before retry] *************************************************************************************************************************************** +Pausing for 10 seconds +(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort) +ok: [terraform] + +TASK [docker : Retry apt update] **************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Retry adding Docker GPG key] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ************************************************************************************************************************ +ok: [terraform] + +PLAY RECAP ************************************************************************************************************************************************************** +terraform : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0 +``` + +### List all available tags + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml --list-tags + +playbook: playbooks/provision.yml + + play #1 (webservers): Provision web servers TAGS: [] + TASK TAGS: [common, docker, docker_config, docker_install, packages, timezone, users] +``` + +## Docker Compose Migration (Template structure, role dependencies, before/after comparison) + +### Comparison + +- docker-compose provides richer functionality to set up env variables, dependencies, and configs, compared to just the docker container + +- docker-compose.yaml.j2 + +```yml +version: '{{ docker_compose_version }}' + +services: + {{ app_name }}: + image: {{ web_app_docker_image }}:{{ web_app_docker_tag }} + + ports: + - "{{ app_port }}:{{ app_internal_port }}" + + restart: unless-stopped + + environment: +{% for key, value in web_app_env.items() %} + {{ key }}: "{{ value }}" +{% endfor %} + + networks: + - {{ app_name }}_network + +networks: + {{ app_name }}_network: + driver: bridge +``` + +### First run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Create application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:33:18 + +31 - name: Log deployment attempt +32 copy: +33 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=12 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 +``` + +### Second run removed "changed" status of some fields (not with the current time) + +```bash +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] +``` + +### Verifying on target VM + +```bash +ubuntu@fhmebroid75qocec3dc3:~$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +7c279d0b3c18 fountainer/my-app:latest "python app.py" 10 minutes ago Up 10 minutes 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +0a0701a21e3a 9582a1fe4631 "python app.py" 25 hours ago Up 25 hours 0.0.0.0:8080->12345/tcp my-app +ubuntu@fhmebroid75qocec3dc3:~$ curl http://localhost:1999 +{ + "message": { + "endpoints": [ + { + "description": "Service information", + "method": "GET", + "path": "/" + }, + { + "description": "Health check", + "method": "GET", + "path": "/health" + } + ], + "request": { + "client_ip": "127.0.0.1", + "method": "GET", + "path": "/", + "port": 12345, + "user_agent": "curl/7.81.0" + }, + "runtime": { + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + "uptime_human": "1 hour, 0 minutes", + "uptime_seconds": { + "human": "0 hours, 0 minutes", + "seconds": 0 + } + }, + "service": { + "debug status": true, + "description": "DevOps course info service", + "framework": "Flask", + "name": "devops-info-service", + "version": "1.0.0" + }, + "system": { + "architecture": "x86_64", + "cpu_count": 8, + "hostname": "7c279d0b3c18", + "platform": "Linux", + "platform_version": "Ubuntu 24.04", + "python_version": "3.13.12" + } + } +} +ubuntu@fhmebroid75qocec3dc3:~$ +``` + +## Wipe Logic (Implementation details, variable + tag approach, test results) + +- Implementation details can be seen in ansible/roles/web-app/tasks/wipe.yml. + +- The tag is web_app_wipe, the task is triggered by "when: web_app_wipe | bool" + +- Variables: "{{ web_app_compose_project_dir }}", "{{ ansible_check_mode }}", {{ app_name }} + +### Scenario 1: Normal deployment (wipe should NOT run) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Create application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +skipping: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:38:18 + +36 - name: Log deployment attempt +37 copy: +38 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=13 changed=1 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +7c279d0b3c18 fountainer/my-app:latest "python app.py" 21 minutes ago Up 21 minutes 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +0a0701a21e3a 9582a1fe4631 "python app.py" 25 hours ago Up 25 hours 0.0.0.0:8080->12345/tcp my-app +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Scenario 2: Wipe only (remove existing deployment) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[ERROR]: Task failed: Module failed: "/opt/my-app" is not a directory +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml:5:7 + +3 block: +4 +5 - name: Stop and remove containers + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "\"/opt/my-app\" is not a directory"} +...ignoring + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "ls /opt" +containerd +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Scenario 3: Clean reinstallation (wipe → deploy) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[ERROR]: Task failed: Module failed: "/opt/my-app" is not a directory +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml:5:7 + +3 block: +4 +5 - name: Stop and remove containers + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "\"/opt/my-app\" is not a directory"} +...ignoring + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +TASK [web_app : Create application directory] ********************************************************************************************************************* +changed: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:37:18 + +35 - name: Log deployment attempt +36 copy: +37 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=17 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +167ea730bdc1 fountainer/my-app:latest "python app.py" 20 seconds ago Up 19 seconds 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +``` +### Scenario 4: Safety checks (should NOT wipe) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +skipping: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +skipping: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=2 changed=0 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +changed: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 +``` + +### App running after complete reinstall + +![](./screenshots/lab06-shots/app%20running%20after%20clean%20reinstall.png) + + +## CI/CD Integration (Workflow architecture, setup steps, evidence of automated deployments) + +### Workflow + +- this workflow runs ansible-lint on pushes and pr’s first to catch errors, then deploys the app automatically on master/lab06 branches + +### Setup steps + +- setup steps include checking out code, installing python and ansible, configuring ssh with github secrets, decoding vault vars, and running the playbook remotely + +### Evidence + +- evidence of automated deployment is in the deploy job: it ssh’s to the vm, runs ansible-playbook with vault and extra vars, then verifies the app with curl requests + +![](./screenshots/lab06-shots/ci:cd%20success.png) + +Terminal output: + +```bash +ssh -i ~/.ssh/terraform-vm-key ubuntu@*** "echo connected" + + echo '***' > /tmp/vault_pass + cd app_python/ansible + + ansible-playbook playbooks/deploy.yml \ + -i inventory/hosts.ini \ + --vault-password-file /tmp/vault_pass \ + --extra-vars @./group_vars/all.yml + + rm /tmp/vault_pass + shell: /usr/bin/bash -e {0} + env: + pythonLocation: /opt/hostedtoolcache/Python/3.12.12/x64 + PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib/pkgconfig + Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib +connected + +PLAY [Deploy application] ****************************************************** + +TASK [Gathering Facts] ********************************************************* +ok: [terraform] + +TASK [docker : Install prerequisites] ****************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ********************************************* +ok: [terraform] +Warning: : Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. + +TASK [docker : Add Docker APT repository] ************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:8:14 + +6 docker_user: "ubuntu" +7 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +8 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] **************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************** +ok: [terraform] + +TASK [docker : Ensure docker service is enabled] ******************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] *************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************** +included: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] ************************************ +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] ************************************ +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************** +skipping: [terraform] + +TASK [web_app : Log wipe completion] ******************************************* +skipping: [terraform] + +TASK [web_app : Create application directory] ********************************** +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] *********************************** +ok: [terraform] + +TASK [web_app : Deploy with Docker Compose] ************************************ +Warning: : Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] + +TASK [web_app : Log deployment attempt] **************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:41:18 + +39 - name: Log deployment attempt +40 ansible.builtin.copy: +41 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ********************************************************************* +terraform : ok=13 changed=1 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + + sleep 10 + curl -f http://***:1999 || exit 1 + curl -f http://***:1999/health || exit 1 + shell: /usr/bin/bash -e {0} + env: + pythonLocation: /opt/hostedtoolcache/Python/3.12.12/x64 + PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib/pkgconfig + Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed + + 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 + 0 1058 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 +100 1058 100 1058 0 0 4059 0 --:--:-- --:--:-- --:--:-- 4053 +{ + "message": { + "endpoints": [ + { + "description": "Service information", + "method": "GET", + "path": "/" + }, + { + "description": "Health check", + "method": "GET", + "path": "/health" + } + ], + "request": { + "client_ip": "127.0.0.1", + "method": "GET", + "path": "/", + "port": 12345, + "user_agent": "curl/7.81.0" + }, + "runtime": { + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + "uptime_human": "1 hour, 0 minutes", + "uptime_seconds": { + "human": "0 hours, 0 minutes", + "seconds": 0 + } + }, + "service": { + "debug status": true, + "description": "DevOps course info service", + "framework": "Flask", + "name": "devops-info-service", + "version": "1.0.0" + }, + "system": { + "architecture": "x86_64", + "cpu_count": 8, + "hostname": "8081285df89f", + "platform": "Linux", + "platform_version": "Ubuntu 24.04", + "python_version": "3.13.12" + } + } +} + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed + + 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 +100 99 100 99 0 0 364 0 --:--:-- --:--:-- --:--:-- 365 +{ + "status": "healthy", + "timestamp": "2026-03-07T16:04:23.506918", + "uptime_seconds": 146470 +} +``` + +## Testing Results (All test scenarios, idempotency verification, application accessibility) + +- all testing results are provided in the corresponding sections above + +## Challenges & Solutions (Difficulties encountered and how you solved them) + +- I had a hard time making ssh connection work in the github actions and wasted 2 days after the deadline for this... it was eventually solved by generating new ssh pair and coniguring vm's authorised keys again. + +## Research Answers (All research questions answered with analysis) + +### 1.3 + +Q1: What happens if rescue block also fails? + +- if the rescue block fails, ansible just shows an error and moves on to the always block. it won’t stop the playbook completely + +Q2: Can you have nested blocks? + +- yes, you can nest blocks inside other blocks, each with their own rescue/always if needed + +Q3: How do tags inherit to tasks within blocks? + +- tags on a block automatically apply to all tasks inside, but you can override or add extra tags per task + +### 2.3 + +Q1: What's the difference between restart: always and restart: unless-stopped? + +- restart: always makes the container restart no matter what, even if you stop it manually. unless-stopped restarts it only if it crashes or docker restarts, but not if you stopped it yourself + +Q2: How do Docker Compose networks differ from Docker bridge networks? + +- docker compose networks are defined per project and let containers talk using service names. bridge networks are default docker networks, simpler and not tied to a compose project + +Q3: Can you reference Ansible Vault variables in the template? + +- yes, you can use ansible vault variables in templates by referencing them like any other ansible variable ({{ vault_var_name }}) + +### 2.5 + +Q1: Look up community.docker.docker_compose_v2 module + +- community.docker.docker_compose_v2 manages compose projects using docker compose v2 cli under the hood, can pull images, recreate services, and set project source + +Q2: Compare state: present vs other state options + +- state: present makes sure services are running, absent removes them, stopped just stops without removing, restarted forces a restart + +Q3: Understand recreate parameter options + +- recreate: auto only recreates changed containers, never won’t recreate, force always recreates even if unchanged + +### 3.6 + +Q1: Why use both variable AND tag? (Double safety mechanism) + +- using both variable and tag is double safety: the variable controls behavior in code, tag controls execution from command line + +Q2: What's the difference between never tag and this approach? + +- never tag completely ignores tasks unless explicitly forced, this approach lets you selectively run blocks with normal tags + +Q3: Why must wipe logic come BEFORE deployment in main.yml? (Clean reinstall scenario) + +- wipe logic must run first to remove old containers/configs so deployment starts clean, avoids conflicts or leftover data + +Q4: When would you want clean reinstallation vs. rolling update? + +- clean reinstall is good if configs or images changed, rolling update is better for small changes with minimal downtime + +Q5: How would you extend this to wipe Docker images and volumes too? + +- extend wipe by adding tasks that remove docker images (docker_image module) and volumes (docker_volume module) before deployment + +### 4.10 + +Q1: What are the security implications of storing SSH keys in GitHub Secrets? + +- storing ssh keys in github secrets is safe if encrypted, but exposure risk exists if repo or workflows are misconfigured + +Q2: How would you implement a staging → production deployment pipeline? + +- implement staging → production by having two environments, separate compose dirs, and deploy to staging first, then promote to production after tests pass + +Q3: What would you add to make rollbacks possible? + +- for rollbacks, keep previous compose files and image tags, and add tasks to revert to last known good version if deployment fails + +Q4: How does self-hosted runner improve security compared to GitHub-hosted? + +- self-hosted runners limit exposure because the runner machine is under your control, unlike github-hosted where VM is shared and short-lived \ No newline at end of file diff --git a/app_python/docs/LAB07.md b/app_python/docs/LAB07.md new file mode 100644 index 0000000000..f6df6dcf12 --- /dev/null +++ b/app_python/docs/LAB07.md @@ -0,0 +1,260 @@ +# Documentation + +## Architecture (diagram showing how components connect, and the data flow) + +```mermaid +flowchart LR + +subgraph Docker_Network["Docker Compose Network"] + + A[Promtail
port 9080
scrapes logs] + + B[Loki
port 3100
stores logs] + + C[Grafana
port 3000
visualizes logs] + +end + +D[Docker Engine
/var/lib/docker/containers
/var/run/docker.sock] + +D -->|discover containers
read logs| A + +A -->|push logs
http://loki:3100/loki/api/v1/push| B + +C -->|query logs
http://loki:3100| B +``` + +## Setup Guide (step-by-step deployment instructions) + +```bash +# make sure your docker compose is working +docker compose version +``` +```bash +# enter the monitoring directory +cd app_python/monitoring +``` +```bash +# deploy the containers +docker compose up -d +``` +Important! Make sure you have your .env file with secrets GF_PASSWORD and GF_EMAIL for Grafana authorization. + +## Configuration (explain your Loki/Promtail configs and why) + +### Configuration file snippets for Loki + +```bash +auth_enabled: false +# no authentication required +``` + +```bash +server: + http_listen_port: 3100 +# loki is listening on port 3100 +``` + +```bash +# common values are shared among all modules if not redefined explicitly +common: + path_prefix: /loki + storage: + # filesystem object store + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + # number of instances + replication_factor: 1 + # hashes are stored in ram + ring: + kvstore: + store: inmemory +``` + +```bash +# configures the schema for chunk index +schema_config: + configs: + # index buckets are created from this date + - from: 2026-03-01 + # index type + store: tsdb + object_store: filesystem + schema: v13 + index: + # prefix for all created indices + prefix: index_ + # the index is remade every 24 hours + period: 24h +``` + +```bash +# storage config for chunks +storage_config: + filesystem: + directory: /loki/chunks +``` +```bash +# logs are stored for 168 hours and then discarded +limits_config: + retention_period: 168h +``` + +```bash +# compactor merges small index shards for performance and deletes old logs +compactor: + working_directory: /loki/compactor + retention_enabled: true +``` + +### Configuration file snippets for Promtail + +```bash +server: + # listening port for promtail itself + http_listen_port: 9080 +``` +```bash +# positions help promtail to identify where it left of while reading the file +positions: + filename: "/tmp/positions.yaml" + sync_period: 10s + ignore_invalid_yaml: false +``` +```bash +# where promtail will push logs to +clients: + - url: http://loki:3100/loki/api/v1/push +``` +```bash +# discovery configs +scrape_configs: + - job_name: docker + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + # relabeling + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + # regex helps to remove / from container names + regex: '/(.*)' + target_label: 'container' +``` + +## Application Logging (how you implemented JSON logging) + +I implemented logging using JsonFormatter from pythonjsonlogger.json. It has basic configured fields and I can add extra fields relating to the request from dedicated functions that handle connections. + +### Screenshot of JSON log output from your app + +![](./screenshots/lab07-shots/logs%20from%20the%20app.png) + +## Dashboard (explain each panel and the LogQL queries) + +### Screenshot showing logs from at least 3 containers in Grafana Explore + +![](./screenshots/lab07-shots/grafana_logs_3_containers.png) + +### Screenshots of Grafana showing logs from the app + +![](./screenshots/lab07-shots/grafana-app-name.png) + +![](./screenshots/lab07-shots/grafana-error.png) + +![](./screenshots/lab07-shots/grafana_get.png) + +### Screenshot of your dashboard showing all 4 panels with real data. + +I have 4 panels: +- All collected logs ("Logs from all apps") +- The rate of getting logs ("Logs rate") +- A pie chart that shows the relative size of logs with different errors ("Level statistics") +- All collected error logs ("Error logs") + +![](./screenshots/lab07-shots/grafana%20dashboard.png) + +### Example LogQL queries with explanations (At least 3 different LogQL queries that work) + +- all logs from the app: +- - {service_name="app-python"} +- access all logs from the app which level is error: +- - {service_name="app-python"} |= "ERROR" +- all logs where request method was GET: +- - {service_name="app-python"} | json | METHOD = `GET` +- relative size of logs by levels: +- - sum by (level) (count_over_time({service_name=~"app-.*"} | json [60m])) +- rate of logs: +- - sum by (service_name) (rate({service_name=~"app-.*"} [120m])) +- error logs: +- - {service_name=~"app-.*"} | json | LEVEL="ERROR" + +## Production Config (security measures, resources, retention) + +### Configuration file snippets + +```bash +# healthchecks for loki and grafana to verify that containers are running and everything is okay +healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s +``` + +```bash +# resources limits: promtail, grafana, and app_python are pretty lightweight, but loki needs a lot of memory to store logs +deploy: + resources: + limits: + cpus: "1.50" + memory: 2G + reservations: + cpus: "0.50" + memory: 512M +``` + +```bash +# now grafana requires login authorization, it is configured with .env file with environment variables +environment: + - GF_AUTH_ANONYMOUS_ENABLED=false + - GF_SECURITY_ADMIN_PASSWORD=${GF_PASSWORD} + - GF_SECURITY_ADMIN_EMAIL=${GF_EMAIL} +``` + +### docker-compose ps showing all services healthy + +![](./screenshots/lab07-shots/healthy%20services.png) + +### Screenshot of Grafana login page (no anonymous access) + +![](./screenshots/lab07-shots/grafana_login.png) + +## Testing (commands to verify everything works) + +```bash +# check that the containers are up and healthy +docker compose ps +``` + +```bash +# check loki +curl http://localhost:3100/ready +``` + +```bash +# check promtail +curl http://localhost:9080/targets +``` + +```bash +# check grafana +open http://localhost:3000 +``` + +## Challenges (problems you encountered and solutions) + +- It was pretty hard for me to set up logging but after a long research it was done (I used python documentation and some guides to work out how JsonFormatter works with/without logging.BasicConfig()) + +- Also I was very confused about how to make promtail keep logs from a service with a specific label (I researched Stack Overflow and used AI a bit for this one) \ No newline at end of file diff --git a/app_python/docs/LAB08.md b/app_python/docs/LAB08.md new file mode 100644 index 0000000000..c9afdd3a36 --- /dev/null +++ b/app_python/docs/LAB08.md @@ -0,0 +1,144 @@ +# Documentation + +## Architecture - Diagram showing metric flow (app → Prometheus → Grafana) + +```mermaid +flowchart LR + +subgraph Docker_Network["Docker Compose Network"] + + A[app-python
exposes /metrics] + + B[Prometheus
scrapes metrics
stores time-series data] + + C[Grafana
port 3000
visualizes metrics] + +end + +A -->|exposes metrics endpoint
http://app-python:12345/metrics| B + +B -->|PromQL queries
data source| C +``` + + +### Comparison: metrics vs logs (Lab 7) - when to use each + +- we use logs to see what has happened and metric to see the quantities: how much and how often. + +## Application Instrumentation - What metrics you added and why + +### Screenshot of /metrics endpoint output + +![](./screenshots/lab08-shots/metrics%20for%20app%20logs.png) + +### Code showing metric definitions + +- you can see it in app.py +- sections: imports updated, Counter, Histogram, Gauge metrics defined, before request/after request decorators, metrics route decorator + +### Documentation explaining your metric choices + +- http_requests_total counts how many requests hit the service, so i can see overall traffic and usage patterns +- http_request_duration_seconds measures how long requests take, which helps spot slow endpoints or performance issues +- http_requests_in_progress tracks how many requests are being processed right now, useful for detecting load spikes +- endpoint_calls tracks how often each endpoint is used, so i can understand which parts of the service are actually used +- system_info_duration measures how long it takes to collect system info, mainly to check if that logic becomes slow over time + +## Prometheus Configuration + +### Screenshot of /targets page showing all targets UP + +![](./screenshots/lab08-shots/prometheous%20targets.png) + +### Screenshot of a successful PromQL query + +![](./screenshots/lab08-shots/successful%20query.png) + +### prometheus.yml configuration file + +- you can find it in ./app_python_monitoring/prometheous/prometheous.yml + +## Dashboard Walkthrough + +### Each panel's purpose and query + +- request rate — shows how many requests/sec the service gets per endpoint +[query: sum(rate(http_requests_total[5m])) by (endpoint)] + +- error rate — shows how many failed requests (4xx/5xx) happen per second +[query: sum(rate(http_requests_total{status_code=~"[45].."}[5m]))] + +- p95 latency — shows how slow requests are (95th percentile) +[query: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))] + +- latency heatmap — shows distribution of request durations +[query: rate(http_request_duration_seconds_bucket[5m])] + +- active requests — shows how many requests are currently being processed +[query: http_requests_in_progress] + +- status codes — shows distribution of responses (2xx, 4xx, 5xx) +[query: sum by (status_code) (rate(http_requests_total[5m]))] + +- service health — shows if the service is up (1) or down (0) +[query: up{job="app"}] + +### Screenshots of dashboards with live data (all 6+ panels working) + +![](./screenshots/lab08-shots/dashboard1.png) +![](./screenshots/lab08-shots/dashboard2.png) + +### Exported dashboard JSON file + +- you can find the file in ./app_python/monitoring/prometheus/dashboard.json + +## PromQL Examples - 5+ queries with explanations + +### PromQL queries that demonstrate RED method + +### 5+ queries with explanations (red method) + +- `sum(rate(http_requests_total[5m]))` + **rate**: total requests per second, shows overall traffic load + +- `sum(rate(http_requests_total{status_code=~"[45].."}[5m]))` + **errors**: failed requests per second (4xx + 5xx), shows reliability issues + +- `histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))` + **duration**: p95 latency, shows how slow requests are + +- `sum by (endpoint) (rate(http_requests_total[5m]))` + **rate**: request rate per endpoint, helps see which endpoints are used most + +- `sum by (status_code) (rate(http_requests_total[5m]))` + **errors**: distribution of response codes, helps identify types of failures + +- `http_requests_in_progress` + **rate/load (approx)**: current number of active requests, useful to observe system load + +- `up{job="app"}` + **availability (related to errors)**: shows if service is reachable (1 = up, 0 = down) + +## Production Setup - Health checks, resources, retention policies + +### docker compose ps showing all services healthy + +![](./screenshots/lab08-shots/health%20statuses.png) + +### Documentation of retention policies + +- prometheus is configured to keep metrics for 15 days and up to 10gb of data; this is defined using the retention time and size settings in the docker compose file. this helps to control disk usage and keeps the dataset smaller, which improves query performance. older data is automatically removed once the limits are reached, so the system doesn’t run out of storage. + +### Proof of data persistence after restart + +- if I set 6 hours range I still see my previous data + +![](./screenshots/lab08-shots/persistency.png) + +## Testing Results - Screenshots showing everything working + +- you can see all required screenshots in the above sections + +## Challenges & Solutions - Issues encountered and fixes + +- my app-python container showed unhealty status because in the dockerfile no curl installation happened, I didn't think of it since in the VM it worked fine and I missed that those have different settings, once I added it to the Dockerfile and updated the image, it worked \ No newline at end of file diff --git a/app_python/docs/LAB09.md b/app_python/docs/LAB09.md new file mode 100644 index 0000000000..20ea3e883d --- /dev/null +++ b/app_python/docs/LAB09.md @@ -0,0 +1,301 @@ +# Documentation + +## Architecture Overview + +### Diagram or description of your deployment architecture + +```mermaid +flowchart LR + +subgraph Kubernetes_Cluster["Minikube Kubernetes Cluster"] + + S[Service node port
port 80 -> nodePort 30007] + + subgraph Deployment["Deployment: my-app (5 replicas)"] + P1[Pod 1
app-python] + P2[Pod 2
app-python] + P3[Pod 3
app-python] + P4[Pod 4
app-python] + P5[Pod 5
app-python] + end + +end + +User[User / curl / browser] + +User -->|HTTP request
http://nodeIP:30007| S + +S -->|routes traffic via selector
app: my-app| P1 +S --> P2 +S --> P3 +S --> P4 +S --> P5 + +P1 -->|/health /ready /metrics| AppLogic[(Flask App)] +P2 --> AppLogic +P3 --> AppLogic +P4 --> AppLogic +P5 --> AppLogic + +``` + +### How many Pods, which Services, networking flow + +- I used 5 pods managed by a deployment and one NodePort service, where traffic goes from the service (port 80 / nodePort 30007) to the pods using the app: my-app label selector. + +### Resource allocation strategy + +- I defined small cpu and memory requests/limits (100m–500m cpu, 128Mi–256Mi memory) to keep the app stable and prevent it from using too many cluster resources. + +### Brief explanation of your chosen tool (minikube/kind) and why + +I used minikube because it’s easy to set up locally and lets me run a full kubernetes cluster on my machine, which is enough for testing deployments without needing a real cloud setup. + +## Manifest Files + +### Brief description of each manifest + +- Deployment: deployment.yml defines how my app runs in Kubernetes, including how many pods, which image to use, and how they are configured. + +- Service: service.yml exposes the app inside and outside the cluster by routing traffic to the pods created by the deployment. + +### Key configuration choices + +- Deployment: I set 3 replicas, added resource limits/requests, configured liveness and readiness probes, used labels for selection, and set a rolling update strategy + +- Service: I used NodePort type, matched the selector with app: my-app, set port 80 as the service port, and mapped it to container port 12345 with a fixed nodePort. + +### Why you chose specific values (replicas, resources, etc.) + +- Deployment: I used 3 replicas for basic availability, small cpu/memory values since the app is lightweight, and probes to make sure kubernetes can detect when the app is healthy and ready to serve traffic. + +- Service: I used NodePort so i can access the app locally with minikube, port 80 for convenience, targetPort 12345 to match the app, and a fixed nodePort (30007) to make testing easier. + +## Deployment Evidence + +### Successful cluster setup + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % minikube start + +😄 minikube v1.38.1 on Darwin 26.3 (arm64) +✨ Using the docker driver based on existing profile +👍 Starting "minikube" primary control-plane node in "minikube" cluster +🚜 Pulling base image v0.0.50 ... +🏃 Updating the running docker "minikube" container ... +🐳 Preparing Kubernetes v1.35.1 on Docker 29.2.1 ... +🔎 Verifying Kubernetes components... + ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5 +🌟 Enabled addons: default-storageclass, storage-provisioner + +❗ /Applications/Docker.app/Contents/Resources/bin/kubectl is version 1.32.2, which may have incompatibilities with Kubernetes 1.35.1. + ▪ Want kubectl v1.35.1? Try 'minikube kubectl -- get pods -A' +🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default +``` +### Output of kubectl cluster-info and kubectl get nodes + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl cluster-info +Kubernetes control plane is running at https://127.0.0.1:51390 +CoreDNS is running at https://127.0.0.1:51390/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy + +To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get nodes +NAME STATUS ROLES AGE VERSION +minikube Ready control-plane 6h45m v1.35.1 +``` + +### kubectl get all output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m11s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h58m + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-app-deployment 3/3 3 3 3m11s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-app-deployment-6f67848dfb 3 3 3 3m11s +``` +### kubectl get pods,svc with detailed view + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods,svc +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m35s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h59m +``` + +### kubectl describe deployment showing replicas and strategy + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 29s +my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 29s +my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 29s +``` +### Screenshot or curl output showing app working + +![](./../docs/screenshots/lab09-shots/curl%20app%20.png) + +### Service deployment + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 38m +my-app-service NodePort 10.98.179.244 80:30007/TCP 41s +(devops) fountainer@Veronicas-MacBook-Air k8s % minikube service my-app-service +┌───────────┬────────────────┬─────────────┬───────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼───────────────────────────┤ +│ default │ my-app-service │ 80 │ http://192.168.49.2:30007 │ +└───────────┴────────────────┴─────────────┴───────────────────────────┘ +🔗 Starting tunnel for service my-app-service. +┌───────────┬────────────────┬─────────────┬────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼────────────────────────┤ +│ default │ my-app-service │ │ http://127.0.0.1:57348 │ +└───────────┴────────────────┴─────────────┴────────────────────────┘ +🎉 Opening service default/my-app-service in default browser... +❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it. +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 42m +my-app-service NodePort 10.98.179.244 80:30007/TCP 4m16s +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl describe service my-app-service +Name: my-app-service +Namespace: default +Labels: +Annotations: +Selector: app=my-app +Type: NodePort +IP Family Policy: SingleStack +IP Families: IPv4 +IP: 10.98.179.244 +IPs: 10.98.179.244 +Port: 80/TCP +TargetPort: 12345/TCP +NodePort: 30007/TCP +Endpoints: 10.244.0.16:12345,10.244.0.14:12345,10.244.0.15:12345 +Session Affinity: None +External Traffic Policy: Cluster +Internal Traffic Policy: Cluster +Events: +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get endpoints +Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice +NAME ENDPOINTS AGE +kubernetes 192.168.49.2:8443 42m +my-app-service 10.244.0.14:12345,10.244.0.15:12345,10.244.0.16:12345 4m36s +(devops) fountainer@Veronicas-MacBook-Air k8s % +``` + +## Operations Performed + +### Commands used to deploy + +- ```bash kubectl apply -f k8s/deployment.yml``` +- ```bash kubectl apply -f k8s/service.yml``` +- ```bash kubectl get pods``` +- ```bash kubectl get services ``` + +### Scaling demonstration output + +![](./../docs/screenshots/lab09-shots/scaling.png) + +### Rolling update demonstration output + +I changed ```bash image: fountainer/my-app:latest``` to ```bash image: fountainer/my-app:2026.03.26```. + +![](./../docs/screenshots/lab09-shots/rollout.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout history deployment/my-app-deployment +deployment.apps/my-app-deployment +REVISION CHANGE-CAUSE +1 +2 +3 + +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout undo deployment/my-app-deployment +deployment.apps/my-app-deployment rolled back +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout status deployment/my-app-deployment +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +deployment "my-app-deployment" successfully rolled out +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-7b5479788b-bj4pt 1/1 Running 0 37s +my-app-deployment-7b5479788b-n2pnb 1/1 Running 0 12s +my-app-deployment-7b5479788b-nvzdr 1/1 Running 0 22s +my-app-deployment-7b5479788b-q9g66 1/1 Running 0 36s +my-app-deployment-7b5479788b-w2nc6 1/1 Running 0 22s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Service access method and verification + +I accessed the app using ```bash minikube service my-app-service ``` and verified it by sending requests with curl to endpoints like /health and /ready. + +## Production Considerations + +### What health checks did you implement and why? + +I implemented a liveness probe on /health to restart unhealthy containers and a readiness probe on /ready to ensure pods start receiving traffic only when they are ready to work. + +### Resource limits rationale + +- I set limits to prevent resource overuse, and requests to guarantee the pod gets enough cpu and memory to run reliably. + +### How would you improve this for production? + +- I would add proper logging/monitoring like we did in the previous labs, add autoscaling, consider other update strategies (like canary update). + +### Monitoring and observability strategy + +- In previous labs we used Prometheus for metrics and loki & promtail for logs, also Grafana for dashboard representation + +## Challenges & Solutions + +### Issues encountered + +- I didn't work with NodePort before so I has to stydy it a little bit. Also I didn't know about minikube. + +### How you debugged (logs, describe, events) + +- I researched StackOverflow and other sources, such as documentation for kubernetes and minikube + +### What you learned about Kubernetes + +- I studied kubernetes in the SRE course last semester so I was already pretty familiar with it. We didn't use NodePort service though, and also didn't set up our own cluster since the course team provided us with it. + diff --git a/app_python/docs/LAB10.md b/app_python/docs/LAB10.md new file mode 100644 index 0000000000..e3f80de3ab --- /dev/null +++ b/app_python/docs/LAB10.md @@ -0,0 +1,499 @@ +# Documentation + +## Chart Overview + +### Chart structure explanation + +- the chart has templates/ for kubectl manifests, values.yaml for default settings, and _helpers.tpl for reusable template functions. Hooks are in templates/hooks/ for pre and post-install jobs + +### Key template files and their purpose + +- deployment.yaml defines the app pods and containers +- service.yaml exposes them, and hooks run tasks before or after install +- helpers in _helpers.tpl build names, labels, and selectors consistently, and then can be reused in different config files + +### Values organization strategy + +- values.yaml has defaults, while values-dev.yaml and values-prod.yaml override settings for different environments. +This keeps environment configs separate and easy to manage. + +## Configuration Guide + +### Important values and their purpose + +- replicaCount sets pod number, resources manage CPU/memory, service.type controls what role will the service have (NodePort vs LoadBalancer, etc). LivenessProbe and readinessProbe check pod health. + +### How to customize for different environments + +- you can use values-dev.yaml for dev with 1 replica and NodePort, values-prod.yaml for prod with more replicas and LoadBalancer. and you can also override values on install with --set + +### Example installations with different configurations + +- dev + +```bash +helm install myapp-dev k8s/app_python -f k8s/app_python/values-dev.yaml +``` + +- prod + +```bash +helm install myapp-prod k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +## Hook Implementation + +### What hooks you implemented and why + +- I implemented a pre-install job for setup tasks and a post-install job for validation, to simulate real lifecycle tasks + +### Hook execution order and weights + +- Pre-install has weight -5 to run early, post-install has weight 5 to run after deployment. + +### Deletion policies explanation + +- both hooks have hook-succeeded policy so they auto-delete after finishing successfully + +## Installation Evidence + +### Terminal output showing Helm installation and version (should be 4.x) + +```bash +==> Fetching downloads for: helm +✔︎ Bottle Manifest helm (4.1.3) Downloaded 7.4KB/ 7.4KB +✔︎ Bottle helm (4.1.3) Downloaded 18.1MB/ 18.1MB +==> Pouring helm--4.1.3.arm64_tahoe.bottle.tar.gz +🍺 /opt/homebrew/Cellar/helm/4.1.3: 69 files, 61.3MB +==> Running `brew cleanup helm`... +Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`. +Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). +==> Caveats +zsh completions have been installed to: + /opt/homebrew/share/zsh/site-functions +``` +### Output of exploring a public chart (e.g., helm show chart prometheus-community/prometheus) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm show chart prometheus-community/prometheus +annotations: + artifacthub.io/license: Apache-2.0 + artifacthub.io/links: | + - name: Chart Source + url: https://github.com/prometheus-community/helm-charts + - name: Upstream Project + url: https://github.com/prometheus/prometheus +apiVersion: v2 +appVersion: v3.11.0 +dependencies: +- condition: alertmanager.enabled + name: alertmanager + repository: https://prometheus-community.github.io/helm-charts + version: 1.34.* +- condition: kube-state-metrics.enabled + name: kube-state-metrics + repository: https://prometheus-community.github.io/helm-charts + version: 7.2.* +- condition: prometheus-node-exporter.enabled + name: prometheus-node-exporter + repository: https://prometheus-community.github.io/helm-charts + version: 4.52.* +- condition: prometheus-pushgateway.enabled + name: prometheus-pushgateway + repository: https://prometheus-community.github.io/helm-charts + version: 3.6.* +description: Prometheus is a monitoring system and time series database. +home: https://prometheus.io/ +icon: https://raw.githubusercontent.com/prometheus/prometheus.github.io/master/assets/prometheus_logo-cb55bb5c346.png +keywords: +- monitoring +- prometheus +kubeVersion: '>=1.19.0-0' +maintainers: +- email: gianrubio@gmail.com + name: gianrubio + url: https://github.com/gianrubio +- email: zanhsieh@gmail.com + name: zanhsieh + url: https://github.com/zanhsieh +- email: miroslav.hadzhiev@gmail.com + name: Xtigyro + url: https://github.com/Xtigyro +- email: naseem@transit.app + name: naseemkullah + url: https://github.com/naseemkullah +- email: rootsandtrees@posteo.de + name: zeritti + url: https://github.com/zeritti +name: prometheus +sources: +- https://github.com/prometheus/alertmanager +- https://github.com/prometheus/prometheus +- https://github.com/prometheus/pushgateway +- https://github.com/prometheus/node_exporter +- https://github.com/kubernetes/kube-state-metrics +type: application +version: 28.15.0 +``` + +### helm list output + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm list +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +my-python-app-dev default 1 2026-04-02 22:00:55.999506 +0300 MSK deployed app_python-0.1.0 1.0 +my-python-app-prod default 1 2026-04-02 22:12:24.157572 +0300 MSK deployed app_python-0.1.0 1.0 +myrelease default 1 2026-04-02 22:40:56.562009 +0300 MSK deployed app_python-0.1.0 1.0 +``` + +### kubectl get all showing deployed resources + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 43m +pod/my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 31m +pod/myrelease-app-python-569fb4b645-6v9dt 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-8ws5n 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-glt5r 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-qtg4j 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-rgppk 1/1 Running 0 2m32s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 8d +service/my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 43m +service/my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 31m +service/myrelease-app-python-service NodePort 10.107.17.3 80:30009/TCP 2m32s + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-python-app-dev-app-python 1/1 1 1 43m +deployment.apps/my-python-app-prod-app-python 0/5 5 0 31m +deployment.apps/myrelease-app-python 5/5 5 5 2m32s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-python-app-dev-app-python-7d7f699d85 1 1 1 43m +replicaset.apps/my-python-app-prod-app-python-74c5b97dd5 5 5 0 31m +replicaset.apps/myrelease-app-python-569fb4b645 5 5 5 2m32s +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Hook execution output (kubectl get jobs) + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get jobs -w +NAME STATUS COMPLETIONS DURATION AGE +myrelease-app-python-pre-install Running 0/1 0s +myrelease-app-python-pre-install Running 0/1 0s 0s +myrelease-app-python-pre-install Running 0/1 33s 33s +myrelease-app-python-pre-install Running 0/1 43s 43s +myrelease-app-python-pre-install SuccessCriteriaMet 0/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-post-install Running 0/1 0s +myrelease-app-python-post-install Running 0/1 0s 0s +myrelease-app-python-post-install Running 0/1 5s 5s +myrelease-app-python-post-install Running 0/1 15s 15s +myrelease-app-python-post-install SuccessCriteriaMet 0/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +``` + +### Different environment deployments (dev vs prod) + +- Dev + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-dev k8s/app_python -f k8s/app_python/values-dev.yaml +NAME: my-python-app-dev +LAST DEPLOYED: Thu Apr 2 22:00:55 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 2m20s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 2m27s +``` + +- Prod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-prod k8s/app_python -f k8s/app_python/values-prod.yaml +NAME: my-python-app-prod +LAST DEPLOYED: Thu Apr 2 22:12:24 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 12m +my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 12m +my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 49s +``` + + +## Operations + +### Installation commands used + +```bash +helm install name-of-new-release k8s/app_python -f k8s/app_python/values-for-new-release.yaml +``` +### How to upgrade a release + +```bash +helm upgrade myrelease k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +### How to rollback + +```bash +helm rollback myrelease 1 +``` + +### How to uninstall + +```bash +helm uninstall name-of-release +``` + +## Testing & Validation + +### helm lint output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % helm lint app_python +==> Linting app_python +[INFO] Chart.yaml: icon is recommended + +1 chart(s) linted, 0 chart(s) failed +``` +### helm template verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm template app-python k8s/app_python +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: app-python-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app-python-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 +``` + +### Dry-run output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install --dry-run --debug test-release k8s/app_python +level=WARN msg="--dry-run is deprecated and should be replaced with '--dry-run=client'" +level=DEBUG msg="Original chart version" version="" +level=DEBUG msg="Chart path" path=/Users/fountainer/uni/devops/DevOps-Core-Course/app_python/k8s/app_python +level=DEBUG msg="number of dependencies in the chart" chart=app_python dependencies=0 +NAME: test-release +LAST DEPLOYED: Thu Apr 2 21:46:17 2026 +NAMESPACE: default +STATUS: pending-install +REVISION: 1 +DESCRIPTION: Dry run complete +TEST SUITE: None +USER-SUPPLIED VALUES: +{} + +COMPUTED VALUES: +container: + port: 12345 +image: + pullPolicy: IfNotPresent + repository: fountainer/my-app + tag: 2026.03.26 +livenessProbe: + failureThreshold: 5 + initialDelaySeconds: 10 + path: /health + periodSeconds: 10 + timeoutSeconds: 5 +readinessProbe: + initialDelaySeconds: 5 + path: /ready + periodSeconds: 5 +replicaCount: 5 +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi +service: + nodePort: 30007 + port: 80 + protocol: TCP + targetPort: 12345 + type: NodePort +strategy: + maxSurge: 1 + maxUnavailable: 1 + +HOOKS: +MANIFEST: +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: test-release-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-release-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 + +``` +### Application accessibility verification + +![](./../docs/screenshots/lab10-shots/app-working.png) \ No newline at end of file diff --git a/app_python/docs/LAB11.md b/app_python/docs/LAB11.md new file mode 100644 index 0000000000..1c62eae2de --- /dev/null +++ b/app_python/docs/LAB11.md @@ -0,0 +1,197 @@ +# Documentation + +## Kubernetes Secrets + +### Output of creating and viewing your secret + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl create secret generic app-credentials --from-literal=username=fountainer --from-literal=password=‘mypass293i20@@nekf’ +secret/app-credentials created +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get secret app-credentials -o yaml +apiVersion: v1 +data: + password: 4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ + username: Zm91bnRhaW5lcg== +kind: Secret +metadata: + creationTimestamp: "2026-04-07T14:46:16Z" + name: app-credentials + namespace: default + resourceVersion: "24859" + uid: 6997ca85-68fa-4278-9d51-a6531df977e9 +type: Opaque +``` +### Decoded secret values demonstration + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ" | base64 -d +‘mypass293i20@@nekf’% +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "Zm91bnRhaW5lcg==" | base64 -d +fountainer% +``` +### Explanation of base64 encoding vs encryption + +- Encoding is when we use some publicly accesible algorithm to encode our data. The goal is keeping integrity and usability of the data, it is not really about security. + +- In turn, Encryption is about securuty. It envolves encrypting with an algorithm that can be only resolved by a user who has an encryption key. + +## Helm Secret Integration + +### Chart structure showing secrets.yaml + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % tree app_python/k8s/app_python +app_python/k8s/app_python +├── Chart.yaml +├── charts +├── templates +│ ├── _helpers.tpl +│ ├── deployment.yaml +│ ├── hooks +│ │ ├── post-install-job.yaml +│ │ └── pre-install-job.yaml +│ ├── secrets.yaml +│ └── service.yaml +├── values-dev.yaml +├── values-prod.yaml +└── values.yaml +``` + +### How secrets are consumed in deployment + +- I have $secretName variable that is dynamically set to the name from the values.yaml and values I provide in the helm install command OR defaults to the value from helper. + +### Verification output (env vars in pod, excluding actual values) + +- in pod I have correct env vars: + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-7975557578-6zc9m -- sh +$ echo $PASSWORD +mypass293i20@@nekf +$ echo $USERNAME +fountainer +``` + +- and outside the secrets are hidden + +from ```bash kubectl describe pod mysecretrelease-app-python-7975557578-6zc9m``` + +```bash +Environment: + PASSWORD: Optional: false + USERNAME: Optional: false +``` + + +## Resource Management + +### Resource limits configuration + +```bash +resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} +``` +- in values.yaml I have + +```bash +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" +``` + +### Explanation of requests vs limits + +- requests is a setting that shows kubernates how much resources are needed for a container to run +- limits show how much resources a container is allowed to use (max) + +### How to choose appropriate values + +- you should analyze what processes does your container run and how many cpu/memory it may need +- values can be adjusted by observing the running container +- if you have multiple containers/pods you should constraint them in such a way that they all can work without throttling +- if the memory limit is too low the container can be killed right away + +## Vault Integration + +### Vault installation verification (kubectl get pods) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7975557578-6zc9m 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-7l4tv 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-bqnpd 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-cjjcb 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-st2jd 1/1 Running 0 63m +vault-0 1/1 Running 0 8m23s +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 0 8m23s +``` + +### Policy and role configuration (sanitized) + +- policy + +```bash +/ $ vault policy write myapp-policy /tmp/myapp-policy.hcl +Success! Uploaded policy: myapp-policy +/ $ vault policy read myapp-policy +path "secret/data/myapp/config" { + capabilities = ["read"] +} +``` + +- role config + +```bash +vault write auth/kubernetes/role/myapp-role \ + bound_service_account_names=default \ + bound_service_account_namespaces=default \ + policies=myapp-policy \ + ttl=48h +``` + + +### Proof of secret injection (show file exists, path structure) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-558b98bb9d-8299m -- /bin/sh +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +$ ls -l /vault/secrets +total 4 +-rw-r--r-- 1 100 newuser 180 Apr 7 23:55 config +$ cat /vault/secrets/config +data: map[password:mypass293i20@@nekf username:fountainer] +metadata: map[created_time:2026-04-07T23:32:33.85543147Z custom_metadata: deletion_time: destroyed:false version:1] +$ +``` + +### Explanation of the sidecar injection pattern + +- now every pod contains not only my app container but also vault sidecar container +- vault is able to authenticate in kubernates and inject secrets into the pod + +## Security Analysis + +### Comparison: K8s Secrets vs Vault + +- kubernates secrets are just encoded into base 64 and everyone who gets access to the cluster can decode them and get sensitive data, on the other hand, vault provides data encryption that is much more safer since you need an encryption key to encrypt it + +### When to use each approach + +- encoding is good for keeping data usability and integrity, so different machines can use it (like for seeing special symbols on a web page), it is like... more secure than nothing, but not reeally secure + +- vault encryption is needed for keeping sensitive data secure, like for storing passwords for the services on the virtual machines, etc + +### Production recommendations + +- in production you should always try to use strong encryption algorithms to keep your data secure diff --git a/app_python/docs/LAB12.md b/app_python/docs/LAB12.md new file mode 100644 index 0000000000..f79a158422 --- /dev/null +++ b/app_python/docs/LAB12.md @@ -0,0 +1,141 @@ +# Documentation + +## Application Changes + +### Description of visits counter implementation + +visits counter is a global integer that increases every time / endpoint is called. it writes the value into a file (visits) so it can survive pod restarts via pvc. + +### New endpoint documentation + +/visits returns the current stored counter value from data/visits file + +### Local testing evidence with Docker + +Here you can see the counter value persistence across restarts. +![](./../docs/screenshots/lab12-shots/counter.png) + +## ConfigMap Implementation + +### ConfigMap template structure + +Helm ConfigMap loads a local config.json file via .Files.Get, so the whole json is injected into the cluster as one config object + +### config.json content + +it contains basic app metadata like name, environment, and feature flags (debug, metrics) plus log level settings + +### How ConfigMap is mounted as file + +the ConfigMap is mounted as a volume at /config, so inside the container we can read /config/config.json as a normal file. + +### How ConfigMap provides environment variables + +by using envFrom: configMapRef to inject keys like APP_NAME, APP_ENV, and LOG_LEVEL directly as environment variables. + + +### Verification outputs + +- File content inside pod (cat /config/config.json) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- cat /config/config.json +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +}% +``` + +- Environment variables in pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep APP_ +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +APP_ENV=dev +APP_NAME=my-app +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PROTO=tcp +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_HOST=10.103.123.105 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_ADDR=10.103.123.105 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep LOG_LEVEL +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +LOG_LEVEL=info +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + +## Persistent Volume + +### PVC configuration explanation + +PVC requests a small storage size (100Mi) and creates a persistent volume that is mounted into the pod at /app/data to store the visits file. + +### Access modes and storage class discussion + +ReadWriteOnce is used because only one pod needs to write to the volume, and storageClass is optional so it uses the cluster default. + +### Volume mount configuration + +the volume is mounted into the container at /app/data, and the app writes visits file there so data stays after pod restarts/recreations + +### Persistence test evidence: + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-z6r7b -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl delete pod mysecretrelease-app-python-7b6579656c-z6r7b +pod "mysecretrelease-app-python-7b6579656c-z6r7b" deleted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7b6579656c-b2tzf 2/2 Running 0 60s +vault-0 1/1 Running 3 (84m ago) 8d +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 3 (85m ago) 8d +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-b2tzf -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## ConfigMap vs Secret + +### When to use ConfigMap + +ConfigMap is used for non-sensitive configuration like app name, environment, log level, and feature flags. + +### When to use Secret + +secret is used for sensitive data like username, password, or anything that should not be visible in plain text + +### Key differences + +configMap is plain text and not encrypted, while Secret is base64 encoded and used for sensitive data. + +## Additional Outputs: + +### kubectl get configmap,pvc output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get configmap,pvc +NAME DATA AGE +configmap/kube-root-ca.crt 1 22d +configmap/mysecretrelease-app-python-config 1 7m2s +configmap/mysecretrelease-app-python-env 3 7m2s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/mysecretrelease-app-python-data Bound pvc-42d4685f-8463-4434-8959-0bacd5d972b6 100Mi RWO standard 7m2s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + diff --git a/app_python/docs/LAB13.md b/app_python/docs/LAB13.md new file mode 100644 index 0000000000..67c2551dda --- /dev/null +++ b/app_python/docs/LAB13.md @@ -0,0 +1,214 @@ +# Documentation + +## ArgoCD Setup + +### Installation verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install argocd argo/argo-cd --namespace argocd +NAME: argocd +LAST DEPLOYED: Thu Apr 23 18:37:11 2026 +NAMESPACE: argocd +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +NOTES: +In order to access the server UI you have the following options: + +1. kubectl port-forward service/argocd-server -n argocd 8080:443 + + and then open the browser on http://localhost:8080 and accept the certificate + +2. enable ingress in the values file `server.ingress.enabled` and either + - Add the annotation for ssl passthrough: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-1-ssl-passthrough + - Set the `configs.params."server.insecure"` in the values file and terminate SSL at your ingress: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-2-multiple-ingress-objects-and-hosts + + +After reaching the UI the first time you can login with username: admin and the random password generated during the installation. You can find the password by running: + +kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d + +(You should delete the initial secret afterwards as suggested by the Getting Started Guide: https://argo-cd.readthedocs.io/en/stable/getting_started/#4-login-using-the-cli) +(devops) fountainer@Veronicas-MacBook-Air app_python % helm list -n argocd +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +argocd argocd 1 2026-04-23 18:37:11.582773 +0300 MSK deployed argo-cd-9.5.4 v3.3.8 +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd version +argocd: v3.3.8+7ae7d2c.dirty + BuildDate: 2026-04-21T20:19:34Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: dirty + GitTag: v3.3.8 + GoVersion: go1.26.2 + Compiler: gc + Platform: darwin/arm64 +argocd-server: v3.3.8 + BuildDate: 2026-04-21T17:19:47Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: clean + GitTag: v3.3.8 + GoVersion: go1.25.5 + Compiler: gc + Platform: linux/arm64 + Kustomize Version: v5.8.1 2026-02-09T16:15:27Z + Helm Version: v3.19.4+g7cfb6e4 + Kubectl Version: v0.34.0 + Jsonnet Version: v0.21.0 + ``` + +### UI access method + +Accessed ArgoCD UI via kubectl port-forward to localhost and logged in through the browser using the admin credentials and a previously created password. + +### CLI configuration + +Installed the argocd CLI with Homebrew and connected to the server using argocd login localhost:8080 --insecure. + +## Application Configuration + +### Application manifests + +Created an ArgoCD Application manifest defining the app name, project, source repository, and destination cluster settings. + +### Source and destination configuration + +Configured the application to pull from the Git repository (Helm chart path) and deploy to the in-cluster Kubernetes API in the default namespace. + +### Values file selection + +Specified values.yaml as the Helm values file to control application configuration during deployment. To test, I changed the number of replicas from 1 to 3. + +## Multi-Environment + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (22m ago) 22m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n prod +NAME READY STATUS RESTARTS AGE +python-app-prod-app-python-9dc9c6fbc-n8mtr 1/1 Running 0 4m14s +python-app-prod-app-python-9dc9c6fbc-sqflv 1/1 Running 0 21m +python-app-prod-app-python-9dc9c6fbc-xfvbj 1/1 Running 0 4m14s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app list +NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET +argocd/my-app https://kubernetes.default.svc default default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-dev https://kubernetes.default.svc dev default Synced Healthy Auto-Prune https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-prod https://kubernetes.default.svc prod default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +``` + +### Dev vs Prod configuration differences + +Dev uses smaller resource limits and fewer replicas for faster iteration, while prod uses higher replica count and stricter resource limits for stability and performance. + +### Sync policy differences and rationale + +Dev is configured with automated sync including self-heal and prune for continuous deployment, while prod uses manual sync to ensure controlled and reviewed releases. + +### Namespace separation + +Dev and prod are deployed into separate namespaces to isolate environments, prevent interference, and ensure independent lifecycle management. + +## Self-Healing Evidence + +### Manual scale test with before/after + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 30m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=5 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 31m +``` + +### Pod deletion test + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (31m ago) 32m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl delete pod python-app-dev-app-python-59fdf484d5-g97xn -n dev +pod "python-app-dev-app-python-59fdf484d5-g97xn" deleted +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 0/1 Running 0 15s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 1/1 Running 0 37s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Configuration drift test + +Here you can see the drift +![](./../docs/screenshots/lab13-shots/drift.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app get python-app-dev +Name: argocd/python-app-dev +Project: default +Server: https://kubernetes.default.svc +Namespace: dev +URL: https://argocd.example.com/applications/python-app-dev +Source: +- Repo: https://github.com/ffountainer/DevOps-Core-Course + Target: lab13 + Path: app_python/k8s/app_python + Helm Values: values-dev.yaml +SyncWindow: Sync Allowed +Sync Policy: Automated (Prune) +Sync Status: Synced to lab13 (27593a9) +Health Status: Healthy + +GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE +batch Job dev python-app-dev-app-python-pre-install Succeeded PreSync Reached expected number of succeeded pods + Secret dev app-credentials Synced secret/app-credentials configured + ConfigMap dev python-app-dev-app-python-env Synced configmap/python-app-dev-app-python-env unchanged + ConfigMap dev python-app-dev-app-python-config Synced configmap/python-app-dev-app-python-config unchanged + PersistentVolumeClaim dev python-app-dev-app-python-data Synced Healthy persistentvolumeclaim/python-app-dev-app-python-data unchanged + Service dev python-app-dev-app-python-service Synced Healthy service/python-app-dev-app-python-service unchanged +apps Deployment dev python-app-dev-app-python Synced Healthy deployment.apps/python-app-dev-app-python configured +batch Job dev python-app-dev-app-python-post-install Succeeded PostSync Reached expected number of succeeded pods +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=2 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 52m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl describe deployment python-app-dev-app-python -n dev | grep Replicas +Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable + Available True MinimumReplicasAvailable +``` + +### Explanation of behaviors + +- Explain when ArgoCD syncs vs when Kubernetes heals + +ArgoCD references changes in git, and Kubernetes monitors the cluster (it will heal if the pod crushes, etc) + +- What triggers ArgoCD sync? + +git repo changes, manual sync triggered, auto-sync enabled, drift detected + self-heal enabled + +- What is the sync interval? + +the default sync is every 3 minutes + +## Screenshots + +### ArgoCD UI showing the applications + +![](./../docs/screenshots/lab13-shots/application.png) + +### Sync status + +![](./../docs/screenshots/lab13-shots/sync%20status.png) +![](./../docs/screenshots/lab13-shots/sync.png) + +### Application details view + +![](./../docs/screenshots/lab13-shots/details.png) \ No newline at end of file diff --git a/app_python/docs/LAB14.md b/app_python/docs/LAB14.md new file mode 100644 index 0000000000..7a37244b4c --- /dev/null +++ b/app_python/docs/LAB14.md @@ -0,0 +1,252 @@ +# Documentation + +## Argo Rollouts Setup + +### Installation verification + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl apply -n argo-rollouts -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml +customresourcedefinition.apiextensions.k8s.io/analysisruns.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/analysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/clusteranalysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/experiments.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/rollouts.argoproj.io created +serviceaccount/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-admin created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-edit created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-view created +clusterrolebinding.rbac.authorization.k8s.io/argo-rollouts created +configmap/argo-rollouts-config created +secret/argo-rollouts-notification-secret created +service/argo-rollouts-metrics created +deployment.apps/argo-rollouts created +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 54s +``` +```bash +==> Fetching downloads for: kubectl-argo-rollouts +✔︎ Formula kubectl-argo-rollouts (v1.8.3) Verified 130.1MB/130.1MB +==> Installing kubectl-argo-rollouts from argoproj/tap +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl argo rollouts version +kubectl-argo-rollouts: v1.8.3+49fa151 + BuildDate: 2025-06-04T22:19:21Z + GitCommit: 49fa1516cf71672b69e265267da4e1d16e1fe114 + GitTreeState: clean + GoVersion: go1.23.9 + Compiler: gc + Platform: darwin/amd64 +``` + +### Dashboard access + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 12m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 28s +``` + +![](./../docs/screenshots/lab14-shots/argo-dashboard-access.png) + +### Understand Rollout vs Deployment + +Rollout CRD vs Deployment + +- Rollout and Deployment are kinda similar and both have replicas, selector, template, strategy fields, they manage pod creation. But rollout has additional fields for strategy that allow to perform more controllable rollouts with specific configurations, like rolling an update for a group of users, not for all. + +Additional fields for progressive delivery + +- canary: allows gradual traffic shifting to a new version using steps (e.g., setWeight, pause) +- blueGreen: supports switching between old and new versions using separate services +- steps: defines staged rollout progression +- analysis: integrates automated checks (metrics, tests) during rollout +- pause: enables manual or timed pauses between steps +- trafficRouting: controls how traffic is split between versions (with ingress/service mesh) + + +## Canary Deployment + +### Strategy configuration explained + +The rollout uses a canary strategy to gradually shift traffic from the old version to the new one. It is configured in steps (20%, 40%, 60%, 80%, 100%) with pauses to allow validation and manual control. This approach reduces risk by exposing the new version to a small part of users before full deployment. + +### Step-by-step rollout progression (screenshots from dashboard) + +![](./../docs/screenshots/lab14-shots/canary-prom-1.png) +![](./../docs/screenshots/lab14-shots/canary-prom-2.png) +![](./../docs/screenshots/lab14-shots/canary-prom-3.png) + +### Promotion and abort demonstration + +Promotion (screenshots can be seen in the prev step) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ॥ Paused +Message: CanaryPauseStep +Strategy: Canary + Step: 1/9 + SetWeight: 20 + ActualWeight: 25 +Images: fountainer/my-app:16-04 (canary, stable) +Replicas: + Desired: 3 + Current: 4 + Updated: 1 + Ready: 4 + Available: 4 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ॥ Paused 17m +├──# revision:2 +│ └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 69s canary +│ └──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 68s ready:1/1 +└──# revision:1 + └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet ✔ Healthy 17m stable + ├──□ myapp-app-python-5bc87cfdf6-2tzkc Pod ✔ Running 17m ready:1/1 + ├──□ myapp-app-python-5bc87cfdf6-bnpd6 Pod ✔ Running 17m ready:1/1 + └──□ myapp-app-python-5bc87cfdf6-qfg9s Pod ✔ Running 17m ready:1/1 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts promote myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' promoted +``` + +Abort + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get rollouts -n argo-rollouts +NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE +myapp-app-python 3 4 1 4 31m +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts abort myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' aborted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ✖ Degraded +Message: RolloutAborted: Rollout aborted update to revision 3 +Strategy: Canary + Step: 0/9 + SetWeight: 0 + ActualWeight: 0 +Images: fountainer/my-app:16-04 (stable) +Replicas: + Desired: 3 + Current: 3 + Updated: 0 + Ready: 3 + Available: 3 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ✖ Degraded 32m +├──# revision:3 +│ └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet • ScaledDown 32m canary +└──# revision:2 + └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 16m stable + ├──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 16m ready:1/1 + ├──□ myapp-app-python-76b59b6c66-7cwr4 Pod ✔ Running 10m ready:1/1 + └──□ myapp-app-python-76b59b6c66-skfdd Pod ✔ Running 10m ready:1/1 +``` +![](./../docs/screenshots/lab14-shots/canary-abort.png) + +## Blue-Green Deployment + +### Strategy configuration explained + +The blue-green strategy uses two environments: active and preview. The preview service runs the new version while the active service continues serving production traffic. After testing, the active service is switched to the new version instantly when promoted. This allows safe testing before release and quick rollback if needed. + +### Preview vs active service + +The active service is used by users in production and always points to the stable version. The preview service is used to test the new version before it is promoted. This separation ensures the new version can be verified without affecting real users. + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-preview 8081:80 -n argo-rollouts +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-service 8080:80 -n argo-rollouts +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +``` + +### Promotion process + +Promotion + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm upgrade --install myapp . -n argo-rollouts +Release "myapp" has been upgraded. Happy Helming! +NAME: myapp +LAST DEPLOYED: Thu Apr 30 23:12:57 2026 +NAMESPACE: argo-rollouts +STATUS: deployed +REVISION: 9 +DESCRIPTION: Upgrade complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods -n argo-rollouts +kubectl get svc -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 6h24m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 6h12m +myapp-app-python-76b59b6c66-7cwr4 1/1 Running 0 37m +myapp-app-python-76b59b6c66-pgtgq 1/1 Running 0 43m +myapp-app-python-76b59b6c66-skfdd 1/1 Running 0 37m +myapp-app-python-f7cddd7c7-5nvtx 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-xng4z 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-zjfpv 1/1 Running 0 12m +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +argo-rollouts-dashboard ClusterIP 10.106.240.192 3100/TCP 6h12m +argo-rollouts-metrics ClusterIP 10.109.176.51 8090/TCP 6h24m +myapp-app-python-preview ClusterIP 10.97.144.248 80/TCP 16m +myapp-app-python-service NodePort 10.101.217.107 80:30009/TCP 59m +``` + +![](./../docs/screenshots/lab14-shots/blue-green-1.png) +![](./../docs/screenshots/lab14-shots/bg-2.png) +![](./../docs/screenshots/lab14-shots/bg-4.png) + +Rollback + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts undo myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' undo +``` +![](./../docs/screenshots/lab14-shots/bg-5.png) + + +## Strategy Comparison + +### When to use canary vs blue-green + +canary is used when you want to slowly roll out changes to users and reduce risk step by step. blue-green is used when you want an instant switch between versions after testing + +### Pros and cons of each + +- canary is safer for production because it exposes changes gradually, but it takes longer and is more complex to monitor + +- blue-green is faster and simpler at switch time, but requires double resources and has less gradual control. + +### Your recommendation for different scenarios + +use canary for production systems where stability is critical. use blue-green for fast releases or when you want quick testing and instant rollback. + +## CLI Commands Reference + +### Commands you used + +```kubectl argo rollouts get rollout -w``` is used to watch rollout progress. ```kubectl argo rollouts promote``` is used to move to the next step in canary or switch in blue-green. ```kubectl argo rollouts undo``` is used to rollback to the previous version. + +### Monitoring and troubleshooting + +```kubectl get pods```, ```kubectl get svc```, and ```kubectl describe rollout``` are used to check cluster state and debug issues. dashboard is used to visually monitor rollout progress and traffic changes. \ No newline at end of file diff --git a/app_python/docs/LAB15.md b/app_python/docs/LAB15.md new file mode 100644 index 0000000000..b5a5b60b72 --- /dev/null +++ b/app_python/docs/LAB15.md @@ -0,0 +1,168 @@ +# Documentation + +## StatefulSet Overview + +### Why StatefulSet + +It is used when pods need stable identity and storage, like each pod keeping its own data and name even after restart. + +### Differences from Deployment + +Key differences: +- deployment pods are interchangeable and can change names/storage after restarts, while statefulset pods have fixed names (pod-0, pod-1) and their own persistent storage. + +When to use Deployment vs StatefulSet: +- deployment is used for stateless apps (like web servers), and statefulset for apps that need stable data and identity (like databases). + +Examples of stateful workloads: +- databases like mysql/postgresql, message queues, systems like elasticsearch + +### Headless Services + +What is a headless service (clusterIP: None)? +- a service without a cluster ip that lets you directly access individual pods instead of load balancing + +How DNS works with StatefulSets? +- each pod gets its own dns name like pod-0.service-name.namespace.svc.cluster.local, and they can be addressed individually + +## Resource Verification + +### Output of kubectl get pod,sts,svc,pvc + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get statefulset +NAME READY AGE +myapp-app-python 3/3 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +myapp-app-python-0 1/1 Running 0 42s +myapp-app-python-1 1/1 Running 0 25s +myapp-app-python-2 1/1 Running 0 16s +myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 42s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pvc +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 53s +data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 36s +data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 27s +my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 53s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod,sts,svc,pvc +NAME READY STATUS RESTARTS AGE +pod/my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +pod/my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +pod/my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +pod/myapp-app-python-0 1/1 Running 0 99s +pod/myapp-app-python-1 1/1 Running 0 82s +pod/myapp-app-python-2 1/1 Running 0 73s +pod/myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 99s + +NAME READY AGE +statefulset.apps/myapp-app-python 3/3 99s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 7d8h +service/myapp-app-python-preview ClusterIP 10.98.51.97 80/TCP 99s +service/myapp-app-python-service NodePort 10.110.182.139 80:30009/TCP 99s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 99s +persistentvolumeclaim/data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 82s +persistentvolumeclaim/data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 73s +persistentvolumeclaim/my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +persistentvolumeclaim/myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 99s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## Network Identity + +### DNS resolution outputs + +the naming pattern is ```.``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec -it myapp-app-python-0 -- /bin/sh +$ getent hosts myapp-app-python-0.myapp-app-python-headless +10.244.0.177 myapp-app-python-0.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-1.myapp-app-python-headless +10.244.0.179 myapp-app-python-1.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-2.myapp-app-python-headless +10.244.0.180 myapp-app-python-2.myapp-app-python-headless.default.svc.cluster.local +``` + +## Per-Pod Storage Evidence + +### Different visit counts per pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-0 8080:12345 +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % pyenv shell devops +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-1 8081:12345 +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-2 8082:12345 +Forwarding from 127.0.0.1:8082 -> 12345 +Forwarding from [::1]:8082 -> 12345 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8080/visits +{ + "visits": 13 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8081/visits +{ + "visits": 7 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8082/visits +{ + "visits": 2 +} +``` + +![](./../docs/screenshots/lab15-shots/visits-diff.png) + +## Persistence Test + +### data survives pod deletion + +![](./../docs/screenshots/lab15-shots/pers_test.png) \ No newline at end of file diff --git a/app_python/docs/LAB16.md b/app_python/docs/LAB16.md new file mode 100644 index 0000000000..b71aebd57e --- /dev/null +++ b/app_python/docs/LAB16.md @@ -0,0 +1,227 @@ +# Documentation + +## Stack Components + +### Descriptions in your own words + +- Prometheus Operator: it's a kubernates tool that allows you to automate prometheus deployment and management. it provides a set of custom resource definitions, and you can make your own configuration with those. + +- Prometheus: it's a tool for monitoring and alerting, it stores metrics as a series of timestampts. + +- Alertmanager: an instruments to manage alerts. when metrics reach invalid state, alertmanager will receive alerts, group and send them to asignees. you can configure when to silence alerts or start reaching out to the next person if the first one is not responding (it's an escalation), and create other custom settings. + +- Grafana: it is a dashboard for tracking the current state of the system by visualising logs and metrics. you can define alert rules there to see if the new metric value is out of the valid tresholds. + +- kube-state-metrics: it's a service that exposes metrics related to kubernates objects, they are created automatically and describe your pods current state. + +- node-exporter: it's an agent that exposes internal metrics for a node (like cpu, memory, etc), then prometheus can scrape those metrics. + +## Installation Evidence + +### kubectl get po,svc -n monitoring + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo add prometheus-community https://prometheus-community.github.io/helm-charts +"prometheus-community" already exists with the same configuration, skipping +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo update +Hang tight while we grab the latest from your chart repositories... +...Successfully got an update from the "hashicorp" chart repository +...Successfully got an update from the "argo" chart repository +...Successfully got an update from the "prometheus-community" chart repository +Update Complete. ⎈Happy Helming!⎈ +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get po,svc -n monitoring +NAME READY STATUS RESTARTS AGE +pod/alertmanager-monitoring-kube-prometheus-alertmanager-0 2/2 Running 0 2m34s +pod/monitoring-grafana-bbc5c674-8cbd9 3/3 Running 0 2m56s +pod/monitoring-kube-prometheus-operator-54f68d65b4-99ck2 1/1 Running 0 2m56s +pod/monitoring-kube-state-metrics-5957bd45bc-5rpqr 1/1 Running 0 2m56s +pod/monitoring-prometheus-node-exporter-c8fg6 1/1 Running 0 2m57s +pod/prometheus-monitoring-kube-prometheus-prometheus-0 2/2 Running 0 2m34s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/alertmanager-operated ClusterIP None 9093/TCP,9094/TCP,9094/UDP 2m34s +service/monitoring-grafana ClusterIP 10.110.156.182 80/TCP 2m57s +service/monitoring-kube-prometheus-alertmanager ClusterIP 10.111.243.229 9093/TCP,8080/TCP 2m57s +service/monitoring-kube-prometheus-operator ClusterIP 10.99.16.80 443/TCP 2m57s +service/monitoring-kube-prometheus-prometheus ClusterIP 10.106.17.206 9090/TCP,8080/TCP 2m57s +service/monitoring-kube-state-metrics ClusterIP 10.102.26.186 8080/TCP 2m57s +service/monitoring-prometheus-node-exporter ClusterIP 10.100.205.92 9100/TCP 2m57s +service/prometheus-operated ClusterIP None 9090/TCP 2m34s +``` + +## Dashboard Answers + +### Pod Resources: CPU/memory usage of your StatefulSet + +Due to the pods and the app itself being very lightweight, CPU and memory usage never went higher than initially allocated resources (100m CPU and 128Mi memory). Even under high load (I used multiple loops with curl), the initial resources were enough. You will see more detailed usages info in the next question. + +Example for pod 2: + +![](./../docs/screenshots/lab16-shots/cpupod2.png) + +![](./../docs/screenshots/lab16-shots/memorypod2.png) + +### Namespace Analysis: Which pods use most/least CPU in default namespace? + +I decided to use Prometheus for evidence, since the resource usage was really low, and didn't show up properly in Grafana. + +curl I used (the first count is much bigger since I previously tested only with pod 2) + +![](./../docs/screenshots/lab16-shots/curl.png) + +usage + +![](./../docs/screenshots/lab16-shots/namespace%20usage.png) + +As we can see, all statefulset pods used roughly the same amount of CPU and memory resources. This is anticipated, because load balancing is used for routing traffic to different pods. + +### Node Metrics: Memory usage (% and MB), CPU cores + +CPU and Memory usage for the whole minikube node was: + +![](./../docs/screenshots/lab16-shots/node%20cpu%20memory.png) + +It is much higher than resources used in statefulset since the node contains all different namespaces in my minikube cluster. + +### Kubelet: How many pods/containers managed? + +16 pods and 41 containers + +![](./../docs/screenshots/lab16-shots/pods%20managed.png) + +### Network: Traffic for pods in default namespace + +![](./../docs/screenshots/lab16-shots/network.png) + +### Alerts: How many active alerts? Check Alertmanager UI + +![](./../docs/screenshots/lab16-shots/alert.png) + +## Init Containers: Implementation and proof of success + +I implemented two init container patterns. First one downloads a file with wget into a shared emptyDir volume and the main container successfully accessed it from /data/index.html. Second one uses a wait-for-service init container that continuously checks the nginx service with wget and only starts the main container after the service becomes reachable. + +### Init container + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 Init:0/1 0 2s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 PodInitializing 0 4s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 5s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +``` +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl logs init-download-pod -c init-download +Connecting to example.com (172.66.147.243:443) +wget: note: TLS certificate validation not implemented +saving to '/work-dir/index.html' +index.html 100% |********************************| 528 0:00:00 ETA +'/work-dir/index.html' saved +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec init-download-pod -- cat /data/index.html +Defaulted container "main-app" out of: main-app, init-download (init) +Example Domain

Example Domain

This domain is for use in documentation examples without needing permission. Avoid use in operations.

Learn more

+``` + +### Waiting for service container + +```bash +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f waiting.yaml +pod/wait-service-pod created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +wait-service-pod 0/1 Init:0/1 0 22s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl logs wait-service-pod -c wait-for-service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f nginx.yaml +deployment.apps/myservice created +service/myservice created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 3s +wait-service-pod 0/1 Init:0/1 0 55s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 5s +wait-service-pod 0/1 PodInitializing 0 57s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 7s +wait-service-pod 1/1 Running 0 59s +``` + +After service was discovered: + +```bash +waiting for service + + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, nginx is successfully installed and working. +Further configuration is required for the web server, reverse proxy, +API gateway, load balancer, content cache, or other features.

+ +

For online documentation and support please refer to +nginx.org.
+To engage with the community please visit +community.nginx.org.
+For enterprise grade support, professional services, additional +security features and capabilities please refer to +f5.com/nginx.

+ +

Thank you for using nginx.

+ + +``` \ No newline at end of file diff --git a/app_python/docs/LAB17.md b/app_python/docs/LAB17.md new file mode 100644 index 0000000000..7a1216a967 --- /dev/null +++ b/app_python/docs/LAB17.md @@ -0,0 +1,215 @@ +# Documentation + +## Deployment summary + +### Worker URL + +[https://edge-api.v-levasheva.workers.dev](https://edge-api.v-levasheva.workers.dev) + +### Main routes + +/ for general app info + +/health for health status + +/edge for cloudflare edge metadata + +/counter for a persistent KV-based request counter, which shows the number of visits for / route + +/admin-check is for checking if the requester is an admin, it uses secrets API_TOKEN and ADMIN_EMAIL, and the requester should provide valid token and email in the request headers + +and also there is "Not Found" response for non-existent route + +### Configuration used + +the worker was configured using wrangler.jsonc, which defines the worker name, entrypoint (src/index.ts), compatibility date, environment variables, and resource bindings. the project uses a TypeScript Worker template and includes an APP_NAME variable binding together with a COUNTER_KV KV namespace binding used for persistent storage in the /counter endpoint. + +## Evidence + +### Screenshot of Cloudflare dashboard + +![](./../docs/screenshots/lab17-shots/dashboard.png) + +### Example /edge JSON response + +Firstly, the local testing of all endpoints (for the deployed /edge some info such as asn and httpProtocol were added): + +![](./../docs/screenshots/lab17-shots/endpoints%20local.png) + +And the /edge endpoint in the deployed worker: + +![](./../docs/screenshots/lab17-shots/edge%20deployed.png) + +### How Workers distributes execution globally? + +cloudflare workers are automatically executed on cloudflare edge locations around the world, so requests are handled close to the user without manually selecting regions. unlike traditional VM or PaaS platforms where you often deploy separately to regions, workers use cloudflare’s global network automatically, so there is no separate “deploy to 3 regions” step. + +### The difference between workers.dev, Routes, and Custom Domains + +- workers.dev is the default public cloudflare subdomain automatically provided for testing and accessing workers + +- routes are specific url endpoints that worker will provide access to + +- custom domains allow exposing the worker directly through an owned custom domain, not through a provided workers.dev + +## Configuration, Secrets & Persistence + +### Explain why plaintext vars are not suitable for secrets + +variables I added: "APP_NAME" and "COURSE_NAME" + +plaintext vars are not safe for secrets because they are stored in config and visible in repo and dashboard, so anyone can read them + +### Secrets + +I added 2 secrets: API_TOKEN and ADMIN_EMAIL. They are used in the /admin-check endpoint, where the requester can pass their access token and email and see if they can be authenticated as an admin. + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler secret list +[ + { + "name": "ADMIN_EMAIL", + "type": "secret_text" + }, + { + "name": "API_TOKEN", + "type": "secret_text" + } +] +``` + +![](./../docs/screenshots/lab17-shots/secrets.png) + +### Workers KV persistence + +### Document what you stored and how you verified it + +I stored the number of visits of the / endpoint. Each time / is visited, the counter increases by one, and visits value is updated. The value then can be accessed through the /visits endpoint. + +The persistance verification: + +![](./../docs/screenshots/lab17-shots/persistance.png) + +## Observability & Operations + +### Example log or metrics screenshot + +Logs + +![](./../docs/screenshots/lab17-shots/logs.png) + +Metrics + +![](./../docs/screenshots/lab17-shots/metrics.png) + +I looked at errors, requests, and request duration metrics for all deployed versions for the last 24 hours. + +requests: total of 192. this metrics shows how many http requests hit the Worker through all endpoints. + +errors: 0 errors. it can be confusing since I intentially hit invalid endpoint such as https://edge-api.v-levasheva.workers.dev/smth a lot of times to get error: "Not Found", but the case was that the Worker does not count 404 responses as errors, the real errors that are considered are runtime failures, which I did not have. + +request duration: 3.15 ms on average. this metric shows the latency - how much it takes to process a request and send back a response. + +### Multiple Deployments + +* I created about 10 deployments but put here last 2 for readability + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler deployments list + + ⛅️ wrangler 4.90.0 +Created: 2026-05-10T16:57:52.287Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 4844c942-5e05-4199-999f-b54af219ed99 + Created: 2026-05-10T16:48:11.805Z + Tag: - + Message: - + +Created: 2026-05-10T17:01:05.875Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f + Created: 2026-05-10T17:01:03.267Z + Tag: - + Message: - +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler rollback + + ⛅️ wrangler 4.90.0 +─────────────────── +├ Your current deployment has 1 version(s): +│ +│ (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f +│ Created: 2026-05-10T17:01:03.267267Z +│ Tag: - +│ Message: - +│ +✔ Please provide an optional message for this rollback (120 characters max) … test rollback +│ +├  WARNING  You are about to rollback to Worker Version 4844c942-5e05-4199-999f-b54af219ed99. +│ This will immediately replace the current deployment and become the active deployment across all your deployed triggers. +│ However, your local development environment will not be affected by this rollback. +│ Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc). +│ +│ (100%) 4844c942-5e05-4199-999f-b54af219ed99 +│ Created: 2026-05-10T16:48:11.805042Z +│ Tag: - +│ Message: - +│ +✔ Are you sure you want to deploy this Worker Version to 100% of traffic? … yes +Performing rollback... +│ +╰  SUCCESS  Worker Version 4844c942-5e05-4199-999f-b54af219ed99 has been deployed to 100% of traffic. + +Current Version ID: 4844c942-5e05-4199-999f-b54af219ed99 +``` + +## Kubernetes vs Cloudflare Workers Comparison + +| Aspect | Kubernetes | Cloudflare Workers | +|--------|------------|--------------------| +| Setup complexity | really high, you should create cluster setup, manifests, networking, monitoring, and scaling configuration yourself | much simpler, just write code and deploy with minimal config | +| Deployment speed | slower, containers must build and start | very fast, seconds are enough | +| Global distribution | usually requires manual configuration | automatic global edge distribution | +| Cost (for small apps) | can become expensive due to always-running release | cheaper for lightweight APIs and low traffic apps | +| State/persistence model | persistent volumes, databases, StatefulSets | stateless by default, persistence through KV | +| Control/flexibility | very high control over runtime, networking, and storage | less low-level control, but much simpler to use | +| Best use case | complex and heavy programs, projects that require customisation | edge APIs, lightweight services, and globally distributed low-latency apps | + +## When to Use Each + +### Scenarios favoring Kubernetes + +- complex applications with multiple services +- custom configuration +- long-running work processes + +### Scenarios favoring Workers + +- lightweight APIs +- edge services +- globally distributed applications with low latency requirements + +### Your recommendation + +I would use k8s and VMs for big projects that require heavy customisation, and Cloudflare Workers for lightweight apps that will benefit immensely from locating close to the users. + +## Reflection + +### What felt easier than Kubernetes? + +- to be honest, everything felt easier + +### What felt more constrained? + +- no control over networking, no customisation, no manual resource distribution and controllable scaling. + +### What changed because Workers is not a Docker host? + +- as opposed to docker, Workers provide serverless architecture, and Workers runtime model is forced. moreover, I didn't have to keep process running locally, like with docker containers, it was nice. + diff --git a/app_python/docs/screenshots/01-main-endpoint.png b/app_python/docs/screenshots/01-main-endpoint.png new file mode 100644 index 0000000000..ba33e3a732 Binary files /dev/null and b/app_python/docs/screenshots/01-main-endpoint.png differ diff --git a/app_python/docs/screenshots/02-health-check.png b/app_python/docs/screenshots/02-health-check.png new file mode 100644 index 0000000000..aeb0c64170 Binary files /dev/null and b/app_python/docs/screenshots/02-health-check.png differ diff --git a/app_python/docs/screenshots/03-formatted-output.png b/app_python/docs/screenshots/03-formatted-output.png new file mode 100644 index 0000000000..1d5b985415 Binary files /dev/null and b/app_python/docs/screenshots/03-formatted-output.png differ diff --git a/app_python/docs/screenshots/lab02-shots/build.png b/app_python/docs/screenshots/lab02-shots/build.png new file mode 100644 index 0000000000..83b9fec021 Binary files /dev/null and b/app_python/docs/screenshots/lab02-shots/build.png differ diff --git a/app_python/docs/screenshots/lab02-shots/pull.png b/app_python/docs/screenshots/lab02-shots/pull.png new file mode 100644 index 0000000000..b5aa80a5b0 Binary files /dev/null and b/app_python/docs/screenshots/lab02-shots/pull.png differ diff --git a/app_python/docs/screenshots/lab02-shots/push.png b/app_python/docs/screenshots/lab02-shots/push.png new file mode 100644 index 0000000000..dc267951d2 Binary files /dev/null and b/app_python/docs/screenshots/lab02-shots/push.png differ diff --git a/app_python/docs/screenshots/lab02-shots/run.png b/app_python/docs/screenshots/lab02-shots/run.png new file mode 100644 index 0000000000..9cb5eb6708 Binary files /dev/null and b/app_python/docs/screenshots/lab02-shots/run.png differ diff --git a/app_python/docs/screenshots/lab02-shots/test.png b/app_python/docs/screenshots/lab02-shots/test.png new file mode 100644 index 0000000000..48d590cfa6 Binary files /dev/null and b/app_python/docs/screenshots/lab02-shots/test.png differ diff --git a/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png b/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png new file mode 100644 index 0000000000..4919561bfd Binary files /dev/null and b/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png differ diff --git a/app_python/docs/screenshots/lab03-shots/improved perf.png b/app_python/docs/screenshots/lab03-shots/improved perf.png new file mode 100644 index 0000000000..dffca15721 Binary files /dev/null and b/app_python/docs/screenshots/lab03-shots/improved perf.png differ diff --git a/app_python/docs/screenshots/lab03-shots/pipeline success.png b/app_python/docs/screenshots/lab03-shots/pipeline success.png new file mode 100644 index 0000000000..e5c638d566 Binary files /dev/null and b/app_python/docs/screenshots/lab03-shots/pipeline success.png differ diff --git a/app_python/docs/screenshots/lab03-shots/unit test output.png b/app_python/docs/screenshots/lab03-shots/unit test output.png new file mode 100644 index 0000000000..a3beab71dd Binary files /dev/null and b/app_python/docs/screenshots/lab03-shots/unit test output.png differ diff --git a/app_python/docs/screenshots/lab04-shots/pulumi preview.png b/app_python/docs/screenshots/lab04-shots/pulumi preview.png new file mode 100644 index 0000000000..5ba4f05400 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/pulumi preview.png differ diff --git a/app_python/docs/screenshots/lab04-shots/pulumi ssh.png b/app_python/docs/screenshots/lab04-shots/pulumi ssh.png new file mode 100644 index 0000000000..2311bbfb38 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/pulumi ssh.png differ diff --git a/app_python/docs/screenshots/lab04-shots/pulumi up.png b/app_python/docs/screenshots/lab04-shots/pulumi up.png new file mode 100644 index 0000000000..54bf4d50e7 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/pulumi up.png differ diff --git a/app_python/docs/screenshots/lab04-shots/ssh output terraform.png b/app_python/docs/screenshots/lab04-shots/ssh output terraform.png new file mode 100644 index 0000000000..463910cf4a Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/ssh output terraform.png differ diff --git a/app_python/docs/screenshots/lab04-shots/terraform apply-1.png b/app_python/docs/screenshots/lab04-shots/terraform apply-1.png new file mode 100644 index 0000000000..7598bfbefe Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/terraform apply-1.png differ diff --git a/app_python/docs/screenshots/lab04-shots/terraform apply-2.png b/app_python/docs/screenshots/lab04-shots/terraform apply-2.png new file mode 100644 index 0000000000..7715fc8423 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/terraform apply-2.png differ diff --git a/app_python/docs/screenshots/lab04-shots/terraform destroy.png b/app_python/docs/screenshots/lab04-shots/terraform destroy.png new file mode 100644 index 0000000000..d9137cc00e Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/terraform destroy.png differ diff --git a/app_python/docs/screenshots/lab04-shots/terraform plan.png b/app_python/docs/screenshots/lab04-shots/terraform plan.png new file mode 100644 index 0000000000..e73c54a786 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/terraform plan.png differ diff --git a/app_python/docs/screenshots/lab04-shots/vm status.png b/app_python/docs/screenshots/lab04-shots/vm status.png new file mode 100644 index 0000000000..95c51dae37 Binary files /dev/null and b/app_python/docs/screenshots/lab04-shots/vm status.png differ diff --git a/app_python/docs/screenshots/lab05-shots/encrypted.png b/app_python/docs/screenshots/lab05-shots/encrypted.png new file mode 100644 index 0000000000..e874cc9e27 Binary files /dev/null and b/app_python/docs/screenshots/lab05-shots/encrypted.png differ diff --git a/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png b/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png new file mode 100644 index 0000000000..ce2e9eab9f Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png differ diff --git a/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png b/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png new file mode 100644 index 0000000000..a1e3ff8169 Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png differ diff --git a/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png b/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png new file mode 100644 index 0000000000..4d4adec394 Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png differ diff --git a/app_python/docs/screenshots/lab06-shots/Skip common role.png b/app_python/docs/screenshots/lab06-shots/Skip common role.png new file mode 100644 index 0000000000..0d15d7b10e Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/Skip common role.png differ diff --git a/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png b/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png new file mode 100644 index 0000000000..44d8374d37 Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png differ diff --git a/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png b/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png new file mode 100644 index 0000000000..8705183b16 Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png differ diff --git a/app_python/docs/screenshots/lab06-shots/ci:cd success.png b/app_python/docs/screenshots/lab06-shots/ci:cd success.png new file mode 100644 index 0000000000..aec7e3de34 Binary files /dev/null and b/app_python/docs/screenshots/lab06-shots/ci:cd success.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana dashboard.png b/app_python/docs/screenshots/lab07-shots/grafana dashboard.png new file mode 100644 index 0000000000..e9ea4b2f99 Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana dashboard.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana-app-name.png b/app_python/docs/screenshots/lab07-shots/grafana-app-name.png new file mode 100644 index 0000000000..fd72e51ce9 Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana-app-name.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana-error.png b/app_python/docs/screenshots/lab07-shots/grafana-error.png new file mode 100644 index 0000000000..0dba1e1844 Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana-error.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana_get.png b/app_python/docs/screenshots/lab07-shots/grafana_get.png new file mode 100644 index 0000000000..01553d407a Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana_get.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana_login.png b/app_python/docs/screenshots/lab07-shots/grafana_login.png new file mode 100644 index 0000000000..65e92615cf Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana_login.png differ diff --git a/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png b/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png new file mode 100644 index 0000000000..2ca33c387f Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png differ diff --git a/app_python/docs/screenshots/lab07-shots/healthy services.png b/app_python/docs/screenshots/lab07-shots/healthy services.png new file mode 100644 index 0000000000..efabf016f3 Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/healthy services.png differ diff --git a/app_python/docs/screenshots/lab07-shots/logs from the app.png b/app_python/docs/screenshots/lab07-shots/logs from the app.png new file mode 100644 index 0000000000..c4048b4b63 Binary files /dev/null and b/app_python/docs/screenshots/lab07-shots/logs from the app.png differ diff --git a/app_python/docs/screenshots/lab08-shots/dashboard1.png b/app_python/docs/screenshots/lab08-shots/dashboard1.png new file mode 100644 index 0000000000..e94922775b Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/dashboard1.png differ diff --git a/app_python/docs/screenshots/lab08-shots/dashboard2.png b/app_python/docs/screenshots/lab08-shots/dashboard2.png new file mode 100644 index 0000000000..d1f8714c6f Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/dashboard2.png differ diff --git a/app_python/docs/screenshots/lab08-shots/health statuses.png b/app_python/docs/screenshots/lab08-shots/health statuses.png new file mode 100644 index 0000000000..cf89eeb949 Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/health statuses.png differ diff --git a/app_python/docs/screenshots/lab08-shots/metrics for app logs.png b/app_python/docs/screenshots/lab08-shots/metrics for app logs.png new file mode 100644 index 0000000000..2c6998a0e0 Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/metrics for app logs.png differ diff --git a/app_python/docs/screenshots/lab08-shots/persistency.png b/app_python/docs/screenshots/lab08-shots/persistency.png new file mode 100644 index 0000000000..5e7459e607 Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/persistency.png differ diff --git a/app_python/docs/screenshots/lab08-shots/prometheous targets.png b/app_python/docs/screenshots/lab08-shots/prometheous targets.png new file mode 100644 index 0000000000..6963da43ce Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/prometheous targets.png differ diff --git a/app_python/docs/screenshots/lab08-shots/successful query.png b/app_python/docs/screenshots/lab08-shots/successful query.png new file mode 100644 index 0000000000..bc76a157c7 Binary files /dev/null and b/app_python/docs/screenshots/lab08-shots/successful query.png differ diff --git a/app_python/docs/screenshots/lab09-shots/curl app .png b/app_python/docs/screenshots/lab09-shots/curl app .png new file mode 100644 index 0000000000..8ec4de04f6 Binary files /dev/null and b/app_python/docs/screenshots/lab09-shots/curl app .png differ diff --git a/app_python/docs/screenshots/lab09-shots/rollout.png b/app_python/docs/screenshots/lab09-shots/rollout.png new file mode 100644 index 0000000000..866425dd76 Binary files /dev/null and b/app_python/docs/screenshots/lab09-shots/rollout.png differ diff --git a/app_python/docs/screenshots/lab09-shots/scaling.png b/app_python/docs/screenshots/lab09-shots/scaling.png new file mode 100644 index 0000000000..e74aaa0833 Binary files /dev/null and b/app_python/docs/screenshots/lab09-shots/scaling.png differ diff --git a/app_python/docs/screenshots/lab10-shots/app-working.png b/app_python/docs/screenshots/lab10-shots/app-working.png new file mode 100644 index 0000000000..47bab43c9f Binary files /dev/null and b/app_python/docs/screenshots/lab10-shots/app-working.png differ diff --git a/app_python/docs/screenshots/lab12-shots/counter.png b/app_python/docs/screenshots/lab12-shots/counter.png new file mode 100644 index 0000000000..e83a4ff5b8 Binary files /dev/null and b/app_python/docs/screenshots/lab12-shots/counter.png differ diff --git a/app_python/docs/screenshots/lab13-shots/application.png b/app_python/docs/screenshots/lab13-shots/application.png new file mode 100644 index 0000000000..ba2f7f8e21 Binary files /dev/null and b/app_python/docs/screenshots/lab13-shots/application.png differ diff --git a/app_python/docs/screenshots/lab13-shots/details.png b/app_python/docs/screenshots/lab13-shots/details.png new file mode 100644 index 0000000000..a0341d0c5b Binary files /dev/null and b/app_python/docs/screenshots/lab13-shots/details.png differ diff --git a/app_python/docs/screenshots/lab13-shots/drift.png b/app_python/docs/screenshots/lab13-shots/drift.png new file mode 100644 index 0000000000..c98c4fea1c Binary files /dev/null and b/app_python/docs/screenshots/lab13-shots/drift.png differ diff --git a/app_python/docs/screenshots/lab13-shots/sync status.png b/app_python/docs/screenshots/lab13-shots/sync status.png new file mode 100644 index 0000000000..705de1d582 Binary files /dev/null and b/app_python/docs/screenshots/lab13-shots/sync status.png differ diff --git a/app_python/docs/screenshots/lab13-shots/sync.png b/app_python/docs/screenshots/lab13-shots/sync.png new file mode 100644 index 0000000000..0720e0396c Binary files /dev/null and b/app_python/docs/screenshots/lab13-shots/sync.png differ diff --git a/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png b/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png new file mode 100644 index 0000000000..93cb0f496d Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png differ diff --git a/app_python/docs/screenshots/lab14-shots/bg-2.png b/app_python/docs/screenshots/lab14-shots/bg-2.png new file mode 100644 index 0000000000..0d1e97d457 Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/bg-2.png differ diff --git a/app_python/docs/screenshots/lab14-shots/bg-3.png b/app_python/docs/screenshots/lab14-shots/bg-3.png new file mode 100644 index 0000000000..bea1b39d10 Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/bg-3.png differ diff --git a/app_python/docs/screenshots/lab14-shots/bg-4.png b/app_python/docs/screenshots/lab14-shots/bg-4.png new file mode 100644 index 0000000000..196ef487ab Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/bg-4.png differ diff --git a/app_python/docs/screenshots/lab14-shots/bg-5.png b/app_python/docs/screenshots/lab14-shots/bg-5.png new file mode 100644 index 0000000000..275e4f4796 Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/bg-5.png differ diff --git a/app_python/docs/screenshots/lab14-shots/blue-green-1.png b/app_python/docs/screenshots/lab14-shots/blue-green-1.png new file mode 100644 index 0000000000..15936932ff Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/blue-green-1.png differ diff --git a/app_python/docs/screenshots/lab14-shots/canary-abort.png b/app_python/docs/screenshots/lab14-shots/canary-abort.png new file mode 100644 index 0000000000..dd031144fd Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/canary-abort.png differ diff --git a/app_python/docs/screenshots/lab14-shots/canary-prom-1.png b/app_python/docs/screenshots/lab14-shots/canary-prom-1.png new file mode 100644 index 0000000000..73395076d0 Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/canary-prom-1.png differ diff --git a/app_python/docs/screenshots/lab14-shots/canary-prom-2.png b/app_python/docs/screenshots/lab14-shots/canary-prom-2.png new file mode 100644 index 0000000000..bb11cd91cc Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/canary-prom-2.png differ diff --git a/app_python/docs/screenshots/lab14-shots/canary-prom-3.png b/app_python/docs/screenshots/lab14-shots/canary-prom-3.png new file mode 100644 index 0000000000..98be269459 Binary files /dev/null and b/app_python/docs/screenshots/lab14-shots/canary-prom-3.png differ diff --git a/app_python/docs/screenshots/lab15-shots/pers_test.png b/app_python/docs/screenshots/lab15-shots/pers_test.png new file mode 100644 index 0000000000..f142fe13fa Binary files /dev/null and b/app_python/docs/screenshots/lab15-shots/pers_test.png differ diff --git a/app_python/docs/screenshots/lab15-shots/visits-diff.png b/app_python/docs/screenshots/lab15-shots/visits-diff.png new file mode 100644 index 0000000000..ddcbd746e0 Binary files /dev/null and b/app_python/docs/screenshots/lab15-shots/visits-diff.png differ diff --git a/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png b/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png new file mode 100644 index 0000000000..3dd0a8efe7 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png differ diff --git a/app_python/docs/screenshots/lab16-shots/alert.png b/app_python/docs/screenshots/lab16-shots/alert.png new file mode 100644 index 0000000000..fde3f418b3 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/alert.png differ diff --git a/app_python/docs/screenshots/lab16-shots/cpupod2.png b/app_python/docs/screenshots/lab16-shots/cpupod2.png new file mode 100644 index 0000000000..3d2ab981f6 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/cpupod2.png differ diff --git a/app_python/docs/screenshots/lab16-shots/curl.png b/app_python/docs/screenshots/lab16-shots/curl.png new file mode 100644 index 0000000000..94cea81937 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/curl.png differ diff --git a/app_python/docs/screenshots/lab16-shots/memorypod2.png b/app_python/docs/screenshots/lab16-shots/memorypod2.png new file mode 100644 index 0000000000..7e83d87b1f Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/memorypod2.png differ diff --git a/app_python/docs/screenshots/lab16-shots/namespace usage.png b/app_python/docs/screenshots/lab16-shots/namespace usage.png new file mode 100644 index 0000000000..3b492e8e19 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/namespace usage.png differ diff --git a/app_python/docs/screenshots/lab16-shots/network.png b/app_python/docs/screenshots/lab16-shots/network.png new file mode 100644 index 0000000000..ac0c61c561 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/network.png differ diff --git a/app_python/docs/screenshots/lab16-shots/node cpu memory.png b/app_python/docs/screenshots/lab16-shots/node cpu memory.png new file mode 100644 index 0000000000..6b67c8c7d2 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/node cpu memory.png differ diff --git a/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png b/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png new file mode 100644 index 0000000000..b48bf4e190 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png differ diff --git a/app_python/docs/screenshots/lab16-shots/pods managed.png b/app_python/docs/screenshots/lab16-shots/pods managed.png new file mode 100644 index 0000000000..9920abeb46 Binary files /dev/null and b/app_python/docs/screenshots/lab16-shots/pods managed.png differ diff --git a/app_python/docs/screenshots/lab17-shots/dashboard.png b/app_python/docs/screenshots/lab17-shots/dashboard.png new file mode 100644 index 0000000000..46fb3d234c Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/dashboard.png differ diff --git a/app_python/docs/screenshots/lab17-shots/edge deployed.png b/app_python/docs/screenshots/lab17-shots/edge deployed.png new file mode 100644 index 0000000000..ddc26f2de3 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/edge deployed.png differ diff --git a/app_python/docs/screenshots/lab17-shots/endpoints local.png b/app_python/docs/screenshots/lab17-shots/endpoints local.png new file mode 100644 index 0000000000..b7a9612d97 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/endpoints local.png differ diff --git a/app_python/docs/screenshots/lab17-shots/logs.png b/app_python/docs/screenshots/lab17-shots/logs.png new file mode 100644 index 0000000000..ca44c916d5 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/logs.png differ diff --git a/app_python/docs/screenshots/lab17-shots/metrics.png b/app_python/docs/screenshots/lab17-shots/metrics.png new file mode 100644 index 0000000000..5646d44d46 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/metrics.png differ diff --git a/app_python/docs/screenshots/lab17-shots/persistance.png b/app_python/docs/screenshots/lab17-shots/persistance.png new file mode 100644 index 0000000000..d437a22939 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/persistance.png differ diff --git a/app_python/docs/screenshots/lab17-shots/secrets.png b/app_python/docs/screenshots/lab17-shots/secrets.png new file mode 100644 index 0000000000..a0d01bea32 Binary files /dev/null and b/app_python/docs/screenshots/lab17-shots/secrets.png differ diff --git a/app_python/k8s/ARGOCD.md b/app_python/k8s/ARGOCD.md new file mode 100644 index 0000000000..67c2551dda --- /dev/null +++ b/app_python/k8s/ARGOCD.md @@ -0,0 +1,214 @@ +# Documentation + +## ArgoCD Setup + +### Installation verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install argocd argo/argo-cd --namespace argocd +NAME: argocd +LAST DEPLOYED: Thu Apr 23 18:37:11 2026 +NAMESPACE: argocd +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +NOTES: +In order to access the server UI you have the following options: + +1. kubectl port-forward service/argocd-server -n argocd 8080:443 + + and then open the browser on http://localhost:8080 and accept the certificate + +2. enable ingress in the values file `server.ingress.enabled` and either + - Add the annotation for ssl passthrough: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-1-ssl-passthrough + - Set the `configs.params."server.insecure"` in the values file and terminate SSL at your ingress: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-2-multiple-ingress-objects-and-hosts + + +After reaching the UI the first time you can login with username: admin and the random password generated during the installation. You can find the password by running: + +kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d + +(You should delete the initial secret afterwards as suggested by the Getting Started Guide: https://argo-cd.readthedocs.io/en/stable/getting_started/#4-login-using-the-cli) +(devops) fountainer@Veronicas-MacBook-Air app_python % helm list -n argocd +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +argocd argocd 1 2026-04-23 18:37:11.582773 +0300 MSK deployed argo-cd-9.5.4 v3.3.8 +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd version +argocd: v3.3.8+7ae7d2c.dirty + BuildDate: 2026-04-21T20:19:34Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: dirty + GitTag: v3.3.8 + GoVersion: go1.26.2 + Compiler: gc + Platform: darwin/arm64 +argocd-server: v3.3.8 + BuildDate: 2026-04-21T17:19:47Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: clean + GitTag: v3.3.8 + GoVersion: go1.25.5 + Compiler: gc + Platform: linux/arm64 + Kustomize Version: v5.8.1 2026-02-09T16:15:27Z + Helm Version: v3.19.4+g7cfb6e4 + Kubectl Version: v0.34.0 + Jsonnet Version: v0.21.0 + ``` + +### UI access method + +Accessed ArgoCD UI via kubectl port-forward to localhost and logged in through the browser using the admin credentials and a previously created password. + +### CLI configuration + +Installed the argocd CLI with Homebrew and connected to the server using argocd login localhost:8080 --insecure. + +## Application Configuration + +### Application manifests + +Created an ArgoCD Application manifest defining the app name, project, source repository, and destination cluster settings. + +### Source and destination configuration + +Configured the application to pull from the Git repository (Helm chart path) and deploy to the in-cluster Kubernetes API in the default namespace. + +### Values file selection + +Specified values.yaml as the Helm values file to control application configuration during deployment. To test, I changed the number of replicas from 1 to 3. + +## Multi-Environment + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (22m ago) 22m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n prod +NAME READY STATUS RESTARTS AGE +python-app-prod-app-python-9dc9c6fbc-n8mtr 1/1 Running 0 4m14s +python-app-prod-app-python-9dc9c6fbc-sqflv 1/1 Running 0 21m +python-app-prod-app-python-9dc9c6fbc-xfvbj 1/1 Running 0 4m14s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app list +NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET +argocd/my-app https://kubernetes.default.svc default default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-dev https://kubernetes.default.svc dev default Synced Healthy Auto-Prune https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-prod https://kubernetes.default.svc prod default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +``` + +### Dev vs Prod configuration differences + +Dev uses smaller resource limits and fewer replicas for faster iteration, while prod uses higher replica count and stricter resource limits for stability and performance. + +### Sync policy differences and rationale + +Dev is configured with automated sync including self-heal and prune for continuous deployment, while prod uses manual sync to ensure controlled and reviewed releases. + +### Namespace separation + +Dev and prod are deployed into separate namespaces to isolate environments, prevent interference, and ensure independent lifecycle management. + +## Self-Healing Evidence + +### Manual scale test with before/after + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 30m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=5 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 31m +``` + +### Pod deletion test + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (31m ago) 32m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl delete pod python-app-dev-app-python-59fdf484d5-g97xn -n dev +pod "python-app-dev-app-python-59fdf484d5-g97xn" deleted +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 0/1 Running 0 15s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 1/1 Running 0 37s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Configuration drift test + +Here you can see the drift +![](./../docs/screenshots/lab13-shots/drift.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app get python-app-dev +Name: argocd/python-app-dev +Project: default +Server: https://kubernetes.default.svc +Namespace: dev +URL: https://argocd.example.com/applications/python-app-dev +Source: +- Repo: https://github.com/ffountainer/DevOps-Core-Course + Target: lab13 + Path: app_python/k8s/app_python + Helm Values: values-dev.yaml +SyncWindow: Sync Allowed +Sync Policy: Automated (Prune) +Sync Status: Synced to lab13 (27593a9) +Health Status: Healthy + +GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE +batch Job dev python-app-dev-app-python-pre-install Succeeded PreSync Reached expected number of succeeded pods + Secret dev app-credentials Synced secret/app-credentials configured + ConfigMap dev python-app-dev-app-python-env Synced configmap/python-app-dev-app-python-env unchanged + ConfigMap dev python-app-dev-app-python-config Synced configmap/python-app-dev-app-python-config unchanged + PersistentVolumeClaim dev python-app-dev-app-python-data Synced Healthy persistentvolumeclaim/python-app-dev-app-python-data unchanged + Service dev python-app-dev-app-python-service Synced Healthy service/python-app-dev-app-python-service unchanged +apps Deployment dev python-app-dev-app-python Synced Healthy deployment.apps/python-app-dev-app-python configured +batch Job dev python-app-dev-app-python-post-install Succeeded PostSync Reached expected number of succeeded pods +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=2 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 52m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl describe deployment python-app-dev-app-python -n dev | grep Replicas +Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable + Available True MinimumReplicasAvailable +``` + +### Explanation of behaviors + +- Explain when ArgoCD syncs vs when Kubernetes heals + +ArgoCD references changes in git, and Kubernetes monitors the cluster (it will heal if the pod crushes, etc) + +- What triggers ArgoCD sync? + +git repo changes, manual sync triggered, auto-sync enabled, drift detected + self-heal enabled + +- What is the sync interval? + +the default sync is every 3 minutes + +## Screenshots + +### ArgoCD UI showing the applications + +![](./../docs/screenshots/lab13-shots/application.png) + +### Sync status + +![](./../docs/screenshots/lab13-shots/sync%20status.png) +![](./../docs/screenshots/lab13-shots/sync.png) + +### Application details view + +![](./../docs/screenshots/lab13-shots/details.png) \ No newline at end of file diff --git a/app_python/k8s/CONFIGMAPS.md b/app_python/k8s/CONFIGMAPS.md new file mode 100644 index 0000000000..f79a158422 --- /dev/null +++ b/app_python/k8s/CONFIGMAPS.md @@ -0,0 +1,141 @@ +# Documentation + +## Application Changes + +### Description of visits counter implementation + +visits counter is a global integer that increases every time / endpoint is called. it writes the value into a file (visits) so it can survive pod restarts via pvc. + +### New endpoint documentation + +/visits returns the current stored counter value from data/visits file + +### Local testing evidence with Docker + +Here you can see the counter value persistence across restarts. +![](./../docs/screenshots/lab12-shots/counter.png) + +## ConfigMap Implementation + +### ConfigMap template structure + +Helm ConfigMap loads a local config.json file via .Files.Get, so the whole json is injected into the cluster as one config object + +### config.json content + +it contains basic app metadata like name, environment, and feature flags (debug, metrics) plus log level settings + +### How ConfigMap is mounted as file + +the ConfigMap is mounted as a volume at /config, so inside the container we can read /config/config.json as a normal file. + +### How ConfigMap provides environment variables + +by using envFrom: configMapRef to inject keys like APP_NAME, APP_ENV, and LOG_LEVEL directly as environment variables. + + +### Verification outputs + +- File content inside pod (cat /config/config.json) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- cat /config/config.json +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +}% +``` + +- Environment variables in pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep APP_ +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +APP_ENV=dev +APP_NAME=my-app +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PROTO=tcp +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_HOST=10.103.123.105 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_ADDR=10.103.123.105 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep LOG_LEVEL +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +LOG_LEVEL=info +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + +## Persistent Volume + +### PVC configuration explanation + +PVC requests a small storage size (100Mi) and creates a persistent volume that is mounted into the pod at /app/data to store the visits file. + +### Access modes and storage class discussion + +ReadWriteOnce is used because only one pod needs to write to the volume, and storageClass is optional so it uses the cluster default. + +### Volume mount configuration + +the volume is mounted into the container at /app/data, and the app writes visits file there so data stays after pod restarts/recreations + +### Persistence test evidence: + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-z6r7b -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl delete pod mysecretrelease-app-python-7b6579656c-z6r7b +pod "mysecretrelease-app-python-7b6579656c-z6r7b" deleted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7b6579656c-b2tzf 2/2 Running 0 60s +vault-0 1/1 Running 3 (84m ago) 8d +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 3 (85m ago) 8d +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-b2tzf -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## ConfigMap vs Secret + +### When to use ConfigMap + +ConfigMap is used for non-sensitive configuration like app name, environment, log level, and feature flags. + +### When to use Secret + +secret is used for sensitive data like username, password, or anything that should not be visible in plain text + +### Key differences + +configMap is plain text and not encrypted, while Secret is base64 encoded and used for sensitive data. + +## Additional Outputs: + +### kubectl get configmap,pvc output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get configmap,pvc +NAME DATA AGE +configmap/kube-root-ca.crt 1 22d +configmap/mysecretrelease-app-python-config 1 7m2s +configmap/mysecretrelease-app-python-env 3 7m2s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/mysecretrelease-app-python-data Bound pvc-42d4685f-8463-4434-8959-0bacd5d972b6 100Mi RWO standard 7m2s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + diff --git a/app_python/k8s/HELM.md b/app_python/k8s/HELM.md new file mode 100644 index 0000000000..e3f80de3ab --- /dev/null +++ b/app_python/k8s/HELM.md @@ -0,0 +1,499 @@ +# Documentation + +## Chart Overview + +### Chart structure explanation + +- the chart has templates/ for kubectl manifests, values.yaml for default settings, and _helpers.tpl for reusable template functions. Hooks are in templates/hooks/ for pre and post-install jobs + +### Key template files and their purpose + +- deployment.yaml defines the app pods and containers +- service.yaml exposes them, and hooks run tasks before or after install +- helpers in _helpers.tpl build names, labels, and selectors consistently, and then can be reused in different config files + +### Values organization strategy + +- values.yaml has defaults, while values-dev.yaml and values-prod.yaml override settings for different environments. +This keeps environment configs separate and easy to manage. + +## Configuration Guide + +### Important values and their purpose + +- replicaCount sets pod number, resources manage CPU/memory, service.type controls what role will the service have (NodePort vs LoadBalancer, etc). LivenessProbe and readinessProbe check pod health. + +### How to customize for different environments + +- you can use values-dev.yaml for dev with 1 replica and NodePort, values-prod.yaml for prod with more replicas and LoadBalancer. and you can also override values on install with --set + +### Example installations with different configurations + +- dev + +```bash +helm install myapp-dev k8s/app_python -f k8s/app_python/values-dev.yaml +``` + +- prod + +```bash +helm install myapp-prod k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +## Hook Implementation + +### What hooks you implemented and why + +- I implemented a pre-install job for setup tasks and a post-install job for validation, to simulate real lifecycle tasks + +### Hook execution order and weights + +- Pre-install has weight -5 to run early, post-install has weight 5 to run after deployment. + +### Deletion policies explanation + +- both hooks have hook-succeeded policy so they auto-delete after finishing successfully + +## Installation Evidence + +### Terminal output showing Helm installation and version (should be 4.x) + +```bash +==> Fetching downloads for: helm +✔︎ Bottle Manifest helm (4.1.3) Downloaded 7.4KB/ 7.4KB +✔︎ Bottle helm (4.1.3) Downloaded 18.1MB/ 18.1MB +==> Pouring helm--4.1.3.arm64_tahoe.bottle.tar.gz +🍺 /opt/homebrew/Cellar/helm/4.1.3: 69 files, 61.3MB +==> Running `brew cleanup helm`... +Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`. +Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). +==> Caveats +zsh completions have been installed to: + /opt/homebrew/share/zsh/site-functions +``` +### Output of exploring a public chart (e.g., helm show chart prometheus-community/prometheus) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm show chart prometheus-community/prometheus +annotations: + artifacthub.io/license: Apache-2.0 + artifacthub.io/links: | + - name: Chart Source + url: https://github.com/prometheus-community/helm-charts + - name: Upstream Project + url: https://github.com/prometheus/prometheus +apiVersion: v2 +appVersion: v3.11.0 +dependencies: +- condition: alertmanager.enabled + name: alertmanager + repository: https://prometheus-community.github.io/helm-charts + version: 1.34.* +- condition: kube-state-metrics.enabled + name: kube-state-metrics + repository: https://prometheus-community.github.io/helm-charts + version: 7.2.* +- condition: prometheus-node-exporter.enabled + name: prometheus-node-exporter + repository: https://prometheus-community.github.io/helm-charts + version: 4.52.* +- condition: prometheus-pushgateway.enabled + name: prometheus-pushgateway + repository: https://prometheus-community.github.io/helm-charts + version: 3.6.* +description: Prometheus is a monitoring system and time series database. +home: https://prometheus.io/ +icon: https://raw.githubusercontent.com/prometheus/prometheus.github.io/master/assets/prometheus_logo-cb55bb5c346.png +keywords: +- monitoring +- prometheus +kubeVersion: '>=1.19.0-0' +maintainers: +- email: gianrubio@gmail.com + name: gianrubio + url: https://github.com/gianrubio +- email: zanhsieh@gmail.com + name: zanhsieh + url: https://github.com/zanhsieh +- email: miroslav.hadzhiev@gmail.com + name: Xtigyro + url: https://github.com/Xtigyro +- email: naseem@transit.app + name: naseemkullah + url: https://github.com/naseemkullah +- email: rootsandtrees@posteo.de + name: zeritti + url: https://github.com/zeritti +name: prometheus +sources: +- https://github.com/prometheus/alertmanager +- https://github.com/prometheus/prometheus +- https://github.com/prometheus/pushgateway +- https://github.com/prometheus/node_exporter +- https://github.com/kubernetes/kube-state-metrics +type: application +version: 28.15.0 +``` + +### helm list output + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm list +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +my-python-app-dev default 1 2026-04-02 22:00:55.999506 +0300 MSK deployed app_python-0.1.0 1.0 +my-python-app-prod default 1 2026-04-02 22:12:24.157572 +0300 MSK deployed app_python-0.1.0 1.0 +myrelease default 1 2026-04-02 22:40:56.562009 +0300 MSK deployed app_python-0.1.0 1.0 +``` + +### kubectl get all showing deployed resources + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 43m +pod/my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 31m +pod/myrelease-app-python-569fb4b645-6v9dt 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-8ws5n 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-glt5r 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-qtg4j 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-rgppk 1/1 Running 0 2m32s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 8d +service/my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 43m +service/my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 31m +service/myrelease-app-python-service NodePort 10.107.17.3 80:30009/TCP 2m32s + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-python-app-dev-app-python 1/1 1 1 43m +deployment.apps/my-python-app-prod-app-python 0/5 5 0 31m +deployment.apps/myrelease-app-python 5/5 5 5 2m32s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-python-app-dev-app-python-7d7f699d85 1 1 1 43m +replicaset.apps/my-python-app-prod-app-python-74c5b97dd5 5 5 0 31m +replicaset.apps/myrelease-app-python-569fb4b645 5 5 5 2m32s +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Hook execution output (kubectl get jobs) + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get jobs -w +NAME STATUS COMPLETIONS DURATION AGE +myrelease-app-python-pre-install Running 0/1 0s +myrelease-app-python-pre-install Running 0/1 0s 0s +myrelease-app-python-pre-install Running 0/1 33s 33s +myrelease-app-python-pre-install Running 0/1 43s 43s +myrelease-app-python-pre-install SuccessCriteriaMet 0/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-post-install Running 0/1 0s +myrelease-app-python-post-install Running 0/1 0s 0s +myrelease-app-python-post-install Running 0/1 5s 5s +myrelease-app-python-post-install Running 0/1 15s 15s +myrelease-app-python-post-install SuccessCriteriaMet 0/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +``` + +### Different environment deployments (dev vs prod) + +- Dev + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-dev k8s/app_python -f k8s/app_python/values-dev.yaml +NAME: my-python-app-dev +LAST DEPLOYED: Thu Apr 2 22:00:55 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 2m20s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 2m27s +``` + +- Prod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-prod k8s/app_python -f k8s/app_python/values-prod.yaml +NAME: my-python-app-prod +LAST DEPLOYED: Thu Apr 2 22:12:24 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 12m +my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 12m +my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 49s +``` + + +## Operations + +### Installation commands used + +```bash +helm install name-of-new-release k8s/app_python -f k8s/app_python/values-for-new-release.yaml +``` +### How to upgrade a release + +```bash +helm upgrade myrelease k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +### How to rollback + +```bash +helm rollback myrelease 1 +``` + +### How to uninstall + +```bash +helm uninstall name-of-release +``` + +## Testing & Validation + +### helm lint output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % helm lint app_python +==> Linting app_python +[INFO] Chart.yaml: icon is recommended + +1 chart(s) linted, 0 chart(s) failed +``` +### helm template verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm template app-python k8s/app_python +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: app-python-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app-python-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 +``` + +### Dry-run output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install --dry-run --debug test-release k8s/app_python +level=WARN msg="--dry-run is deprecated and should be replaced with '--dry-run=client'" +level=DEBUG msg="Original chart version" version="" +level=DEBUG msg="Chart path" path=/Users/fountainer/uni/devops/DevOps-Core-Course/app_python/k8s/app_python +level=DEBUG msg="number of dependencies in the chart" chart=app_python dependencies=0 +NAME: test-release +LAST DEPLOYED: Thu Apr 2 21:46:17 2026 +NAMESPACE: default +STATUS: pending-install +REVISION: 1 +DESCRIPTION: Dry run complete +TEST SUITE: None +USER-SUPPLIED VALUES: +{} + +COMPUTED VALUES: +container: + port: 12345 +image: + pullPolicy: IfNotPresent + repository: fountainer/my-app + tag: 2026.03.26 +livenessProbe: + failureThreshold: 5 + initialDelaySeconds: 10 + path: /health + periodSeconds: 10 + timeoutSeconds: 5 +readinessProbe: + initialDelaySeconds: 5 + path: /ready + periodSeconds: 5 +replicaCount: 5 +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi +service: + nodePort: 30007 + port: 80 + protocol: TCP + targetPort: 12345 + type: NodePort +strategy: + maxSurge: 1 + maxUnavailable: 1 + +HOOKS: +MANIFEST: +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: test-release-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-release-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 + +``` +### Application accessibility verification + +![](./../docs/screenshots/lab10-shots/app-working.png) \ No newline at end of file diff --git a/app_python/k8s/MONITORING.md b/app_python/k8s/MONITORING.md new file mode 100644 index 0000000000..b71aebd57e --- /dev/null +++ b/app_python/k8s/MONITORING.md @@ -0,0 +1,227 @@ +# Documentation + +## Stack Components + +### Descriptions in your own words + +- Prometheus Operator: it's a kubernates tool that allows you to automate prometheus deployment and management. it provides a set of custom resource definitions, and you can make your own configuration with those. + +- Prometheus: it's a tool for monitoring and alerting, it stores metrics as a series of timestampts. + +- Alertmanager: an instruments to manage alerts. when metrics reach invalid state, alertmanager will receive alerts, group and send them to asignees. you can configure when to silence alerts or start reaching out to the next person if the first one is not responding (it's an escalation), and create other custom settings. + +- Grafana: it is a dashboard for tracking the current state of the system by visualising logs and metrics. you can define alert rules there to see if the new metric value is out of the valid tresholds. + +- kube-state-metrics: it's a service that exposes metrics related to kubernates objects, they are created automatically and describe your pods current state. + +- node-exporter: it's an agent that exposes internal metrics for a node (like cpu, memory, etc), then prometheus can scrape those metrics. + +## Installation Evidence + +### kubectl get po,svc -n monitoring + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo add prometheus-community https://prometheus-community.github.io/helm-charts +"prometheus-community" already exists with the same configuration, skipping +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo update +Hang tight while we grab the latest from your chart repositories... +...Successfully got an update from the "hashicorp" chart repository +...Successfully got an update from the "argo" chart repository +...Successfully got an update from the "prometheus-community" chart repository +Update Complete. ⎈Happy Helming!⎈ +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get po,svc -n monitoring +NAME READY STATUS RESTARTS AGE +pod/alertmanager-monitoring-kube-prometheus-alertmanager-0 2/2 Running 0 2m34s +pod/monitoring-grafana-bbc5c674-8cbd9 3/3 Running 0 2m56s +pod/monitoring-kube-prometheus-operator-54f68d65b4-99ck2 1/1 Running 0 2m56s +pod/monitoring-kube-state-metrics-5957bd45bc-5rpqr 1/1 Running 0 2m56s +pod/monitoring-prometheus-node-exporter-c8fg6 1/1 Running 0 2m57s +pod/prometheus-monitoring-kube-prometheus-prometheus-0 2/2 Running 0 2m34s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/alertmanager-operated ClusterIP None 9093/TCP,9094/TCP,9094/UDP 2m34s +service/monitoring-grafana ClusterIP 10.110.156.182 80/TCP 2m57s +service/monitoring-kube-prometheus-alertmanager ClusterIP 10.111.243.229 9093/TCP,8080/TCP 2m57s +service/monitoring-kube-prometheus-operator ClusterIP 10.99.16.80 443/TCP 2m57s +service/monitoring-kube-prometheus-prometheus ClusterIP 10.106.17.206 9090/TCP,8080/TCP 2m57s +service/monitoring-kube-state-metrics ClusterIP 10.102.26.186 8080/TCP 2m57s +service/monitoring-prometheus-node-exporter ClusterIP 10.100.205.92 9100/TCP 2m57s +service/prometheus-operated ClusterIP None 9090/TCP 2m34s +``` + +## Dashboard Answers + +### Pod Resources: CPU/memory usage of your StatefulSet + +Due to the pods and the app itself being very lightweight, CPU and memory usage never went higher than initially allocated resources (100m CPU and 128Mi memory). Even under high load (I used multiple loops with curl), the initial resources were enough. You will see more detailed usages info in the next question. + +Example for pod 2: + +![](./../docs/screenshots/lab16-shots/cpupod2.png) + +![](./../docs/screenshots/lab16-shots/memorypod2.png) + +### Namespace Analysis: Which pods use most/least CPU in default namespace? + +I decided to use Prometheus for evidence, since the resource usage was really low, and didn't show up properly in Grafana. + +curl I used (the first count is much bigger since I previously tested only with pod 2) + +![](./../docs/screenshots/lab16-shots/curl.png) + +usage + +![](./../docs/screenshots/lab16-shots/namespace%20usage.png) + +As we can see, all statefulset pods used roughly the same amount of CPU and memory resources. This is anticipated, because load balancing is used for routing traffic to different pods. + +### Node Metrics: Memory usage (% and MB), CPU cores + +CPU and Memory usage for the whole minikube node was: + +![](./../docs/screenshots/lab16-shots/node%20cpu%20memory.png) + +It is much higher than resources used in statefulset since the node contains all different namespaces in my minikube cluster. + +### Kubelet: How many pods/containers managed? + +16 pods and 41 containers + +![](./../docs/screenshots/lab16-shots/pods%20managed.png) + +### Network: Traffic for pods in default namespace + +![](./../docs/screenshots/lab16-shots/network.png) + +### Alerts: How many active alerts? Check Alertmanager UI + +![](./../docs/screenshots/lab16-shots/alert.png) + +## Init Containers: Implementation and proof of success + +I implemented two init container patterns. First one downloads a file with wget into a shared emptyDir volume and the main container successfully accessed it from /data/index.html. Second one uses a wait-for-service init container that continuously checks the nginx service with wget and only starts the main container after the service becomes reachable. + +### Init container + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 Init:0/1 0 2s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 PodInitializing 0 4s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 5s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +``` +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl logs init-download-pod -c init-download +Connecting to example.com (172.66.147.243:443) +wget: note: TLS certificate validation not implemented +saving to '/work-dir/index.html' +index.html 100% |********************************| 528 0:00:00 ETA +'/work-dir/index.html' saved +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec init-download-pod -- cat /data/index.html +Defaulted container "main-app" out of: main-app, init-download (init) +Example Domain

Example Domain

This domain is for use in documentation examples without needing permission. Avoid use in operations.

Learn more

+``` + +### Waiting for service container + +```bash +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f waiting.yaml +pod/wait-service-pod created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +wait-service-pod 0/1 Init:0/1 0 22s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl logs wait-service-pod -c wait-for-service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f nginx.yaml +deployment.apps/myservice created +service/myservice created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 3s +wait-service-pod 0/1 Init:0/1 0 55s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 5s +wait-service-pod 0/1 PodInitializing 0 57s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 7s +wait-service-pod 1/1 Running 0 59s +``` + +After service was discovered: + +```bash +waiting for service + + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, nginx is successfully installed and working. +Further configuration is required for the web server, reverse proxy, +API gateway, load balancer, content cache, or other features.

+ +

For online documentation and support please refer to +nginx.org.
+To engage with the community please visit +community.nginx.org.
+For enterprise grade support, professional services, additional +security features and capabilities please refer to +f5.com/nginx.

+ +

Thank you for using nginx.

+ + +``` \ No newline at end of file diff --git a/app_python/k8s/README.md b/app_python/k8s/README.md new file mode 100644 index 0000000000..20ea3e883d --- /dev/null +++ b/app_python/k8s/README.md @@ -0,0 +1,301 @@ +# Documentation + +## Architecture Overview + +### Diagram or description of your deployment architecture + +```mermaid +flowchart LR + +subgraph Kubernetes_Cluster["Minikube Kubernetes Cluster"] + + S[Service node port
port 80 -> nodePort 30007] + + subgraph Deployment["Deployment: my-app (5 replicas)"] + P1[Pod 1
app-python] + P2[Pod 2
app-python] + P3[Pod 3
app-python] + P4[Pod 4
app-python] + P5[Pod 5
app-python] + end + +end + +User[User / curl / browser] + +User -->|HTTP request
http://nodeIP:30007| S + +S -->|routes traffic via selector
app: my-app| P1 +S --> P2 +S --> P3 +S --> P4 +S --> P5 + +P1 -->|/health /ready /metrics| AppLogic[(Flask App)] +P2 --> AppLogic +P3 --> AppLogic +P4 --> AppLogic +P5 --> AppLogic + +``` + +### How many Pods, which Services, networking flow + +- I used 5 pods managed by a deployment and one NodePort service, where traffic goes from the service (port 80 / nodePort 30007) to the pods using the app: my-app label selector. + +### Resource allocation strategy + +- I defined small cpu and memory requests/limits (100m–500m cpu, 128Mi–256Mi memory) to keep the app stable and prevent it from using too many cluster resources. + +### Brief explanation of your chosen tool (minikube/kind) and why + +I used minikube because it’s easy to set up locally and lets me run a full kubernetes cluster on my machine, which is enough for testing deployments without needing a real cloud setup. + +## Manifest Files + +### Brief description of each manifest + +- Deployment: deployment.yml defines how my app runs in Kubernetes, including how many pods, which image to use, and how they are configured. + +- Service: service.yml exposes the app inside and outside the cluster by routing traffic to the pods created by the deployment. + +### Key configuration choices + +- Deployment: I set 3 replicas, added resource limits/requests, configured liveness and readiness probes, used labels for selection, and set a rolling update strategy + +- Service: I used NodePort type, matched the selector with app: my-app, set port 80 as the service port, and mapped it to container port 12345 with a fixed nodePort. + +### Why you chose specific values (replicas, resources, etc.) + +- Deployment: I used 3 replicas for basic availability, small cpu/memory values since the app is lightweight, and probes to make sure kubernetes can detect when the app is healthy and ready to serve traffic. + +- Service: I used NodePort so i can access the app locally with minikube, port 80 for convenience, targetPort 12345 to match the app, and a fixed nodePort (30007) to make testing easier. + +## Deployment Evidence + +### Successful cluster setup + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % minikube start + +😄 minikube v1.38.1 on Darwin 26.3 (arm64) +✨ Using the docker driver based on existing profile +👍 Starting "minikube" primary control-plane node in "minikube" cluster +🚜 Pulling base image v0.0.50 ... +🏃 Updating the running docker "minikube" container ... +🐳 Preparing Kubernetes v1.35.1 on Docker 29.2.1 ... +🔎 Verifying Kubernetes components... + ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5 +🌟 Enabled addons: default-storageclass, storage-provisioner + +❗ /Applications/Docker.app/Contents/Resources/bin/kubectl is version 1.32.2, which may have incompatibilities with Kubernetes 1.35.1. + ▪ Want kubectl v1.35.1? Try 'minikube kubectl -- get pods -A' +🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default +``` +### Output of kubectl cluster-info and kubectl get nodes + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl cluster-info +Kubernetes control plane is running at https://127.0.0.1:51390 +CoreDNS is running at https://127.0.0.1:51390/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy + +To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get nodes +NAME STATUS ROLES AGE VERSION +minikube Ready control-plane 6h45m v1.35.1 +``` + +### kubectl get all output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m11s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h58m + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-app-deployment 3/3 3 3 3m11s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-app-deployment-6f67848dfb 3 3 3 3m11s +``` +### kubectl get pods,svc with detailed view + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods,svc +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m35s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h59m +``` + +### kubectl describe deployment showing replicas and strategy + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 29s +my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 29s +my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 29s +``` +### Screenshot or curl output showing app working + +![](./../docs/screenshots/lab09-shots/curl%20app%20.png) + +### Service deployment + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 38m +my-app-service NodePort 10.98.179.244 80:30007/TCP 41s +(devops) fountainer@Veronicas-MacBook-Air k8s % minikube service my-app-service +┌───────────┬────────────────┬─────────────┬───────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼───────────────────────────┤ +│ default │ my-app-service │ 80 │ http://192.168.49.2:30007 │ +└───────────┴────────────────┴─────────────┴───────────────────────────┘ +🔗 Starting tunnel for service my-app-service. +┌───────────┬────────────────┬─────────────┬────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼────────────────────────┤ +│ default │ my-app-service │ │ http://127.0.0.1:57348 │ +└───────────┴────────────────┴─────────────┴────────────────────────┘ +🎉 Opening service default/my-app-service in default browser... +❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it. +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 42m +my-app-service NodePort 10.98.179.244 80:30007/TCP 4m16s +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl describe service my-app-service +Name: my-app-service +Namespace: default +Labels: +Annotations: +Selector: app=my-app +Type: NodePort +IP Family Policy: SingleStack +IP Families: IPv4 +IP: 10.98.179.244 +IPs: 10.98.179.244 +Port: 80/TCP +TargetPort: 12345/TCP +NodePort: 30007/TCP +Endpoints: 10.244.0.16:12345,10.244.0.14:12345,10.244.0.15:12345 +Session Affinity: None +External Traffic Policy: Cluster +Internal Traffic Policy: Cluster +Events: +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get endpoints +Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice +NAME ENDPOINTS AGE +kubernetes 192.168.49.2:8443 42m +my-app-service 10.244.0.14:12345,10.244.0.15:12345,10.244.0.16:12345 4m36s +(devops) fountainer@Veronicas-MacBook-Air k8s % +``` + +## Operations Performed + +### Commands used to deploy + +- ```bash kubectl apply -f k8s/deployment.yml``` +- ```bash kubectl apply -f k8s/service.yml``` +- ```bash kubectl get pods``` +- ```bash kubectl get services ``` + +### Scaling demonstration output + +![](./../docs/screenshots/lab09-shots/scaling.png) + +### Rolling update demonstration output + +I changed ```bash image: fountainer/my-app:latest``` to ```bash image: fountainer/my-app:2026.03.26```. + +![](./../docs/screenshots/lab09-shots/rollout.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout history deployment/my-app-deployment +deployment.apps/my-app-deployment +REVISION CHANGE-CAUSE +1 +2 +3 + +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout undo deployment/my-app-deployment +deployment.apps/my-app-deployment rolled back +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout status deployment/my-app-deployment +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +deployment "my-app-deployment" successfully rolled out +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-7b5479788b-bj4pt 1/1 Running 0 37s +my-app-deployment-7b5479788b-n2pnb 1/1 Running 0 12s +my-app-deployment-7b5479788b-nvzdr 1/1 Running 0 22s +my-app-deployment-7b5479788b-q9g66 1/1 Running 0 36s +my-app-deployment-7b5479788b-w2nc6 1/1 Running 0 22s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Service access method and verification + +I accessed the app using ```bash minikube service my-app-service ``` and verified it by sending requests with curl to endpoints like /health and /ready. + +## Production Considerations + +### What health checks did you implement and why? + +I implemented a liveness probe on /health to restart unhealthy containers and a readiness probe on /ready to ensure pods start receiving traffic only when they are ready to work. + +### Resource limits rationale + +- I set limits to prevent resource overuse, and requests to guarantee the pod gets enough cpu and memory to run reliably. + +### How would you improve this for production? + +- I would add proper logging/monitoring like we did in the previous labs, add autoscaling, consider other update strategies (like canary update). + +### Monitoring and observability strategy + +- In previous labs we used Prometheus for metrics and loki & promtail for logs, also Grafana for dashboard representation + +## Challenges & Solutions + +### Issues encountered + +- I didn't work with NodePort before so I has to stydy it a little bit. Also I didn't know about minikube. + +### How you debugged (logs, describe, events) + +- I researched StackOverflow and other sources, such as documentation for kubernetes and minikube + +### What you learned about Kubernetes + +- I studied kubernetes in the SRE course last semester so I was already pretty familiar with it. We didn't use NodePort service though, and also didn't set up our own cluster since the course team provided us with it. + diff --git a/app_python/k8s/ROLLOUTS.md b/app_python/k8s/ROLLOUTS.md new file mode 100644 index 0000000000..7a37244b4c --- /dev/null +++ b/app_python/k8s/ROLLOUTS.md @@ -0,0 +1,252 @@ +# Documentation + +## Argo Rollouts Setup + +### Installation verification + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl apply -n argo-rollouts -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml +customresourcedefinition.apiextensions.k8s.io/analysisruns.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/analysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/clusteranalysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/experiments.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/rollouts.argoproj.io created +serviceaccount/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-admin created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-edit created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-view created +clusterrolebinding.rbac.authorization.k8s.io/argo-rollouts created +configmap/argo-rollouts-config created +secret/argo-rollouts-notification-secret created +service/argo-rollouts-metrics created +deployment.apps/argo-rollouts created +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 54s +``` +```bash +==> Fetching downloads for: kubectl-argo-rollouts +✔︎ Formula kubectl-argo-rollouts (v1.8.3) Verified 130.1MB/130.1MB +==> Installing kubectl-argo-rollouts from argoproj/tap +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl argo rollouts version +kubectl-argo-rollouts: v1.8.3+49fa151 + BuildDate: 2025-06-04T22:19:21Z + GitCommit: 49fa1516cf71672b69e265267da4e1d16e1fe114 + GitTreeState: clean + GoVersion: go1.23.9 + Compiler: gc + Platform: darwin/amd64 +``` + +### Dashboard access + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 12m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 28s +``` + +![](./../docs/screenshots/lab14-shots/argo-dashboard-access.png) + +### Understand Rollout vs Deployment + +Rollout CRD vs Deployment + +- Rollout and Deployment are kinda similar and both have replicas, selector, template, strategy fields, they manage pod creation. But rollout has additional fields for strategy that allow to perform more controllable rollouts with specific configurations, like rolling an update for a group of users, not for all. + +Additional fields for progressive delivery + +- canary: allows gradual traffic shifting to a new version using steps (e.g., setWeight, pause) +- blueGreen: supports switching between old and new versions using separate services +- steps: defines staged rollout progression +- analysis: integrates automated checks (metrics, tests) during rollout +- pause: enables manual or timed pauses between steps +- trafficRouting: controls how traffic is split between versions (with ingress/service mesh) + + +## Canary Deployment + +### Strategy configuration explained + +The rollout uses a canary strategy to gradually shift traffic from the old version to the new one. It is configured in steps (20%, 40%, 60%, 80%, 100%) with pauses to allow validation and manual control. This approach reduces risk by exposing the new version to a small part of users before full deployment. + +### Step-by-step rollout progression (screenshots from dashboard) + +![](./../docs/screenshots/lab14-shots/canary-prom-1.png) +![](./../docs/screenshots/lab14-shots/canary-prom-2.png) +![](./../docs/screenshots/lab14-shots/canary-prom-3.png) + +### Promotion and abort demonstration + +Promotion (screenshots can be seen in the prev step) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ॥ Paused +Message: CanaryPauseStep +Strategy: Canary + Step: 1/9 + SetWeight: 20 + ActualWeight: 25 +Images: fountainer/my-app:16-04 (canary, stable) +Replicas: + Desired: 3 + Current: 4 + Updated: 1 + Ready: 4 + Available: 4 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ॥ Paused 17m +├──# revision:2 +│ └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 69s canary +│ └──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 68s ready:1/1 +└──# revision:1 + └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet ✔ Healthy 17m stable + ├──□ myapp-app-python-5bc87cfdf6-2tzkc Pod ✔ Running 17m ready:1/1 + ├──□ myapp-app-python-5bc87cfdf6-bnpd6 Pod ✔ Running 17m ready:1/1 + └──□ myapp-app-python-5bc87cfdf6-qfg9s Pod ✔ Running 17m ready:1/1 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts promote myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' promoted +``` + +Abort + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get rollouts -n argo-rollouts +NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE +myapp-app-python 3 4 1 4 31m +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts abort myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' aborted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ✖ Degraded +Message: RolloutAborted: Rollout aborted update to revision 3 +Strategy: Canary + Step: 0/9 + SetWeight: 0 + ActualWeight: 0 +Images: fountainer/my-app:16-04 (stable) +Replicas: + Desired: 3 + Current: 3 + Updated: 0 + Ready: 3 + Available: 3 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ✖ Degraded 32m +├──# revision:3 +│ └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet • ScaledDown 32m canary +└──# revision:2 + └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 16m stable + ├──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 16m ready:1/1 + ├──□ myapp-app-python-76b59b6c66-7cwr4 Pod ✔ Running 10m ready:1/1 + └──□ myapp-app-python-76b59b6c66-skfdd Pod ✔ Running 10m ready:1/1 +``` +![](./../docs/screenshots/lab14-shots/canary-abort.png) + +## Blue-Green Deployment + +### Strategy configuration explained + +The blue-green strategy uses two environments: active and preview. The preview service runs the new version while the active service continues serving production traffic. After testing, the active service is switched to the new version instantly when promoted. This allows safe testing before release and quick rollback if needed. + +### Preview vs active service + +The active service is used by users in production and always points to the stable version. The preview service is used to test the new version before it is promoted. This separation ensures the new version can be verified without affecting real users. + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-preview 8081:80 -n argo-rollouts +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-service 8080:80 -n argo-rollouts +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +``` + +### Promotion process + +Promotion + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm upgrade --install myapp . -n argo-rollouts +Release "myapp" has been upgraded. Happy Helming! +NAME: myapp +LAST DEPLOYED: Thu Apr 30 23:12:57 2026 +NAMESPACE: argo-rollouts +STATUS: deployed +REVISION: 9 +DESCRIPTION: Upgrade complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods -n argo-rollouts +kubectl get svc -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 6h24m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 6h12m +myapp-app-python-76b59b6c66-7cwr4 1/1 Running 0 37m +myapp-app-python-76b59b6c66-pgtgq 1/1 Running 0 43m +myapp-app-python-76b59b6c66-skfdd 1/1 Running 0 37m +myapp-app-python-f7cddd7c7-5nvtx 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-xng4z 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-zjfpv 1/1 Running 0 12m +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +argo-rollouts-dashboard ClusterIP 10.106.240.192 3100/TCP 6h12m +argo-rollouts-metrics ClusterIP 10.109.176.51 8090/TCP 6h24m +myapp-app-python-preview ClusterIP 10.97.144.248 80/TCP 16m +myapp-app-python-service NodePort 10.101.217.107 80:30009/TCP 59m +``` + +![](./../docs/screenshots/lab14-shots/blue-green-1.png) +![](./../docs/screenshots/lab14-shots/bg-2.png) +![](./../docs/screenshots/lab14-shots/bg-4.png) + +Rollback + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts undo myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' undo +``` +![](./../docs/screenshots/lab14-shots/bg-5.png) + + +## Strategy Comparison + +### When to use canary vs blue-green + +canary is used when you want to slowly roll out changes to users and reduce risk step by step. blue-green is used when you want an instant switch between versions after testing + +### Pros and cons of each + +- canary is safer for production because it exposes changes gradually, but it takes longer and is more complex to monitor + +- blue-green is faster and simpler at switch time, but requires double resources and has less gradual control. + +### Your recommendation for different scenarios + +use canary for production systems where stability is critical. use blue-green for fast releases or when you want quick testing and instant rollback. + +## CLI Commands Reference + +### Commands you used + +```kubectl argo rollouts get rollout -w``` is used to watch rollout progress. ```kubectl argo rollouts promote``` is used to move to the next step in canary or switch in blue-green. ```kubectl argo rollouts undo``` is used to rollback to the previous version. + +### Monitoring and troubleshooting + +```kubectl get pods```, ```kubectl get svc```, and ```kubectl describe rollout``` are used to check cluster state and debug issues. dashboard is used to visually monitor rollout progress and traffic changes. \ No newline at end of file diff --git a/app_python/k8s/SECRETS.md b/app_python/k8s/SECRETS.md new file mode 100644 index 0000000000..1c62eae2de --- /dev/null +++ b/app_python/k8s/SECRETS.md @@ -0,0 +1,197 @@ +# Documentation + +## Kubernetes Secrets + +### Output of creating and viewing your secret + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl create secret generic app-credentials --from-literal=username=fountainer --from-literal=password=‘mypass293i20@@nekf’ +secret/app-credentials created +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get secret app-credentials -o yaml +apiVersion: v1 +data: + password: 4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ + username: Zm91bnRhaW5lcg== +kind: Secret +metadata: + creationTimestamp: "2026-04-07T14:46:16Z" + name: app-credentials + namespace: default + resourceVersion: "24859" + uid: 6997ca85-68fa-4278-9d51-a6531df977e9 +type: Opaque +``` +### Decoded secret values demonstration + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ" | base64 -d +‘mypass293i20@@nekf’% +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "Zm91bnRhaW5lcg==" | base64 -d +fountainer% +``` +### Explanation of base64 encoding vs encryption + +- Encoding is when we use some publicly accesible algorithm to encode our data. The goal is keeping integrity and usability of the data, it is not really about security. + +- In turn, Encryption is about securuty. It envolves encrypting with an algorithm that can be only resolved by a user who has an encryption key. + +## Helm Secret Integration + +### Chart structure showing secrets.yaml + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % tree app_python/k8s/app_python +app_python/k8s/app_python +├── Chart.yaml +├── charts +├── templates +│ ├── _helpers.tpl +│ ├── deployment.yaml +│ ├── hooks +│ │ ├── post-install-job.yaml +│ │ └── pre-install-job.yaml +│ ├── secrets.yaml +│ └── service.yaml +├── values-dev.yaml +├── values-prod.yaml +└── values.yaml +``` + +### How secrets are consumed in deployment + +- I have $secretName variable that is dynamically set to the name from the values.yaml and values I provide in the helm install command OR defaults to the value from helper. + +### Verification output (env vars in pod, excluding actual values) + +- in pod I have correct env vars: + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-7975557578-6zc9m -- sh +$ echo $PASSWORD +mypass293i20@@nekf +$ echo $USERNAME +fountainer +``` + +- and outside the secrets are hidden + +from ```bash kubectl describe pod mysecretrelease-app-python-7975557578-6zc9m``` + +```bash +Environment: + PASSWORD: Optional: false + USERNAME: Optional: false +``` + + +## Resource Management + +### Resource limits configuration + +```bash +resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} +``` +- in values.yaml I have + +```bash +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" +``` + +### Explanation of requests vs limits + +- requests is a setting that shows kubernates how much resources are needed for a container to run +- limits show how much resources a container is allowed to use (max) + +### How to choose appropriate values + +- you should analyze what processes does your container run and how many cpu/memory it may need +- values can be adjusted by observing the running container +- if you have multiple containers/pods you should constraint them in such a way that they all can work without throttling +- if the memory limit is too low the container can be killed right away + +## Vault Integration + +### Vault installation verification (kubectl get pods) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7975557578-6zc9m 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-7l4tv 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-bqnpd 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-cjjcb 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-st2jd 1/1 Running 0 63m +vault-0 1/1 Running 0 8m23s +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 0 8m23s +``` + +### Policy and role configuration (sanitized) + +- policy + +```bash +/ $ vault policy write myapp-policy /tmp/myapp-policy.hcl +Success! Uploaded policy: myapp-policy +/ $ vault policy read myapp-policy +path "secret/data/myapp/config" { + capabilities = ["read"] +} +``` + +- role config + +```bash +vault write auth/kubernetes/role/myapp-role \ + bound_service_account_names=default \ + bound_service_account_namespaces=default \ + policies=myapp-policy \ + ttl=48h +``` + + +### Proof of secret injection (show file exists, path structure) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-558b98bb9d-8299m -- /bin/sh +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +$ ls -l /vault/secrets +total 4 +-rw-r--r-- 1 100 newuser 180 Apr 7 23:55 config +$ cat /vault/secrets/config +data: map[password:mypass293i20@@nekf username:fountainer] +metadata: map[created_time:2026-04-07T23:32:33.85543147Z custom_metadata: deletion_time: destroyed:false version:1] +$ +``` + +### Explanation of the sidecar injection pattern + +- now every pod contains not only my app container but also vault sidecar container +- vault is able to authenticate in kubernates and inject secrets into the pod + +## Security Analysis + +### Comparison: K8s Secrets vs Vault + +- kubernates secrets are just encoded into base 64 and everyone who gets access to the cluster can decode them and get sensitive data, on the other hand, vault provides data encryption that is much more safer since you need an encryption key to encrypt it + +### When to use each approach + +- encoding is good for keeping data usability and integrity, so different machines can use it (like for seeing special symbols on a web page), it is like... more secure than nothing, but not reeally secure + +- vault encryption is needed for keeping sensitive data secure, like for storing passwords for the services on the virtual machines, etc + +### Production recommendations + +- in production you should always try to use strong encryption algorithms to keep your data secure diff --git a/app_python/k8s/STATEFULSET.md b/app_python/k8s/STATEFULSET.md new file mode 100644 index 0000000000..b5a5b60b72 --- /dev/null +++ b/app_python/k8s/STATEFULSET.md @@ -0,0 +1,168 @@ +# Documentation + +## StatefulSet Overview + +### Why StatefulSet + +It is used when pods need stable identity and storage, like each pod keeping its own data and name even after restart. + +### Differences from Deployment + +Key differences: +- deployment pods are interchangeable and can change names/storage after restarts, while statefulset pods have fixed names (pod-0, pod-1) and their own persistent storage. + +When to use Deployment vs StatefulSet: +- deployment is used for stateless apps (like web servers), and statefulset for apps that need stable data and identity (like databases). + +Examples of stateful workloads: +- databases like mysql/postgresql, message queues, systems like elasticsearch + +### Headless Services + +What is a headless service (clusterIP: None)? +- a service without a cluster ip that lets you directly access individual pods instead of load balancing + +How DNS works with StatefulSets? +- each pod gets its own dns name like pod-0.service-name.namespace.svc.cluster.local, and they can be addressed individually + +## Resource Verification + +### Output of kubectl get pod,sts,svc,pvc + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get statefulset +NAME READY AGE +myapp-app-python 3/3 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +myapp-app-python-0 1/1 Running 0 42s +myapp-app-python-1 1/1 Running 0 25s +myapp-app-python-2 1/1 Running 0 16s +myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 42s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pvc +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 53s +data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 36s +data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 27s +my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 53s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod,sts,svc,pvc +NAME READY STATUS RESTARTS AGE +pod/my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +pod/my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +pod/my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +pod/myapp-app-python-0 1/1 Running 0 99s +pod/myapp-app-python-1 1/1 Running 0 82s +pod/myapp-app-python-2 1/1 Running 0 73s +pod/myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 99s + +NAME READY AGE +statefulset.apps/myapp-app-python 3/3 99s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 7d8h +service/myapp-app-python-preview ClusterIP 10.98.51.97 80/TCP 99s +service/myapp-app-python-service NodePort 10.110.182.139 80:30009/TCP 99s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 99s +persistentvolumeclaim/data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 82s +persistentvolumeclaim/data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 73s +persistentvolumeclaim/my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +persistentvolumeclaim/myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 99s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## Network Identity + +### DNS resolution outputs + +the naming pattern is ```.``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec -it myapp-app-python-0 -- /bin/sh +$ getent hosts myapp-app-python-0.myapp-app-python-headless +10.244.0.177 myapp-app-python-0.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-1.myapp-app-python-headless +10.244.0.179 myapp-app-python-1.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-2.myapp-app-python-headless +10.244.0.180 myapp-app-python-2.myapp-app-python-headless.default.svc.cluster.local +``` + +## Per-Pod Storage Evidence + +### Different visit counts per pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-0 8080:12345 +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % pyenv shell devops +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-1 8081:12345 +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-2 8082:12345 +Forwarding from 127.0.0.1:8082 -> 12345 +Forwarding from [::1]:8082 -> 12345 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8080/visits +{ + "visits": 13 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8081/visits +{ + "visits": 7 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8082/visits +{ + "visits": 2 +} +``` + +![](./../docs/screenshots/lab15-shots/visits-diff.png) + +## Persistence Test + +### data survives pod deletion + +![](./../docs/screenshots/lab15-shots/pers_test.png) \ No newline at end of file diff --git a/app_python/k8s/app_python/.helmignore b/app_python/k8s/app_python/.helmignore new file mode 100644 index 0000000000..0e8a0eb36f --- /dev/null +++ b/app_python/k8s/app_python/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/app_python/k8s/app_python/Chart.yaml b/app_python/k8s/app_python/Chart.yaml new file mode 100644 index 0000000000..3e280aeaaf --- /dev/null +++ b/app_python/k8s/app_python/Chart.yaml @@ -0,0 +1,16 @@ +apiVersion: v2 +name: app_python +description: My Python application Helm chart + +type: application +version: 0.1.0 +appVersion: "1.0" + +keywords: + - python + - web +maintainers: + - name: Veronika Levasheva + email: veronikalev2005@gmail.com +sources: + - https://github.com/ffountainer/DevOps-Core-Course diff --git a/app_python/k8s/app_python/files/config.json b/app_python/k8s/app_python/files/config.json new file mode 100644 index 0000000000..ba8808215e --- /dev/null +++ b/app_python/k8s/app_python/files/config.json @@ -0,0 +1,11 @@ +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +} \ No newline at end of file diff --git a/app_python/k8s/app_python/service-headless.yaml b/app_python/k8s/app_python/service-headless.yaml new file mode 100644 index 0000000000..04d26061e8 --- /dev/null +++ b/app_python/k8s/app_python/service-headless.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-headless +spec: + clusterIP: None + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/_helpers.tpl b/app_python/k8s/app_python/templates/_helpers.tpl new file mode 100644 index 0000000000..2697f80e58 --- /dev/null +++ b/app_python/k8s/app_python/templates/_helpers.tpl @@ -0,0 +1,50 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "mychart.name" -}} +{{- default .Chart.Name .Values.nameOverride | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +*/}} +{{- define "mychart.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := include "mychart.name" . }} +{{- printf "%s-%s" .Release.Name $name | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} + +{{/* +Chart name and version. +*/}} +{{- define "mychart.chart" -}} +{{ .Chart.Name }}-{{ .Chart.Version }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "mychart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "mychart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Common labels. +*/}} +{{- define "mychart.labels" -}} +helm.sh/chart: {{ include "mychart.chart" . }} +{{ include "mychart.selectorLabels" . }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Service name helper. +*/}} +{{- define "mychart.serviceName" -}} +{{ include "mychart.fullname" . }}-service +{{- end }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/configmap-env.yaml b/app_python/k8s/app_python/templates/configmap-env.yaml new file mode 100644 index 0000000000..f41c2df7d3 --- /dev/null +++ b/app_python/k8s/app_python/templates/configmap-env.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "mychart.fullname" . }}-env + labels: + app: {{ include "mychart.name" . }} +data: + APP_NAME: {{ .Values.appName | quote }} + APP_ENV: {{ .Values.environment | quote }} + LOG_LEVEL: {{ .Values.logLevel | quote }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/configmap.yaml b/app_python/k8s/app_python/templates/configmap.yaml new file mode 100644 index 0000000000..a037d8bd60 --- /dev/null +++ b/app_python/k8s/app_python/templates/configmap.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "mychart.fullname" . }}-config + labels: + app: {{ include "mychart.name" . }} +data: + config.json: |- +{{ .Files.Get "files/config.json" | indent 4 }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/deployment.yaml b/app_python/k8s/app_python/templates/deployment.yaml new file mode 100644 index 0000000000..c3cada620d --- /dev/null +++ b/app_python/k8s/app_python/templates/deployment.yaml @@ -0,0 +1,82 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: {{ .Values.strategy.maxUnavailable }} + maxSurge: {{ .Values.strategy.maxSurge }} + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + - name: data-volume + persistentVolumeClaim: + claimName: {{ include "mychart.fullname" . }}-data \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/headless-service.yaml b/app_python/k8s/app_python/templates/headless-service.yaml new file mode 100644 index 0000000000..04d26061e8 --- /dev/null +++ b/app_python/k8s/app_python/templates/headless-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-headless +spec: + clusterIP: None + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/hooks/post-install-job.yaml b/app_python/k8s/app_python/templates/hooks/post-install-job.yaml new file mode 100644 index 0000000000..578cdf58a7 --- /dev/null +++ b/app_python/k8s/app_python/templates/hooks/post-install-job.yaml @@ -0,0 +1,26 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ include "mychart.fullname" . }}-post-install" + labels: + {{- include "mychart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": post-install + "helm.sh/hook-weight": "5" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + name: "{{ include "mychart.fullname" . }}-post-install" + spec: + restartPolicy: Never + containers: + - name: post-install-job + image: busybox + command: + - sh + - -c + - | + echo "Post-install validation running" + sleep 10 + echo "Validation passed" \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml b/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml new file mode 100644 index 0000000000..90018bbb06 --- /dev/null +++ b/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml @@ -0,0 +1,26 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ include "mychart.fullname" . }}-pre-install" + labels: + {{- include "mychart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": pre-install + "helm.sh/hook-weight": "-5" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + name: "{{ include "mychart.fullname" . }}-pre-install" + spec: + restartPolicy: Never + containers: + - name: pre-install-job + image: busybox + command: + - sh + - -c + - | + echo "Pre-install task running" + sleep 10 + echo "Pre-install completed" \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/init.yaml b/app_python/k8s/app_python/templates/init.yaml new file mode 100644 index 0000000000..e7b959f9de --- /dev/null +++ b/app_python/k8s/app_python/templates/init.yaml @@ -0,0 +1,31 @@ +apiVersion: v1 +kind: Pod +metadata: + name: init-download-pod + namespace: default +spec: + initContainers: + - name: init-download + image: busybox:1.36 + command: + - sh + - -c + - wget -O /work-dir/index.html https://example.com + volumeMounts: + - name: workdir + mountPath: /work-dir + + containers: + - name: main-app + image: busybox:1.36 + command: + - sh + - -c + - sleep 3600 + volumeMounts: + - name: workdir + mountPath: /data + + volumes: + - name: workdir + emptyDir: {} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/nginx.yaml b/app_python/k8s/app_python/templates/nginx.yaml new file mode 100644 index 0000000000..cc07ba1e93 --- /dev/null +++ b/app_python/k8s/app_python/templates/nginx.yaml @@ -0,0 +1,32 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myservice + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: myservice + template: + metadata: + labels: + app: myservice + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 +--- +apiVersion: v1 +kind: Service +metadata: + name: myservice + namespace: default +spec: + selector: + app: myservice + ports: + - port: 80 + targetPort: 80 \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/preview-service.yaml b/app_python/k8s/app_python/templates/preview-service.yaml new file mode 100644 index 0000000000..e59503c982 --- /dev/null +++ b/app_python/k8s/app_python/templates/preview-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-preview +spec: + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.service.targetPort }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/pvc.yaml b/app_python/k8s/app_python/templates/pvc.yaml new file mode 100644 index 0000000000..a930a3cb18 --- /dev/null +++ b/app_python/k8s/app_python/templates/pvc.yaml @@ -0,0 +1,17 @@ +{{- if .Values.persistence.enabled }} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "mychart.fullname" . }}-data + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.persistence.size }} + {{- if .Values.persistence.storageClass }} + storageClassName: {{ .Values.persistence.storageClass }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/rollout.yaml b/app_python/k8s/app_python/templates/rollout.yaml new file mode 100644 index 0000000000..6d4e687d75 --- /dev/null +++ b/app_python/k8s/app_python/templates/rollout.yaml @@ -0,0 +1,86 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: argoproj.io/v1alpha1 +kind: Rollout +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + - name: data-volume + persistentVolumeClaim: + claimName: {{ include "mychart.fullname" . }}-data + + strategy: + blueGreen: + activeService: {{ include "mychart.fullname" . }}-service + previewService: {{ include "mychart.fullname" . }}-preview + autoPromotionEnabled: false \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/secrets.yaml b/app_python/k8s/app_python/templates/secrets.yaml new file mode 100644 index 0000000000..2460f91d72 --- /dev/null +++ b/app_python/k8s/app_python/templates/secrets.yaml @@ -0,0 +1,12 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: v1 +kind: Secret +metadata: + name: {{ $secretName }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +type: Opaque +stringData: + username: {{ .Values.secret.data.username | quote }} + password: {{ .Values.secret.data.password | quote }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/service.yaml b/app_python/k8s/app_python/templates/service.yaml new file mode 100644 index 0000000000..558f0675e3 --- /dev/null +++ b/app_python/k8s/app_python/templates/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-service +spec: + type: {{ .Values.service.type }} + selector: + {{- include "mychart.selectorLabels" . | nindent 6 }} + ports: + - protocol: {{ .Values.service.protocol }} + port: {{ .Values.service.port }} + targetPort: {{ .Values.service.targetPort }} + nodePort: {{ .Values.service.nodePort }} \ No newline at end of file diff --git a/app_python/k8s/app_python/templates/statefulset.yaml b/app_python/k8s/app_python/templates/statefulset.yaml new file mode 100644 index 0000000000..333b200ca0 --- /dev/null +++ b/app_python/k8s/app_python/templates/statefulset.yaml @@ -0,0 +1,84 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + serviceName: {{ include "mychart.fullname" . }}-headless + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + volumeClaimTemplates: + - metadata: + name: data-volume + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: {{ .Values.persistence.size }} diff --git a/app_python/k8s/app_python/templates/waiting.yaml b/app_python/k8s/app_python/templates/waiting.yaml new file mode 100644 index 0000000000..12a9e5575b --- /dev/null +++ b/app_python/k8s/app_python/templates/waiting.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Pod +metadata: + name: wait-service-pod + namespace: default +spec: + initContainers: + - name: wait-for-service + image: busybox:1.36 + command: + - sh + - -c + - until wget -qO- http://myservice.default.svc.cluster.local; do echo waiting for service; sleep 2; done + containers: + - name: main-app + image: busybox:1.36 + command: + - sh + - -c + - echo Service found! && sleep 3600 \ No newline at end of file diff --git a/app_python/k8s/app_python/values-dev.yaml b/app_python/k8s/app_python/values-dev.yaml new file mode 100644 index 0000000000..81c63488ec --- /dev/null +++ b/app_python/k8s/app_python/values-dev.yaml @@ -0,0 +1,40 @@ +replicaCount: 1 + +image: + repository: fountainer/my-app + tag: "16-04" + pullPolicy: Always + +container: + port: 12345 + +resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 5 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 3 + +readinessProbe: + path: /ready + initialDelaySeconds: 2 + periodSeconds: 5 + +service: + type: NodePort + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30007 \ No newline at end of file diff --git a/app_python/k8s/app_python/values-prod.yaml b/app_python/k8s/app_python/values-prod.yaml new file mode 100644 index 0000000000..0c3548c5d2 --- /dev/null +++ b/app_python/k8s/app_python/values-prod.yaml @@ -0,0 +1,40 @@ +replicaCount: 3 + +image: + repository: fountainer/my-app + tag: "16-04" + pullPolicy: Always + +container: + port: 12345 + +resources: + requests: + cpu: "200m" + memory: "256Mi" + limits: + cpu: "500m" + memory: "512Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 30 + periodSeconds: 5 + timeoutSeconds: 5 + failureThreshold: 5 + +readinessProbe: + path: /ready + initialDelaySeconds: 10 + periodSeconds: 3 + +service: + type: LoadBalancer + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30008 \ No newline at end of file diff --git a/app_python/k8s/app_python/values.yaml b/app_python/k8s/app_python/values.yaml new file mode 100644 index 0000000000..b0ce838464 --- /dev/null +++ b/app_python/k8s/app_python/values.yaml @@ -0,0 +1,60 @@ +replicaCount: 3 + +image: + repository: fountainer/my-app + tag: "2026.05.01" + pullPolicy: Always + +container: + port: 12345 + +persistence: + size: 1Gi + +storageClass: "" + +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + +readinessProbe: + path: /ready + initialDelaySeconds: 5 + periodSeconds: 5 + +service: + type: NodePort + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30009 + +secret: + name: app-credentials + data: + username: "placeholder-user" + password: "placeholder-password" + +appName: my-app +environment: dev +logLevel: info + +persistence: + enabled: true + size: 100Mi + storageClass: "" \ No newline at end of file diff --git a/app_python/k8s/app_python/vault/myapp-policy.hcl b/app_python/k8s/app_python/vault/myapp-policy.hcl new file mode 100644 index 0000000000..230c2125cb --- /dev/null +++ b/app_python/k8s/app_python/vault/myapp-policy.hcl @@ -0,0 +1,3 @@ +path "secret/data/myapp/config" { + capabilities = ["read"] +} \ No newline at end of file diff --git a/app_python/k8s/argocd/application-dev.yaml b/app_python/k8s/argocd/application-dev.yaml new file mode 100644 index 0000000000..587c67c97d --- /dev/null +++ b/app_python/k8s/argocd/application-dev.yaml @@ -0,0 +1,26 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: python-app-dev + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values-dev.yaml + + destination: + server: https://kubernetes.default.svc + namespace: dev + + syncPolicy: + automated: + prune: true + selfHeal: true + syncOptions: + - CreateNamespace=true \ No newline at end of file diff --git a/app_python/k8s/argocd/application-prod.yaml b/app_python/k8s/argocd/application-prod.yaml new file mode 100644 index 0000000000..9ada9f09eb --- /dev/null +++ b/app_python/k8s/argocd/application-prod.yaml @@ -0,0 +1,23 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: python-app-prod + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values-prod.yaml + + destination: + server: https://kubernetes.default.svc + namespace: prod + + syncPolicy: + syncOptions: + - CreateNamespace=true \ No newline at end of file diff --git a/app_python/k8s/argocd/application.yaml b/app_python/k8s/argocd/application.yaml new file mode 100644 index 0000000000..72e9fd692c --- /dev/null +++ b/app_python/k8s/argocd/application.yaml @@ -0,0 +1,21 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: my-app + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values.yaml + + destination: + server: https://kubernetes.default.svc + namespace: default + + syncPolicy: {} \ No newline at end of file diff --git a/app_python/k8s/deployment.yml b/app_python/k8s/deployment.yml new file mode 100644 index 0000000000..8be175e432 --- /dev/null +++ b/app_python/k8s/deployment.yml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-app-deployment + labels: + app: my-app +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app: my-app + template: + metadata: + labels: + app: my-app + spec: + containers: + - name: app-python + image: fountainer/my-app:2026.03.26 + ports: + - containerPort: 12345 + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 \ No newline at end of file diff --git a/app_python/monitoring/docker-compose.yml b/app_python/monitoring/docker-compose.yml new file mode 100644 index 0000000000..84f8d57b49 --- /dev/null +++ b/app_python/monitoring/docker-compose.yml @@ -0,0 +1,138 @@ +services: + loki: + image: grafana/loki:3.0.0 + container_name: loki + ports: + - "3100:3100" + command: -config.file=/etc/loki/config.yml + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + volumes: + - ./loki/config.yml:/etc/loki/config.yml + - loki-data:/loki + networks: + - logging + deploy: + resources: + limits: + cpus: "1.0" + memory: 1G + reservations: + cpus: "0.50" + memory: 512M + promtail: + image: grafana/promtail:3.0.0 + container_name: promtail + ports: + - "9080:9080" + command: -config.file=/etc/promtail/config.yml + volumes: + - ./promtail/config.yml:/etc/promtail/config.yml + - /var/lib/docker/containers:/var/lib/docker/containers:ro + - /var/run/docker.sock:/var/run/docker.sock:ro + depends_on: + - loki + networks: + - logging + deploy: + resources: + limits: + cpus: "0.25" + memory: 256M + reservations: + cpus: "0.10" + memory: 128M + grafana: + image: grafana/grafana:12.3.1 + container_name: grafana + ports: + - "3000:3000" + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + environment: + - GF_AUTH_ANONYMOUS_ENABLED=false + - GF_SECURITY_ADMIN_PASSWORD=${GF_PASSWORD} + - GF_SECURITY_ADMIN_EMAIL=${GF_EMAIL} + volumes: + - grafana-data:/var/lib/grafana + depends_on: + - loki + networks: + - logging + deploy: + resources: + limits: + cpus: "0.5" + memory: 512M + reservations: + cpus: "0.25" + memory: 256M + app-python: + image: fountainer/my-app:latest + container_name: app-python + ports: + - "1999:12345" + healthcheck: + test: ["CMD-SHELL", "curl -f http://localhost:12345/health || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + networks: + - logging + labels: + logging: "promtail" + app: "my-app" + deploy: + resources: + limits: + cpus: "0.5" + memory: 256M + reservations: + cpus: "0.10" + memory: 128M + prometheus: + image: prom/prometheus:v3.9.0 + container_name: prometheus + ports: + - "9090:9090" + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:9090/-/healthy || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + command: + - --config.file=/etc/prometheus/prometheus.yml + - --storage.tsdb.retention.time=15d + - --storage.tsdb.retention.size=10GB + volumes: + - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml + - prometheus-data:/prometheus + networks: + - logging + depends_on: + - app-python + - loki + - grafana + deploy: + resources: + limits: + cpus: "1.0" + memory: 1G +volumes: + loki-data: + grafana-data: + prometheus-data: +networks: + logging: + driver: bridge + + + diff --git a/app_python/monitoring/docs/LAB07.md b/app_python/monitoring/docs/LAB07.md new file mode 100644 index 0000000000..02946794ee --- /dev/null +++ b/app_python/monitoring/docs/LAB07.md @@ -0,0 +1 @@ +!!!!!! real LAB07.md documentation lies in the app_python/docs/LAB07.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/app_python/monitoring/docs/LAB08.md b/app_python/monitoring/docs/LAB08.md new file mode 100644 index 0000000000..8da919fdae --- /dev/null +++ b/app_python/monitoring/docs/LAB08.md @@ -0,0 +1 @@ +!!!!!! real LAB08.md documentation lies in the app_python/docs/LAB08.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/app_python/monitoring/loki/config.yml b/app_python/monitoring/loki/config.yml new file mode 100644 index 0000000000..9a27a4c15d --- /dev/null +++ b/app_python/monitoring/loki/config.yml @@ -0,0 +1,31 @@ +auth_enabled: false +server: + http_listen_port: 3100 +common: + path_prefix: /loki + storage: + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + replication_factor: 1 + ring: + kvstore: + store: inmemory +schema_config: + configs: + - from: 2026-03-01 + store: tsdb + object_store: filesystem + schema: v13 + index: + prefix: index_ + period: 24h +storage_config: + filesystem: + directory: /loki/chunks +limits_config: + retention_period: 168h +compactor: + working_directory: /loki/compactor + retention_enabled: true + delete_request_store: filesystem \ No newline at end of file diff --git a/app_python/monitoring/prometheus/dashboard.json b/app_python/monitoring/prometheus/dashboard.json new file mode 100644 index 0000000000..d63d022ade --- /dev/null +++ b/app_python/monitoring/prometheus/dashboard.json @@ -0,0 +1,630 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 0, + "links": [], + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, + "id": 1, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "editorMode": "code", + "expr": "sum(rate(http_requests_total[5m])) by (endpoint)", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "title": "request rate", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "fixedColor": "red", + "mode": "fixed" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 0 + } + ] + } + }, + "overrides": [ + { + "__systemRef": "hideSeriesFrom", + "matcher": { + "id": "byNames", + "options": { + "mode": "exclude", + "names": [ + "sum(rate(http_requests_total{status_code=~\"[45]..\"}[5m]))" + ], + "prefix": "All except:", + "readOnly": true + } + }, + "properties": [ + { + "id": "custom.hideFrom", + "value": { + "legend": false, + "tooltip": true, + "viz": true + } + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 0 + }, + "id": 2, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "sum(rate(http_requests_total{status_code=~\"[45]..\"}[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "error rate", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + } + }, + "mappings": [] + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "404" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "light-red", + "mode": "fixed" + } + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 8 + }, + "id": 5, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "pieType": "pie", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "sort": "desc", + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "sum by (status_code) (rate(http_requests_total[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "status code distribution", + "type": "piechart" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + }, + "unit": "arcsec" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 8 + }, + "id": 3, + "options": { + "calculate": false, + "cellGap": 1, + "color": { + "exponent": 0.5, + "fill": "dark-orange", + "mode": "scheme", + "reverse": false, + "scale": "exponential", + "scheme": "Oranges", + "steps": 64 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": false, + "yHistogram": false + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "rate(http_request_duration_seconds_bucket[5m])", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "latency heatmap", + "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 16 + }, + "id": 7, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "p95 Latency", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 16 + }, + "id": 4, + "options": { + "minVizHeight": 75, + "minVizWidth": 75, + "orientation": "auto", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showThresholdLabels": false, + "showThresholdMarkers": true, + "sizing": "auto" + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "http_requests_in_progress", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "active requests", + "type": "gauge" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 0 + }, + { + "color": "semi-dark-green", + "value": 1 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 24 + }, + "id": 6, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "auto", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "up{job=\"app\"}", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "service health", + "type": "stat" + } + ], + "preload": false, + "schemaVersion": 42, + "tags": [], + "templating": { + "list": [] + }, + "time": { + "from": "now-5m", + "to": "now" + }, + "timepicker": {}, + "timezone": "browser", + "title": "devops lab8", + "uid": "adt5g7d", + "version": 11 +} \ No newline at end of file diff --git a/app_python/monitoring/prometheus/prometheus.yml b/app_python/monitoring/prometheus/prometheus.yml new file mode 100644 index 0000000000..442e8ae576 --- /dev/null +++ b/app_python/monitoring/prometheus/prometheus.yml @@ -0,0 +1,23 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + +scrape_configs: + - job_name: 'prometheus' + static_configs: + - targets: ['localhost:9090'] + + - job_name: 'app' + static_configs: + - targets: ['app-python:12345'] + metrics_path: /metrics + + - job_name: 'loki' + static_configs: + - targets: ['loki:3100'] + metrics_path: /metrics + + - job_name: 'grafana' + static_configs: + - targets: ['grafana:3000'] + metrics_path: /metrics \ No newline at end of file diff --git a/app_python/monitoring/promtail/config.yml b/app_python/monitoring/promtail/config.yml new file mode 100644 index 0000000000..02c6e0501c --- /dev/null +++ b/app_python/monitoring/promtail/config.yml @@ -0,0 +1,22 @@ +server: + http_listen_port: 9080 +positions: + filename: "/tmp/positions.yaml" + sync_period: 10s + ignore_invalid_yaml: false +clients: + - url: http://loki:3100/loki/api/v1/push +scrape_configs: + - job_name: docker + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + regex: '/(.*)' + target_label: 'container' + - target_label: job + replacement: docker + - source_labels: ['__meta_docker_container_label_logging'] + regex: promtail + action: keep \ No newline at end of file diff --git a/app_python/pulumi/.gitignore b/app_python/pulumi/.gitignore new file mode 100644 index 0000000000..a3807e5bdb --- /dev/null +++ b/app_python/pulumi/.gitignore @@ -0,0 +1,2 @@ +*.pyc +venv/ diff --git a/app_python/pulumi/Pulumi.dev.yaml b/app_python/pulumi/Pulumi.dev.yaml new file mode 100644 index 0000000000..87ad76aa1d --- /dev/null +++ b/app_python/pulumi/Pulumi.dev.yaml @@ -0,0 +1,9 @@ +encryptionsalt: v1:ibiWcVbfzPE=:v1:JGYI/AyYOtO6LyQX:wm2sULkxKAuOHYQjzutkotLh1kDMWQ== +config: + pulumi-vm:zone: "ru-central1-a" + pulumi-vm:appPort: "1999" + pulumi-vm:sshUser: "ubuntu" + pulumi-vm:sshPublicKeyPath: "/Users/fountainer/.ssh/terraform-vm-key.pub" + yandex:cloudId: "b1g4lhssapi17vlk0t0m" + yandex:folderId: "b1gcu87lbfoq3i0quvi9" + yandex:serviceAccountKeyFile: "/Users/fountainer/.yc/pulumi-key.json" \ No newline at end of file diff --git a/app_python/pulumi/Pulumi.yaml b/app_python/pulumi/Pulumi.yaml new file mode 100644 index 0000000000..f1628db528 --- /dev/null +++ b/app_python/pulumi/Pulumi.yaml @@ -0,0 +1,11 @@ +name: pulumi-vm +description: A minimal Python Pulumi program +runtime: + name: python + options: + toolchain: pip + virtualenv: venv +config: + pulumi:tags: + value: + pulumi:template: python diff --git a/app_python/pulumi/__main__.py b/app_python/pulumi/__main__.py new file mode 100644 index 0000000000..b519111013 --- /dev/null +++ b/app_python/pulumi/__main__.py @@ -0,0 +1,64 @@ +"""A Python Pulumi program""" + +import pulumi +import pulumi_yandex as yandex +import os + +config = pulumi.Config() +zone = config.require("zone") +app_port = config.require_int("appPort") +ssh_user = config.require("sshUser") +ssh_key_path = config.require("sshPublicKeyPath") + +with open(os.path.expanduser(ssh_key_path)) as f: + public_key = f.read().strip() + +network = yandex.VpcNetwork("network", name="network") + +subnet = yandex.VpcSubnet( + "subnet", + name="subnet1", + zone=zone, + network_id=network.id, + v4_cidr_blocks=["192.168.10.0/24"] +) + +vm_sg = yandex.VpcSecurityGroup( + "vm-sg", + description="VM security group", + network_id=network.id, + ingresses=[ + yandex.VpcSecurityGroupIngressArgs(port=22, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + yandex.VpcSecurityGroupIngressArgs(port=80, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + yandex.VpcSecurityGroupIngressArgs(port=app_port, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + ], + egresses=[ + yandex.VpcSecurityGroupEgressArgs(protocol="ANY", from_port=0, to_port=0, v4_cidr_blocks=["0.0.0.0/0"]) + ] +) + +image = yandex.get_compute_image(family="ubuntu-2204-lts") + +vm = yandex.ComputeInstance( + "vm", + name="pulumi", + zone=zone, + resources={"cores": 2, "memory": 2}, + boot_disk={ + "initialize_params": { + "name": "boot-disk", + "size": 20, + "type": "network-hdd", + "image_id": image.id, + } + }, + network_interfaces=[{ + "subnet_id": subnet.id, + "nat": True, + "security_group_ids": [vm_sg.id], + }], + metadata={"ssh-keys": f"{ssh_user}:{public_key}"} +) + +pulumi.export("internal_ip", vm.network_interfaces[0]["ip_address"]) +pulumi.export("external_ip", vm.network_interfaces[0]["nat_ip_address"]) diff --git a/app_python/pulumi/requirements.txt b/app_python/pulumi/requirements.txt new file mode 100644 index 0000000000..938e7bfb54 --- /dev/null +++ b/app_python/pulumi/requirements.txt @@ -0,0 +1,4 @@ +pulumi>=3.223.0 +pulumi_yandex==0.13.0 +PyYAML +setuptools \ No newline at end of file diff --git a/app_python/requirements.txt b/app_python/requirements.txt new file mode 100644 index 0000000000..47296a42b8 --- /dev/null +++ b/app_python/requirements.txt @@ -0,0 +1,6 @@ +# Web Framework +flask==3.1.2 +pytest==9.0.2 +python-json-logger==4.0.0 +python-dotenv==1.2.2 +prometheus-client==0.23.1 \ No newline at end of file diff --git a/app_python/terraform/.gitignore b/app_python/terraform/.gitignore new file mode 100644 index 0000000000..b7cf2bdd62 --- /dev/null +++ b/app_python/terraform/.gitignore @@ -0,0 +1,4 @@ +.terraform/ +terraform.tfstate +terraform.tfstate.backup +terraform.tfvars \ No newline at end of file diff --git a/app_python/terraform/.terraform.lock.hcl b/app_python/terraform/.terraform.lock.hcl new file mode 100644 index 0000000000..280b0e4395 --- /dev/null +++ b/app_python/terraform/.terraform.lock.hcl @@ -0,0 +1,9 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/yandex-cloud/yandex" { + version = "0.191.0" + hashes = [ + "h1:MGHtJSlDigrSSCHWdl28B+XXnH87C82Qu0978/lbJtM=", + ] +} diff --git a/app_python/terraform/README.md b/app_python/terraform/README.md new file mode 100644 index 0000000000..22dafe1be3 --- /dev/null +++ b/app_python/terraform/README.md @@ -0,0 +1,33 @@ +## SETUP INSTRUCTIONS: TERRAFORM + +### Set environment variables +```bash +export YC_TOKEN=$(yc iam create-token --impersonate-service-account-id ) +export YC_CLOUD_ID=$(yc config get cloud-id) +export YC_FOLDER_ID=$(yc config get folder-id) +``` + +### Initialize Terraform +```bash +terraform init +``` + +### Plan infrastructure +```bash +terraform plan +``` + +### Apply infrastructure +```bash +terraform apply +``` + +### Access VM via SSH +```bash +ssh -i /path/to/terraform-vm-key ubuntu@ +``` + +### Destroy resources +```bash +terraform destroy +``` diff --git a/app_python/terraform/main.tf b/app_python/terraform/main.tf new file mode 100644 index 0000000000..6d122555f9 --- /dev/null +++ b/app_python/terraform/main.tf @@ -0,0 +1,95 @@ +terraform { + required_providers { + yandex = { + source = "yandex-cloud/yandex" + } + } + required_version = ">= 0.13" +} + +provider "yandex" { + service_account_key_file = "/Users/fountainer/.yandexcloud/my-sa-key.json" + zone = var.zone + cloud_id = "b1g4lhssapi17vlk0t0m" + folder_id = "b1gcu87lbfoq3i0quvi9" +} + +data "yandex_compute_image" "ubuntu_2204" { + family = "ubuntu-2204-lts" +} + +resource "yandex_compute_disk" "boot-disk" { + name = "boot-disk-terraform" + type = "network-hdd" + zone = var.zone + size = "20" + image_id = data.yandex_compute_image.ubuntu_2204.id +} + +resource "yandex_compute_instance" "vm" { + name = "terraform" + + resources { + cores = 2 + memory = 2 + } + + boot_disk { + disk_id = yandex_compute_disk.boot-disk.id + } + + network_interface { + subnet_id = yandex_vpc_subnet.subnet.id + nat = true + security_group_ids = [yandex_vpc_security_group.vm_sg.id] + + } + + metadata = { + ssh-keys = "ubuntu:${file(var.ssh_key_path)}" + } +} + +resource "yandex_vpc_network" "network" { + name = "network" +} + +resource "yandex_vpc_subnet" "subnet" { + name = "subnet1" + zone = var.zone + network_id = yandex_vpc_network.network.id + v4_cidr_blocks = ["192.168.10.0/24"] +} + +resource "yandex_vpc_security_group" "vm_sg" { + name = "vm-security-group" + network_id = yandex_vpc_network.network.id + + ingress { + protocol = "TCP" + description = "SSH" + port = 22 + v4_cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "TCP" + description = "HTTP" + port = 80 + v4_cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "TCP" + description = "App port" + port = var.app_port + v4_cidr_blocks = ["0.0.0.0/0"] + } + + egress { + protocol = "ANY" + description = "Allow all outgoing traffic" + v4_cidr_blocks = ["0.0.0.0/0"] + } +} + diff --git a/app_python/terraform/outputs.tf b/app_python/terraform/outputs.tf new file mode 100644 index 0000000000..17df178a5f --- /dev/null +++ b/app_python/terraform/outputs.tf @@ -0,0 +1,10 @@ +output "internal_ip_address_vm" { + description = "Internal IP address of the VM" + value = yandex_compute_instance.vm.network_interface.0.ip_address +} + +output "external_ip_address_vm" { + description = "External (public) IP address of the VM" + value = yandex_compute_instance.vm.network_interface.0.nat_ip_address +} + diff --git a/app_python/terraform/variables.tf b/app_python/terraform/variables.tf new file mode 100644 index 0000000000..c30b81c0b3 --- /dev/null +++ b/app_python/terraform/variables.tf @@ -0,0 +1,15 @@ +variable "zone" { + description = "Yandex Cloud availability zone" +} + +variable "ssh_key_path" { + description = "Path to the SSH key for VM access" +} + +variable "ssh_source_ip" { + description = "Your IP address to allow SSH access" +} + +variable "app_port" { + description = "Custom app port to open in the firewall" +} diff --git a/app_python/tests/__init__.py b/app_python/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app_python/tests/test_health_endpoint.py b/app_python/tests/test_health_endpoint.py new file mode 100644 index 0000000000..74f99cefe8 --- /dev/null +++ b/app_python/tests/test_health_endpoint.py @@ -0,0 +1,40 @@ +from app import app +import pytest + + +def test_health_endpoint(): + client = app.test_client() + response = client.get("/health") + + assert ( + response.status_code == 200 + or response.status_code == 404 + or response.status_code == 500 + ) + + resp = response.get_json() + + if response.status_code == 200: + + status = resp.get("status") + assert status + assert isinstance(status, str) + + timestamp = resp.get("timestamp") + assert timestamp + assert isinstance(timestamp, str) + + uptime_seconds = resp.get("uptime_seconds") + assert uptime_seconds >= 0 + assert isinstance(uptime_seconds, int) + + elif response.status_code == 404 or response.status_code == 500: + assert isinstance(resp, dict) + + error = resp.get("error") + assert error + assert isinstance(error, str) + + message = resp.get("message") + assert message + assert isinstance(message, str) diff --git a/app_python/tests/test_home_endpoint.py b/app_python/tests/test_home_endpoint.py new file mode 100644 index 0000000000..2bd12651a9 --- /dev/null +++ b/app_python/tests/test_home_endpoint.py @@ -0,0 +1,74 @@ +from app import app +import pytest + + +def test_home_endpoint(): + client = app.test_client() + response = client.get("/") + + assert ( + response.status_code == 200 + or response.status_code == 404 + or response.status_code == 500 + ) + resp = response.get_json() + + if response.status_code == 200: + message = resp.get("message") + assert message + assert isinstance(message, dict) + + service = message.get("service") + assert service + assert isinstance(service, dict) + assert service.get("name") + assert service.get("version") + assert service.get("description") + assert service.get("framework") + assert service.get("debug status") + + system = message.get("system") + assert system + assert isinstance(system, dict) + assert system.get("hostname") + assert system.get("platform") + assert system.get("platform_version") + assert system.get("architecture") + assert system.get("cpu_count") + assert system.get("python_version") + + runtime = message.get("runtime") + assert runtime + assert isinstance(runtime, dict) + assert runtime.get("uptime_seconds") + assert runtime.get("uptime_human") + assert runtime.get("current_time") + assert runtime.get("timezone") + + request = message.get("request") + assert request + assert isinstance(request, dict) + assert request.get("client_ip") + assert request.get("port") + assert request.get("user_agent") + assert request.get("method") + assert request.get("path") + + endpoints = message.get("endpoints") + assert endpoints + assert isinstance(endpoints, list) + assert endpoints[0] + assert endpoints[0].get("path") + assert endpoints[1] + assert endpoints[1].get("path") + + elif response.status_code == 404 or response.status_code == 500: + assert isinstance(resp, dict) + + error = resp.get("error") + assert error + assert isinstance(error, str) + + message = resp.get("message") + assert message + assert isinstance(message, str) diff --git a/cloudflare/WORKERS.md b/cloudflare/WORKERS.md new file mode 100644 index 0000000000..bb77eb3bd8 --- /dev/null +++ b/cloudflare/WORKERS.md @@ -0,0 +1,215 @@ +# Documentation + +## Deployment summary + +### Worker URL + +[https://edge-api.v-levasheva.workers.dev](https://edge-api.v-levasheva.workers.dev) + +### Main routes + +/ for general app info + +/health for health status + +/edge for cloudflare edge metadata + +/counter for a persistent KV-based request counter, which shows the number of visits for / route + +/admin-check is for checking if the requester is an admin, it uses secrets API_TOKEN and ADMIN_EMAIL, and the requester should provide valid token and email in the request headers + +and also there is "Not Found" response for non-existent route + +### Configuration used + +the worker was configured using wrangler.jsonc, which defines the worker name, entrypoint (src/index.ts), compatibility date, environment variables, and resource bindings. the project uses a TypeScript Worker template and includes an APP_NAME variable binding together with a COUNTER_KV KV namespace binding used for persistent storage in the /counter endpoint. + +## Evidence + +### Screenshot of Cloudflare dashboard + +![](./../app_python/docs/screenshots/lab17-shots/dashboard.png) + +### Example /edge JSON response + +Firstly, the local testing of all endpoints (for the deployed /edge some info such as asn and httpProtocol were added): + +![](./../app_python/docs/screenshots/lab17-shots/endpoints%20local.png) + +And the /edge endpoint in the deployed worker: + +![](./../app_python/docs/screenshots/lab17-shots/edge%20deployed.png) + +### How Workers distributes execution globally? + +cloudflare workers are automatically executed on cloudflare edge locations around the world, so requests are handled close to the user without manually selecting regions. unlike traditional VM or PaaS platforms where you often deploy separately to regions, workers use cloudflare’s global network automatically, so there is no separate “deploy to 3 regions” step. + +### The difference between workers.dev, Routes, and Custom Domains + +- workers.dev is the default public cloudflare subdomain automatically provided for testing and accessing workers + +- routes are specific url endpoints that worker will provide access to + +- custom domains allow exposing the worker directly through an owned custom domain, not through a provided workers.dev + +## Configuration, Secrets & Persistence + +### Explain why plaintext vars are not suitable for secrets + +variables I added: "APP_NAME" and "COURSE_NAME" + +plaintext vars are not safe for secrets because they are stored in config and visible in repo and dashboard, so anyone can read them + +### Secrets + +I added 2 secrets: API_TOKEN and ADMIN_EMAIL. They are used in the /admin-check endpoint, where the requester can pass their access token and email and see if they can be authenticated as an admin. + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler secret list +[ + { + "name": "ADMIN_EMAIL", + "type": "secret_text" + }, + { + "name": "API_TOKEN", + "type": "secret_text" + } +] +``` + +![](./../app_python/docs/screenshots/lab17-shots/secrets.png) + +### Workers KV persistence + +### Document what you stored and how you verified it + +I stored the number of visits of the / endpoint. Each time / is visited, the counter increases by one, and visits value is updated. The value then can be accessed through the /visits endpoint. + +The persistance verification: + +![](./../app_python/docs/screenshots/lab17-shots/persistance.png) + +## Observability & Operations + +### Example log or metrics screenshot + +Logs + +![](./../app_python/docs/screenshots/lab17-shots/logs.png) + +Metrics + +![](./../app_python/docs/screenshots/lab17-shots/metrics.png) + +I looked at errors, requests, and request duration metrics for all deployed versions for the last 24 hours. + +requests: total of 192. this metrics shows how many http requests hit the Worker through all endpoints. + +errors: 0 errors. it can be confusing since I intentially hit invalid endpoint such as https://edge-api.v-levasheva.workers.dev/smth a lot of times to get error: "Not Found", but the case was that the Worker does not count 404 responses as errors, the real errors that are considered are runtime failures, which I did not have. + +request duration: 3.15 ms on average. this metric shows the latency - how much it takes to process a request and send back a response. + +### Multiple Deployments + +* I created about 10 deployments but put here last 2 for readability + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler deployments list + + ⛅️ wrangler 4.90.0 +Created: 2026-05-10T16:57:52.287Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 4844c942-5e05-4199-999f-b54af219ed99 + Created: 2026-05-10T16:48:11.805Z + Tag: - + Message: - + +Created: 2026-05-10T17:01:05.875Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f + Created: 2026-05-10T17:01:03.267Z + Tag: - + Message: - +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler rollback + + ⛅️ wrangler 4.90.0 +─────────────────── +├ Your current deployment has 1 version(s): +│ +│ (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f +│ Created: 2026-05-10T17:01:03.267267Z +│ Tag: - +│ Message: - +│ +✔ Please provide an optional message for this rollback (120 characters max) … test rollback +│ +├  WARNING  You are about to rollback to Worker Version 4844c942-5e05-4199-999f-b54af219ed99. +│ This will immediately replace the current deployment and become the active deployment across all your deployed triggers. +│ However, your local development environment will not be affected by this rollback. +│ Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc). +│ +│ (100%) 4844c942-5e05-4199-999f-b54af219ed99 +│ Created: 2026-05-10T16:48:11.805042Z +│ Tag: - +│ Message: - +│ +✔ Are you sure you want to deploy this Worker Version to 100% of traffic? … yes +Performing rollback... +│ +╰  SUCCESS  Worker Version 4844c942-5e05-4199-999f-b54af219ed99 has been deployed to 100% of traffic. + +Current Version ID: 4844c942-5e05-4199-999f-b54af219ed99 +``` + +## Kubernetes vs Cloudflare Workers Comparison + +| Aspect | Kubernetes | Cloudflare Workers | +|--------|------------|--------------------| +| Setup complexity | really high, you should create cluster setup, manifests, networking, monitoring, and scaling configuration yourself | much simpler, just write code and deploy with minimal config | +| Deployment speed | slower, containers must build and start | very fast, seconds are enough | +| Global distribution | usually requires manual configuration | automatic global edge distribution | +| Cost (for small apps) | can become expensive due to always-running release | cheaper for lightweight APIs and low traffic apps | +| State/persistence model | persistent volumes, databases, StatefulSets | stateless by default, persistence through KV | +| Control/flexibility | very high control over runtime, networking, and storage | less low-level control, but much simpler to use | +| Best use case | complex and heavy programs, projects that require customisation | edge APIs, lightweight services, and globally distributed low-latency apps | + +## When to Use Each + +### Scenarios favoring Kubernetes + +- complex applications with multiple services +- custom configuration +- long-running work processes + +### Scenarios favoring Workers + +- lightweight APIs +- edge services +- globally distributed applications with low latency requirements + +### Your recommendation + +I would use k8s and VMs for big projects that require heavy customisation, and Cloudflare Workers for lightweight apps that will benefit immensely from locating close to the users. + +## Reflection + +### What felt easier than Kubernetes? + +- to be honest, everything felt easier + +### What felt more constrained? + +- no control over networking, no customisation, no manual resource distribution and controllable scaling. + +### What changed because Workers is not a Docker host? + +- as opposed to docker, Workers provide serverless architecture, and Workers runtime model is forced. moreover, I didn't have to keep process running locally, like with docker containers, it was nice. + diff --git a/cloudflare/edge-api/.editorconfig b/cloudflare/edge-api/.editorconfig new file mode 100644 index 0000000000..a727df347a --- /dev/null +++ b/cloudflare/edge-api/.editorconfig @@ -0,0 +1,12 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space diff --git a/cloudflare/edge-api/.gitignore b/cloudflare/edge-api/.gitignore new file mode 100644 index 0000000000..4138168d75 --- /dev/null +++ b/cloudflare/edge-api/.gitignore @@ -0,0 +1,167 @@ +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# wrangler project + +.dev.vars* +!.dev.vars.example +.env* +!.env.example +.wrangler/ diff --git a/cloudflare/edge-api/.prettierrc b/cloudflare/edge-api/.prettierrc new file mode 100644 index 0000000000..5c7b5d3c7a --- /dev/null +++ b/cloudflare/edge-api/.prettierrc @@ -0,0 +1,6 @@ +{ + "printWidth": 140, + "singleQuote": true, + "semi": true, + "useTabs": true +} diff --git a/cloudflare/edge-api/.vscode/settings.json b/cloudflare/edge-api/.vscode/settings.json new file mode 100644 index 0000000000..0126e59b82 --- /dev/null +++ b/cloudflare/edge-api/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "wrangler.json": "jsonc" + } +} \ No newline at end of file diff --git a/cloudflare/edge-api/package-lock.json b/cloudflare/edge-api/package-lock.json new file mode 100644 index 0000000000..7adee25cf0 --- /dev/null +++ b/cloudflare/edge-api/package-lock.json @@ -0,0 +1,2913 @@ +{ + "name": "edge-api", + "version": "0.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "edge-api", + "version": "0.0.0", + "devDependencies": { + "@cloudflare/vitest-pool-workers": "^0.12.4", + "@types/node": "^25.6.2", + "typescript": "^5.5.2", + "vitest": "~3.2.0", + "wrangler": "^4.90.0" + } + }, + "node_modules/@cloudflare/kv-asset-handler": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.5.0.tgz", + "integrity": "sha512-jxQYkj8dSIzc0cD6cMMNdOc1UVjqSqu8BZdor5s8cGjW2I8BjODt/kWPVdY+u9zj3ms75Q5qaZgnxUad83+eAg==", + "dev": true, + "license": "MIT OR Apache-2.0", + "engines": { + "node": ">=22.0.0" + } + }, + "node_modules/@cloudflare/unenv-preset": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/@cloudflare/unenv-preset/-/unenv-preset-2.16.1.tgz", + "integrity": "sha512-ECxObrMfyTl5bhQf/lZCXwo5G6xX9IAUo+nDMKK4SZ8m4Jvvxp52vilxyySSWh2YTZz8+HQ07qGH/2rEom1vDw==", + "dev": true, + "license": "MIT OR Apache-2.0", + "peerDependencies": { + "unenv": "2.0.0-rc.24", + "workerd": ">1.20260305.0 <2.0.0-0" + }, + "peerDependenciesMeta": { + "workerd": { + "optional": true + } + } + }, + "node_modules/@cloudflare/vitest-pool-workers": { + "version": "0.12.21", + "resolved": "https://registry.npmjs.org/@cloudflare/vitest-pool-workers/-/vitest-pool-workers-0.12.21.tgz", + "integrity": "sha512-xqvqVR+qAhekXWaTNY36UtFFmHrz13yGUoWVGOu6LDC2ABiQqI1E1lQ3eUZY8KVB+1FXY/mP5dB6oD07XUGnPg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cjs-module-lexer": "^1.2.3", + "esbuild": "0.27.3", + "miniflare": "4.20260310.0", + "wrangler": "4.72.0" + }, + "peerDependencies": { + "@vitest/runner": "2.0.x - 3.2.x", + "@vitest/snapshot": "2.0.x - 3.2.x", + "vitest": "2.0.x - 3.2.x" + } + }, + "node_modules/@cloudflare/vitest-pool-workers/node_modules/@cloudflare/kv-asset-handler": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.4.2.tgz", + "integrity": "sha512-SIOD2DxrRRwQ+jgzlXCqoEFiKOFqaPjhnNTGKXSRLvp1HiOvapLaFG2kEr9dYQTYe8rKrd9uvDUzmAITeNyaHQ==", + "dev": true, + "license": "MIT OR Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@cloudflare/vitest-pool-workers/node_modules/@cloudflare/unenv-preset": { + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/@cloudflare/unenv-preset/-/unenv-preset-2.15.0.tgz", + "integrity": "sha512-EGYmJaGZKWl+X8tXxcnx4v2bOZSjQeNI5dWFeXivgX9+YCT69AkzHHwlNbVpqtEUTbew8eQurpyOpeN8fg00nw==", + "dev": true, + "license": "MIT OR Apache-2.0", + "peerDependencies": { + "unenv": "2.0.0-rc.24", + "workerd": "1.20260301.1 || ~1.20260302.1 || ~1.20260303.1 || ~1.20260304.1 || >1.20260305.0 <2.0.0-0" + }, + "peerDependenciesMeta": { + "workerd": { + "optional": true + } + } + }, + "node_modules/@cloudflare/vitest-pool-workers/node_modules/wrangler": { + "version": "4.72.0", + "resolved": "https://registry.npmjs.org/wrangler/-/wrangler-4.72.0.tgz", + "integrity": "sha512-bKkb8150JGzJZJWiNB2nu/33smVfawmfYiecA6rW4XH7xS23/jqMbgpdelM34W/7a1IhR66qeQGVqTRXROtAZg==", + "dev": true, + "license": "MIT OR Apache-2.0", + "dependencies": { + "@cloudflare/kv-asset-handler": "0.4.2", + "@cloudflare/unenv-preset": "2.15.0", + "blake3-wasm": "2.1.5", + "esbuild": "0.27.3", + "miniflare": "4.20260310.0", + "path-to-regexp": "6.3.0", + "unenv": "2.0.0-rc.24", + "workerd": "1.20260310.1" + }, + "bin": { + "wrangler": "bin/wrangler.js", + "wrangler2": "bin/wrangler.js" + }, + "engines": { + "node": ">=20.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20260310.1" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + } + }, + "node_modules/@cloudflare/workerd-darwin-64": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-64/-/workerd-darwin-64-1.20260310.1.tgz", + "integrity": "sha512-hF2VpoWaMb1fiGCQJqCY6M8I+2QQqjkyY4LiDYdTL5D/w6C1l5v1zhc0/jrjdD1DXfpJtpcSMSmEPjHse4p9Ig==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-darwin-arm64": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-arm64/-/workerd-darwin-arm64-1.20260310.1.tgz", + "integrity": "sha512-h/Vl3XrYYPI6yFDE27XO1QPq/1G1lKIM8tzZGIWYpntK3IN5XtH3Ee/sLaegpJ49aIJoqhF2mVAZ6Yw+Vk2gJw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-64": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-64/-/workerd-linux-64-1.20260310.1.tgz", + "integrity": "sha512-XzQ0GZ8G5P4d74bQYOIP2Su4CLdNPpYidrInaSOuSxMw+HamsHaFrjVsrV2mPy/yk2hi6SY2yMbgKFK9YjA7vw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-arm64": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-arm64/-/workerd-linux-arm64-1.20260310.1.tgz", + "integrity": "sha512-sxv4CxnN4ZR0uQGTFVGa0V4KTqwdej/czpIc5tYS86G8FQQoGIBiAIs2VvU7b8EROPcandxYHDBPTb+D9HIMPw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-windows-64": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20260310.1.tgz", + "integrity": "sha512-+1ZTViWKJypLfgH/luAHCqkent0DEBjAjvO40iAhOMHRLYP/SPphLvr4Jpi6lb+sIocS8Q1QZL4uM5Etg1Wskg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.10.0.tgz", + "integrity": "sha512-ewvYlk86xUoGI0zQRNq/mC+16R1QeDlKQy21Ki3oSYXNgLb45GV1P6A0M+/s6nyCuNDqe5VpaY84BzXGwVbwFA==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.3.tgz", + "integrity": "sha512-9fJMTNFTWZMh5qwrBItuziu834eOCUcEqymSH7pY+zoMVEZg3gcPuBNxH1EvfVYe9h0x/Ptw8KBzv7qxb7l8dg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.27.3.tgz", + "integrity": "sha512-i5D1hPY7GIQmXlXhs2w8AWHhenb00+GxjxRncS2ZM7YNVGNfaMxgzSGuO8o8SJzRc/oZwU2bcScvVERk03QhzA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.27.3.tgz", + "integrity": "sha512-YdghPYUmj/FX2SYKJ0OZxf+iaKgMsKHVPF1MAq/P8WirnSpCStzKJFjOjzsW0QQ7oIAiccHdcqjbHmJxRb/dmg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.27.3.tgz", + "integrity": "sha512-IN/0BNTkHtk8lkOM8JWAYFg4ORxBkZQf9zXiEOfERX/CzxW3Vg1ewAhU7QSWQpVIzTW+b8Xy+lGzdYXV6UZObQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.3.tgz", + "integrity": "sha512-Re491k7ByTVRy0t3EKWajdLIr0gz2kKKfzafkth4Q8A5n1xTHrkqZgLLjFEHVD+AXdUGgQMq+Godfq45mGpCKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.27.3.tgz", + "integrity": "sha512-vHk/hA7/1AckjGzRqi6wbo+jaShzRowYip6rt6q7VYEDX4LEy1pZfDpdxCBnGtl+A5zq8iXDcyuxwtv3hNtHFg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.27.3.tgz", + "integrity": "sha512-ipTYM2fjt3kQAYOvo6vcxJx3nBYAzPjgTCk7QEgZG8AUO3ydUhvelmhrbOheMnGOlaSFUoHXB6un+A7q4ygY9w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.27.3.tgz", + "integrity": "sha512-dDk0X87T7mI6U3K9VjWtHOXqwAMJBNN2r7bejDsc+j03SEjtD9HrOl8gVFByeM0aJksoUuUVU9TBaZa2rgj0oA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.27.3.tgz", + "integrity": "sha512-s6nPv2QkSupJwLYyfS+gwdirm0ukyTFNl3KTgZEAiJDd+iHZcbTPPcWCcRYH+WlNbwChgH2QkE9NSlNrMT8Gfw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.27.3.tgz", + "integrity": "sha512-sZOuFz/xWnZ4KH3YfFrKCf1WyPZHakVzTiqji3WDc0BCl2kBwiJLCXpzLzUBLgmp4veFZdvN5ChW4Eq/8Fc2Fg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.27.3.tgz", + "integrity": "sha512-yGlQYjdxtLdh0a3jHjuwOrxQjOZYD/C9PfdbgJJF3TIZWnm/tMd/RcNiLngiu4iwcBAOezdnSLAwQDPqTmtTYg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.27.3.tgz", + "integrity": "sha512-WO60Sn8ly3gtzhyjATDgieJNet/KqsDlX5nRC5Y3oTFcS1l0KWba+SEa9Ja1GfDqSF1z6hif/SkpQJbL63cgOA==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.27.3.tgz", + "integrity": "sha512-APsymYA6sGcZ4pD6k+UxbDjOFSvPWyZhjaiPyl/f79xKxwTnrn5QUnXR5prvetuaSMsb4jgeHewIDCIWljrSxw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.27.3.tgz", + "integrity": "sha512-eizBnTeBefojtDb9nSh4vvVQ3V9Qf9Df01PfawPcRzJH4gFSgrObw+LveUyDoKU3kxi5+9RJTCWlj4FjYXVPEA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.27.3.tgz", + "integrity": "sha512-3Emwh0r5wmfm3ssTWRQSyVhbOHvqegUDRd0WhmXKX2mkHJe1SFCMJhagUleMq+Uci34wLSipf8Lagt4LlpRFWQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.27.3.tgz", + "integrity": "sha512-pBHUx9LzXWBc7MFIEEL0yD/ZVtNgLytvx60gES28GcWMqil8ElCYR4kvbV2BDqsHOvVDRrOxGySBM9Fcv744hw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.27.3.tgz", + "integrity": "sha512-Czi8yzXUWIQYAtL/2y6vogER8pvcsOsk5cpwL4Gk5nJqH5UZiVByIY8Eorm5R13gq+DQKYg0+JyQoytLQas4dA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.27.3.tgz", + "integrity": "sha512-sDpk0RgmTCR/5HguIZa9n9u+HVKf40fbEUt+iTzSnCaGvY9kFP0YKBWZtJaraonFnqef5SlJ8/TiPAxzyS+UoA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.27.3.tgz", + "integrity": "sha512-P14lFKJl/DdaE00LItAukUdZO5iqNH7+PjoBm+fLQjtxfcfFE20Xf5CrLsmZdq5LFFZzb5JMZ9grUwvtVYzjiA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.27.3.tgz", + "integrity": "sha512-AIcMP77AvirGbRl/UZFTq5hjXK+2wC7qFRGoHSDrZ5v5b8DK/GYpXW3CPRL53NkvDqb9D+alBiC/dV0Fb7eJcw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.27.3.tgz", + "integrity": "sha512-DnW2sRrBzA+YnE70LKqnM3P+z8vehfJWHXECbwBmH/CU51z6FiqTQTHFenPlHmo3a8UgpLyH3PT+87OViOh1AQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.27.3.tgz", + "integrity": "sha512-NinAEgr/etERPTsZJ7aEZQvvg/A6IsZG/LgZy+81wON2huV7SrK3e63dU0XhyZP4RKGyTm7aOgmQk0bGp0fy2g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.27.3.tgz", + "integrity": "sha512-PanZ+nEz+eWoBJ8/f8HKxTTD172SKwdXebZ0ndd953gt1HRBbhMsaNqjTyYLGLPdoWHy4zLU7bDVJztF5f3BHA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.27.3.tgz", + "integrity": "sha512-B2t59lWWYrbRDw/tjiWOuzSsFh1Y/E95ofKz7rIVYSQkUYBjfSgf6oeYPNWHToFRr2zx52JKApIcAS/D5TUBnA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.27.3.tgz", + "integrity": "sha512-QLKSFeXNS8+tHW7tZpMtjlNb7HKau0QDpwm49u0vUp9y1WOF+PEzkU84y9GqYaAVW8aH8f3GcBck26jh54cX4Q==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.27.3.tgz", + "integrity": "sha512-4uJGhsxuptu3OcpVAzli+/gWusVGwZZHTlS63hh++ehExkVT8SgiEf7/uC/PclrPPkLhZqGgCTjd0VWLo6xMqA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.34.5.tgz", + "integrity": "sha512-imtQ3WMJXbMY4fxb/Ndp6HBTNVtWCUI0WdobyheGf5+ad6xX8VIDO8u2xE4qc/fr08CKG/7dDseFtn6M6g/r3w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.34.5.tgz", + "integrity": "sha512-YNEFAF/4KQ/PeW0N+r+aVVsoIY0/qxxikF2SWdp+NRkmMB7y9LBZAVqQ4yhGCm/H3H270OSykqmQMKLBhBJDEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.2.4.tgz", + "integrity": "sha512-zqjjo7RatFfFoP0MkQ51jfuFZBnVE2pRiaydKJ1G/rHZvnsrHAOcQALIi9sA5co5xenQdTugCvtb1cuf78Vf4g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.2.4.tgz", + "integrity": "sha512-1IOd5xfVhlGwX+zXv2N93k0yMONvUlANylbJw1eTah8K/Jtpi15KC+WSiaX/nBmbm2HxRM1gZ0nSdjSsrZbGKg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.2.4.tgz", + "integrity": "sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A==", + "cpu": [ + "arm" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.2.4.tgz", + "integrity": "sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.2.4.tgz", + "integrity": "sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.2.4.tgz", + "integrity": "sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.2.4.tgz", + "integrity": "sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.2.4.tgz", + "integrity": "sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.2.4.tgz", + "integrity": "sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.2.4.tgz", + "integrity": "sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.34.5.tgz", + "integrity": "sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw==", + "cpu": [ + "arm" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.34.5.tgz", + "integrity": "sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.34.5.tgz", + "integrity": "sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.34.5.tgz", + "integrity": "sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.34.5.tgz", + "integrity": "sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg==", + "cpu": [ + "s390x" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.34.5.tgz", + "integrity": "sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.34.5.tgz", + "integrity": "sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.34.5.tgz", + "integrity": "sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.34.5.tgz", + "integrity": "sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.7.0" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.34.5.tgz", + "integrity": "sha512-WQ3AgWCWYSb2yt+IG8mnC6Jdk9Whs7O0gxphblsLvdhSpSTtmu69ZG1Gkb6NuvxsNACwiPV6cNSZNzt0KPsw7g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.34.5.tgz", + "integrity": "sha512-FV9m/7NmeCmSHDD5j4+4pNI8Cp3aW+JvLoXcTUo0IqyjSfAZJ8dIUmijx1qaJsIiU+Hosw6xM5KijAWRJCSgNg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.34.5.tgz", + "integrity": "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@poppinss/colors": { + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/@poppinss/colors/-/colors-4.1.6.tgz", + "integrity": "sha512-H9xkIdFswbS8n1d6vmRd8+c10t2Qe+rZITbbDHHkQixH5+2x1FDGmi/0K+WgWiqQFKPSlIYB7jlH6Kpfn6Fleg==", + "dev": true, + "license": "MIT", + "dependencies": { + "kleur": "^4.1.5" + } + }, + "node_modules/@poppinss/dumper": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/@poppinss/dumper/-/dumper-0.6.5.tgz", + "integrity": "sha512-NBdYIb90J7LfOI32dOewKI1r7wnkiH6m920puQ3qHUeZkxNkQiFnXVWoE6YtFSv6QOiPPf7ys6i+HWWecDz7sw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@sindresorhus/is": "^7.0.2", + "supports-color": "^10.0.0" + } + }, + "node_modules/@poppinss/exception": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@poppinss/exception/-/exception-1.2.3.tgz", + "integrity": "sha512-dCED+QRChTVatE9ibtoaxc+WkdzOSjYTKi/+uacHWIsfodVfpsueo3+DKpgU5Px8qXjgmXkSvhXvSCz3fnP9lw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@rollup/rollup-android-arm-eabi": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.60.3.tgz", + "integrity": "sha512-x35CNW/ANXG3hE/EZpRU8MXX1JDN86hBb2wMGAtltkz7pc6cxgjpy1OMMfDosOQ+2hWqIkag/fGok1Yady9nGw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-android-arm64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.60.3.tgz", + "integrity": "sha512-xw3xtkDApIOGayehp2+Rz4zimfkaX65r4t47iy+ymQB2G4iJCBBfj0ogVg5jpvjpn8UWn/+q9tprxleYeNp3Hw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/@rollup/rollup-darwin-arm64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.60.3.tgz", + "integrity": "sha512-vo6Y5Qfpx7/5EaamIwi0WqW2+zfiusVihKatLvtN1VFVy3D13uERk/6gZLU1UiHRL6fDXqj/ELIeVRGnvcTE1g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-darwin-x64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.60.3.tgz", + "integrity": "sha512-D+0QGcZhBzTN82weOnsSlY7V7+RMmPuF1CkbxyMAGE8+ZHeUjyb76ZiWmBlCu//AQQONvxcqRbwZTajZKqjuOw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@rollup/rollup-freebsd-arm64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.60.3.tgz", + "integrity": "sha512-6HnvHCT7fDyj6R0Ph7A6x8dQS/S38MClRWeDLqc0MdfWkxjiu1HSDYrdPhqSILzjTIC/pnXbbJbo+ft+gy/9hQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-freebsd-x64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.60.3.tgz", + "integrity": "sha512-KHLgC3WKlUYW3ShFKnnosZDOJ0xjg9zp7au3sIm2bs/tGBeC2ipmvRh/N7JKi0t9Ue20C0dpEshi8WUubg+cnA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/@rollup/rollup-linux-arm-gnueabihf": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.60.3.tgz", + "integrity": "sha512-DV6fJoxEYWJOvaZIsok7KrYl0tPvga5OZ2yvKHNNYyk/2roMLqQAbGhr78EQ5YhHpnhLKJD3S1WFusAkmUuV5g==", + "cpu": [ + "arm" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm-musleabihf": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.60.3.tgz", + "integrity": "sha512-mQKoJAzvuOs6F+TZybQO4GOTSMUu7v0WdxEk24krQ/uUxXoPTtHjuaUuPmFhtBcM4K0ons8nrE3JyhTuCFtT/w==", + "cpu": [ + "arm" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.60.3.tgz", + "integrity": "sha512-Whjj2qoiJ6+OOJMGptTYazaJvjOJm+iKHpXQM1P3LzGjt7Ff++Tp7nH4N8J/BUA7R9IHfDyx4DJIflifwnbmIA==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-arm64-musl": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.60.3.tgz", + "integrity": "sha512-4YTNHKqGng5+yiZt3mg77nmyuCfmNfX4fPmyUapBcIk+BdwSwmCWGXOUxhXbBEkFHtoN5boLj/5NON+u5QC9tg==", + "cpu": [ + "arm64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-loong64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-gnu/-/rollup-linux-loong64-gnu-4.60.3.tgz", + "integrity": "sha512-SU3kNlhkpI4UqlUc2VXPGK9o886ZsSeGfMAX2ba2b8DKmMXq4AL7KUrkSWVbb7koVqx41Yczx6dx5PNargIrEA==", + "cpu": [ + "loong64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-loong64-musl": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-musl/-/rollup-linux-loong64-musl-4.60.3.tgz", + "integrity": "sha512-6lDLl5h4TXpB1mTf2rQWnAk/LcXrx9vBfu/DT5TIPhvMhRWaZ5MxkIc8u4lJAmBo6klTe1ywXIUHFjylW505sg==", + "cpu": [ + "loong64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-ppc64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-gnu/-/rollup-linux-ppc64-gnu-4.60.3.tgz", + "integrity": "sha512-BMo8bOw8evlup/8G+cj5xWtPyp93xPdyoSN16Zy90Q2QZ0ZYRhCt6ZJSwbrRzG9HApFabjwj2p25TUPDWrhzqQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-ppc64-musl": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-musl/-/rollup-linux-ppc64-musl-4.60.3.tgz", + "integrity": "sha512-E0L8X1dZN1/Rph+5VPF6Xj2G7JJvMACVXtamTJIDrVI44Y3K+G8gQaMEAavbqCGTa16InptiVrX6eM6pmJ+7qA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.60.3.tgz", + "integrity": "sha512-oZJ/WHaVfHUiRAtmTAeo3DcevNsVvH8mbvodjZy7D5QKvCefO371SiKRpxoDcCxB3PTRTLayWBkvmDQKTcX/sw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-riscv64-musl": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.60.3.tgz", + "integrity": "sha512-Dhbyh7j9FybM3YaTgaHmVALwA8AkUwTPccyCQ79TG9AJUsMQqgN1DDEZNr4+QUfwiWvLDumW5vdwzoeUF+TNxQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-s390x-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.60.3.tgz", + "integrity": "sha512-cJd1X5XhHHlltkaypz1UcWLA8AcoIi1aWhsvaWDskD1oz2eKCypnqvTQ8ykMNI0RSmm7NkTdSqSSD7zM0xa6Ig==", + "cpu": [ + "s390x" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.60.3.tgz", + "integrity": "sha512-DAZDBHQfG2oQuhY7mc6I3/qB4LU2fQCjRvxbDwd/Jdvb9fypP4IJ4qmtu6lNjes6B531AI8cg1aKC2di97bUxA==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-linux-x64-musl": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.60.3.tgz", + "integrity": "sha512-cRxsE8c13mZOh3vP+wLDxpQBRrOHDIGOWyDL93Sy0Ga8y515fBcC2pjUfFwUe5T7tqvTvWbCpg1URM/AXdWIXA==", + "cpu": [ + "x64" + ], + "dev": true, + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@rollup/rollup-openbsd-x64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-openbsd-x64/-/rollup-openbsd-x64-4.60.3.tgz", + "integrity": "sha512-QaWcIgRxqEdQdhJqW4DJctsH6HCmo5vHxY0krHSX4jMtOqfzC+dqDGuHM87bu4H8JBeibWx7jFz+h6/4C8wA5Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ] + }, + "node_modules/@rollup/rollup-openharmony-arm64": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-openharmony-arm64/-/rollup-openharmony-arm64-4.60.3.tgz", + "integrity": "sha512-AaXwSvUi3QIPtroAUw1t5yHGIyqKEXwH54WUocFolZhpGDruJcs8c+xPNDRn4XiQsS7MEwnYsHW2l0MBLDMkWg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ] + }, + "node_modules/@rollup/rollup-win32-arm64-msvc": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.60.3.tgz", + "integrity": "sha512-65LAKM/bAWDqKNEelHlcHvm2V+Vfb8C6INFxQXRHCvaVN1rJfwr4NvdP4FyzUaLqWfaCGaadf6UbTm8xJeYfEg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-ia32-msvc": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.60.3.tgz", + "integrity": "sha512-EEM2gyhBF5MFnI6vMKdX1LAosE627RGBzIoGMdLloPZkXrUN0Ckqgr2Qi8+J3zip/8NVVro3/FjB+tjhZUgUHA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-gnu": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-gnu/-/rollup-win32-x64-gnu-4.60.3.tgz", + "integrity": "sha512-E5Eb5H/DpxaoXH++Qkv28RcUJboMopmdDUALBczvHMf7hNIxaDZqwY5lK12UK1BHacSmvupoEWGu+n993Z0y1A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@rollup/rollup-win32-x64-msvc": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.60.3.tgz", + "integrity": "sha512-hPt/bgL5cE+Qp+/TPHBqptcAgPzgj46mPcg/16zNUmbQk0j+mOEQV/+Lqu8QRtDV3Ek95Q6FeFITpuhl6OTsAA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@sindresorhus/is": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-7.2.0.tgz", + "integrity": "sha512-P1Cz1dWaFfR4IR+U13mqqiGsLFf1KbayybWwdd2vfctdV6hDpUkgCY0nKOLLTMSoRd/jJNjtbqzf13K8DCCXQw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sindresorhus/is?sponsor=1" + } + }, + "node_modules/@speed-highlight/core": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@speed-highlight/core/-/core-1.2.15.tgz", + "integrity": "sha512-BMq1K3DsElxDWawkX6eLg9+CKJrTVGCBAWVuHXVUV2u0s2711qiChLSId6ikYPfxhdYocLNt3wWwSvDiTvFabw==", + "dev": true, + "license": "CC0-1.0" + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz", + "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "25.6.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-25.6.2.tgz", + "integrity": "sha512-sokuT28dxf9JT5Kady1fsXOvI4HVpjZa95NKT5y9PNTIrs2AsobR4GFAA90ZG8M+nxVRLysCXsVj6eGC7Vbrlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~7.19.0" + } + }, + "node_modules/@vitest/expect": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.2.4.tgz", + "integrity": "sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/mocker": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.2.4.tgz", + "integrity": "sha512-46ryTE9RZO/rfDd7pEqFl7etuyzekzEhUbTW3BvmeO/BcCMEgq59BKhek3dXDWgAj4oMK6OZi+vRr1wPW6qjEQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "3.2.4", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.17" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/pretty-format": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-3.2.4.tgz", + "integrity": "sha512-IVNZik8IVRJRTr9fxlitMKeJeXFFFN0JaB9PHPGQ8NKQbGpfjlTx9zO4RefN8gp7eqjNy8nyK3NZmBzOPeIxtA==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-3.2.4.tgz", + "integrity": "sha512-oukfKT9Mk41LreEW09vt45f8wx7DordoWUZMYdY/cyAk7w5TWkTRCNZYF7sX7n2wB7jyGAl74OxgwhPgKaqDMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "3.2.4", + "pathe": "^2.0.3", + "strip-literal": "^3.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-3.2.4.tgz", + "integrity": "sha512-dEYtS7qQP2CjU27QBC5oUOxLE/v5eLkGqPE0ZKEIDGMs4vKWe7IjgLOeauHsR0D5YuuycGRO5oSRXnwnmA78fQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "magic-string": "^0.30.17", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.2.4.tgz", + "integrity": "sha512-vAfasCOe6AIK70iP5UD11Ac4siNUNJ9i/9PZ3NKx07sG6sUxeag1LWdNrMWeKKYBLlzuK+Gn65Yd5nyL6ds+nw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyspy": "^4.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-3.2.4.tgz", + "integrity": "sha512-fB2V0JFrQSMsCo9HiSq3Ezpdv4iYaXRG1Sx8edX3MwxfyNn83mKiGzOcH+Fkxt4MHxr3y42fQi1oeAInqgX2QA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "loupe": "^3.1.4", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, + "node_modules/blake3-wasm": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/blake3-wasm/-/blake3-wasm-2.1.5.tgz", + "integrity": "sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g==", + "dev": true, + "license": "MIT" + }, + "node_modules/cac": { + "version": "6.7.14", + "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", + "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/chai": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/chai/-/chai-5.3.3.tgz", + "integrity": "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "assertion-error": "^2.0.1", + "check-error": "^2.1.1", + "deep-eql": "^5.0.1", + "loupe": "^3.1.0", + "pathval": "^2.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/check-error": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.3.tgz", + "integrity": "sha512-PAJdDJusoxnwm1VwW07VWwUN1sl7smmC3OKggvndJFadxxDRyFJBX/ggnu/KE4kQAB7a3Dp8f/YXC1FlUprWmA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 16" + } + }, + "node_modules/cjs-module-lexer": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.4.3.tgz", + "integrity": "sha512-9z8TZaGM1pfswYeXrUpzPrkx8UnWYdhJclsiYMm6x/w5+nN+8Tf/LnAgfLGQCm59qAOxU8WwHEq2vNwF6i4j+Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/deep-eql": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz", + "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/error-stack-parser-es": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/error-stack-parser-es/-/error-stack-parser-es-1.0.5.tgz", + "integrity": "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/es-module-lexer": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.7.0.tgz", + "integrity": "sha512-jEQoCwk8hyb2AZziIOLhDqpm5+2ww5uIE6lkO/6jcOCusfk6LhMHpXXfBLXTZ7Ydyt0j4VoUQv6uGNYbdW+kBA==", + "dev": true, + "license": "MIT" + }, + "node_modules/esbuild": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.3.tgz", + "integrity": "sha512-8VwMnyGCONIs6cWue2IdpHxHnAjzxnw2Zr7MkVxB2vjmQ2ivqGFb4LEG3SMnv0Gb2F/G/2yA8zUaiL1gywDCCg==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.27.3", + "@esbuild/android-arm": "0.27.3", + "@esbuild/android-arm64": "0.27.3", + "@esbuild/android-x64": "0.27.3", + "@esbuild/darwin-arm64": "0.27.3", + "@esbuild/darwin-x64": "0.27.3", + "@esbuild/freebsd-arm64": "0.27.3", + "@esbuild/freebsd-x64": "0.27.3", + "@esbuild/linux-arm": "0.27.3", + "@esbuild/linux-arm64": "0.27.3", + "@esbuild/linux-ia32": "0.27.3", + "@esbuild/linux-loong64": "0.27.3", + "@esbuild/linux-mips64el": "0.27.3", + "@esbuild/linux-ppc64": "0.27.3", + "@esbuild/linux-riscv64": "0.27.3", + "@esbuild/linux-s390x": "0.27.3", + "@esbuild/linux-x64": "0.27.3", + "@esbuild/netbsd-arm64": "0.27.3", + "@esbuild/netbsd-x64": "0.27.3", + "@esbuild/openbsd-arm64": "0.27.3", + "@esbuild/openbsd-x64": "0.27.3", + "@esbuild/openharmony-arm64": "0.27.3", + "@esbuild/sunos-x64": "0.27.3", + "@esbuild/win32-arm64": "0.27.3", + "@esbuild/win32-ia32": "0.27.3", + "@esbuild/win32-x64": "0.27.3" + } + }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/expect-type": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.3.0.tgz", + "integrity": "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/js-tokens": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.1.tgz", + "integrity": "sha512-mxa9E9ITFOt0ban3j6L5MpjwegGz6lBQmM1IJkWeBZGcMxto50+eWdjC/52xDbS2vy0k7vIMK0Fe2wfL9OQSpQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/kleur": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", + "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/loupe": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.2.1.tgz", + "integrity": "sha512-CdzqowRJCeLU72bHvWqwRBBlLcMEtIvGrlvef74kMnV2AolS9Y8xUv1I0U/MNAWMhBlKIoyuEgoJ0t/bbwHbLQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, + "node_modules/miniflare": { + "version": "4.20260310.0", + "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-4.20260310.0.tgz", + "integrity": "sha512-uC5vNPenFpDSj5aUU3wGSABG6UUqMr+Xs1m4AkCrTHo37F4Z6xcQw5BXqViTfPDVT/zcYH1UgTVoXhr1l6ZMXw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cspotcode/source-map-support": "0.8.1", + "sharp": "^0.34.5", + "undici": "7.18.2", + "workerd": "1.20260310.1", + "ws": "8.18.0", + "youch": "4.1.0-beta.10" + }, + "bin": { + "miniflare": "bootstrap.js" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true, + "license": "MIT" + }, + "node_modules/nanoid": { + "version": "3.3.12", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.12.tgz", + "integrity": "sha512-ZB9RH/39qpq5Vu6Y+NmUaFhQR6pp+M2Xt76XBnEwDaGcVAqhlvxrl3B2bKS5D3NH3QR76v3aSrKaF/Kiy7lEtQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/path-to-regexp": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz", + "integrity": "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, + "node_modules/pathval": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.1.tgz", + "integrity": "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 14.16" + } + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/postcss": { + "version": "8.5.14", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.14.tgz", + "integrity": "sha512-SoSL4+OSEtR99LHFZQiJLkT59C5B1amGO1NzTwj7TT1qCUgUO6hxOvzkOYxD+vMrXBM3XJIKzokoERdqQq/Zmg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.11", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/rollup": { + "version": "4.60.3", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.60.3.tgz", + "integrity": "sha512-pAQK9HalE84QSm4Po3EmWIZPd3FnjkShVkiMlz1iligWYkWQ7wHYd1PF/T7QZ5TVSD6uSTon5gBVMSM4JfBV+A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "1.0.8" + }, + "bin": { + "rollup": "dist/bin/rollup" + }, + "engines": { + "node": ">=18.0.0", + "npm": ">=8.0.0" + }, + "optionalDependencies": { + "@rollup/rollup-android-arm-eabi": "4.60.3", + "@rollup/rollup-android-arm64": "4.60.3", + "@rollup/rollup-darwin-arm64": "4.60.3", + "@rollup/rollup-darwin-x64": "4.60.3", + "@rollup/rollup-freebsd-arm64": "4.60.3", + "@rollup/rollup-freebsd-x64": "4.60.3", + "@rollup/rollup-linux-arm-gnueabihf": "4.60.3", + "@rollup/rollup-linux-arm-musleabihf": "4.60.3", + "@rollup/rollup-linux-arm64-gnu": "4.60.3", + "@rollup/rollup-linux-arm64-musl": "4.60.3", + "@rollup/rollup-linux-loong64-gnu": "4.60.3", + "@rollup/rollup-linux-loong64-musl": "4.60.3", + "@rollup/rollup-linux-ppc64-gnu": "4.60.3", + "@rollup/rollup-linux-ppc64-musl": "4.60.3", + "@rollup/rollup-linux-riscv64-gnu": "4.60.3", + "@rollup/rollup-linux-riscv64-musl": "4.60.3", + "@rollup/rollup-linux-s390x-gnu": "4.60.3", + "@rollup/rollup-linux-x64-gnu": "4.60.3", + "@rollup/rollup-linux-x64-musl": "4.60.3", + "@rollup/rollup-openbsd-x64": "4.60.3", + "@rollup/rollup-openharmony-arm64": "4.60.3", + "@rollup/rollup-win32-arm64-msvc": "4.60.3", + "@rollup/rollup-win32-ia32-msvc": "4.60.3", + "@rollup/rollup-win32-x64-gnu": "4.60.3", + "@rollup/rollup-win32-x64-msvc": "4.60.3", + "fsevents": "~2.3.2" + } + }, + "node_modules/rollup/node_modules/@types/estree": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", + "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==", + "dev": true, + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.8.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.0.tgz", + "integrity": "sha512-AcM7dV/5ul4EekoQ29Agm5vri8JNqRyj39o0qpX6vDF2GZrtutZl5RwgD1XnZjiTAfncsJhMI48QQH3sN87YNA==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/sharp": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.34.5.tgz", + "integrity": "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.0.0", + "detect-libc": "^2.1.2", + "semver": "^7.7.3" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.34.5", + "@img/sharp-darwin-x64": "0.34.5", + "@img/sharp-libvips-darwin-arm64": "1.2.4", + "@img/sharp-libvips-darwin-x64": "1.2.4", + "@img/sharp-libvips-linux-arm": "1.2.4", + "@img/sharp-libvips-linux-arm64": "1.2.4", + "@img/sharp-libvips-linux-ppc64": "1.2.4", + "@img/sharp-libvips-linux-riscv64": "1.2.4", + "@img/sharp-libvips-linux-s390x": "1.2.4", + "@img/sharp-libvips-linux-x64": "1.2.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", + "@img/sharp-libvips-linuxmusl-x64": "1.2.4", + "@img/sharp-linux-arm": "0.34.5", + "@img/sharp-linux-arm64": "0.34.5", + "@img/sharp-linux-ppc64": "0.34.5", + "@img/sharp-linux-riscv64": "0.34.5", + "@img/sharp-linux-s390x": "0.34.5", + "@img/sharp-linux-x64": "0.34.5", + "@img/sharp-linuxmusl-arm64": "0.34.5", + "@img/sharp-linuxmusl-x64": "0.34.5", + "@img/sharp-wasm32": "0.34.5", + "@img/sharp-win32-arm64": "0.34.5", + "@img/sharp-win32-ia32": "0.34.5", + "@img/sharp-win32-x64": "0.34.5" + } + }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/std-env": { + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.10.0.tgz", + "integrity": "sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg==", + "dev": true, + "license": "MIT" + }, + "node_modules/strip-literal": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/strip-literal/-/strip-literal-3.1.0.tgz", + "integrity": "sha512-8r3mkIM/2+PpjHoOtiAW8Rg3jJLHaV7xPwG+YRGrv6FP0wwk/toTpATxWYOW0BKdWwl82VT2tFYi5DlROa0Mxg==", + "dev": true, + "license": "MIT", + "dependencies": { + "js-tokens": "^9.0.1" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/supports-color": { + "version": "10.2.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-10.2.2.tgz", + "integrity": "sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyexec": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-0.3.2.tgz", + "integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyglobby": { + "version": "0.2.16", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.16.tgz", + "integrity": "sha512-pn99VhoACYR8nFHhxqix+uvsbXineAasWm5ojXoN8xEwK5Kd3/TrhNn1wByuD52UxWRLy8pu+kRMniEi6Eq9Zg==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinypool": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-1.1.1.tgz", + "integrity": "sha512-Zba82s87IFq9A9XmjiX5uZA/ARWDrB03OHlq+Vw1fSdt0I+4/Kutwy8BP4Y/y/aORMo61FQ0vIb5j44vSo5Pkg==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.0.0 || >=20.0.0" + } + }, + "node_modules/tinyrainbow": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-2.0.0.tgz", + "integrity": "sha512-op4nsTR47R6p0vMUUoYl/a+ljLFVtlfaXkLQmqfLR1qHma1h/ysYk4hEXZ880bf2CYgTskvTa/e196Vd5dDQXw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/tinyspy": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-4.0.4.tgz", + "integrity": "sha512-azl+t0z7pw/z958Gy9svOTuzqIk6xq+NSheJzn5MMWtWTFywIacg2wUlzKFGtt3cthx0r2SxMK0yzJOR0IES7Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true, + "license": "0BSD", + "optional": true + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici": { + "version": "7.18.2", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.18.2.tgz", + "integrity": "sha512-y+8YjDFzWdQlSE9N5nzKMT3g4a5UBX1HKowfdXh0uvAnTaqqwqB92Jt4UXBAeKekDs5IaDKyJFR4X1gYVCgXcw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.18.1" + } + }, + "node_modules/undici-types": { + "version": "7.19.2", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.19.2.tgz", + "integrity": "sha512-qYVnV5OEm2AW8cJMCpdV20CDyaN3g0AjDlOGf1OW4iaDEx8MwdtChUp4zu4H0VP3nDRF/8RKWH+IPp9uW0YGZg==", + "dev": true, + "license": "MIT" + }, + "node_modules/unenv": { + "version": "2.0.0-rc.24", + "resolved": "https://registry.npmjs.org/unenv/-/unenv-2.0.0-rc.24.tgz", + "integrity": "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "pathe": "^2.0.3" + } + }, + "node_modules/vite": { + "version": "7.3.3", + "resolved": "https://registry.npmjs.org/vite/-/vite-7.3.3.tgz", + "integrity": "sha512-/4XH147Ui7OGTjg3HbdWe5arnZQSbfuRzdr9Ec7TQi5I7R+ir0Rlc9GIvD4v0XZurELqA035KVXJXpR61xhiTA==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "^0.27.0", + "fdir": "^6.5.0", + "picomatch": "^4.0.3", + "postcss": "^8.5.6", + "rollup": "^4.43.0", + "tinyglobby": "^0.2.15" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "lightningcss": "^1.21.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "lightningcss": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vite-node": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-3.2.4.tgz", + "integrity": "sha512-EbKSKh+bh1E1IFxeO0pg1n4dvoOTt0UDiXMd/qn++r98+jPO1xtJilvXldeuQ8giIB5IkpjCgMleHMNEsGH6pg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cac": "^6.7.14", + "debug": "^4.4.1", + "es-module-lexer": "^1.7.0", + "pathe": "^2.0.3", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "bin": { + "vite-node": "vite-node.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/vitest": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.2.4.tgz", + "integrity": "sha512-LUCP5ev3GURDysTWiP47wRRUpLKMOfPh+yKTx3kVIEiu5KOMeqzpnYNsKyOoVrULivR8tLcks4+lga33Whn90A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/expect": "3.2.4", + "@vitest/mocker": "3.2.4", + "@vitest/pretty-format": "^3.2.4", + "@vitest/runner": "3.2.4", + "@vitest/snapshot": "3.2.4", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "debug": "^4.4.1", + "expect-type": "^1.2.1", + "magic-string": "^0.30.17", + "pathe": "^2.0.3", + "picomatch": "^4.0.2", + "std-env": "^3.9.0", + "tinybench": "^2.9.0", + "tinyexec": "^0.3.2", + "tinyglobby": "^0.2.14", + "tinypool": "^1.1.1", + "tinyrainbow": "^2.0.0", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0", + "vite-node": "3.2.4", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@types/debug": "^4.1.12", + "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", + "@vitest/browser": "3.2.4", + "@vitest/ui": "3.2.4", + "happy-dom": "*", + "jsdom": "*" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@types/debug": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + } + } + }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/workerd": { + "version": "1.20260310.1", + "resolved": "https://registry.npmjs.org/workerd/-/workerd-1.20260310.1.tgz", + "integrity": "sha512-yawXhypXXHtArikJj15HOMknNGikpBbSg2ZDe6lddUbqZnJXuCVSkgc/0ArUeVMG1jbbGvpst+REFtKwILvRTQ==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "bin": { + "workerd": "bin/workerd" + }, + "engines": { + "node": ">=16" + }, + "optionalDependencies": { + "@cloudflare/workerd-darwin-64": "1.20260310.1", + "@cloudflare/workerd-darwin-arm64": "1.20260310.1", + "@cloudflare/workerd-linux-64": "1.20260310.1", + "@cloudflare/workerd-linux-arm64": "1.20260310.1", + "@cloudflare/workerd-windows-64": "1.20260310.1" + } + }, + "node_modules/wrangler": { + "version": "4.90.0", + "resolved": "https://registry.npmjs.org/wrangler/-/wrangler-4.90.0.tgz", + "integrity": "sha512-bmNIykl59TfCUn5xQgU7IWylSsPx3LQaPLMSAq2VQHt89CBrcj9qXQ0eYfjBCWA5XTBVgten391evt7xxtXwcA==", + "dev": true, + "license": "MIT OR Apache-2.0", + "dependencies": { + "@cloudflare/kv-asset-handler": "0.5.0", + "@cloudflare/unenv-preset": "2.16.1", + "blake3-wasm": "2.1.5", + "esbuild": "0.27.3", + "miniflare": "4.20260507.1", + "path-to-regexp": "6.3.0", + "unenv": "2.0.0-rc.24", + "workerd": "1.20260507.1" + }, + "bin": { + "wrangler": "bin/wrangler.js", + "wrangler2": "bin/wrangler.js" + }, + "engines": { + "node": ">=22.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20260507.1" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + } + }, + "node_modules/wrangler/node_modules/@cloudflare/workerd-darwin-64": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-64/-/workerd-darwin-64-1.20260507.1.tgz", + "integrity": "sha512-S85aMwcaPJUjKWDiG6iMMnioKWtPLACa6m0j/EhHR1GYfVpnxb974cBc6d25L+sf7jHWHJI2u5hGp0UTJ7MtXQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/wrangler/node_modules/@cloudflare/workerd-darwin-arm64": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-arm64/-/workerd-darwin-arm64-1.20260507.1.tgz", + "integrity": "sha512-GMEBu8Zp9Q97HLnf7bWJN4KjWpN5MxpeqdvHjBGWNl8UYprJI0k+Jkp89+Wh5S8vIon+HoVbDfOzPa7VwgL6Eg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/wrangler/node_modules/@cloudflare/workerd-linux-64": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-64/-/workerd-linux-64-1.20260507.1.tgz", + "integrity": "sha512-QlrKEBdgA3uVc0Ok0Q3+0/CW0CTjgj5ySir1i1YY5FXVv0X6GpwtnB5umjunjF2MFprss+L+iFGZzxcSvMC1nA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/wrangler/node_modules/@cloudflare/workerd-linux-arm64": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-arm64/-/workerd-linux-arm64-1.20260507.1.tgz", + "integrity": "sha512-eGbbupEtK2nh9V9Dhcx3vv3GTKeXqSVNgAEYVCCN0NGS9tl9HbMoHRX/4JL181FKXROMigWBCQVL//qPhsAzBQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/wrangler/node_modules/@cloudflare/workerd-windows-64": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20260507.1.tgz", + "integrity": "sha512-dmClJ/E0BAcuDetQIZFqbeAXejWrG5pysGRMQ6T83Y0IW/7IAamY2zFEkAJ10I5xwZsdHuYsZtzlOxpEXpJs7A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/wrangler/node_modules/miniflare": { + "version": "4.20260507.1", + "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-4.20260507.1.tgz", + "integrity": "sha512-PSXBiLExTdZ4UGO/raKCHQauUpYL7F880ZRB7j0+78Rv8h7TsdN2E/iEDK9sK2Y+SPQ5wJSeAa+rDeVKoZZoEw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cspotcode/source-map-support": "0.8.1", + "sharp": "^0.34.5", + "undici": "7.24.8", + "workerd": "1.20260507.1", + "ws": "8.18.0", + "youch": "4.1.0-beta.10" + }, + "bin": { + "miniflare": "bootstrap.js" + }, + "engines": { + "node": ">=22.0.0" + } + }, + "node_modules/wrangler/node_modules/undici": { + "version": "7.24.8", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.24.8.tgz", + "integrity": "sha512-6KQ/+QxK49Z/p3HO6E5ZCZWNnCasyZLa5ExaVYyvPxUwKtbCPMKELJOqh7EqOle0t9cH/7d2TaaTRRa6Nhs4YQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.18.1" + } + }, + "node_modules/wrangler/node_modules/workerd": { + "version": "1.20260507.1", + "resolved": "https://registry.npmjs.org/workerd/-/workerd-1.20260507.1.tgz", + "integrity": "sha512-z7JhsFSe6+X1b5fUHaVpo15VM1IRMJiLofEkq8iKdCo+Veqc+FUg5lIsuz8NwePxuSKrXtO4ZQpGkQLbPVXFhg==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "bin": { + "workerd": "bin/workerd" + }, + "engines": { + "node": ">=16" + }, + "optionalDependencies": { + "@cloudflare/workerd-darwin-64": "1.20260507.1", + "@cloudflare/workerd-darwin-arm64": "1.20260507.1", + "@cloudflare/workerd-linux-64": "1.20260507.1", + "@cloudflare/workerd-linux-arm64": "1.20260507.1", + "@cloudflare/workerd-windows-64": "1.20260507.1" + } + }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/youch": { + "version": "4.1.0-beta.10", + "resolved": "https://registry.npmjs.org/youch/-/youch-4.1.0-beta.10.tgz", + "integrity": "sha512-rLfVLB4FgQneDr0dv1oddCVZmKjcJ6yX6mS4pU82Mq/Dt9a3cLZQ62pDBL4AUO+uVrCvtWz3ZFUL2HFAFJ/BXQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@poppinss/dumper": "^0.6.4", + "@speed-highlight/core": "^1.2.7", + "cookie": "^1.0.2", + "youch-core": "^0.3.3" + } + }, + "node_modules/youch-core": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/youch-core/-/youch-core-0.3.3.tgz", + "integrity": "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/exception": "^1.2.2", + "error-stack-parser-es": "^1.0.5" + } + } + } +} diff --git a/cloudflare/edge-api/package.json b/cloudflare/edge-api/package.json new file mode 100644 index 0000000000..4452b7a7f3 --- /dev/null +++ b/cloudflare/edge-api/package.json @@ -0,0 +1,19 @@ +{ + "name": "edge-api", + "version": "0.0.0", + "private": true, + "scripts": { + "deploy": "wrangler deploy", + "dev": "wrangler dev", + "start": "wrangler dev", + "test": "vitest", + "cf-typegen": "wrangler types" + }, + "devDependencies": { + "@cloudflare/vitest-pool-workers": "^0.12.4", + "@types/node": "^25.6.2", + "typescript": "^5.5.2", + "vitest": "~3.2.0", + "wrangler": "^4.90.0" + } +} \ No newline at end of file diff --git a/cloudflare/edge-api/src/index.ts b/cloudflare/edge-api/src/index.ts new file mode 100644 index 0000000000..6b8ad0d63c --- /dev/null +++ b/cloudflare/edge-api/src/index.ts @@ -0,0 +1,129 @@ +/** + * Welcome to Cloudflare Workers! This is your first worker. + * + * - Run `npm run dev` in your terminal to start a development server + * - Open a browser tab at http://localhost:8787/ to see your worker in action + * - Run `npm run deploy` to publish your worker + * + * Bind resources to your worker in `wrangler.jsonc`. After adding bindings, a type definition for the + * `Env` object can be regenerated with `npm run cf-typegen`. + * + * Learn more at https://developers.cloudflare.com/workers/ + */ + +export interface Env { + COURSE_NAME: string; + APP_NAME: string; + COUNTER_KV: KVNamespace; + API_TOKEN: string; + ADMIN_EMAIL: string; +} + +function jsonResponse(data: unknown, status = 200): Response { + return new Response(JSON.stringify(data, null, 2), { + status, + headers: { + "Content-Type": "application/json", + }, + }); +} + +export default { + async fetch(request: Request, env: Env): Promise { + const url = new URL(request.url); + + if (url.pathname === "/") { + const current = await env.COUNTER_KV.get("visits"); + let count = current ? parseInt(current) : 0; + count += 1; + await env.COUNTER_KV.put("visits", count.toString()); + + console.log("request", { + path: url.pathname, + method: request.method, + colo: request.cf?.colo, + }); + + return jsonResponse({ + app: env.APP_NAME, + course: env.COURSE_NAME, + message: "Hello friend!", + framework: "Cloudflare Workers", + timestamp: new Date().toISOString(), + visits: count + }); + } + + if (url.pathname === "/health") { + console.log("request", { + path: url.pathname, + method: request.method, + colo: request.cf?.colo, + }); + return jsonResponse({ + status: "healthy", + timestamp: new Date().toISOString(), + }); + } + + if (url.pathname === "/admin-check") { + console.log("request", { + path: url.pathname, + method: request.method, + colo: request.cf?.colo, + }); + const token = request.headers.get("token"); + const email = request.headers.get("email"); + + const valid = + token === env.API_TOKEN && + email === env.ADMIN_EMAIL; + + return jsonResponse({ + valid: valid, + }); + } + + + if (url.pathname === "/edge") { + console.log("request", { + path: url.pathname, + method: request.method, + colo: request.cf?.colo, + }); + return jsonResponse({ + colo: request.cf?.colo, + country: request.cf?.country, + city: request.cf?.city, + asn: request.cf?.asn, + httpProtocol: request.cf?.httpProtocol, + tlsVersion: request.cf?.tlsVersion, + userAgent: request.headers.get("User-Agent"), + }); + } + + if (url.pathname === "/counter") { + console.log("request", { + path: url.pathname, + method: request.method, + colo: request.cf?.colo, + }); + const current = await env.COUNTER_KV.get("visits"); + let count = current ? parseInt(current) : 0; + count += 1; + + return jsonResponse({ + visits: count, + }); + } + + return jsonResponse( + { + error: "Not Found", + }, + 404 + ); + + + }, +}; \ No newline at end of file diff --git a/cloudflare/edge-api/test/env.d.ts b/cloudflare/edge-api/test/env.d.ts new file mode 100644 index 0000000000..67b3610dbc --- /dev/null +++ b/cloudflare/edge-api/test/env.d.ts @@ -0,0 +1,3 @@ +declare module "cloudflare:test" { + interface ProvidedEnv extends Env {} +} diff --git a/cloudflare/edge-api/test/index.spec.ts b/cloudflare/edge-api/test/index.spec.ts new file mode 100644 index 0000000000..017dbd0c3e --- /dev/null +++ b/cloudflare/edge-api/test/index.spec.ts @@ -0,0 +1,33 @@ +import { + env, + SELF, +} from "cloudflare:test"; + +import { describe, it, expect } from "vitest"; +import worker from "../src/index"; + +const IncomingRequest = Request; + +describe("Edge API worker", () => { + it("responds from root endpoint", async () => { + const request = new IncomingRequest("http://example.com"); + + const response = await worker.fetch(request, env); + + expect(response.status).toBe(200); + + const text = await response.text(); + + expect(text).toContain("Hello friend!"); + }); + + it("responds from integration test", async () => { + const response = await SELF.fetch("https://example.com"); + + expect(response.status).toBe(200); + + const text = await response.text(); + + expect(text).toContain("Hello friend!"); + }); +}); \ No newline at end of file diff --git a/cloudflare/edge-api/test/tsconfig.json b/cloudflare/edge-api/test/tsconfig.json new file mode 100644 index 0000000000..978ecd87b7 --- /dev/null +++ b/cloudflare/edge-api/test/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["@cloudflare/vitest-pool-workers"] + }, + "include": ["./**/*.ts", "../worker-configuration.d.ts"], + "exclude": [] +} diff --git a/cloudflare/edge-api/tsconfig.json b/cloudflare/edge-api/tsconfig.json new file mode 100644 index 0000000000..8c98cdbece --- /dev/null +++ b/cloudflare/edge-api/tsconfig.json @@ -0,0 +1,46 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2024", + /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": ["es2024"], + /* Specify what JSX code is generated. */ + "jsx": "react-jsx", + + /* Specify what module code is generated. */ + "module": "es2022", + /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "Bundler", + /* Enable importing .json files */ + "resolveJsonModule": true, + + /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + "allowJs": true, + /* Enable error reporting in type-checked JavaScript files. */ + "checkJs": false, + + /* Disable emitting files from a compilation. */ + "noEmit": true, + + /* Ensure that each file can be safely transpiled without relying on other imports. */ + "isolatedModules": true, + /* Allow 'import x from y' when a module doesn't have a default export. */ + "allowSyntheticDefaultImports": true, + /* Ensure that casing is correct in imports. */ + "forceConsistentCasingInFileNames": true, + + /* Enable all strict type-checking options. */ + "strict": true, + + /* Skip type checking all .d.ts files. */ + "skipLibCheck": true, + "types": [ + "./worker-configuration.d.ts", + "node" + ] + }, + "exclude": ["test"], + "include": ["worker-configuration.d.ts", "src/**/*.ts"] +} diff --git a/cloudflare/edge-api/vitest.config.mts b/cloudflare/edge-api/vitest.config.mts new file mode 100644 index 0000000000..7ccad75efa --- /dev/null +++ b/cloudflare/edge-api/vitest.config.mts @@ -0,0 +1,11 @@ +import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; + +export default defineWorkersConfig({ + test: { + poolOptions: { + workers: { + wrangler: { configPath: "./wrangler.jsonc" }, + }, + }, + }, +}); diff --git a/cloudflare/edge-api/worker-configuration.d.ts b/cloudflare/edge-api/worker-configuration.d.ts new file mode 100644 index 0000000000..2e82a8c549 --- /dev/null +++ b/cloudflare/edge-api/worker-configuration.d.ts @@ -0,0 +1,13559 @@ +/* eslint-disable */ +// Generated by Wrangler by running `wrangler types` (hash: dde33b4d5fc92e4633f964a9bd2a5f64) +// Runtime types generated with workerd@1.20260507.1 2026-05-09 nodejs_compat +declare namespace Cloudflare { + interface GlobalProps { + mainModule: typeof import("./src/index"); + } + interface Env { + COUNTER_KV: KVNamespace; + APP_NAME: "my-edge-api-app"; + COURSE_NAME: "devops-core"; + } +} +interface Env extends Cloudflare.Env {} +type StringifyValues> = { + [Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string; +}; +declare namespace NodeJS { + interface ProcessEnv extends StringifyValues> {} +} + +// Begin runtime types +/*! ***************************************************************************** +Copyright (c) Cloudflare. All rights reserved. +Copyright (c) Microsoft Corporation. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* eslint-disable */ +// noinspection JSUnusedGlobalSymbols +declare var onmessage: never; +/** + * The **`DOMException`** interface represents an abnormal event (called an **exception**) that occurs as a result of calling a method or accessing a property of a web API. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException) + */ +declare class DOMException extends Error { + constructor(message?: string, name?: string); + /** + * The **`message`** read-only property of the a message or description associated with the given error name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/message) + */ + readonly message: string; + /** + * The **`name`** read-only property of the one of the strings associated with an error name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/name) + */ + readonly name: string; + /** + * The **`code`** read-only property of the DOMException interface returns one of the legacy error code constants, or `0` if none match. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/code) + */ + readonly code: number; + static readonly INDEX_SIZE_ERR: number; + static readonly DOMSTRING_SIZE_ERR: number; + static readonly HIERARCHY_REQUEST_ERR: number; + static readonly WRONG_DOCUMENT_ERR: number; + static readonly INVALID_CHARACTER_ERR: number; + static readonly NO_DATA_ALLOWED_ERR: number; + static readonly NO_MODIFICATION_ALLOWED_ERR: number; + static readonly NOT_FOUND_ERR: number; + static readonly NOT_SUPPORTED_ERR: number; + static readonly INUSE_ATTRIBUTE_ERR: number; + static readonly INVALID_STATE_ERR: number; + static readonly SYNTAX_ERR: number; + static readonly INVALID_MODIFICATION_ERR: number; + static readonly NAMESPACE_ERR: number; + static readonly INVALID_ACCESS_ERR: number; + static readonly VALIDATION_ERR: number; + static readonly TYPE_MISMATCH_ERR: number; + static readonly SECURITY_ERR: number; + static readonly NETWORK_ERR: number; + static readonly ABORT_ERR: number; + static readonly URL_MISMATCH_ERR: number; + static readonly QUOTA_EXCEEDED_ERR: number; + static readonly TIMEOUT_ERR: number; + static readonly INVALID_NODE_TYPE_ERR: number; + static readonly DATA_CLONE_ERR: number; + get stack(): any; + set stack(value: any); +} +type WorkerGlobalScopeEventMap = { + fetch: FetchEvent; + scheduled: ScheduledEvent; + queue: QueueEvent; + unhandledrejection: PromiseRejectionEvent; + rejectionhandled: PromiseRejectionEvent; +}; +declare abstract class WorkerGlobalScope extends EventTarget { + EventTarget: typeof EventTarget; +} +/* The **`console`** object provides access to the debugging console (e.g., the Web console in Firefox). * + * The **`console`** object provides access to the debugging console (e.g., the Web console in Firefox). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console) + */ +interface Console { + "assert"(condition?: boolean, ...data: any[]): void; + /** + * The **`console.clear()`** static method clears the console if possible. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/clear_static) + */ + clear(): void; + /** + * The **`console.count()`** static method logs the number of times that this particular call to `count()` has been called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static) + */ + count(label?: string): void; + /** + * The **`console.countReset()`** static method resets counter used with console/count_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/countReset_static) + */ + countReset(label?: string): void; + /** + * The **`console.debug()`** static method outputs a message to the console at the 'debug' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/debug_static) + */ + debug(...data: any[]): void; + /** + * The **`console.dir()`** static method displays a list of the properties of the specified JavaScript object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/dir_static) + */ + dir(item?: any, options?: any): void; + /** + * The **`console.dirxml()`** static method displays an interactive tree of the descendant elements of the specified XML/HTML element. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/dirxml_static) + */ + dirxml(...data: any[]): void; + /** + * The **`console.error()`** static method outputs a message to the console at the 'error' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static) + */ + error(...data: any[]): void; + /** + * The **`console.group()`** static method creates a new inline group in the Web console log, causing any subsequent console messages to be indented by an additional level, until console/groupEnd_static is called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/group_static) + */ + group(...data: any[]): void; + /** + * The **`console.groupCollapsed()`** static method creates a new inline group in the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/groupCollapsed_static) + */ + groupCollapsed(...data: any[]): void; + /** + * The **`console.groupEnd()`** static method exits the current inline group in the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/groupEnd_static) + */ + groupEnd(): void; + /** + * The **`console.info()`** static method outputs a message to the console at the 'info' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/info_static) + */ + info(...data: any[]): void; + /** + * The **`console.log()`** static method outputs a message to the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) + */ + log(...data: any[]): void; + /** + * The **`console.table()`** static method displays tabular data as a table. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/table_static) + */ + table(tabularData?: any, properties?: string[]): void; + /** + * The **`console.time()`** static method starts a timer you can use to track how long an operation takes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/time_static) + */ + time(label?: string): void; + /** + * The **`console.timeEnd()`** static method stops a timer that was previously started by calling console/time_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/timeEnd_static) + */ + timeEnd(label?: string): void; + /** + * The **`console.timeLog()`** static method logs the current value of a timer that was previously started by calling console/time_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/timeLog_static) + */ + timeLog(label?: string, ...data: any[]): void; + timeStamp(label?: string): void; + /** + * The **`console.trace()`** static method outputs a stack trace to the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/trace_static) + */ + trace(...data: any[]): void; + /** + * The **`console.warn()`** static method outputs a warning message to the console at the 'warning' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/warn_static) + */ + warn(...data: any[]): void; +} +declare const console: Console; +type BufferSource = ArrayBufferView | ArrayBuffer; +type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; +declare namespace WebAssembly { + class CompileError extends Error { + constructor(message?: string); + } + class RuntimeError extends Error { + constructor(message?: string); + } + type ValueType = "anyfunc" | "externref" | "f32" | "f64" | "i32" | "i64" | "v128"; + interface GlobalDescriptor { + value: ValueType; + mutable?: boolean; + } + class Global { + constructor(descriptor: GlobalDescriptor, value?: any); + value: any; + valueOf(): any; + } + type ImportValue = ExportValue | number; + type ModuleImports = Record; + type Imports = Record; + type ExportValue = Function | Global | Memory | Table; + type Exports = Record; + class Instance { + constructor(module: Module, imports?: Imports); + readonly exports: Exports; + } + interface MemoryDescriptor { + initial: number; + maximum?: number; + shared?: boolean; + } + class Memory { + constructor(descriptor: MemoryDescriptor); + readonly buffer: ArrayBuffer; + grow(delta: number): number; + } + type ImportExportKind = "function" | "global" | "memory" | "table"; + interface ModuleExportDescriptor { + kind: ImportExportKind; + name: string; + } + interface ModuleImportDescriptor { + kind: ImportExportKind; + module: string; + name: string; + } + abstract class Module { + static customSections(module: Module, sectionName: string): ArrayBuffer[]; + static exports(module: Module): ModuleExportDescriptor[]; + static imports(module: Module): ModuleImportDescriptor[]; + } + type TableKind = "anyfunc" | "externref"; + interface TableDescriptor { + element: TableKind; + initial: number; + maximum?: number; + } + class Table { + constructor(descriptor: TableDescriptor, value?: any); + readonly length: number; + get(index: number): any; + grow(delta: number, value?: any): number; + set(index: number, value?: any): void; + } + function instantiate(module: Module, imports?: Imports): Promise; + function validate(bytes: BufferSource): boolean; +} +/** + * The **`ServiceWorkerGlobalScope`** interface of the Service Worker API represents the global execution context of a service worker. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ServiceWorkerGlobalScope) + */ +interface ServiceWorkerGlobalScope extends WorkerGlobalScope { + DOMException: typeof DOMException; + WorkerGlobalScope: typeof WorkerGlobalScope; + btoa(data: string): string; + atob(data: string): string; + setTimeout(callback: (...args: any[]) => void, msDelay?: number): number; + setTimeout(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; + clearTimeout(timeoutId: number | null): void; + setInterval(callback: (...args: any[]) => void, msDelay?: number): number; + setInterval(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; + clearInterval(timeoutId: number | null): void; + queueMicrotask(task: Function): void; + structuredClone(value: T, options?: StructuredSerializeOptions): T; + reportError(error: any): void; + fetch(input: RequestInfo | URL, init?: RequestInit): Promise; + self: ServiceWorkerGlobalScope; + crypto: Crypto; + caches: CacheStorage; + scheduler: Scheduler; + performance: Performance; + Cloudflare: Cloudflare; + readonly origin: string; + Event: typeof Event; + ExtendableEvent: typeof ExtendableEvent; + CustomEvent: typeof CustomEvent; + PromiseRejectionEvent: typeof PromiseRejectionEvent; + FetchEvent: typeof FetchEvent; + TailEvent: typeof TailEvent; + TraceEvent: typeof TailEvent; + ScheduledEvent: typeof ScheduledEvent; + MessageEvent: typeof MessageEvent; + CloseEvent: typeof CloseEvent; + ReadableStreamDefaultReader: typeof ReadableStreamDefaultReader; + ReadableStreamBYOBReader: typeof ReadableStreamBYOBReader; + ReadableStream: typeof ReadableStream; + WritableStream: typeof WritableStream; + WritableStreamDefaultWriter: typeof WritableStreamDefaultWriter; + TransformStream: typeof TransformStream; + ByteLengthQueuingStrategy: typeof ByteLengthQueuingStrategy; + CountQueuingStrategy: typeof CountQueuingStrategy; + ErrorEvent: typeof ErrorEvent; + MessageChannel: typeof MessageChannel; + MessagePort: typeof MessagePort; + EventSource: typeof EventSource; + ReadableStreamBYOBRequest: typeof ReadableStreamBYOBRequest; + ReadableStreamDefaultController: typeof ReadableStreamDefaultController; + ReadableByteStreamController: typeof ReadableByteStreamController; + WritableStreamDefaultController: typeof WritableStreamDefaultController; + TransformStreamDefaultController: typeof TransformStreamDefaultController; + CompressionStream: typeof CompressionStream; + DecompressionStream: typeof DecompressionStream; + TextEncoderStream: typeof TextEncoderStream; + TextDecoderStream: typeof TextDecoderStream; + Headers: typeof Headers; + Body: typeof Body; + Request: typeof Request; + Response: typeof Response; + WebSocket: typeof WebSocket; + WebSocketPair: typeof WebSocketPair; + WebSocketRequestResponsePair: typeof WebSocketRequestResponsePair; + AbortController: typeof AbortController; + AbortSignal: typeof AbortSignal; + TextDecoder: typeof TextDecoder; + TextEncoder: typeof TextEncoder; + navigator: Navigator; + Navigator: typeof Navigator; + URL: typeof URL; + URLSearchParams: typeof URLSearchParams; + URLPattern: typeof URLPattern; + Blob: typeof Blob; + File: typeof File; + FormData: typeof FormData; + Crypto: typeof Crypto; + SubtleCrypto: typeof SubtleCrypto; + CryptoKey: typeof CryptoKey; + CacheStorage: typeof CacheStorage; + Cache: typeof Cache; + FixedLengthStream: typeof FixedLengthStream; + IdentityTransformStream: typeof IdentityTransformStream; + HTMLRewriter: typeof HTMLRewriter; +} +declare function addEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetAddEventListenerOptions | boolean): void; +declare function removeEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetEventListenerOptions | boolean): void; +/** + * The **`dispatchEvent()`** method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent) + */ +declare function dispatchEvent(event: WorkerGlobalScopeEventMap[keyof WorkerGlobalScopeEventMap]): boolean; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/btoa) */ +declare function btoa(data: string): string; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/atob) */ +declare function atob(data: string): string; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setTimeout) */ +declare function setTimeout(callback: (...args: any[]) => void, msDelay?: number): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setTimeout) */ +declare function setTimeout(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/clearTimeout) */ +declare function clearTimeout(timeoutId: number | null): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setInterval) */ +declare function setInterval(callback: (...args: any[]) => void, msDelay?: number): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setInterval) */ +declare function setInterval(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/clearInterval) */ +declare function clearInterval(timeoutId: number | null): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/queueMicrotask) */ +declare function queueMicrotask(task: Function): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/structuredClone) */ +declare function structuredClone(value: T, options?: StructuredSerializeOptions): T; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/reportError) */ +declare function reportError(error: any): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/fetch) */ +declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise; +declare const self: ServiceWorkerGlobalScope; +/** +* The Web Crypto API provides a set of low-level functions for common cryptographic tasks. +* The Workers runtime implements the full surface of this API, but with some differences in +* the [supported algorithms](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/#supported-algorithms) +* compared to those implemented in most browsers. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/) +*/ +declare const crypto: Crypto; +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare const caches: CacheStorage; +declare const scheduler: Scheduler; +/** +* The Workers runtime supports a subset of the Performance API, used to measure timing and performance, +* as well as timing of subrequests and other operations. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/) +*/ +declare const performance: Performance; +declare const Cloudflare: Cloudflare; +declare const origin: string; +declare const navigator: Navigator; +interface TestController { +} +interface ExecutionContext { + waitUntil(promise: Promise): void; + passThroughOnException(): void; + readonly exports: Cloudflare.Exports; + readonly props: Props; + cache?: CacheContext; + tracing?: Tracing; +} +type ExportedHandlerFetchHandler = (request: Request>, env: Env, ctx: ExecutionContext) => Response | Promise; +type ExportedHandlerConnectHandler = (socket: Socket, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTailHandler = (events: TraceItem[], env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTraceHandler = (traces: TraceItem[], env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTailStreamHandler = (event: TailStream.TailEvent, env: Env, ctx: ExecutionContext) => TailStream.TailEventHandlerType | Promise; +type ExportedHandlerScheduledHandler = (controller: ScheduledController, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerQueueHandler = (batch: MessageBatch, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTestHandler = (controller: TestController, env: Env, ctx: ExecutionContext) => void | Promise; +interface ExportedHandler { + fetch?: ExportedHandlerFetchHandler; + connect?: ExportedHandlerConnectHandler; + tail?: ExportedHandlerTailHandler; + trace?: ExportedHandlerTraceHandler; + tailStream?: ExportedHandlerTailStreamHandler; + scheduled?: ExportedHandlerScheduledHandler; + test?: ExportedHandlerTestHandler; + email?: EmailExportedHandler; + queue?: ExportedHandlerQueueHandler; +} +interface StructuredSerializeOptions { + transfer?: any[]; +} +declare abstract class Navigator { + sendBeacon(url: string, body?: BodyInit): boolean; + readonly userAgent: string; + readonly hardwareConcurrency: number; + readonly platform: string; + readonly language: string; + readonly languages: string[]; +} +interface AlarmInvocationInfo { + readonly isRetry: boolean; + readonly retryCount: number; + readonly scheduledTime: number; +} +interface Cloudflare { + readonly compatibilityFlags: Record; +} +interface CachePurgeError { + code: number; + message: string; +} +interface CachePurgeResult { + success: boolean; + errors: CachePurgeError[]; +} +interface CachePurgeOptions { + tags?: string[]; + pathPrefixes?: string[]; + purgeEverything?: boolean; +} +interface CacheContext { + purge(options: CachePurgeOptions): Promise; +} +declare abstract class ColoLocalActorNamespace { + get(actorId: string): Fetcher; +} +interface DurableObject { + fetch(request: Request): Response | Promise; + connect?(socket: Socket): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; + webSocketMessage?(ws: WebSocket, message: string | ArrayBuffer): void | Promise; + webSocketClose?(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise; + webSocketError?(ws: WebSocket, error: unknown): void | Promise; +} +type DurableObjectStub = Fetcher & { + readonly id: DurableObjectId; + readonly name?: string; +}; +interface DurableObjectId { + toString(): string; + equals(other: DurableObjectId): boolean; + readonly name?: string; + readonly jurisdiction?: string; +} +declare abstract class DurableObjectNamespace { + newUniqueId(options?: DurableObjectNamespaceNewUniqueIdOptions): DurableObjectId; + idFromName(name: string): DurableObjectId; + idFromString(id: string): DurableObjectId; + get(id: DurableObjectId, options?: DurableObjectNamespaceGetDurableObjectOptions): DurableObjectStub; + getByName(name: string, options?: DurableObjectNamespaceGetDurableObjectOptions): DurableObjectStub; + jurisdiction(jurisdiction: DurableObjectJurisdiction): DurableObjectNamespace; +} +type DurableObjectJurisdiction = "eu" | "fedramp" | "fedramp-high"; +interface DurableObjectNamespaceNewUniqueIdOptions { + jurisdiction?: DurableObjectJurisdiction; +} +type DurableObjectLocationHint = "wnam" | "enam" | "sam" | "weur" | "eeur" | "apac" | "oc" | "afr" | "me"; +type DurableObjectRoutingMode = "primary-only"; +interface DurableObjectNamespaceGetDurableObjectOptions { + locationHint?: DurableObjectLocationHint; + routingMode?: DurableObjectRoutingMode; +} +interface DurableObjectClass<_T extends Rpc.DurableObjectBranded | undefined = undefined> { +} +interface DurableObjectState { + waitUntil(promise: Promise): void; + readonly exports: Cloudflare.Exports; + readonly props: Props; + readonly id: DurableObjectId; + readonly storage: DurableObjectStorage; + container?: Container; + facets: DurableObjectFacets; + blockConcurrencyWhile(callback: () => Promise): Promise; + acceptWebSocket(ws: WebSocket, tags?: string[]): void; + getWebSockets(tag?: string): WebSocket[]; + setWebSocketAutoResponse(maybeReqResp?: WebSocketRequestResponsePair): void; + getWebSocketAutoResponse(): WebSocketRequestResponsePair | null; + getWebSocketAutoResponseTimestamp(ws: WebSocket): Date | null; + setHibernatableWebSocketEventTimeout(timeoutMs?: number): void; + getHibernatableWebSocketEventTimeout(): number | null; + getTags(ws: WebSocket): string[]; + abort(reason?: string): void; +} +interface DurableObjectTransaction { + get(key: string, options?: DurableObjectGetOptions): Promise; + get(keys: string[], options?: DurableObjectGetOptions): Promise>; + list(options?: DurableObjectListOptions): Promise>; + put(key: string, value: T, options?: DurableObjectPutOptions): Promise; + put(entries: Record, options?: DurableObjectPutOptions): Promise; + delete(key: string, options?: DurableObjectPutOptions): Promise; + delete(keys: string[], options?: DurableObjectPutOptions): Promise; + rollback(): void; + getAlarm(options?: DurableObjectGetAlarmOptions): Promise; + setAlarm(scheduledTime: number | Date, options?: DurableObjectSetAlarmOptions): Promise; + deleteAlarm(options?: DurableObjectSetAlarmOptions): Promise; +} +interface DurableObjectStorage { + get(key: string, options?: DurableObjectGetOptions): Promise; + get(keys: string[], options?: DurableObjectGetOptions): Promise>; + list(options?: DurableObjectListOptions): Promise>; + put(key: string, value: T, options?: DurableObjectPutOptions): Promise; + put(entries: Record, options?: DurableObjectPutOptions): Promise; + delete(key: string, options?: DurableObjectPutOptions): Promise; + delete(keys: string[], options?: DurableObjectPutOptions): Promise; + deleteAll(options?: DurableObjectPutOptions): Promise; + transaction(closure: (txn: DurableObjectTransaction) => Promise): Promise; + getAlarm(options?: DurableObjectGetAlarmOptions): Promise; + setAlarm(scheduledTime: number | Date, options?: DurableObjectSetAlarmOptions): Promise; + deleteAlarm(options?: DurableObjectSetAlarmOptions): Promise; + sync(): Promise; + sql: SqlStorage; + kv: SyncKvStorage; + transactionSync(closure: () => T): T; + getCurrentBookmark(): Promise; + getBookmarkForTime(timestamp: number | Date): Promise; + onNextSessionRestoreBookmark(bookmark: string): Promise; +} +interface DurableObjectListOptions { + start?: string; + startAfter?: string; + end?: string; + prefix?: string; + reverse?: boolean; + limit?: number; + allowConcurrency?: boolean; + noCache?: boolean; +} +interface DurableObjectGetOptions { + allowConcurrency?: boolean; + noCache?: boolean; +} +interface DurableObjectGetAlarmOptions { + allowConcurrency?: boolean; +} +interface DurableObjectPutOptions { + allowConcurrency?: boolean; + allowUnconfirmed?: boolean; + noCache?: boolean; +} +interface DurableObjectSetAlarmOptions { + allowConcurrency?: boolean; + allowUnconfirmed?: boolean; +} +declare class WebSocketRequestResponsePair { + constructor(request: string, response: string); + get request(): string; + get response(): string; +} +interface DurableObjectFacets { + get(name: string, getStartupOptions: () => FacetStartupOptions | Promise>): Fetcher; + abort(name: string, reason: any): void; + delete(name: string): void; +} +interface FacetStartupOptions { + id?: DurableObjectId | string; + class: DurableObjectClass; +} +interface AnalyticsEngineDataset { + writeDataPoint(event?: AnalyticsEngineDataPoint): void; +} +interface AnalyticsEngineDataPoint { + indexes?: ((ArrayBuffer | string) | null)[]; + doubles?: number[]; + blobs?: ((ArrayBuffer | string) | null)[]; +} +/** + * The **`Event`** interface represents an event which takes place on an `EventTarget`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event) + */ +declare class Event { + constructor(type: string, init?: EventInit); + /** + * The **`type`** read-only property of the Event interface returns a string containing the event's type. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/type) + */ + get type(): string; + /** + * The **`eventPhase`** read-only property of the being evaluated. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/eventPhase) + */ + get eventPhase(): number; + /** + * The read-only **`composed`** property of the or not the event will propagate across the shadow DOM boundary into the standard DOM. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composed) + */ + get composed(): boolean; + /** + * The **`bubbles`** read-only property of the Event interface indicates whether the event bubbles up through the DOM tree or not. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/bubbles) + */ + get bubbles(): boolean; + /** + * The **`cancelable`** read-only property of the Event interface indicates whether the event can be canceled, and therefore prevented as if the event never happened. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelable) + */ + get cancelable(): boolean; + /** + * The **`defaultPrevented`** read-only property of the Event interface returns a boolean value indicating whether or not the call to Event.preventDefault() canceled the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/defaultPrevented) + */ + get defaultPrevented(): boolean; + /** + * The Event property **`returnValue`** indicates whether the default action for this event has been prevented or not. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/returnValue) + */ + get returnValue(): boolean; + /** + * The **`currentTarget`** read-only property of the Event interface identifies the element to which the event handler has been attached. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/currentTarget) + */ + get currentTarget(): EventTarget | undefined; + /** + * The read-only **`target`** property of the dispatched. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/target) + */ + get target(): EventTarget | undefined; + /** + * The deprecated **`Event.srcElement`** is an alias for the Event.target property. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/srcElement) + */ + get srcElement(): EventTarget | undefined; + /** + * The **`timeStamp`** read-only property of the Event interface returns the time (in milliseconds) at which the event was created. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/timeStamp) + */ + get timeStamp(): number; + /** + * The **`isTrusted`** read-only property of the when the event was generated by the user agent (including via user actions and programmatic methods such as HTMLElement.focus()), and `false` when the event was dispatched via The only exception is the `click` event, which initializes the `isTrusted` property to `false` in user agents. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/isTrusted) + */ + get isTrusted(): boolean; + /** + * The **`cancelBubble`** property of the Event interface is deprecated. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble) + */ + get cancelBubble(): boolean; + /** + * The **`cancelBubble`** property of the Event interface is deprecated. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble) + */ + set cancelBubble(value: boolean); + /** + * The **`stopImmediatePropagation()`** method of the If several listeners are attached to the same element for the same event type, they are called in the order in which they were added. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopImmediatePropagation) + */ + stopImmediatePropagation(): void; + /** + * The **`preventDefault()`** method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/preventDefault) + */ + preventDefault(): void; + /** + * The **`stopPropagation()`** method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation) + */ + stopPropagation(): void; + /** + * The **`composedPath()`** method of the Event interface returns the event's path which is an array of the objects on which listeners will be invoked. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composedPath) + */ + composedPath(): EventTarget[]; + static readonly NONE: number; + static readonly CAPTURING_PHASE: number; + static readonly AT_TARGET: number; + static readonly BUBBLING_PHASE: number; +} +interface EventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} +type EventListener = (event: EventType) => void; +interface EventListenerObject { + handleEvent(event: EventType): void; +} +type EventListenerOrEventListenerObject = EventListener | EventListenerObject; +/** + * The **`EventTarget`** interface is implemented by objects that can receive events and may have listeners for them. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget) + */ +declare class EventTarget = Record> { + constructor(); + /** + * The **`addEventListener()`** method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener) + */ + addEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetAddEventListenerOptions | boolean): void; + /** + * The **`removeEventListener()`** method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener) + */ + removeEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetEventListenerOptions | boolean): void; + /** + * The **`dispatchEvent()`** method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent) + */ + dispatchEvent(event: EventMap[keyof EventMap]): boolean; +} +interface EventTargetEventListenerOptions { + capture?: boolean; +} +interface EventTargetAddEventListenerOptions { + capture?: boolean; + passive?: boolean; + once?: boolean; + signal?: AbortSignal; +} +interface EventTargetHandlerObject { + handleEvent: (event: Event) => any | undefined; +} +/** + * The **`AbortController`** interface represents a controller object that allows you to abort one or more Web requests as and when desired. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController) + */ +declare class AbortController { + constructor(); + /** + * The **`signal`** read-only property of the AbortController interface returns an AbortSignal object instance, which can be used to communicate with/abort an asynchronous operation as desired. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/signal) + */ + get signal(): AbortSignal; + /** + * The **`abort()`** method of the AbortController interface aborts an asynchronous operation before it has completed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/abort) + */ + abort(reason?: any): void; +} +/** + * The **`AbortSignal`** interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal) + */ +declare abstract class AbortSignal extends EventTarget { + /** + * The **`AbortSignal.abort()`** static method returns an AbortSignal that is already set as aborted (and which does not trigger an AbortSignal/abort_event event). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_static) + */ + static abort(reason?: any): AbortSignal; + /** + * The **`AbortSignal.timeout()`** static method returns an AbortSignal that will automatically abort after a specified time. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/timeout_static) + */ + static timeout(delay: number): AbortSignal; + /** + * The **`AbortSignal.any()`** static method takes an iterable of abort signals and returns an AbortSignal. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/any_static) + */ + static any(signals: AbortSignal[]): AbortSignal; + /** + * The **`aborted`** read-only property returns a value that indicates whether the asynchronous operations the signal is communicating with are aborted (`true`) or not (`false`). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/aborted) + */ + get aborted(): boolean; + /** + * The **`reason`** read-only property returns a JavaScript value that indicates the abort reason. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/reason) + */ + get reason(): any; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_event) */ + get onabort(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_event) */ + set onabort(value: any | null); + /** + * The **`throwIfAborted()`** method throws the signal's abort AbortSignal.reason if the signal has been aborted; otherwise it does nothing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/throwIfAborted) + */ + throwIfAborted(): void; +} +interface Scheduler { + wait(delay: number, maybeOptions?: SchedulerWaitOptions): Promise; +} +interface SchedulerWaitOptions { + signal?: AbortSignal; +} +/** + * The **`ExtendableEvent`** interface extends the lifetime of the `install` and `activate` events dispatched on the global scope as part of the service worker lifecycle. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ExtendableEvent) + */ +declare abstract class ExtendableEvent extends Event { + /** + * The **`ExtendableEvent.waitUntil()`** method tells the event dispatcher that work is ongoing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ExtendableEvent/waitUntil) + */ + waitUntil(promise: Promise): void; +} +/** + * The **`CustomEvent`** interface represents events initialized by an application for any purpose. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent) + */ +declare class CustomEvent extends Event { + constructor(type: string, init?: CustomEventCustomEventInit); + /** + * The read-only **`detail`** property of the CustomEvent interface returns any data passed when initializing the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail) + */ + get detail(): T; +} +interface CustomEventCustomEventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; + detail?: any; +} +/** + * The **`Blob`** interface represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob) + */ +declare class Blob { + constructor(bits?: ((ArrayBuffer | ArrayBufferView) | string | Blob)[], options?: BlobOptions); + /** + * The **`size`** read-only property of the Blob interface returns the size of the Blob or File in bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) + */ + get size(): number; + /** + * The **`type`** read-only property of the Blob interface returns the MIME type of the file. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) + */ + get type(): string; + /** + * The **`slice()`** method of the Blob interface creates and returns a new `Blob` object which contains data from a subset of the blob on which it's called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) + */ + slice(start?: number, end?: number, type?: string): Blob; + /** + * The **`arrayBuffer()`** method of the Blob interface returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer) + */ + arrayBuffer(): Promise; + /** + * The **`bytes()`** method of the Blob interface returns a Promise that resolves with a Uint8Array containing the contents of the blob as an array of bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/bytes) + */ + bytes(): Promise; + /** + * The **`text()`** method of the string containing the contents of the blob, interpreted as UTF-8. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) + */ + text(): Promise; + /** + * The **`stream()`** method of the Blob interface returns a ReadableStream which upon reading returns the data contained within the `Blob`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream) + */ + stream(): ReadableStream; +} +interface BlobOptions { + type?: string; +} +/** + * The **`File`** interface provides information about files and allows JavaScript in a web page to access their content. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File) + */ +declare class File extends Blob { + constructor(bits: ((ArrayBuffer | ArrayBufferView) | string | Blob)[] | undefined, name: string, options?: FileOptions); + /** + * The **`name`** read-only property of the File interface returns the name of the file represented by a File object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) + */ + get name(): string; + /** + * The **`lastModified`** read-only property of the File interface provides the last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) + */ + get lastModified(): number; +} +interface FileOptions { + type?: string; + lastModified?: number; +} +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare abstract class CacheStorage { + /** + * The **`open()`** method of the the Cache object matching the `cacheName`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CacheStorage/open) + */ + open(cacheName: string): Promise; + readonly default: Cache; +} +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare abstract class Cache { + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#delete) */ + delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#match) */ + match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#put) */ + put(request: RequestInfo | URL, response: Response): Promise; +} +interface CacheQueryOptions { + ignoreMethod?: boolean; +} +/** +* The Web Crypto API provides a set of low-level functions for common cryptographic tasks. +* The Workers runtime implements the full surface of this API, but with some differences in +* the [supported algorithms](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/#supported-algorithms) +* compared to those implemented in most browsers. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/) +*/ +declare abstract class Crypto { + /** + * The **`Crypto.subtle`** read-only property returns a cryptographic operations. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/subtle) + */ + get subtle(): SubtleCrypto; + /** + * The **`Crypto.getRandomValues()`** method lets you get cryptographically strong random values. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) + */ + getRandomValues(buffer: T): T; + /** + * The **`randomUUID()`** method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID) + */ + randomUUID(): string; + DigestStream: typeof DigestStream; +} +/** + * The **`SubtleCrypto`** interface of the Web Crypto API provides a number of low-level cryptographic functions. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto) + */ +declare abstract class SubtleCrypto { + /** + * The **`encrypt()`** method of the SubtleCrypto interface encrypts data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/encrypt) + */ + encrypt(algorithm: string | SubtleCryptoEncryptAlgorithm, key: CryptoKey, plainText: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`decrypt()`** method of the SubtleCrypto interface decrypts some encrypted data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/decrypt) + */ + decrypt(algorithm: string | SubtleCryptoEncryptAlgorithm, key: CryptoKey, cipherText: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`sign()`** method of the SubtleCrypto interface generates a digital signature. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/sign) + */ + sign(algorithm: string | SubtleCryptoSignAlgorithm, key: CryptoKey, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`verify()`** method of the SubtleCrypto interface verifies a digital signature. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/verify) + */ + verify(algorithm: string | SubtleCryptoSignAlgorithm, key: CryptoKey, signature: ArrayBuffer | ArrayBufferView, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`digest()`** method of the SubtleCrypto interface generates a _digest_ of the given data, using the specified hash function. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/digest) + */ + digest(algorithm: string | SubtleCryptoHashAlgorithm, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`generateKey()`** method of the SubtleCrypto interface is used to generate a new key (for symmetric algorithms) or key pair (for public-key algorithms). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) + */ + generateKey(algorithm: string | SubtleCryptoGenerateKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`deriveKey()`** method of the SubtleCrypto interface can be used to derive a secret key from a master key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveKey) + */ + deriveKey(algorithm: string | SubtleCryptoDeriveKeyAlgorithm, baseKey: CryptoKey, derivedKeyAlgorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`deriveBits()`** method of the key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveBits) + */ + deriveBits(algorithm: string | SubtleCryptoDeriveKeyAlgorithm, baseKey: CryptoKey, length?: number | null): Promise; + /** + * The **`importKey()`** method of the SubtleCrypto interface imports a key: that is, it takes as input a key in an external, portable format and gives you a CryptoKey object that you can use in the Web Crypto API. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/importKey) + */ + importKey(format: string, keyData: (ArrayBuffer | ArrayBufferView) | JsonWebKey, algorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`exportKey()`** method of the SubtleCrypto interface exports a key: that is, it takes as input a CryptoKey object and gives you the key in an external, portable format. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) + */ + exportKey(format: string, key: CryptoKey): Promise; + /** + * The **`wrapKey()`** method of the SubtleCrypto interface 'wraps' a key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/wrapKey) + */ + wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: string | SubtleCryptoEncryptAlgorithm): Promise; + /** + * The **`unwrapKey()`** method of the SubtleCrypto interface 'unwraps' a key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/unwrapKey) + */ + unwrapKey(format: string, wrappedKey: ArrayBuffer | ArrayBufferView, unwrappingKey: CryptoKey, unwrapAlgorithm: string | SubtleCryptoEncryptAlgorithm, unwrappedKeyAlgorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + timingSafeEqual(a: ArrayBuffer | ArrayBufferView, b: ArrayBuffer | ArrayBufferView): boolean; +} +/** + * The **`CryptoKey`** interface of the Web Crypto API represents a cryptographic key obtained from one of the SubtleCrypto methods SubtleCrypto.generateKey, SubtleCrypto.deriveKey, SubtleCrypto.importKey, or SubtleCrypto.unwrapKey. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey) + */ +declare abstract class CryptoKey { + /** + * The read-only **`type`** property of the CryptoKey interface indicates which kind of key is represented by the object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/type) + */ + readonly type: string; + /** + * The read-only **`extractable`** property of the CryptoKey interface indicates whether or not the key may be extracted using `SubtleCrypto.exportKey()` or `SubtleCrypto.wrapKey()`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/extractable) + */ + readonly extractable: boolean; + /** + * The read-only **`algorithm`** property of the CryptoKey interface returns an object describing the algorithm for which this key can be used, and any associated extra parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/algorithm) + */ + readonly algorithm: CryptoKeyKeyAlgorithm | CryptoKeyAesKeyAlgorithm | CryptoKeyHmacKeyAlgorithm | CryptoKeyRsaKeyAlgorithm | CryptoKeyEllipticKeyAlgorithm | CryptoKeyArbitraryKeyAlgorithm; + /** + * The read-only **`usages`** property of the CryptoKey interface indicates what can be done with the key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/usages) + */ + readonly usages: string[]; +} +interface CryptoKeyPair { + publicKey: CryptoKey; + privateKey: CryptoKey; +} +interface JsonWebKey { + kty: string; + use?: string; + key_ops?: string[]; + alg?: string; + ext?: boolean; + crv?: string; + x?: string; + y?: string; + d?: string; + n?: string; + e?: string; + p?: string; + q?: string; + dp?: string; + dq?: string; + qi?: string; + oth?: RsaOtherPrimesInfo[]; + k?: string; +} +interface RsaOtherPrimesInfo { + r?: string; + d?: string; + t?: string; +} +interface SubtleCryptoDeriveKeyAlgorithm { + name: string; + salt?: (ArrayBuffer | ArrayBufferView); + iterations?: number; + hash?: (string | SubtleCryptoHashAlgorithm); + $public?: CryptoKey; + info?: (ArrayBuffer | ArrayBufferView); +} +interface SubtleCryptoEncryptAlgorithm { + name: string; + iv?: (ArrayBuffer | ArrayBufferView); + additionalData?: (ArrayBuffer | ArrayBufferView); + tagLength?: number; + counter?: (ArrayBuffer | ArrayBufferView); + length?: number; + label?: (ArrayBuffer | ArrayBufferView); +} +interface SubtleCryptoGenerateKeyAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + modulusLength?: number; + publicExponent?: (ArrayBuffer | ArrayBufferView); + length?: number; + namedCurve?: string; +} +interface SubtleCryptoHashAlgorithm { + name: string; +} +interface SubtleCryptoImportKeyAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + length?: number; + namedCurve?: string; + compressed?: boolean; +} +interface SubtleCryptoSignAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + dataLength?: number; + saltLength?: number; +} +interface CryptoKeyKeyAlgorithm { + name: string; +} +interface CryptoKeyAesKeyAlgorithm { + name: string; + length: number; +} +interface CryptoKeyHmacKeyAlgorithm { + name: string; + hash: CryptoKeyKeyAlgorithm; + length: number; +} +interface CryptoKeyRsaKeyAlgorithm { + name: string; + modulusLength: number; + publicExponent: ArrayBuffer | ArrayBufferView; + hash?: CryptoKeyKeyAlgorithm; +} +interface CryptoKeyEllipticKeyAlgorithm { + name: string; + namedCurve: string; +} +interface CryptoKeyArbitraryKeyAlgorithm { + name: string; + hash?: CryptoKeyKeyAlgorithm; + namedCurve?: string; + length?: number; +} +declare class DigestStream extends WritableStream { + constructor(algorithm: string | SubtleCryptoHashAlgorithm); + readonly digest: Promise; + get bytesWritten(): number | bigint; +} +/** + * The **`TextDecoder`** interface represents a decoder for a specific text encoding, such as `UTF-8`, `ISO-8859-2`, `KOI8-R`, `GBK`, etc. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder) + */ +declare class TextDecoder { + constructor(label?: string, options?: TextDecoderConstructorOptions); + /** + * The **`TextDecoder.decode()`** method returns a string containing text decoded from the buffer passed as a parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/decode) + */ + decode(input?: (ArrayBuffer | ArrayBufferView), options?: TextDecoderDecodeOptions): string; + get encoding(): string; + get fatal(): boolean; + get ignoreBOM(): boolean; +} +/** + * The **`TextEncoder`** interface takes a stream of code points as input and emits a stream of UTF-8 bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder) + */ +declare class TextEncoder { + constructor(); + /** + * The **`TextEncoder.encode()`** method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encode) + */ + encode(input?: string): Uint8Array; + /** + * The **`TextEncoder.encodeInto()`** method takes a string to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto) + */ + encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult; + get encoding(): string; +} +interface TextDecoderConstructorOptions { + fatal: boolean; + ignoreBOM: boolean; +} +interface TextDecoderDecodeOptions { + stream: boolean; +} +interface TextEncoderEncodeIntoResult { + read: number; + written: number; +} +/** + * The **`ErrorEvent`** interface represents events providing information related to errors in scripts or in files. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent) + */ +declare class ErrorEvent extends Event { + constructor(type: string, init?: ErrorEventErrorEventInit); + /** + * The **`filename`** read-only property of the ErrorEvent interface returns a string containing the name of the script file in which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/filename) + */ + get filename(): string; + /** + * The **`message`** read-only property of the ErrorEvent interface returns a string containing a human-readable error message describing the problem. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/message) + */ + get message(): string; + /** + * The **`lineno`** read-only property of the ErrorEvent interface returns an integer containing the line number of the script file on which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/lineno) + */ + get lineno(): number; + /** + * The **`colno`** read-only property of the ErrorEvent interface returns an integer containing the column number of the script file on which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/colno) + */ + get colno(): number; + /** + * The **`error`** read-only property of the ErrorEvent interface returns a JavaScript value, such as an Error or DOMException, representing the error associated with this event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/error) + */ + get error(): any; +} +interface ErrorEventErrorEventInit { + message?: string; + filename?: string; + lineno?: number; + colno?: number; + error?: any; +} +/** + * The **`MessageEvent`** interface represents a message received by a target object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent) + */ +declare class MessageEvent extends Event { + constructor(type: string, initializer: MessageEventInit); + /** + * The **`data`** read-only property of the The data sent by the message emitter; this can be any data type, depending on what originated this event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/data) + */ + readonly data: any; + /** + * The **`origin`** read-only property of the origin of the message emitter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/origin) + */ + readonly origin: string | null; + /** + * The **`lastEventId`** read-only property of the unique ID for the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/lastEventId) + */ + readonly lastEventId: string; + /** + * The **`source`** read-only property of the a WindowProxy, MessagePort, or a `MessageEventSource` (which can be a WindowProxy, message emitter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/source) + */ + readonly source: MessagePort | null; + /** + * The **`ports`** read-only property of the containing all MessagePort objects sent with the message, in order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/ports) + */ + readonly ports: MessagePort[]; +} +interface MessageEventInit { + data: ArrayBuffer | string; +} +/** + * The **`PromiseRejectionEvent`** interface represents events which are sent to the global script context when JavaScript Promises are rejected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent) + */ +declare abstract class PromiseRejectionEvent extends Event { + /** + * The PromiseRejectionEvent interface's **`promise`** read-only property indicates the JavaScript rejected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent/promise) + */ + readonly promise: Promise; + /** + * The PromiseRejectionEvent **`reason`** read-only property is any JavaScript value or Object which provides the reason passed into Promise.reject(). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent/reason) + */ + readonly reason: any; +} +/** + * The **`FormData`** interface provides a way to construct a set of key/value pairs representing form fields and their values, which can be sent using the Window/fetch, XMLHttpRequest.send() or navigator.sendBeacon() methods. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData) + */ +declare class FormData { + constructor(); + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: string | Blob): void; + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: string): void; + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: Blob, filename?: string): void; + /** + * The **`delete()`** method of the FormData interface deletes a key and its value(s) from a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/delete) + */ + delete(name: string): void; + /** + * The **`get()`** method of the FormData interface returns the first value associated with a given key from within a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/get) + */ + get(name: string): (File | string) | null; + /** + * The **`getAll()`** method of the FormData interface returns all the values associated with a given key from within a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/getAll) + */ + getAll(name: string): (File | string)[]; + /** + * The **`has()`** method of the FormData interface returns whether a `FormData` object contains a certain key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/has) + */ + has(name: string): boolean; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: string | Blob): void; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: string): void; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: Blob, filename?: string): void; + /* Returns an array of key, value pairs for every entry in the list. */ + entries(): IterableIterator<[ + key: string, + value: File | string + ]>; + /* Returns a list of keys in the list. */ + keys(): IterableIterator; + /* Returns a list of values in the list. */ + values(): IterableIterator<(File | string)>; + forEach(callback: (this: This, value: File | string, key: string, parent: FormData) => void, thisArg?: This): void; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: File | string + ]>; +} +interface ContentOptions { + html?: boolean; +} +declare class HTMLRewriter { + constructor(); + on(selector: string, handlers: HTMLRewriterElementContentHandlers): HTMLRewriter; + onDocument(handlers: HTMLRewriterDocumentContentHandlers): HTMLRewriter; + transform(response: Response): Response; +} +interface HTMLRewriterElementContentHandlers { + element?(element: Element): void | Promise; + comments?(comment: Comment): void | Promise; + text?(element: Text): void | Promise; +} +interface HTMLRewriterDocumentContentHandlers { + doctype?(doctype: Doctype): void | Promise; + comments?(comment: Comment): void | Promise; + text?(text: Text): void | Promise; + end?(end: DocumentEnd): void | Promise; +} +interface Doctype { + readonly name: string | null; + readonly publicId: string | null; + readonly systemId: string | null; +} +interface Element { + tagName: string; + readonly attributes: IterableIterator; + readonly removed: boolean; + readonly namespaceURI: string; + getAttribute(name: string): string | null; + hasAttribute(name: string): boolean; + setAttribute(name: string, value: string): Element; + removeAttribute(name: string): Element; + before(content: string | ReadableStream | Response, options?: ContentOptions): Element; + after(content: string | ReadableStream | Response, options?: ContentOptions): Element; + prepend(content: string | ReadableStream | Response, options?: ContentOptions): Element; + append(content: string | ReadableStream | Response, options?: ContentOptions): Element; + replace(content: string | ReadableStream | Response, options?: ContentOptions): Element; + remove(): Element; + removeAndKeepContent(): Element; + setInnerContent(content: string | ReadableStream | Response, options?: ContentOptions): Element; + onEndTag(handler: (tag: EndTag) => void | Promise): void; +} +interface EndTag { + name: string; + before(content: string | ReadableStream | Response, options?: ContentOptions): EndTag; + after(content: string | ReadableStream | Response, options?: ContentOptions): EndTag; + remove(): EndTag; +} +interface Comment { + text: string; + readonly removed: boolean; + before(content: string, options?: ContentOptions): Comment; + after(content: string, options?: ContentOptions): Comment; + replace(content: string, options?: ContentOptions): Comment; + remove(): Comment; +} +interface Text { + readonly text: string; + readonly lastInTextNode: boolean; + readonly removed: boolean; + before(content: string | ReadableStream | Response, options?: ContentOptions): Text; + after(content: string | ReadableStream | Response, options?: ContentOptions): Text; + replace(content: string | ReadableStream | Response, options?: ContentOptions): Text; + remove(): Text; +} +interface DocumentEnd { + append(content: string, options?: ContentOptions): DocumentEnd; +} +/** + * This is the event type for `fetch` events dispatched on the ServiceWorkerGlobalScope. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent) + */ +declare abstract class FetchEvent extends ExtendableEvent { + /** + * The **`request`** read-only property of the the event handler. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent/request) + */ + readonly request: Request; + /** + * The **`respondWith()`** method of allows you to provide a promise for a Response yourself. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent/respondWith) + */ + respondWith(promise: Response | Promise): void; + passThroughOnException(): void; +} +type HeadersInit = Headers | Iterable> | Record; +/** + * The **`Headers`** interface of the Fetch API allows you to perform various actions on HTTP request and response headers. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers) + */ +declare class Headers { + constructor(init?: HeadersInit); + /** + * The **`get()`** method of the Headers interface returns a byte string of all the values of a header within a `Headers` object with a given name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/get) + */ + get(name: string): string | null; + getAll(name: string): string[]; + /** + * The **`getSetCookie()`** method of the Headers interface returns an array containing the values of all Set-Cookie headers associated with a response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/getSetCookie) + */ + getSetCookie(): string[]; + /** + * The **`has()`** method of the Headers interface returns a boolean stating whether a `Headers` object contains a certain header. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/has) + */ + has(name: string): boolean; + /** + * The **`set()`** method of the Headers interface sets a new value for an existing header inside a `Headers` object, or adds the header if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set) + */ + set(name: string, value: string): void; + /** + * The **`append()`** method of the Headers interface appends a new value onto an existing header inside a `Headers` object, or adds the header if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/append) + */ + append(name: string, value: string): void; + /** + * The **`delete()`** method of the Headers interface deletes a header from the current `Headers` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/delete) + */ + delete(name: string): void; + forEach(callback: (this: This, value: string, key: string, parent: Headers) => void, thisArg?: This): void; + /* Returns an iterator allowing to go through all key/value pairs contained in this object. */ + entries(): IterableIterator<[ + key: string, + value: string + ]>; + /* Returns an iterator allowing to go through all keys of the key/value pairs contained in this object. */ + keys(): IterableIterator; + /* Returns an iterator allowing to go through all values of the key/value pairs contained in this object. */ + values(): IterableIterator; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: string + ]>; +} +type BodyInit = ReadableStream | string | ArrayBuffer | ArrayBufferView | Blob | URLSearchParams | FormData | Iterable | AsyncIterable; +declare abstract class Body { + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/body) */ + get body(): ReadableStream | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bodyUsed) */ + get bodyUsed(): boolean; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/arrayBuffer) */ + arrayBuffer(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes) */ + bytes(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/text) */ + text(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */ + json(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/formData) */ + formData(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */ + blob(): Promise; +} +/** + * The **`Response`** interface of the Fetch API represents the response to a request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response) + */ +declare var Response: { + prototype: Response; + new (body?: BodyInit | null, init?: ResponseInit): Response; + error(): Response; + redirect(url: string, status?: number): Response; + json(any: any, maybeInit?: (ResponseInit | Response)): Response; +}; +/** + * The **`Response`** interface of the Fetch API represents the response to a request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response) + */ +interface Response extends Body { + /** + * The **`clone()`** method of the Response interface creates a clone of a response object, identical in every way, but stored in a different variable. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/clone) + */ + clone(): Response; + /** + * The **`status`** read-only property of the Response interface contains the HTTP status codes of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/status) + */ + status: number; + /** + * The **`statusText`** read-only property of the Response interface contains the status message corresponding to the HTTP status code in Response.status. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/statusText) + */ + statusText: string; + /** + * The **`headers`** read-only property of the with the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/headers) + */ + headers: Headers; + /** + * The **`ok`** read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/ok) + */ + ok: boolean; + /** + * The **`redirected`** read-only property of the Response interface indicates whether or not the response is the result of a request you made which was redirected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/redirected) + */ + redirected: boolean; + /** + * The **`url`** read-only property of the Response interface contains the URL of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/url) + */ + url: string; + webSocket: WebSocket | null; + cf: any | undefined; + /** + * The **`type`** read-only property of the Response interface contains the type of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/type) + */ + type: "default" | "error"; +} +interface ResponseInit { + status?: number; + statusText?: string; + headers?: HeadersInit; + cf?: any; + webSocket?: (WebSocket | null); + encodeBody?: "automatic" | "manual"; +} +type RequestInfo> = Request | string; +/** + * The **`Request`** interface of the Fetch API represents a resource request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request) + */ +declare var Request: { + prototype: Request; + new >(input: RequestInfo | URL, init?: RequestInit): Request; +}; +/** + * The **`Request`** interface of the Fetch API represents a resource request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request) + */ +interface Request> extends Body { + /** + * The **`clone()`** method of the Request interface creates a copy of the current `Request` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/clone) + */ + clone(): Request; + /** + * The **`method`** read-only property of the `POST`, etc.) A String indicating the method of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/method) + */ + method: string; + /** + * The **`url`** read-only property of the Request interface contains the URL of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/url) + */ + url: string; + /** + * The **`headers`** read-only property of the with the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/headers) + */ + headers: Headers; + /** + * The **`redirect`** read-only property of the Request interface contains the mode for how redirects are handled. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/redirect) + */ + redirect: string; + fetcher: Fetcher | null; + /** + * The read-only **`signal`** property of the Request interface returns the AbortSignal associated with the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/signal) + */ + signal: AbortSignal; + cf?: Cf; + /** + * The **`integrity`** read-only property of the Request interface contains the subresource integrity value of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/integrity) + */ + integrity: string; + /** + * The **`keepalive`** read-only property of the Request interface contains the request's `keepalive` setting (`true` or `false`), which indicates whether the browser will keep the associated request alive if the page that initiated it is unloaded before the request is complete. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/keepalive) + */ + keepalive: boolean; + /** + * The **`cache`** read-only property of the Request interface contains the cache mode of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/cache) + */ + cache?: "no-store" | "no-cache"; +} +interface RequestInit { + /* A string to set request's method. */ + method?: string; + /* A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ + headers?: HeadersInit; + /* A BodyInit object or null to set request's body. */ + body?: BodyInit | null; + /* A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ + redirect?: string; + fetcher?: (Fetcher | null); + cf?: Cf; + /* A string indicating how the request will interact with the browser's cache to set request's cache. */ + cache?: "no-store" | "no-cache"; + /* A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ + integrity?: string; + /* An AbortSignal to set request's signal. */ + signal?: (AbortSignal | null); + encodeResponseBody?: "automatic" | "manual"; +} +type Service Rpc.WorkerEntrypointBranded) | Rpc.WorkerEntrypointBranded | ExportedHandler | undefined = undefined> = T extends new (...args: any[]) => Rpc.WorkerEntrypointBranded ? Fetcher> : T extends Rpc.WorkerEntrypointBranded ? Fetcher : T extends Exclude ? never : Fetcher; +type Fetcher = (T extends Rpc.EntrypointBranded ? Rpc.Provider : unknown) & { + fetch(input: RequestInfo | URL, init?: RequestInit): Promise; + connect(address: SocketAddress | string, options?: SocketOptions): Socket; +}; +interface KVNamespaceListKey { + name: Key; + expiration?: number; + metadata?: Metadata; +} +type KVNamespaceListResult = { + list_complete: false; + keys: KVNamespaceListKey[]; + cursor: string; + cacheStatus: string | null; +} | { + list_complete: true; + keys: KVNamespaceListKey[]; + cacheStatus: string | null; +}; +interface KVNamespace { + get(key: Key, options?: Partial>): Promise; + get(key: Key, type: "text"): Promise; + get(key: Key, type: "json"): Promise; + get(key: Key, type: "arrayBuffer"): Promise; + get(key: Key, type: "stream"): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"text">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"json">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"arrayBuffer">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"stream">): Promise; + get(key: Array, type: "text"): Promise>; + get(key: Array, type: "json"): Promise>; + get(key: Array, options?: Partial>): Promise>; + get(key: Array, options?: KVNamespaceGetOptions<"text">): Promise>; + get(key: Array, options?: KVNamespaceGetOptions<"json">): Promise>; + list(options?: KVNamespaceListOptions): Promise>; + put(key: Key, value: string | ArrayBuffer | ArrayBufferView | ReadableStream, options?: KVNamespacePutOptions): Promise; + getWithMetadata(key: Key, options?: Partial>): Promise>; + getWithMetadata(key: Key, type: "text"): Promise>; + getWithMetadata(key: Key, type: "json"): Promise>; + getWithMetadata(key: Key, type: "arrayBuffer"): Promise>; + getWithMetadata(key: Key, type: "stream"): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"text">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"json">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"arrayBuffer">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"stream">): Promise>; + getWithMetadata(key: Array, type: "text"): Promise>>; + getWithMetadata(key: Array, type: "json"): Promise>>; + getWithMetadata(key: Array, options?: Partial>): Promise>>; + getWithMetadata(key: Array, options?: KVNamespaceGetOptions<"text">): Promise>>; + getWithMetadata(key: Array, options?: KVNamespaceGetOptions<"json">): Promise>>; + delete(key: Key): Promise; +} +interface KVNamespaceListOptions { + limit?: number; + prefix?: (string | null); + cursor?: (string | null); +} +interface KVNamespaceGetOptions { + type: Type; + cacheTtl?: number; +} +interface KVNamespacePutOptions { + expiration?: number; + expirationTtl?: number; + metadata?: (any | null); +} +interface KVNamespaceGetWithMetadataResult { + value: Value | null; + metadata: Metadata | null; + cacheStatus: string | null; +} +type QueueContentType = "text" | "bytes" | "json" | "v8"; +interface Queue { + metrics(): Promise; + send(message: Body, options?: QueueSendOptions): Promise; + sendBatch(messages: Iterable>, options?: QueueSendBatchOptions): Promise; +} +interface QueueSendMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface QueueSendMetadata { + metrics: QueueSendMetrics; +} +interface QueueSendResponse { + metadata: QueueSendMetadata; +} +interface QueueSendBatchMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface QueueSendBatchMetadata { + metrics: QueueSendBatchMetrics; +} +interface QueueSendBatchResponse { + metadata: QueueSendBatchMetadata; +} +interface QueueSendOptions { + contentType?: QueueContentType; + delaySeconds?: number; +} +interface QueueSendBatchOptions { + delaySeconds?: number; +} +interface MessageSendRequest { + body: Body; + contentType?: QueueContentType; + delaySeconds?: number; +} +interface QueueMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface MessageBatchMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface MessageBatchMetadata { + metrics: MessageBatchMetrics; +} +interface QueueRetryOptions { + delaySeconds?: number; +} +interface Message { + readonly id: string; + readonly timestamp: Date; + readonly body: Body; + readonly attempts: number; + retry(options?: QueueRetryOptions): void; + ack(): void; +} +interface QueueEvent extends ExtendableEvent { + readonly messages: readonly Message[]; + readonly queue: string; + readonly metadata: MessageBatchMetadata; + retryAll(options?: QueueRetryOptions): void; + ackAll(): void; +} +interface MessageBatch { + readonly messages: readonly Message[]; + readonly queue: string; + readonly metadata: MessageBatchMetadata; + retryAll(options?: QueueRetryOptions): void; + ackAll(): void; +} +interface R2Error extends Error { + readonly name: string; + readonly code: number; + readonly message: string; + readonly action: string; + readonly stack: any; +} +interface R2ListOptions { + limit?: number; + prefix?: string; + cursor?: string; + delimiter?: string; + startAfter?: string; + include?: ("httpMetadata" | "customMetadata")[]; +} +interface R2Bucket { + head(key: string): Promise; + get(key: string, options: R2GetOptions & { + onlyIf: R2Conditional | Headers; + }): Promise; + get(key: string, options?: R2GetOptions): Promise; + put(key: string, value: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob, options?: R2PutOptions & { + onlyIf: R2Conditional | Headers; + }): Promise; + put(key: string, value: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob, options?: R2PutOptions): Promise; + createMultipartUpload(key: string, options?: R2MultipartOptions): Promise; + resumeMultipartUpload(key: string, uploadId: string): R2MultipartUpload; + delete(keys: string | string[]): Promise; + list(options?: R2ListOptions): Promise; +} +interface R2MultipartUpload { + readonly key: string; + readonly uploadId: string; + uploadPart(partNumber: number, value: ReadableStream | (ArrayBuffer | ArrayBufferView) | string | Blob, options?: R2UploadPartOptions): Promise; + abort(): Promise; + complete(uploadedParts: R2UploadedPart[]): Promise; +} +interface R2UploadedPart { + partNumber: number; + etag: string; +} +declare abstract class R2Object { + readonly key: string; + readonly version: string; + readonly size: number; + readonly etag: string; + readonly httpEtag: string; + readonly checksums: R2Checksums; + readonly uploaded: Date; + readonly httpMetadata?: R2HTTPMetadata; + readonly customMetadata?: Record; + readonly range?: R2Range; + readonly storageClass: string; + readonly ssecKeyMd5?: string; + writeHttpMetadata(headers: Headers): void; +} +interface R2ObjectBody extends R2Object { + get body(): ReadableStream; + get bodyUsed(): boolean; + arrayBuffer(): Promise; + bytes(): Promise; + text(): Promise; + json(): Promise; + blob(): Promise; +} +type R2Range = { + offset: number; + length?: number; +} | { + offset?: number; + length: number; +} | { + suffix: number; +}; +interface R2Conditional { + etagMatches?: string; + etagDoesNotMatch?: string; + uploadedBefore?: Date; + uploadedAfter?: Date; + secondsGranularity?: boolean; +} +interface R2GetOptions { + onlyIf?: (R2Conditional | Headers); + range?: (R2Range | Headers); + ssecKey?: (ArrayBuffer | string); +} +interface R2PutOptions { + onlyIf?: (R2Conditional | Headers); + httpMetadata?: (R2HTTPMetadata | Headers); + customMetadata?: Record; + md5?: ((ArrayBuffer | ArrayBufferView) | string); + sha1?: ((ArrayBuffer | ArrayBufferView) | string); + sha256?: ((ArrayBuffer | ArrayBufferView) | string); + sha384?: ((ArrayBuffer | ArrayBufferView) | string); + sha512?: ((ArrayBuffer | ArrayBufferView) | string); + storageClass?: string; + ssecKey?: (ArrayBuffer | string); +} +interface R2MultipartOptions { + httpMetadata?: (R2HTTPMetadata | Headers); + customMetadata?: Record; + storageClass?: string; + ssecKey?: (ArrayBuffer | string); +} +interface R2Checksums { + readonly md5?: ArrayBuffer; + readonly sha1?: ArrayBuffer; + readonly sha256?: ArrayBuffer; + readonly sha384?: ArrayBuffer; + readonly sha512?: ArrayBuffer; + toJSON(): R2StringChecksums; +} +interface R2StringChecksums { + md5?: string; + sha1?: string; + sha256?: string; + sha384?: string; + sha512?: string; +} +interface R2HTTPMetadata { + contentType?: string; + contentLanguage?: string; + contentDisposition?: string; + contentEncoding?: string; + cacheControl?: string; + cacheExpiry?: Date; +} +type R2Objects = { + objects: R2Object[]; + delimitedPrefixes: string[]; +} & ({ + truncated: true; + cursor: string; +} | { + truncated: false; +}); +interface R2UploadPartOptions { + ssecKey?: (ArrayBuffer | string); +} +declare abstract class ScheduledEvent extends ExtendableEvent { + readonly scheduledTime: number; + readonly cron: string; + noRetry(): void; +} +interface ScheduledController { + readonly scheduledTime: number; + readonly cron: string; + noRetry(): void; +} +interface QueuingStrategy { + highWaterMark?: (number | bigint); + size?: (chunk: T) => number | bigint; +} +interface UnderlyingSink { + type?: string; + start?: (controller: WritableStreamDefaultController) => void | Promise; + write?: (chunk: W, controller: WritableStreamDefaultController) => void | Promise; + abort?: (reason: any) => void | Promise; + close?: () => void | Promise; +} +interface UnderlyingByteSource { + type: "bytes"; + autoAllocateChunkSize?: number; + start?: (controller: ReadableByteStreamController) => void | Promise; + pull?: (controller: ReadableByteStreamController) => void | Promise; + cancel?: (reason: any) => void | Promise; +} +interface UnderlyingSource { + type?: "" | undefined; + start?: (controller: ReadableStreamDefaultController) => void | Promise; + pull?: (controller: ReadableStreamDefaultController) => void | Promise; + cancel?: (reason: any) => void | Promise; + expectedLength?: (number | bigint); +} +interface Transformer { + readableType?: string; + writableType?: string; + start?: (controller: TransformStreamDefaultController) => void | Promise; + transform?: (chunk: I, controller: TransformStreamDefaultController) => void | Promise; + flush?: (controller: TransformStreamDefaultController) => void | Promise; + cancel?: (reason: any) => void | Promise; + expectedLength?: number; +} +interface StreamPipeOptions { + preventAbort?: boolean; + preventCancel?: boolean; + /** + * Pipes this readable stream to a given writable stream destination. The way in which the piping process behaves under various error conditions can be customized with a number of passed options. It returns a promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. + * + * Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader. + * + * Errors and closures of the source and destination streams propagate as follows: + * + * An error in this source readable stream will abort destination, unless preventAbort is truthy. The returned promise will be rejected with the source's error, or with any error that occurs during aborting the destination. + * + * An error in destination will cancel this source readable stream, unless preventCancel is truthy. The returned promise will be rejected with the destination's error, or with any error that occurs during canceling the source. + * + * When this source readable stream closes, destination will be closed, unless preventClose is truthy. The returned promise will be fulfilled once this process completes, unless an error is encountered while closing the destination, in which case it will be rejected with that error. + * + * If destination starts out closed or closing, this source readable stream will be canceled, unless preventCancel is true. The returned promise will be rejected with an error indicating piping to a closed stream failed, or with any error that occurs during canceling the source. + * + * The signal option can be set to an AbortSignal to allow aborting an ongoing pipe operation via the corresponding AbortController. In this case, this source readable stream will be canceled, and destination aborted, unless the respective options preventCancel or preventAbort are set. + */ + preventClose?: boolean; + signal?: AbortSignal; +} +type ReadableStreamReadResult = { + done: false; + value: R; +} | { + done: true; + value?: undefined; +}; +/** + * The `ReadableStream` interface of the Streams API represents a readable stream of byte data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream) + */ +interface ReadableStream { + /** + * The **`locked`** read-only property of the ReadableStream interface returns whether or not the readable stream is locked to a reader. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/locked) + */ + get locked(): boolean; + /** + * The **`cancel()`** method of the ReadableStream interface returns a Promise that resolves when the stream is canceled. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/cancel) + */ + cancel(reason?: any): Promise; + /** + * The **`getReader()`** method of the ReadableStream interface creates a reader and locks the stream to it. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader) + */ + getReader(): ReadableStreamDefaultReader; + /** + * The **`getReader()`** method of the ReadableStream interface creates a reader and locks the stream to it. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader) + */ + getReader(options: ReadableStreamGetReaderOptions): ReadableStreamBYOBReader; + /** + * The **`pipeThrough()`** method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeThrough) + */ + pipeThrough(transform: ReadableWritablePair, options?: StreamPipeOptions): ReadableStream; + /** + * The **`pipeTo()`** method of the ReadableStream interface pipes the current `ReadableStream` to a given WritableStream and returns a Promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeTo) + */ + pipeTo(destination: WritableStream, options?: StreamPipeOptions): Promise; + /** + * The **`tee()`** method of the two-element array containing the two resulting branches as new ReadableStream instances. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/tee) + */ + tee(): [ + ReadableStream, + ReadableStream + ]; + values(options?: ReadableStreamValuesOptions): AsyncIterableIterator; + [Symbol.asyncIterator](options?: ReadableStreamValuesOptions): AsyncIterableIterator; +} +/** + * The `ReadableStream` interface of the Streams API represents a readable stream of byte data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream) + */ +declare const ReadableStream: { + prototype: ReadableStream; + new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy): ReadableStream; + new (underlyingSource?: UnderlyingSource, strategy?: QueuingStrategy): ReadableStream; +}; +/** + * The **`ReadableStreamDefaultReader`** interface of the Streams API represents a default reader that can be used to read stream data supplied from a network (such as a fetch request). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader) + */ +declare class ReadableStreamDefaultReader { + constructor(stream: ReadableStream); + get closed(): Promise; + cancel(reason?: any): Promise; + /** + * The **`read()`** method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/read) + */ + read(): Promise>; + /** + * The **`releaseLock()`** method of the ReadableStreamDefaultReader interface releases the reader's lock on the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/releaseLock) + */ + releaseLock(): void; +} +/** + * The `ReadableStreamBYOBReader` interface of the Streams API defines a reader for a ReadableStream that supports zero-copy reading from an underlying byte source. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader) + */ +declare class ReadableStreamBYOBReader { + constructor(stream: ReadableStream); + get closed(): Promise; + cancel(reason?: any): Promise; + /** + * The **`read()`** method of the ReadableStreamBYOBReader interface is used to read data into a view on a user-supplied buffer from an associated readable byte stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/read) + */ + read(view: T): Promise>; + /** + * The **`releaseLock()`** method of the ReadableStreamBYOBReader interface releases the reader's lock on the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/releaseLock) + */ + releaseLock(): void; + readAtLeast(minElements: number, view: T): Promise>; +} +interface ReadableStreamBYOBReaderReadableStreamBYOBReaderReadOptions { + min?: number; +} +interface ReadableStreamGetReaderOptions { + /** + * Creates a ReadableStreamBYOBReader and locks the stream to the new reader. + * + * This call behaves the same way as the no-argument variant, except that it only works on readable byte streams, i.e. streams which were constructed specifically with the ability to handle "bring your own buffer" reading. The returned BYOB reader provides the ability to directly read individual chunks from the stream via its read() method, into developer-supplied buffers, allowing more precise control over allocation. + */ + mode: "byob"; +} +/** + * The **`ReadableStreamBYOBRequest`** interface of the Streams API represents a 'pull request' for data from an underlying source that will made as a zero-copy transfer to a consumer (bypassing the stream's internal queues). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest) + */ +declare abstract class ReadableStreamBYOBRequest { + /** + * The **`view`** getter property of the ReadableStreamBYOBRequest interface returns the current view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/view) + */ + get view(): Uint8Array | null; + /** + * The **`respond()`** method of the ReadableStreamBYOBRequest interface is used to signal to the associated readable byte stream that the specified number of bytes were written into the ReadableStreamBYOBRequest.view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/respond) + */ + respond(bytesWritten: number): void; + /** + * The **`respondWithNewView()`** method of the ReadableStreamBYOBRequest interface specifies a new view that the consumer of the associated readable byte stream should write to instead of ReadableStreamBYOBRequest.view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/respondWithNewView) + */ + respondWithNewView(view: ArrayBuffer | ArrayBufferView): void; + get atLeast(): number | null; +} +/** + * The **`ReadableStreamDefaultController`** interface of the Streams API represents a controller allowing control of a ReadableStream's state and internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController) + */ +declare abstract class ReadableStreamDefaultController { + /** + * The **`desiredSize`** read-only property of the required to fill the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`close()`** method of the ReadableStreamDefaultController interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/close) + */ + close(): void; + /** + * The **`enqueue()`** method of the ```js-nolint enqueue(chunk) ``` - `chunk` - : The chunk to enqueue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/enqueue) + */ + enqueue(chunk?: R): void; + /** + * The **`error()`** method of the with the associated stream to error. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/error) + */ + error(reason: any): void; +} +/** + * The **`ReadableByteStreamController`** interface of the Streams API represents a controller for a readable byte stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController) + */ +declare abstract class ReadableByteStreamController { + /** + * The **`byobRequest`** read-only property of the ReadableByteStreamController interface returns the current BYOB request, or `null` if there are no pending requests. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/byobRequest) + */ + get byobRequest(): ReadableStreamBYOBRequest | null; + /** + * The **`desiredSize`** read-only property of the ReadableByteStreamController interface returns the number of bytes required to fill the stream's internal queue to its 'desired size'. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`close()`** method of the ReadableByteStreamController interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/close) + */ + close(): void; + /** + * The **`enqueue()`** method of the ReadableByteStreamController interface enqueues a given chunk on the associated readable byte stream (the chunk is copied into the stream's internal queues). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/enqueue) + */ + enqueue(chunk: ArrayBuffer | ArrayBufferView): void; + /** + * The **`error()`** method of the ReadableByteStreamController interface causes any future interactions with the associated stream to error with the specified reason. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/error) + */ + error(reason: any): void; +} +/** + * The **`WritableStreamDefaultController`** interface of the Streams API represents a controller allowing control of a WritableStream's state. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController) + */ +declare abstract class WritableStreamDefaultController { + /** + * The read-only **`signal`** property of the WritableStreamDefaultController interface returns the AbortSignal associated with the controller. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController/signal) + */ + get signal(): AbortSignal; + /** + * The **`error()`** method of the with the associated stream to error. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController/error) + */ + error(reason?: any): void; +} +/** + * The **`TransformStreamDefaultController`** interface of the Streams API provides methods to manipulate the associated ReadableStream and WritableStream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController) + */ +declare abstract class TransformStreamDefaultController { + /** + * The **`desiredSize`** read-only property of the TransformStreamDefaultController interface returns the desired size to fill the queue of the associated ReadableStream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`enqueue()`** method of the TransformStreamDefaultController interface enqueues the given chunk in the readable side of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/enqueue) + */ + enqueue(chunk?: O): void; + /** + * The **`error()`** method of the TransformStreamDefaultController interface errors both sides of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/error) + */ + error(reason: any): void; + /** + * The **`terminate()`** method of the TransformStreamDefaultController interface closes the readable side and errors the writable side of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/terminate) + */ + terminate(): void; +} +interface ReadableWritablePair { + readable: ReadableStream; + /** + * Provides a convenient, chainable way of piping this readable stream through a transform stream (or any other { writable, readable } pair). It simply pipes the stream into the writable side of the supplied pair, and returns the readable side for further use. + * + * Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader. + */ + writable: WritableStream; +} +/** + * The **`WritableStream`** interface of the Streams API provides a standard abstraction for writing streaming data to a destination, known as a sink. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream) + */ +declare class WritableStream { + constructor(underlyingSink?: UnderlyingSink, queuingStrategy?: QueuingStrategy); + /** + * The **`locked`** read-only property of the WritableStream interface returns a boolean indicating whether the `WritableStream` is locked to a writer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/locked) + */ + get locked(): boolean; + /** + * The **`abort()`** method of the WritableStream interface aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/abort) + */ + abort(reason?: any): Promise; + /** + * The **`close()`** method of the WritableStream interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/close) + */ + close(): Promise; + /** + * The **`getWriter()`** method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/getWriter) + */ + getWriter(): WritableStreamDefaultWriter; +} +/** + * The **`WritableStreamDefaultWriter`** interface of the Streams API is the object returned by WritableStream.getWriter() and once created locks the writer to the `WritableStream` ensuring that no other streams can write to the underlying sink. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter) + */ +declare class WritableStreamDefaultWriter { + constructor(stream: WritableStream); + /** + * The **`closed`** read-only property of the the stream errors or the writer's lock is released. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/closed) + */ + get closed(): Promise; + /** + * The **`ready`** read-only property of the that resolves when the desired size of the stream's internal queue transitions from non-positive to positive, signaling that it is no longer applying backpressure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/ready) + */ + get ready(): Promise; + /** + * The **`desiredSize`** read-only property of the to fill the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`abort()`** method of the the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/abort) + */ + abort(reason?: any): Promise; + /** + * The **`close()`** method of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/close) + */ + close(): Promise; + /** + * The **`write()`** method of the operation. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/write) + */ + write(chunk?: W): Promise; + /** + * The **`releaseLock()`** method of the corresponding stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/releaseLock) + */ + releaseLock(): void; +} +/** + * The **`TransformStream`** interface of the Streams API represents a concrete implementation of the pipe chain _transform stream_ concept. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream) + */ +declare class TransformStream { + constructor(transformer?: Transformer, writableStrategy?: QueuingStrategy, readableStrategy?: QueuingStrategy); + /** + * The **`readable`** read-only property of the TransformStream interface returns the ReadableStream instance controlled by this `TransformStream`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream/readable) + */ + get readable(): ReadableStream; + /** + * The **`writable`** read-only property of the TransformStream interface returns the WritableStream instance controlled by this `TransformStream`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream/writable) + */ + get writable(): WritableStream; +} +declare class FixedLengthStream extends IdentityTransformStream { + constructor(expectedLength: number | bigint, queuingStrategy?: IdentityTransformStreamQueuingStrategy); +} +declare class IdentityTransformStream extends TransformStream { + constructor(queuingStrategy?: IdentityTransformStreamQueuingStrategy); +} +interface IdentityTransformStreamQueuingStrategy { + highWaterMark?: (number | bigint); +} +interface ReadableStreamValuesOptions { + preventCancel?: boolean; +} +/** + * The **`CompressionStream`** interface of the Compression Streams API is an API for compressing a stream of data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CompressionStream) + */ +declare class CompressionStream extends TransformStream { + constructor(format: "gzip" | "deflate" | "deflate-raw"); +} +/** + * The **`DecompressionStream`** interface of the Compression Streams API is an API for decompressing a stream of data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DecompressionStream) + */ +declare class DecompressionStream extends TransformStream { + constructor(format: "gzip" | "deflate" | "deflate-raw"); +} +/** + * The **`TextEncoderStream`** interface of the Encoding API converts a stream of strings into bytes in the UTF-8 encoding. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoderStream) + */ +declare class TextEncoderStream extends TransformStream { + constructor(); + get encoding(): string; +} +/** + * The **`TextDecoderStream`** interface of the Encoding API converts a stream of text in a binary encoding, such as UTF-8 etc., to a stream of strings. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoderStream) + */ +declare class TextDecoderStream extends TransformStream { + constructor(label?: string, options?: TextDecoderStreamTextDecoderStreamInit); + get encoding(): string; + get fatal(): boolean; + get ignoreBOM(): boolean; +} +interface TextDecoderStreamTextDecoderStreamInit { + fatal?: boolean; + ignoreBOM?: boolean; +} +/** + * The **`ByteLengthQueuingStrategy`** interface of the Streams API provides a built-in byte length queuing strategy that can be used when constructing streams. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy) + */ +declare class ByteLengthQueuingStrategy implements QueuingStrategy { + constructor(init: QueuingStrategyInit); + /** + * The read-only **`ByteLengthQueuingStrategy.highWaterMark`** property returns the total number of bytes that can be contained in the internal queue before backpressure is applied. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy/highWaterMark) + */ + get highWaterMark(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy/size) */ + get size(): (chunk?: any) => number; +} +/** + * The **`CountQueuingStrategy`** interface of the Streams API provides a built-in chunk counting queuing strategy that can be used when constructing streams. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy) + */ +declare class CountQueuingStrategy implements QueuingStrategy { + constructor(init: QueuingStrategyInit); + /** + * The read-only **`CountQueuingStrategy.highWaterMark`** property returns the total number of chunks that can be contained in the internal queue before backpressure is applied. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy/highWaterMark) + */ + get highWaterMark(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy/size) */ + get size(): (chunk?: any) => number; +} +interface QueuingStrategyInit { + /** + * Creates a new ByteLengthQueuingStrategy with the provided high water mark. + * + * Note that the provided high water mark will not be validated ahead of time. Instead, if it is negative, NaN, or not a number, the resulting ByteLengthQueuingStrategy will cause the corresponding stream constructor to throw. + */ + highWaterMark: number; +} +interface TracePreviewInfo { + id: string; + slug: string; + name: string; +} +interface ScriptVersion { + id?: string; + tag?: string; + message?: string; +} +declare abstract class TailEvent extends ExtendableEvent { + readonly events: TraceItem[]; + readonly traces: TraceItem[]; +} +interface TraceItem { + readonly event: (TraceItemFetchEventInfo | TraceItemJsRpcEventInfo | TraceItemConnectEventInfo | TraceItemScheduledEventInfo | TraceItemAlarmEventInfo | TraceItemQueueEventInfo | TraceItemEmailEventInfo | TraceItemTailEventInfo | TraceItemCustomEventInfo | TraceItemHibernatableWebSocketEventInfo) | null; + readonly eventTimestamp: number | null; + readonly logs: TraceLog[]; + readonly exceptions: TraceException[]; + readonly diagnosticsChannelEvents: TraceDiagnosticChannelEvent[]; + readonly scriptName: string | null; + readonly entrypoint?: string; + readonly scriptVersion?: ScriptVersion; + readonly dispatchNamespace?: string; + readonly scriptTags?: string[]; + readonly tailAttributes?: Record; + readonly preview?: TracePreviewInfo; + readonly durableObjectId?: string; + readonly outcome: string; + readonly executionModel: string; + readonly truncated: boolean; + readonly cpuTime: number; + readonly wallTime: number; +} +interface TraceItemAlarmEventInfo { + readonly scheduledTime: Date; +} +interface TraceItemConnectEventInfo { +} +interface TraceItemCustomEventInfo { +} +interface TraceItemScheduledEventInfo { + readonly scheduledTime: number; + readonly cron: string; +} +interface TraceItemQueueEventInfo { + readonly queue: string; + readonly batchSize: number; +} +interface TraceItemEmailEventInfo { + readonly mailFrom: string; + readonly rcptTo: string; + readonly rawSize: number; +} +interface TraceItemTailEventInfo { + readonly consumedEvents: TraceItemTailEventInfoTailItem[]; +} +interface TraceItemTailEventInfoTailItem { + readonly scriptName: string | null; +} +interface TraceItemFetchEventInfo { + readonly response?: TraceItemFetchEventInfoResponse; + readonly request: TraceItemFetchEventInfoRequest; +} +interface TraceItemFetchEventInfoRequest { + readonly cf?: any; + readonly headers: Record; + readonly method: string; + readonly url: string; + getUnredacted(): TraceItemFetchEventInfoRequest; +} +interface TraceItemFetchEventInfoResponse { + readonly status: number; +} +interface TraceItemJsRpcEventInfo { + readonly rpcMethod: string; +} +interface TraceItemHibernatableWebSocketEventInfo { + readonly getWebSocketEvent: TraceItemHibernatableWebSocketEventInfoMessage | TraceItemHibernatableWebSocketEventInfoClose | TraceItemHibernatableWebSocketEventInfoError; +} +interface TraceItemHibernatableWebSocketEventInfoMessage { + readonly webSocketEventType: string; +} +interface TraceItemHibernatableWebSocketEventInfoClose { + readonly webSocketEventType: string; + readonly code: number; + readonly wasClean: boolean; +} +interface TraceItemHibernatableWebSocketEventInfoError { + readonly webSocketEventType: string; +} +interface TraceLog { + readonly timestamp: number; + readonly level: string; + readonly message: any; +} +interface TraceException { + readonly timestamp: number; + readonly message: string; + readonly name: string; + readonly stack?: string; +} +interface TraceDiagnosticChannelEvent { + readonly timestamp: number; + readonly channel: string; + readonly message: any; +} +interface TraceMetrics { + readonly cpuTime: number; + readonly wallTime: number; +} +interface UnsafeTraceMetrics { + fromTrace(item: TraceItem): TraceMetrics; +} +/** + * The **`URL`** interface is used to parse, construct, normalize, and encode URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL) + */ +declare class URL { + constructor(url: string | URL, base?: string | URL); + /** + * The **`origin`** read-only property of the URL interface returns a string containing the Unicode serialization of the origin of the represented URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/origin) + */ + get origin(): string; + /** + * The **`href`** property of the URL interface is a string containing the whole URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/href) + */ + get href(): string; + /** + * The **`href`** property of the URL interface is a string containing the whole URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/href) + */ + set href(value: string); + /** + * The **`protocol`** property of the URL interface is a string containing the protocol or scheme of the URL, including the final `':'`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/protocol) + */ + get protocol(): string; + /** + * The **`protocol`** property of the URL interface is a string containing the protocol or scheme of the URL, including the final `':'`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/protocol) + */ + set protocol(value: string); + /** + * The **`username`** property of the URL interface is a string containing the username component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/username) + */ + get username(): string; + /** + * The **`username`** property of the URL interface is a string containing the username component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/username) + */ + set username(value: string); + /** + * The **`password`** property of the URL interface is a string containing the password component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/password) + */ + get password(): string; + /** + * The **`password`** property of the URL interface is a string containing the password component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/password) + */ + set password(value: string); + /** + * The **`host`** property of the URL interface is a string containing the host, which is the URL.hostname, and then, if the port of the URL is nonempty, a `':'`, followed by the URL.port of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/host) + */ + get host(): string; + /** + * The **`host`** property of the URL interface is a string containing the host, which is the URL.hostname, and then, if the port of the URL is nonempty, a `':'`, followed by the URL.port of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/host) + */ + set host(value: string); + /** + * The **`hostname`** property of the URL interface is a string containing either the domain name or IP address of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hostname) + */ + get hostname(): string; + /** + * The **`hostname`** property of the URL interface is a string containing either the domain name or IP address of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hostname) + */ + set hostname(value: string); + /** + * The **`port`** property of the URL interface is a string containing the port number of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/port) + */ + get port(): string; + /** + * The **`port`** property of the URL interface is a string containing the port number of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/port) + */ + set port(value: string); + /** + * The **`pathname`** property of the URL interface represents a location in a hierarchical structure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/pathname) + */ + get pathname(): string; + /** + * The **`pathname`** property of the URL interface represents a location in a hierarchical structure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/pathname) + */ + set pathname(value: string); + /** + * The **`search`** property of the URL interface is a search string, also called a _query string_, that is a string containing a `'?'` followed by the parameters of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/search) + */ + get search(): string; + /** + * The **`search`** property of the URL interface is a search string, also called a _query string_, that is a string containing a `'?'` followed by the parameters of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/search) + */ + set search(value: string); + /** + * The **`hash`** property of the URL interface is a string containing a `'#'` followed by the fragment identifier of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hash) + */ + get hash(): string; + /** + * The **`hash`** property of the URL interface is a string containing a `'#'` followed by the fragment identifier of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hash) + */ + set hash(value: string); + /** + * The **`searchParams`** read-only property of the access to the [MISSING: httpmethod('GET')] decoded query arguments contained in the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/searchParams) + */ + get searchParams(): URLSearchParams; + /** + * The **`toJSON()`** method of the URL interface returns a string containing a serialized version of the URL, although in practice it seems to have the same effect as ```js-nolint toJSON() ``` None. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/toJSON) + */ + toJSON(): string; + /*function toString() { [native code] }*/ + toString(): string; + /** + * The **`URL.canParse()`** static method of the URL interface returns a boolean indicating whether or not an absolute URL, or a relative URL combined with a base URL, are parsable and valid. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/canParse_static) + */ + static canParse(url: string, base?: string): boolean; + /** + * The **`URL.parse()`** static method of the URL interface returns a newly created URL object representing the URL defined by the parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/parse_static) + */ + static parse(url: string, base?: string): URL | null; + /** + * The **`createObjectURL()`** static method of the URL interface creates a string containing a URL representing the object given in the parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/createObjectURL_static) + */ + static createObjectURL(object: File | Blob): string; + /** + * The **`revokeObjectURL()`** static method of the URL interface releases an existing object URL which was previously created by calling Call this method when you've finished using an object URL to let the browser know not to keep the reference to the file any longer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static) + */ + static revokeObjectURL(object_url: string): void; +} +/** + * The **`URLSearchParams`** interface defines utility methods to work with the query string of a URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams) + */ +declare class URLSearchParams { + constructor(init?: (Iterable> | Record | string)); + /** + * The **`size`** read-only property of the URLSearchParams interface indicates the total number of search parameter entries. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/size) + */ + get size(): number; + /** + * The **`append()`** method of the URLSearchParams interface appends a specified key/value pair as a new search parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/append) + */ + append(name: string, value: string): void; + /** + * The **`delete()`** method of the URLSearchParams interface deletes specified parameters and their associated value(s) from the list of all search parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete) + */ + delete(name: string, value?: string): void; + /** + * The **`get()`** method of the URLSearchParams interface returns the first value associated to the given search parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get) + */ + get(name: string): string | null; + /** + * The **`getAll()`** method of the URLSearchParams interface returns all the values associated with a given search parameter as an array. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll) + */ + getAll(name: string): string[]; + /** + * The **`has()`** method of the URLSearchParams interface returns a boolean value that indicates whether the specified parameter is in the search parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/has) + */ + has(name: string, value?: string): boolean; + /** + * The **`set()`** method of the URLSearchParams interface sets the value associated with a given search parameter to the given value. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/set) + */ + set(name: string, value: string): void; + /** + * The **`URLSearchParams.sort()`** method sorts all key/value pairs contained in this object in place and returns `undefined`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/sort) + */ + sort(): void; + /* Returns an array of key, value pairs for every entry in the search params. */ + entries(): IterableIterator<[ + key: string, + value: string + ]>; + /* Returns a list of keys in the search params. */ + keys(): IterableIterator; + /* Returns a list of values in the search params. */ + values(): IterableIterator; + forEach(callback: (this: This, value: string, key: string, parent: URLSearchParams) => void, thisArg?: This): void; + /*function toString() { [native code] }*/ + toString(): string; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: string + ]>; +} +declare class URLPattern { + constructor(input?: (string | URLPatternInit), baseURL?: (string | URLPatternOptions), patternOptions?: URLPatternOptions); + get protocol(): string; + get username(): string; + get password(): string; + get hostname(): string; + get port(): string; + get pathname(): string; + get search(): string; + get hash(): string; + get hasRegExpGroups(): boolean; + test(input?: (string | URLPatternInit), baseURL?: string): boolean; + exec(input?: (string | URLPatternInit), baseURL?: string): URLPatternResult | null; +} +interface URLPatternInit { + protocol?: string; + username?: string; + password?: string; + hostname?: string; + port?: string; + pathname?: string; + search?: string; + hash?: string; + baseURL?: string; +} +interface URLPatternComponentResult { + input: string; + groups: Record; +} +interface URLPatternResult { + inputs: (string | URLPatternInit)[]; + protocol: URLPatternComponentResult; + username: URLPatternComponentResult; + password: URLPatternComponentResult; + hostname: URLPatternComponentResult; + port: URLPatternComponentResult; + pathname: URLPatternComponentResult; + search: URLPatternComponentResult; + hash: URLPatternComponentResult; +} +interface URLPatternOptions { + ignoreCase?: boolean; +} +/** + * A `CloseEvent` is sent to clients using WebSockets when the connection is closed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent) + */ +declare class CloseEvent extends Event { + constructor(type: string, initializer?: CloseEventInit); + /** + * The **`code`** read-only property of the CloseEvent interface returns a WebSocket connection close code indicating the reason the connection was closed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/code) + */ + readonly code: number; + /** + * The **`reason`** read-only property of the CloseEvent interface returns the WebSocket connection close reason the server gave for closing the connection; that is, a concise human-readable prose explanation for the closure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/reason) + */ + readonly reason: string; + /** + * The **`wasClean`** read-only property of the CloseEvent interface returns `true` if the connection closed cleanly. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/wasClean) + */ + readonly wasClean: boolean; +} +interface CloseEventInit { + code?: number; + reason?: string; + wasClean?: boolean; +} +type WebSocketEventMap = { + close: CloseEvent; + message: MessageEvent; + open: Event; + error: ErrorEvent; +}; +/** + * The `WebSocket` object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket) + */ +declare var WebSocket: { + prototype: WebSocket; + new (url: string, protocols?: (string[] | string)): WebSocket; + readonly READY_STATE_CONNECTING: number; + readonly CONNECTING: number; + readonly READY_STATE_OPEN: number; + readonly OPEN: number; + readonly READY_STATE_CLOSING: number; + readonly CLOSING: number; + readonly READY_STATE_CLOSED: number; + readonly CLOSED: number; +}; +/** + * The `WebSocket` object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket) + */ +interface WebSocket extends EventTarget { + accept(options?: WebSocketAcceptOptions): void; + /** + * The **`WebSocket.send()`** method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of `bufferedAmount` by the number of bytes needed to contain the data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/send) + */ + send(message: (ArrayBuffer | ArrayBufferView) | string): void; + /** + * The **`WebSocket.close()`** method closes the already `CLOSED`, this method does nothing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/close) + */ + close(code?: number, reason?: string): void; + serializeAttachment(attachment: any): void; + deserializeAttachment(): any | null; + /** + * The **`WebSocket.readyState`** read-only property returns the current state of the WebSocket connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/readyState) + */ + readyState: number; + /** + * The **`WebSocket.url`** read-only property returns the absolute URL of the WebSocket as resolved by the constructor. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/url) + */ + url: string | null; + /** + * The **`WebSocket.protocol`** read-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in the `protocols` parameter when creating the WebSocket object, or the empty string if no connection is established. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/protocol) + */ + protocol: string | null; + /** + * The **`WebSocket.extensions`** read-only property returns the extensions selected by the server. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/extensions) + */ + extensions: string | null; + /** + * The **`WebSocket.binaryType`** property controls the type of binary data being received over the WebSocket connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/binaryType) + */ + binaryType: "blob" | "arraybuffer"; +} +interface WebSocketAcceptOptions { + /** + * When set to `true`, receiving a server-initiated WebSocket Close frame will not + * automatically send a reciprocal Close frame, leaving the connection in a half-open + * state. This is useful for proxying scenarios where you need to coordinate closing + * both sides independently. Defaults to `false` when the + * `no_web_socket_half_open_by_default` compatibility flag is enabled. + */ + allowHalfOpen?: boolean; +} +declare const WebSocketPair: { + new (): { + 0: WebSocket; + 1: WebSocket; + }; +}; +interface SqlStorage { + exec>(query: string, ...bindings: any[]): SqlStorageCursor; + get databaseSize(): number; + Cursor: typeof SqlStorageCursor; + Statement: typeof SqlStorageStatement; +} +declare abstract class SqlStorageStatement { +} +type SqlStorageValue = ArrayBuffer | string | number | null; +declare abstract class SqlStorageCursor> { + next(): { + done?: false; + value: T; + } | { + done: true; + value?: never; + }; + toArray(): T[]; + one(): T; + raw(): IterableIterator; + columnNames: string[]; + get rowsRead(): number; + get rowsWritten(): number; + [Symbol.iterator](): IterableIterator; +} +interface Socket { + get readable(): ReadableStream; + get writable(): WritableStream; + get closed(): Promise; + get opened(): Promise; + get upgraded(): boolean; + get secureTransport(): "on" | "off" | "starttls"; + close(): Promise; + startTls(options?: TlsOptions): Socket; +} +interface SocketOptions { + secureTransport?: string; + allowHalfOpen: boolean; + highWaterMark?: (number | bigint); +} +interface SocketAddress { + hostname: string; + port: number; +} +interface TlsOptions { + expectedServerHostname?: string; +} +interface SocketInfo { + remoteAddress?: string; + localAddress?: string; +} +/** + * The **`EventSource`** interface is web content's interface to server-sent events. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource) + */ +declare class EventSource extends EventTarget { + constructor(url: string, init?: EventSourceEventSourceInit); + /** + * The **`close()`** method of the EventSource interface closes the connection, if one is made, and sets the ```js-nolint close() ``` None. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close) + */ + close(): void; + /** + * The **`url`** read-only property of the URL of the source. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url) + */ + get url(): string; + /** + * The **`withCredentials`** read-only property of the the `EventSource` object was instantiated with CORS credentials set. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials) + */ + get withCredentials(): boolean; + /** + * The **`readyState`** read-only property of the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState) + */ + get readyState(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */ + get onopen(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */ + set onopen(value: any | null); + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */ + get onmessage(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */ + set onmessage(value: any | null); + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */ + get onerror(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */ + set onerror(value: any | null); + static readonly CONNECTING: number; + static readonly OPEN: number; + static readonly CLOSED: number; + static from(stream: ReadableStream): EventSource; +} +interface EventSourceEventSourceInit { + withCredentials?: boolean; + fetcher?: Fetcher; +} +interface Container { + get running(): boolean; + start(options?: ContainerStartupOptions): void; + monitor(): Promise; + destroy(error?: any): Promise; + signal(signo: number): void; + getTcpPort(port: number): Fetcher; + setInactivityTimeout(durationMs: number | bigint): Promise; + interceptOutboundHttp(addr: string, binding: Fetcher): Promise; + interceptAllOutboundHttp(binding: Fetcher): Promise; + snapshotDirectory(options: ContainerDirectorySnapshotOptions): Promise; + snapshotContainer(options: ContainerSnapshotOptions): Promise; + interceptOutboundHttps(addr: string, binding: Fetcher): Promise; +} +interface ContainerDirectorySnapshot { + id: string; + size: number; + dir: string; + name?: string; +} +interface ContainerDirectorySnapshotOptions { + dir: string; + name?: string; +} +interface ContainerDirectorySnapshotRestoreParams { + snapshot: ContainerDirectorySnapshot; + mountPoint?: string; +} +interface ContainerSnapshot { + id: string; + size: number; + name?: string; +} +interface ContainerSnapshotOptions { + name?: string; +} +interface ContainerStartupOptions { + entrypoint?: string[]; + enableInternet: boolean; + env?: Record; + labels?: Record; + directorySnapshots?: ContainerDirectorySnapshotRestoreParams[]; + containerSnapshot?: ContainerSnapshot; +} +/** + * The **`MessagePort`** interface of the Channel Messaging API represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort) + */ +declare abstract class MessagePort extends EventTarget { + /** + * The **`postMessage()`** method of the transfers ownership of objects to other browsing contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/postMessage) + */ + postMessage(data?: any, options?: (any[] | MessagePortPostMessageOptions)): void; + /** + * The **`close()`** method of the MessagePort interface disconnects the port, so it is no longer active. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/close) + */ + close(): void; + /** + * The **`start()`** method of the MessagePort interface starts the sending of messages queued on the port. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/start) + */ + start(): void; + get onmessage(): any | null; + set onmessage(value: any | null); +} +/** + * The **`MessageChannel`** interface of the Channel Messaging API allows us to create a new message channel and send data through it via its two MessagePort properties. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel) + */ +declare class MessageChannel { + constructor(); + /** + * The **`port1`** read-only property of the the port attached to the context that originated the channel. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port1) + */ + readonly port1: MessagePort; + /** + * The **`port2`** read-only property of the the port attached to the context at the other end of the channel, which the message is initially sent to. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port2) + */ + readonly port2: MessagePort; +} +interface MessagePortPostMessageOptions { + transfer?: any[]; +} +type LoopbackForExport Rpc.EntrypointBranded) | ExportedHandler | undefined = undefined> = T extends new (...args: any[]) => Rpc.WorkerEntrypointBranded ? LoopbackServiceStub> : T extends new (...args: any[]) => Rpc.DurableObjectBranded ? LoopbackDurableObjectClass> : T extends ExportedHandler ? LoopbackServiceStub : undefined; +type LoopbackServiceStub = Fetcher & (T extends CloudflareWorkersModule.WorkerEntrypoint ? (opts: { + props?: Props; +}) => Fetcher : (opts: { + props?: any; +}) => Fetcher); +type LoopbackDurableObjectClass = DurableObjectClass & (T extends CloudflareWorkersModule.DurableObject ? (opts: { + props?: Props; +}) => DurableObjectClass : (opts: { + props?: any; +}) => DurableObjectClass); +interface LoopbackDurableObjectNamespace extends DurableObjectNamespace { +} +interface LoopbackColoLocalActorNamespace extends ColoLocalActorNamespace { +} +interface SyncKvStorage { + get(key: string): T | undefined; + list(options?: SyncKvListOptions): Iterable<[ + string, + T + ]>; + put(key: string, value: T): void; + delete(key: string): boolean; +} +interface SyncKvListOptions { + start?: string; + startAfter?: string; + end?: string; + prefix?: string; + reverse?: boolean; + limit?: number; +} +interface WorkerStub { + getEntrypoint(name?: string, options?: WorkerStubEntrypointOptions): Fetcher; + getDurableObjectClass(name?: string, options?: WorkerStubEntrypointOptions): DurableObjectClass; +} +interface WorkerStubEntrypointOptions { + props?: any; + limits?: workerdResourceLimits; +} +interface WorkerLoader { + get(name: string | null, getCode: () => WorkerLoaderWorkerCode | Promise): WorkerStub; + load(code: WorkerLoaderWorkerCode): WorkerStub; +} +interface WorkerLoaderModule { + js?: string; + cjs?: string; + text?: string; + data?: ArrayBuffer; + json?: any; + py?: string; + wasm?: ArrayBuffer; +} +interface WorkerLoaderWorkerCode { + compatibilityDate: string; + compatibilityFlags?: string[]; + allowExperimental?: boolean; + limits?: workerdResourceLimits; + mainModule: string; + modules: Record; + env?: any; + globalOutbound?: (Fetcher | null); + tails?: Fetcher[]; + streamingTails?: Fetcher[]; +} +interface workerdResourceLimits { + cpuMs?: number; + subRequests?: number; +} +/** +* The Workers runtime supports a subset of the Performance API, used to measure timing and performance, +* as well as timing of subrequests and other operations. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/) +*/ +declare abstract class Performance { + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/#performancetimeorigin) */ + get timeOrigin(): number; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/#performancenow) */ + now(): number; + /** + * The **`toJSON()`** method of the Performance interface is a Serialization; it returns a JSON representation of the Performance object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Performance/toJSON) + */ + toJSON(): object; +} +interface Tracing { + enterSpan(name: string, callback: (span: Span, ...args: A) => T, ...args: A): T; + Span: typeof Span; +} +declare abstract class Span { + get isTraced(): boolean; + setAttribute(key: string, value?: (boolean | number | string)): void; +} +// ============ AI Search Error Interfaces ============ +interface AiSearchInternalError extends Error { +} +interface AiSearchNotFoundError extends Error { +} +// ============ AI Search Common Types ============ +/** A single message in a conversation-style search or chat request. */ +type AiSearchMessage = { + role: 'system' | 'developer' | 'user' | 'assistant' | 'tool'; + content: string | null; +}; +/** + * Common shape for `ai_search_options` used by both single-instance and multi-instance requests. + * Contains retrieval, query rewrite, reranking, and cache sub-options. + */ +type AiSearchOptions = { + retrieval?: { + /** Which retrieval backend to use. Defaults to the instance's configured index_method. */ + retrieval_type?: 'vector' | 'keyword' | 'hybrid'; + /** Fusion method for combining vector + keyword results. */ + fusion_method?: 'max' | 'rrf'; + /** How keyword terms are combined: "and" = all terms must match, "or" = any term matches. */ + keyword_match_mode?: 'and' | 'or'; + /** Minimum similarity score (0-1) for a result to be included. Default 0.4. */ + match_threshold?: number; + /** Maximum number of results to return (1-50). Default 10. */ + max_num_results?: number; + /** Vectorize metadata filters applied to the search. */ + filters?: VectorizeVectorMetadataFilter; + /** Number of surrounding chunks to include for context (0-3). Default 0. */ + context_expansion?: number; + /** If true, return only item metadata without chunk text. */ + metadata_only?: boolean; + /** If true (default), return empty results on retrieval failure instead of throwing. */ + return_on_failure?: boolean; + /** Boost results by metadata field values. Max 3 entries. */ + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + [key: string]: unknown; + }; + query_rewrite?: { + enabled?: boolean; + model?: string; + rewrite_prompt?: string; + [key: string]: unknown; + }; + reranking?: { + enabled?: boolean; + model?: string; + /** Match threshold (0-1, default 0.4) */ + match_threshold?: number; + [key: string]: unknown; + }; + cache?: { + enabled?: boolean; + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + }; + [key: string]: unknown; +}; +// ============ AI Search Request Types ============ +/** + * Request body for single-instance search. + * Exactly one of `query` or `messages` must be provided. + */ +type AiSearchSearchRequest = { + /** Simple query string. */ + query: string; + messages?: never; + ai_search_options?: AiSearchOptions; +} | { + query?: never; + /** Conversation-style input. At least one user message with non-empty content is required. */ + messages: AiSearchMessage[]; + ai_search_options?: AiSearchOptions; +}; +type AiSearchChatCompletionsRequest = { + messages: AiSearchMessage[]; + model?: string; + stream?: boolean; + ai_search_options?: AiSearchOptions; + [key: string]: unknown; +}; +// ============ AI Search Multi-Instance Types (Namespace-Scoped) ============ +/** `ai_search_options` shape for multi-instance requests — requires `instance_ids`. */ +type AiSearchMultiSearchOptions = AiSearchOptions & { + /** Instance IDs to search across (1-10). */ + instance_ids: string[]; +}; +/** + * Request for searching across multiple instances within a namespace. + * `ai_search_options` is required and must include `instance_ids`. + * Exactly one of `query` or `messages` must be provided. + */ +type AiSearchMultiSearchRequest = { + /** Simple query string. */ + query: string; + messages?: never; + ai_search_options: AiSearchMultiSearchOptions; +} | { + query?: never; + /** Conversation-style input. */ + messages: AiSearchMessage[]; + ai_search_options: AiSearchMultiSearchOptions; +}; +/** A search result chunk tagged with the instance it originated from. */ +type AiSearchMultiSearchChunk = AiSearchSearchResponse['chunks'][number] & { + instance_id: string; +}; +/** Describes a per-instance error during a multi-instance operation. */ +type AiSearchMultiSearchError = { + instance_id: string; + message: string; +}; +/** Response from a multi-instance search, with chunks tagged by instance and optional partial-failure errors. */ +type AiSearchMultiSearchResponse = { + search_query: string; + chunks: AiSearchMultiSearchChunk[]; + errors?: AiSearchMultiSearchError[]; +}; +/** Request for chat completions across multiple instances within a namespace. `ai_search_options` is required and must include `instance_ids`. */ +type AiSearchMultiChatCompletionsRequest = Omit & { + ai_search_options: AiSearchMultiSearchOptions; +}; +/** Response from multi-instance chat completions, with chunks tagged by instance and optional partial-failure errors. */ +type AiSearchMultiChatCompletionsResponse = Omit & { + chunks: AiSearchMultiSearchChunk[]; + errors?: AiSearchMultiSearchError[]; +}; +// ============ AI Search Response Types ============ +type AiSearchSearchResponse = { + search_query: string; + chunks: Array<{ + id: string; + type: string; + /** Match score (0-1) */ + score: number; + text: string; + item: { + timestamp?: number; + key: string; + metadata?: Record; + }; + scoring_details?: { + /** Keyword match score (0-1) */ + keyword_score?: number; + /** Vector similarity score (0-1) */ + vector_score?: number; + /** Keyword rank position */ + keyword_rank?: number; + /** Vector rank position */ + vector_rank?: number; + /** Reranking model score */ + reranking_score?: number; + /** Fusion method used to combine results */ + fusion_method?: 'rrf' | 'max'; + [key: string]: unknown; + }; + }>; +}; +type AiSearchChatCompletionsResponse = { + id?: string; + object?: string; + model?: string; + choices: Array<{ + index?: number; + message: { + role: 'system' | 'developer' | 'user' | 'assistant' | 'tool'; + content: string | null; + [key: string]: unknown; + }; + [key: string]: unknown; + }>; + chunks: AiSearchSearchResponse['chunks']; + [key: string]: unknown; +}; +type AiSearchStatsResponse = { + queued?: number; + running?: number; + completed?: number; + error?: number; + skipped?: number; + outdated?: number; + last_activity?: string; + /** Storage engine statistics. */ + engine?: { + vectorize?: { + vectorsCount: number; + dimensions: number; + }; + r2?: { + payloadSizeBytes: number; + metadataSizeBytes: number; + objectCount: number; + }; + }; +}; +// ============ AI Search Instance Info Types ============ +type AiSearchInstanceInfo = { + id: string; + type?: 'r2' | 'web-crawler' | string; + source?: string; + source_params?: unknown; + paused?: boolean; + status?: string; + namespace?: string; + created_at?: string; + modified_at?: string; + token_id?: string; + ai_gateway_id?: string; + rewrite_query?: boolean; + reranking?: boolean; + embedding_model?: string; + ai_search_model?: string; + rewrite_model?: string; + reranking_model?: string; + /** @deprecated Use index_method instead. */ + hybrid_search_enabled?: boolean; + /** Controls which storage backends are active. */ + index_method?: { + vector?: boolean; + keyword?: boolean; + }; + /** Fusion method for combining vector and keyword results. */ + fusion_method?: 'max' | 'rrf'; + indexing_options?: { + keyword_tokenizer?: 'porter' | 'trigram'; + } | null; + retrieval_options?: { + keyword_match_mode?: 'and' | 'or'; + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + } | null; + chunk?: boolean; + chunk_size?: number; + chunk_overlap?: number; + score_threshold?: number; + max_num_results?: number; + cache?: boolean; + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + custom_metadata?: Array<{ + field_name: string; + data_type: 'text' | 'number' | 'boolean' | 'datetime'; + }>; + /** Sync interval in seconds. */ + sync_interval?: 3600 | 7200 | 14400 | 21600 | 43200 | 86400; + metadata?: Record; + [key: string]: unknown; +}; +/** Pagination, search, and ordering parameters for listing instances within a namespace. */ +type AiSearchListInstancesParams = { + page?: number; + per_page?: number; + /** Search instances by ID. */ + search?: string; + /** Field to sort by. */ + order_by?: 'created_at'; + /** Sort direction. */ + order_by_direction?: 'asc' | 'desc'; +}; +type AiSearchListResponse = { + result: AiSearchInstanceInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Config Types ============ +type AiSearchConfig = { + /** Instance ID (1-32 chars, pattern: ^[a-z0-9_]+(?:-[a-z0-9_]+)*$) */ + id: string; + /** Instance type. Omit to create with built-in storage. */ + type?: 'r2' | 'web-crawler' | string; + /** Source URL (required for web-crawler type). */ + source?: string; + source_params?: unknown; + /** Token ID (UUID format) */ + token_id?: string; + ai_gateway_id?: string; + /** Enable query rewriting (default false) */ + rewrite_query?: boolean; + /** Enable reranking (default false) */ + reranking?: boolean; + embedding_model?: string; + ai_search_model?: string; + rewrite_model?: string; + reranking_model?: string; + /** @deprecated Use index_method instead. */ + hybrid_search_enabled?: boolean; + /** Controls which storage backends are used during indexing. Defaults to vector-only. */ + index_method?: { + vector?: boolean; + keyword?: boolean; + }; + /** Fusion method for combining vector and keyword results. "rrf" = reciprocal rank fusion (default), "max" = maximum score. */ + fusion_method?: 'max' | 'rrf'; + indexing_options?: { + keyword_tokenizer?: 'porter' | 'trigram'; + } | null; + retrieval_options?: { + keyword_match_mode?: 'and' | 'or'; + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + } | null; + chunk?: boolean; + chunk_size?: number; + chunk_overlap?: number; + /** Minimum similarity score (0-1) for a result to be included. */ + score_threshold?: number; + max_num_results?: number; + cache?: boolean; + /** Similarity threshold for cache hits. Stricter = fewer cache hits but higher relevance. */ + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + custom_metadata?: Array<{ + field_name: string; + data_type: 'text' | 'number' | 'boolean' | 'datetime'; + }>; + namespace?: string; + /** Sync interval in seconds. 3600=1h, 7200=2h, 14400=4h, 21600=6h, 43200=12h, 86400=24h. */ + sync_interval?: 3600 | 7200 | 14400 | 21600 | 43200 | 86400; + metadata?: Record; + [key: string]: unknown; +}; +// ============ AI Search Item Types ============ +type AiSearchItemInfo = { + id: string; + key: string; + status: 'completed' | 'error' | 'skipped' | 'queued' | 'running' | 'outdated'; + next_action?: 'INDEX' | 'DELETE' | null; + error?: string; + checksum?: string; + namespace?: string; + chunks_count?: number | null; + file_size?: number | null; + source_id?: string | null; + last_seen_at?: string; + created_at?: string; + metadata?: Record; + [key: string]: unknown; +}; +type AiSearchItemContentResult = { + body: ReadableStream; + contentType: string; + filename: string; + size: number; +}; +type AiSearchUploadItemOptions = { + metadata?: Record; +}; +type AiSearchListItemsParams = { + page?: number; + per_page?: number; + /** Search items by key name. */ + search?: string; + /** Sort order for results. */ + sort_by?: 'status' | 'modified_at'; + /** Filter items by processing status. */ + status?: 'queued' | 'running' | 'completed' | 'error' | 'skipped' | 'outdated'; + /** Filter items by source (e.g. "builtin" or "web-crawler:https://example.com"). */ + source?: string; + /** JSON-encoded Vectorize filter for metadata filtering. */ + metadata_filter?: string; +}; +type AiSearchListItemsResponse = { + result: AiSearchItemInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Item Logs Types ============ +type AiSearchItemLogsParams = { + /** Maximum number of log entries to return (1-100, default 50). */ + limit?: number; + /** Opaque cursor for pagination. Pass the `cursor` value from a previous response. */ + cursor?: string; +}; +type AiSearchItemLog = { + timestamp: string; + action: string; + message: string; + fileKey?: string; + chunkCount?: number; + processingTimeMs?: number; + errorType?: string; +}; +/** Paginated response for item processing logs (cursor-based). */ +type AiSearchItemLogsResponse = { + result: AiSearchItemLog[]; + result_info: { + count: number; + per_page: number; + cursor: string | null; + truncated: boolean; + }; +}; +// ============ AI Search Item Chunks Types ============ +type AiSearchItemChunksParams = { + /** Maximum number of chunks to return (1-100, default 20). */ + limit?: number; + /** Offset into the chunks list (default 0). */ + offset?: number; +}; +/** A single indexed chunk belonging to an item, including its text content and byte range. */ +type AiSearchItemChunk = { + id: string; + text: string; + start_byte: number; + end_byte: number; + item?: { + timestamp?: number; + key: string; + metadata?: Record; + }; +}; +/** Paginated response for item chunks (offset-based). */ +type AiSearchItemChunksResponse = { + result: AiSearchItemChunk[]; + result_info: { + count: number; + total: number; + limit: number; + offset: number; + }; +}; +// ============ AI Search Job Types ============ +type AiSearchJobInfo = { + id: string; + source: 'user' | 'schedule'; + description?: string; + last_seen_at?: string; + started_at?: string; + ended_at?: string; + end_reason?: string; +}; +type AiSearchJobLog = { + id: number; + message: string; + message_type: number; + created_at: number; +}; +type AiSearchCreateJobParams = { + description?: string; +}; +type AiSearchListJobsParams = { + page?: number; + per_page?: number; +}; +type AiSearchListJobsResponse = { + result: AiSearchJobInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +type AiSearchJobLogsParams = { + page?: number; + per_page?: number; +}; +type AiSearchJobLogsResponse = { + result: AiSearchJobLog[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Sub-Service Classes ============ +/** + * Single item service for an AI Search instance. + * Provides info, download, sync, logs, and chunks operations on a specific item. + */ +declare abstract class AiSearchItem { + /** Get metadata about this item. */ + info(): Promise; + /** + * Download the item's content. + * @returns Object with body stream, content type, filename, and size. + */ + download(): Promise; + /** + * Trigger re-indexing of this item. + * @returns The updated item info. + */ + sync(): Promise; + /** + * Retrieve processing logs for this item (cursor-based pagination). + * @param params Optional pagination parameters (limit, cursor). + * @returns Paginated log entries for this item. + */ + logs(params?: AiSearchItemLogsParams): Promise; + /** + * List indexed chunks for this item (offset-based pagination). + * @param params Optional pagination parameters (limit, offset). + * @returns Paginated chunk entries for this item. + */ + chunks(params?: AiSearchItemChunksParams): Promise; +} +/** + * Items collection service for an AI Search instance. + * Provides list, upload, and access to individual items. + */ +declare abstract class AiSearchItems { + /** List items in this instance. */ + list(params?: AiSearchListItemsParams): Promise; + /** + * Upload a file as an item. Behaves as an upsert: if an item with the same + * filename already exists, it is overwritten and re-indexed. + * @param name Filename for the uploaded item. + * @param content File content as a ReadableStream, Blob, or string. + * @param options Optional metadata to attach to the item. + * @returns The created item info. + */ + upload(name: string, content: ReadableStream | Blob | string, options?: AiSearchUploadItemOptions): Promise; + /** + * Upload a file and poll until processing completes. + * Behaves as an upsert: if an item with the same filename already exists, + * it is overwritten and re-indexed. + * @param name Filename for the uploaded item. + * @param content File content as a ReadableStream, Blob, or string. + * @param options Optional metadata and polling configuration. + * @returns The item info after processing completes (or timeout). + */ + uploadAndPoll(name: string, content: ReadableStream | Blob | string, options?: AiSearchUploadItemOptions & { + /** Polling interval in milliseconds (default 1000). */ + pollIntervalMs?: number; + /** Maximum time to wait in milliseconds (default 30000). */ + timeoutMs?: number; + }): Promise; + /** + * Get an item by ID. + * @param itemId The item identifier. + * @returns Item service for info, download, sync, logs, and chunks operations. + */ + get(itemId: string): AiSearchItem; + /** + * Delete an item from the instance. + * @param itemId The item identifier. + */ + delete(itemId: string): Promise; +} +/** + * Single job service for an AI Search instance. + * Provides info, logs, and cancel operations for a specific job. + */ +declare abstract class AiSearchJob { + /** Get metadata about this job. */ + info(): Promise; + /** Get logs for this job. */ + logs(params?: AiSearchJobLogsParams): Promise; + /** + * Cancel a running job. + * @returns The updated job info. + * @throws AiSearchNotFoundError if the job does not exist. + */ + cancel(): Promise; +} +/** + * Jobs collection service for an AI Search instance. + * Provides list, create, and access to individual jobs. + */ +declare abstract class AiSearchJobs { + /** List jobs for this instance. */ + list(params?: AiSearchListJobsParams): Promise; + /** + * Create a new indexing job. + * @param params Optional job parameters. + * @returns The created job info. + */ + create(params?: AiSearchCreateJobParams): Promise; + /** + * Get a job by ID. + * @param jobId The job identifier. + * @returns Job service for info, logs, and cancel operations. + */ + get(jobId: string): AiSearchJob; +} +// ============ AI Search Binding Classes ============ +/** + * Instance-level AI Search service. + * + * Used as: + * - The return type of `AiSearchNamespace.get(name)` (namespace binding) + * - The type of `env.BLOG_SEARCH` (single instance binding via `ai_search`) + * + * Provides search, chat, update, stats, items, and jobs operations. + * + * @example + * ```ts + * // Via namespace binding + * const instance = env.AI_SEARCH.get("blog"); + * const results = await instance.search({ + * query: "How does caching work?", + * }); + * + * // Via single instance binding + * const results = await env.BLOG_SEARCH.search({ + * messages: [{ role: "user", content: "How does caching work?" }], + * }); + * ``` + */ +declare abstract class AiSearchInstance { + /** + * Search the AI Search instance for relevant chunks. + * @param params Search request with query or messages and optional AI search options. + * @returns Search response with matching chunks and search query. + */ + search(params: AiSearchSearchRequest): Promise; + /** + * Generate chat completions with AI Search context (streaming). + * @param params Chat completions request with stream: true. + * @returns ReadableStream of server-sent events. + */ + chatCompletions(params: AiSearchChatCompletionsRequest & { + stream: true; + }): Promise; + /** + * Generate chat completions with AI Search context. + * @param params Chat completions request. + * @returns Chat completion response with choices and RAG chunks. + */ + chatCompletions(params: AiSearchChatCompletionsRequest): Promise; + /** + * Update the instance configuration. + * @param config Partial configuration to update. + * @returns Updated instance info. + */ + update(config: Partial): Promise; + /** Get metadata about this instance. */ + info(): Promise; + /** + * Get instance statistics (item count, indexing status, etc.). + * @returns Statistics with counts per status, last activity time, and engine details. + */ + stats(): Promise; + /** Items collection — list, upload, and manage items in this instance. */ + get items(): AiSearchItems; + /** Jobs collection — list, create, and inspect indexing jobs. */ + get jobs(): AiSearchJobs; +} +/** + * Namespace-level AI Search service. + * + * Used as the type of `env.AI_SEARCH` (namespace binding via `ai_search_namespaces`). + * Scoped to a single namespace. Provides dynamic instance access, creation, deletion, + * and multi-instance search/chat operations. + * + * @example + * ```ts + * // Access an instance within the namespace + * const blog = env.AI_SEARCH.get("blog"); + * const results = await blog.search({ query: "How does caching work?" }); + * + * // List all instances in the namespace + * const instances = await env.AI_SEARCH.list(); + * + * // Create a new instance with built-in storage + * const tenant = await env.AI_SEARCH.create({ id: "tenant-123" }); + * + * // Upload items into the instance + * await tenant.items.upload("doc.pdf", fileContent); + * + * // Search across multiple instances + * const multi = await env.AI_SEARCH.search({ + * query: "caching", + * ai_search_options: { instance_ids: ["blog", "docs"] }, + * }); + * + * // Delete an instance + * await env.AI_SEARCH.delete("tenant-123"); + * ``` + */ +declare abstract class AiSearchNamespace { + /** + * Get an instance by name within the bound namespace. + * @param name Instance name. + * @returns Instance service for search, chat, update, stats, items, and jobs. + */ + get(name: string): AiSearchInstance; + /** + * List instances in the bound namespace. + * @param params Optional pagination, search, and ordering parameters. + * @returns Array of instance metadata with pagination info. + */ + list(params?: AiSearchListInstancesParams): Promise; + /** + * Create a new instance within the bound namespace. + * @param config Instance configuration. Only `id` is required — omit `type` and `source` to create with built-in storage. + * @returns Instance service for the newly created instance. + * + * @example + * ```ts + * // Create with built-in storage (upload items manually) + * const instance = await env.AI_SEARCH.create({ id: "my-search" }); + * + * // Create with web crawler source + * const instance = await env.AI_SEARCH.create({ + * id: "docs-search", + * type: "web-crawler", + * source: "https://developers.cloudflare.com", + * }); + * ``` + */ + create(config: AiSearchConfig): Promise; + /** + * Delete an instance from the bound namespace. + * @param name Instance name to delete. + */ + delete(name: string): Promise; + /** + * Search across multiple instances within the bound namespace. + * Fans out to the specified instance_ids and merges results. + * @param params Search request with required `ai_search_options.instance_ids`. + * @returns Search response with chunks tagged by instance_id and optional partial-failure errors. + */ + search(params: AiSearchMultiSearchRequest): Promise; + /** + * Generate chat completions across multiple instances within the bound namespace (streaming). + * Fans out to the specified instance_ids, merges context, and generates a response. + * @param params Chat completions request with stream: true and required `ai_search_options.instance_ids`. + * @returns ReadableStream of server-sent events. + */ + chatCompletions(params: AiSearchMultiChatCompletionsRequest & { + stream: true; + }): Promise; + /** + * Generate chat completions across multiple instances within the bound namespace. + * Fans out to the specified instance_ids, merges context, and generates a response. + * @param params Chat completions request with required `ai_search_options.instance_ids`. + * @returns Chat completion response with choices, chunks tagged by instance_id, and optional partial-failure errors. + */ + chatCompletions(params: AiSearchMultiChatCompletionsRequest): Promise; +} +type AiImageClassificationInput = { + image: number[]; +}; +type AiImageClassificationOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiImageClassification { + inputs: AiImageClassificationInput; + postProcessedOutputs: AiImageClassificationOutput; +} +type AiImageToTextInput = { + image: number[]; + prompt?: string; + max_tokens?: number; + temperature?: number; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + raw?: boolean; + messages?: RoleScopedChatInput[]; +}; +type AiImageToTextOutput = { + description: string; +}; +declare abstract class BaseAiImageToText { + inputs: AiImageToTextInput; + postProcessedOutputs: AiImageToTextOutput; +} +type AiImageTextToTextInput = { + image: string; + prompt?: string; + max_tokens?: number; + temperature?: number; + ignore_eos?: boolean; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + raw?: boolean; + messages?: RoleScopedChatInput[]; +}; +type AiImageTextToTextOutput = { + description: string; +}; +declare abstract class BaseAiImageTextToText { + inputs: AiImageTextToTextInput; + postProcessedOutputs: AiImageTextToTextOutput; +} +type AiMultimodalEmbeddingsInput = { + image: string; + text: string[]; +}; +type AiIMultimodalEmbeddingsOutput = { + data: number[][]; + shape: number[]; +}; +declare abstract class BaseAiMultimodalEmbeddings { + inputs: AiImageTextToTextInput; + postProcessedOutputs: AiImageTextToTextOutput; +} +type AiObjectDetectionInput = { + image: number[]; +}; +type AiObjectDetectionOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiObjectDetection { + inputs: AiObjectDetectionInput; + postProcessedOutputs: AiObjectDetectionOutput; +} +type AiSentenceSimilarityInput = { + source: string; + sentences: string[]; +}; +type AiSentenceSimilarityOutput = number[]; +declare abstract class BaseAiSentenceSimilarity { + inputs: AiSentenceSimilarityInput; + postProcessedOutputs: AiSentenceSimilarityOutput; +} +type AiAutomaticSpeechRecognitionInput = { + audio: number[]; +}; +type AiAutomaticSpeechRecognitionOutput = { + text?: string; + words?: { + word: string; + start: number; + end: number; + }[]; + vtt?: string; +}; +declare abstract class BaseAiAutomaticSpeechRecognition { + inputs: AiAutomaticSpeechRecognitionInput; + postProcessedOutputs: AiAutomaticSpeechRecognitionOutput; +} +type AiSummarizationInput = { + input_text: string; + max_length?: number; +}; +type AiSummarizationOutput = { + summary: string; +}; +declare abstract class BaseAiSummarization { + inputs: AiSummarizationInput; + postProcessedOutputs: AiSummarizationOutput; +} +type AiTextClassificationInput = { + text: string; +}; +type AiTextClassificationOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiTextClassification { + inputs: AiTextClassificationInput; + postProcessedOutputs: AiTextClassificationOutput; +} +type AiTextEmbeddingsInput = { + text: string | string[]; +}; +type AiTextEmbeddingsOutput = { + shape: number[]; + data: number[][]; +}; +declare abstract class BaseAiTextEmbeddings { + inputs: AiTextEmbeddingsInput; + postProcessedOutputs: AiTextEmbeddingsOutput; +} +type RoleScopedChatInput = { + role: "user" | "assistant" | "system" | "tool" | (string & NonNullable); + content: string; + name?: string; +}; +type AiTextGenerationToolLegacyInput = { + name: string; + description: string; + parameters?: { + type: "object" | (string & NonNullable); + properties: { + [key: string]: { + type: string; + description?: string; + }; + }; + required: string[]; + }; +}; +type AiTextGenerationToolInput = { + type: "function" | (string & NonNullable); + function: { + name: string; + description: string; + parameters?: { + type: "object" | (string & NonNullable); + properties: { + [key: string]: { + type: string; + description?: string; + }; + }; + required: string[]; + }; + }; +}; +type AiTextGenerationFunctionsInput = { + name: string; + code: string; +}; +type AiTextGenerationResponseFormat = { + type: string; + json_schema?: any; +}; +type AiTextGenerationInput = { + prompt?: string; + raw?: boolean; + stream?: boolean; + max_tokens?: number; + temperature?: number; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + messages?: RoleScopedChatInput[]; + response_format?: AiTextGenerationResponseFormat; + tools?: AiTextGenerationToolInput[] | AiTextGenerationToolLegacyInput[] | (object & NonNullable); + functions?: AiTextGenerationFunctionsInput[]; +}; +type AiTextGenerationToolLegacyOutput = { + name: string; + arguments: unknown; +}; +type AiTextGenerationToolOutput = { + id: string; + type: "function"; + function: { + name: string; + arguments: string; + }; +}; +type UsageTags = { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; +}; +type AiTextGenerationOutput = { + response?: string; + tool_calls?: AiTextGenerationToolLegacyOutput[] & AiTextGenerationToolOutput[]; + usage?: UsageTags; +}; +declare abstract class BaseAiTextGeneration { + inputs: AiTextGenerationInput; + postProcessedOutputs: AiTextGenerationOutput; +} +type AiTextToSpeechInput = { + prompt: string; + lang?: string; +}; +type AiTextToSpeechOutput = Uint8Array | { + audio: string; +}; +declare abstract class BaseAiTextToSpeech { + inputs: AiTextToSpeechInput; + postProcessedOutputs: AiTextToSpeechOutput; +} +type AiTextToImageInput = { + prompt: string; + negative_prompt?: string; + height?: number; + width?: number; + image?: number[]; + image_b64?: string; + mask?: number[]; + num_steps?: number; + strength?: number; + guidance?: number; + seed?: number; +}; +type AiTextToImageOutput = ReadableStream; +declare abstract class BaseAiTextToImage { + inputs: AiTextToImageInput; + postProcessedOutputs: AiTextToImageOutput; +} +type AiTranslationInput = { + text: string; + target_lang: string; + source_lang?: string; +}; +type AiTranslationOutput = { + translated_text?: string; +}; +declare abstract class BaseAiTranslation { + inputs: AiTranslationInput; + postProcessedOutputs: AiTranslationOutput; +} +/** + * Workers AI support for OpenAI's Chat Completions API + */ +type ChatCompletionContentPartText = { + type: "text"; + text: string; +}; +type ChatCompletionContentPartImage = { + type: "image_url"; + image_url: { + url: string; + detail?: "auto" | "low" | "high"; + }; +}; +type ChatCompletionContentPartInputAudio = { + type: "input_audio"; + input_audio: { + /** Base64 encoded audio data. */ + data: string; + format: "wav" | "mp3"; + }; +}; +type ChatCompletionContentPartFile = { + type: "file"; + file: { + /** Base64 encoded file data. */ + file_data?: string; + /** The ID of an uploaded file. */ + file_id?: string; + filename?: string; + }; +}; +type ChatCompletionContentPartRefusal = { + type: "refusal"; + refusal: string; +}; +type ChatCompletionContentPart = ChatCompletionContentPartText | ChatCompletionContentPartImage | ChatCompletionContentPartInputAudio | ChatCompletionContentPartFile; +type FunctionDefinition = { + name: string; + description?: string; + parameters?: Record; + strict?: boolean | null; +}; +type ChatCompletionFunctionTool = { + type: "function"; + function: FunctionDefinition; +}; +type ChatCompletionCustomToolGrammarFormat = { + type: "grammar"; + grammar: { + definition: string; + syntax: "lark" | "regex"; + }; +}; +type ChatCompletionCustomToolTextFormat = { + type: "text"; +}; +type ChatCompletionCustomToolFormat = ChatCompletionCustomToolTextFormat | ChatCompletionCustomToolGrammarFormat; +type ChatCompletionCustomTool = { + type: "custom"; + custom: { + name: string; + description?: string; + format?: ChatCompletionCustomToolFormat; + }; +}; +type ChatCompletionTool = ChatCompletionFunctionTool | ChatCompletionCustomTool; +type ChatCompletionMessageFunctionToolCall = { + id: string; + type: "function"; + function: { + name: string; + /** JSON-encoded arguments string. */ + arguments: string; + }; +}; +type ChatCompletionMessageCustomToolCall = { + id: string; + type: "custom"; + custom: { + name: string; + input: string; + }; +}; +type ChatCompletionMessageToolCall = ChatCompletionMessageFunctionToolCall | ChatCompletionMessageCustomToolCall; +type ChatCompletionToolChoiceFunction = { + type: "function"; + function: { + name: string; + }; +}; +type ChatCompletionToolChoiceCustom = { + type: "custom"; + custom: { + name: string; + }; +}; +type ChatCompletionToolChoiceAllowedTools = { + type: "allowed_tools"; + allowed_tools: { + mode: "auto" | "required"; + tools: Array>; + }; +}; +type ChatCompletionToolChoiceOption = "none" | "auto" | "required" | ChatCompletionToolChoiceFunction | ChatCompletionToolChoiceCustom | ChatCompletionToolChoiceAllowedTools; +type DeveloperMessage = { + role: "developer"; + content: string | Array<{ + type: "text"; + text: string; + }>; + name?: string; +}; +type SystemMessage = { + role: "system"; + content: string | Array<{ + type: "text"; + text: string; + }>; + name?: string; +}; +/** + * Permissive merged content part used inside UserMessage arrays. + * + * Cabidela has a limitation where anyOf/oneOf with enum-based discrimination + * inside nested array items does not correctly match different branches for + * different array elements, so the schema uses a single merged object. + */ +type UserMessageContentPart = { + type: "text" | "image_url" | "input_audio" | "file"; + text?: string; + image_url?: { + url?: string; + detail?: "auto" | "low" | "high"; + }; + input_audio?: { + data?: string; + format?: "wav" | "mp3"; + }; + file?: { + file_data?: string; + file_id?: string; + filename?: string; + }; +}; +type UserMessage = { + role: "user"; + content: string | Array; + name?: string; +}; +type AssistantMessageContentPart = { + type: "text" | "refusal"; + text?: string; + refusal?: string; +}; +type AssistantMessage = { + role: "assistant"; + content?: string | null | Array; + refusal?: string | null; + name?: string; + audio?: { + id: string; + }; + tool_calls?: Array; + function_call?: { + name: string; + arguments: string; + }; +}; +type ToolMessage = { + role: "tool"; + content: string | Array<{ + type: "text"; + text: string; + }>; + tool_call_id: string; +}; +type FunctionMessage = { + role: "function"; + content: string; + name: string; +}; +type ChatCompletionMessageParam = DeveloperMessage | SystemMessage | UserMessage | AssistantMessage | ToolMessage | FunctionMessage; +type ChatCompletionsResponseFormatText = { + type: "text"; +}; +type ChatCompletionsResponseFormatJSONObject = { + type: "json_object"; +}; +type ResponseFormatJSONSchema = { + type: "json_schema"; + json_schema: { + name: string; + description?: string; + schema?: Record; + strict?: boolean | null; + }; +}; +type ResponseFormat = ChatCompletionsResponseFormatText | ChatCompletionsResponseFormatJSONObject | ResponseFormatJSONSchema; +type ChatCompletionsStreamOptions = { + include_usage?: boolean; + include_obfuscation?: boolean; +}; +type PredictionContent = { + type: "content"; + content: string | Array<{ + type: "text"; + text: string; + }>; +}; +type AudioParams = { + voice: string | { + id: string; + }; + format: "wav" | "aac" | "mp3" | "flac" | "opus" | "pcm16"; +}; +type WebSearchUserLocation = { + type: "approximate"; + approximate: { + city?: string; + country?: string; + region?: string; + timezone?: string; + }; +}; +type WebSearchOptions = { + search_context_size?: "low" | "medium" | "high"; + user_location?: WebSearchUserLocation; +}; +type ChatTemplateKwargs = { + /** Whether to enable reasoning, enabled by default. */ + enable_thinking?: boolean; + /** If false, preserves reasoning context between turns. */ + clear_thinking?: boolean; +}; +/** Shared optional properties used by both Prompt and Messages input branches. */ +type ChatCompletionsCommonOptions = { + model?: string; + audio?: AudioParams; + frequency_penalty?: number | null; + logit_bias?: Record | null; + logprobs?: boolean | null; + top_logprobs?: number | null; + max_tokens?: number | null; + max_completion_tokens?: number | null; + metadata?: Record | null; + modalities?: Array<"text" | "audio"> | null; + n?: number | null; + parallel_tool_calls?: boolean; + prediction?: PredictionContent; + presence_penalty?: number | null; + reasoning_effort?: "low" | "medium" | "high" | null; + chat_template_kwargs?: ChatTemplateKwargs; + response_format?: ResponseFormat; + seed?: number | null; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + stop?: string | Array | null; + store?: boolean | null; + stream?: boolean | null; + stream_options?: ChatCompletionsStreamOptions; + temperature?: number | null; + tool_choice?: ChatCompletionToolChoiceOption; + tools?: Array; + top_p?: number | null; + user?: string; + web_search_options?: WebSearchOptions; + function_call?: "none" | "auto" | { + name: string; + }; + functions?: Array; +}; +type PromptTokensDetails = { + cached_tokens?: number; + audio_tokens?: number; +}; +type CompletionTokensDetails = { + reasoning_tokens?: number; + audio_tokens?: number; + accepted_prediction_tokens?: number; + rejected_prediction_tokens?: number; +}; +type CompletionUsage = { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + prompt_tokens_details?: PromptTokensDetails; + completion_tokens_details?: CompletionTokensDetails; +}; +type ChatCompletionTopLogprob = { + token: string; + logprob: number; + bytes: Array | null; +}; +type ChatCompletionTokenLogprob = { + token: string; + logprob: number; + bytes: Array | null; + top_logprobs: Array; +}; +type ChatCompletionAudio = { + id: string; + /** Base64 encoded audio bytes. */ + data: string; + expires_at: number; + transcript: string; +}; +type ChatCompletionUrlCitation = { + type: "url_citation"; + url_citation: { + url: string; + title: string; + start_index: number; + end_index: number; + }; +}; +type ChatCompletionResponseMessage = { + role: "assistant"; + content: string | null; + refusal: string | null; + annotations?: Array; + audio?: ChatCompletionAudio; + tool_calls?: Array; + function_call?: { + name: string; + arguments: string; + } | null; +}; +type ChatCompletionLogprobs = { + content: Array | null; + refusal?: Array | null; +}; +type ChatCompletionChoice = { + index: number; + message: ChatCompletionResponseMessage; + finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | "function_call"; + logprobs: ChatCompletionLogprobs | null; +}; +type ChatCompletionsPromptInput = { + prompt: string; +} & ChatCompletionsCommonOptions; +type ChatCompletionsMessagesInput = { + messages: Array; +} & ChatCompletionsCommonOptions; +type ChatCompletionsOutput = { + id: string; + object: string; + created: number; + model: string; + choices: Array; + usage?: CompletionUsage; + system_fingerprint?: string | null; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; +}; +/** + * Workers AI support for OpenAI's Responses API + * Reference: https://github.com/openai/openai-node/blob/master/src/resources/responses/responses.ts + * + * It's a stripped down version from its source. + * It currently supports basic function calling, json mode and accepts images as input. + * + * It does not include types for WebSearch, CodeInterpreter, FileInputs, MCP, CustomTools. + * We plan to add those incrementally as model + platform capabilities evolve. + */ +type ResponsesInput = { + background?: boolean | null; + conversation?: string | ResponseConversationParam | null; + include?: Array | null; + input?: string | ResponseInput; + instructions?: string | null; + max_output_tokens?: number | null; + parallel_tool_calls?: boolean | null; + previous_response_id?: string | null; + prompt_cache_key?: string; + reasoning?: Reasoning | null; + safety_identifier?: string; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + stream?: boolean | null; + stream_options?: StreamOptions | null; + temperature?: number | null; + text?: ResponseTextConfig; + tool_choice?: ToolChoiceOptions | ToolChoiceFunction; + tools?: Array; + top_p?: number | null; + truncation?: "auto" | "disabled" | null; +}; +type ResponsesOutput = { + id?: string; + created_at?: number; + output_text?: string; + error?: ResponseError | null; + incomplete_details?: ResponseIncompleteDetails | null; + instructions?: string | Array | null; + object?: "response"; + output?: Array; + parallel_tool_calls?: boolean; + temperature?: number | null; + tool_choice?: ToolChoiceOptions | ToolChoiceFunction; + tools?: Array; + top_p?: number | null; + max_output_tokens?: number | null; + previous_response_id?: string | null; + prompt?: ResponsePrompt | null; + reasoning?: Reasoning | null; + safety_identifier?: string; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + status?: ResponseStatus; + text?: ResponseTextConfig; + truncation?: "auto" | "disabled" | null; + usage?: ResponseUsage; +}; +type EasyInputMessage = { + content: string | ResponseInputMessageContentList; + role: "user" | "assistant" | "system" | "developer"; + type?: "message"; +}; +type ResponsesFunctionTool = { + name: string; + parameters: { + [key: string]: unknown; + } | null; + strict: boolean | null; + type: "function"; + description?: string | null; +}; +type ResponseIncompleteDetails = { + reason?: "max_output_tokens" | "content_filter"; +}; +type ResponsePrompt = { + id: string; + variables?: { + [key: string]: string | ResponseInputText | ResponseInputImage; + } | null; + version?: string | null; +}; +type Reasoning = { + effort?: ReasoningEffort | null; + generate_summary?: "auto" | "concise" | "detailed" | null; + summary?: "auto" | "concise" | "detailed" | null; +}; +type ResponseContent = ResponseInputText | ResponseInputImage | ResponseOutputText | ResponseOutputRefusal | ResponseContentReasoningText; +type ResponseContentReasoningText = { + text: string; + type: "reasoning_text"; +}; +type ResponseConversationParam = { + id: string; +}; +type ResponseCreatedEvent = { + response: Response; + sequence_number: number; + type: "response.created"; +}; +type ResponseCustomToolCallOutput = { + call_id: string; + output: string | Array; + type: "custom_tool_call_output"; + id?: string; +}; +type ResponseError = { + code: "server_error" | "rate_limit_exceeded" | "invalid_prompt" | "vector_store_timeout" | "invalid_image" | "invalid_image_format" | "invalid_base64_image" | "invalid_image_url" | "image_too_large" | "image_too_small" | "image_parse_error" | "image_content_policy_violation" | "invalid_image_mode" | "image_file_too_large" | "unsupported_image_media_type" | "empty_image_file" | "failed_to_download_image" | "image_file_not_found"; + message: string; +}; +type ResponseErrorEvent = { + code: string | null; + message: string; + param: string | null; + sequence_number: number; + type: "error"; +}; +type ResponseFailedEvent = { + response: Response; + sequence_number: number; + type: "response.failed"; +}; +type ResponseFormatText = { + type: "text"; +}; +type ResponseFormatJSONObject = { + type: "json_object"; +}; +type ResponseFormatTextConfig = ResponseFormatText | ResponseFormatTextJSONSchemaConfig | ResponseFormatJSONObject; +type ResponseFormatTextJSONSchemaConfig = { + name: string; + schema: { + [key: string]: unknown; + }; + type: "json_schema"; + description?: string; + strict?: boolean | null; +}; +type ResponseFunctionCallArgumentsDeltaEvent = { + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.function_call_arguments.delta"; +}; +type ResponseFunctionCallArgumentsDoneEvent = { + arguments: string; + item_id: string; + name: string; + output_index: number; + sequence_number: number; + type: "response.function_call_arguments.done"; +}; +type ResponseFunctionCallOutputItem = ResponseInputTextContent | ResponseInputImageContent; +type ResponseFunctionCallOutputItemList = Array; +type ResponseFunctionToolCall = { + arguments: string; + call_id: string; + name: string; + type: "function_call"; + id?: string; + status?: "in_progress" | "completed" | "incomplete"; +}; +interface ResponseFunctionToolCallItem extends ResponseFunctionToolCall { + id: string; +} +type ResponseFunctionToolCallOutputItem = { + id: string; + call_id: string; + output: string | Array; + type: "function_call_output"; + status?: "in_progress" | "completed" | "incomplete"; +}; +type ResponseIncludable = "message.input_image.image_url" | "message.output_text.logprobs"; +type ResponseIncompleteEvent = { + response: Response; + sequence_number: number; + type: "response.incomplete"; +}; +type ResponseInput = Array; +type ResponseInputContent = ResponseInputText | ResponseInputImage; +type ResponseInputImage = { + detail: "low" | "high" | "auto"; + type: "input_image"; + /** + * Base64 encoded image + */ + image_url?: string | null; +}; +type ResponseInputImageContent = { + type: "input_image"; + detail?: "low" | "high" | "auto" | null; + /** + * Base64 encoded image + */ + image_url?: string | null; +}; +type ResponseInputItem = EasyInputMessage | ResponseInputItemMessage | ResponseOutputMessage | ResponseFunctionToolCall | ResponseInputItemFunctionCallOutput | ResponseReasoningItem; +type ResponseInputItemFunctionCallOutput = { + call_id: string; + output: string | ResponseFunctionCallOutputItemList; + type: "function_call_output"; + id?: string | null; + status?: "in_progress" | "completed" | "incomplete" | null; +}; +type ResponseInputItemMessage = { + content: ResponseInputMessageContentList; + role: "user" | "system" | "developer"; + status?: "in_progress" | "completed" | "incomplete"; + type?: "message"; +}; +type ResponseInputMessageContentList = Array; +type ResponseInputMessageItem = { + id: string; + content: ResponseInputMessageContentList; + role: "user" | "system" | "developer"; + status?: "in_progress" | "completed" | "incomplete"; + type?: "message"; +}; +type ResponseInputText = { + text: string; + type: "input_text"; +}; +type ResponseInputTextContent = { + text: string; + type: "input_text"; +}; +type ResponseItem = ResponseInputMessageItem | ResponseOutputMessage | ResponseFunctionToolCallItem | ResponseFunctionToolCallOutputItem; +type ResponseOutputItem = ResponseOutputMessage | ResponseFunctionToolCall | ResponseReasoningItem; +type ResponseOutputItemAddedEvent = { + item: ResponseOutputItem; + output_index: number; + sequence_number: number; + type: "response.output_item.added"; +}; +type ResponseOutputItemDoneEvent = { + item: ResponseOutputItem; + output_index: number; + sequence_number: number; + type: "response.output_item.done"; +}; +type ResponseOutputMessage = { + id: string; + content: Array; + role: "assistant"; + status: "in_progress" | "completed" | "incomplete"; + type: "message"; +}; +type ResponseOutputRefusal = { + refusal: string; + type: "refusal"; +}; +type ResponseOutputText = { + text: string; + type: "output_text"; + logprobs?: Array; +}; +type ResponseReasoningItem = { + id: string; + summary: Array; + type: "reasoning"; + content?: Array; + encrypted_content?: string | null; + status?: "in_progress" | "completed" | "incomplete"; +}; +type ResponseReasoningSummaryItem = { + text: string; + type: "summary_text"; +}; +type ResponseReasoningContentItem = { + text: string; + type: "reasoning_text"; +}; +type ResponseReasoningTextDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.reasoning_text.delta"; +}; +type ResponseReasoningTextDoneEvent = { + content_index: number; + item_id: string; + output_index: number; + sequence_number: number; + text: string; + type: "response.reasoning_text.done"; +}; +type ResponseRefusalDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.refusal.delta"; +}; +type ResponseRefusalDoneEvent = { + content_index: number; + item_id: string; + output_index: number; + refusal: string; + sequence_number: number; + type: "response.refusal.done"; +}; +type ResponseStatus = "completed" | "failed" | "in_progress" | "cancelled" | "queued" | "incomplete"; +type ResponseStreamEvent = ResponseCompletedEvent | ResponseCreatedEvent | ResponseErrorEvent | ResponseFunctionCallArgumentsDeltaEvent | ResponseFunctionCallArgumentsDoneEvent | ResponseFailedEvent | ResponseIncompleteEvent | ResponseOutputItemAddedEvent | ResponseOutputItemDoneEvent | ResponseReasoningTextDeltaEvent | ResponseReasoningTextDoneEvent | ResponseRefusalDeltaEvent | ResponseRefusalDoneEvent | ResponseTextDeltaEvent | ResponseTextDoneEvent; +type ResponseCompletedEvent = { + response: Response; + sequence_number: number; + type: "response.completed"; +}; +type ResponseTextConfig = { + format?: ResponseFormatTextConfig; + verbosity?: "low" | "medium" | "high" | null; +}; +type ResponseTextDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + logprobs: Array; + output_index: number; + sequence_number: number; + type: "response.output_text.delta"; +}; +type ResponseTextDoneEvent = { + content_index: number; + item_id: string; + logprobs: Array; + output_index: number; + sequence_number: number; + text: string; + type: "response.output_text.done"; +}; +type Logprob = { + token: string; + logprob: number; + top_logprobs?: Array; +}; +type TopLogprob = { + token?: string; + logprob?: number; +}; +type ResponseUsage = { + input_tokens: number; + output_tokens: number; + total_tokens: number; +}; +type Tool = ResponsesFunctionTool; +type ToolChoiceFunction = { + name: string; + type: "function"; +}; +type ToolChoiceOptions = "none"; +type ReasoningEffort = "minimal" | "low" | "medium" | "high" | null; +type StreamOptions = { + include_obfuscation?: boolean; +}; +/** Marks keys from T that aren't in U as optional never */ +type Without = { + [P in Exclude]?: never; +}; +/** Either T or U, but not both (mutually exclusive) */ +type XOR = (T & Without) | (U & Without); +type Ai_Cf_Baai_Bge_Base_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Base_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Base_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Base_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Base_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Base_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Base_En_V1_5_Output; +} +type Ai_Cf_Openai_Whisper_Input = string | { + /** + * An array of integers that represent the audio data constrained to 8-bit unsigned integer values + */ + audio: number[]; +}; +interface Ai_Cf_Openai_Whisper_Output { + /** + * The transcription + */ + text: string; + word_count?: number; + words?: { + word?: string; + /** + * The second this word begins in the recording + */ + start?: number; + /** + * The ending second when the word completes + */ + end?: number; + }[]; + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper { + inputs: Ai_Cf_Openai_Whisper_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Output; +} +type Ai_Cf_Meta_M2M100_1_2B_Input = { + /** + * The text to be translated + */ + text: string; + /** + * The language code of the source text (e.g., 'en' for English). Defaults to 'en' if not specified + */ + source_lang?: string; + /** + * The language code to translate the text into (e.g., 'es' for Spanish) + */ + target_lang: string; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + /** + * The text to be translated + */ + text: string; + /** + * The language code of the source text (e.g., 'en' for English). Defaults to 'en' if not specified + */ + source_lang?: string; + /** + * The language code to translate the text into (e.g., 'es' for Spanish) + */ + target_lang: string; + }[]; +}; +type Ai_Cf_Meta_M2M100_1_2B_Output = { + /** + * The translated text in the target language + */ + translated_text?: string; +} | Ai_Cf_Meta_M2M100_1_2B_AsyncResponse; +interface Ai_Cf_Meta_M2M100_1_2B_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Meta_M2M100_1_2B { + inputs: Ai_Cf_Meta_M2M100_1_2B_Input; + postProcessedOutputs: Ai_Cf_Meta_M2M100_1_2B_Output; +} +type Ai_Cf_Baai_Bge_Small_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Small_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Small_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Small_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Small_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Small_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Small_En_V1_5_Output; +} +type Ai_Cf_Baai_Bge_Large_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Large_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Large_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Large_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Large_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Large_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Large_En_V1_5_Output; +} +type Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Input = string | { + /** + * The input text prompt for the model to generate a response. + */ + prompt?: string; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * Controls the creativity of the AI's responses by adjusting how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + image: number[] | (string & NonNullable); + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; +}; +interface Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Output { + description?: string; +} +declare abstract class Base_Ai_Cf_Unum_Uform_Gen2_Qwen_500M { + inputs: Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Input; + postProcessedOutputs: Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Output; +} +type Ai_Cf_Openai_Whisper_Tiny_En_Input = string | { + /** + * An array of integers that represent the audio data constrained to 8-bit unsigned integer values + */ + audio: number[]; +}; +interface Ai_Cf_Openai_Whisper_Tiny_En_Output { + /** + * The transcription + */ + text: string; + word_count?: number; + words?: { + word?: string; + /** + * The second this word begins in the recording + */ + start?: number; + /** + * The ending second when the word completes + */ + end?: number; + }[]; + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper_Tiny_En { + inputs: Ai_Cf_Openai_Whisper_Tiny_En_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Tiny_En_Output; +} +interface Ai_Cf_Openai_Whisper_Large_V3_Turbo_Input { + audio: string | { + body?: object; + contentType?: string; + }; + /** + * Supported tasks are 'translate' or 'transcribe'. + */ + task?: string; + /** + * The language of the audio being transcribed or translated. + */ + language?: string; + /** + * Preprocess the audio with a voice activity detection model. + */ + vad_filter?: boolean; + /** + * A text prompt to help provide context to the model on the contents of the audio. + */ + initial_prompt?: string; + /** + * The prefix appended to the beginning of the output of the transcription and can guide the transcription result. + */ + prefix?: string; + /** + * The number of beams to use in beam search decoding. Higher values may improve accuracy at the cost of speed. + */ + beam_size?: number; + /** + * Whether to condition on previous text during transcription. Setting to false may help prevent hallucination loops. + */ + condition_on_previous_text?: boolean; + /** + * Threshold for detecting no-speech segments. Segments with no-speech probability above this value are skipped. + */ + no_speech_threshold?: number; + /** + * Threshold for filtering out segments with high compression ratio, which often indicate repetitive or hallucinated text. + */ + compression_ratio_threshold?: number; + /** + * Threshold for filtering out segments with low average log probability, indicating low confidence. + */ + log_prob_threshold?: number; + /** + * Optional threshold (in seconds) to skip silent periods that may cause hallucinations. + */ + hallucination_silence_threshold?: number; +} +interface Ai_Cf_Openai_Whisper_Large_V3_Turbo_Output { + transcription_info?: { + /** + * The language of the audio being transcribed or translated. + */ + language?: string; + /** + * The confidence level or probability of the detected language being accurate, represented as a decimal between 0 and 1. + */ + language_probability?: number; + /** + * The total duration of the original audio file, in seconds. + */ + duration?: number; + /** + * The duration of the audio after applying Voice Activity Detection (VAD) to remove silent or irrelevant sections, in seconds. + */ + duration_after_vad?: number; + }; + /** + * The complete transcription of the audio. + */ + text: string; + /** + * The total number of words in the transcription. + */ + word_count?: number; + segments?: { + /** + * The starting time of the segment within the audio, in seconds. + */ + start?: number; + /** + * The ending time of the segment within the audio, in seconds. + */ + end?: number; + /** + * The transcription of the segment. + */ + text?: string; + /** + * The temperature used in the decoding process, controlling randomness in predictions. Lower values result in more deterministic outputs. + */ + temperature?: number; + /** + * The average log probability of the predictions for the words in this segment, indicating overall confidence. + */ + avg_logprob?: number; + /** + * The compression ratio of the input to the output, measuring how much the text was compressed during the transcription process. + */ + compression_ratio?: number; + /** + * The probability that the segment contains no speech, represented as a decimal between 0 and 1. + */ + no_speech_prob?: number; + words?: { + /** + * The individual word transcribed from the audio. + */ + word?: string; + /** + * The starting time of the word within the audio, in seconds. + */ + start?: number; + /** + * The ending time of the word within the audio, in seconds. + */ + end?: number; + }[]; + }[]; + /** + * The transcription in WebVTT format, which includes timing and text information for use in subtitles. + */ + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper_Large_V3_Turbo { + inputs: Ai_Cf_Openai_Whisper_Large_V3_Turbo_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Large_V3_Turbo_Output; +} +type Ai_Cf_Baai_Bge_M3_Input = Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts | Ai_Cf_Baai_Bge_M3_Input_Embedding | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: (Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts_1 | Ai_Cf_Baai_Bge_M3_Input_Embedding_1)[]; +}; +interface Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts { + /** + * A query you wish to perform against the provided contexts. If no query is provided the model with respond with embeddings for contexts + */ + query?: string; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_Embedding { + text: string | string[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts_1 { + /** + * A query you wish to perform against the provided contexts. If no query is provided the model with respond with embeddings for contexts + */ + query?: string; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_Embedding_1 { + text: string | string[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +type Ai_Cf_Baai_Bge_M3_Output = Ai_Cf_Baai_Bge_M3_Output_Query | Ai_Cf_Baai_Bge_M3_Output_EmbeddingFor_Contexts | Ai_Cf_Baai_Bge_M3_Output_Embedding | Ai_Cf_Baai_Bge_M3_AsyncResponse; +interface Ai_Cf_Baai_Bge_M3_Output_Query { + response?: { + /** + * Index of the context in the request + */ + id?: number; + /** + * Score of the context under the index. + */ + score?: number; + }[]; +} +interface Ai_Cf_Baai_Bge_M3_Output_EmbeddingFor_Contexts { + response?: number[][]; + shape?: number[]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} +interface Ai_Cf_Baai_Bge_M3_Output_Embedding { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} +interface Ai_Cf_Baai_Bge_M3_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_M3 { + inputs: Ai_Cf_Baai_Bge_M3_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_M3_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * The number of diffusion steps; higher values can improve quality but take longer. + */ + steps?: number; +} +interface Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Output { + /** + * The generated image in Base64 format. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_1_Schnell { + inputs: Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Output; +} +type Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Input = Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Prompt | Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Messages; +interface Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + image?: number[] | (string & NonNullable); + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; +} +interface Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + image?: number[] | (string & NonNullable); + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * If true, the response will be streamed back incrementally. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Controls the creativity of the AI's responses by adjusting how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Output = { + /** + * The generated text response from the model + */ + response?: string; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct { + inputs: Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Output; +} +type Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Input = Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Prompt | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Messages | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Async_Batch; +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Async_Batch { + requests?: { + /** + * User-supplied reference. This field will be present in the response as well it can be used to reference the request and response. It's NOT validated to be unique. + */ + external_reference?: string; + /** + * Prompt for the text generation model + */ + prompt?: string; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_2; + }[]; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +} | string | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_AsyncResponse; +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast { + inputs: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Output; +} +interface Ai_Cf_Meta_Llama_Guard_3_8B_Input { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender must alternate between 'user' and 'assistant'. + */ + role: "user" | "assistant"; + /** + * The content of the message as a string. + */ + content: string; + }[]; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Dictate the output format of the generated response. + */ + response_format?: { + /** + * Set to json_object to process and output generated text as JSON. + */ + type?: string; + }; +} +interface Ai_Cf_Meta_Llama_Guard_3_8B_Output { + response?: string | { + /** + * Whether the conversation is safe or not. + */ + safe?: boolean; + /** + * A list of what hazard categories predicted for the conversation, if the conversation is deemed unsafe. + */ + categories?: string[]; + }; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +declare abstract class Base_Ai_Cf_Meta_Llama_Guard_3_8B { + inputs: Ai_Cf_Meta_Llama_Guard_3_8B_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_Guard_3_8B_Output; +} +interface Ai_Cf_Baai_Bge_Reranker_Base_Input { + /** + * A query you wish to perform against the provided contexts. + */ + /** + * Number of returned results starting with the best score. + */ + top_k?: number; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; +} +interface Ai_Cf_Baai_Bge_Reranker_Base_Output { + response?: { + /** + * Index of the context in the request + */ + id?: number; + /** + * Score of the context under the index. + */ + score?: number; + }[]; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Reranker_Base { + inputs: Ai_Cf_Baai_Bge_Reranker_Base_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Reranker_Base_Output; +} +type Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Input = Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Prompt | Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Messages; +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + /** + * The content of the message as a string. + */ + content: string; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct { + inputs: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Output; +} +type Ai_Cf_Qwen_Qwq_32B_Input = Ai_Cf_Qwen_Qwq_32B_Prompt | Ai_Cf_Qwen_Qwq_32B_Messages; +interface Ai_Cf_Qwen_Qwq_32B_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwq_32B_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Qwen_Qwq_32B_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Qwen_Qwq_32B { + inputs: Ai_Cf_Qwen_Qwq_32B_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwq_32B_Output; +} +type Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Input = Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Prompt | Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Messages; +interface Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. Must be supplied for tool calls for Mistral-3. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct { + inputs: Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Input; + postProcessedOutputs: Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Output; +} +type Ai_Cf_Google_Gemma_3_12B_It_Input = Ai_Cf_Google_Gemma_3_12B_It_Prompt | Ai_Cf_Google_Gemma_3_12B_It_Messages; +interface Ai_Cf_Google_Gemma_3_12B_It_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Google_Gemma_3_12B_It_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Google_Gemma_3_12B_It_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Google_Gemma_3_12B_It { + inputs: Ai_Cf_Google_Gemma_3_12B_It_Input; + postProcessedOutputs: Ai_Cf_Google_Gemma_3_12B_It_Output; +} +type Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Input = Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Async_Batch; +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Async_Batch { + requests: (Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt_Inner | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages_Inner)[]; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt_Inner { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages_Inner { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The tool call id. + */ + id?: string; + /** + * Specifies the type of tool (e.g., 'function'). + */ + type?: string; + /** + * Details of the function tool. + */ + function?: { + /** + * The name of the tool to be called + */ + name?: string; + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + }; + }[]; +}; +declare abstract class Base_Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct { + inputs: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Output; +} +type Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Input = Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Async_Batch; +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Async_Batch { + requests: (Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt_1 | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages_1)[]; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt_1 { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_2; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages_1 { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_3; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_3 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Output = Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Chat_Completion_Response | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Text_Completion_Response | string | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_AsyncResponse; +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Chat_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "chat.completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index?: number; + /** + * The message generated by the model + */ + message?: { + /** + * Role of the message author + */ + role: string; + /** + * The content of the message + */ + content: string; + /** + * Internal reasoning content (if available) + */ + reasoning_content?: string; + /** + * Tool calls made by the assistant + */ + tool_calls?: { + /** + * Unique identifier for the tool call + */ + id: string; + /** + * Type of tool call + */ + type: "function"; + function: { + /** + * Name of the function to call + */ + name: string; + /** + * JSON string of arguments for the function + */ + arguments: string; + }; + }[]; + }; + /** + * Reason why the model stopped generating + */ + finish_reason?: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Text_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "text_completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index: number; + /** + * The generated text completion + */ + text: string; + /** + * Reason why the model stopped generating + */ + finish_reason: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8 { + inputs: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Output; +} +interface Ai_Cf_Deepgram_Nova_3_Input { + audio: { + body: object; + contentType: string; + }; + /** + * Sets how the model will interpret strings submitted to the custom_topic param. When strict, the model will only return topics submitted using the custom_topic param. When extended, the model will return its own detected topics in addition to those submitted using the custom_topic param. + */ + custom_topic_mode?: "extended" | "strict"; + /** + * Custom topics you want the model to detect within your input audio or text if present Submit up to 100 + */ + custom_topic?: string; + /** + * Sets how the model will interpret intents submitted to the custom_intent param. When strict, the model will only return intents submitted using the custom_intent param. When extended, the model will return its own detected intents in addition those submitted using the custom_intents param + */ + custom_intent_mode?: "extended" | "strict"; + /** + * Custom intents you want the model to detect within your input audio if present + */ + custom_intent?: string; + /** + * Identifies and extracts key entities from content in submitted audio + */ + detect_entities?: boolean; + /** + * Identifies the dominant language spoken in submitted audio + */ + detect_language?: boolean; + /** + * Recognize speaker changes. Each word in the transcript will be assigned a speaker number starting at 0 + */ + diarize?: boolean; + /** + * Identify and extract key entities from content in submitted audio + */ + dictation?: boolean; + /** + * Specify the expected encoding of your submitted audio + */ + encoding?: "linear16" | "flac" | "mulaw" | "amr-nb" | "amr-wb" | "opus" | "speex" | "g729"; + /** + * Arbitrary key-value pairs that are attached to the API response for usage in downstream processing + */ + extra?: string; + /** + * Filler Words can help transcribe interruptions in your audio, like 'uh' and 'um' + */ + filler_words?: boolean; + /** + * Key term prompting can boost or suppress specialized terminology and brands. + */ + keyterm?: string; + /** + * Keywords can boost or suppress specialized terminology and brands. + */ + keywords?: string; + /** + * The BCP-47 language tag that hints at the primary spoken language. Depending on the Model and API endpoint you choose only certain languages are available. + */ + language?: string; + /** + * Spoken measurements will be converted to their corresponding abbreviations. + */ + measurements?: boolean; + /** + * Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip. + */ + mip_opt_out?: boolean; + /** + * Mode of operation for the model representing broad area of topic that will be talked about in the supplied audio + */ + mode?: "general" | "medical" | "finance"; + /** + * Transcribe each audio channel independently. + */ + multichannel?: boolean; + /** + * Numerals converts numbers from written format to numerical format. + */ + numerals?: boolean; + /** + * Splits audio into paragraphs to improve transcript readability. + */ + paragraphs?: boolean; + /** + * Profanity Filter looks for recognized profanity and converts it to the nearest recognized non-profane word or removes it from the transcript completely. + */ + profanity_filter?: boolean; + /** + * Add punctuation and capitalization to the transcript. + */ + punctuate?: boolean; + /** + * Redaction removes sensitive information from your transcripts. + */ + redact?: string; + /** + * Search for terms or phrases in submitted audio and replaces them. + */ + replace?: string; + /** + * Search for terms or phrases in submitted audio. + */ + search?: string; + /** + * Recognizes the sentiment throughout a transcript or text. + */ + sentiment?: boolean; + /** + * Apply formatting to transcript output. When set to true, additional formatting will be applied to transcripts to improve readability. + */ + smart_format?: boolean; + /** + * Detect topics throughout a transcript or text. + */ + topics?: boolean; + /** + * Segments speech into meaningful semantic units. + */ + utterances?: boolean; + /** + * Seconds to wait before detecting a pause between words in submitted audio. + */ + utt_split?: number; + /** + * The number of channels in the submitted audio + */ + channels?: number; + /** + * Specifies whether the streaming endpoint should provide ongoing transcription updates as more audio is received. When set to true, the endpoint sends continuous updates, meaning transcription results may evolve over time. Note: Supported only for webosockets. + */ + interim_results?: boolean; + /** + * Indicates how long model will wait to detect whether a speaker has finished speaking or pauses for a significant period of time. When set to a value, the streaming endpoint immediately finalizes the transcription for the processed time range and returns the transcript with a speech_final parameter set to true. Can also be set to false to disable endpointing + */ + endpointing?: string; + /** + * Indicates that speech has started. You'll begin receiving Speech Started messages upon speech starting. Note: Supported only for webosockets. + */ + vad_events?: boolean; + /** + * Indicates how long model will wait to send an UtteranceEnd message after a word has been transcribed. Use with interim_results. Note: Supported only for webosockets. + */ + utterance_end_ms?: boolean; +} +interface Ai_Cf_Deepgram_Nova_3_Output { + results?: { + channels?: { + alternatives?: { + confidence?: number; + transcript?: string; + words?: { + confidence?: number; + end?: number; + start?: number; + word?: string; + }[]; + }[]; + }[]; + summary?: { + result?: string; + short?: string; + }; + sentiments?: { + segments?: { + text?: string; + start_word?: number; + end_word?: number; + sentiment?: string; + sentiment_score?: number; + }[]; + average?: { + sentiment?: string; + sentiment_score?: number; + }; + }; + }; +} +declare abstract class Base_Ai_Cf_Deepgram_Nova_3 { + inputs: Ai_Cf_Deepgram_Nova_3_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Nova_3_Output; +} +interface Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Input { + queries?: string | string[]; + /** + * Optional instruction for the task + */ + instruction?: string; + documents?: string | string[]; + text?: string | string[]; +} +interface Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Output { + data?: number[][]; + shape?: number[]; +} +declare abstract class Base_Ai_Cf_Qwen_Qwen3_Embedding_0_6B { + inputs: Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Output; +} +type Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Input = { + /** + * readable stream with audio data and content-type specified for that data + */ + audio: { + body: object; + contentType: string; + }; + /** + * type of data PCM data that's sent to the inference server as raw array + */ + dtype?: "uint8" | "float32" | "float64"; +} | { + /** + * base64 encoded audio data + */ + audio: string; + /** + * type of data PCM data that's sent to the inference server as raw array + */ + dtype?: "uint8" | "float32" | "float64"; +}; +interface Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output { + /** + * if true, end-of-turn was detected + */ + is_complete?: boolean; + /** + * probability of the end-of-turn detection + */ + probability?: number; +} +declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { + inputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Input; + postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; +} +declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { + inputs: XOR; + postProcessedOutputs: XOR; +} +declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { + inputs: XOR; + postProcessedOutputs: XOR; +} +interface Ai_Cf_Leonardo_Phoenix_1_0_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * Controls how closely the generated image should adhere to the prompt; higher values make the image more aligned with the prompt + */ + guidance?: number; + /** + * Random seed for reproducibility of the image generation + */ + seed?: number; + /** + * The height of the generated image in pixels + */ + height?: number; + /** + * The width of the generated image in pixels + */ + width?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + num_steps?: number; + /** + * Specify what to exclude from the generated images + */ + negative_prompt?: string; +} +/** + * The generated image in JPEG format + */ +type Ai_Cf_Leonardo_Phoenix_1_0_Output = string; +declare abstract class Base_Ai_Cf_Leonardo_Phoenix_1_0 { + inputs: Ai_Cf_Leonardo_Phoenix_1_0_Input; + postProcessedOutputs: Ai_Cf_Leonardo_Phoenix_1_0_Output; +} +interface Ai_Cf_Leonardo_Lucid_Origin_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * Controls how closely the generated image should adhere to the prompt; higher values make the image more aligned with the prompt + */ + guidance?: number; + /** + * Random seed for reproducibility of the image generation + */ + seed?: number; + /** + * The height of the generated image in pixels + */ + height?: number; + /** + * The width of the generated image in pixels + */ + width?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + num_steps?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + steps?: number; +} +interface Ai_Cf_Leonardo_Lucid_Origin_Output { + /** + * The generated image in Base64 format. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Leonardo_Lucid_Origin { + inputs: Ai_Cf_Leonardo_Lucid_Origin_Input; + postProcessedOutputs: Ai_Cf_Leonardo_Lucid_Origin_Output; +} +interface Ai_Cf_Deepgram_Aura_1_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "angus" | "asteria" | "arcas" | "orion" | "orpheus" | "athena" | "luna" | "zeus" | "perseus" | "helios" | "hera" | "stella"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_1_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_1 { + inputs: Ai_Cf_Deepgram_Aura_1_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_1_Output; +} +interface Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Input { + /** + * Input text to translate. Can be a single string or a list of strings. + */ + text: string | string[]; + /** + * Target langauge to translate to + */ + target_language: "asm_Beng" | "awa_Deva" | "ben_Beng" | "bho_Deva" | "brx_Deva" | "doi_Deva" | "eng_Latn" | "gom_Deva" | "gon_Deva" | "guj_Gujr" | "hin_Deva" | "hne_Deva" | "kan_Knda" | "kas_Arab" | "kas_Deva" | "kha_Latn" | "lus_Latn" | "mag_Deva" | "mai_Deva" | "mal_Mlym" | "mar_Deva" | "mni_Beng" | "mni_Mtei" | "npi_Deva" | "ory_Orya" | "pan_Guru" | "san_Deva" | "sat_Olck" | "snd_Arab" | "snd_Deva" | "tam_Taml" | "tel_Telu" | "urd_Arab" | "unr_Deva"; +} +interface Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Output { + /** + * Translated texts + */ + translations: string[]; +} +declare abstract class Base_Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B { + inputs: Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Input; + postProcessedOutputs: Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Output; +} +type Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Input = Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Async_Batch; +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Async_Batch { + requests: (Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt_1 | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages_1)[]; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt_1 { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_2; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages_1 { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_3; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_3 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Output = Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Chat_Completion_Response | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Text_Completion_Response | string | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_AsyncResponse; +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Chat_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "chat.completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index?: number; + /** + * The message generated by the model + */ + message?: { + /** + * Role of the message author + */ + role: string; + /** + * The content of the message + */ + content: string; + /** + * Internal reasoning content (if available) + */ + reasoning_content?: string; + /** + * Tool calls made by the assistant + */ + tool_calls?: { + /** + * Unique identifier for the tool call + */ + id: string; + /** + * Type of tool call + */ + type: "function"; + function: { + /** + * Name of the function to call + */ + name: string; + /** + * JSON string of arguments for the function + */ + arguments: string; + }; + }[]; + }; + /** + * Reason why the model stopped generating + */ + finish_reason?: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Text_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "text_completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index: number; + /** + * The generated text completion + */ + text: string; + /** + * Reason why the model stopped generating + */ + finish_reason: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It { + inputs: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Input; + postProcessedOutputs: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Output; +} +interface Ai_Cf_Pfnet_Plamo_Embedding_1B_Input { + /** + * Input text to embed. Can be a single string or a list of strings. + */ + text: string | string[]; +} +interface Ai_Cf_Pfnet_Plamo_Embedding_1B_Output { + /** + * Embedding vectors, where each vector is a list of floats. + */ + data: number[][]; + /** + * Shape of the embedding data as [number_of_embeddings, embedding_dimension]. + * + * @minItems 2 + * @maxItems 2 + */ + shape: [ + number, + number + ]; +} +declare abstract class Base_Ai_Cf_Pfnet_Plamo_Embedding_1B { + inputs: Ai_Cf_Pfnet_Plamo_Embedding_1B_Input; + postProcessedOutputs: Ai_Cf_Pfnet_Plamo_Embedding_1B_Output; +} +interface Ai_Cf_Deepgram_Flux_Input { + /** + * Encoding of the audio stream. Currently only supports raw signed little-endian 16-bit PCM. + */ + encoding: "linear16"; + /** + * Sample rate of the audio stream in Hz. + */ + sample_rate: string; + /** + * End-of-turn confidence required to fire an eager end-of-turn event. When set, enables EagerEndOfTurn and TurnResumed events. Valid Values 0.3 - 0.9. + */ + eager_eot_threshold?: string; + /** + * End-of-turn confidence required to finish a turn. Valid Values 0.5 - 0.9. + */ + eot_threshold?: string; + /** + * A turn will be finished when this much time has passed after speech, regardless of EOT confidence. + */ + eot_timeout_ms?: string; + /** + * Keyterm prompting can improve recognition of specialized terminology. Pass multiple keyterm query parameters to boost multiple keyterms. + */ + keyterm?: string; + /** + * Opts out requests from the Deepgram Model Improvement Program. Refer to Deepgram Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip + */ + mip_opt_out?: "true" | "false"; + /** + * Label your requests for the purpose of identification during usage reporting + */ + tag?: string; +} +/** + * Output will be returned as websocket messages. + */ +interface Ai_Cf_Deepgram_Flux_Output { + /** + * The unique identifier of the request (uuid) + */ + request_id?: string; + /** + * Starts at 0 and increments for each message the server sends to the client. + */ + sequence_id?: number; + /** + * The type of event being reported. + */ + event?: "Update" | "StartOfTurn" | "EagerEndOfTurn" | "TurnResumed" | "EndOfTurn"; + /** + * The index of the current turn + */ + turn_index?: number; + /** + * Start time in seconds of the audio range that was transcribed + */ + audio_window_start?: number; + /** + * End time in seconds of the audio range that was transcribed + */ + audio_window_end?: number; + /** + * Text that was said over the course of the current turn + */ + transcript?: string; + /** + * The words in the transcript + */ + words?: { + /** + * The individual punctuated, properly-cased word from the transcript + */ + word: string; + /** + * Confidence that this word was transcribed correctly + */ + confidence: number; + }[]; + /** + * Confidence that no more speech is coming in this turn + */ + end_of_turn_confidence?: number; +} +declare abstract class Base_Ai_Cf_Deepgram_Flux { + inputs: Ai_Cf_Deepgram_Flux_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Flux_Output; +} +interface Ai_Cf_Deepgram_Aura_2_En_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "amalthea" | "andromeda" | "apollo" | "arcas" | "aries" | "asteria" | "athena" | "atlas" | "aurora" | "callista" | "cora" | "cordelia" | "delia" | "draco" | "electra" | "harmonia" | "helena" | "hera" | "hermes" | "hyperion" | "iris" | "janus" | "juno" | "jupiter" | "luna" | "mars" | "minerva" | "neptune" | "odysseus" | "ophelia" | "orion" | "orpheus" | "pandora" | "phoebe" | "pluto" | "saturn" | "thalia" | "theia" | "vesta" | "zeus"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_2_En_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_2_En { + inputs: Ai_Cf_Deepgram_Aura_2_En_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_2_En_Output; +} +interface Ai_Cf_Deepgram_Aura_2_Es_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "sirio" | "nestor" | "carina" | "celeste" | "alvaro" | "diana" | "aquila" | "selena" | "estrella" | "javier"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_2_Es_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_2_Es { + inputs: Ai_Cf_Deepgram_Aura_2_Es_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_2_Es_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Dev { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Output; +} +declare abstract class Base_Ai_Cf_Zai_Org_Glm_4_7_Flash { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +interface AiModels { + "@cf/huggingface/distilbert-sst-2-int8": BaseAiTextClassification; + "@cf/stabilityai/stable-diffusion-xl-base-1.0": BaseAiTextToImage; + "@cf/runwayml/stable-diffusion-v1-5-inpainting": BaseAiTextToImage; + "@cf/runwayml/stable-diffusion-v1-5-img2img": BaseAiTextToImage; + "@cf/lykon/dreamshaper-8-lcm": BaseAiTextToImage; + "@cf/bytedance/stable-diffusion-xl-lightning": BaseAiTextToImage; + "@cf/myshell-ai/melotts": BaseAiTextToSpeech; + "@cf/google/embeddinggemma-300m": BaseAiTextEmbeddings; + "@cf/microsoft/resnet-50": BaseAiImageClassification; + "@cf/meta/llama-2-7b-chat-int8": BaseAiTextGeneration; + "@cf/mistral/mistral-7b-instruct-v0.1": BaseAiTextGeneration; + "@cf/meta/llama-2-7b-chat-fp16": BaseAiTextGeneration; + "@hf/thebloke/llama-2-13b-chat-awq": BaseAiTextGeneration; + "@hf/thebloke/mistral-7b-instruct-v0.1-awq": BaseAiTextGeneration; + "@hf/thebloke/zephyr-7b-beta-awq": BaseAiTextGeneration; + "@hf/thebloke/openhermes-2.5-mistral-7b-awq": BaseAiTextGeneration; + "@hf/thebloke/neural-chat-7b-v3-1-awq": BaseAiTextGeneration; + "@hf/thebloke/deepseek-coder-6.7b-base-awq": BaseAiTextGeneration; + "@hf/thebloke/deepseek-coder-6.7b-instruct-awq": BaseAiTextGeneration; + "@cf/deepseek-ai/deepseek-math-7b-instruct": BaseAiTextGeneration; + "@cf/defog/sqlcoder-7b-2": BaseAiTextGeneration; + "@cf/openchat/openchat-3.5-0106": BaseAiTextGeneration; + "@cf/tiiuae/falcon-7b-instruct": BaseAiTextGeneration; + "@cf/thebloke/discolm-german-7b-v1-awq": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-0.5b-chat": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-7b-chat-awq": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-14b-chat-awq": BaseAiTextGeneration; + "@cf/tinyllama/tinyllama-1.1b-chat-v1.0": BaseAiTextGeneration; + "@cf/microsoft/phi-2": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-1.8b-chat": BaseAiTextGeneration; + "@cf/mistral/mistral-7b-instruct-v0.2-lora": BaseAiTextGeneration; + "@hf/nousresearch/hermes-2-pro-mistral-7b": BaseAiTextGeneration; + "@hf/nexusflow/starling-lm-7b-beta": BaseAiTextGeneration; + "@hf/google/gemma-7b-it": BaseAiTextGeneration; + "@cf/meta-llama/llama-2-7b-chat-hf-lora": BaseAiTextGeneration; + "@cf/google/gemma-2b-it-lora": BaseAiTextGeneration; + "@cf/google/gemma-7b-it-lora": BaseAiTextGeneration; + "@hf/mistral/mistral-7b-instruct-v0.2": BaseAiTextGeneration; + "@cf/meta/llama-3-8b-instruct": BaseAiTextGeneration; + "@cf/fblgit/una-cybertron-7b-v2-bf16": BaseAiTextGeneration; + "@cf/meta/llama-3-8b-instruct-awq": BaseAiTextGeneration; + "@cf/meta/llama-3.1-8b-instruct-fp8": BaseAiTextGeneration; + "@cf/meta/llama-3.1-8b-instruct-awq": BaseAiTextGeneration; + "@cf/meta/llama-3.2-3b-instruct": BaseAiTextGeneration; + "@cf/meta/llama-3.2-1b-instruct": BaseAiTextGeneration; + "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b": BaseAiTextGeneration; + "@cf/ibm-granite/granite-4.0-h-micro": BaseAiTextGeneration; + "@cf/facebook/bart-large-cnn": BaseAiSummarization; + "@cf/llava-hf/llava-1.5-7b-hf": BaseAiImageToText; + "@cf/baai/bge-base-en-v1.5": Base_Ai_Cf_Baai_Bge_Base_En_V1_5; + "@cf/openai/whisper": Base_Ai_Cf_Openai_Whisper; + "@cf/meta/m2m100-1.2b": Base_Ai_Cf_Meta_M2M100_1_2B; + "@cf/baai/bge-small-en-v1.5": Base_Ai_Cf_Baai_Bge_Small_En_V1_5; + "@cf/baai/bge-large-en-v1.5": Base_Ai_Cf_Baai_Bge_Large_En_V1_5; + "@cf/unum/uform-gen2-qwen-500m": Base_Ai_Cf_Unum_Uform_Gen2_Qwen_500M; + "@cf/openai/whisper-tiny-en": Base_Ai_Cf_Openai_Whisper_Tiny_En; + "@cf/openai/whisper-large-v3-turbo": Base_Ai_Cf_Openai_Whisper_Large_V3_Turbo; + "@cf/baai/bge-m3": Base_Ai_Cf_Baai_Bge_M3; + "@cf/black-forest-labs/flux-1-schnell": Base_Ai_Cf_Black_Forest_Labs_Flux_1_Schnell; + "@cf/meta/llama-3.2-11b-vision-instruct": Base_Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct; + "@cf/meta/llama-3.3-70b-instruct-fp8-fast": Base_Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast; + "@cf/meta/llama-guard-3-8b": Base_Ai_Cf_Meta_Llama_Guard_3_8B; + "@cf/baai/bge-reranker-base": Base_Ai_Cf_Baai_Bge_Reranker_Base; + "@cf/qwen/qwen2.5-coder-32b-instruct": Base_Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct; + "@cf/qwen/qwq-32b": Base_Ai_Cf_Qwen_Qwq_32B; + "@cf/mistralai/mistral-small-3.1-24b-instruct": Base_Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct; + "@cf/google/gemma-3-12b-it": Base_Ai_Cf_Google_Gemma_3_12B_It; + "@cf/meta/llama-4-scout-17b-16e-instruct": Base_Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct; + "@cf/qwen/qwen3-30b-a3b-fp8": Base_Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8; + "@cf/deepgram/nova-3": Base_Ai_Cf_Deepgram_Nova_3; + "@cf/qwen/qwen3-embedding-0.6b": Base_Ai_Cf_Qwen_Qwen3_Embedding_0_6B; + "@cf/pipecat-ai/smart-turn-v2": Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2; + "@cf/openai/gpt-oss-120b": Base_Ai_Cf_Openai_Gpt_Oss_120B; + "@cf/openai/gpt-oss-20b": Base_Ai_Cf_Openai_Gpt_Oss_20B; + "@cf/leonardo/phoenix-1.0": Base_Ai_Cf_Leonardo_Phoenix_1_0; + "@cf/leonardo/lucid-origin": Base_Ai_Cf_Leonardo_Lucid_Origin; + "@cf/deepgram/aura-1": Base_Ai_Cf_Deepgram_Aura_1; + "@cf/ai4bharat/indictrans2-en-indic-1B": Base_Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B; + "@cf/aisingapore/gemma-sea-lion-v4-27b-it": Base_Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It; + "@cf/pfnet/plamo-embedding-1b": Base_Ai_Cf_Pfnet_Plamo_Embedding_1B; + "@cf/deepgram/flux": Base_Ai_Cf_Deepgram_Flux; + "@cf/deepgram/aura-2-en": Base_Ai_Cf_Deepgram_Aura_2_En; + "@cf/deepgram/aura-2-es": Base_Ai_Cf_Deepgram_Aura_2_Es; + "@cf/black-forest-labs/flux-2-dev": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Dev; + "@cf/black-forest-labs/flux-2-klein-4b": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B; + "@cf/black-forest-labs/flux-2-klein-9b": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B; + "@cf/zai-org/glm-4.7-flash": Base_Ai_Cf_Zai_Org_Glm_4_7_Flash; + "@cf/moonshotai/kimi-k2.5": Base_Ai_Cf_Moonshotai_Kimi_K2_5; + "@cf/nvidia/nemotron-3-120b-a12b": Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B; +} +type AiOptions = { + /** + * Send requests as an asynchronous batch job, only works for supported models + * https://developers.cloudflare.com/workers-ai/features/batch-api + */ + queueRequest?: boolean; + /** + * Establish websocket connections, only works for supported models + */ + websocket?: boolean; + /** + * Tag your requests to group and view them in Cloudflare dashboard. + * + * Rules: + * Tags must only contain letters, numbers, and the symbols: : - . / @ + * Each tag can have maximum 50 characters. + * Maximum 5 tags are allowed each request. + * Duplicate tags will removed. + */ + tags?: string[]; + gateway?: GatewayOptions; + returnRawResponse?: boolean; + prefix?: string; + extraHeaders?: object; + signal?: AbortSignal; +}; +type AiModelsSearchParams = { + author?: string; + hide_experimental?: boolean; + page?: number; + per_page?: number; + search?: string; + source?: number; + task?: string; +}; +type AiModelsSearchObject = { + id: string; + source: number; + name: string; + description: string; + task: { + id: string; + name: string; + description: string; + }; + tags: string[]; + properties: { + property_id: string; + value: string; + }[]; +}; +type ChatCompletionsBase = XOR; +type ChatCompletionsInput = XOR; +interface InferenceUpstreamError extends Error { +} +interface AiInternalError extends Error { +} +type AiModelListType = Record; +type AiAsyncBatchResponse = { + request_id: string; +}; +declare abstract class Ai { + aiGatewayLogId: string | null; + gateway(gatewayId: string): AiGateway; + /** + * @deprecated Use the standalone `ai_search_namespaces` or `ai_search` Workers bindings instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(): AiSearchNamespace; + /** + * @deprecated AutoRAG has been replaced by AI Search. + * Use the standalone `ai_search_namespaces` or `ai_search` Workers bindings instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + * + * @param autoragId Instance ID + */ + autorag(autoragId: string): AutoRAG; + // Batch request + run(model: Name, inputs: { + requests: AiModelList[Name]['inputs'][]; + }, options: AiOptions & { + queueRequest: true; + }): Promise; + // Raw response + run(model: Name, inputs: AiModelList[Name]['inputs'], options: AiOptions & { + returnRawResponse: true; + }): Promise; + // WebSocket + run(model: Name, inputs: AiModelList[Name]['inputs'], options: AiOptions & { + websocket: true; + }): Promise; + // Streaming + run(model: Name, inputs: AiModelList[Name]['inputs'] & { + stream: true; + }, options?: AiOptions): Promise; + // Normal (default) - known model + run(model: Name, inputs: AiModelList[Name]['inputs'], options?: AiOptions): Promise; + // Unknown model (gateway fallback) + run(model: string & {}, inputs: Record, options?: AiOptions): Promise>; + models(params?: AiModelsSearchParams): Promise; + toMarkdown(): ToMarkdownService; + toMarkdown(files: MarkdownDocument[], options?: ConversionRequestOptions): Promise; + toMarkdown(files: MarkdownDocument, options?: ConversionRequestOptions): Promise; +} +type GatewayRetries = { + maxAttempts?: 1 | 2 | 3 | 4 | 5; + retryDelayMs?: number; + backoff?: 'constant' | 'linear' | 'exponential'; +}; +type GatewayOptions = { + id: string; + cacheKey?: string; + cacheTtl?: number; + skipCache?: boolean; + metadata?: Record; + collectLog?: boolean; + eventId?: string; + requestTimeoutMs?: number; + retries?: GatewayRetries; +}; +type UniversalGatewayOptions = Exclude & { + /** + ** @deprecated + */ + id?: string; +}; +type AiGatewayPatchLog = { + score?: number | null; + feedback?: -1 | 1 | null; + metadata?: Record | null; +}; +type AiGatewayLog = { + id: string; + provider: string; + model: string; + model_type?: string; + path: string; + duration: number; + request_type?: string; + request_content_type?: string; + status_code: number; + response_content_type?: string; + success: boolean; + cached: boolean; + tokens_in?: number; + tokens_out?: number; + metadata?: Record; + step?: number; + cost?: number; + custom_cost?: boolean; + request_size: number; + request_head?: string; + request_head_complete: boolean; + response_size: number; + response_head?: string; + response_head_complete: boolean; + created_at: Date; +}; +type AIGatewayProviders = 'workers-ai' | 'anthropic' | 'aws-bedrock' | 'azure-openai' | 'google-vertex-ai' | 'huggingface' | 'openai' | 'perplexity-ai' | 'replicate' | 'groq' | 'cohere' | 'google-ai-studio' | 'mistral' | 'grok' | 'openrouter' | 'deepseek' | 'cerebras' | 'cartesia' | 'elevenlabs' | 'adobe-firefly'; +type AIGatewayHeaders = { + 'cf-aig-metadata': Record | string; + 'cf-aig-custom-cost': { + per_token_in?: number; + per_token_out?: number; + } | { + total_cost?: number; + } | string; + 'cf-aig-cache-ttl': number | string; + 'cf-aig-skip-cache': boolean | string; + 'cf-aig-cache-key': string; + 'cf-aig-event-id': string; + 'cf-aig-request-timeout': number | string; + 'cf-aig-max-attempts': number | string; + 'cf-aig-retry-delay': number | string; + 'cf-aig-backoff': string; + 'cf-aig-collect-log': boolean | string; + Authorization: string; + 'Content-Type': string; + [key: string]: string | number | boolean | object; +}; +type AIGatewayUniversalRequest = { + provider: AIGatewayProviders | string; // eslint-disable-line + endpoint: string; + headers: Partial; + query: unknown; +}; +interface AiGatewayInternalError extends Error { +} +interface AiGatewayLogNotFound extends Error { +} +declare abstract class AiGateway { + patchLog(logId: string, data: AiGatewayPatchLog): Promise; + getLog(logId: string): Promise; + run(data: AIGatewayUniversalRequest | AIGatewayUniversalRequest[], options?: { + gateway?: UniversalGatewayOptions; + extraHeaders?: object; + signal?: AbortSignal; + }): Promise; + getUrl(provider?: AIGatewayProviders | string): Promise; // eslint-disable-line +} +// Copyright (c) 2022-2025 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 +/** + * Artifacts — Git-compatible file storage on Cloudflare Workers. + * + * Provides programmatic access to create, manage, and fork repositories, + * and to issue and revoke scoped access tokens. + */ +/** Information about a repository. */ +interface ArtifactsRepoInfo { + /** Unique repository ID. */ + id: string; + /** Repository name. */ + name: string; + /** Repository description, or null if not set. */ + description: string | null; + /** Default branch name (e.g. "main"). */ + defaultBranch: string; + /** ISO 8601 creation timestamp. */ + createdAt: string; + /** ISO 8601 last-updated timestamp. */ + updatedAt: string; + /** ISO 8601 timestamp of the last push, or null if never pushed. */ + lastPushAt: string | null; + /** Fork source (e.g. "github:owner/repo", "artifacts:namespace/repo"), or null if not a fork. */ + source: string | null; + /** Whether the repository is read-only. */ + readOnly: boolean; + /** HTTPS git remote URL. */ + remote: string; +} +/** Result of creating a repository — includes the initial access token. */ +interface ArtifactsCreateRepoResult { + /** Unique repository ID. */ + id: string; + /** Repository name. */ + name: string; + /** Repository description, or null if not set. */ + description: string | null; + /** Default branch name. */ + defaultBranch: string; + /** HTTPS git remote URL. */ + remote: string; + /** Plaintext access token (only returned at creation time). */ + token: string; + /** ISO 8601 token expiry timestamp. */ + tokenExpiresAt: string; +} +/** Paginated list of repositories. */ +interface ArtifactsRepoListResult { + /** Repositories in this page (without the `remote` field). */ + repos: Omit[]; + /** Total number of repositories in the namespace. */ + total: number; + /** Cursor for the next page, if there are more results. */ + cursor?: string; +} +/** Result of creating an access token. */ +interface ArtifactsCreateTokenResult { + /** Unique token ID. */ + id: string; + /** Plaintext token (only returned at creation time). */ + plaintext: string; + /** Token scope: "read" or "write". */ + scope: 'read' | 'write'; + /** ISO 8601 token expiry timestamp. */ + expiresAt: string; +} +/** Token metadata (no plaintext). */ +interface ArtifactsTokenInfo { + /** Unique token ID. */ + id: string; + /** Token scope: "read" or "write". */ + scope: 'read' | 'write'; + /** Token state: "active", "expired", or "revoked". */ + state: 'active' | 'expired' | 'revoked'; + /** ISO 8601 creation timestamp. */ + createdAt: string; + /** ISO 8601 expiry timestamp. */ + expiresAt: string; +} +/** Paginated list of tokens for a repository. */ +interface ArtifactsTokenListResult { + /** Tokens in this page. */ + tokens: ArtifactsTokenInfo[]; + /** Total number of tokens for the repository. */ + total: number; +} +/** Handle for a single repository. Returned by Artifacts.get(). */ +interface ArtifactsRepo extends ArtifactsRepoInfo { + /** + * Create an access token for this repo. + * @param scope Token scope: "write" (default) or "read". + * @param ttl Time-to-live in seconds (default 86400, min 60, max 31536000). + */ + createToken(scope?: 'write' | 'read', ttl?: number): Promise; + /** List tokens for this repo (metadata only, no plaintext). */ + listTokens(): Promise; + /** + * Revoke a token by plaintext or ID. + * @param tokenOrId Plaintext token or token ID. + * @returns true if revoked, false if not found. + */ + revokeToken(tokenOrId: string): Promise; + // ── Fork ── + /** + * Fork this repo to a new repo. + * @param name Target repository name. + * @param opts Optional: description, readOnly flag, defaultBranchOnly (default true). + */ + fork(name: string, opts?: { + description?: string; + readOnly?: boolean; + defaultBranchOnly?: boolean; + }): Promise; +} +/** Artifacts binding — namespace-level operations. */ +interface Artifacts { + /** + * Create a new repository with an initial access token. + * @param name Repository name (alphanumeric, dots, hyphens, underscores). + * @param opts Optional: readOnly flag, description, default branch name. + * @returns Repo metadata with initial token. + */ + create(name: string, opts?: { + readOnly?: boolean; + description?: string; + setDefaultBranch?: string; + }): Promise; + /** + * Get a handle to an existing repository. + * @param name Repository name. + * @returns Repo handle. + */ + get(name: string): Promise; + /** + * Import a repository from an external git remote. + * @param params Source URL and optional branch/depth, plus target name and options. + * @returns Repo metadata with initial token. + */ + import(params: { + source: { + url: string; + branch?: string; + depth?: number; + }; + target: { + name: string; + opts?: { + description?: string; + readOnly?: boolean; + }; + }; + }): Promise; + /** + * List repositories with cursor-based pagination. + * @param opts Optional: limit (1–200, default 50), cursor for next page. + */ + list(opts?: { + limit?: number; + cursor?: string; + }): Promise; + /** + * Delete a repository and all associated tokens. + * @param name Repository name. + * @returns true if deleted, false if not found. + */ + delete(name: string): Promise; +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGInternalError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGNotFoundError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGUnauthorizedError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGNameNotSetError extends Error { +} +type ComparisonFilter = { + key: string; + type: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte'; + value: string | number | boolean; +}; +type CompoundFilter = { + type: 'and' | 'or'; + filters: ComparisonFilter[]; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagSearchRequest = { + query: string; + filters?: CompoundFilter | ComparisonFilter; + max_num_results?: number; + ranking_options?: { + ranker?: string; + score_threshold?: number; + }; + reranking?: { + enabled?: boolean; + model?: string; + }; + rewrite_query?: boolean; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchRequest = AutoRagSearchRequest & { + stream?: boolean; + system_prompt?: string; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchRequestStreaming = Omit & { + stream: true; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagSearchResponse = { + object: 'vector_store.search_results.page'; + search_query: string; + data: { + file_id: string; + filename: string; + score: number; + attributes: Record; + content: { + type: 'text'; + text: string; + }[]; + }[]; + has_more: boolean; + next_page: string | null; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagListResponse = { + id: string; + enable: boolean; + type: string; + source: string; + vectorize_name: string; + paused: boolean; + status: string; +}[]; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchResponse = AutoRagSearchResponse & { + response: string; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +declare abstract class AutoRAG { + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + list(): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + search(params: AutoRagSearchRequest): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequestStreaming): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequest): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequest): Promise; +} +interface BasicImageTransformations { + /** + * Maximum width in image pixels. The value must be an integer. + */ + width?: number; + /** + * Maximum height in image pixels. The value must be an integer. + */ + height?: number; + /** + * Resizing mode as a string. It affects interpretation of width and height + * options: + * - scale-down: Similar to contain, but the image is never enlarged. If + * the image is larger than given width or height, it will be resized. + * Otherwise its original size will be kept. + * - contain: Resizes to maximum size that fits within the given width and + * height. If only a single dimension is given (e.g. only width), the + * image will be shrunk or enlarged to exactly match that dimension. + * Aspect ratio is always preserved. + * - cover: Resizes (shrinks or enlarges) to fill the entire area of width + * and height. If the image has an aspect ratio different from the ratio + * of width and height, it will be cropped to fit. + * - crop: The image will be shrunk and cropped to fit within the area + * specified by width and height. The image will not be enlarged. For images + * smaller than the given dimensions it's the same as scale-down. For + * images larger than the given dimensions, it's the same as cover. + * See also trim. + * - pad: Resizes to the maximum size that fits within the given width and + * height, and then fills the remaining area with a background color + * (white by default). Use of this mode is not recommended, as the same + * effect can be more efficiently achieved with the contain mode and the + * CSS object-fit: contain property. + * - squeeze: Stretches and deforms to the width and height given, even if it + * breaks aspect ratio + */ + fit?: "scale-down" | "contain" | "cover" | "crop" | "pad" | "squeeze"; + /** + * Image segmentation using artificial intelligence models. Sets pixels not + * within selected segment area to transparent e.g "foreground" sets every + * background pixel as transparent. + */ + segment?: "foreground"; + /** + * When cropping with fit: "cover", this defines the side or point that should + * be left uncropped. The value is either a string + * "left", "right", "top", "bottom", "auto", or "center" (the default), + * or an object {x, y} containing focal point coordinates in the original + * image expressed as fractions ranging from 0.0 (top or left) to 1.0 + * (bottom or right), 0.5 being the center. {fit: "cover", gravity: "top"} will + * crop bottom or left and right sides as necessary, but won’t crop anything + * from the top. {fit: "cover", gravity: {x:0.5, y:0.2}} will crop each side to + * preserve as much as possible around a point at 20% of the height of the + * source image. + */ + gravity?: 'face' | 'left' | 'right' | 'top' | 'bottom' | 'center' | 'auto' | 'entropy' | BasicImageTransformationsGravityCoordinates; + /** + * Background color to add underneath the image. Applies only to images with + * transparency (such as PNG). Accepts any CSS color (#RRGGBB, rgba(…), + * hsl(…), etc.) + */ + background?: string; + /** + * Number of degrees (90, 180, 270) to rotate the image by. width and height + * options refer to axes after rotation. + */ + rotate?: 0 | 90 | 180 | 270 | 360; +} +interface BasicImageTransformationsGravityCoordinates { + x?: number; + y?: number; + mode?: 'remainder' | 'box-center'; +} +/** + * In addition to the properties you can set in the RequestInit dict + * that you pass as an argument to the Request constructor, you can + * set certain properties of a `cf` object to control how Cloudflare + * features are applied to that new Request. + * + * Note: Currently, these properties cannot be tested in the + * playground. + */ +interface RequestInitCfProperties extends Record { + cacheEverything?: boolean; + /** + * A request's cache key is what determines if two requests are + * "the same" for caching purposes. If a request has the same cache key + * as some previous request, then we can serve the same cached response for + * both. (e.g. 'some-key') + * + * Only available for Enterprise customers. + */ + cacheKey?: string; + /** + * This allows you to append additional Cache-Tag response headers + * to the origin response without modifications to the origin server. + * This will allow for greater control over the Purge by Cache Tag feature + * utilizing changes only in the Workers process. + * + * Only available for Enterprise customers. + */ + cacheTags?: string[]; + /** + * Force response to be cached for a given number of seconds. (e.g. 300) + */ + cacheTtl?: number; + /** + * Force response to be cached for a given number of seconds based on the Origin status code. + * (e.g. { '200-299': 86400, '404': 1, '500-599': 0 }) + */ + cacheTtlByStatus?: Record; + /** + * Explicit Cache-Control header value to set on the response stored in cache. + * This gives full control over cache directives (e.g. 'public, max-age=3600, s-maxage=86400'). + * + * Cannot be used together with `cacheTtl` or the `cache` request option (`no-store`/`no-cache`), + * as these are mutually exclusive cache control mechanisms. Setting both will throw a TypeError. + * + * Can be used together with `cacheTtlByStatus`. + */ + cacheControl?: string; + /** + * Whether the response should be eligible for Cache Reserve storage. + */ + cacheReserveEligible?: boolean; + /** + * Whether to respect strong ETags (as opposed to weak ETags) from the origin. + */ + respectStrongEtag?: boolean; + /** + * Whether to strip ETag headers from the origin response before caching. + */ + stripEtags?: boolean; + /** + * Whether to strip Last-Modified headers from the origin response before caching. + */ + stripLastModified?: boolean; + /** + * Whether to enable Cache Deception Armor, which protects against web cache + * deception attacks by verifying the Content-Type matches the URL extension. + */ + cacheDeceptionArmor?: boolean; + /** + * Minimum file size in bytes for a response to be eligible for Cache Reserve storage. + */ + cacheReserveMinimumFileSize?: number; + scrapeShield?: boolean; + apps?: boolean; + image?: RequestInitCfPropertiesImage; + minify?: RequestInitCfPropertiesImageMinify; + mirage?: boolean; + polish?: "lossy" | "lossless" | "off"; + r2?: RequestInitCfPropertiesR2; + /** + * Redirects the request to an alternate origin server. You can use this, + * for example, to implement load balancing across several origins. + * (e.g.us-east.example.com) + * + * Note - For security reasons, the hostname set in resolveOverride must + * be proxied on the same Cloudflare zone of the incoming request. + * Otherwise, the setting is ignored. CNAME hosts are allowed, so to + * resolve to a host under a different domain or a DNS only domain first + * declare a CNAME record within your own zone’s DNS mapping to the + * external hostname, set proxy on Cloudflare, then set resolveOverride + * to point to that CNAME record. + */ + resolveOverride?: string; +} +interface RequestInitCfPropertiesImageDraw extends BasicImageTransformations { + /** + * Absolute URL of the image file to use for the drawing. It can be any of + * the supported file formats. For drawing of watermarks or non-rectangular + * overlays we recommend using PNG or WebP images. + */ + url: string; + /** + * Floating-point number between 0 (transparent) and 1 (opaque). + * For example, opacity: 0.5 makes overlay semitransparent. + */ + opacity?: number; + /** + * - If set to true, the overlay image will be tiled to cover the entire + * area. This is useful for stock-photo-like watermarks. + * - If set to "x", the overlay image will be tiled horizontally only + * (form a line). + * - If set to "y", the overlay image will be tiled vertically only + * (form a line). + */ + repeat?: true | "x" | "y"; + /** + * Position of the overlay image relative to a given edge. Each property is + * an offset in pixels. 0 aligns exactly to the edge. For example, left: 10 + * positions left side of the overlay 10 pixels from the left edge of the + * image it's drawn over. bottom: 0 aligns bottom of the overlay with bottom + * of the background image. + * + * Setting both left & right, or both top & bottom is an error. + * + * If no position is specified, the image will be centered. + */ + top?: number; + left?: number; + bottom?: number; + right?: number; +} +interface RequestInitCfPropertiesImage extends BasicImageTransformations { + /** + * Device Pixel Ratio. Default 1. Multiplier for width/height that makes it + * easier to specify higher-DPI sizes in . + */ + dpr?: number; + /** + * Allows you to trim your image. Takes dpr into account and is performed before + * resizing or rotation. + * + * It can be used as: + * - left, top, right, bottom - it will specify the number of pixels to cut + * off each side + * - width, height - the width/height you'd like to end up with - can be used + * in combination with the properties above + * - border - this will automatically trim the surroundings of an image based on + * it's color. It consists of three properties: + * - color: rgb or hex representation of the color you wish to trim (todo: verify the rgba bit) + * - tolerance: difference from color to treat as color + * - keep: the number of pixels of border to keep + */ + trim?: "border" | { + top?: number; + bottom?: number; + left?: number; + right?: number; + width?: number; + height?: number; + border?: boolean | { + color?: string; + tolerance?: number; + keep?: number; + }; + }; + /** + * Quality setting from 1-100 (useful values are in 60-90 range). Lower values + * make images look worse, but load faster. The default is 85. It applies only + * to JPEG and WebP images. It doesn’t have any effect on PNG. + */ + quality?: number | "low" | "medium-low" | "medium-high" | "high"; + /** + * Output format to generate. It can be: + * - avif: generate images in AVIF format. + * - webp: generate images in Google WebP format. Set quality to 100 to get + * the WebP-lossless format. + * - json: instead of generating an image, outputs information about the + * image, in JSON format. The JSON object will contain image size + * (before and after resizing), source image’s MIME type, file size, etc. + * - jpeg: generate images in JPEG format. + * - png: generate images in PNG format. + */ + format?: "avif" | "webp" | "json" | "jpeg" | "png" | "baseline-jpeg" | "png-force" | "svg"; + /** + * Whether to preserve animation frames from input files. Default is true. + * Setting it to false reduces animations to still images. This setting is + * recommended when enlarging images or processing arbitrary user content, + * because large GIF animations can weigh tens or even hundreds of megabytes. + * It is also useful to set anim:false when using format:"json" to get the + * response quicker without the number of frames. + */ + anim?: boolean; + /** + * What EXIF data should be preserved in the output image. Note that EXIF + * rotation and embedded color profiles are always applied ("baked in" into + * the image), and aren't affected by this option. Note that if the Polish + * feature is enabled, all metadata may have been removed already and this + * option may have no effect. + * - keep: Preserve most of EXIF metadata, including GPS location if there's + * any. + * - copyright: Only keep the copyright tag, and discard everything else. + * This is the default behavior for JPEG files. + * - none: Discard all invisible EXIF metadata. Currently WebP and PNG + * output formats always discard metadata. + */ + metadata?: "keep" | "copyright" | "none"; + /** + * Strength of sharpening filter to apply to the image. Floating-point + * number between 0 (no sharpening, default) and 10 (maximum). 1.0 is a + * recommended value for downscaled images. + */ + sharpen?: number; + /** + * Radius of a blur filter (approximate gaussian). Maximum supported radius + * is 250. + */ + blur?: number; + /** + * Overlays are drawn in the order they appear in the array (last array + * entry is the topmost layer). + */ + draw?: RequestInitCfPropertiesImageDraw[]; + /** + * Fetching image from authenticated origin. Setting this property will + * pass authentication headers (Authorization, Cookie, etc.) through to + * the origin. + */ + "origin-auth"?: "share-publicly"; + /** + * Adds a border around the image. The border is added after resizing. Border + * width takes dpr into account, and can be specified either using a single + * width property, or individually for each side. + */ + border?: { + color: string; + width: number; + } | { + color: string; + top: number; + right: number; + bottom: number; + left: number; + }; + /** + * Increase brightness by a factor. A value of 1.0 equals no change, a value + * of 0.5 equals half brightness, and a value of 2.0 equals twice as bright. + * 0 is ignored. + */ + brightness?: number; + /** + * Increase contrast by a factor. A value of 1.0 equals no change, a value of + * 0.5 equals low contrast, and a value of 2.0 equals high contrast. 0 is + * ignored. + */ + contrast?: number; + /** + * Increase exposure by a factor. A value of 1.0 equals no change, a value of + * 0.5 darkens the image, and a value of 2.0 lightens the image. 0 is ignored. + */ + gamma?: number; + /** + * Increase contrast by a factor. A value of 1.0 equals no change, a value of + * 0.5 equals low contrast, and a value of 2.0 equals high contrast. 0 is + * ignored. + */ + saturation?: number; + /** + * Flips the images horizontally, vertically, or both. Flipping is applied before + * rotation, so if you apply flip=h,rotate=90 then the image will be flipped + * horizontally, then rotated by 90 degrees. + */ + flip?: 'h' | 'v' | 'hv'; + /** + * Slightly reduces latency on a cache miss by selecting a + * quickest-to-compress file format, at a cost of increased file size and + * lower image quality. It will usually override the format option and choose + * JPEG over WebP or AVIF. We do not recommend using this option, except in + * unusual circumstances like resizing uncacheable dynamically-generated + * images. + */ + compression?: "fast"; +} +interface RequestInitCfPropertiesImageMinify { + javascript?: boolean; + css?: boolean; + html?: boolean; +} +interface RequestInitCfPropertiesR2 { + /** + * Colo id of bucket that an object is stored in + */ + bucketColoId?: number; +} +/** + * Request metadata provided by Cloudflare's edge. + */ +type IncomingRequestCfProperties = IncomingRequestCfPropertiesBase & IncomingRequestCfPropertiesBotManagementEnterprise & IncomingRequestCfPropertiesCloudflareForSaaSEnterprise & IncomingRequestCfPropertiesGeographicInformation & IncomingRequestCfPropertiesCloudflareAccessOrApiShield; +interface IncomingRequestCfPropertiesBase extends Record { + /** + * [ASN](https://www.iana.org/assignments/as-numbers/as-numbers.xhtml) of the incoming request. + * + * @example 395747 + */ + asn?: number; + /** + * The organization which owns the ASN of the incoming request. + * + * @example "Google Cloud" + */ + asOrganization?: string; + /** + * The original value of the `Accept-Encoding` header if Cloudflare modified it. + * + * @example "gzip, deflate, br" + */ + clientAcceptEncoding?: string; + /** + * The number of milliseconds it took for the request to reach your worker. + * + * @example 22 + */ + clientTcpRtt?: number; + /** + * The three-letter [IATA](https://en.wikipedia.org/wiki/IATA_airport_code) + * airport code of the data center that the request hit. + * + * @example "DFW" + */ + colo: string; + /** + * Represents the upstream's response to a + * [TCP `keepalive` message](https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html) + * from cloudflare. + * + * For workers with no upstream, this will always be `1`. + * + * @example 3 + */ + edgeRequestKeepAliveStatus: IncomingRequestCfPropertiesEdgeRequestKeepAliveStatus; + /** + * The HTTP Protocol the request used. + * + * @example "HTTP/2" + */ + httpProtocol: string; + /** + * The browser-requested prioritization information in the request object. + * + * If no information was set, defaults to the empty string `""` + * + * @example "weight=192;exclusive=0;group=3;group-weight=127" + * @default "" + */ + requestPriority: string; + /** + * The TLS version of the connection to Cloudflare. + * In requests served over plaintext (without TLS), this property is the empty string `""`. + * + * @example "TLSv1.3" + */ + tlsVersion: string; + /** + * The cipher for the connection to Cloudflare. + * In requests served over plaintext (without TLS), this property is the empty string `""`. + * + * @example "AEAD-AES128-GCM-SHA256" + */ + tlsCipher: string; + /** + * Metadata containing the [`HELLO`](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.1.2) and [`FINISHED`](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.9) messages from this request's TLS handshake. + * + * If the incoming request was served over plaintext (without TLS) this field is undefined. + */ + tlsExportedAuthenticator?: IncomingRequestCfPropertiesExportedAuthenticatorMetadata; +} +interface IncomingRequestCfPropertiesBotManagementBase { + /** + * Cloudflare’s [level of certainty](https://developers.cloudflare.com/bots/concepts/bot-score/) that a request comes from a bot, + * represented as an integer percentage between `1` (almost certainly a bot) and `99` (almost certainly human). + * + * @example 54 + */ + score: number; + /** + * A boolean value that is true if the request comes from a good bot, like Google or Bing. + * Most customers choose to allow this traffic. For more details, see [Traffic from known bots](https://developers.cloudflare.com/firewall/known-issues-and-faq/#how-does-firewall-rules-handle-traffic-from-known-bots). + */ + verifiedBot: boolean; + /** + * A boolean value that is true if the request originates from a + * Cloudflare-verified proxy service. + */ + corporateProxy: boolean; + /** + * A boolean value that's true if the request matches [file extensions](https://developers.cloudflare.com/bots/reference/static-resources/) for many types of static resources. + */ + staticResource: boolean; + /** + * List of IDs that correlate to the Bot Management heuristic detections made on a request (you can have multiple heuristic detections on the same request). + */ + detectionIds: number[]; +} +interface IncomingRequestCfPropertiesBotManagement { + /** + * Results of Cloudflare's Bot Management analysis + */ + botManagement: IncomingRequestCfPropertiesBotManagementBase; + /** + * Duplicate of `botManagement.score`. + * + * @deprecated + */ + clientTrustScore: number; +} +interface IncomingRequestCfPropertiesBotManagementEnterprise extends IncomingRequestCfPropertiesBotManagement { + /** + * Results of Cloudflare's Bot Management analysis + */ + botManagement: IncomingRequestCfPropertiesBotManagementBase & { + /** + * A [JA3 Fingerprint](https://developers.cloudflare.com/bots/concepts/ja3-fingerprint/) to help profile specific SSL/TLS clients + * across different destination IPs, Ports, and X509 certificates. + */ + ja3Hash: string; + }; +} +interface IncomingRequestCfPropertiesCloudflareForSaaSEnterprise { + /** + * Custom metadata set per-host in [Cloudflare for SaaS](https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/). + * + * This field is only present if you have Cloudflare for SaaS enabled on your account + * and you have followed the [required steps to enable it]((https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata/)). + */ + hostMetadata?: HostMetadata; +} +interface IncomingRequestCfPropertiesCloudflareAccessOrApiShield { + /** + * Information about the client certificate presented to Cloudflare. + * + * This is populated when the incoming request is served over TLS using + * either Cloudflare Access or API Shield (mTLS) + * and the presented SSL certificate has a valid + * [Certificate Serial Number](https://ldapwiki.com/wiki/Certificate%20Serial%20Number) + * (i.e., not `null` or `""`). + * + * Otherwise, a set of placeholder values are used. + * + * The property `certPresented` will be set to `"1"` when + * the object is populated (i.e. the above conditions were met). + */ + tlsClientAuth: IncomingRequestCfPropertiesTLSClientAuth | IncomingRequestCfPropertiesTLSClientAuthPlaceholder; +} +/** + * Metadata about the request's TLS handshake + */ +interface IncomingRequestCfPropertiesExportedAuthenticatorMetadata { + /** + * The client's [`HELLO` message](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.1.2), encoded in hexadecimal + * + * @example "44372ba35fa1270921d318f34c12f155dc87b682cf36a790cfaa3ba8737a1b5d" + */ + clientHandshake: string; + /** + * The server's [`HELLO` message](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.1.2), encoded in hexadecimal + * + * @example "44372ba35fa1270921d318f34c12f155dc87b682cf36a790cfaa3ba8737a1b5d" + */ + serverHandshake: string; + /** + * The client's [`FINISHED` message](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.9), encoded in hexadecimal + * + * @example "084ee802fe1348f688220e2a6040a05b2199a761f33cf753abb1b006792d3f8b" + */ + clientFinished: string; + /** + * The server's [`FINISHED` message](https://www.rfc-editor.org/rfc/rfc5246#section-7.4.9), encoded in hexadecimal + * + * @example "084ee802fe1348f688220e2a6040a05b2199a761f33cf753abb1b006792d3f8b" + */ + serverFinished: string; +} +/** + * Geographic data about the request's origin. + */ +interface IncomingRequestCfPropertiesGeographicInformation { + /** + * The [ISO 3166-1 Alpha 2](https://www.iso.org/iso-3166-country-codes.html) country code the request originated from. + * + * If your worker is [configured to accept TOR connections](https://support.cloudflare.com/hc/en-us/articles/203306930-Understanding-Cloudflare-Tor-support-and-Onion-Routing), this may also be `"T1"`, indicating a request that originated over TOR. + * + * If Cloudflare is unable to determine where the request originated this property is omitted. + * + * The country code `"T1"` is used for requests originating on TOR. + * + * @example "GB" + */ + country?: Iso3166Alpha2Code | "T1"; + /** + * If present, this property indicates that the request originated in the EU + * + * @example "1" + */ + isEUCountry?: "1"; + /** + * A two-letter code indicating the continent the request originated from. + * + * @example "AN" + */ + continent?: ContinentCode; + /** + * The city the request originated from + * + * @example "Austin" + */ + city?: string; + /** + * Postal code of the incoming request + * + * @example "78701" + */ + postalCode?: string; + /** + * Latitude of the incoming request + * + * @example "30.27130" + */ + latitude?: string; + /** + * Longitude of the incoming request + * + * @example "-97.74260" + */ + longitude?: string; + /** + * Timezone of the incoming request + * + * @example "America/Chicago" + */ + timezone?: string; + /** + * If known, the ISO 3166-2 name for the first level region associated with + * the IP address of the incoming request + * + * @example "Texas" + */ + region?: string; + /** + * If known, the ISO 3166-2 code for the first-level region associated with + * the IP address of the incoming request + * + * @example "TX" + */ + regionCode?: string; + /** + * Metro code (DMA) of the incoming request + * + * @example "635" + */ + metroCode?: string; +} +/** Data about the incoming request's TLS certificate */ +interface IncomingRequestCfPropertiesTLSClientAuth { + /** Always `"1"`, indicating that the certificate was presented */ + certPresented: "1"; + /** + * Result of certificate verification. + * + * @example "FAILED:self signed certificate" + */ + certVerified: Exclude; + /** The presented certificate's revokation status. + * + * - A value of `"1"` indicates the certificate has been revoked + * - A value of `"0"` indicates the certificate has not been revoked + */ + certRevoked: "1" | "0"; + /** + * The certificate issuer's [distinguished name](https://knowledge.digicert.com/generalinformation/INFO1745.html) + * + * @example "CN=cloudflareaccess.com, C=US, ST=Texas, L=Austin, O=Cloudflare" + */ + certIssuerDN: string; + /** + * The certificate subject's [distinguished name](https://knowledge.digicert.com/generalinformation/INFO1745.html) + * + * @example "CN=*.cloudflareaccess.com, C=US, ST=Texas, L=Austin, O=Cloudflare" + */ + certSubjectDN: string; + /** + * The certificate issuer's [distinguished name](https://knowledge.digicert.com/generalinformation/INFO1745.html) ([RFC 2253](https://www.rfc-editor.org/rfc/rfc2253.html) formatted) + * + * @example "CN=cloudflareaccess.com, C=US, ST=Texas, L=Austin, O=Cloudflare" + */ + certIssuerDNRFC2253: string; + /** + * The certificate subject's [distinguished name](https://knowledge.digicert.com/generalinformation/INFO1745.html) ([RFC 2253](https://www.rfc-editor.org/rfc/rfc2253.html) formatted) + * + * @example "CN=*.cloudflareaccess.com, C=US, ST=Texas, L=Austin, O=Cloudflare" + */ + certSubjectDNRFC2253: string; + /** The certificate issuer's distinguished name (legacy policies) */ + certIssuerDNLegacy: string; + /** The certificate subject's distinguished name (legacy policies) */ + certSubjectDNLegacy: string; + /** + * The certificate's serial number + * + * @example "00936EACBE07F201DF" + */ + certSerial: string; + /** + * The certificate issuer's serial number + * + * @example "2489002934BDFEA34" + */ + certIssuerSerial: string; + /** + * The certificate's Subject Key Identifier + * + * @example "BB:AF:7E:02:3D:FA:A6:F1:3C:84:8E:AD:EE:38:98:EC:D9:32:32:D4" + */ + certSKI: string; + /** + * The certificate issuer's Subject Key Identifier + * + * @example "BB:AF:7E:02:3D:FA:A6:F1:3C:84:8E:AD:EE:38:98:EC:D9:32:32:D4" + */ + certIssuerSKI: string; + /** + * The certificate's SHA-1 fingerprint + * + * @example "6b9109f323999e52259cda7373ff0b4d26bd232e" + */ + certFingerprintSHA1: string; + /** + * The certificate's SHA-256 fingerprint + * + * @example "acf77cf37b4156a2708e34c4eb755f9b5dbbe5ebb55adfec8f11493438d19e6ad3f157f81fa3b98278453d5652b0c1fd1d71e5695ae4d709803a4d3f39de9dea" + */ + certFingerprintSHA256: string; + /** + * The effective starting date of the certificate + * + * @example "Dec 22 19:39:00 2018 GMT" + */ + certNotBefore: string; + /** + * The effective expiration date of the certificate + * + * @example "Dec 22 19:39:00 2018 GMT" + */ + certNotAfter: string; +} +/** Placeholder values for TLS Client Authorization */ +interface IncomingRequestCfPropertiesTLSClientAuthPlaceholder { + certPresented: "0"; + certVerified: "NONE"; + certRevoked: "0"; + certIssuerDN: ""; + certSubjectDN: ""; + certIssuerDNRFC2253: ""; + certSubjectDNRFC2253: ""; + certIssuerDNLegacy: ""; + certSubjectDNLegacy: ""; + certSerial: ""; + certIssuerSerial: ""; + certSKI: ""; + certIssuerSKI: ""; + certFingerprintSHA1: ""; + certFingerprintSHA256: ""; + certNotBefore: ""; + certNotAfter: ""; +} +/** Possible outcomes of TLS verification */ +declare type CertVerificationStatus = +/** Authentication succeeded */ +"SUCCESS" +/** No certificate was presented */ + | "NONE" +/** Failed because the certificate was self-signed */ + | "FAILED:self signed certificate" +/** Failed because the certificate failed a trust chain check */ + | "FAILED:unable to verify the first certificate" +/** Failed because the certificate not yet valid */ + | "FAILED:certificate is not yet valid" +/** Failed because the certificate is expired */ + | "FAILED:certificate has expired" +/** Failed for another unspecified reason */ + | "FAILED"; +/** + * An upstream endpoint's response to a TCP `keepalive` message from Cloudflare. + */ +declare type IncomingRequestCfPropertiesEdgeRequestKeepAliveStatus = 0 /** Unknown */ | 1 /** no keepalives (not found) */ | 2 /** no connection re-use, opening keepalive connection failed */ | 3 /** no connection re-use, keepalive accepted and saved */ | 4 /** connection re-use, refused by the origin server (`TCP FIN`) */ | 5; /** connection re-use, accepted by the origin server */ +/** ISO 3166-1 Alpha-2 codes */ +declare type Iso3166Alpha2Code = "AD" | "AE" | "AF" | "AG" | "AI" | "AL" | "AM" | "AO" | "AQ" | "AR" | "AS" | "AT" | "AU" | "AW" | "AX" | "AZ" | "BA" | "BB" | "BD" | "BE" | "BF" | "BG" | "BH" | "BI" | "BJ" | "BL" | "BM" | "BN" | "BO" | "BQ" | "BR" | "BS" | "BT" | "BV" | "BW" | "BY" | "BZ" | "CA" | "CC" | "CD" | "CF" | "CG" | "CH" | "CI" | "CK" | "CL" | "CM" | "CN" | "CO" | "CR" | "CU" | "CV" | "CW" | "CX" | "CY" | "CZ" | "DE" | "DJ" | "DK" | "DM" | "DO" | "DZ" | "EC" | "EE" | "EG" | "EH" | "ER" | "ES" | "ET" | "FI" | "FJ" | "FK" | "FM" | "FO" | "FR" | "GA" | "GB" | "GD" | "GE" | "GF" | "GG" | "GH" | "GI" | "GL" | "GM" | "GN" | "GP" | "GQ" | "GR" | "GS" | "GT" | "GU" | "GW" | "GY" | "HK" | "HM" | "HN" | "HR" | "HT" | "HU" | "ID" | "IE" | "IL" | "IM" | "IN" | "IO" | "IQ" | "IR" | "IS" | "IT" | "JE" | "JM" | "JO" | "JP" | "KE" | "KG" | "KH" | "KI" | "KM" | "KN" | "KP" | "KR" | "KW" | "KY" | "KZ" | "LA" | "LB" | "LC" | "LI" | "LK" | "LR" | "LS" | "LT" | "LU" | "LV" | "LY" | "MA" | "MC" | "MD" | "ME" | "MF" | "MG" | "MH" | "MK" | "ML" | "MM" | "MN" | "MO" | "MP" | "MQ" | "MR" | "MS" | "MT" | "MU" | "MV" | "MW" | "MX" | "MY" | "MZ" | "NA" | "NC" | "NE" | "NF" | "NG" | "NI" | "NL" | "NO" | "NP" | "NR" | "NU" | "NZ" | "OM" | "PA" | "PE" | "PF" | "PG" | "PH" | "PK" | "PL" | "PM" | "PN" | "PR" | "PS" | "PT" | "PW" | "PY" | "QA" | "RE" | "RO" | "RS" | "RU" | "RW" | "SA" | "SB" | "SC" | "SD" | "SE" | "SG" | "SH" | "SI" | "SJ" | "SK" | "SL" | "SM" | "SN" | "SO" | "SR" | "SS" | "ST" | "SV" | "SX" | "SY" | "SZ" | "TC" | "TD" | "TF" | "TG" | "TH" | "TJ" | "TK" | "TL" | "TM" | "TN" | "TO" | "TR" | "TT" | "TV" | "TW" | "TZ" | "UA" | "UG" | "UM" | "US" | "UY" | "UZ" | "VA" | "VC" | "VE" | "VG" | "VI" | "VN" | "VU" | "WF" | "WS" | "YE" | "YT" | "ZA" | "ZM" | "ZW"; +/** The 2-letter continent codes Cloudflare uses */ +declare type ContinentCode = "AF" | "AN" | "AS" | "EU" | "NA" | "OC" | "SA"; +type CfProperties = IncomingRequestCfProperties | RequestInitCfProperties; +interface D1Meta { + duration: number; + size_after: number; + rows_read: number; + rows_written: number; + last_row_id: number; + changed_db: boolean; + changes: number; + /** + * The region of the database instance that executed the query. + */ + served_by_region?: string; + /** + * The three letters airport code of the colo that executed the query. + */ + served_by_colo?: string; + /** + * True if-and-only-if the database instance that executed the query was the primary. + */ + served_by_primary?: boolean; + timings?: { + /** + * The duration of the SQL query execution by the database instance. It doesn't include any network time. + */ + sql_duration_ms: number; + }; + /** + * Number of total attempts to execute the query, due to automatic retries. + * Note: All other fields in the response like `timings` only apply to the last attempt. + */ + total_attempts?: number; +} +interface D1Response { + success: true; + meta: D1Meta & Record; + error?: never; +} +type D1Result = D1Response & { + results: T[]; +}; +interface D1ExecResult { + count: number; + duration: number; +} +type D1SessionConstraint = +// Indicates that the first query should go to the primary, and the rest queries +// using the same D1DatabaseSession will go to any replica that is consistent with +// the bookmark maintained by the session (returned by the first query). +'first-primary' +// Indicates that the first query can go anywhere (primary or replica), and the rest queries +// using the same D1DatabaseSession will go to any replica that is consistent with +// the bookmark maintained by the session (returned by the first query). + | 'first-unconstrained'; +type D1SessionBookmark = string; +declare abstract class D1Database { + prepare(query: string): D1PreparedStatement; + batch(statements: D1PreparedStatement[]): Promise[]>; + exec(query: string): Promise; + /** + * Creates a new D1 Session anchored at the given constraint or the bookmark. + * All queries executed using the created session will have sequential consistency, + * meaning that all writes done through the session will be visible in subsequent reads. + * + * @param constraintOrBookmark Either the session constraint or the explicit bookmark to anchor the created session. + */ + withSession(constraintOrBookmark?: D1SessionBookmark | D1SessionConstraint): D1DatabaseSession; + /** + * @deprecated dump() will be removed soon, only applies to deprecated alpha v1 databases. + */ + dump(): Promise; +} +declare abstract class D1DatabaseSession { + prepare(query: string): D1PreparedStatement; + batch(statements: D1PreparedStatement[]): Promise[]>; + /** + * @returns The latest session bookmark across all executed queries on the session. + * If no query has been executed yet, `null` is returned. + */ + getBookmark(): D1SessionBookmark | null; +} +declare abstract class D1PreparedStatement { + bind(...values: unknown[]): D1PreparedStatement; + first(colName: string): Promise; + first>(): Promise; + run>(): Promise>; + all>(): Promise>; + raw(options: { + columnNames: true; + }): Promise<[ + string[], + ...T[] + ]>; + raw(options?: { + columnNames?: false; + }): Promise; +} +// `Disposable` was added to TypeScript's standard lib types in version 5.2. +// To support older TypeScript versions, define an empty `Disposable` interface. +// Users won't be able to use `using`/`Symbol.dispose` without upgrading to 5.2, +// but this will ensure type checking on older versions still passes. +// TypeScript's interface merging will ensure our empty interface is effectively +// ignored when `Disposable` is included in the standard lib. +interface Disposable { +} +/** + * The returned data after sending an email + */ +interface EmailSendResult { + /** + * The Email Message ID + */ + messageId: string; +} +/** + * An email message that can be sent from a Worker. + */ +interface EmailMessage { + /** + * Envelope From attribute of the email message. + */ + readonly from: string; + /** + * Envelope To attribute of the email message. + */ + readonly to: string; +} +/** + * An email message that is sent to a consumer Worker and can be rejected/forwarded. + */ +interface ForwardableEmailMessage extends EmailMessage { + /** + * Stream of the email message content. + */ + readonly raw: ReadableStream; + /** + * An [Headers object](https://developer.mozilla.org/en-US/docs/Web/API/Headers). + */ + readonly headers: Headers; + /** + * Size of the email message content. + */ + readonly rawSize: number; + /** + * Reject this email message by returning a permanent SMTP error back to the connecting client including the given reason. + * @param reason The reject reason. + * @returns void + */ + setReject(reason: string): void; + /** + * Forward this email message to a verified destination address of the account. + * @param rcptTo Verified destination address. + * @param headers A [Headers object](https://developer.mozilla.org/en-US/docs/Web/API/Headers). + * @returns A promise that resolves when the email message is forwarded. + */ + forward(rcptTo: string, headers?: Headers): Promise; + /** + * Reply to the sender of this email message with a new EmailMessage object. + * @param message The reply message. + * @returns A promise that resolves when the email message is replied. + */ + reply(message: EmailMessage): Promise; +} +/** A file attachment for an email message */ +type EmailAttachment = { + disposition: 'inline'; + contentId: string; + filename: string; + type: string; + content: string | ArrayBuffer | ArrayBufferView; +} | { + disposition: 'attachment'; + contentId?: undefined; + filename: string; + type: string; + content: string | ArrayBuffer | ArrayBufferView; +}; +/** An Email Address */ +interface EmailAddress { + name: string; + email: string; +} +/** + * A binding that allows a Worker to send email messages. + */ +interface SendEmail { + send(message: EmailMessage): Promise; + send(builder: { + from: string | EmailAddress; + to: string | string[]; + subject: string; + replyTo?: string | EmailAddress; + cc?: string | string[]; + bcc?: string | string[]; + headers?: Record; + text?: string; + html?: string; + attachments?: EmailAttachment[]; + }): Promise; +} +declare abstract class EmailEvent extends ExtendableEvent { + readonly message: ForwardableEmailMessage; +} +declare type EmailExportedHandler = (message: ForwardableEmailMessage, env: Env, ctx: ExecutionContext) => void | Promise; +declare module "cloudflare:email" { + let _EmailMessage: { + prototype: EmailMessage; + new (from: string, to: string, raw: ReadableStream | string): EmailMessage; + }; + export { _EmailMessage as EmailMessage }; +} +/** + * Evaluation context for targeting rules. + * Keys are attribute names (e.g. "userId", "country"), values are the attribute values. + */ +type FlagshipEvaluationContext = Record; +interface FlagshipEvaluationDetails { + flagKey: string; + value: T; + variant?: string | undefined; + reason?: string | undefined; + errorCode?: string | undefined; + errorMessage?: string | undefined; +} +interface FlagshipEvaluationError extends Error { +} +/** + * Feature flags binding for evaluating feature flags from a Cloudflare Workers script. + * + * @example + * ```typescript + * // Get a boolean flag value with a default + * const enabled = await env.FLAGS.getBooleanValue('my-feature', false); + * + * // Get a flag value with evaluation context for targeting + * const variant = await env.FLAGS.getStringValue('experiment', 'control', { + * userId: 'user-123', + * country: 'US', + * }); + * + * // Get full evaluation details including variant and reason + * const details = await env.FLAGS.getBooleanDetails('my-feature', false); + * console.log(details.variant, details.reason); + * ``` + */ +declare abstract class Flagship { + /** + * Get a flag value without type checking. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Optional default value returned when evaluation fails. + * @param context Optional evaluation context for targeting rules. + */ + get(flagKey: string, defaultValue?: unknown, context?: FlagshipEvaluationContext): Promise; + /** + * Get a boolean flag value. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getBooleanValue(flagKey: string, defaultValue: boolean, context?: FlagshipEvaluationContext): Promise; + /** + * Get a string flag value. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getStringValue(flagKey: string, defaultValue: string, context?: FlagshipEvaluationContext): Promise; + /** + * Get a number flag value. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getNumberValue(flagKey: string, defaultValue: number, context?: FlagshipEvaluationContext): Promise; + /** + * Get an object flag value. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getObjectValue(flagKey: string, defaultValue: T, context?: FlagshipEvaluationContext): Promise; + /** + * Get a boolean flag value with full evaluation details. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getBooleanDetails(flagKey: string, defaultValue: boolean, context?: FlagshipEvaluationContext): Promise>; + /** + * Get a string flag value with full evaluation details. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getStringDetails(flagKey: string, defaultValue: string, context?: FlagshipEvaluationContext): Promise>; + /** + * Get a number flag value with full evaluation details. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getNumberDetails(flagKey: string, defaultValue: number, context?: FlagshipEvaluationContext): Promise>; + /** + * Get an object flag value with full evaluation details. + * @param flagKey The key of the flag to evaluate. + * @param defaultValue Default value returned when evaluation fails or the flag type does not match. + * @param context Optional evaluation context for targeting rules. + */ + getObjectDetails(flagKey: string, defaultValue: T, context?: FlagshipEvaluationContext): Promise>; +} +/** + * Hello World binding to serve as an explanatory example. DO NOT USE + */ +interface HelloWorldBinding { + /** + * Retrieve the current stored value + */ + get(): Promise<{ + value: string; + ms?: number; + }>; + /** + * Set a new stored value + */ + set(value: string): Promise; +} +interface Hyperdrive { + /** + * Connect directly to Hyperdrive as if it's your database, returning a TCP socket. + * + * Calling this method returns an identical socket to if you call + * `connect("host:port")` using the `host` and `port` fields from this object. + * Pick whichever approach works better with your preferred DB client library. + * + * Note that this socket is not yet authenticated -- it's expected that your + * code (or preferably, the client library of your choice) will authenticate + * using the information in this class's readonly fields. + */ + connect(): Socket; + /** + * A valid DB connection string that can be passed straight into the typical + * client library/driver/ORM. This will typically be the easiest way to use + * Hyperdrive. + */ + readonly connectionString: string; + /* + * A randomly generated hostname that is only valid within the context of the + * currently running Worker which, when passed into `connect()` function from + * the "cloudflare:sockets" module, will connect to the Hyperdrive instance + * for your database. + */ + readonly host: string; + /* + * The port that must be paired the the host field when connecting. + */ + readonly port: number; + /* + * The username to use when authenticating to your database via Hyperdrive. + * Unlike the host and password, this will be the same every time + */ + readonly user: string; + /* + * The randomly generated password to use when authenticating to your + * database via Hyperdrive. Like the host field, this password is only valid + * within the context of the currently running Worker instance from which + * it's read. + */ + readonly password: string; + /* + * The name of the database to connect to. + */ + readonly database: string; +} +// Copyright (c) 2024 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 +type ImageInfoResponse = { + format: 'image/svg+xml'; +} | { + format: string; + fileSize: number; + width: number; + height: number; +}; +type ImageTransform = { + width?: number; + height?: number; + background?: string; + blur?: number; + border?: { + color?: string; + width?: number; + } | { + top?: number; + bottom?: number; + left?: number; + right?: number; + }; + brightness?: number; + contrast?: number; + fit?: 'scale-down' | 'contain' | 'pad' | 'squeeze' | 'cover' | 'crop'; + flip?: 'h' | 'v' | 'hv'; + gamma?: number; + segment?: 'foreground'; + gravity?: 'face' | 'left' | 'right' | 'top' | 'bottom' | 'center' | 'auto' | 'entropy' | { + x?: number; + y?: number; + mode: 'remainder' | 'box-center'; + }; + rotate?: 0 | 90 | 180 | 270; + saturation?: number; + sharpen?: number; + trim?: 'border' | { + top?: number; + bottom?: number; + left?: number; + right?: number; + width?: number; + height?: number; + border?: boolean | { + color?: string; + tolerance?: number; + keep?: number; + }; + }; +}; +type ImageDrawOptions = { + opacity?: number; + repeat?: boolean | string; + top?: number; + left?: number; + bottom?: number; + right?: number; +}; +type ImageInputOptions = { + encoding?: 'base64'; +}; +type ImageOutputOptions = { + format: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp' | 'image/avif' | 'rgb' | 'rgba'; + quality?: number; + background?: string; + anim?: boolean; +}; +interface ImageMetadata { + id: string; + filename?: string; + uploaded?: string; + requireSignedURLs: boolean; + meta?: Record; + variants: string[]; + draft?: boolean; + creator?: string; +} +interface ImageUploadOptions { + id?: string; + filename?: string; + requireSignedURLs?: boolean; + metadata?: Record; + creator?: string; + encoding?: 'base64'; +} +interface ImageUpdateOptions { + requireSignedURLs?: boolean; + metadata?: Record; + creator?: string; +} +interface ImageListOptions { + limit?: number; + cursor?: string; + sortOrder?: 'asc' | 'desc'; + creator?: string; +} +interface ImageList { + images: ImageMetadata[]; + cursor?: string; + listComplete: boolean; +} +interface ImageHandle { + /** + * Get metadata for a hosted image + * @returns Image metadata, or null if not found + */ + details(): Promise; + /** + * Get the raw image data for a hosted image + * @returns ReadableStream of image bytes, or null if not found + */ + bytes(): Promise | null>; + /** + * Update hosted image metadata + * @param options Properties to update + * @returns Updated image metadata + * @throws {@link ImagesError} if update fails + */ + update(options: ImageUpdateOptions): Promise; + /** + * Delete a hosted image + * @returns True if deleted, false if not found + */ + delete(): Promise; +} +interface HostedImagesBinding { + /** + * Get a handle for a hosted image + * @param imageId The ID of the image (UUID or custom ID) + * @returns A handle for per-image operations + */ + image(imageId: string): ImageHandle; + /** + * Upload a new hosted image + * @param image The image file to upload + * @param options Upload configuration + * @returns Metadata for the uploaded image + * @throws {@link ImagesError} if upload fails + */ + upload(image: ReadableStream | ArrayBuffer, options?: ImageUploadOptions): Promise; + /** + * List hosted images with pagination + * @param options List configuration + * @returns List of images with pagination info + * @throws {@link ImagesError} if list fails + */ + list(options?: ImageListOptions): Promise; +} +interface ImagesBinding { + /** + * Get image metadata (type, width and height) + * @throws {@link ImagesError} with code 9412 if input is not an image + * @param stream The image bytes + */ + info(stream: ReadableStream, options?: ImageInputOptions): Promise; + /** + * Begin applying a series of transformations to an image + * @param stream The image bytes + * @returns A transform handle + */ + input(stream: ReadableStream, options?: ImageInputOptions): ImageTransformer; + /** + * Access hosted images CRUD operations + */ + readonly hosted: HostedImagesBinding; +} +interface ImageTransformer { + /** + * Apply transform next, returning a transform handle. + * You can then apply more transformations, draw, or retrieve the output. + * @param transform + */ + transform(transform: ImageTransform): ImageTransformer; + /** + * Draw an image on this transformer, returning a transform handle. + * You can then apply more transformations, draw, or retrieve the output. + * @param image The image (or transformer that will give the image) to draw + * @param options The options configuring how to draw the image + */ + draw(image: ReadableStream | ImageTransformer, options?: ImageDrawOptions): ImageTransformer; + /** + * Retrieve the image that results from applying the transforms to the + * provided input + * @param options Options that apply to the output e.g. output format + */ + output(options: ImageOutputOptions): Promise; +} +type ImageTransformationOutputOptions = { + encoding?: 'base64'; +}; +interface ImageTransformationResult { + /** + * The image as a response, ready to store in cache or return to users + */ + response(): Response; + /** + * The content type of the returned image + */ + contentType(): string; + /** + * The bytes of the response + */ + image(options?: ImageTransformationOutputOptions): ReadableStream; +} +interface ImagesError extends Error { + readonly code: number; + readonly message: string; + readonly stack?: string; +} +/** + * Media binding for transforming media streams. + * Provides the entry point for media transformation operations. + */ +interface MediaBinding { + /** + * Creates a media transformer from an input stream. + * @param media - The input media bytes + * @returns A MediaTransformer instance for applying transformations + */ + input(media: ReadableStream): MediaTransformer; +} +/** + * Media transformer for applying transformation operations to media content. + * Handles sizing, fitting, and other input transformation parameters. + */ +interface MediaTransformer { + /** + * Applies transformation options to the media content. + * @param transform - Configuration for how the media should be transformed + * @returns A generator for producing the transformed media output + */ + transform(transform?: MediaTransformationInputOptions): MediaTransformationGenerator; + /** + * Generates the final media output with specified options. + * @param output - Configuration for the output format and parameters + * @returns The final transformation result containing the transformed media + */ + output(output?: MediaTransformationOutputOptions): MediaTransformationResult; +} +/** + * Generator for producing media transformation results. + * Configures the output format and parameters for the transformed media. + */ +interface MediaTransformationGenerator { + /** + * Generates the final media output with specified options. + * @param output - Configuration for the output format and parameters + * @returns The final transformation result containing the transformed media + */ + output(output?: MediaTransformationOutputOptions): MediaTransformationResult; +} +/** + * Result of a media transformation operation. + * Provides multiple ways to access the transformed media content. + */ +interface MediaTransformationResult { + /** + * Returns the transformed media as a readable stream of bytes. + * @returns A promise containing a readable stream with the transformed media + */ + media(): Promise>; + /** + * Returns the transformed media as an HTTP response object. + * @returns The transformed media as a Promise, ready to store in cache or return to users + */ + response(): Promise; + /** + * Returns the MIME type of the transformed media. + * @returns A promise containing the content type string (e.g., 'image/jpeg', 'video/mp4') + */ + contentType(): Promise; +} +/** + * Configuration options for transforming media input. + * Controls how the media should be resized and fitted. + */ +type MediaTransformationInputOptions = { + /** How the media should be resized to fit the specified dimensions */ + fit?: 'contain' | 'cover' | 'scale-down'; + /** Target width in pixels */ + width?: number; + /** Target height in pixels */ + height?: number; +}; +/** + * Configuration options for Media Transformations output. + * Controls the format, timing, and type of the generated output. + */ +type MediaTransformationOutputOptions = { + /** + * Output mode determining the type of media to generate + */ + mode?: 'video' | 'spritesheet' | 'frame' | 'audio'; + /** Whether to include audio in the output */ + audio?: boolean; + /** + * Starting timestamp for frame extraction or start time for clips. (e.g. '2s'). + */ + time?: string; + /** + * Duration for video clips, audio extraction, and spritesheet generation (e.g. '5s'). + */ + duration?: string; + /** + * Number of frames in the spritesheet. + */ + imageCount?: number; + /** + * Output format for the generated media. + */ + format?: 'jpg' | 'png' | 'm4a'; +}; +/** + * Error object for media transformation operations. + * Extends the standard Error interface with additional media-specific information. + */ +interface MediaError extends Error { + readonly code: number; + readonly message: string; + readonly stack?: string; +} +declare module 'cloudflare:node' { + interface NodeStyleServer { + listen(...args: unknown[]): this; + address(): { + port?: number | null | undefined; + }; + } + export function httpServerHandler(port: number): ExportedHandler; + export function httpServerHandler(options: { + port: number; + }): ExportedHandler; + export function httpServerHandler(server: NodeStyleServer): ExportedHandler; +} +type Params

= Record; +type EventContext = { + request: Request>; + functionPath: string; + waitUntil: (promise: Promise) => void; + passThroughOnException: () => void; + next: (input?: Request | string, init?: RequestInit) => Promise; + env: Env & { + ASSETS: { + fetch: typeof fetch; + }; + }; + params: Params

; + data: Data; +}; +type PagesFunction = Record> = (context: EventContext) => Response | Promise; +type EventPluginContext = { + request: Request>; + functionPath: string; + waitUntil: (promise: Promise) => void; + passThroughOnException: () => void; + next: (input?: Request | string, init?: RequestInit) => Promise; + env: Env & { + ASSETS: { + fetch: typeof fetch; + }; + }; + params: Params

; + data: Data; + pluginArgs: PluginArgs; +}; +type PagesPluginFunction = Record, PluginArgs = unknown> = (context: EventPluginContext) => Response | Promise; +declare module "assets:*" { + export const onRequest: PagesFunction; +} +// Copyright (c) 2022-2023 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 +declare module "cloudflare:pipelines" { + export abstract class PipelineTransformationEntrypoint { + protected env: Env; + protected ctx: ExecutionContext; + constructor(ctx: ExecutionContext, env: Env); + /** + * run receives an array of PipelineRecord which can be + * transformed and returned to the pipeline + * @param records Incoming records from the pipeline to be transformed + * @param metadata Information about the specific pipeline calling the transformation entrypoint + * @returns A promise containing the transformed PipelineRecord array + */ + public run(records: I[], metadata: PipelineBatchMetadata): Promise; + } + export type PipelineRecord = Record; + export type PipelineBatchMetadata = { + pipelineId: string; + pipelineName: string; + }; + export interface Pipeline { + /** + * The Pipeline interface represents the type of a binding to a Pipeline + * + * @param records The records to send to the pipeline + */ + send(records: T[]): Promise; + } +} +// PubSubMessage represents an incoming PubSub message. +// The message includes metadata about the broker, the client, and the payload +// itself. +// https://developers.cloudflare.com/pub-sub/ +interface PubSubMessage { + // Message ID + readonly mid: number; + // MQTT broker FQDN in the form mqtts://BROKER.NAMESPACE.cloudflarepubsub.com:PORT + readonly broker: string; + // The MQTT topic the message was sent on. + readonly topic: string; + // The client ID of the client that published this message. + readonly clientId: string; + // The unique identifier (JWT ID) used by the client to authenticate, if token + // auth was used. + readonly jti?: string; + // A Unix timestamp (seconds from Jan 1, 1970), set when the Pub/Sub Broker + // received the message from the client. + readonly receivedAt: number; + // An (optional) string with the MIME type of the payload, if set by the + // client. + readonly contentType: string; + // Set to 1 when the payload is a UTF-8 string + // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901063 + readonly payloadFormatIndicator: number; + // Pub/Sub (MQTT) payloads can be UTF-8 strings, or byte arrays. + // You can use payloadFormatIndicator to inspect this before decoding. + payload: string | Uint8Array; +} +// JsonWebKey extended by kid parameter +interface JsonWebKeyWithKid extends JsonWebKey { + // Key Identifier of the JWK + readonly kid: string; +} +interface RateLimitOptions { + key: string; +} +interface RateLimitOutcome { + success: boolean; +} +interface RateLimit { + /** + * Rate limit a request based on the provided options. + * @see https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/ + * @returns A promise that resolves with the outcome of the rate limit. + */ + limit(options: RateLimitOptions): Promise; +} +// Namespace for RPC utility types. Unfortunately, we can't use a `module` here as these types need +// to referenced by `Fetcher`. This is included in the "importable" version of the types which +// strips all `module` blocks. +declare namespace Rpc { + // Branded types for identifying `WorkerEntrypoint`/`DurableObject`/`Target`s. + // TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`. + // For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to + // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape) + export const __RPC_STUB_BRAND: '__RPC_STUB_BRAND'; + export const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND'; + export const __WORKER_ENTRYPOINT_BRAND: '__WORKER_ENTRYPOINT_BRAND'; + export const __DURABLE_OBJECT_BRAND: '__DURABLE_OBJECT_BRAND'; + export const __WORKFLOW_ENTRYPOINT_BRAND: '__WORKFLOW_ENTRYPOINT_BRAND'; + export interface RpcTargetBranded { + [__RPC_TARGET_BRAND]: never; + } + export interface WorkerEntrypointBranded { + [__WORKER_ENTRYPOINT_BRAND]: never; + } + export interface DurableObjectBranded { + [__DURABLE_OBJECT_BRAND]: never; + } + export interface WorkflowEntrypointBranded { + [__WORKFLOW_ENTRYPOINT_BRAND]: never; + } + export type EntrypointBranded = WorkerEntrypointBranded | DurableObjectBranded | WorkflowEntrypointBranded; + // Types that can be used through `Stub`s + export type Stubable = RpcTargetBranded | ((...args: any[]) => any); + // Types that can be passed over RPC + // The reason for using a generic type here is to build a serializable subset of structured + // cloneable composite types. This allows types defined with the "interface" keyword to pass the + // serializable check as well. Otherwise, only types defined with the "type" keyword would pass. + type Serializable = + // Structured cloneables + BaseType + // Structured cloneable composites + | Map ? Serializable : never, T extends Map ? Serializable : never> | Set ? Serializable : never> | ReadonlyArray ? Serializable : never> | { + [K in keyof T]: K extends number | string ? Serializable : never; + } + // Special types + | Stub + // Serialized as stubs, see `Stubify` + | Stubable; + // Base type for all RPC stubs, including common memory management methods. + // `T` is used as a marker type for unwrapping `Stub`s later. + interface StubBase extends Disposable { + [__RPC_STUB_BRAND]: T; + dup(): this; + } + export type Stub = Provider & StubBase; + // This represents all the types that can be sent as-is over an RPC boundary + type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | ReadableStream | WritableStream | Request | Response | Headers; + // Recursively rewrite all `Stubable` types with `Stub`s + // prettier-ignore + type Stubify = T extends Stubable ? Stub : T extends Map ? Map, Stubify> : T extends Set ? Set> : T extends Array ? Array> : T extends ReadonlyArray ? ReadonlyArray> : T extends BaseType ? T : T extends { + [key: string | number]: any; + } ? { + [K in keyof T]: Stubify; + } : T; + // Recursively rewrite all `Stub`s with the corresponding `T`s. + // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies: + // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`. + // prettier-ignore + type Unstubify = T extends StubBase ? V : T extends Map ? Map, Unstubify> : T extends Set ? Set> : T extends Array ? Array> : T extends ReadonlyArray ? ReadonlyArray> : T extends BaseType ? T : T extends { + [key: string | number]: unknown; + } ? { + [K in keyof T]: Unstubify; + } : T; + type UnstubifyAll = { + [I in keyof A]: Unstubify; + }; + // Utility type for adding `Provider`/`Disposable`s to `object` types only. + // Note `unknown & T` is equivalent to `T`. + type MaybeProvider = T extends object ? Provider : unknown; + type MaybeDisposable = T extends object ? Disposable : unknown; + // Type for method return or property on an RPC interface. + // - Stubable types are replaced by stubs. + // - Serializable types are passed by value, with stubable types replaced by stubs + // and a top-level `Disposer`. + // Everything else can't be passed over PRC. + // Technically, we use custom thenables here, but they quack like `Promise`s. + // Intersecting with `(Maybe)Provider` allows pipelining. + // prettier-ignore + type Result = R extends Stubable ? Promise> & Provider : R extends Serializable ? Promise & MaybeDisposable> & MaybeProvider : never; + // Type for method or property on an RPC interface. + // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s. + // Unwrapping `Stub`s allows calling with `Stubable` arguments. + // For properties, rewrite types to be `Result`s. + // In each case, unwrap `Promise`s. + type MethodOrProperty = V extends (...args: infer P) => infer R ? (...args: UnstubifyAll

) => Result> : Result>; + // Type for the callable part of an `Provider` if `T` is callable. + // This is intersected with methods/properties. + type MaybeCallableProvider = T extends (...args: any[]) => any ? MethodOrProperty : unknown; + // Base type for all other types providing RPC-like interfaces. + // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types. + // `Reserved` names (e.g. stub method names like `dup()`) and symbols can't be accessed over RPC. + export type Provider = MaybeCallableProvider & Pick<{ + [K in keyof T]: MethodOrProperty; + }, Exclude>>; +} +declare namespace Cloudflare { + // Type of `env`. + // + // The specific project can extend `Env` by redeclaring it in project-specific files. Typescript + // will merge all declarations. + // + // You can use `wrangler types` to generate the `Env` type automatically. + interface Env { + } + // Project-specific parameters used to inform types. + // + // This interface is, again, intended to be declared in project-specific files, and then that + // declaration will be merged with this one. + // + // A project should have a declaration like this: + // + // interface GlobalProps { + // // Declares the main module's exports. Used to populate Cloudflare.Exports aka the type + // // of `ctx.exports`. + // mainModule: typeof import("my-main-module"); + // + // // Declares which of the main module's exports are configured with durable storage, and + // // thus should behave as Durable Object namsepace bindings. + // durableNamespaces: "MyDurableObject" | "AnotherDurableObject"; + // } + // + // You can use `wrangler types` to generate `GlobalProps` automatically. + interface GlobalProps { + } + // Evaluates to the type of a property in GlobalProps, defaulting to `Default` if it is not + // present. + type GlobalProp = K extends keyof GlobalProps ? GlobalProps[K] : Default; + // The type of the program's main module exports, if known. Requires `GlobalProps` to declare the + // `mainModule` property. + type MainModule = GlobalProp<"mainModule", {}>; + // The type of ctx.exports, which contains loopback bindings for all top-level exports. + type Exports = { + [K in keyof MainModule]: LoopbackForExport + // If the export is listed in `durableNamespaces`, then it is also a + // DurableObjectNamespace. + & (K extends GlobalProp<"durableNamespaces", never> ? MainModule[K] extends new (...args: any[]) => infer DoInstance ? DoInstance extends Rpc.DurableObjectBranded ? DurableObjectNamespace : DurableObjectNamespace : DurableObjectNamespace : {}); + }; +} +declare namespace CloudflareWorkersModule { + export type RpcStub = Rpc.Stub; + export const RpcStub: { + new (value: T): Rpc.Stub; + }; + export abstract class RpcTarget implements Rpc.RpcTargetBranded { + [Rpc.__RPC_TARGET_BRAND]: never; + } + // `protected` fields don't appear in `keyof`s, so can't be accessed over RPC + export abstract class WorkerEntrypoint implements Rpc.WorkerEntrypointBranded { + [Rpc.__WORKER_ENTRYPOINT_BRAND]: never; + protected ctx: ExecutionContext; + protected env: Env; + constructor(ctx: ExecutionContext, env: Env); + email?(message: ForwardableEmailMessage): void | Promise; + fetch?(request: Request): Response | Promise; + connect?(socket: Socket): void | Promise; + queue?(batch: MessageBatch): void | Promise; + scheduled?(controller: ScheduledController): void | Promise; + tail?(events: TraceItem[]): void | Promise; + tailStream?(event: TailStream.TailEvent): TailStream.TailEventHandlerType | Promise; + test?(controller: TestController): void | Promise; + trace?(traces: TraceItem[]): void | Promise; + } + export abstract class DurableObject implements Rpc.DurableObjectBranded { + [Rpc.__DURABLE_OBJECT_BRAND]: never; + protected ctx: DurableObjectState; + protected env: Env; + constructor(ctx: DurableObjectState, env: Env); + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; + fetch?(request: Request): Response | Promise; + connect?(socket: Socket): void | Promise; + webSocketMessage?(ws: WebSocket, message: string | ArrayBuffer): void | Promise; + webSocketClose?(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise; + webSocketError?(ws: WebSocket, error: unknown): void | Promise; + } + export type WorkflowDurationLabel = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; + export type WorkflowSleepDuration = `${number} ${WorkflowDurationLabel}${'s' | ''}` | number; + export type WorkflowDelayDuration = WorkflowSleepDuration; + export type WorkflowTimeoutDuration = WorkflowSleepDuration; + export type WorkflowRetentionDuration = WorkflowSleepDuration; + export type WorkflowBackoff = 'constant' | 'linear' | 'exponential'; + export type WorkflowStepConfig = { + retries?: { + limit: number; + delay: WorkflowDelayDuration | number; + backoff?: WorkflowBackoff; + }; + timeout?: WorkflowTimeoutDuration | number; + }; + export type WorkflowEvent = { + payload: Readonly; + timestamp: Date; + instanceId: string; + }; + export type WorkflowStepEvent = { + payload: Readonly; + timestamp: Date; + type: string; + }; + export type WorkflowStepContext = { + step: { + name: string; + count: number; + }; + attempt: number; + config: WorkflowStepConfig; + }; + export abstract class WorkflowStep { + do>(name: string, callback: (ctx: WorkflowStepContext) => Promise): Promise; + do>(name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise): Promise; + sleep: (name: string, duration: WorkflowSleepDuration) => Promise; + sleepUntil: (name: string, timestamp: Date | number) => Promise; + waitForEvent>(name: string, options: { + type: string; + timeout?: WorkflowTimeoutDuration | number; + }): Promise>; + } + export type WorkflowInstanceStatus = 'queued' | 'running' | 'paused' | 'errored' | 'terminated' | 'complete' | 'waiting' | 'waitingForPause' | 'unknown'; + export abstract class WorkflowEntrypoint | unknown = unknown> implements Rpc.WorkflowEntrypointBranded { + [Rpc.__WORKFLOW_ENTRYPOINT_BRAND]: never; + protected ctx: ExecutionContext; + protected env: Env; + constructor(ctx: ExecutionContext, env: Env); + run(event: Readonly>, step: WorkflowStep): Promise; + } + export function waitUntil(promise: Promise): void; + export function withEnv(newEnv: unknown, fn: () => unknown): unknown; + export function withExports(newExports: unknown, fn: () => unknown): unknown; + export function withEnvAndExports(newEnv: unknown, newExports: unknown, fn: () => unknown): unknown; + export const env: Cloudflare.Env; + export const exports: Cloudflare.Exports; + export const cache: CacheContext; + export const tracing: Tracing; +} +declare module 'cloudflare:workers' { + export = CloudflareWorkersModule; +} +interface SecretsStoreSecret { + /** + * Get a secret from the Secrets Store, returning a string of the secret value + * if it exists, or throws an error if it does not exist + */ + get(): Promise; +} +declare module "cloudflare:sockets" { + function _connect(address: string | SocketAddress, options?: SocketOptions): Socket; + export { _connect as connect }; +} +/** + * Binding entrypoint for Cloudflare Stream. + * + * Usage: + * - Binding-level operations: + * `await env.STREAM.videos.upload` + * `await env.STREAM.videos.createDirectUpload` + * `await env.STREAM.videos.*` + * `await env.STREAM.watermarks.*` + * - Per-video operations: + * `await env.STREAM.video(id).downloads.*` + * `await env.STREAM.video(id).captions.*` + * + * Example usage: + * ```ts + * await env.STREAM.video(id).downloads.generate(); + * + * const video = env.STREAM.video(id) + * const captions = video.captions.list(); + * const videoDetails = video.details() + * ``` + */ +interface StreamBinding { + /** + * Returns a handle scoped to a single video for per-video operations. + * @param id The unique identifier for the video. + * @returns A handle for per-video operations. + */ + video(id: string): StreamVideoHandle; + /** + * Uploads a new video from a provided URL. + * @param url The URL to upload from. + * @param params Optional upload parameters. + * @returns The uploaded video details. + * @throws {BadRequestError} if the upload parameter is invalid or the URL is invalid + * @throws {QuotaReachedError} if the account storage capacity is exceeded + * @throws {MaxFileSizeError} if the file size is too large + * @throws {RateLimitedError} if the server received too many requests + * @throws {AlreadyUploadedError} if a video was already uploaded to this URL + * @throws {InternalError} if an unexpected error occurs + */ + upload(url: string, params?: StreamUrlUploadParams): Promise; + /** + * Creates a direct upload that allows video uploads without an API key. + * @param params Parameters for the direct upload + * @returns The direct upload details. + * @throws {BadRequestError} if the parameters are invalid + * @throws {RateLimitedError} if the server received too many requests + * @throws {InternalError} if an unexpected error occurs + */ + createDirectUpload(params: StreamDirectUploadCreateParams): Promise; + videos: StreamVideos; + watermarks: StreamWatermarks; +} +/** + * Handle for operations scoped to a single Stream video. + */ +interface StreamVideoHandle { + /** + * The unique identifier for the video. + */ + id: string; + /** + * Get a full videos details + * @returns The full video details. + * @throws {NotFoundError} if the video is not found + * @throws {InternalError} if an unexpected error occurs + */ + details(): Promise; + /** + * Update details for a single video. + * @param params The fields to update for the video. + * @returns The updated video details. + * @throws {NotFoundError} if the video is not found + * @throws {BadRequestError} if the parameters are invalid + * @throws {InternalError} if an unexpected error occurs + */ + update(params: StreamUpdateVideoParams): Promise; + /** + * Deletes a video and its copies from Cloudflare Stream. + * @returns A promise that resolves when deletion completes. + * @throws {NotFoundError} if the video is not found + * @throws {InternalError} if an unexpected error occurs + */ + delete(): Promise; + /** + * Creates a signed URL token for a video. + * @returns The signed token that was created. + * @throws {InternalError} if the signing key cannot be retrieved or the token cannot be signed + */ + generateToken(): Promise; + downloads: StreamScopedDownloads; + captions: StreamScopedCaptions; +} +interface StreamVideo { + /** + * The unique identifier for the video. + */ + id: string; + /** + * A user-defined identifier for the media creator. + */ + creator: string | null; + /** + * The thumbnail URL for the video. + */ + thumbnail: string; + /** + * The thumbnail timestamp percentage. + */ + thumbnailTimestampPct: number; + /** + * Indicates whether the video is ready to stream. + */ + readyToStream: boolean; + /** + * The date and time the video became ready to stream. + */ + readyToStreamAt: string | null; + /** + * Processing status information. + */ + status: StreamVideoStatus; + /** + * A user modifiable key-value store. + */ + meta: Record; + /** + * The date and time the video was created. + */ + created: string; + /** + * The date and time the video was last modified. + */ + modified: string; + /** + * The date and time at which the video will be deleted. + */ + scheduledDeletion: string | null; + /** + * The size of the video in bytes. + */ + size: number; + /** + * The preview URL for the video. + */ + preview?: string; + /** + * Origins allowed to display the video. + */ + allowedOrigins: Array; + /** + * Indicates whether signed URLs are required. + */ + requireSignedURLs: boolean | null; + /** + * The date and time the video was uploaded. + */ + uploaded: string | null; + /** + * The date and time when the upload URL expires. + */ + uploadExpiry: string | null; + /** + * The maximum size in bytes for direct uploads. + */ + maxSizeBytes: number | null; + /** + * The maximum duration in seconds for direct uploads. + */ + maxDurationSeconds: number | null; + /** + * The video duration in seconds. -1 indicates unknown. + */ + duration: number; + /** + * Input metadata for the original upload. + */ + input: StreamVideoInput; + /** + * Playback URLs for the video. + */ + hlsPlaybackUrl: string; + dashPlaybackUrl: string; + /** + * The watermark applied to the video, if any. + */ + watermark: StreamWatermark | null; + /** + * The live input id associated with the video, if any. + */ + liveInputId?: string | null; + /** + * The source video id if this is a clip. + */ + clippedFromId: string | null; + /** + * Public details associated with the video. + */ + publicDetails: StreamPublicDetails | null; +} +type StreamVideoStatus = { + /** + * The current processing state. + */ + state: string; + /** + * The current processing step. + */ + step?: string; + /** + * The percent complete as a string. + */ + pctComplete?: string; + /** + * An error reason code, if applicable. + */ + errorReasonCode: string; + /** + * An error reason text, if applicable. + */ + errorReasonText: string; +}; +type StreamVideoInput = { + /** + * The input width in pixels. + */ + width: number; + /** + * The input height in pixels. + */ + height: number; +}; +type StreamPublicDetails = { + /** + * The public title for the video. + */ + title: string | null; + /** + * The public share link. + */ + share_link: string | null; + /** + * The public channel link. + */ + channel_link: string | null; + /** + * The public logo URL. + */ + logo: string | null; +}; +type StreamDirectUpload = { + /** + * The URL an unauthenticated upload can use for a single multipart request. + */ + uploadURL: string; + /** + * A Cloudflare-generated unique identifier for a media item. + */ + id: string; + /** + * The watermark profile applied to the upload. + */ + watermark: StreamWatermark | null; + /** + * The scheduled deletion time, if any. + */ + scheduledDeletion: string | null; +}; +type StreamDirectUploadCreateParams = { + /** + * The maximum duration in seconds for a video upload. + */ + maxDurationSeconds: number; + /** + * The date and time after upload when videos will not be accepted. + */ + expiry?: string; + /** + * A user-defined identifier for the media creator. + */ + creator?: string; + /** + * A user modifiable key-value store used to reference other systems of record for + * managing videos. + */ + meta?: Record; + /** + * Lists the origins allowed to display the video. + */ + allowedOrigins?: Array; + /** + * Indicates whether the video can be accessed using the id. When set to `true`, + * a signed token must be generated with a signing key to view the video. + */ + requireSignedURLs?: boolean; + /** + * The thumbnail timestamp percentage. + */ + thumbnailTimestampPct?: number; + /** + * The date and time at which the video will be deleted. Include `null` to remove + * a scheduled deletion. + */ + scheduledDeletion?: string | null; + /** + * The watermark profile to apply. + */ + watermark?: StreamDirectUploadWatermark; +}; +type StreamDirectUploadWatermark = { + /** + * The unique identifier for the watermark profile. + */ + id: string; +}; +type StreamUrlUploadParams = { + /** + * Lists the origins allowed to display the video. Enter allowed origin + * domains in an array and use `*` for wildcard subdomains. Empty arrays allow the + * video to be viewed on any origin. + */ + allowedOrigins?: Array; + /** + * A user-defined identifier for the media creator. + */ + creator?: string; + /** + * A user modifiable key-value store used to reference other systems of + * record for managing videos. + */ + meta?: Record; + /** + * Indicates whether the video can be a accessed using the id. When + * set to `true`, a signed token must be generated with a signing key to view the + * video. + */ + requireSignedURLs?: boolean; + /** + * Indicates the date and time at which the video will be deleted. Omit + * the field to indicate no change, or include with a `null` value to remove an + * existing scheduled deletion. If specified, must be at least 30 days from upload + * time. + */ + scheduledDeletion?: string | null; + /** + * The timestamp for a thumbnail image calculated as a percentage value + * of the video's duration. To convert from a second-wise timestamp to a + * percentage, divide the desired timestamp by the total duration of the video. If + * this value is not set, the default thumbnail image is taken from 0s of the + * video. + */ + thumbnailTimestampPct?: number; + /** + * The identifier for the watermark profile + */ + watermarkId?: string; +}; +interface StreamScopedCaptions { + /** + * Uploads the caption or subtitle file to the endpoint for a specific BCP47 language. + * One caption or subtitle file per language is allowed. + * @param language The BCP 47 language tag for the caption or subtitle. + * @param input The caption or subtitle stream to upload. + * @returns The created caption entry. + * @throws {NotFoundError} if the video is not found + * @throws {BadRequestError} if the language or file is invalid + * @throws {InternalError} if an unexpected error occurs + */ + upload(language: string, input: ReadableStream): Promise; + /** + * Generate captions or subtitles for the provided language via AI. + * @param language The BCP 47 language tag to generate. + * @returns The generated caption entry. + * @throws {NotFoundError} if the video is not found + * @throws {BadRequestError} if the language is invalid + * @throws {StreamError} if a generated caption already exists + * @throws {StreamError} if the video duration is too long + * @throws {StreamError} if the video is missing audio + * @throws {StreamError} if the requested language is not supported + * @throws {InternalError} if an unexpected error occurs + */ + generate(language: string): Promise; + /** + * Lists the captions or subtitles. + * Use the language parameter to filter by a specific language. + * @param language The optional BCP 47 language tag to filter by. + * @returns The list of captions or subtitles. + * @throws {NotFoundError} if the video or caption is not found + * @throws {InternalError} if an unexpected error occurs + */ + list(language?: string): Promise; + /** + * Removes the captions or subtitles from a video. + * @param language The BCP 47 language tag to remove. + * @returns A promise that resolves when deletion completes. + * @throws {NotFoundError} if the video or caption is not found + * @throws {InternalError} if an unexpected error occurs + */ + delete(language: string): Promise; +} +interface StreamScopedDownloads { + /** + * Generates a download for a video when a video is ready to view. Available + * types are `default` and `audio`. Defaults to `default` when omitted. + * @param downloadType The download type to create. + * @returns The current downloads for the video. + * @throws {NotFoundError} if the video is not found + * @throws {BadRequestError} if the download type is invalid + * @throws {StreamError} if the video duration is too long to generate a download + * @throws {StreamError} if the video is not ready to stream + * @throws {InternalError} if an unexpected error occurs + */ + generate(downloadType?: StreamDownloadType): Promise; + /** + * Lists the downloads created for a video. + * @returns The current downloads for the video. + * @throws {NotFoundError} if the video or downloads are not found + * @throws {InternalError} if an unexpected error occurs + */ + get(): Promise; + /** + * Delete the downloads for a video. Available types are `default` and `audio`. + * Defaults to `default` when omitted. + * @param downloadType The download type to delete. + * @returns A promise that resolves when deletion completes. + * @throws {NotFoundError} if the video or downloads are not found + * @throws {InternalError} if an unexpected error occurs + */ + delete(downloadType?: StreamDownloadType): Promise; +} +interface StreamVideos { + /** + * Lists all videos in a users account. + * @returns The list of videos. + * @throws {BadRequestError} if the parameters are invalid + * @throws {InternalError} if an unexpected error occurs + */ + list(params?: StreamVideosListParams): Promise; +} +interface StreamWatermarks { + /** + * Generate a new watermark profile + * @param input The image stream to upload + * @param params The watermark creation parameters. + * @returns The created watermark profile. + * @throws {BadRequestError} if the parameters are invalid + * @throws {InvalidURLError} if the URL is invalid + * @throws {TooManyWatermarksError} if the number of allowed watermarks is reached + * @throws {InternalError} if an unexpected error occurs + */ + generate(input: ReadableStream, params: StreamWatermarkCreateParams): Promise; + /** + * Generate a new watermark profile + * @param url The image url to upload + * @param params The watermark creation parameters. + * @returns The created watermark profile. + * @throws {BadRequestError} if the parameters are invalid + * @throws {InvalidURLError} if the URL is invalid + * @throws {TooManyWatermarksError} if the number of allowed watermarks is reached + * @throws {InternalError} if an unexpected error occurs + */ + generate(url: string, params: StreamWatermarkCreateParams): Promise; + /** + * Lists all watermark profiles for an account. + * @returns The list of watermark profiles. + * @throws {InternalError} if an unexpected error occurs + */ + list(): Promise; + /** + * Retrieves details for a single watermark profile. + * @param watermarkId The watermark profile identifier. + * @returns The watermark profile details. + * @throws {NotFoundError} if the watermark is not found + * @throws {InternalError} if an unexpected error occurs + */ + get(watermarkId: string): Promise; + /** + * Deletes a watermark profile. + * @param watermarkId The watermark profile identifier. + * @returns A promise that resolves when deletion completes. + * @throws {NotFoundError} if the watermark is not found + * @throws {InternalError} if an unexpected error occurs + */ + delete(watermarkId: string): Promise; +} +type StreamUpdateVideoParams = { + /** + * Lists the origins allowed to display the video. Enter allowed origin + * domains in an array and use `*` for wildcard subdomains. Empty arrays allow the + * video to be viewed on any origin. + */ + allowedOrigins?: Array; + /** + * A user-defined identifier for the media creator. + */ + creator?: string; + /** + * The maximum duration in seconds for a video upload. Can be set for a + * video that is not yet uploaded to limit its duration. Uploads that exceed the + * specified duration will fail during processing. A value of `-1` means the value + * is unknown. + */ + maxDurationSeconds?: number; + /** + * A user modifiable key-value store used to reference other systems of + * record for managing videos. + */ + meta?: Record; + /** + * Indicates whether the video can be a accessed using the id. When + * set to `true`, a signed token must be generated with a signing key to view the + * video. + */ + requireSignedURLs?: boolean; + /** + * Indicates the date and time at which the video will be deleted. Omit + * the field to indicate no change, or include with a `null` value to remove an + * existing scheduled deletion. If specified, must be at least 30 days from upload + * time. + */ + scheduledDeletion?: string | null; + /** + * The timestamp for a thumbnail image calculated as a percentage value + * of the video's duration. To convert from a second-wise timestamp to a + * percentage, divide the desired timestamp by the total duration of the video. If + * this value is not set, the default thumbnail image is taken from 0s of the + * video. + */ + thumbnailTimestampPct?: number; +}; +type StreamCaption = { + /** + * Whether the caption was generated via AI. + */ + generated?: boolean; + /** + * The language label displayed in the native language to users. + */ + label: string; + /** + * The language tag in BCP 47 format. + */ + language: string; + /** + * The status of a generated caption. + */ + status?: 'ready' | 'inprogress' | 'error'; +}; +type StreamDownloadStatus = 'ready' | 'inprogress' | 'error'; +type StreamDownloadType = 'default' | 'audio'; +type StreamDownload = { + /** + * Indicates the progress as a percentage between 0 and 100. + */ + percentComplete: number; + /** + * The status of a generated download. + */ + status: StreamDownloadStatus; + /** + * The URL to access the generated download. + */ + url?: string; +}; +/** + * An object with download type keys. Each key is optional and only present if that + * download type has been created. + */ +type StreamDownloadGetResponse = { + /** + * The audio-only download. Only present if this download type has been created. + */ + audio?: StreamDownload; + /** + * The default video download. Only present if this download type has been created. + */ + default?: StreamDownload; +}; +type StreamWatermarkPosition = 'upperRight' | 'upperLeft' | 'lowerLeft' | 'lowerRight' | 'center'; +type StreamWatermark = { + /** + * The unique identifier for a watermark profile. + */ + id: string; + /** + * The size of the image in bytes. + */ + size: number; + /** + * The height of the image in pixels. + */ + height: number; + /** + * The width of the image in pixels. + */ + width: number; + /** + * The date and a time a watermark profile was created. + */ + created: string; + /** + * The source URL for a downloaded image. If the watermark profile was created via + * direct upload, this field is null. + */ + downloadedFrom: string | null; + /** + * A short description of the watermark profile. + */ + name: string; + /** + * The translucency of the image. A value of `0.0` makes the image completely + * transparent, and `1.0` makes the image completely opaque. Note that if the image + * is already semi-transparent, setting this to `1.0` will not make the image + * completely opaque. + */ + opacity: number; + /** + * The whitespace between the adjacent edges (determined by position) of the video + * and the image. `0.0` indicates no padding, and `1.0` indicates a fully padded + * video width or length, as determined by the algorithm. + */ + padding: number; + /** + * The size of the image relative to the overall size of the video. This parameter + * will adapt to horizontal and vertical videos automatically. `0.0` indicates no + * scaling (use the size of the image as-is), and `1.0 `fills the entire video. + */ + scale: number; + /** + * The location of the image. Valid positions are: `upperRight`, `upperLeft`, + * `lowerLeft`, `lowerRight`, and `center`. Note that `center` ignores the + * `padding` parameter. + */ + position: StreamWatermarkPosition; +}; +type StreamWatermarkCreateParams = { + /** + * A short description of the watermark profile. + */ + name?: string; + /** + * The translucency of the image. A value of `0.0` makes the image completely + * transparent, and `1.0` makes the image completely opaque. Note that if the + * image is already semi-transparent, setting this to `1.0` will not make the + * image completely opaque. + */ + opacity?: number; + /** + * The whitespace between the adjacent edges (determined by position) of the + * video and the image. `0.0` indicates no padding, and `1.0` indicates a fully + * padded video width or length, as determined by the algorithm. + */ + padding?: number; + /** + * The size of the image relative to the overall size of the video. This + * parameter will adapt to horizontal and vertical videos automatically. `0.0` + * indicates no scaling (use the size of the image as-is), and `1.0 `fills the + * entire video. + */ + scale?: number; + /** + * The location of the image. + */ + position?: StreamWatermarkPosition; +}; +type StreamVideosListParams = { + /** + * The maximum number of videos to return. + */ + limit?: number; + /** + * Return videos created before this timestamp. + * (RFC3339/RFC3339Nano) + */ + before?: string; + /** + * Comparison operator for the `before` field. + * @default 'lt' + */ + beforeComp?: StreamPaginationComparison; + /** + * Return videos created after this timestamp. + * (RFC3339/RFC3339Nano) + */ + after?: string; + /** + * Comparison operator for the `after` field. + * @default 'gte' + */ + afterComp?: StreamPaginationComparison; +}; +type StreamPaginationComparison = 'eq' | 'gt' | 'gte' | 'lt' | 'lte'; +/** + * Error object for Stream binding operations. + */ +interface StreamError extends Error { + readonly code: number; + readonly statusCode: number; + readonly message: string; + readonly stack?: string; +} +interface InternalError extends StreamError { + name: 'InternalError'; +} +interface BadRequestError extends StreamError { + name: 'BadRequestError'; +} +interface NotFoundError extends StreamError { + name: 'NotFoundError'; +} +interface ForbiddenError extends StreamError { + name: 'ForbiddenError'; +} +interface RateLimitedError extends StreamError { + name: 'RateLimitedError'; +} +interface QuotaReachedError extends StreamError { + name: 'QuotaReachedError'; +} +interface MaxFileSizeError extends StreamError { + name: 'MaxFileSizeError'; +} +interface InvalidURLError extends StreamError { + name: 'InvalidURLError'; +} +interface AlreadyUploadedError extends StreamError { + name: 'AlreadyUploadedError'; +} +interface TooManyWatermarksError extends StreamError { + name: 'TooManyWatermarksError'; +} +type MarkdownDocument = { + name: string; + blob: Blob; +}; +type ConversionResponse = { + id: string; + name: string; + mimeType: string; + format: 'markdown'; + tokens: number; + data: string; +} | { + id: string; + name: string; + mimeType: string; + format: 'error'; + error: string; +}; +type ImageConversionOptions = { + descriptionLanguage?: 'en' | 'es' | 'fr' | 'it' | 'pt' | 'de'; +}; +type EmbeddedImageConversionOptions = ImageConversionOptions & { + convert?: boolean; + maxConvertedImages?: number; +}; +type ConversionOptions = { + html?: { + images?: EmbeddedImageConversionOptions & { + convertOGImage?: boolean; + }; + hostname?: string; + cssSelector?: string; + }; + docx?: { + images?: EmbeddedImageConversionOptions; + }; + image?: ImageConversionOptions; + pdf?: { + images?: EmbeddedImageConversionOptions; + metadata?: boolean; + }; +}; +type ConversionRequestOptions = { + gateway?: GatewayOptions; + extraHeaders?: object; + conversionOptions?: ConversionOptions; +}; +type SupportedFileFormat = { + mimeType: string; + extension: string; +}; +declare abstract class ToMarkdownService { + transform(files: MarkdownDocument[], options?: ConversionRequestOptions): Promise; + transform(files: MarkdownDocument, options?: ConversionRequestOptions): Promise; + supported(): Promise; +} +declare namespace TailStream { + interface Header { + readonly name: string; + readonly value: string; + } + interface FetchEventInfo { + readonly type: "fetch"; + readonly method: string; + readonly url: string; + readonly cfJson?: object; + readonly headers: Header[]; + } + interface JsRpcEventInfo { + readonly type: "jsrpc"; + } + interface ScheduledEventInfo { + readonly type: "scheduled"; + readonly scheduledTime: Date; + readonly cron: string; + } + interface AlarmEventInfo { + readonly type: "alarm"; + readonly scheduledTime: Date; + } + interface QueueEventInfo { + readonly type: "queue"; + readonly queueName: string; + readonly batchSize: number; + } + interface EmailEventInfo { + readonly type: "email"; + readonly mailFrom: string; + readonly rcptTo: string; + readonly rawSize: number; + } + interface TraceEventInfo { + readonly type: "trace"; + readonly traces: (string | null)[]; + } + interface HibernatableWebSocketEventInfoMessage { + readonly type: "message"; + } + interface HibernatableWebSocketEventInfoError { + readonly type: "error"; + } + interface HibernatableWebSocketEventInfoClose { + readonly type: "close"; + readonly code: number; + readonly wasClean: boolean; + } + interface HibernatableWebSocketEventInfo { + readonly type: "hibernatableWebSocket"; + readonly info: HibernatableWebSocketEventInfoClose | HibernatableWebSocketEventInfoError | HibernatableWebSocketEventInfoMessage; + } + interface CustomEventInfo { + readonly type: "custom"; + } + interface FetchResponseInfo { + readonly type: "fetch"; + readonly statusCode: number; + } + interface ConnectEventInfo { + readonly type: "connect"; + } + type EventOutcome = "ok" | "canceled" | "exception" | "unknown" | "killSwitch" | "daemonDown" | "exceededCpu" | "exceededMemory" | "loadShed" | "responseStreamDisconnected" | "scriptNotFound" | "internalError"; + interface ScriptVersion { + readonly id: string; + readonly tag?: string; + readonly message?: string; + } + interface TracePreviewInfo { + readonly id: string; + readonly slug: string; + readonly name: string; + } + interface Onset { + readonly type: "onset"; + readonly attributes: Attribute[]; + // id for the span being opened by this Onset event. + readonly spanId: string; + readonly dispatchNamespace?: string; + readonly entrypoint?: string; + readonly executionModel: string; + readonly scriptName?: string; + readonly scriptTags?: string[]; + readonly scriptVersion?: ScriptVersion; + readonly preview?: TracePreviewInfo; + readonly info: FetchEventInfo | ConnectEventInfo | JsRpcEventInfo | ScheduledEventInfo | AlarmEventInfo | QueueEventInfo | EmailEventInfo | TraceEventInfo | HibernatableWebSocketEventInfo | CustomEventInfo; + } + interface Outcome { + readonly type: "outcome"; + readonly outcome: EventOutcome; + readonly cpuTime: number; + readonly wallTime: number; + } + interface SpanOpen { + readonly type: "spanOpen"; + readonly name: string; + // id for the span being opened by this SpanOpen event. + readonly spanId: string; + readonly info?: FetchEventInfo | JsRpcEventInfo | Attributes; + } + interface SpanClose { + readonly type: "spanClose"; + readonly outcome: EventOutcome; + } + interface DiagnosticChannelEvent { + readonly type: "diagnosticChannel"; + readonly channel: string; + readonly message: any; + } + interface Exception { + readonly type: "exception"; + readonly name: string; + readonly message: string; + readonly stack?: string; + } + interface Log { + readonly type: "log"; + readonly level: "debug" | "error" | "info" | "log" | "warn"; + readonly message: object; + } + interface DroppedEventsDiagnostic { + readonly diagnosticsType: "droppedEvents"; + readonly count: number; + } + interface StreamDiagnostic { + readonly type: 'streamDiagnostic'; + // To add new diagnostic types, define a new interface and add it to this union type. + readonly diagnostic: DroppedEventsDiagnostic; + } + // This marks the worker handler return information. + // This is separate from Outcome because the worker invocation can live for a long time after + // returning. For example - Websockets that return an http upgrade response but then continue + // streaming information or SSE http connections. + interface Return { + readonly type: "return"; + readonly info?: FetchResponseInfo; + } + interface Attribute { + readonly name: string; + readonly value: string | string[] | boolean | boolean[] | number | number[] | bigint | bigint[]; + } + interface Attributes { + readonly type: "attributes"; + readonly info: Attribute[]; + } + type EventType = Onset | Outcome | SpanOpen | SpanClose | DiagnosticChannelEvent | Exception | Log | StreamDiagnostic | Return | Attributes; + // Context in which this trace event lives. + interface SpanContext { + // Single id for the entire top-level invocation + // This should be a new traceId for the first worker stage invoked in the eyeball request and then + // same-account service-bindings should reuse the same traceId but cross-account service-bindings + // should use a new traceId. + readonly traceId: string; + // spanId in which this event is handled + // for Onset and SpanOpen events this would be the parent span id + // for Outcome and SpanClose these this would be the span id of the opening Onset and SpanOpen events + // For Hibernate and Mark this would be the span under which they were emitted. + // spanId is not set ONLY if: + // 1. This is an Onset event + // 2. We are not inheriting any SpanContext. (e.g. this is a cross-account service binding or a new top-level invocation) + readonly spanId?: string; + } + interface TailEvent { + // invocation id of the currently invoked worker stage. + // invocation id will always be unique to every Onset event and will be the same until the Outcome event. + readonly invocationId: string; + // Inherited spanContext for this event. + readonly spanContext: SpanContext; + readonly timestamp: Date; + readonly sequence: number; + readonly event: Event; + } + type TailEventHandler = (event: TailEvent) => void | Promise; + type TailEventHandlerObject = { + outcome?: TailEventHandler; + spanOpen?: TailEventHandler; + spanClose?: TailEventHandler; + diagnosticChannel?: TailEventHandler; + exception?: TailEventHandler; + log?: TailEventHandler; + return?: TailEventHandler; + attributes?: TailEventHandler; + }; + type TailEventHandlerType = TailEventHandler | TailEventHandlerObject; +} +// Copyright (c) 2022-2023 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 +/** + * Data types supported for holding vector metadata. + */ +type VectorizeVectorMetadataValue = string | number | boolean | string[]; +/** + * Additional information to associate with a vector. + */ +type VectorizeVectorMetadata = VectorizeVectorMetadataValue | Record; +type VectorFloatArray = Float32Array | Float64Array; +interface VectorizeError { + code?: number; + error: string; +} +/** + * Comparison logic/operation to use for metadata filtering. + * + * This list is expected to grow as support for more operations are released. + */ +type VectorizeVectorMetadataFilterOp = '$eq' | '$ne' | '$lt' | '$lte' | '$gt' | '$gte'; +type VectorizeVectorMetadataFilterCollectionOp = '$in' | '$nin'; +/** + * Filter criteria for vector metadata used to limit the retrieved query result set. + */ +type VectorizeVectorMetadataFilter = { + [field: string]: Exclude | null | { + [Op in VectorizeVectorMetadataFilterOp]?: Exclude | null; + } | { + [Op in VectorizeVectorMetadataFilterCollectionOp]?: Exclude[]; + }; +}; +/** + * Supported distance metrics for an index. + * Distance metrics determine how other "similar" vectors are determined. + */ +type VectorizeDistanceMetric = "euclidean" | "cosine" | "dot-product"; +/** + * Metadata return levels for a Vectorize query. + * + * Default to "none". + * + * @property all Full metadata for the vector return set, including all fields (including those un-indexed) without truncation. This is a more expensive retrieval, as it requires additional fetching & reading of un-indexed data. + * @property indexed Return all metadata fields configured for indexing in the vector return set. This level of retrieval is "free" in that no additional overhead is incurred returning this data. However, note that indexed metadata is subject to truncation (especially for larger strings). + * @property none No indexed metadata will be returned. + */ +type VectorizeMetadataRetrievalLevel = "all" | "indexed" | "none"; +interface VectorizeQueryOptions { + topK?: number; + namespace?: string; + returnValues?: boolean; + returnMetadata?: boolean | VectorizeMetadataRetrievalLevel; + filter?: VectorizeVectorMetadataFilter; +} +/** + * Information about the configuration of an index. + */ +type VectorizeIndexConfig = { + dimensions: number; + metric: VectorizeDistanceMetric; +} | { + preset: string; // keep this generic, as we'll be adding more presets in the future and this is only in a read capacity +}; +/** + * Metadata about an existing index. + * + * This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released. + * See {@link VectorizeIndexInfo} for its post-beta equivalent. + */ +interface VectorizeIndexDetails { + /** The unique ID of the index */ + readonly id: string; + /** The name of the index. */ + name: string; + /** (optional) A human readable description for the index. */ + description?: string; + /** The index configuration, including the dimension size and distance metric. */ + config: VectorizeIndexConfig; + /** The number of records containing vectors within the index. */ + vectorsCount: number; +} +/** + * Metadata about an existing index. + */ +interface VectorizeIndexInfo { + /** The number of records containing vectors within the index. */ + vectorCount: number; + /** Number of dimensions the index has been configured for. */ + dimensions: number; + /** ISO 8601 datetime of the last processed mutation on in the index. All changes before this mutation will be reflected in the index state. */ + processedUpToDatetime: number; + /** UUIDv4 of the last mutation processed by the index. All changes before this mutation will be reflected in the index state. */ + processedUpToMutation: number; +} +/** + * Represents a single vector value set along with its associated metadata. + */ +interface VectorizeVector { + /** The ID for the vector. This can be user-defined, and must be unique. It should uniquely identify the object, and is best set based on the ID of what the vector represents. */ + id: string; + /** The vector values */ + values: VectorFloatArray | number[]; + /** The namespace this vector belongs to. */ + namespace?: string; + /** Metadata associated with the vector. Includes the values of other fields and potentially additional details. */ + metadata?: Record; +} +/** + * Represents a matched vector for a query along with its score and (if specified) the matching vector information. + */ +type VectorizeMatch = Pick, "values"> & Omit & { + /** The score or rank for similarity, when returned as a result */ + score: number; +}; +/** + * A set of matching {@link VectorizeMatch} for a particular query. + */ +interface VectorizeMatches { + matches: VectorizeMatch[]; + count: number; +} +/** + * Results of an operation that performed a mutation on a set of vectors. + * Here, `ids` is a list of vectors that were successfully processed. + * + * This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released. + * See {@link VectorizeAsyncMutation} for its post-beta equivalent. + */ +interface VectorizeVectorMutation { + /* List of ids of vectors that were successfully processed. */ + ids: string[]; + /* Total count of the number of processed vectors. */ + count: number; +} +/** + * Result type indicating a mutation on the Vectorize Index. + * Actual mutations are processed async where the `mutationId` is the unique identifier for the operation. + */ +interface VectorizeAsyncMutation { + /** The unique identifier for the async mutation operation containing the changeset. */ + mutationId: string; +} +/** + * A Vectorize Vector Search Index for querying vectors/embeddings. + * + * This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released. + * See {@link Vectorize} for its new implementation. + */ +declare abstract class VectorizeIndex { + /** + * Get information about the currently bound index. + * @returns A promise that resolves with information about the current index. + */ + public describe(): Promise; + /** + * Use the provided vector to perform a similarity search across the index. + * @param vector Input vector that will be used to drive the similarity search. + * @param options Configuration options to massage the returned data. + * @returns A promise that resolves with matched and scored vectors. + */ + public query(vector: VectorFloatArray | number[], options?: VectorizeQueryOptions): Promise; + /** + * Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown. + * @param vectors List of vectors that will be inserted. + * @returns A promise that resolves with the ids & count of records that were successfully processed. + */ + public insert(vectors: VectorizeVector[]): Promise; + /** + * Upsert a list of vectors into the index dataset. If a provided id exists, it will be replaced with the new values. + * @param vectors List of vectors that will be upserted. + * @returns A promise that resolves with the ids & count of records that were successfully processed. + */ + public upsert(vectors: VectorizeVector[]): Promise; + /** + * Delete a list of vectors with a matching id. + * @param ids List of vector ids that should be deleted. + * @returns A promise that resolves with the ids & count of records that were successfully processed (and thus deleted). + */ + public deleteByIds(ids: string[]): Promise; + /** + * Get a list of vectors with a matching id. + * @param ids List of vector ids that should be returned. + * @returns A promise that resolves with the raw unscored vectors matching the id set. + */ + public getByIds(ids: string[]): Promise; +} +/** + * A Vectorize Vector Search Index for querying vectors/embeddings. + * + * Mutations in this version are async, returning a mutation id. + */ +declare abstract class Vectorize { + /** + * Get information about the currently bound index. + * @returns A promise that resolves with information about the current index. + */ + public describe(): Promise; + /** + * Use the provided vector to perform a similarity search across the index. + * @param vector Input vector that will be used to drive the similarity search. + * @param options Configuration options to massage the returned data. + * @returns A promise that resolves with matched and scored vectors. + */ + public query(vector: VectorFloatArray | number[], options?: VectorizeQueryOptions): Promise; + /** + * Use the provided vector-id to perform a similarity search across the index. + * @param vectorId Id for a vector in the index against which the index should be queried. + * @param options Configuration options to massage the returned data. + * @returns A promise that resolves with matched and scored vectors. + */ + public queryById(vectorId: string, options?: VectorizeQueryOptions): Promise; + /** + * Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown. + * @param vectors List of vectors that will be inserted. + * @returns A promise that resolves with a unique identifier of a mutation containing the insert changeset. + */ + public insert(vectors: VectorizeVector[]): Promise; + /** + * Upsert a list of vectors into the index dataset. If a provided id exists, it will be replaced with the new values. + * @param vectors List of vectors that will be upserted. + * @returns A promise that resolves with a unique identifier of a mutation containing the upsert changeset. + */ + public upsert(vectors: VectorizeVector[]): Promise; + /** + * Delete a list of vectors with a matching id. + * @param ids List of vector ids that should be deleted. + * @returns A promise that resolves with a unique identifier of a mutation containing the delete changeset. + */ + public deleteByIds(ids: string[]): Promise; + /** + * Get a list of vectors with a matching id. + * @param ids List of vector ids that should be returned. + * @returns A promise that resolves with the raw unscored vectors matching the id set. + */ + public getByIds(ids: string[]): Promise; +} +/** + * The interface for "version_metadata" binding + * providing metadata about the Worker Version using this binding. + */ +type WorkerVersionMetadata = { + /** The ID of the Worker Version using this binding */ + id: string; + /** The tag of the Worker Version using this binding */ + tag: string; + /** The timestamp of when the Worker Version was uploaded */ + timestamp: string; +}; +interface DynamicDispatchLimits { + /** + * Limit CPU time in milliseconds. + */ + cpuMs?: number; + /** + * Limit number of subrequests. + */ + subRequests?: number; +} +interface DynamicDispatchOptions { + /** + * Limit resources of invoked Worker script. + */ + limits?: DynamicDispatchLimits; + /** + * Arguments for outbound Worker script, if configured. + */ + outbound?: { + [key: string]: any; + }; +} +interface DispatchNamespace { + /** + * @param name Name of the Worker script. + * @param args Arguments to Worker script. + * @param options Options for Dynamic Dispatch invocation. + * @returns A Fetcher object that allows you to send requests to the Worker script. + * @throws If the Worker script does not exist in this dispatch namespace, an error will be thrown. + */ + get(name: string, args?: { + [key: string]: any; + }, options?: DynamicDispatchOptions): Fetcher; +} +declare module 'cloudflare:workflows' { + /** + * NonRetryableError allows for a user to throw a fatal error + * that makes a Workflow instance fail immediately without triggering a retry + */ + export class NonRetryableError extends Error { + public constructor(message: string, name?: string); + } +} +declare abstract class Workflow { + /** + * Get a handle to an existing instance of the Workflow. + * @param id Id for the instance of this Workflow + * @returns A promise that resolves with a handle for the Instance + */ + public get(id: string): Promise; + /** + * Create a new instance and return a handle to it. If a provided id exists, an error will be thrown. + * @param options Options when creating an instance including id and params + * @returns A promise that resolves with a handle for the Instance + */ + public create(options?: WorkflowInstanceCreateOptions): Promise; + /** + * Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown. + * `createBatch` is limited at 100 instances at a time or when the RPC limit for the batch (1MiB) is reached. + * @param batch List of Options when creating an instance including name and params + * @returns A promise that resolves with a list of handles for the created instances. + */ + public createBatch(batch: WorkflowInstanceCreateOptions[]): Promise; +} +type WorkflowDurationLabel = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; +type WorkflowSleepDuration = `${number} ${WorkflowDurationLabel}${'s' | ''}` | number; +type WorkflowRetentionDuration = WorkflowSleepDuration; +interface WorkflowInstanceCreateOptions { + /** + * An id for your Workflow instance. Must be unique within the Workflow. + */ + id?: string; + /** + * The event payload the Workflow instance is triggered with + */ + params?: PARAMS; + /** + * The retention policy for Workflow instance. + * Defaults to the maximum retention period available for the owner's account. + */ + retention?: { + successRetention?: WorkflowRetentionDuration; + errorRetention?: WorkflowRetentionDuration; + }; +} +type InstanceStatus = { + status: 'queued' // means that instance is waiting to be started (see concurrency limits) + | 'running' | 'paused' | 'errored' | 'terminated' // user terminated the instance while it was running + | 'complete' | 'waiting' // instance is hibernating and waiting for sleep or event to finish + | 'waitingForPause' // instance is finishing the current work to pause + | 'unknown'; + error?: { + name: string; + message: string; + }; + output?: unknown; +}; +interface WorkflowError { + code?: number; + message: string; +} +declare abstract class WorkflowInstance { + public id: string; + /** + * Pause the instance. + */ + public pause(): Promise; + /** + * Resume the instance. If it is already running, an error will be thrown. + */ + public resume(): Promise; + /** + * Terminate the instance. If it is errored, terminated or complete, an error will be thrown. + */ + public terminate(): Promise; + /** + * Restart the instance. + */ + public restart(): Promise; + /** + * Returns the current status of the instance. + */ + public status(): Promise; + /** + * Send an event to this instance. + */ + public sendEvent({ type, payload, }: { + type: string; + payload: unknown; + }): Promise; +} diff --git a/cloudflare/edge-api/wrangler.jsonc b/cloudflare/edge-api/wrangler.jsonc new file mode 100644 index 0000000000..b45e10668c --- /dev/null +++ b/cloudflare/edge-api/wrangler.jsonc @@ -0,0 +1,61 @@ +/** + * For more details on how to configure Wrangler, refer to: + * https://developers.cloudflare.com/workers/wrangler/configuration/ + */ +{ + "$schema": "node_modules/wrangler/config-schema.json", + "name": "edge-api", + "main": "src/index.ts", + "compatibility_date": "2026-05-09", + "observability": { + "enabled": true + }, + "upload_source_maps": true, + "compatibility_flags": [ + "nodejs_compat" + ], + "kv_namespaces": [ + { + "binding": "COUNTER_KV", + "id": "e4ba4a6ed0f9491294fd7f4fde51eb7f" + }, + ], + "vars": { + "APP_NAME": "my-edge-api-app", + "COURSE_NAME": "devops-core", + }, + "secrets": { + "required": [ + "API_TOKEN", + "ADMIN_EMAIL" + ], + }, + /** + * Smart Placement + * https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement + */ + // "placement": { "mode": "smart" } + /** + * Bindings + * Bindings allow your Worker to interact with resources on the Cloudflare Developer Platform, including + * databases, object storage, AI inference, real-time communication and more. + * https://developers.cloudflare.com/workers/runtime-apis/bindings/ + */ + /** + * Environment Variables + * https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables + * Note: Use secrets to store sensitive data. + * https://developers.cloudflare.com/workers/configuration/secrets/ + */ + // "vars": { "MY_VARIABLE": "production_value" } + /** + * Static Assets + * https://developers.cloudflare.com/workers/static-assets/binding/ + */ + // "assets": { "directory": "./public/", "binding": "ASSETS" } + /** + * Service Bindings (communicate between multiple Workers) + * https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings + */ + // "services": [ { "binding": "MY_SERVICE", "service": "my-service" } ] +} \ No newline at end of file diff --git a/data/visits b/data/visits new file mode 100644 index 0000000000..d8263ee986 --- /dev/null +++ b/data/visits @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/labs/.DS_Store b/labs/.DS_Store new file mode 100644 index 0000000000..6c0065a21b Binary files /dev/null and b/labs/.DS_Store differ diff --git a/labs/lab05.md b/labs/lab05.md index a76d4960aa..16b3febf17 100644 --- a/labs/lab05.md +++ b/labs/lab05.md @@ -94,7 +94,7 @@ ansible/ │ │ │ └── main.yml │ │ └── defaults/ │ │ └── main.yml -│ └── app_deploy/ # Application deployment +│ └── web_app/ # Application deployment │ ├── tasks/ │ │ └── main.yml │ ├── handlers/ @@ -523,7 +523,7 @@ vault_password_file = .vault_pass #### 3.2 Create Application Deployment Role -Create `roles/app_deploy/tasks/main.yml`: +Create `roles/web_app/tasks/main.yml`: **Required Tasks:** 1. Log in to Docker Hub (using vaulted credentials) @@ -538,10 +538,10 @@ Create `roles/app_deploy/tasks/main.yml`: 6. Wait for application to be ready (port check) 7. Verify health endpoint -**Create `roles/app_deploy/handlers/main.yml`:** +**Create `roles/web_app/handlers/main.yml`:** - Handler to restart application container -**Create `roles/app_deploy/defaults/main.yml`:** +**Create `roles/web_app/defaults/main.yml`:** - Default port - Default restart policy - Default environment variables @@ -611,7 +611,7 @@ Create `playbooks/deploy.yml`: become: yes roles: - - app_deploy + - web_app ``` #### 3.4 Run Deployment @@ -652,7 +652,7 @@ Create `ansible/docs/LAB05.md` with these sections: #### 2. Roles Documentation -For each role (common, docker, app_deploy): +For each role (common, docker, web_app): - **Purpose**: What does this role do? - **Variables**: Key variables and defaults - **Handlers**: What handlers are defined? @@ -856,7 +856,7 @@ Ansible has official plugins for major clouds. **Setup & Structure (2 pts):** - [ ] Proper role-based directory structure created -- [ ] All three roles created (common, docker, app_deploy) +- [ ] All three roles created (common, docker, web_app) - [ ] Each role has appropriate tasks, handlers, and defaults - [ ] Ansible.cfg configured correctly - [ ] Inventory configured and connectivity tested @@ -872,7 +872,7 @@ Ansible has official plugins for major clouds. **Application Deployment (2 pts):** - [ ] Ansible Vault used for credentials - [ ] Vault file encrypted (verify with `ansible-vault view`) -- [ ] App_deploy role complete with all required tasks +- [ ] web_app role complete with all required tasks - [ ] Deploy playbook uses role correctly - [ ] Container running with proper configuration - [ ] Health check verification included diff --git a/labs/lab06.md b/labs/lab06.md index c4405cbc47..ea1cd40516 100644 --- a/labs/lab06.md +++ b/labs/lab06.md @@ -167,12 +167,12 @@ ansible-playbook playbooks/provision.yml --tags "docker_install" **Action Required:** ```bash cd ansible/roles -mv app_deploy web_app +mv web_app web_app ``` **Update all references:** -- Playbook imports: `roles/app_deploy` → `roles/web_app` -- Documentation: app_deploy → web_app +- Playbook imports: `roles/web_app` → `roles/web_app` +- Documentation: web_app → web_app - Variable prefixes: Consider `web_app_*` for consistency **Why rename?** @@ -257,7 +257,7 @@ ansible-playbook playbooks/deploy.yml 2. Template docker-compose.yml to the directory 3. Use `docker_compose` module (or `community.docker.docker_compose`) 4. Ensure idempotency (check if already running) -5. Add appropriate tags: `app_deploy`, `compose` +5. Add appropriate tags: `web_app`, `compose` **Deployment Block Pattern:** ```yaml @@ -278,7 +278,7 @@ ansible-playbook playbooks/deploy.yml # Log error, optionally rollback tags: - - app_deploy + - web_app - compose ``` @@ -647,7 +647,7 @@ deploy: echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > /tmp/vault_pass ansible-playbook playbooks/deploy.yml \ --vault-password-file /tmp/vault_pass \ - --tags "app_deploy" + --tags "web_app" rm /tmp/vault_pass ``` diff --git a/labs/lab18/app_python/Dockerfile b/labs/lab18/app_python/Dockerfile new file mode 100644 index 0000000000..22740e5e88 --- /dev/null +++ b/labs/lab18/app_python/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.13-slim AS builder +WORKDIR /app_python +COPY requirements.txt . +RUN pip install --upgrade pip && \ + pip install --no-cache-dir -r requirements.txt +COPY app.py . + +FROM python:3.13-slim +ENV PORT="12345" +EXPOSE 12345 +RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/* +RUN useradd --create-home --shell /bin/bash newuser +WORKDIR /app +RUN mkdir -p /app/data && chown -R newuser:newuser /app +COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages +COPY --from=builder ./app_python . +USER newuser +CMD ["python", "app.py"] \ No newline at end of file diff --git a/labs/lab18/app_python/README.md b/labs/lab18/app_python/README.md new file mode 100644 index 0000000000..14c94fc1da --- /dev/null +++ b/labs/lab18/app_python/README.md @@ -0,0 +1,109 @@ +# User-facing documentation + +## Overview + +When acessed, this web service answers with information either about itself (path "/") or with its health status ("/health). The information includes fields with the details of the service, system, runtime, request and enpoints. + +## Prerequisites + +### Python version + +Pyenv is used for the virtual environment managing. To install it refer to [this website](https://akrabat.com/creating-virtual-environments-with-pyenv/) for instructions. + +Python version used: 3.12.9 + + +### Python libraries: +- jsonify imported from Flask library +- request imported from Flask library +- platform +- socket +- datetime imported from datetime +- os +- logging + +### Installation + +```bash +pyenv virtualenv 3.12.9 webapp +pyenv activate webapp +pip install -r requirements.txt + ``` +### Running the Application +```bash +python app.py +PORT=8080 python app.py # if you want to use a custom port (the default is 5000) +``` + +### API Endpoints ### +- `GET /` - Service and system information +- `GET /health` - Health check +- `GET /visits` - See the number of times root directory was accessed +- `GET /ready` - Check if the application is ready to take requests +- `GET /health` - Check if the application is dead and should be reloaded + +### Configuration ### + +| № | Var name | Description +| ---| ----- | --------------------| +| #1 | HOST | Name of the host (defaults to socket.hostname first if available) | +| #2 | PORT | Port for the application access | +| #3 | DEBUG | Debug status | + +### Docker container ### + +#### Building the image locally ### + +Use the command in format: +```bash +docker build -t your-docker-username/image-name:image-tag directory-with-dockerfile +``` +Example: +```bash +docker build -t fountainer/my-app:1.1.0 . +``` +#### Running a container ### + +Use the command in format: +```bash +docker run -p host-port:container-port image-name:tag +``` +Example: +```bash +docker run -p 12345:12345 fountainer/my-app:1.1.0 +``` +#### Pulling from Docker Hub ### + +```bash +docker image pull image-name:image-tag +``` + +Example: +```bash +docker image pull app:1.0.0 +``` + +### Testing ### + +#### Workflow Badge + +[![My FLask App Testing](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/python-ci.yml/badge.svg?branch=master)](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/python-ci.yml) + +#### Unit testing #### + +To test the ./ endpoint: + +```bash +pytest ./app_python/tests/test_home_endpoint.py +``` + +To test the ./health endpoint: + + +```bash +pytest ./app_python/tests/test_health_endpoint.py +``` + +## Ansible deployment + +[![Ansible Deployment](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/ansible-deploy.yml/badge.svg)](https://github.com/ffountainer/DevOps-Core-Course/actions/workflows/ansible-deploy.yml) \ No newline at end of file diff --git a/labs/lab18/app_python/__pycache__/app.cpython-311.pyc b/labs/lab18/app_python/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000000..dddb1c09a5 Binary files /dev/null and b/labs/lab18/app_python/__pycache__/app.cpython-311.pyc differ diff --git a/labs/lab18/app_python/__pycache__/app.cpython-312.pyc b/labs/lab18/app_python/__pycache__/app.cpython-312.pyc new file mode 100644 index 0000000000..00e92d3c94 Binary files /dev/null and b/labs/lab18/app_python/__pycache__/app.cpython-312.pyc differ diff --git a/labs/lab18/app_python/ansible/.vault_pass.txt b/labs/lab18/app_python/ansible/.vault_pass.txt new file mode 100644 index 0000000000..d9d37937a0 --- /dev/null +++ b/labs/lab18/app_python/ansible/.vault_pass.txt @@ -0,0 +1 @@ +1967 diff --git a/labs/lab18/app_python/ansible/ansible.cfg b/labs/lab18/app_python/ansible/ansible.cfg new file mode 100644 index 0000000000..18d1667515 --- /dev/null +++ b/labs/lab18/app_python/ansible/ansible.cfg @@ -0,0 +1,12 @@ +[defaults] +inventory = inventory/hosts.ini +roles_path = roles +host_key_checking = False +remote_user = ubuntu +retry_files_enabled = False +vault_password_file = .vault_pass.txt + +[privilege_escalation] +become = True +become_method = sudo +become_user = root \ No newline at end of file diff --git a/labs/lab18/app_python/ansible/docs/LAB05.md b/labs/lab18/app_python/ansible/docs/LAB05.md new file mode 100644 index 0000000000..082dcec5d2 --- /dev/null +++ b/labs/lab18/app_python/ansible/docs/LAB05.md @@ -0,0 +1 @@ +!!!!!! real LAB05.md documentation lies in the app_python/docs/LAB05.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/labs/lab18/app_python/ansible/docs/LAB06.md b/labs/lab18/app_python/ansible/docs/LAB06.md new file mode 100644 index 0000000000..72d34032cc --- /dev/null +++ b/labs/lab18/app_python/ansible/docs/LAB06.md @@ -0,0 +1 @@ +!!!!!! real LAB06.md documentation lies in the app_python/docs/LAB06.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/labs/lab18/app_python/ansible/group_vars/all.yml b/labs/lab18/app_python/ansible/group_vars/all.yml new file mode 100644 index 0000000000..f7b89f4e85 --- /dev/null +++ b/labs/lab18/app_python/ansible/group_vars/all.yml @@ -0,0 +1,151 @@ +$ANSIBLE_VAULT;1.1;AES256 +36383635313638313166363365366631613832376366383661656134616538376431656531323930 +3363646335643864363035353832653664313631666539620a656263383835386438386434323739 +36313562376437353331663738663530653933363231323165306138666665666633303665333735 +6661623736653239380a356335353765316132303332333064386430373461346562373130366232 +33333039363436663464326562346462633766383630626538396131373463653933326331306233 +31356138353237623563646533626466303735663939313032626235656661313761613234613561 +66306633643831616638633161316333353563326532386164613334323739326435333162313336 +38366436343861343634326635663739313730643933396335613063626632393865366366326364 +61333238373039616232366431383934636339303631386664373131363663636531376564376362 +61313337613134643736336663663966386230346338643462303736323665316439343562636366 +36393065333939313439623265343561363238393934343566353762373664363963383532396566 +63323139626635303663633730646131303065356433656535653362333232656662363137393462 +38356265386264643462356565663738333733616463666663323761613431386534396337636566 +65643838333566613662386339323964333736356136343337633564336234656366396233323331 +31313931636130636463633361363135393832363465633937663464313330626462626337616166 +65353134353665626264323931633264383837333034373837623933633937383739636236383738 +35306638376436396438653761663461323830343331313266656135343636613065663164303632 +31653965666330303032373731363962623764343231306362323636616230623432633631666663 +31646236643165623865346164353537363965313033343837336262353564336561616239323437 +36623835373064393333383135356634306435386538333939363163336334643639363034633832 +61376530396638303838356564613039323861376630386332616238306532623431366361633361 +65316531333631633631653465646263633664313839373335616435613430653436363637646333 +66393837383666303432326434373731333162373932663265656633663361383033386130393861 +36366436613031363035316463643839303565623266663533306536386563326333333030623166 +64336333356462323932393561643064366261323338326134336231313335393464393132653438 +31633763666261313537393936336565633235303035653839616333613834623632663564643766 +32383166393132313439346336393835383336383033313861376566363933663830343233613036 +32363163303436323362393730363539336439323263666530633230313538366361343965363139 +66313531633839356530663763373661356362303830326634306464363932346165663766626463 +64356565623862656432363964633061313234313934636538626665373433653235633965656461 +39366161343633393936306634643866643533333935346430373161313865393063333234613733 +30623265316534363731393464326436343435343261336333616364666231616663386264316536 +63386130383836616466643965386337623433353265383430383462383461303030376463346662 +65366531613935383635633630613233303030643537623463356136373365346539366265626233 +38663535396435613266353130323039646464633939353064346531653137383030363162646234 +38316533623630396335303266396533383238356366323962303034393131663663613731343634 +64613237393662656337626635326434316163316435623466313636376561633534373361323836 +38366362346232636665363036653634383539306364393562383034663434653565333132336633 +36313839616464343734646237326162306130356237653766356361393431653361643637313938 +65316532353664363039363330306633616665343236616635366130353130396331383331343435 +61393061623932616237313065653137313538623334653632326232386636306362666162653233 +61326535356638316432313337383030303964383835373063393564356264646133623665623962 +33356636303632346366633936613736303630346365663963663634373634666431646365303765 +37623063393236636537303939316665626139633765636638366634623830363139353033613238 +31356337353039623337633932393338393538386265363662376665336237346630666438323262 +34366665616132623061616138396466396332313030316139623664353637633563303538643039 +33666163613761313361303039336462333838653534383038366634363666336632663631326131 +30356365626337303637353864323434616638613831346638623131303462356166346534343562 +38616262323134633034313433383930653362383236613866656561633133376535326663326562 +37356136613262343362663564316564626132653433393661663761316463643132346131313837 +61613661376537616134363533653532356566633037643733383265353861353263356262393462 +38633663343465303532356138316135336561303363353730653230306638623830356535383531 +34316463336434643466346262393337363933613763306130376163363039333330633236353664 +62333338333139666531336432363634636238393533376666666138373130366632303836653539 +38363934343830393633633538613035613235373863376564356163383438623064356665393338 +39363262333163653530386264363333626436353535373032333135356532626336356362643963 +31663862353934616432343537366431363830396130613931313830353134363162376436613737 +62343435373664336637646664353736656239643965666530333630313666396666346334393735 +32656433313030323938376162396364313830383437393636643863363264353930353833313131 +61353333363733663061386238653139343966313865316434626133386335393937376331313965 +61656234383166386633613838633330393065313931666238653865303664393733393732393037 +32343639396230316339656439393164313637323334656130376338356532326535666130356539 +64656161373430333130383931616333343130346664346163666331646461323164373066653934 +34353161373533633763663233343535666637343738663033346332653863663365633963393036 +38303863323733366466303865396235366566643766643862316364353238333563373432386136 +63383662316538633338616436356235313830666165383564393764643861333635623935373639 +34396632363262333330306531376534373635336563333963323966613665626139343466613236 +32313235363236306238666662666330666161616534396533346363353164376263633934356133 +31656561656464353437393563656537663339396236313461363635386237666562313636666437 +32323763633063643039643331633265653037616132343161326238613265316239323165646564 +34306463313238393234623031353465653864313435376530663239653432633739316635316632 +31306566643765653036353233643130306531306435356335323231633966623633303532343066 +32623332646533643762376261386133613362333633353465613066343064373635343335303535 +36663731353532373133663534353832323432333435646236366230356336386430646264373933 +62333637626431333061633638303139656564636435636639306231613961393235386666616131 +64663534623861366635616662653331656366373632646264613764333239336232666634353636 +35643033313337383364383866303638326137366238396561643238616638323133386663666364 +66343330343331316434643065653766626334363332636633323835396531613264646631653861 +32396130313432363561396637316134373031633734376438666232636139383838623164353264 +35633666643832653962323935613338313536323331313138363333343136376666363332643363 +31666133343661613531323938623362343664333062666662626335653462313866626562643939 +39653832656334393133366636323865346639643262316139383431343539373064373034333432 +30323737646463636632383536386539373863353730303362336232383465353466333362393235 +65353332373331376538383335373538326566626238326434363666616633316634313731633739 +39376165323263363437323331653931323939353361343261313565366561316634386265393435 +32346566306332613233316634663836643331653664343562343632316336383964383166313862 +61643337373636623230353265383736626236383633643266643537386438663963396166343332 +30393738663131656332303765336336363635326265626537353966636130306662353963353866 +32346331663536663638326538616361386131373830623535636263313632636663646164353332 +65653866633137396435653765366564663938323964303231326239366566343061346563393636 +39373865326135306262633033303736303865623532393631616564643464643063613964303237 +33613336323131306632303531343436656331356636613132346661636365303261383565393135 +36353338393431313866313837393065363039383932646361366536636339333164616162643561 +33646535636366666534336435663939613134643435613538383334306532356366346235373039 +64336230363230306364313963646132346134636436316230363165383766316339313164353333 +37303364306365643738613637356361353534356237633164373839343432376634356666383435 +34313566353938333734303839353731346566313662663837363735323733643763656339613964 +62313465323233363035353335333263643236376365366261646266393062666436306465663430 +31633933336330353563336232356466393536393731373235326132626161343837336133313133 +39383661326534366165663166323431663235363435383937663465323534333065346337356165 +33636638383133626265626238666638316331383666323335373761393132356334386530396336 +38633634343434343936643431633838306635626663663338343631376364303462313663326639 +38346264613937373138613863386336326263366236336630343531373837386139653763326530 +32333533663436666562663434623431383231386665353062613862353337356335303036613362 +34363033356438353831356233633834393738636330343162623235633661383736303964613336 +66323666646330623961626235633437353433343334356163303065393731346162666136663530 +38353565353836326532316661363339373065343233333335363163613230386230613564646562 +39663330306339336133343235316261326537346362653265653338323739313839313063333630 +39636631383937656662343333343234313066326439396162353839323836636461316562373863 +37316562323363653564316430373864336264623535666566373535383936303662346561653734 +36343465343031323235613539373434623031653665343130366565306438393666333264306331 +38313233376562366666323132646630313134333134366364313164306237643833616331616139 +36623331333063306636383936666263316661366433373834376533356233386233376431333535 +62353166373261643766373934633034303561633433653932633436623164323333383833313065 +31363632616361376132616231386633323037353261333766646538613937383364646338656539 +32313565353032663663396266303862646236373137333666313939396332653836663366323665 +32643561373534306539333566343362653632623433333737313234396239323336343061643831 +63666632346662666331303037303165396134623430626631313565376633316434313932343964 +33366434643535373062313233666466396134356666653566666330343137346131336237646562 +34343164636633653134646337336230306531393666633937313137373033396337643061363462 +65653632663265643663313565313064656364313565623038393862356234373333613561353430 +34643933613766356439643835653965643336643565373462656132356264373633363637363031 +63616264623966303437633030636664613636353465353963636530636635366165313036366131 +62363036643964343338636433313237396165663163316462613839366335646264393162356161 +62353532666162616434306138646236333865626662323764323030393763306662313335333534 +62636435393165636566663337666231333733373163386161376464396364653236653533366464 +35623466306166353530646534363061643764343563623064396165633664313930373033333263 +61373731636137303435343863336330323630343531313666303338613036373435356662303464 +32613731303166373132643364633263343134383837393730323836633631333739663136616437 +33306261353639373332366361333662333433326262323633326337303362366166376338313039 +61313531383665363132636565643164303362653239656666333930353066666235613938356262 +62333939616231616363643264306438386263643064646261363862333732616664303931613633 +34373934313863346462653261303130343136346432626339386363623130303639346461663766 +64356364376438656533313566346365346439633437383833373631323231373439303335366331 +63303935656234623262623337353731636166613166363936303936383937386564326134333563 +64616365646464353436343934383763653734303461396130643538326230383235306661353433 +61383039356634376564323235373566326363306432373332356266636335353935306638333466 +66396662393462646435333738646537616161616461373037373733316131386139346130356230 +61356466383835386566313036626564613065623133656434393637383463636666376634306365 +39626364383161366235316663396530313234626366376532643061623539313232646564373239 +35313563633832636430653936653038376662626663343463336233613634343630306335356530 +37373734376363653330333334353365366264363535653635653736663563396135366335636565 +63613738623539353064616137303164323363323564373636346135633930626131366533373737 +62616532353331386461633037383934623030643131613135303163313338353562323930356635 +39653837666365653032356336306433666430353063613132333865323466386436303537366634 +34346532336362383432336564376133633439393961346134376166343437386461303332396438 +30653630386533356237646162393537376231646661393037643831383165316566356532356638 +32376532353665663561303532363964343938653738336339396463663633306237346332646166 +35333334643434653433613435613663383139376666626432623261393134623435623331363063 +3336393739373562383537623037626335643036316261323866 diff --git a/labs/lab18/app_python/ansible/inventory/hosts.ini b/labs/lab18/app_python/ansible/inventory/hosts.ini new file mode 100644 index 0000000000..af7b76d3fc --- /dev/null +++ b/labs/lab18/app_python/ansible/inventory/hosts.ini @@ -0,0 +1,7 @@ +[webservers] +terraform ansible_host=93.77.189.231 + +[webservers:vars] +ansible_user=ubuntu +ansible_python_interpreter=/usr/bin/python3 +ansible_ssh_private_key_file=~/.ssh/devops-terraform-passwordless \ No newline at end of file diff --git a/labs/lab18/app_python/ansible/playbooks/deploy.yml b/labs/lab18/app_python/ansible/playbooks/deploy.yml new file mode 100644 index 0000000000..95174b9e0e --- /dev/null +++ b/labs/lab18/app_python/ansible/playbooks/deploy.yml @@ -0,0 +1,7 @@ +--- +- name: Deploy application + hosts: webservers + become: true + + roles: + - web_app diff --git a/labs/lab18/app_python/ansible/playbooks/provision.yml b/labs/lab18/app_python/ansible/playbooks/provision.yml new file mode 100644 index 0000000000..6334c412cc --- /dev/null +++ b/labs/lab18/app_python/ansible/playbooks/provision.yml @@ -0,0 +1,12 @@ +--- +- name: Provision web servers + hosts: webservers + become: true + + roles: + - role: common + tags: + - common + - role: docker + tags: + - docker diff --git a/labs/lab18/app_python/ansible/roles/common/defaults/main.yml b/labs/lab18/app_python/ansible/roles/common/defaults/main.yml new file mode 100644 index 0000000000..ec12cf9268 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/common/defaults/main.yml @@ -0,0 +1,12 @@ +--- +common_packages: + - vim + - git + - curl + - python3-pip + - htop + +common_timezone_name: "Europe/Moscow" + +common_users: + - ubuntu diff --git a/labs/lab18/app_python/ansible/roles/common/tasks/main.yml b/labs/lab18/app_python/ansible/roles/common/tasks/main.yml new file mode 100644 index 0000000000..72042655d6 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/common/tasks/main.yml @@ -0,0 +1,47 @@ +--- +# the first block with package installation tasks +- name: Install dependencies + become: true + tags: + - packages + block: + - name: Update apt cache + ansible.builtin.apt: + update_cache: true + cache_valid_time: 3600 + - name: Install common_packages + ansible.builtin.apt: + name: "{{ common_packages }}" + state: present + rescue: + - name: Handle installation failure + ansible.builtin.apt: + update_cache: true + force_apt_get: true + always: + - name: Write log file + ansible.builtin.copy: + content: "Common role has finished" + dest: /tmp/common.log + mode: "0644" + # The second block with user creation +- name: Manage users + become: true + tags: + - users + block: + - name: Create common users + ansible.builtin.user: + name: "{{ item }}" + state: present + create_home: true + loop: "{{ common_users }}" + # the third block with timezone set up +- name: Ensure timezone is set + become: true + tags: + - timezone + block: + - name: Set timezome + community.general.timezone: + name: "{{ common_timezone_name }}" diff --git a/labs/lab18/app_python/ansible/roles/docker/defaults/main.yml b/labs/lab18/app_python/ansible/roles/docker/defaults/main.yml new file mode 100644 index 0000000000..5638745c1e --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/docker/defaults/main.yml @@ -0,0 +1,8 @@ +--- +docker_packages: + - docker-ce + - docker-ce-cli + - containerd.io +docker_user: "ubuntu" +docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" diff --git a/labs/lab18/app_python/ansible/roles/docker/handlers/main.yml b/labs/lab18/app_python/ansible/roles/docker/handlers/main.yml new file mode 100644 index 0000000000..07aa0eb290 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/docker/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: Restart docker + ansible.builtin.service: + name: docker + state: restarted diff --git a/labs/lab18/app_python/ansible/roles/docker/tasks/main.yml b/labs/lab18/app_python/ansible/roles/docker/tasks/main.yml new file mode 100644 index 0000000000..58c0a6981d --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/docker/tasks/main.yml @@ -0,0 +1,66 @@ +--- +- name: Docker installation + tags: + - docker_install + become: true + # block with docker installation and set up + block: + - name: Install prerequisites + ansible.builtin.apt: + name: + - apt-transport-https + - ca-certificates + - curl + - gnupg + - lsb-release + state: present + update_cache: true + force_apt_get: true + - name: Add Docker GPG key + ansible.builtin.apt_key: + url: "{{ docker_gpg_url }}" + state: present + - name: Add Docker APT repository + ansible.builtin.apt_repository: + repo: "{{ docker_repo }}" + state: present + filename: docker + - name: Install Docker packages + ansible.builtin.apt: + name: "{{ docker_packages }}" + state: present + update_cache: true + force_apt_get: true + notify: Restart docker + - name: Install python3-docker for Ansible Docker modules + ansible.builtin.apt: + name: python3-docker + state: present + rescue: + - name: Wait before retry + ansible.builtin.pause: + seconds: 10 + - name: Retry apt update + ansible.builtin.apt: + update_cache: true + - name: Retry adding Docker GPG key + ansible.builtin.apt_key: + url: "{{ docker_gpg_url }}" + state: present + always: + - name: Ensure docker service is enabled + ansible.builtin.service: + name: docker + state: started + enabled: true +- name: Configure docker + tags: + - docker_config + become: true + # block with docker configuration + block: + - name: Add user to Docker group + ansible.builtin.user: + name: "{{ docker_user }}" + groups: docker + append: true diff --git a/labs/lab18/app_python/ansible/roles/web_app/defaults/main.yml b/labs/lab18/app_python/ansible/roles/web_app/defaults/main.yml new file mode 100644 index 0000000000..fd4cb65878 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/defaults/main.yml @@ -0,0 +1,17 @@ +--- +web_app_container_port: "12345" +web_app_restart_policy: unless-stopped +web_app_compose_project_dir: "/opt/{{ app_name }}" +web_app_docker_image: "{{ dockerhub_username }}/{{ app_name }}" +web_app_docker_tag: latest + +web_app_env: + PORT: "{{ web_app_container_port }}" + +# Wipe Logic Control +web_app_wipe: false # Default: do not wipe + +# Usage documentation: +# Set to true to remove application completely +# Wipe only: ansible-playbook deploy.yml -e "web_app_wipe=true" --tags web_app_wipe +# Clean install: ansible-playbook deploy.yml -e "web_app_wipe=true" diff --git a/labs/lab18/app_python/ansible/roles/web_app/handlers/main.yml b/labs/lab18/app_python/ansible/roles/web_app/handlers/main.yml new file mode 100644 index 0000000000..ad70fe1fa6 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/handlers/main.yml @@ -0,0 +1,6 @@ +--- +- name: Restart app container + community.docker.docker_container: + name: "{{ app_container_name }}" + state: started + restart: false diff --git a/labs/lab18/app_python/ansible/roles/web_app/meta/main.yml b/labs/lab18/app_python/ansible/roles/web_app/meta/main.yml new file mode 100644 index 0000000000..cb7d8e0460 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - role: docker diff --git a/labs/lab18/app_python/ansible/roles/web_app/tasks/main.yml b/labs/lab18/app_python/ansible/roles/web_app/tasks/main.yml new file mode 100644 index 0000000000..1536ed6da0 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/tasks/main.yml @@ -0,0 +1,44 @@ +--- +- name: Deploy application with Docker Compose + tags: + - app_deploy + - compose + # block for app deployment with docker compose and possible wipe logic + block: + - name: Include wipe tasks + ansible.builtin.include_tasks: wipe.yml + tags: + - web_app_wipe + + - name: Create application directory + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}" + state: directory + owner: root + group: root + mode: "0755" + + - name: Template docker-compose.yml + ansible.builtin.template: + src: docker-compose.yml.j2 + dest: "{{ web_app_compose_project_dir }}/docker-compose.yml" + mode: "0644" + + - name: Deploy with Docker Compose + community.docker.docker_compose_v2: + project_src: "{{ web_app_compose_project_dir }}" + pull: missing + state: present + recreate: auto + + rescue: + - name: Handle deployment failure + ansible.builtin.debug: + msg: "Docker Compose deployment failed. Check logs and network." + + always: + - name: Log deployment attempt + ansible.builtin.copy: + content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + dest: "/tmp/{{ app_name }}-compose.log" + mode: "0644" diff --git a/labs/lab18/app_python/ansible/roles/web_app/tasks/wipe.yml b/labs/lab18/app_python/ansible/roles/web_app/tasks/wipe.yml new file mode 100644 index 0000000000..0db3164ba0 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/tasks/wipe.yml @@ -0,0 +1,27 @@ +--- +- name: Wipe web application + when: web_app_wipe | bool + tags: + - web_app_wipe + block: + - name: Stop and remove containers + community.docker.docker_compose_v2: + project_src: "{{ web_app_compose_project_dir }}" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Remove docker-compose file + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}/docker-compose.yml" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Remove application directory + ansible.builtin.file: + path: "{{ web_app_compose_project_dir }}" + state: absent + ignore_errors: "{{ ansible_check_mode }}" + + - name: Log wipe completion + ansible.builtin.debug: + msg: "Application {{ app_name }} wiped successfully" diff --git a/labs/lab18/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 b/labs/lab18/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 new file mode 100644 index 0000000000..e373f7d510 --- /dev/null +++ b/labs/lab18/app_python/ansible/roles/web_app/templates/docker-compose.yml.j2 @@ -0,0 +1,22 @@ +version: '{{ docker_compose_version }}' + +services: + {{ app_name }}: + image: {{ web_app_docker_image }}:{{ web_app_docker_tag }} + + ports: + - "{{ app_port }}:{{ app_internal_port }}" + + restart: unless-stopped + + environment: +{% for key, value in web_app_env.items() %} + {{ key }}: "{{ value }}" +{% endfor %} + + networks: + - {{ app_name }}_network + +networks: + {{ app_name }}_network: + driver: bridge \ No newline at end of file diff --git a/labs/lab18/app_python/app.py b/labs/lab18/app_python/app.py new file mode 100644 index 0000000000..1439054f2d --- /dev/null +++ b/labs/lab18/app_python/app.py @@ -0,0 +1,350 @@ +#!/usr/bin/python3 + +""" +DevOps Info Service +Lab 1. Veronika Levasheva +""" + +from flask import Flask, jsonify +from flask import request +import platform +from datetime import datetime +import socket +import os +import logging +from pythonjsonlogger.json import JsonFormatter +from prometheus_client import Counter, Histogram, Gauge, generate_latest +import time +from flask import g, Response +from threading import Lock + +app = Flask(__name__) # creating an instance of Flask + + +logger = logging.getLogger(__name__) + + +# Log important events: startup, HTTP requests, errors +# Include context: method, path, status code, client IP + +logHandler = logging.StreamHandler() +formatter = JsonFormatter( + "levelname, asctime, message", + style=",", + rename_fields=( + {"levelname": "LEVEL", "asctime": "TIMESTAMP", "message": "MESSAGE"} + ), +) + +logHandler.setFormatter(formatter) +logger.addHandler(logHandler) + +logger.setLevel(logging.DEBUG) + +# Prometheus metrics + +http_requests_total = Counter( + "http_requests_total", "Total HTTP requests", + ["method", "endpoint", "status_code"] +) + +http_request_duration_seconds = Histogram( + "http_request_duration_seconds", + "HTTP request duration", ["method", "endpoint"] +) + +http_requests_in_progress = Gauge( + "http_requests_in_progress", + "HTTP requests currently being processed" +) + +endpoint_calls = Counter("devops_info_endpoint_calls", + "Endpoint calls", ["endpoint"]) + +system_info_duration = Histogram( + "devops_info_system_collection_seconds", + "System info collection time" +) + +DATA_DIR = os.getenv("DATA_DIR", "/tmp/data") +VISITS_FILE = os.path.join(DATA_DIR, "visits") + +visits_lock = Lock() +visits_count = 0 + + +def load_visits(): + global visits_count + + os.makedirs(DATA_DIR, exist_ok=True) + + if not os.path.exists(VISITS_FILE): + visits_count = 0 + return + + try: + with open(VISITS_FILE, "r") as f: + visits_count = int(f.read().strip()) + except Exception: + visits_count = 0 + + +def increment_visits(): + global visits_count + + with visits_lock: + visits_count += 1 + with open(VISITS_FILE, "w") as f: + f.write(str(visits_count)) + + return visits_count + + +def get_visits(): + with visits_lock: + return visits_count + + +@app.before_request +def before_request(): + g.start_time = time.time() + http_requests_in_progress.inc() + + +@app.after_request +def after_request(response): + duration = time.time() - g.start_time + + endpoint = request.path + + if endpoint == "/metrics": + return response + + if endpoint.startswith("/user/"): + endpoint = "/user/{id}" + + http_requests_total.labels( + method=request.method, endpoint=endpoint, + status_code=response.status_code + ).inc() + + http_request_duration_seconds.labels( + method=request.method, endpoint=endpoint + ).observe(duration) + + http_requests_in_progress.dec() + + return response + + +# variable names +platform_name = platform.system() +architecture = platform.machine() +python_version = platform.python_version() +hostname = socket.gethostname() + +# env variables +ADDRESS = os.getenv("ADDRESS", "0.0.0.0") +PORT = int(os.getenv("PORT", 1999)) +DEBUG = os.getenv("DEBUG", "True").lower() == "true" + + +@app.route("/metrics") +def metrics(): + return Response(generate_latest(), mimetype="text/plain") + + +# decorator for / path +@app.route("/", methods=["GET"]) +def get_endpoint(): + start = time.time() + endpoint_calls.labels(endpoint="/").inc() + current_visits = increment_visits() + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify({ + "message": message, + "visits": current_visits + }) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +@app.route("/visits", methods=["GET"]) +def visits(): + start = time.time() + endpoint_calls.labels(endpoint="/visits").inc() + + count = get_visits() + + response = jsonify({"visits": count}) + response.status_code = 200 + + system_info_duration.observe(time.time() - start) + return response + + +@app.route("/ready") +def ready(): + start = time.time() + endpoint_calls.labels(endpoint="/ready").inc() + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify({ + "status": "ready", + "timestamp": datetime.now().isoformat() + }) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +# decorator for /health path +@app.route("/health") +def health(): + start = time.time() + endpoint_calls.labels(endpoint="/health").inc() + # extra: status code, client ip + logger.info( + { + "MESSAGE": f"Request: {request.method} {request.path}", + }, + extra={ + "CLIENT_IP": request.remote_addr, + "STATUS_CODE": 200, + "METHOD": request.method, + "PATH": request.path, + }, + ) + response = jsonify( + { + "status": "healthy", + "timestamp": datetime.now().isoformat(), + "uptime_seconds": get_uptime()["seconds"], + } + ) + response.status_code = 200 + system_info_duration.observe(time.time() - start) + return response + + +# error handling + + +@app.errorhandler(404) +def not_found(error): + logger.error( + {"MESSAGE": "Endpoint does not exist"}, + extra={ + "ERROR": "Not Found", + "STATUS_CODE": 404, + "CLIENT_IP": request.remote_addr, + "METHOD": request.method, + "PATH": request.path, + }, + ) + return ( + jsonify(({"error": "Not Found", + "MESSAGE": "Endpoint does not exist"})), + 404, + ) + + +@app.errorhandler(500) +def internal_error(error): + logger.error( + {"MESSAGE": "Internal Server Error"}, + extra={ + "ERROR": "Not Found", + "STATUS_CODE": 500, + "CLIENT_IP": request.remote_addr, + "METHOD": request.method, + "PATH": request.path, + }, + ) + return ( + jsonify( + { + "error": "Internal Server Error", + "MESSAGE": "An unexpected error occurred", + } + ), + 500, + ) + + +start_time = datetime.now() + + +def get_uptime(): + delta = datetime.now() - start_time + seconds = int(delta.total_seconds()) + hours = seconds // 3600 + minutes = (seconds % 3600) // 60 + return {"seconds": seconds, "human": f"{hours} hours, {minutes} minutes"} + + +# json message with system and environment info +message = { + "service": { + "name": "devops-info-service", + "version": "1.0.0", + "description": "DevOps course info service", + "framework": "Flask", + "debug status": DEBUG, + }, + "system": { + "hostname": hostname, + "platform": platform_name, + "platform_version": "Ubuntu 24.04", + "architecture": architecture, + "cpu_count": 8, + "python_version": python_version, + }, + "runtime": { + "uptime_seconds": get_uptime(), + "uptime_human": "1 hour, 0 minutes", + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + }, + "request": { + "client_ip": "127.0.0.1", + "port": PORT, + "user_agent": "curl/7.81.0", + "method": "GET", + "path": "/", + }, + "endpoints": [ + {"path": "/", "method": "GET", "description": "Service information"}, + {"path": "/health", "method": "GET", "description": "Health check"}, + ], +} + +logger.info( + { + "MESSAGE": f"Application starting on port {PORT}...", + } +) + +load_visits() +if __name__ == "__main__": + app.run(host=ADDRESS, port=PORT, debug=DEBUG) diff --git a/labs/lab18/app_python/data/visits b/labs/lab18/app_python/data/visits new file mode 100644 index 0000000000..86ee83a4a2 --- /dev/null +++ b/labs/lab18/app_python/data/visits @@ -0,0 +1 @@ +40 \ No newline at end of file diff --git a/labs/lab18/app_python/default.nix b/labs/lab18/app_python/default.nix new file mode 100644 index 0000000000..52a86ecc21 --- /dev/null +++ b/labs/lab18/app_python/default.nix @@ -0,0 +1,36 @@ +{ + pkgs ? import { + } +}: + +pkgs.python3Packages.buildPythonApplication rec { + pname = "devops-info-service"; + version = "1.0.0"; + + src = ./.; + + format = "other"; + + propagatedBuildInputs = with pkgs.python3Packages; [ + flask + pytest + python-json-logger + python-dotenv + prometheus-client + ]; + + nativeBuildInputs = [ + pkgs.makeWrapper + ]; + + installPhase = '' + mkdir -p $out/bin + + cp app.py $out/bin/devops-info-service + + chmod +x $out/bin/devops-info-service + + wrapProgram $out/bin/devops-info-service \ + --prefix PYTHONPATH : "$PYTHONPATH" + ''; +} diff --git a/labs/lab18/app_python/docker-compose.yml b/labs/lab18/app_python/docker-compose.yml new file mode 100644 index 0000000000..eb623d9169 --- /dev/null +++ b/labs/lab18/app_python/docker-compose.yml @@ -0,0 +1,13 @@ +version: "3.9" + +services: + app: + build: . + ports: + - "1999:12345" + environment: + - PORT=12345 + - ADDRESS=0.0.0.0 + - DATA_DIR=/app/data + volumes: + - ./data:/app/data \ No newline at end of file diff --git a/labs/lab18/app_python/docker.nix b/labs/lab18/app_python/docker.nix new file mode 100644 index 0000000000..bb35083eab --- /dev/null +++ b/labs/lab18/app_python/docker.nix @@ -0,0 +1,44 @@ +{ + pkgs ? import { + } +}: + +let + app = import ./default.nix { inherit pkgs; }; + +in pkgs.dockerTools.buildLayeredImage { + name = "devops-info-service-nix"; + tag = "1.0.0"; + + contents = [ + app + pkgs.bash + pkgs.coreutils + ]; + + config = { + Cmd = [ + "${pkgs.bash}/bin/bash" + "-c" + '' + echo contents of app; + ls -la ${app}/bin/devops-info-service"; + echo file contents; + cat ${app}/bin/devops-info-service; + echo executing; + ${app}/bin/devops-info-service + '' + ]; + + ExposedPorts = { + "12345/tcp" = { }; + }; + + Env = [ + "PORT=12345" + "HOST=0.0.0.0" + ]; + }; + + created = "1970-01-01T00:00:01Z"; +} diff --git a/labs/lab18/app_python/docs/LAB01.md b/labs/lab18/app_python/docs/LAB01.md new file mode 100644 index 0000000000..159dae34a3 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB01.md @@ -0,0 +1,134 @@ +# Documentation + +## Framework Selection + +I chose Flask because it is quite good for the small projects and easy enough for the beginners, as I didn't have an experience with API before. It also has a lot of community support since it was released long ago. + +| Framework | Features | +|---------|------------| +| Flask | Easy to learn, a lot of documentation | +| FastAPI | Harder to learn, more suitable for big projects (but from reddit comments people seem to like it a lot)| +| Django | Also too heavy for a small project | + +## Best Practices Applied + +- Informative comments + +```python +# decorator for / path +# decorator for /health path +# error handling +``` +- PEP8 standarts with auto linting from black formatter (example is app.py itself) + +- clear structure + +- error handling + +```python +@app.errorhandler(404) +def not_found(error): + return jsonify({"error": "Not Found", "message": "Endpoint does not exist"}), 404 + + +@app.errorhandler(500) +def internal_error(error): + return ( + jsonify( + { + "error": "Internal Server Error", + "message": "An unexpected error occurred", + } + ), + 500, + ) +``` + +- logging +```python +logging.basicConfig( + level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" +) + +logger.info("Application starting...") + +logger.debug(f"Request: {request.method} {request.path}") # inside decorators +``` +- requirements.txt and .gitignore + +## API Documentation + +- The web app can be accesed by this link: http://127.0.0.1:5000 + +### Request/response examples + +- Request Examples: + - GET / HTTP/1.1 + - GET /health HTTP/1.1 +- Response examples + - json and 200 OK code + - {"error":"Not Found","message":"Endpoint does not exist"} 404 not found error + - {"error":"Internal Server Error","message":"An unexpected error occurred"} 500 internal server error + +### Testing commands + +- test urls + - http://127.0.0.1:5000 + - http://127.0.0.1:5000/health + - http://127.0.0.1:5000/unknown +- curl + - curl http://127.0.0.1:5000/unknown + - curl http://127.0.0.1:5000/health + - curl http://127.0.0.1:5000/ + +## Testing Evidence + +### Screenshots + +Screenshots can be found in app/python/docs/screenshots. + +### Terminal output + +#### Curl + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/unknown +{"error":"Not Found","message":"Endpoint does not exist"} + +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/health +{"status":"healthy","timestamp":"2026-01-28T22:32:13.607128","uptime_seconds":352} + +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % curl http://127.0.0.1:5000/ +{"message":{"endpoints":[{"description":"Service information","method":"GET","path":"/"},{"description":"Health check","method":"GET","path":"/health"}],"request":{"client_ip":"127.0.0.1","method":"GET","path":"/","port":5000,"user_agent":"curl/7.81.0"},"runtime":{"current_time":"2026-01-07T14:30:00.000Z","timezone":"UTC","uptime_human":"1 hour, 0 minutes","uptime_seconds":{"human":"0 hours, 0 minutes","seconds":0}},"service":{"debug status":true,"description":"DevOps course info service","framework":"Flask","name":"devops-info-service","version":"1.0.0"},"system":{"architecture":"arm64","cpu_count":8,"hostname":"Veronicas-MacBook-Air.local","platform":"Darwin","platform_version":"Ubuntu 24.04","python_version":"3.12.9"}}} +``` +#### App started + +```bash +devops) fountainer@Veronicas-MacBook-Air app_python % python app.py +2026-01-28 22:36:05,289 - __main__ - INFO - Application starting... + * Serving Flask app 'app' + * Debug mode: off +2026-01-28 22:36:05,299 - werkzeug - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on http://127.0.0.1:5000 +2026-01-28 22:36:05,299 - werkzeug - INFO - Press CTRL+C to quit +2026-01-28 22:36:09,089 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:09] "GET /unknown HTTP/1.1" 404 - +2026-01-28 22:36:12,152 - __main__ - DEBUG - Request: GET / +2026-01-28 22:36:12,153 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:12] "GET / HTTP/1.1" 200 - +2026-01-28 22:36:18,600 - __main__ - DEBUG - Request: GET /health +2026-01-28 22:36:18,607 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:18] "GET /health HTTP/1.1" 200 - +2026-01-28 22:36:20,246 - __main__ - DEBUG - Request: GET /health +2026-01-28 22:36:20,247 - werkzeug - INFO - 127.0.0.1 - - [28/Jan/2026 22:36:20] "GET /health HTTP/1.1" 200 - +``` + +## Challenges & Solutions + +- It was quite hard for me to understand the structure since I didn't work with Flask and API in general before. I got some info from stackoverflow, documentations, etc. + +- I have encountered a problem with a jsonify library since I thought it wasn't comming from Flask library. Terminal errors helped to understand it eventually. + +- It took some time to understand how methods from request library work. + +## GitHub Community + +- starring repositories support open-source development and small but promising projects +- following developers lead to the opportunities of communication, exchanging experience, and making connections in the field. diff --git a/labs/lab18/app_python/docs/LAB02.md b/labs/lab18/app_python/docs/LAB02.md new file mode 100644 index 0000000000..9ae3741628 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB02.md @@ -0,0 +1,84 @@ +# Documentation + +## Docker Best Practices Applied + +- COPY commands for copying requirements before copying application code. This is a good practice because this way if we change the application code we won't have to rebuild all dependencies. + +```bash +COPY requirements.txt . +COPY app.py . +``` +- Multi-stage image. Firstly we build an image, then we run commands. It helps to reduce the final image size since all build tools won't be included. Example is the whole Dockerfile. + +- Small base image (python:3.13-slim), this way image builds faster since we don't need to install a giant package. + +- Creating and setting a non-root user. This is good for security since running as root is vulnerable. + +```bash +RUN useradd --create-home --shell /bin/bash newuser +USER newuser +``` +- .dockerignore file. We explicitly exclude files that should not be leaked to the public image (for security). + +- COPY only those files that are really needed (application code and dependencies). + +```bash +COPY requirements.txt . +COPY app.py . +``` +- EXPOSE port for documentation purposes. + +```bash +EXPOSE 12345 +``` + +## Image Information & Decisions + +- I chose python:3.13-slim because it is really small compared to the full version and was released not so long ago (I thought about using a distroless image, but the last available version was 3.12). + +- Final image is 158MB, it is very light (compressed size in dockerhub is even smaller: 47.32 MB) + +- Layer structure: I use multi-stage Dockerfile, so I have two parts: for building and for running. In the build stage I copy all dependencies and then application code, in the run-time stage I set an environment variable for the port, document it, add a new user and create a directory, then copy files with dependencies and application code, and then run commands. This way the time for building is utilised in an effective manner due to the small image, copy commands order, and multi-stage dockerfile. + +## Build & Run Process + +![Build](./screenshots/lab02-shots/build.png) + +![Running](./screenshots/lab02-shots/run.png) + +![Testing](./screenshots/lab02-shots/test.png) + +![Push](./screenshots/lab02-shots/push.png) + +![Pull](./screenshots/lab02-shots/pull.png) + +[Link to the image](https://hub.docker.com/repository/docker/fountainer/my-app/general) + +## Technical Analysis + +### Why does your Dockerfile work the way it does? + +Because Dockerfile provides a clear structure that can be easily followed and suited for your particular needs. It works the way it does because it replicates how the script is running on the host machine. + +### What would happen if you changed the layer order? + +If I change the COPY commands order after I make changes in application code all dependencies will be rebuilt too. If I remove the multi-stage the size of the image will increase. + +### What security considerations did you implement? + +Non-root user, .dockerignore, copying specific files. + +### How does .dockerignore improve your build? + +It excludes the possibility to copy files that contain private information (but we still can forget to add some files so it is better to only copy what we actually need). + +### Challenges & Solutions + +- I faced a problem with port mapping but docker documentation helped. Now I know how to set up the communication between host and container. While I was struggling I tried to debug what processes blocked the 5000 port (and it seemed to be docker itself). + + + + + + + diff --git a/labs/lab18/app_python/docs/LAB03.md b/labs/lab18/app_python/docs/LAB03.md new file mode 100644 index 0000000000..0585052aa9 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB03.md @@ -0,0 +1,97 @@ +# Documentation + +## Overview + +### Choose a Testing Framework + +Since the project is quite simple, it is not a big difference between frameworks regarding their more complex features. I chose pytest because it is easy to use as a beginner and it provides all needed functionality. + +### Test coverage & trigger configuration + +- I check the structure of a json response for ./ and ./health requests, and additionally review error cases (response.status code is 404 or 500). Also, I test the type of the value for importand fields. + +- The pipeline is triggered on push and pull requests. It is useful for me since I can check the pipeline during working on the lab (pushes) and then on pull request for the submission. + +### Versioning Strategy + +- latest tag + +- I chose to use Calendar Versioning since it is quite indicative for the university course (I can easily determine the image corresponding to the particular lab (by the date)). + +- Also I tag by commit SHA for needed image detection + +## Workflow Evidence + +### Tests terminal output + +![Passed tests](./screenshots/lab03-shots/unit%20test%20output.png) + +### Pipeline test with GitHub actions + +[Successful run](https://github.com/ffountainer/DevOps-Core-Course/actions/runs/21885090766) + +![](./screenshots/lab03-shots/pipeline%20success.png) + +### GitHub Image + +[Image](https://hub.docker.com/layers/fountainer/my-app/latest/images/sha256:ec4a12a2a6e91d464be4f1a908f23a3646ed05233d2ae82101357ea1e23bd677?uuid=8c4ce238-1b75-4c64-ba2e-b07167c9cb11%0A) + +## Best Practices Implemented + +- check on pull request to see the status before merging +- secrets for sensitive data +- docker image layer caching for quicker builds +- job dependencies (so docker build and push do not run if unit tests have not passed) +- add status badge to immediately see the current pipeline status +- dependency caching for quicker unit testing +- security scanning with Snyk for detecting vulnerabilities + +![Improvements](./screenshots/lab03-shots/improved%20perf.png) + +### Caching implementation and speed improvement metrics + +Caching is enabled by actions/setup-python@v5 and Docker layer caching, reducing dependency installation and image rebuild times. This decreased average workflow time from ~2-1 min to ~1-0.4 min (~40–60% faster on subsequent runs). + +![DockerHub images](./screenshots/lab03-shots/images%20with%20tags%20docker%20hub.png) + +### Snyk integration + +Initially there was a vulnerability with outdated flask version, so I upgraded it. + +## Key Decisions + +### Versioning Strategy + +- I chose to use Calendar Versioning since it is quite indicative for the university course (I can easily determine the image corresponding to the particular lab (by the date)). + +### Docker Tags + +- CI creates CalVer, latest, and SHA tags. + +### Workflow Triggers + +- The pipeline is triggered on push and pull requests. It is useful for me since I can check the pipeline during working on the lab (pushes) and then on pull request for the submission. + +### Test Coverage + +I check the structure of a json response for ./ and ./health requests, and additionally review error cases (response.status code is 404 or 500). Also, I test the type of the value for importand fields. + +What is not tested: + - if env variables provide correct values + +### Chosen actions + +- actions/checkout@v4: to check-out my repository under $GITHUB_WORKSPACE, so my workflow can access it + +- actions/setup-python@v5: to install python and add it to path and to allow caching + +- docker/login-action@v3: to login into docker + +- docker/setup-buildx-action@v3: to enable layer caching + +- docker/build-push-action@v5: to build and push image + +## Challenges + +- didn't work with unit testing and assert command before +- snyk didn't find my files so I stopped using snyk action and configured job manually \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB04.md b/labs/lab18/app_python/docs/LAB04.md new file mode 100644 index 0000000000..ab38156561 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB04.md @@ -0,0 +1,198 @@ +# Documentation + +## Cloud provider and infrastructure + +### Cloud provider chosen and rationale + +- I chose Yandex Cloud since it provides solid resource provision with a lot of RAM and storage space, also it has a free trial period. + +### Instance type/size and why + +- 2 cpu cores and 2 gb of ram because the project is really small and we don't need a lot of resources for it. + +### Region/zone selected + +- zone ru-central1-a + +### Resources created (list all) + +- service account + +- boot disk with ubuntu image + +- vm (2 cores, 2 memory) + +- network + +- subnet (zone ru-central1-a) + +- security group + +## Task 1 (Terraform implementation) + +### Terraform version used + +Terraform v1.14.5 on darwin_arm64 + +### Project structure explanation + +``` +app_python/ +├── pulumi/ +│ ├── venv/ # Python virtual environment +│ ├── Pulumi.yaml # Pulumi project metadata +│ ├── Pulumi.dev.yaml # Stack config (gitignored) +│ ├── requirements.txt # Python dependencies +│ ├── __main__.py # Pulumi infrastructure code +| ├── .gitignore # Ignore state, credentials +│ └── README.md # Pulumi setup instructions +└── terraform/ + ├── .gitignore # Ignore state, credentials + ├── main.tf # Main resources + ├── variables.tf # Input variables + ├── outputs.tf # Output values + ├── terraform.tfvars # Variable values (gitignored) + └── README.md # Terraform setup instructions +``` + +### Key configuration decisions + +- connect with pair of SSH keys +- configure security group rules +- expose only necessary ports +- do not expose credentials + +### Challenges encountered + +Getting accustomed to the Yandex Cloud wasn't easy, and setting up SSH connection also was a little challanging. + +### Public IP address of created VM + +```bash +external_ip_address_vm = "62.84.117.91" +``` + +### SSH connection command + +```bash +ssh -i /home-directory/.ssh/terraform-vm-key ubuntu@62.84.117.91 +``` + +### Terminal output from terraform plan and terraform apply + +![Plan](./screenshots/lab04-shots/terraform%20plan.png) + +![Apply-1](./screenshots/lab04-shots/terraform%20apply-1.png) + +![Apply-2](./screenshots/lab04-shots/terraform%20apply-2.png) + +### Proof of SSH access to VM + +![SSH](./screenshots/lab04-shots/ssh%20output%20terraform.png) + +## Task 2 (Pulumi Implementation) + +### Pulumi version and language used + +- pulumi==3.222.0, python + +### Terraform destroy output + +![](./screenshots/lab04-shots/terraform%20destroy.png) + +### How code differs from Terraform + +- Palumi supports many programming language for configuration, while terraform only lets to use its default config language. + +### Advantages you discovered + +- It is easier to write in a familiar language in Palumi, but to be honest, I enjoyed writing terraform config more. + +### Challenges encountered + +- Had problems with SSH as well, also it wasn't so easy to find documentation and guides. + +### Terminal output + +- pulumi preview + +![](./screenshots/lab04-shots/pulumi%20preview.png) + +- pulumi up +![](./screenshots/lab04-shots/pulumi%20up.png) + +- SSH connection to VM + +![](./screenshots/lab04-shots/pulumi%20ssh.png) + +### Public IP of Pulumi-created VM + +```bash +(venv) fountainer@Veronicas-MacBook-Air pulumi % pulumi stack output + +Enter your passphrase to unlock config/secrets + (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember): +Enter your passphrase to unlock config/secrets +Current stack outputs (2): + OUTPUT VALUE + external_ip 93.77.185.195 + internal_ip 192.168.10.5 +``` +### SSH connection + +(I reused the key from the terraform config) + +```bash +ssh -i ~/.ssh/terraform-vm-key ubuntu@93.77.185.195 +``` + +![](./screenshots/lab04-shots/pulumi%20ssh.png) + +## Terraform vs Pulumi Comparison + +### Ease of Learning: Which was easier to learn and why? + +- Documentation for Terraform was easier to find (for me), but Polumi was more familiar to work with. I also felt like Polumni's config is less complex. + +### Code Readability: Which is more readable for you? + +- Terraform + +### Debugging: Which was easier to debug when things went wrong? + +- Terraform, plan command was really useful + +### Documentation: Which has better docs and examples? + +- Terraform + +### Use Case: When would you use Terraform? When Pulumi? + +- Pulumi is good for projects where you want to employ features that only conplex programming languages can provide. Terraform is good for standardized infrastructure. + +### Code differences (HCL vs Python/TypeScript) + +- Terraform uses declarative HCL with blocks and attributes, while Pulumi uses imperative Python/TypeScript with function calls, objects, and full programming language features for resource creation and logic. + +### Which tool you prefer and why + +- I liked Terraform more (partially because on this step I was creating a vm in the cloud for the first time, but doing the same with Palumi was quite boring), I like the declarative languages, and they seem to be intuitively understandable. + +## Lab 5 Preparation & Cleanup + +### Are you keeping your VM for Lab 5? (Yes/No) + +- Yes + +### If yes: Which VM (Terraform or Pulumi created)? + +- Palumi, but I kind of contemplating on returning to Terraform... + +### VM Status + +![](./screenshots/lab04-shots/vm%20status.png) + + + + + diff --git a/labs/lab18/app_python/docs/LAB05.md b/labs/lab18/app_python/docs/LAB05.md new file mode 100644 index 0000000000..f18d4db760 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB05.md @@ -0,0 +1,306 @@ +# Documentation + +## Architecture Overview + +### Ansible version used + +core 2.20.2 + +### Target VM OS and version + +linux, ubuntu-2204-lts + +### Role structure diagram or explanation + +``` +roles/ +├─ web_app/ # deploying the application +│ ├─ defaults/main.yml # default variables: container_port, restart_policy, app_env, etc +│ ├─ handlers/main.yml # handlers, for example, restart container +│ └─ tasks/main.yml # deployment tasks +│ +├─ docker/ # docker setup/configuration +│ ├─ defaults/main.yml # defaults for docker config +│ ├─ handlers/main.yml # docker handlers +│ └─ tasks/main.yml # tasks to install and configure docker +│ +├─ common/ # general tasks +│ ├─ defaults/main.yml # default variables for common tasks +│ ├─ handlers/main.yml # handlers for common tasks +│ └─ tasks/main.yml # general-purpose tasks shared across roles +``` + +### Why roles instead of monolithic playbooks? + +- Roles can be used across different projects, thay are easy to configure and control, as well as to change if needed. + +## Roles Documentation + +### Common + +- Purpose: installs common packages and sets system timezone +- Variables: common_packages (default: vim, git, curl, python3-pip, htop), timezone_name (default: Europe/Moscow) +- Handlers: doesn't have +- Dependencies: doesn't have + +### Docker + +- Purpose: installs and configures docker engine, ensures service is running, adds user to docker group, and sets up python for ansible +- Variables: docker_packages (docker-ce, docker-ce-cli, containerd.io), docker_user (default: ubuntu), docker_gpg_url, docker_repo +- Handlers: restart docker – restarts docker service when notified +- Dependencies: doesn't have + +### web_app + +- Purpose: deploys the application container: logs in to Docker Hub, pulls the image, stops/removes old container, runs new container, waits for it to be ready, and verifies health +- Variables: Vaulted: dockerhub_username, dockerhub_password, app_name, docker_image_tag, app_port, app_container_name; Defaults: container_port, restart_policy, app_env +- Handlers: restart app container – restarts the application container when notified +- Dependencies: depends on Docker being installed and configured (accomplished with docker role) + +## Idempotency Demonstration + +### Terminal output from FIRST provision.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml + +PLAY [Provision web servers] ********************************************************************************************************************************************************* + +TASK [Gathering Facts] *************************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Update apt cache] ***************************************************************************************************************************************************** +changed: [terraform] + +TASK [common : Install common packages] ********************************************************************************************************************************************** +changed: [terraform] + +TASK [common : Ensure timezone is set] *********************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Install prerequisites] ************************************************************************************************************************************************ +ok: [terraform] + +TASK [docker : Add Docker GPG key] *************************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Add Docker APT repository] ******************************************************************************************************************************************** +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +TASK [docker : Install Docker packages] ********************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Ensure Docker service is running] ************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] ********************************************************************************************************************************************* +changed: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ******************************************************************************************************************** +changed: [terraform] + +RUNNING HANDLER [docker : restart docker] ******************************************************************************************************************************************** +changed: [terraform] + +PLAY RECAP *************************************************************************************************************************************************************************** +terraform : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Terminal output from SECOND provision.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml + +PLAY [Provision web servers] ********************************************************************************************************************************************************* + +TASK [Gathering Facts] *************************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Update apt cache] ***************************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Install common packages] ********************************************************************************************************************************************** +ok: [terraform] + +TASK [common : Ensure timezone is set] *********************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ************************************************************************************************************************************************ +ok: [terraform] + +TASK [docker : Add Docker GPG key] *************************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ******************************************************************************************************************************************** +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] ********************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Ensure Docker service is running] ************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] ********************************************************************************************************************************************* +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ******************************************************************************************************************** +ok: [terraform] + +PLAY RECAP *************************************************************************************************************************************************************************** +terraform : ok=11 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Analysis: What changed first time? What didn't change second time? + +- first run made actual changes to install and configure packages/services; second run found everything already in desired state, so no changes + +### What makes your roles idempotent? + +- all tasks use Ansible modules that check state before making changes, ensuring applying the same playbook multiple times does not alter already-correct configuration + +## Ansible Vault Usage + +### How you store credentials securely + +- secrets like dockerhub username/token etc are saved in encrypted files (group_vars/all.yml) using ansible-vault create + +### Vault password management strategy + +- use a dedicated vault password file (~/.vault_pass.txt) or just type password manually with --ask-vault-pass + +### Example of encrypted file (show it's encrypted!) + +![](./screenshots/lab05-shots/encrypted.png) + +### Why Ansible Vault is important + +- keeps sensitive data safe, prevents accidental exposure in repos or logs, allows secure automation + + +## Deployment Verification + +### Terminal output from deploy.yml run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] **************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Show Docker password] ************************************************************************************************* +ok: [terraform] => { + "container_port": "12345" +} + +TASK [web_app : Log in to Docker Hub] ************************************************************************************************* +ok: [terraform] + +TASK [web_app : Pull Docker image] **************************************************************************************************** +ok: [terraform] + +TASK [web_app : Stop existing container (if running)] ********************************************************************************* +changed: [terraform] + +TASK [web_app : Remove old container (if exists)] ************************************************************************************* +changed: [terraform] + +TASK [web_app : Run new container] **************************************************************************************************** +changed: [terraform] + +TASK [web_app : Wait for application to be ready] ************************************************************************************* +ok: [terraform] + +TASK [web_app : Verify health endpoint] *********************************************************************************************** +ok: [terraform] + +RUNNING HANDLER [web_app : restart app container] ************************************************************************************* +ok: [terraform] + +PLAY RECAP ******************************************************************************************************************************* +terraform : ok=10 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + +``` + +### Container status: docker ps output + +```bash +Last login: Thu Feb 26 23:56:36 2026 from 45.12.151.45 +ubuntu@fhmebroid75qocec3dc3:~$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +bf2d264719af fountainer/my-app:latest "python app.py" About a minute ago Up About a minute 0.0.0.0:8080->12345/tcp my-app +ubuntu@fhmebroid75qocec3dc3:~$ +``` + +### Health check verification: curl outputs + +```bash +ubuntu@fhmebroid75qocec3dc3:~$ curl http://127.0.0.1:8080/health +{ + "status": "healthy", + "timestamp": "2026-02-26T20:58:46.335570", + "uptime_seconds": 134 +} +``` + +### Handler execution (if any) + +```bash +RUNNING HANDLER [web_app : restart app container] ************************************************************************************* +ok: [terraform] +``` + +## Key Decisions + +### Why use roles instead of plain playbooks? + +- roles can be used across different projects, thay are easy to configure and control, as well as to change if needed + +### How do roles improve reusability? + +- roles define tasks, defaults, and handlers so they can be reused across multiple playbooks or projects + +### What makes a task idempotent? + +- changes are made only when a system is not already in a needed state + +### How do handlers improve efficiency? + +- handlers run only when notified, avoiding repeated or unnecessary actions like service restarts + +### Why is Ansible Vault necessary? + +- vault encrypts sensitive data (passwords, tokens) from ending up in playbooks, logs, or git + +## Challenges + +- it was really hard for me to configure the internet access from my pulumi VM on yandex cloud. turned out it was kinda impossible so I returned to my terraform VM + +- while trying to run ```bash ansible-playbook playbooks/deploy.yml --ask-vault-pass``` ansible didn't see my vaulted variables \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB06.md b/labs/lab18/app_python/docs/LAB06.md new file mode 100644 index 0000000000..6d86869cca --- /dev/null +++ b/labs/lab18/app_python/docs/LAB06.md @@ -0,0 +1,868 @@ +# Documentation + +## Overview (What you accomplished and technologies used) + +- My stack consisted of Ansible core 2.20.2, Docker Compose 2.39.2, Jinja 3.1.6, python 3.11.9. +- I've accomplished refactoring the roles for using with docker-compose, implemented a wipe logic, and set up a new CI/CD worflow. + +## Blocks & Tags (Block usage in each role, tag strategy, execution examples with screenshots) + +All tags are made for quick understanding of the block usage. + +### Common role + +- block with package installation tasks (tag: packages) +- block with user creation (tag: users) +- block with timezone set up (tag: timezone) + +### Docker role + +- block with docker installation and set up (tag: docker_install) +- block with docker configuration (tag: docker_config) + +### Web-app role + +- block for app deployment with docker compose and possible wipe logic (from wipe.yaml with tag web_app_wipe), (tags: app_deploy, compose) + +## Screenshots and terminal outputs + +### Test provision with only docker +![](./screenshots/lab06-shots/Test%20provision%20with%20only%20docker.png) + +### Skip common role +![](./screenshots/lab06-shots/Skip%20common%20role.png) + +### Install packages only across all roles +![](./screenshots/lab06-shots/Install%20packages%20only%20across%20all%20roles.png) + +### Check mode to see what would run +![](./screenshots/lab06-shots/Check%20mode%20to%20see%20what%20would%20run.png) + +### Run only docker installation tasks +![](./screenshots/lab06-shots/Run%20only%20docker%20installation%20tasks.png) + +### Error handling with rescue block triggered + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml --tags "docker_install" + +PLAY [Provision web servers] ******************************************************************************************************************************************** + +TASK [Gathering Facts] ************************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] *********************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ************************************************************************************************************************************** +[ERROR]: Task failed: Module failed: unknown url type: 'blabla' +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/tasks/main.yml:14:7 + +12 state: present +13 update_cache: yes +14 - name: Add Docker GPG key + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "unknown url type: 'blabla'", "status": -1, "url": "blabla"} + +TASK [docker : Wait before retry] *************************************************************************************************************************************** +Pausing for 10 seconds +(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort) +ok: [terraform] + +TASK [docker : Retry apt update] **************************************************************************************************************************************** +changed: [terraform] + +TASK [docker : Retry adding Docker GPG key] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ************************************************************************************************************************ +ok: [terraform] + +PLAY RECAP ************************************************************************************************************************************************************** +terraform : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0 +``` + +### List all available tags + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/provision.yml --list-tags + +playbook: playbooks/provision.yml + + play #1 (webservers): Provision web servers TAGS: [] + TASK TAGS: [common, docker, docker_config, docker_install, packages, timezone, users] +``` + +## Docker Compose Migration (Template structure, role dependencies, before/after comparison) + +### Comparison + +- docker-compose provides richer functionality to set up env variables, dependencies, and configs, compared to just the docker container + +- docker-compose.yaml.j2 + +```yml +version: '{{ docker_compose_version }}' + +services: + {{ app_name }}: + image: {{ web_app_docker_image }}:{{ web_app_docker_tag }} + + ports: + - "{{ app_port }}:{{ app_internal_port }}" + + restart: unless-stopped + + environment: +{% for key, value in web_app_env.items() %} + {{ key }}: "{{ value }}" +{% endfor %} + + networks: + - {{ app_name }}_network + +networks: + {{ app_name }}_network: + driver: bridge +``` + +### First run + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Create application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:33:18 + +31 - name: Log deployment attempt +32 copy: +33 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=12 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 +``` + +### Second run removed "changed" status of some fields (not with the current time) + +```bash +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] +``` + +### Verifying on target VM + +```bash +ubuntu@fhmebroid75qocec3dc3:~$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +7c279d0b3c18 fountainer/my-app:latest "python app.py" 10 minutes ago Up 10 minutes 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +0a0701a21e3a 9582a1fe4631 "python app.py" 25 hours ago Up 25 hours 0.0.0.0:8080->12345/tcp my-app +ubuntu@fhmebroid75qocec3dc3:~$ curl http://localhost:1999 +{ + "message": { + "endpoints": [ + { + "description": "Service information", + "method": "GET", + "path": "/" + }, + { + "description": "Health check", + "method": "GET", + "path": "/health" + } + ], + "request": { + "client_ip": "127.0.0.1", + "method": "GET", + "path": "/", + "port": 12345, + "user_agent": "curl/7.81.0" + }, + "runtime": { + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + "uptime_human": "1 hour, 0 minutes", + "uptime_seconds": { + "human": "0 hours, 0 minutes", + "seconds": 0 + } + }, + "service": { + "debug status": true, + "description": "DevOps course info service", + "framework": "Flask", + "name": "devops-info-service", + "version": "1.0.0" + }, + "system": { + "architecture": "x86_64", + "cpu_count": 8, + "hostname": "7c279d0b3c18", + "platform": "Linux", + "platform_version": "Ubuntu 24.04", + "python_version": "3.13.12" + } + } +} +ubuntu@fhmebroid75qocec3dc3:~$ +``` + +## Wipe Logic (Implementation details, variable + tag approach, test results) + +- Implementation details can be seen in ansible/roles/web-app/tasks/wipe.yml. + +- The tag is web_app_wipe, the task is triggered by "when: web_app_wipe | bool" + +- Variables: "{{ web_app_compose_project_dir }}", "{{ ansible_check_mode }}", {{ app_name }} + +### Scenario 1: Normal deployment (wipe should NOT run) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Create application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +skipping: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:38:18 + +36 - name: Log deployment attempt +37 copy: +38 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=13 changed=1 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +7c279d0b3c18 fountainer/my-app:latest "python app.py" 21 minutes ago Up 21 minutes 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +0a0701a21e3a 9582a1fe4631 "python app.py" 25 hours ago Up 25 hours 0.0.0.0:8080->12345/tcp my-app +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Scenario 2: Wipe only (remove existing deployment) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[ERROR]: Task failed: Module failed: "/opt/my-app" is not a directory +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml:5:7 + +3 block: +4 +5 - name: Stop and remove containers + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "\"/opt/my-app\" is not a directory"} +...ignoring + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "ls /opt" +containerd +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +### Scenario 3: Clean reinstallation (wipe → deploy) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install prerequisites] ***************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ******************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add Docker APT repository] ************************************************************************************************************************* +[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:7:14 + +5 docker_user: "ubuntu" +6 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +7 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] *************************************************************************************************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************************************************************************************************* +ok: [terraform] + +TASK [docker : ensure docker service is enabled] ****************************************************************************************************************** +ok: [terraform] + +TASK [docker : Add user to Docker group] ************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[ERROR]: Task failed: Module failed: "/opt/my-app" is not a directory +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml:5:7 + +3 block: +4 +5 - name: Stop and remove containers + ^ column 7 + +fatal: [terraform]: FAILED! => {"changed": false, "msg": "\"/opt/my-app\" is not a directory"} +...ignoring + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +ok: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +TASK [web_app : Create application directory] ********************************************************************************************************************* +changed: [terraform] + +TASK [web_app : Template docker-compose.yml] ********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Deploy with Docker Compose] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Log deployment attempt] *************************************************************************************************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:37:18 + +35 - name: Log deployment attempt +36 copy: +37 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=17 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ssh ubuntu@93.77.181.173 "docker ps" +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +167ea730bdc1 fountainer/my-app:latest "python app.py" 20 seconds ago Up 19 seconds 0.0.0.0:1999->12345/tcp, [::]:1999->12345/tcp my-app-my-app-1 +``` +### Scenario 4: Safety checks (should NOT wipe) + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +skipping: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +skipping: [terraform] + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=2 changed=0 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + +(devops) fountainer@Veronicas-MacBook-Air ansible % +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air ansible % ansible-playbook playbooks/deploy.yml -e "web_app_wipe=true" --tags web_app_wipe --extra-vars @./group_vars/all.yml + +PLAY [Deploy application] ***************************************************************************************************************************************** + +TASK [Gathering Facts] ******************************************************************************************************************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************************************************************************************************* +included: /Users/fountainer/uni/devops/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] *********************************************************************************************************************** +[WARNING]: Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +changed: [terraform] + +TASK [web_app : Remove docker-compose file] *********************************************************************************************************************** +changed: [terraform] + +TASK [web_app : Remove application directory] ********************************************************************************************************************* +changed: [terraform] + +TASK [web_app : Log wipe completion] ****************************************************************************************************************************** +ok: [terraform] => { + "msg": "Application my-app wiped successfully" +} + +PLAY RECAP ******************************************************************************************************************************************************** +terraform : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 +``` + +### App running after complete reinstall + +![](./screenshots/lab06-shots/app%20running%20after%20clean%20reinstall.png) + + +## CI/CD Integration (Workflow architecture, setup steps, evidence of automated deployments) + +### Workflow + +- this workflow runs ansible-lint on pushes and pr’s first to catch errors, then deploys the app automatically on master/lab06 branches + +### Setup steps + +- setup steps include checking out code, installing python and ansible, configuring ssh with github secrets, decoding vault vars, and running the playbook remotely + +### Evidence + +- evidence of automated deployment is in the deploy job: it ssh’s to the vm, runs ansible-playbook with vault and extra vars, then verifies the app with curl requests + +![](./screenshots/lab06-shots/ci:cd%20success.png) + +Terminal output: + +```bash +ssh -i ~/.ssh/terraform-vm-key ubuntu@*** "echo connected" + + echo '***' > /tmp/vault_pass + cd app_python/ansible + + ansible-playbook playbooks/deploy.yml \ + -i inventory/hosts.ini \ + --vault-password-file /tmp/vault_pass \ + --extra-vars @./group_vars/all.yml + + rm /tmp/vault_pass + shell: /usr/bin/bash -e {0} + env: + pythonLocation: /opt/hostedtoolcache/Python/3.12.12/x64 + PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib/pkgconfig + Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib +connected + +PLAY [Deploy application] ****************************************************** + +TASK [Gathering Facts] ********************************************************* +ok: [terraform] + +TASK [docker : Install prerequisites] ****************************************** +ok: [terraform] + +TASK [docker : Add Docker GPG key] ********************************************* +ok: [terraform] +Warning: : Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg. + +TASK [docker : Add Docker APT repository] ************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/docker/defaults/main.yml:8:14 + +6 docker_user: "ubuntu" +7 docker_gpg_url: "https://download.docker.com/linux/ubuntu/gpg" +8 docker_repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" + ^ column 14 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +ok: [terraform] + +TASK [docker : Install Docker packages] **************************************** +ok: [terraform] + +TASK [docker : Install python3-docker for Ansible Docker modules] ************** +ok: [terraform] + +TASK [docker : Ensure docker service is enabled] ******************************* +ok: [terraform] + +TASK [docker : Add user to Docker group] *************************************** +ok: [terraform] + +TASK [web_app : Include wipe tasks] ******************************************** +included: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/wipe.yml for terraform + +TASK [web_app : Stop and remove containers] ************************************ +skipping: [terraform] + +TASK [web_app : Remove docker-compose file] ************************************ +skipping: [terraform] + +TASK [web_app : Remove application directory] ********************************** +skipping: [terraform] + +TASK [web_app : Log wipe completion] ******************************************* +skipping: [terraform] + +TASK [web_app : Create application directory] ********************************** +ok: [terraform] + +TASK [web_app : Template docker-compose.yml] *********************************** +ok: [terraform] + +TASK [web_app : Deploy with Docker Compose] ************************************ +Warning: : Docker compose: unknown None: /opt/my-app/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion +ok: [terraform] + +TASK [web_app : Log deployment attempt] **************************************** +[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24. +Origin: /home/runner/work/DevOps-Core-Course/DevOps-Core-Course/app_python/ansible/roles/web_app/tasks/main.yml:41:18 + +39 - name: Log deployment attempt +40 ansible.builtin.copy: +41 content: "Docker Compose deployment attempted on {{ ansible_date_time.iso8601 }}" + ^ column 18 + +Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead. + +changed: [terraform] + +PLAY RECAP ********************************************************************* +terraform : ok=13 changed=1 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0 + + sleep 10 + curl -f http://***:1999 || exit 1 + curl -f http://***:1999/health || exit 1 + shell: /usr/bin/bash -e {0} + env: + pythonLocation: /opt/hostedtoolcache/Python/3.12.12/x64 + PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib/pkgconfig + Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.12/x64 + LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.12.12/x64/lib + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed + + 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 + 0 1058 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 +100 1058 100 1058 0 0 4059 0 --:--:-- --:--:-- --:--:-- 4053 +{ + "message": { + "endpoints": [ + { + "description": "Service information", + "method": "GET", + "path": "/" + }, + { + "description": "Health check", + "method": "GET", + "path": "/health" + } + ], + "request": { + "client_ip": "127.0.0.1", + "method": "GET", + "path": "/", + "port": 12345, + "user_agent": "curl/7.81.0" + }, + "runtime": { + "current_time": "2026-01-07T14:30:00.000Z", + "timezone": "UTC", + "uptime_human": "1 hour, 0 minutes", + "uptime_seconds": { + "human": "0 hours, 0 minutes", + "seconds": 0 + } + }, + "service": { + "debug status": true, + "description": "DevOps course info service", + "framework": "Flask", + "name": "devops-info-service", + "version": "1.0.0" + }, + "system": { + "architecture": "x86_64", + "cpu_count": 8, + "hostname": "8081285df89f", + "platform": "Linux", + "platform_version": "Ubuntu 24.04", + "python_version": "3.13.12" + } + } +} + % Total % Received % Xferd Average Speed Time Time Time Current + Dload Upload Total Spent Left Speed + + 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 +100 99 100 99 0 0 364 0 --:--:-- --:--:-- --:--:-- 365 +{ + "status": "healthy", + "timestamp": "2026-03-07T16:04:23.506918", + "uptime_seconds": 146470 +} +``` + +## Testing Results (All test scenarios, idempotency verification, application accessibility) + +- all testing results are provided in the corresponding sections above + +## Challenges & Solutions (Difficulties encountered and how you solved them) + +- I had a hard time making ssh connection work in the github actions and wasted 2 days after the deadline for this... it was eventually solved by generating new ssh pair and coniguring vm's authorised keys again. + +## Research Answers (All research questions answered with analysis) + +### 1.3 + +Q1: What happens if rescue block also fails? + +- if the rescue block fails, ansible just shows an error and moves on to the always block. it won’t stop the playbook completely + +Q2: Can you have nested blocks? + +- yes, you can nest blocks inside other blocks, each with their own rescue/always if needed + +Q3: How do tags inherit to tasks within blocks? + +- tags on a block automatically apply to all tasks inside, but you can override or add extra tags per task + +### 2.3 + +Q1: What's the difference between restart: always and restart: unless-stopped? + +- restart: always makes the container restart no matter what, even if you stop it manually. unless-stopped restarts it only if it crashes or docker restarts, but not if you stopped it yourself + +Q2: How do Docker Compose networks differ from Docker bridge networks? + +- docker compose networks are defined per project and let containers talk using service names. bridge networks are default docker networks, simpler and not tied to a compose project + +Q3: Can you reference Ansible Vault variables in the template? + +- yes, you can use ansible vault variables in templates by referencing them like any other ansible variable ({{ vault_var_name }}) + +### 2.5 + +Q1: Look up community.docker.docker_compose_v2 module + +- community.docker.docker_compose_v2 manages compose projects using docker compose v2 cli under the hood, can pull images, recreate services, and set project source + +Q2: Compare state: present vs other state options + +- state: present makes sure services are running, absent removes them, stopped just stops without removing, restarted forces a restart + +Q3: Understand recreate parameter options + +- recreate: auto only recreates changed containers, never won’t recreate, force always recreates even if unchanged + +### 3.6 + +Q1: Why use both variable AND tag? (Double safety mechanism) + +- using both variable and tag is double safety: the variable controls behavior in code, tag controls execution from command line + +Q2: What's the difference between never tag and this approach? + +- never tag completely ignores tasks unless explicitly forced, this approach lets you selectively run blocks with normal tags + +Q3: Why must wipe logic come BEFORE deployment in main.yml? (Clean reinstall scenario) + +- wipe logic must run first to remove old containers/configs so deployment starts clean, avoids conflicts or leftover data + +Q4: When would you want clean reinstallation vs. rolling update? + +- clean reinstall is good if configs or images changed, rolling update is better for small changes with minimal downtime + +Q5: How would you extend this to wipe Docker images and volumes too? + +- extend wipe by adding tasks that remove docker images (docker_image module) and volumes (docker_volume module) before deployment + +### 4.10 + +Q1: What are the security implications of storing SSH keys in GitHub Secrets? + +- storing ssh keys in github secrets is safe if encrypted, but exposure risk exists if repo or workflows are misconfigured + +Q2: How would you implement a staging → production deployment pipeline? + +- implement staging → production by having two environments, separate compose dirs, and deploy to staging first, then promote to production after tests pass + +Q3: What would you add to make rollbacks possible? + +- for rollbacks, keep previous compose files and image tags, and add tasks to revert to last known good version if deployment fails + +Q4: How does self-hosted runner improve security compared to GitHub-hosted? + +- self-hosted runners limit exposure because the runner machine is under your control, unlike github-hosted where VM is shared and short-lived \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB07.md b/labs/lab18/app_python/docs/LAB07.md new file mode 100644 index 0000000000..f6df6dcf12 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB07.md @@ -0,0 +1,260 @@ +# Documentation + +## Architecture (diagram showing how components connect, and the data flow) + +```mermaid +flowchart LR + +subgraph Docker_Network["Docker Compose Network"] + + A[Promtail
port 9080
scrapes logs] + + B[Loki
port 3100
stores logs] + + C[Grafana
port 3000
visualizes logs] + +end + +D[Docker Engine
/var/lib/docker/containers
/var/run/docker.sock] + +D -->|discover containers
read logs| A + +A -->|push logs
http://loki:3100/loki/api/v1/push| B + +C -->|query logs
http://loki:3100| B +``` + +## Setup Guide (step-by-step deployment instructions) + +```bash +# make sure your docker compose is working +docker compose version +``` +```bash +# enter the monitoring directory +cd app_python/monitoring +``` +```bash +# deploy the containers +docker compose up -d +``` +Important! Make sure you have your .env file with secrets GF_PASSWORD and GF_EMAIL for Grafana authorization. + +## Configuration (explain your Loki/Promtail configs and why) + +### Configuration file snippets for Loki + +```bash +auth_enabled: false +# no authentication required +``` + +```bash +server: + http_listen_port: 3100 +# loki is listening on port 3100 +``` + +```bash +# common values are shared among all modules if not redefined explicitly +common: + path_prefix: /loki + storage: + # filesystem object store + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + # number of instances + replication_factor: 1 + # hashes are stored in ram + ring: + kvstore: + store: inmemory +``` + +```bash +# configures the schema for chunk index +schema_config: + configs: + # index buckets are created from this date + - from: 2026-03-01 + # index type + store: tsdb + object_store: filesystem + schema: v13 + index: + # prefix for all created indices + prefix: index_ + # the index is remade every 24 hours + period: 24h +``` + +```bash +# storage config for chunks +storage_config: + filesystem: + directory: /loki/chunks +``` +```bash +# logs are stored for 168 hours and then discarded +limits_config: + retention_period: 168h +``` + +```bash +# compactor merges small index shards for performance and deletes old logs +compactor: + working_directory: /loki/compactor + retention_enabled: true +``` + +### Configuration file snippets for Promtail + +```bash +server: + # listening port for promtail itself + http_listen_port: 9080 +``` +```bash +# positions help promtail to identify where it left of while reading the file +positions: + filename: "/tmp/positions.yaml" + sync_period: 10s + ignore_invalid_yaml: false +``` +```bash +# where promtail will push logs to +clients: + - url: http://loki:3100/loki/api/v1/push +``` +```bash +# discovery configs +scrape_configs: + - job_name: docker + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + # relabeling + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + # regex helps to remove / from container names + regex: '/(.*)' + target_label: 'container' +``` + +## Application Logging (how you implemented JSON logging) + +I implemented logging using JsonFormatter from pythonjsonlogger.json. It has basic configured fields and I can add extra fields relating to the request from dedicated functions that handle connections. + +### Screenshot of JSON log output from your app + +![](./screenshots/lab07-shots/logs%20from%20the%20app.png) + +## Dashboard (explain each panel and the LogQL queries) + +### Screenshot showing logs from at least 3 containers in Grafana Explore + +![](./screenshots/lab07-shots/grafana_logs_3_containers.png) + +### Screenshots of Grafana showing logs from the app + +![](./screenshots/lab07-shots/grafana-app-name.png) + +![](./screenshots/lab07-shots/grafana-error.png) + +![](./screenshots/lab07-shots/grafana_get.png) + +### Screenshot of your dashboard showing all 4 panels with real data. + +I have 4 panels: +- All collected logs ("Logs from all apps") +- The rate of getting logs ("Logs rate") +- A pie chart that shows the relative size of logs with different errors ("Level statistics") +- All collected error logs ("Error logs") + +![](./screenshots/lab07-shots/grafana%20dashboard.png) + +### Example LogQL queries with explanations (At least 3 different LogQL queries that work) + +- all logs from the app: +- - {service_name="app-python"} +- access all logs from the app which level is error: +- - {service_name="app-python"} |= "ERROR" +- all logs where request method was GET: +- - {service_name="app-python"} | json | METHOD = `GET` +- relative size of logs by levels: +- - sum by (level) (count_over_time({service_name=~"app-.*"} | json [60m])) +- rate of logs: +- - sum by (service_name) (rate({service_name=~"app-.*"} [120m])) +- error logs: +- - {service_name=~"app-.*"} | json | LEVEL="ERROR" + +## Production Config (security measures, resources, retention) + +### Configuration file snippets + +```bash +# healthchecks for loki and grafana to verify that containers are running and everything is okay +healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s +``` + +```bash +# resources limits: promtail, grafana, and app_python are pretty lightweight, but loki needs a lot of memory to store logs +deploy: + resources: + limits: + cpus: "1.50" + memory: 2G + reservations: + cpus: "0.50" + memory: 512M +``` + +```bash +# now grafana requires login authorization, it is configured with .env file with environment variables +environment: + - GF_AUTH_ANONYMOUS_ENABLED=false + - GF_SECURITY_ADMIN_PASSWORD=${GF_PASSWORD} + - GF_SECURITY_ADMIN_EMAIL=${GF_EMAIL} +``` + +### docker-compose ps showing all services healthy + +![](./screenshots/lab07-shots/healthy%20services.png) + +### Screenshot of Grafana login page (no anonymous access) + +![](./screenshots/lab07-shots/grafana_login.png) + +## Testing (commands to verify everything works) + +```bash +# check that the containers are up and healthy +docker compose ps +``` + +```bash +# check loki +curl http://localhost:3100/ready +``` + +```bash +# check promtail +curl http://localhost:9080/targets +``` + +```bash +# check grafana +open http://localhost:3000 +``` + +## Challenges (problems you encountered and solutions) + +- It was pretty hard for me to set up logging but after a long research it was done (I used python documentation and some guides to work out how JsonFormatter works with/without logging.BasicConfig()) + +- Also I was very confused about how to make promtail keep logs from a service with a specific label (I researched Stack Overflow and used AI a bit for this one) \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB08.md b/labs/lab18/app_python/docs/LAB08.md new file mode 100644 index 0000000000..c9afdd3a36 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB08.md @@ -0,0 +1,144 @@ +# Documentation + +## Architecture - Diagram showing metric flow (app → Prometheus → Grafana) + +```mermaid +flowchart LR + +subgraph Docker_Network["Docker Compose Network"] + + A[app-python
exposes /metrics] + + B[Prometheus
scrapes metrics
stores time-series data] + + C[Grafana
port 3000
visualizes metrics] + +end + +A -->|exposes metrics endpoint
http://app-python:12345/metrics| B + +B -->|PromQL queries
data source| C +``` + + +### Comparison: metrics vs logs (Lab 7) - when to use each + +- we use logs to see what has happened and metric to see the quantities: how much and how often. + +## Application Instrumentation - What metrics you added and why + +### Screenshot of /metrics endpoint output + +![](./screenshots/lab08-shots/metrics%20for%20app%20logs.png) + +### Code showing metric definitions + +- you can see it in app.py +- sections: imports updated, Counter, Histogram, Gauge metrics defined, before request/after request decorators, metrics route decorator + +### Documentation explaining your metric choices + +- http_requests_total counts how many requests hit the service, so i can see overall traffic and usage patterns +- http_request_duration_seconds measures how long requests take, which helps spot slow endpoints or performance issues +- http_requests_in_progress tracks how many requests are being processed right now, useful for detecting load spikes +- endpoint_calls tracks how often each endpoint is used, so i can understand which parts of the service are actually used +- system_info_duration measures how long it takes to collect system info, mainly to check if that logic becomes slow over time + +## Prometheus Configuration + +### Screenshot of /targets page showing all targets UP + +![](./screenshots/lab08-shots/prometheous%20targets.png) + +### Screenshot of a successful PromQL query + +![](./screenshots/lab08-shots/successful%20query.png) + +### prometheus.yml configuration file + +- you can find it in ./app_python_monitoring/prometheous/prometheous.yml + +## Dashboard Walkthrough + +### Each panel's purpose and query + +- request rate — shows how many requests/sec the service gets per endpoint +[query: sum(rate(http_requests_total[5m])) by (endpoint)] + +- error rate — shows how many failed requests (4xx/5xx) happen per second +[query: sum(rate(http_requests_total{status_code=~"[45].."}[5m]))] + +- p95 latency — shows how slow requests are (95th percentile) +[query: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))] + +- latency heatmap — shows distribution of request durations +[query: rate(http_request_duration_seconds_bucket[5m])] + +- active requests — shows how many requests are currently being processed +[query: http_requests_in_progress] + +- status codes — shows distribution of responses (2xx, 4xx, 5xx) +[query: sum by (status_code) (rate(http_requests_total[5m]))] + +- service health — shows if the service is up (1) or down (0) +[query: up{job="app"}] + +### Screenshots of dashboards with live data (all 6+ panels working) + +![](./screenshots/lab08-shots/dashboard1.png) +![](./screenshots/lab08-shots/dashboard2.png) + +### Exported dashboard JSON file + +- you can find the file in ./app_python/monitoring/prometheus/dashboard.json + +## PromQL Examples - 5+ queries with explanations + +### PromQL queries that demonstrate RED method + +### 5+ queries with explanations (red method) + +- `sum(rate(http_requests_total[5m]))` + **rate**: total requests per second, shows overall traffic load + +- `sum(rate(http_requests_total{status_code=~"[45].."}[5m]))` + **errors**: failed requests per second (4xx + 5xx), shows reliability issues + +- `histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))` + **duration**: p95 latency, shows how slow requests are + +- `sum by (endpoint) (rate(http_requests_total[5m]))` + **rate**: request rate per endpoint, helps see which endpoints are used most + +- `sum by (status_code) (rate(http_requests_total[5m]))` + **errors**: distribution of response codes, helps identify types of failures + +- `http_requests_in_progress` + **rate/load (approx)**: current number of active requests, useful to observe system load + +- `up{job="app"}` + **availability (related to errors)**: shows if service is reachable (1 = up, 0 = down) + +## Production Setup - Health checks, resources, retention policies + +### docker compose ps showing all services healthy + +![](./screenshots/lab08-shots/health%20statuses.png) + +### Documentation of retention policies + +- prometheus is configured to keep metrics for 15 days and up to 10gb of data; this is defined using the retention time and size settings in the docker compose file. this helps to control disk usage and keeps the dataset smaller, which improves query performance. older data is automatically removed once the limits are reached, so the system doesn’t run out of storage. + +### Proof of data persistence after restart + +- if I set 6 hours range I still see my previous data + +![](./screenshots/lab08-shots/persistency.png) + +## Testing Results - Screenshots showing everything working + +- you can see all required screenshots in the above sections + +## Challenges & Solutions - Issues encountered and fixes + +- my app-python container showed unhealty status because in the dockerfile no curl installation happened, I didn't think of it since in the VM it worked fine and I missed that those have different settings, once I added it to the Dockerfile and updated the image, it worked \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB09.md b/labs/lab18/app_python/docs/LAB09.md new file mode 100644 index 0000000000..20ea3e883d --- /dev/null +++ b/labs/lab18/app_python/docs/LAB09.md @@ -0,0 +1,301 @@ +# Documentation + +## Architecture Overview + +### Diagram or description of your deployment architecture + +```mermaid +flowchart LR + +subgraph Kubernetes_Cluster["Minikube Kubernetes Cluster"] + + S[Service node port
port 80 -> nodePort 30007] + + subgraph Deployment["Deployment: my-app (5 replicas)"] + P1[Pod 1
app-python] + P2[Pod 2
app-python] + P3[Pod 3
app-python] + P4[Pod 4
app-python] + P5[Pod 5
app-python] + end + +end + +User[User / curl / browser] + +User -->|HTTP request
http://nodeIP:30007| S + +S -->|routes traffic via selector
app: my-app| P1 +S --> P2 +S --> P3 +S --> P4 +S --> P5 + +P1 -->|/health /ready /metrics| AppLogic[(Flask App)] +P2 --> AppLogic +P3 --> AppLogic +P4 --> AppLogic +P5 --> AppLogic + +``` + +### How many Pods, which Services, networking flow + +- I used 5 pods managed by a deployment and one NodePort service, where traffic goes from the service (port 80 / nodePort 30007) to the pods using the app: my-app label selector. + +### Resource allocation strategy + +- I defined small cpu and memory requests/limits (100m–500m cpu, 128Mi–256Mi memory) to keep the app stable and prevent it from using too many cluster resources. + +### Brief explanation of your chosen tool (minikube/kind) and why + +I used minikube because it’s easy to set up locally and lets me run a full kubernetes cluster on my machine, which is enough for testing deployments without needing a real cloud setup. + +## Manifest Files + +### Brief description of each manifest + +- Deployment: deployment.yml defines how my app runs in Kubernetes, including how many pods, which image to use, and how they are configured. + +- Service: service.yml exposes the app inside and outside the cluster by routing traffic to the pods created by the deployment. + +### Key configuration choices + +- Deployment: I set 3 replicas, added resource limits/requests, configured liveness and readiness probes, used labels for selection, and set a rolling update strategy + +- Service: I used NodePort type, matched the selector with app: my-app, set port 80 as the service port, and mapped it to container port 12345 with a fixed nodePort. + +### Why you chose specific values (replicas, resources, etc.) + +- Deployment: I used 3 replicas for basic availability, small cpu/memory values since the app is lightweight, and probes to make sure kubernetes can detect when the app is healthy and ready to serve traffic. + +- Service: I used NodePort so i can access the app locally with minikube, port 80 for convenience, targetPort 12345 to match the app, and a fixed nodePort (30007) to make testing easier. + +## Deployment Evidence + +### Successful cluster setup + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % minikube start + +😄 minikube v1.38.1 on Darwin 26.3 (arm64) +✨ Using the docker driver based on existing profile +👍 Starting "minikube" primary control-plane node in "minikube" cluster +🚜 Pulling base image v0.0.50 ... +🏃 Updating the running docker "minikube" container ... +🐳 Preparing Kubernetes v1.35.1 on Docker 29.2.1 ... +🔎 Verifying Kubernetes components... + ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5 +🌟 Enabled addons: default-storageclass, storage-provisioner + +❗ /Applications/Docker.app/Contents/Resources/bin/kubectl is version 1.32.2, which may have incompatibilities with Kubernetes 1.35.1. + ▪ Want kubectl v1.35.1? Try 'minikube kubectl -- get pods -A' +🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default +``` +### Output of kubectl cluster-info and kubectl get nodes + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl cluster-info +Kubernetes control plane is running at https://127.0.0.1:51390 +CoreDNS is running at https://127.0.0.1:51390/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy + +To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get nodes +NAME STATUS ROLES AGE VERSION +minikube Ready control-plane 6h45m v1.35.1 +``` + +### kubectl get all output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m11s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h58m + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-app-deployment 3/3 3 3 3m11s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-app-deployment-6f67848dfb 3 3 3 3m11s +``` +### kubectl get pods,svc with detailed view + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods,svc +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m35s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h59m +``` + +### kubectl describe deployment showing replicas and strategy + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 29s +my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 29s +my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 29s +``` +### Screenshot or curl output showing app working + +![](./../docs/screenshots/lab09-shots/curl%20app%20.png) + +### Service deployment + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 38m +my-app-service NodePort 10.98.179.244 80:30007/TCP 41s +(devops) fountainer@Veronicas-MacBook-Air k8s % minikube service my-app-service +┌───────────┬────────────────┬─────────────┬───────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼───────────────────────────┤ +│ default │ my-app-service │ 80 │ http://192.168.49.2:30007 │ +└───────────┴────────────────┴─────────────┴───────────────────────────┘ +🔗 Starting tunnel for service my-app-service. +┌───────────┬────────────────┬─────────────┬────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼────────────────────────┤ +│ default │ my-app-service │ │ http://127.0.0.1:57348 │ +└───────────┴────────────────┴─────────────┴────────────────────────┘ +🎉 Opening service default/my-app-service in default browser... +❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it. +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 42m +my-app-service NodePort 10.98.179.244 80:30007/TCP 4m16s +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl describe service my-app-service +Name: my-app-service +Namespace: default +Labels: +Annotations: +Selector: app=my-app +Type: NodePort +IP Family Policy: SingleStack +IP Families: IPv4 +IP: 10.98.179.244 +IPs: 10.98.179.244 +Port: 80/TCP +TargetPort: 12345/TCP +NodePort: 30007/TCP +Endpoints: 10.244.0.16:12345,10.244.0.14:12345,10.244.0.15:12345 +Session Affinity: None +External Traffic Policy: Cluster +Internal Traffic Policy: Cluster +Events: +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get endpoints +Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice +NAME ENDPOINTS AGE +kubernetes 192.168.49.2:8443 42m +my-app-service 10.244.0.14:12345,10.244.0.15:12345,10.244.0.16:12345 4m36s +(devops) fountainer@Veronicas-MacBook-Air k8s % +``` + +## Operations Performed + +### Commands used to deploy + +- ```bash kubectl apply -f k8s/deployment.yml``` +- ```bash kubectl apply -f k8s/service.yml``` +- ```bash kubectl get pods``` +- ```bash kubectl get services ``` + +### Scaling demonstration output + +![](./../docs/screenshots/lab09-shots/scaling.png) + +### Rolling update demonstration output + +I changed ```bash image: fountainer/my-app:latest``` to ```bash image: fountainer/my-app:2026.03.26```. + +![](./../docs/screenshots/lab09-shots/rollout.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout history deployment/my-app-deployment +deployment.apps/my-app-deployment +REVISION CHANGE-CAUSE +1 +2 +3 + +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout undo deployment/my-app-deployment +deployment.apps/my-app-deployment rolled back +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout status deployment/my-app-deployment +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +deployment "my-app-deployment" successfully rolled out +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-7b5479788b-bj4pt 1/1 Running 0 37s +my-app-deployment-7b5479788b-n2pnb 1/1 Running 0 12s +my-app-deployment-7b5479788b-nvzdr 1/1 Running 0 22s +my-app-deployment-7b5479788b-q9g66 1/1 Running 0 36s +my-app-deployment-7b5479788b-w2nc6 1/1 Running 0 22s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Service access method and verification + +I accessed the app using ```bash minikube service my-app-service ``` and verified it by sending requests with curl to endpoints like /health and /ready. + +## Production Considerations + +### What health checks did you implement and why? + +I implemented a liveness probe on /health to restart unhealthy containers and a readiness probe on /ready to ensure pods start receiving traffic only when they are ready to work. + +### Resource limits rationale + +- I set limits to prevent resource overuse, and requests to guarantee the pod gets enough cpu and memory to run reliably. + +### How would you improve this for production? + +- I would add proper logging/monitoring like we did in the previous labs, add autoscaling, consider other update strategies (like canary update). + +### Monitoring and observability strategy + +- In previous labs we used Prometheus for metrics and loki & promtail for logs, also Grafana for dashboard representation + +## Challenges & Solutions + +### Issues encountered + +- I didn't work with NodePort before so I has to stydy it a little bit. Also I didn't know about minikube. + +### How you debugged (logs, describe, events) + +- I researched StackOverflow and other sources, such as documentation for kubernetes and minikube + +### What you learned about Kubernetes + +- I studied kubernetes in the SRE course last semester so I was already pretty familiar with it. We didn't use NodePort service though, and also didn't set up our own cluster since the course team provided us with it. + diff --git a/labs/lab18/app_python/docs/LAB10.md b/labs/lab18/app_python/docs/LAB10.md new file mode 100644 index 0000000000..e3f80de3ab --- /dev/null +++ b/labs/lab18/app_python/docs/LAB10.md @@ -0,0 +1,499 @@ +# Documentation + +## Chart Overview + +### Chart structure explanation + +- the chart has templates/ for kubectl manifests, values.yaml for default settings, and _helpers.tpl for reusable template functions. Hooks are in templates/hooks/ for pre and post-install jobs + +### Key template files and their purpose + +- deployment.yaml defines the app pods and containers +- service.yaml exposes them, and hooks run tasks before or after install +- helpers in _helpers.tpl build names, labels, and selectors consistently, and then can be reused in different config files + +### Values organization strategy + +- values.yaml has defaults, while values-dev.yaml and values-prod.yaml override settings for different environments. +This keeps environment configs separate and easy to manage. + +## Configuration Guide + +### Important values and their purpose + +- replicaCount sets pod number, resources manage CPU/memory, service.type controls what role will the service have (NodePort vs LoadBalancer, etc). LivenessProbe and readinessProbe check pod health. + +### How to customize for different environments + +- you can use values-dev.yaml for dev with 1 replica and NodePort, values-prod.yaml for prod with more replicas and LoadBalancer. and you can also override values on install with --set + +### Example installations with different configurations + +- dev + +```bash +helm install myapp-dev k8s/app_python -f k8s/app_python/values-dev.yaml +``` + +- prod + +```bash +helm install myapp-prod k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +## Hook Implementation + +### What hooks you implemented and why + +- I implemented a pre-install job for setup tasks and a post-install job for validation, to simulate real lifecycle tasks + +### Hook execution order and weights + +- Pre-install has weight -5 to run early, post-install has weight 5 to run after deployment. + +### Deletion policies explanation + +- both hooks have hook-succeeded policy so they auto-delete after finishing successfully + +## Installation Evidence + +### Terminal output showing Helm installation and version (should be 4.x) + +```bash +==> Fetching downloads for: helm +✔︎ Bottle Manifest helm (4.1.3) Downloaded 7.4KB/ 7.4KB +✔︎ Bottle helm (4.1.3) Downloaded 18.1MB/ 18.1MB +==> Pouring helm--4.1.3.arm64_tahoe.bottle.tar.gz +🍺 /opt/homebrew/Cellar/helm/4.1.3: 69 files, 61.3MB +==> Running `brew cleanup helm`... +Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`. +Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). +==> Caveats +zsh completions have been installed to: + /opt/homebrew/share/zsh/site-functions +``` +### Output of exploring a public chart (e.g., helm show chart prometheus-community/prometheus) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm show chart prometheus-community/prometheus +annotations: + artifacthub.io/license: Apache-2.0 + artifacthub.io/links: | + - name: Chart Source + url: https://github.com/prometheus-community/helm-charts + - name: Upstream Project + url: https://github.com/prometheus/prometheus +apiVersion: v2 +appVersion: v3.11.0 +dependencies: +- condition: alertmanager.enabled + name: alertmanager + repository: https://prometheus-community.github.io/helm-charts + version: 1.34.* +- condition: kube-state-metrics.enabled + name: kube-state-metrics + repository: https://prometheus-community.github.io/helm-charts + version: 7.2.* +- condition: prometheus-node-exporter.enabled + name: prometheus-node-exporter + repository: https://prometheus-community.github.io/helm-charts + version: 4.52.* +- condition: prometheus-pushgateway.enabled + name: prometheus-pushgateway + repository: https://prometheus-community.github.io/helm-charts + version: 3.6.* +description: Prometheus is a monitoring system and time series database. +home: https://prometheus.io/ +icon: https://raw.githubusercontent.com/prometheus/prometheus.github.io/master/assets/prometheus_logo-cb55bb5c346.png +keywords: +- monitoring +- prometheus +kubeVersion: '>=1.19.0-0' +maintainers: +- email: gianrubio@gmail.com + name: gianrubio + url: https://github.com/gianrubio +- email: zanhsieh@gmail.com + name: zanhsieh + url: https://github.com/zanhsieh +- email: miroslav.hadzhiev@gmail.com + name: Xtigyro + url: https://github.com/Xtigyro +- email: naseem@transit.app + name: naseemkullah + url: https://github.com/naseemkullah +- email: rootsandtrees@posteo.de + name: zeritti + url: https://github.com/zeritti +name: prometheus +sources: +- https://github.com/prometheus/alertmanager +- https://github.com/prometheus/prometheus +- https://github.com/prometheus/pushgateway +- https://github.com/prometheus/node_exporter +- https://github.com/kubernetes/kube-state-metrics +type: application +version: 28.15.0 +``` + +### helm list output + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm list +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +my-python-app-dev default 1 2026-04-02 22:00:55.999506 +0300 MSK deployed app_python-0.1.0 1.0 +my-python-app-prod default 1 2026-04-02 22:12:24.157572 +0300 MSK deployed app_python-0.1.0 1.0 +myrelease default 1 2026-04-02 22:40:56.562009 +0300 MSK deployed app_python-0.1.0 1.0 +``` + +### kubectl get all showing deployed resources + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 43m +pod/my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 31m +pod/myrelease-app-python-569fb4b645-6v9dt 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-8ws5n 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-glt5r 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-qtg4j 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-rgppk 1/1 Running 0 2m32s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 8d +service/my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 43m +service/my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 31m +service/myrelease-app-python-service NodePort 10.107.17.3 80:30009/TCP 2m32s + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-python-app-dev-app-python 1/1 1 1 43m +deployment.apps/my-python-app-prod-app-python 0/5 5 0 31m +deployment.apps/myrelease-app-python 5/5 5 5 2m32s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-python-app-dev-app-python-7d7f699d85 1 1 1 43m +replicaset.apps/my-python-app-prod-app-python-74c5b97dd5 5 5 0 31m +replicaset.apps/myrelease-app-python-569fb4b645 5 5 5 2m32s +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Hook execution output (kubectl get jobs) + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get jobs -w +NAME STATUS COMPLETIONS DURATION AGE +myrelease-app-python-pre-install Running 0/1 0s +myrelease-app-python-pre-install Running 0/1 0s 0s +myrelease-app-python-pre-install Running 0/1 33s 33s +myrelease-app-python-pre-install Running 0/1 43s 43s +myrelease-app-python-pre-install SuccessCriteriaMet 0/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-post-install Running 0/1 0s +myrelease-app-python-post-install Running 0/1 0s 0s +myrelease-app-python-post-install Running 0/1 5s 5s +myrelease-app-python-post-install Running 0/1 15s 15s +myrelease-app-python-post-install SuccessCriteriaMet 0/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +``` + +### Different environment deployments (dev vs prod) + +- Dev + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-dev k8s/app_python -f k8s/app_python/values-dev.yaml +NAME: my-python-app-dev +LAST DEPLOYED: Thu Apr 2 22:00:55 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 2m20s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 2m27s +``` + +- Prod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-prod k8s/app_python -f k8s/app_python/values-prod.yaml +NAME: my-python-app-prod +LAST DEPLOYED: Thu Apr 2 22:12:24 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 12m +my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 12m +my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 49s +``` + + +## Operations + +### Installation commands used + +```bash +helm install name-of-new-release k8s/app_python -f k8s/app_python/values-for-new-release.yaml +``` +### How to upgrade a release + +```bash +helm upgrade myrelease k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +### How to rollback + +```bash +helm rollback myrelease 1 +``` + +### How to uninstall + +```bash +helm uninstall name-of-release +``` + +## Testing & Validation + +### helm lint output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % helm lint app_python +==> Linting app_python +[INFO] Chart.yaml: icon is recommended + +1 chart(s) linted, 0 chart(s) failed +``` +### helm template verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm template app-python k8s/app_python +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: app-python-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app-python-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 +``` + +### Dry-run output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install --dry-run --debug test-release k8s/app_python +level=WARN msg="--dry-run is deprecated and should be replaced with '--dry-run=client'" +level=DEBUG msg="Original chart version" version="" +level=DEBUG msg="Chart path" path=/Users/fountainer/uni/devops/DevOps-Core-Course/app_python/k8s/app_python +level=DEBUG msg="number of dependencies in the chart" chart=app_python dependencies=0 +NAME: test-release +LAST DEPLOYED: Thu Apr 2 21:46:17 2026 +NAMESPACE: default +STATUS: pending-install +REVISION: 1 +DESCRIPTION: Dry run complete +TEST SUITE: None +USER-SUPPLIED VALUES: +{} + +COMPUTED VALUES: +container: + port: 12345 +image: + pullPolicy: IfNotPresent + repository: fountainer/my-app + tag: 2026.03.26 +livenessProbe: + failureThreshold: 5 + initialDelaySeconds: 10 + path: /health + periodSeconds: 10 + timeoutSeconds: 5 +readinessProbe: + initialDelaySeconds: 5 + path: /ready + periodSeconds: 5 +replicaCount: 5 +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi +service: + nodePort: 30007 + port: 80 + protocol: TCP + targetPort: 12345 + type: NodePort +strategy: + maxSurge: 1 + maxUnavailable: 1 + +HOOKS: +MANIFEST: +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: test-release-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-release-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 + +``` +### Application accessibility verification + +![](./../docs/screenshots/lab10-shots/app-working.png) \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB11.md b/labs/lab18/app_python/docs/LAB11.md new file mode 100644 index 0000000000..1c62eae2de --- /dev/null +++ b/labs/lab18/app_python/docs/LAB11.md @@ -0,0 +1,197 @@ +# Documentation + +## Kubernetes Secrets + +### Output of creating and viewing your secret + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl create secret generic app-credentials --from-literal=username=fountainer --from-literal=password=‘mypass293i20@@nekf’ +secret/app-credentials created +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get secret app-credentials -o yaml +apiVersion: v1 +data: + password: 4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ + username: Zm91bnRhaW5lcg== +kind: Secret +metadata: + creationTimestamp: "2026-04-07T14:46:16Z" + name: app-credentials + namespace: default + resourceVersion: "24859" + uid: 6997ca85-68fa-4278-9d51-a6531df977e9 +type: Opaque +``` +### Decoded secret values demonstration + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ" | base64 -d +‘mypass293i20@@nekf’% +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "Zm91bnRhaW5lcg==" | base64 -d +fountainer% +``` +### Explanation of base64 encoding vs encryption + +- Encoding is when we use some publicly accesible algorithm to encode our data. The goal is keeping integrity and usability of the data, it is not really about security. + +- In turn, Encryption is about securuty. It envolves encrypting with an algorithm that can be only resolved by a user who has an encryption key. + +## Helm Secret Integration + +### Chart structure showing secrets.yaml + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % tree app_python/k8s/app_python +app_python/k8s/app_python +├── Chart.yaml +├── charts +├── templates +│ ├── _helpers.tpl +│ ├── deployment.yaml +│ ├── hooks +│ │ ├── post-install-job.yaml +│ │ └── pre-install-job.yaml +│ ├── secrets.yaml +│ └── service.yaml +├── values-dev.yaml +├── values-prod.yaml +└── values.yaml +``` + +### How secrets are consumed in deployment + +- I have $secretName variable that is dynamically set to the name from the values.yaml and values I provide in the helm install command OR defaults to the value from helper. + +### Verification output (env vars in pod, excluding actual values) + +- in pod I have correct env vars: + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-7975557578-6zc9m -- sh +$ echo $PASSWORD +mypass293i20@@nekf +$ echo $USERNAME +fountainer +``` + +- and outside the secrets are hidden + +from ```bash kubectl describe pod mysecretrelease-app-python-7975557578-6zc9m``` + +```bash +Environment: + PASSWORD: Optional: false + USERNAME: Optional: false +``` + + +## Resource Management + +### Resource limits configuration + +```bash +resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} +``` +- in values.yaml I have + +```bash +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" +``` + +### Explanation of requests vs limits + +- requests is a setting that shows kubernates how much resources are needed for a container to run +- limits show how much resources a container is allowed to use (max) + +### How to choose appropriate values + +- you should analyze what processes does your container run and how many cpu/memory it may need +- values can be adjusted by observing the running container +- if you have multiple containers/pods you should constraint them in such a way that they all can work without throttling +- if the memory limit is too low the container can be killed right away + +## Vault Integration + +### Vault installation verification (kubectl get pods) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7975557578-6zc9m 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-7l4tv 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-bqnpd 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-cjjcb 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-st2jd 1/1 Running 0 63m +vault-0 1/1 Running 0 8m23s +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 0 8m23s +``` + +### Policy and role configuration (sanitized) + +- policy + +```bash +/ $ vault policy write myapp-policy /tmp/myapp-policy.hcl +Success! Uploaded policy: myapp-policy +/ $ vault policy read myapp-policy +path "secret/data/myapp/config" { + capabilities = ["read"] +} +``` + +- role config + +```bash +vault write auth/kubernetes/role/myapp-role \ + bound_service_account_names=default \ + bound_service_account_namespaces=default \ + policies=myapp-policy \ + ttl=48h +``` + + +### Proof of secret injection (show file exists, path structure) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-558b98bb9d-8299m -- /bin/sh +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +$ ls -l /vault/secrets +total 4 +-rw-r--r-- 1 100 newuser 180 Apr 7 23:55 config +$ cat /vault/secrets/config +data: map[password:mypass293i20@@nekf username:fountainer] +metadata: map[created_time:2026-04-07T23:32:33.85543147Z custom_metadata: deletion_time: destroyed:false version:1] +$ +``` + +### Explanation of the sidecar injection pattern + +- now every pod contains not only my app container but also vault sidecar container +- vault is able to authenticate in kubernates and inject secrets into the pod + +## Security Analysis + +### Comparison: K8s Secrets vs Vault + +- kubernates secrets are just encoded into base 64 and everyone who gets access to the cluster can decode them and get sensitive data, on the other hand, vault provides data encryption that is much more safer since you need an encryption key to encrypt it + +### When to use each approach + +- encoding is good for keeping data usability and integrity, so different machines can use it (like for seeing special symbols on a web page), it is like... more secure than nothing, but not reeally secure + +- vault encryption is needed for keeping sensitive data secure, like for storing passwords for the services on the virtual machines, etc + +### Production recommendations + +- in production you should always try to use strong encryption algorithms to keep your data secure diff --git a/labs/lab18/app_python/docs/LAB12.md b/labs/lab18/app_python/docs/LAB12.md new file mode 100644 index 0000000000..f79a158422 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB12.md @@ -0,0 +1,141 @@ +# Documentation + +## Application Changes + +### Description of visits counter implementation + +visits counter is a global integer that increases every time / endpoint is called. it writes the value into a file (visits) so it can survive pod restarts via pvc. + +### New endpoint documentation + +/visits returns the current stored counter value from data/visits file + +### Local testing evidence with Docker + +Here you can see the counter value persistence across restarts. +![](./../docs/screenshots/lab12-shots/counter.png) + +## ConfigMap Implementation + +### ConfigMap template structure + +Helm ConfigMap loads a local config.json file via .Files.Get, so the whole json is injected into the cluster as one config object + +### config.json content + +it contains basic app metadata like name, environment, and feature flags (debug, metrics) plus log level settings + +### How ConfigMap is mounted as file + +the ConfigMap is mounted as a volume at /config, so inside the container we can read /config/config.json as a normal file. + +### How ConfigMap provides environment variables + +by using envFrom: configMapRef to inject keys like APP_NAME, APP_ENV, and LOG_LEVEL directly as environment variables. + + +### Verification outputs + +- File content inside pod (cat /config/config.json) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- cat /config/config.json +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +}% +``` + +- Environment variables in pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep APP_ +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +APP_ENV=dev +APP_NAME=my-app +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PROTO=tcp +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_HOST=10.103.123.105 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_ADDR=10.103.123.105 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep LOG_LEVEL +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +LOG_LEVEL=info +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + +## Persistent Volume + +### PVC configuration explanation + +PVC requests a small storage size (100Mi) and creates a persistent volume that is mounted into the pod at /app/data to store the visits file. + +### Access modes and storage class discussion + +ReadWriteOnce is used because only one pod needs to write to the volume, and storageClass is optional so it uses the cluster default. + +### Volume mount configuration + +the volume is mounted into the container at /app/data, and the app writes visits file there so data stays after pod restarts/recreations + +### Persistence test evidence: + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-z6r7b -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl delete pod mysecretrelease-app-python-7b6579656c-z6r7b +pod "mysecretrelease-app-python-7b6579656c-z6r7b" deleted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7b6579656c-b2tzf 2/2 Running 0 60s +vault-0 1/1 Running 3 (84m ago) 8d +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 3 (85m ago) 8d +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-b2tzf -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## ConfigMap vs Secret + +### When to use ConfigMap + +ConfigMap is used for non-sensitive configuration like app name, environment, log level, and feature flags. + +### When to use Secret + +secret is used for sensitive data like username, password, or anything that should not be visible in plain text + +### Key differences + +configMap is plain text and not encrypted, while Secret is base64 encoded and used for sensitive data. + +## Additional Outputs: + +### kubectl get configmap,pvc output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get configmap,pvc +NAME DATA AGE +configmap/kube-root-ca.crt 1 22d +configmap/mysecretrelease-app-python-config 1 7m2s +configmap/mysecretrelease-app-python-env 3 7m2s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/mysecretrelease-app-python-data Bound pvc-42d4685f-8463-4434-8959-0bacd5d972b6 100Mi RWO standard 7m2s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + diff --git a/labs/lab18/app_python/docs/LAB13.md b/labs/lab18/app_python/docs/LAB13.md new file mode 100644 index 0000000000..67c2551dda --- /dev/null +++ b/labs/lab18/app_python/docs/LAB13.md @@ -0,0 +1,214 @@ +# Documentation + +## ArgoCD Setup + +### Installation verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install argocd argo/argo-cd --namespace argocd +NAME: argocd +LAST DEPLOYED: Thu Apr 23 18:37:11 2026 +NAMESPACE: argocd +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +NOTES: +In order to access the server UI you have the following options: + +1. kubectl port-forward service/argocd-server -n argocd 8080:443 + + and then open the browser on http://localhost:8080 and accept the certificate + +2. enable ingress in the values file `server.ingress.enabled` and either + - Add the annotation for ssl passthrough: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-1-ssl-passthrough + - Set the `configs.params."server.insecure"` in the values file and terminate SSL at your ingress: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-2-multiple-ingress-objects-and-hosts + + +After reaching the UI the first time you can login with username: admin and the random password generated during the installation. You can find the password by running: + +kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d + +(You should delete the initial secret afterwards as suggested by the Getting Started Guide: https://argo-cd.readthedocs.io/en/stable/getting_started/#4-login-using-the-cli) +(devops) fountainer@Veronicas-MacBook-Air app_python % helm list -n argocd +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +argocd argocd 1 2026-04-23 18:37:11.582773 +0300 MSK deployed argo-cd-9.5.4 v3.3.8 +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd version +argocd: v3.3.8+7ae7d2c.dirty + BuildDate: 2026-04-21T20:19:34Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: dirty + GitTag: v3.3.8 + GoVersion: go1.26.2 + Compiler: gc + Platform: darwin/arm64 +argocd-server: v3.3.8 + BuildDate: 2026-04-21T17:19:47Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: clean + GitTag: v3.3.8 + GoVersion: go1.25.5 + Compiler: gc + Platform: linux/arm64 + Kustomize Version: v5.8.1 2026-02-09T16:15:27Z + Helm Version: v3.19.4+g7cfb6e4 + Kubectl Version: v0.34.0 + Jsonnet Version: v0.21.0 + ``` + +### UI access method + +Accessed ArgoCD UI via kubectl port-forward to localhost and logged in through the browser using the admin credentials and a previously created password. + +### CLI configuration + +Installed the argocd CLI with Homebrew and connected to the server using argocd login localhost:8080 --insecure. + +## Application Configuration + +### Application manifests + +Created an ArgoCD Application manifest defining the app name, project, source repository, and destination cluster settings. + +### Source and destination configuration + +Configured the application to pull from the Git repository (Helm chart path) and deploy to the in-cluster Kubernetes API in the default namespace. + +### Values file selection + +Specified values.yaml as the Helm values file to control application configuration during deployment. To test, I changed the number of replicas from 1 to 3. + +## Multi-Environment + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (22m ago) 22m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n prod +NAME READY STATUS RESTARTS AGE +python-app-prod-app-python-9dc9c6fbc-n8mtr 1/1 Running 0 4m14s +python-app-prod-app-python-9dc9c6fbc-sqflv 1/1 Running 0 21m +python-app-prod-app-python-9dc9c6fbc-xfvbj 1/1 Running 0 4m14s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app list +NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET +argocd/my-app https://kubernetes.default.svc default default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-dev https://kubernetes.default.svc dev default Synced Healthy Auto-Prune https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-prod https://kubernetes.default.svc prod default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +``` + +### Dev vs Prod configuration differences + +Dev uses smaller resource limits and fewer replicas for faster iteration, while prod uses higher replica count and stricter resource limits for stability and performance. + +### Sync policy differences and rationale + +Dev is configured with automated sync including self-heal and prune for continuous deployment, while prod uses manual sync to ensure controlled and reviewed releases. + +### Namespace separation + +Dev and prod are deployed into separate namespaces to isolate environments, prevent interference, and ensure independent lifecycle management. + +## Self-Healing Evidence + +### Manual scale test with before/after + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 30m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=5 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 31m +``` + +### Pod deletion test + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (31m ago) 32m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl delete pod python-app-dev-app-python-59fdf484d5-g97xn -n dev +pod "python-app-dev-app-python-59fdf484d5-g97xn" deleted +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 0/1 Running 0 15s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 1/1 Running 0 37s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Configuration drift test + +Here you can see the drift +![](./../docs/screenshots/lab13-shots/drift.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app get python-app-dev +Name: argocd/python-app-dev +Project: default +Server: https://kubernetes.default.svc +Namespace: dev +URL: https://argocd.example.com/applications/python-app-dev +Source: +- Repo: https://github.com/ffountainer/DevOps-Core-Course + Target: lab13 + Path: app_python/k8s/app_python + Helm Values: values-dev.yaml +SyncWindow: Sync Allowed +Sync Policy: Automated (Prune) +Sync Status: Synced to lab13 (27593a9) +Health Status: Healthy + +GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE +batch Job dev python-app-dev-app-python-pre-install Succeeded PreSync Reached expected number of succeeded pods + Secret dev app-credentials Synced secret/app-credentials configured + ConfigMap dev python-app-dev-app-python-env Synced configmap/python-app-dev-app-python-env unchanged + ConfigMap dev python-app-dev-app-python-config Synced configmap/python-app-dev-app-python-config unchanged + PersistentVolumeClaim dev python-app-dev-app-python-data Synced Healthy persistentvolumeclaim/python-app-dev-app-python-data unchanged + Service dev python-app-dev-app-python-service Synced Healthy service/python-app-dev-app-python-service unchanged +apps Deployment dev python-app-dev-app-python Synced Healthy deployment.apps/python-app-dev-app-python configured +batch Job dev python-app-dev-app-python-post-install Succeeded PostSync Reached expected number of succeeded pods +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=2 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 52m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl describe deployment python-app-dev-app-python -n dev | grep Replicas +Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable + Available True MinimumReplicasAvailable +``` + +### Explanation of behaviors + +- Explain when ArgoCD syncs vs when Kubernetes heals + +ArgoCD references changes in git, and Kubernetes monitors the cluster (it will heal if the pod crushes, etc) + +- What triggers ArgoCD sync? + +git repo changes, manual sync triggered, auto-sync enabled, drift detected + self-heal enabled + +- What is the sync interval? + +the default sync is every 3 minutes + +## Screenshots + +### ArgoCD UI showing the applications + +![](./../docs/screenshots/lab13-shots/application.png) + +### Sync status + +![](./../docs/screenshots/lab13-shots/sync%20status.png) +![](./../docs/screenshots/lab13-shots/sync.png) + +### Application details view + +![](./../docs/screenshots/lab13-shots/details.png) \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB14.md b/labs/lab18/app_python/docs/LAB14.md new file mode 100644 index 0000000000..7a37244b4c --- /dev/null +++ b/labs/lab18/app_python/docs/LAB14.md @@ -0,0 +1,252 @@ +# Documentation + +## Argo Rollouts Setup + +### Installation verification + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl apply -n argo-rollouts -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml +customresourcedefinition.apiextensions.k8s.io/analysisruns.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/analysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/clusteranalysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/experiments.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/rollouts.argoproj.io created +serviceaccount/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-admin created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-edit created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-view created +clusterrolebinding.rbac.authorization.k8s.io/argo-rollouts created +configmap/argo-rollouts-config created +secret/argo-rollouts-notification-secret created +service/argo-rollouts-metrics created +deployment.apps/argo-rollouts created +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 54s +``` +```bash +==> Fetching downloads for: kubectl-argo-rollouts +✔︎ Formula kubectl-argo-rollouts (v1.8.3) Verified 130.1MB/130.1MB +==> Installing kubectl-argo-rollouts from argoproj/tap +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl argo rollouts version +kubectl-argo-rollouts: v1.8.3+49fa151 + BuildDate: 2025-06-04T22:19:21Z + GitCommit: 49fa1516cf71672b69e265267da4e1d16e1fe114 + GitTreeState: clean + GoVersion: go1.23.9 + Compiler: gc + Platform: darwin/amd64 +``` + +### Dashboard access + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 12m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 28s +``` + +![](./../docs/screenshots/lab14-shots/argo-dashboard-access.png) + +### Understand Rollout vs Deployment + +Rollout CRD vs Deployment + +- Rollout and Deployment are kinda similar and both have replicas, selector, template, strategy fields, they manage pod creation. But rollout has additional fields for strategy that allow to perform more controllable rollouts with specific configurations, like rolling an update for a group of users, not for all. + +Additional fields for progressive delivery + +- canary: allows gradual traffic shifting to a new version using steps (e.g., setWeight, pause) +- blueGreen: supports switching between old and new versions using separate services +- steps: defines staged rollout progression +- analysis: integrates automated checks (metrics, tests) during rollout +- pause: enables manual or timed pauses between steps +- trafficRouting: controls how traffic is split between versions (with ingress/service mesh) + + +## Canary Deployment + +### Strategy configuration explained + +The rollout uses a canary strategy to gradually shift traffic from the old version to the new one. It is configured in steps (20%, 40%, 60%, 80%, 100%) with pauses to allow validation and manual control. This approach reduces risk by exposing the new version to a small part of users before full deployment. + +### Step-by-step rollout progression (screenshots from dashboard) + +![](./../docs/screenshots/lab14-shots/canary-prom-1.png) +![](./../docs/screenshots/lab14-shots/canary-prom-2.png) +![](./../docs/screenshots/lab14-shots/canary-prom-3.png) + +### Promotion and abort demonstration + +Promotion (screenshots can be seen in the prev step) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ॥ Paused +Message: CanaryPauseStep +Strategy: Canary + Step: 1/9 + SetWeight: 20 + ActualWeight: 25 +Images: fountainer/my-app:16-04 (canary, stable) +Replicas: + Desired: 3 + Current: 4 + Updated: 1 + Ready: 4 + Available: 4 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ॥ Paused 17m +├──# revision:2 +│ └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 69s canary +│ └──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 68s ready:1/1 +└──# revision:1 + └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet ✔ Healthy 17m stable + ├──□ myapp-app-python-5bc87cfdf6-2tzkc Pod ✔ Running 17m ready:1/1 + ├──□ myapp-app-python-5bc87cfdf6-bnpd6 Pod ✔ Running 17m ready:1/1 + └──□ myapp-app-python-5bc87cfdf6-qfg9s Pod ✔ Running 17m ready:1/1 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts promote myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' promoted +``` + +Abort + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get rollouts -n argo-rollouts +NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE +myapp-app-python 3 4 1 4 31m +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts abort myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' aborted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ✖ Degraded +Message: RolloutAborted: Rollout aborted update to revision 3 +Strategy: Canary + Step: 0/9 + SetWeight: 0 + ActualWeight: 0 +Images: fountainer/my-app:16-04 (stable) +Replicas: + Desired: 3 + Current: 3 + Updated: 0 + Ready: 3 + Available: 3 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ✖ Degraded 32m +├──# revision:3 +│ └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet • ScaledDown 32m canary +└──# revision:2 + └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 16m stable + ├──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 16m ready:1/1 + ├──□ myapp-app-python-76b59b6c66-7cwr4 Pod ✔ Running 10m ready:1/1 + └──□ myapp-app-python-76b59b6c66-skfdd Pod ✔ Running 10m ready:1/1 +``` +![](./../docs/screenshots/lab14-shots/canary-abort.png) + +## Blue-Green Deployment + +### Strategy configuration explained + +The blue-green strategy uses two environments: active and preview. The preview service runs the new version while the active service continues serving production traffic. After testing, the active service is switched to the new version instantly when promoted. This allows safe testing before release and quick rollback if needed. + +### Preview vs active service + +The active service is used by users in production and always points to the stable version. The preview service is used to test the new version before it is promoted. This separation ensures the new version can be verified without affecting real users. + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-preview 8081:80 -n argo-rollouts +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-service 8080:80 -n argo-rollouts +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +``` + +### Promotion process + +Promotion + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm upgrade --install myapp . -n argo-rollouts +Release "myapp" has been upgraded. Happy Helming! +NAME: myapp +LAST DEPLOYED: Thu Apr 30 23:12:57 2026 +NAMESPACE: argo-rollouts +STATUS: deployed +REVISION: 9 +DESCRIPTION: Upgrade complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods -n argo-rollouts +kubectl get svc -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 6h24m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 6h12m +myapp-app-python-76b59b6c66-7cwr4 1/1 Running 0 37m +myapp-app-python-76b59b6c66-pgtgq 1/1 Running 0 43m +myapp-app-python-76b59b6c66-skfdd 1/1 Running 0 37m +myapp-app-python-f7cddd7c7-5nvtx 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-xng4z 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-zjfpv 1/1 Running 0 12m +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +argo-rollouts-dashboard ClusterIP 10.106.240.192 3100/TCP 6h12m +argo-rollouts-metrics ClusterIP 10.109.176.51 8090/TCP 6h24m +myapp-app-python-preview ClusterIP 10.97.144.248 80/TCP 16m +myapp-app-python-service NodePort 10.101.217.107 80:30009/TCP 59m +``` + +![](./../docs/screenshots/lab14-shots/blue-green-1.png) +![](./../docs/screenshots/lab14-shots/bg-2.png) +![](./../docs/screenshots/lab14-shots/bg-4.png) + +Rollback + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts undo myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' undo +``` +![](./../docs/screenshots/lab14-shots/bg-5.png) + + +## Strategy Comparison + +### When to use canary vs blue-green + +canary is used when you want to slowly roll out changes to users and reduce risk step by step. blue-green is used when you want an instant switch between versions after testing + +### Pros and cons of each + +- canary is safer for production because it exposes changes gradually, but it takes longer and is more complex to monitor + +- blue-green is faster and simpler at switch time, but requires double resources and has less gradual control. + +### Your recommendation for different scenarios + +use canary for production systems where stability is critical. use blue-green for fast releases or when you want quick testing and instant rollback. + +## CLI Commands Reference + +### Commands you used + +```kubectl argo rollouts get rollout -w``` is used to watch rollout progress. ```kubectl argo rollouts promote``` is used to move to the next step in canary or switch in blue-green. ```kubectl argo rollouts undo``` is used to rollback to the previous version. + +### Monitoring and troubleshooting + +```kubectl get pods```, ```kubectl get svc```, and ```kubectl describe rollout``` are used to check cluster state and debug issues. dashboard is used to visually monitor rollout progress and traffic changes. \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB15.md b/labs/lab18/app_python/docs/LAB15.md new file mode 100644 index 0000000000..b5a5b60b72 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB15.md @@ -0,0 +1,168 @@ +# Documentation + +## StatefulSet Overview + +### Why StatefulSet + +It is used when pods need stable identity and storage, like each pod keeping its own data and name even after restart. + +### Differences from Deployment + +Key differences: +- deployment pods are interchangeable and can change names/storage after restarts, while statefulset pods have fixed names (pod-0, pod-1) and their own persistent storage. + +When to use Deployment vs StatefulSet: +- deployment is used for stateless apps (like web servers), and statefulset for apps that need stable data and identity (like databases). + +Examples of stateful workloads: +- databases like mysql/postgresql, message queues, systems like elasticsearch + +### Headless Services + +What is a headless service (clusterIP: None)? +- a service without a cluster ip that lets you directly access individual pods instead of load balancing + +How DNS works with StatefulSets? +- each pod gets its own dns name like pod-0.service-name.namespace.svc.cluster.local, and they can be addressed individually + +## Resource Verification + +### Output of kubectl get pod,sts,svc,pvc + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get statefulset +NAME READY AGE +myapp-app-python 3/3 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +myapp-app-python-0 1/1 Running 0 42s +myapp-app-python-1 1/1 Running 0 25s +myapp-app-python-2 1/1 Running 0 16s +myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 42s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pvc +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 53s +data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 36s +data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 27s +my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 53s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod,sts,svc,pvc +NAME READY STATUS RESTARTS AGE +pod/my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +pod/my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +pod/my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +pod/myapp-app-python-0 1/1 Running 0 99s +pod/myapp-app-python-1 1/1 Running 0 82s +pod/myapp-app-python-2 1/1 Running 0 73s +pod/myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 99s + +NAME READY AGE +statefulset.apps/myapp-app-python 3/3 99s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 7d8h +service/myapp-app-python-preview ClusterIP 10.98.51.97 80/TCP 99s +service/myapp-app-python-service NodePort 10.110.182.139 80:30009/TCP 99s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 99s +persistentvolumeclaim/data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 82s +persistentvolumeclaim/data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 73s +persistentvolumeclaim/my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +persistentvolumeclaim/myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 99s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## Network Identity + +### DNS resolution outputs + +the naming pattern is ```.``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec -it myapp-app-python-0 -- /bin/sh +$ getent hosts myapp-app-python-0.myapp-app-python-headless +10.244.0.177 myapp-app-python-0.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-1.myapp-app-python-headless +10.244.0.179 myapp-app-python-1.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-2.myapp-app-python-headless +10.244.0.180 myapp-app-python-2.myapp-app-python-headless.default.svc.cluster.local +``` + +## Per-Pod Storage Evidence + +### Different visit counts per pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-0 8080:12345 +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % pyenv shell devops +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-1 8081:12345 +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-2 8082:12345 +Forwarding from 127.0.0.1:8082 -> 12345 +Forwarding from [::1]:8082 -> 12345 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8080/visits +{ + "visits": 13 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8081/visits +{ + "visits": 7 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8082/visits +{ + "visits": 2 +} +``` + +![](./../docs/screenshots/lab15-shots/visits-diff.png) + +## Persistence Test + +### data survives pod deletion + +![](./../docs/screenshots/lab15-shots/pers_test.png) \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB16.md b/labs/lab18/app_python/docs/LAB16.md new file mode 100644 index 0000000000..b71aebd57e --- /dev/null +++ b/labs/lab18/app_python/docs/LAB16.md @@ -0,0 +1,227 @@ +# Documentation + +## Stack Components + +### Descriptions in your own words + +- Prometheus Operator: it's a kubernates tool that allows you to automate prometheus deployment and management. it provides a set of custom resource definitions, and you can make your own configuration with those. + +- Prometheus: it's a tool for monitoring and alerting, it stores metrics as a series of timestampts. + +- Alertmanager: an instruments to manage alerts. when metrics reach invalid state, alertmanager will receive alerts, group and send them to asignees. you can configure when to silence alerts or start reaching out to the next person if the first one is not responding (it's an escalation), and create other custom settings. + +- Grafana: it is a dashboard for tracking the current state of the system by visualising logs and metrics. you can define alert rules there to see if the new metric value is out of the valid tresholds. + +- kube-state-metrics: it's a service that exposes metrics related to kubernates objects, they are created automatically and describe your pods current state. + +- node-exporter: it's an agent that exposes internal metrics for a node (like cpu, memory, etc), then prometheus can scrape those metrics. + +## Installation Evidence + +### kubectl get po,svc -n monitoring + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo add prometheus-community https://prometheus-community.github.io/helm-charts +"prometheus-community" already exists with the same configuration, skipping +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo update +Hang tight while we grab the latest from your chart repositories... +...Successfully got an update from the "hashicorp" chart repository +...Successfully got an update from the "argo" chart repository +...Successfully got an update from the "prometheus-community" chart repository +Update Complete. ⎈Happy Helming!⎈ +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get po,svc -n monitoring +NAME READY STATUS RESTARTS AGE +pod/alertmanager-monitoring-kube-prometheus-alertmanager-0 2/2 Running 0 2m34s +pod/monitoring-grafana-bbc5c674-8cbd9 3/3 Running 0 2m56s +pod/monitoring-kube-prometheus-operator-54f68d65b4-99ck2 1/1 Running 0 2m56s +pod/monitoring-kube-state-metrics-5957bd45bc-5rpqr 1/1 Running 0 2m56s +pod/monitoring-prometheus-node-exporter-c8fg6 1/1 Running 0 2m57s +pod/prometheus-monitoring-kube-prometheus-prometheus-0 2/2 Running 0 2m34s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/alertmanager-operated ClusterIP None 9093/TCP,9094/TCP,9094/UDP 2m34s +service/monitoring-grafana ClusterIP 10.110.156.182 80/TCP 2m57s +service/monitoring-kube-prometheus-alertmanager ClusterIP 10.111.243.229 9093/TCP,8080/TCP 2m57s +service/monitoring-kube-prometheus-operator ClusterIP 10.99.16.80 443/TCP 2m57s +service/monitoring-kube-prometheus-prometheus ClusterIP 10.106.17.206 9090/TCP,8080/TCP 2m57s +service/monitoring-kube-state-metrics ClusterIP 10.102.26.186 8080/TCP 2m57s +service/monitoring-prometheus-node-exporter ClusterIP 10.100.205.92 9100/TCP 2m57s +service/prometheus-operated ClusterIP None 9090/TCP 2m34s +``` + +## Dashboard Answers + +### Pod Resources: CPU/memory usage of your StatefulSet + +Due to the pods and the app itself being very lightweight, CPU and memory usage never went higher than initially allocated resources (100m CPU and 128Mi memory). Even under high load (I used multiple loops with curl), the initial resources were enough. You will see more detailed usages info in the next question. + +Example for pod 2: + +![](./../docs/screenshots/lab16-shots/cpupod2.png) + +![](./../docs/screenshots/lab16-shots/memorypod2.png) + +### Namespace Analysis: Which pods use most/least CPU in default namespace? + +I decided to use Prometheus for evidence, since the resource usage was really low, and didn't show up properly in Grafana. + +curl I used (the first count is much bigger since I previously tested only with pod 2) + +![](./../docs/screenshots/lab16-shots/curl.png) + +usage + +![](./../docs/screenshots/lab16-shots/namespace%20usage.png) + +As we can see, all statefulset pods used roughly the same amount of CPU and memory resources. This is anticipated, because load balancing is used for routing traffic to different pods. + +### Node Metrics: Memory usage (% and MB), CPU cores + +CPU and Memory usage for the whole minikube node was: + +![](./../docs/screenshots/lab16-shots/node%20cpu%20memory.png) + +It is much higher than resources used in statefulset since the node contains all different namespaces in my minikube cluster. + +### Kubelet: How many pods/containers managed? + +16 pods and 41 containers + +![](./../docs/screenshots/lab16-shots/pods%20managed.png) + +### Network: Traffic for pods in default namespace + +![](./../docs/screenshots/lab16-shots/network.png) + +### Alerts: How many active alerts? Check Alertmanager UI + +![](./../docs/screenshots/lab16-shots/alert.png) + +## Init Containers: Implementation and proof of success + +I implemented two init container patterns. First one downloads a file with wget into a shared emptyDir volume and the main container successfully accessed it from /data/index.html. Second one uses a wait-for-service init container that continuously checks the nginx service with wget and only starts the main container after the service becomes reachable. + +### Init container + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 Init:0/1 0 2s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 PodInitializing 0 4s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 5s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +``` +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl logs init-download-pod -c init-download +Connecting to example.com (172.66.147.243:443) +wget: note: TLS certificate validation not implemented +saving to '/work-dir/index.html' +index.html 100% |********************************| 528 0:00:00 ETA +'/work-dir/index.html' saved +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec init-download-pod -- cat /data/index.html +Defaulted container "main-app" out of: main-app, init-download (init) +Example Domain

+``` + +### Waiting for service container + +```bash +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f waiting.yaml +pod/wait-service-pod created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +wait-service-pod 0/1 Init:0/1 0 22s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl logs wait-service-pod -c wait-for-service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f nginx.yaml +deployment.apps/myservice created +service/myservice created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 3s +wait-service-pod 0/1 Init:0/1 0 55s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 5s +wait-service-pod 0/1 PodInitializing 0 57s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 7s +wait-service-pod 1/1 Running 0 59s +``` + +After service was discovered: + +```bash +waiting for service + + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, nginx is successfully installed and working. +Further configuration is required for the web server, reverse proxy, +API gateway, load balancer, content cache, or other features.

+ +

For online documentation and support please refer to +nginx.org.
+To engage with the community please visit +community.nginx.org.
+For enterprise grade support, professional services, additional +security features and capabilities please refer to +f5.com/nginx.

+ +

Thank you for using nginx.

+ + +``` \ No newline at end of file diff --git a/labs/lab18/app_python/docs/LAB17.md b/labs/lab18/app_python/docs/LAB17.md new file mode 100644 index 0000000000..7a1216a967 --- /dev/null +++ b/labs/lab18/app_python/docs/LAB17.md @@ -0,0 +1,215 @@ +# Documentation + +## Deployment summary + +### Worker URL + +[https://edge-api.v-levasheva.workers.dev](https://edge-api.v-levasheva.workers.dev) + +### Main routes + +/ for general app info + +/health for health status + +/edge for cloudflare edge metadata + +/counter for a persistent KV-based request counter, which shows the number of visits for / route + +/admin-check is for checking if the requester is an admin, it uses secrets API_TOKEN and ADMIN_EMAIL, and the requester should provide valid token and email in the request headers + +and also there is "Not Found" response for non-existent route + +### Configuration used + +the worker was configured using wrangler.jsonc, which defines the worker name, entrypoint (src/index.ts), compatibility date, environment variables, and resource bindings. the project uses a TypeScript Worker template and includes an APP_NAME variable binding together with a COUNTER_KV KV namespace binding used for persistent storage in the /counter endpoint. + +## Evidence + +### Screenshot of Cloudflare dashboard + +![](./../docs/screenshots/lab17-shots/dashboard.png) + +### Example /edge JSON response + +Firstly, the local testing of all endpoints (for the deployed /edge some info such as asn and httpProtocol were added): + +![](./../docs/screenshots/lab17-shots/endpoints%20local.png) + +And the /edge endpoint in the deployed worker: + +![](./../docs/screenshots/lab17-shots/edge%20deployed.png) + +### How Workers distributes execution globally? + +cloudflare workers are automatically executed on cloudflare edge locations around the world, so requests are handled close to the user without manually selecting regions. unlike traditional VM or PaaS platforms where you often deploy separately to regions, workers use cloudflare’s global network automatically, so there is no separate “deploy to 3 regions” step. + +### The difference between workers.dev, Routes, and Custom Domains + +- workers.dev is the default public cloudflare subdomain automatically provided for testing and accessing workers + +- routes are specific url endpoints that worker will provide access to + +- custom domains allow exposing the worker directly through an owned custom domain, not through a provided workers.dev + +## Configuration, Secrets & Persistence + +### Explain why plaintext vars are not suitable for secrets + +variables I added: "APP_NAME" and "COURSE_NAME" + +plaintext vars are not safe for secrets because they are stored in config and visible in repo and dashboard, so anyone can read them + +### Secrets + +I added 2 secrets: API_TOKEN and ADMIN_EMAIL. They are used in the /admin-check endpoint, where the requester can pass their access token and email and see if they can be authenticated as an admin. + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler secret list +[ + { + "name": "ADMIN_EMAIL", + "type": "secret_text" + }, + { + "name": "API_TOKEN", + "type": "secret_text" + } +] +``` + +![](./../docs/screenshots/lab17-shots/secrets.png) + +### Workers KV persistence + +### Document what you stored and how you verified it + +I stored the number of visits of the / endpoint. Each time / is visited, the counter increases by one, and visits value is updated. The value then can be accessed through the /visits endpoint. + +The persistance verification: + +![](./../docs/screenshots/lab17-shots/persistance.png) + +## Observability & Operations + +### Example log or metrics screenshot + +Logs + +![](./../docs/screenshots/lab17-shots/logs.png) + +Metrics + +![](./../docs/screenshots/lab17-shots/metrics.png) + +I looked at errors, requests, and request duration metrics for all deployed versions for the last 24 hours. + +requests: total of 192. this metrics shows how many http requests hit the Worker through all endpoints. + +errors: 0 errors. it can be confusing since I intentially hit invalid endpoint such as https://edge-api.v-levasheva.workers.dev/smth a lot of times to get error: "Not Found", but the case was that the Worker does not count 404 responses as errors, the real errors that are considered are runtime failures, which I did not have. + +request duration: 3.15 ms on average. this metric shows the latency - how much it takes to process a request and send back a response. + +### Multiple Deployments + +* I created about 10 deployments but put here last 2 for readability + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler deployments list + + ⛅️ wrangler 4.90.0 +Created: 2026-05-10T16:57:52.287Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 4844c942-5e05-4199-999f-b54af219ed99 + Created: 2026-05-10T16:48:11.805Z + Tag: - + Message: - + +Created: 2026-05-10T17:01:05.875Z +Author: v.levasheva@innopolis.university +Source: Unknown (deployment) +Message: - +Version(s): (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f + Created: 2026-05-10T17:01:03.267Z + Tag: - + Message: - +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air edge-api % npx wrangler rollback + + ⛅️ wrangler 4.90.0 +─────────────────── +├ Your current deployment has 1 version(s): +│ +│ (100%) 9f0c5465-b3c2-462d-b199-bb1a6dc23d9f +│ Created: 2026-05-10T17:01:03.267267Z +│ Tag: - +│ Message: - +│ +✔ Please provide an optional message for this rollback (120 characters max) … test rollback +│ +├  WARNING  You are about to rollback to Worker Version 4844c942-5e05-4199-999f-b54af219ed99. +│ This will immediately replace the current deployment and become the active deployment across all your deployed triggers. +│ However, your local development environment will not be affected by this rollback. +│ Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc). +│ +│ (100%) 4844c942-5e05-4199-999f-b54af219ed99 +│ Created: 2026-05-10T16:48:11.805042Z +│ Tag: - +│ Message: - +│ +✔ Are you sure you want to deploy this Worker Version to 100% of traffic? … yes +Performing rollback... +│ +╰  SUCCESS  Worker Version 4844c942-5e05-4199-999f-b54af219ed99 has been deployed to 100% of traffic. + +Current Version ID: 4844c942-5e05-4199-999f-b54af219ed99 +``` + +## Kubernetes vs Cloudflare Workers Comparison + +| Aspect | Kubernetes | Cloudflare Workers | +|--------|------------|--------------------| +| Setup complexity | really high, you should create cluster setup, manifests, networking, monitoring, and scaling configuration yourself | much simpler, just write code and deploy with minimal config | +| Deployment speed | slower, containers must build and start | very fast, seconds are enough | +| Global distribution | usually requires manual configuration | automatic global edge distribution | +| Cost (for small apps) | can become expensive due to always-running release | cheaper for lightweight APIs and low traffic apps | +| State/persistence model | persistent volumes, databases, StatefulSets | stateless by default, persistence through KV | +| Control/flexibility | very high control over runtime, networking, and storage | less low-level control, but much simpler to use | +| Best use case | complex and heavy programs, projects that require customisation | edge APIs, lightweight services, and globally distributed low-latency apps | + +## When to Use Each + +### Scenarios favoring Kubernetes + +- complex applications with multiple services +- custom configuration +- long-running work processes + +### Scenarios favoring Workers + +- lightweight APIs +- edge services +- globally distributed applications with low latency requirements + +### Your recommendation + +I would use k8s and VMs for big projects that require heavy customisation, and Cloudflare Workers for lightweight apps that will benefit immensely from locating close to the users. + +## Reflection + +### What felt easier than Kubernetes? + +- to be honest, everything felt easier + +### What felt more constrained? + +- no control over networking, no customisation, no manual resource distribution and controllable scaling. + +### What changed because Workers is not a Docker host? + +- as opposed to docker, Workers provide serverless architecture, and Workers runtime model is forced. moreover, I didn't have to keep process running locally, like with docker containers, it was nice. + diff --git a/labs/lab18/app_python/docs/screenshots/01-main-endpoint.png b/labs/lab18/app_python/docs/screenshots/01-main-endpoint.png new file mode 100644 index 0000000000..ba33e3a732 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/01-main-endpoint.png differ diff --git a/labs/lab18/app_python/docs/screenshots/02-health-check.png b/labs/lab18/app_python/docs/screenshots/02-health-check.png new file mode 100644 index 0000000000..aeb0c64170 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/02-health-check.png differ diff --git a/labs/lab18/app_python/docs/screenshots/03-formatted-output.png b/labs/lab18/app_python/docs/screenshots/03-formatted-output.png new file mode 100644 index 0000000000..1d5b985415 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/03-formatted-output.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab02-shots/build.png b/labs/lab18/app_python/docs/screenshots/lab02-shots/build.png new file mode 100644 index 0000000000..83b9fec021 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab02-shots/build.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab02-shots/pull.png b/labs/lab18/app_python/docs/screenshots/lab02-shots/pull.png new file mode 100644 index 0000000000..b5aa80a5b0 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab02-shots/pull.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab02-shots/push.png b/labs/lab18/app_python/docs/screenshots/lab02-shots/push.png new file mode 100644 index 0000000000..dc267951d2 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab02-shots/push.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab02-shots/run.png b/labs/lab18/app_python/docs/screenshots/lab02-shots/run.png new file mode 100644 index 0000000000..9cb5eb6708 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab02-shots/run.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab02-shots/test.png b/labs/lab18/app_python/docs/screenshots/lab02-shots/test.png new file mode 100644 index 0000000000..48d590cfa6 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab02-shots/test.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png b/labs/lab18/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png new file mode 100644 index 0000000000..4919561bfd Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab03-shots/images with tags docker hub.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab03-shots/improved perf.png b/labs/lab18/app_python/docs/screenshots/lab03-shots/improved perf.png new file mode 100644 index 0000000000..dffca15721 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab03-shots/improved perf.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab03-shots/pipeline success.png b/labs/lab18/app_python/docs/screenshots/lab03-shots/pipeline success.png new file mode 100644 index 0000000000..e5c638d566 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab03-shots/pipeline success.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab03-shots/unit test output.png b/labs/lab18/app_python/docs/screenshots/lab03-shots/unit test output.png new file mode 100644 index 0000000000..a3beab71dd Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab03-shots/unit test output.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi preview.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi preview.png new file mode 100644 index 0000000000..5ba4f05400 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi preview.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi ssh.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi ssh.png new file mode 100644 index 0000000000..2311bbfb38 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi ssh.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi up.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi up.png new file mode 100644 index 0000000000..54bf4d50e7 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/pulumi up.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/ssh output terraform.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/ssh output terraform.png new file mode 100644 index 0000000000..463910cf4a Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/ssh output terraform.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-1.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-1.png new file mode 100644 index 0000000000..7598bfbefe Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-1.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-2.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-2.png new file mode 100644 index 0000000000..7715fc8423 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform apply-2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform destroy.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform destroy.png new file mode 100644 index 0000000000..d9137cc00e Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform destroy.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform plan.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform plan.png new file mode 100644 index 0000000000..e73c54a786 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/terraform plan.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab04-shots/vm status.png b/labs/lab18/app_python/docs/screenshots/lab04-shots/vm status.png new file mode 100644 index 0000000000..95c51dae37 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab04-shots/vm status.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab05-shots/encrypted.png b/labs/lab18/app_python/docs/screenshots/lab05-shots/encrypted.png new file mode 100644 index 0000000000..e874cc9e27 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab05-shots/encrypted.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png new file mode 100644 index 0000000000..ce2e9eab9f Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/Check mode to see what would run.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png new file mode 100644 index 0000000000..a1e3ff8169 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/Install packages only across all roles.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png new file mode 100644 index 0000000000..4d4adec394 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/Run only docker installation tasks.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/Skip common role.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/Skip common role.png new file mode 100644 index 0000000000..0d15d7b10e Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/Skip common role.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png new file mode 100644 index 0000000000..44d8374d37 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/Test provision with only docker.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png new file mode 100644 index 0000000000..8705183b16 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/app running after clean reinstall.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab06-shots/ci:cd success.png b/labs/lab18/app_python/docs/screenshots/lab06-shots/ci:cd success.png new file mode 100644 index 0000000000..aec7e3de34 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab06-shots/ci:cd success.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana dashboard.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana dashboard.png new file mode 100644 index 0000000000..e9ea4b2f99 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana dashboard.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-app-name.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-app-name.png new file mode 100644 index 0000000000..fd72e51ce9 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-app-name.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-error.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-error.png new file mode 100644 index 0000000000..0dba1e1844 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana-error.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_get.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_get.png new file mode 100644 index 0000000000..01553d407a Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_get.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_login.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_login.png new file mode 100644 index 0000000000..65e92615cf Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_login.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png new file mode 100644 index 0000000000..2ca33c387f Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/grafana_logs_3_containers.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/healthy services.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/healthy services.png new file mode 100644 index 0000000000..efabf016f3 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/healthy services.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab07-shots/logs from the app.png b/labs/lab18/app_python/docs/screenshots/lab07-shots/logs from the app.png new file mode 100644 index 0000000000..c4048b4b63 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab07-shots/logs from the app.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard1.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard1.png new file mode 100644 index 0000000000..e94922775b Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard1.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard2.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard2.png new file mode 100644 index 0000000000..d1f8714c6f Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/dashboard2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/health statuses.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/health statuses.png new file mode 100644 index 0000000000..cf89eeb949 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/health statuses.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/metrics for app logs.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/metrics for app logs.png new file mode 100644 index 0000000000..2c6998a0e0 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/metrics for app logs.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/persistency.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/persistency.png new file mode 100644 index 0000000000..5e7459e607 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/persistency.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/prometheous targets.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/prometheous targets.png new file mode 100644 index 0000000000..6963da43ce Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/prometheous targets.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab08-shots/successful query.png b/labs/lab18/app_python/docs/screenshots/lab08-shots/successful query.png new file mode 100644 index 0000000000..bc76a157c7 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab08-shots/successful query.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab09-shots/curl app .png b/labs/lab18/app_python/docs/screenshots/lab09-shots/curl app .png new file mode 100644 index 0000000000..8ec4de04f6 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab09-shots/curl app .png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab09-shots/rollout.png b/labs/lab18/app_python/docs/screenshots/lab09-shots/rollout.png new file mode 100644 index 0000000000..866425dd76 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab09-shots/rollout.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab09-shots/scaling.png b/labs/lab18/app_python/docs/screenshots/lab09-shots/scaling.png new file mode 100644 index 0000000000..e74aaa0833 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab09-shots/scaling.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab10-shots/app-working.png b/labs/lab18/app_python/docs/screenshots/lab10-shots/app-working.png new file mode 100644 index 0000000000..47bab43c9f Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab10-shots/app-working.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab12-shots/counter.png b/labs/lab18/app_python/docs/screenshots/lab12-shots/counter.png new file mode 100644 index 0000000000..e83a4ff5b8 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab12-shots/counter.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab13-shots/application.png b/labs/lab18/app_python/docs/screenshots/lab13-shots/application.png new file mode 100644 index 0000000000..ba2f7f8e21 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab13-shots/application.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab13-shots/details.png b/labs/lab18/app_python/docs/screenshots/lab13-shots/details.png new file mode 100644 index 0000000000..a0341d0c5b Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab13-shots/details.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab13-shots/drift.png b/labs/lab18/app_python/docs/screenshots/lab13-shots/drift.png new file mode 100644 index 0000000000..c98c4fea1c Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab13-shots/drift.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab13-shots/sync status.png b/labs/lab18/app_python/docs/screenshots/lab13-shots/sync status.png new file mode 100644 index 0000000000..705de1d582 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab13-shots/sync status.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab13-shots/sync.png b/labs/lab18/app_python/docs/screenshots/lab13-shots/sync.png new file mode 100644 index 0000000000..0720e0396c Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab13-shots/sync.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png new file mode 100644 index 0000000000..93cb0f496d Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/argo-dashboard-access.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-2.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-2.png new file mode 100644 index 0000000000..0d1e97d457 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-3.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-3.png new file mode 100644 index 0000000000..bea1b39d10 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-3.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-4.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-4.png new file mode 100644 index 0000000000..196ef487ab Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-4.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-5.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-5.png new file mode 100644 index 0000000000..275e4f4796 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/bg-5.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/blue-green-1.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/blue-green-1.png new file mode 100644 index 0000000000..15936932ff Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/blue-green-1.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-abort.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-abort.png new file mode 100644 index 0000000000..dd031144fd Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-abort.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-1.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-1.png new file mode 100644 index 0000000000..73395076d0 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-1.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-2.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-2.png new file mode 100644 index 0000000000..bb11cd91cc Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-3.png b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-3.png new file mode 100644 index 0000000000..98be269459 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab14-shots/canary-prom-3.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab15-shots/pers_test.png b/labs/lab18/app_python/docs/screenshots/lab15-shots/pers_test.png new file mode 100644 index 0000000000..f142fe13fa Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab15-shots/pers_test.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab15-shots/visits-diff.png b/labs/lab18/app_python/docs/screenshots/lab15-shots/visits-diff.png new file mode 100644 index 0000000000..ddcbd746e0 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab15-shots/visits-diff.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png new file mode 100644 index 0000000000..3dd0a8efe7 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/Screenshot 2026-05-09 at 21.24.02.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/alert.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/alert.png new file mode 100644 index 0000000000..fde3f418b3 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/alert.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/cpupod2.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/cpupod2.png new file mode 100644 index 0000000000..3d2ab981f6 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/cpupod2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/curl.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/curl.png new file mode 100644 index 0000000000..94cea81937 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/curl.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/memorypod2.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/memorypod2.png new file mode 100644 index 0000000000..7e83d87b1f Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/memorypod2.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/namespace usage.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/namespace usage.png new file mode 100644 index 0000000000..3b492e8e19 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/namespace usage.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/network.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/network.png new file mode 100644 index 0000000000..ac0c61c561 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/network.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/node cpu memory.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/node cpu memory.png new file mode 100644 index 0000000000..6b67c8c7d2 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/node cpu memory.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png new file mode 100644 index 0000000000..b48bf4e190 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/pod cpu, memory.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab16-shots/pods managed.png b/labs/lab18/app_python/docs/screenshots/lab16-shots/pods managed.png new file mode 100644 index 0000000000..9920abeb46 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab16-shots/pods managed.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/dashboard.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/dashboard.png new file mode 100644 index 0000000000..46fb3d234c Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/dashboard.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/edge deployed.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/edge deployed.png new file mode 100644 index 0000000000..ddc26f2de3 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/edge deployed.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/endpoints local.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/endpoints local.png new file mode 100644 index 0000000000..b7a9612d97 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/endpoints local.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/logs.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/logs.png new file mode 100644 index 0000000000..ca44c916d5 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/logs.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/metrics.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/metrics.png new file mode 100644 index 0000000000..5646d44d46 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/metrics.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/persistance.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/persistance.png new file mode 100644 index 0000000000..d437a22939 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/persistance.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab17-shots/secrets.png b/labs/lab18/app_python/docs/screenshots/lab17-shots/secrets.png new file mode 100644 index 0000000000..a0d01bea32 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab17-shots/secrets.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/app running.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/app running.png new file mode 100644 index 0000000000..d40cc5df95 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/app running.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/curl.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/curl.png new file mode 100644 index 0000000000..d41a4b2513 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/curl.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/hash dockerfile.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/hash dockerfile.png new file mode 100644 index 0000000000..24c11150a9 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/hash dockerfile.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/history-nix.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/history-nix.png new file mode 100644 index 0000000000..a605ff3721 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/history-nix.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/history.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/history.png new file mode 100644 index 0000000000..ddd15d1646 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/history.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/platform error.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/platform error.png new file mode 100644 index 0000000000..ac1a1d19b1 Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/platform error.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/sha.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/sha.png new file mode 100644 index 0000000000..67f5d64d4b Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/sha.png differ diff --git a/labs/lab18/app_python/docs/screenshots/lab18-shots/sizeubuntu.png b/labs/lab18/app_python/docs/screenshots/lab18-shots/sizeubuntu.png new file mode 100644 index 0000000000..a374ef649b Binary files /dev/null and b/labs/lab18/app_python/docs/screenshots/lab18-shots/sizeubuntu.png differ diff --git a/labs/lab18/app_python/freeze1.txt b/labs/lab18/app_python/freeze1.txt new file mode 100644 index 0000000000..af266d21cd --- /dev/null +++ b/labs/lab18/app_python/freeze1.txt @@ -0,0 +1 @@ +Flask==3.1.3 diff --git a/labs/lab18/app_python/freeze2.txt b/labs/lab18/app_python/freeze2.txt new file mode 100644 index 0000000000..af266d21cd --- /dev/null +++ b/labs/lab18/app_python/freeze2.txt @@ -0,0 +1 @@ +Flask==3.1.3 diff --git a/labs/lab18/app_python/k8s/ARGOCD.md b/labs/lab18/app_python/k8s/ARGOCD.md new file mode 100644 index 0000000000..67c2551dda --- /dev/null +++ b/labs/lab18/app_python/k8s/ARGOCD.md @@ -0,0 +1,214 @@ +# Documentation + +## ArgoCD Setup + +### Installation verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install argocd argo/argo-cd --namespace argocd +NAME: argocd +LAST DEPLOYED: Thu Apr 23 18:37:11 2026 +NAMESPACE: argocd +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +NOTES: +In order to access the server UI you have the following options: + +1. kubectl port-forward service/argocd-server -n argocd 8080:443 + + and then open the browser on http://localhost:8080 and accept the certificate + +2. enable ingress in the values file `server.ingress.enabled` and either + - Add the annotation for ssl passthrough: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-1-ssl-passthrough + - Set the `configs.params."server.insecure"` in the values file and terminate SSL at your ingress: https://argo-cd.readthedocs.io/en/stable/operator-manual/ingress/#option-2-multiple-ingress-objects-and-hosts + + +After reaching the UI the first time you can login with username: admin and the random password generated during the installation. You can find the password by running: + +kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d + +(You should delete the initial secret afterwards as suggested by the Getting Started Guide: https://argo-cd.readthedocs.io/en/stable/getting_started/#4-login-using-the-cli) +(devops) fountainer@Veronicas-MacBook-Air app_python % helm list -n argocd +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +argocd argocd 1 2026-04-23 18:37:11.582773 +0300 MSK deployed argo-cd-9.5.4 v3.3.8 +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd version +argocd: v3.3.8+7ae7d2c.dirty + BuildDate: 2026-04-21T20:19:34Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: dirty + GitTag: v3.3.8 + GoVersion: go1.26.2 + Compiler: gc + Platform: darwin/arm64 +argocd-server: v3.3.8 + BuildDate: 2026-04-21T17:19:47Z + GitCommit: 7ae7d2cc723f5408b080a31263e705198af08613 + GitTreeState: clean + GitTag: v3.3.8 + GoVersion: go1.25.5 + Compiler: gc + Platform: linux/arm64 + Kustomize Version: v5.8.1 2026-02-09T16:15:27Z + Helm Version: v3.19.4+g7cfb6e4 + Kubectl Version: v0.34.0 + Jsonnet Version: v0.21.0 + ``` + +### UI access method + +Accessed ArgoCD UI via kubectl port-forward to localhost and logged in through the browser using the admin credentials and a previously created password. + +### CLI configuration + +Installed the argocd CLI with Homebrew and connected to the server using argocd login localhost:8080 --insecure. + +## Application Configuration + +### Application manifests + +Created an ArgoCD Application manifest defining the app name, project, source repository, and destination cluster settings. + +### Source and destination configuration + +Configured the application to pull from the Git repository (Helm chart path) and deploy to the in-cluster Kubernetes API in the default namespace. + +### Values file selection + +Specified values.yaml as the Helm values file to control application configuration during deployment. To test, I changed the number of replicas from 1 to 3. + +## Multi-Environment + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (22m ago) 22m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n prod +NAME READY STATUS RESTARTS AGE +python-app-prod-app-python-9dc9c6fbc-n8mtr 1/1 Running 0 4m14s +python-app-prod-app-python-9dc9c6fbc-sqflv 1/1 Running 0 21m +python-app-prod-app-python-9dc9c6fbc-xfvbj 1/1 Running 0 4m14s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app list +NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH SYNCPOLICY CONDITIONS REPO PATH TARGET +argocd/my-app https://kubernetes.default.svc default default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-dev https://kubernetes.default.svc dev default Synced Healthy Auto-Prune https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +argocd/python-app-prod https://kubernetes.default.svc prod default Synced Healthy Manual https://github.com/ffountainer/DevOps-Core-Course app_python/k8s/app_python lab13 +``` + +### Dev vs Prod configuration differences + +Dev uses smaller resource limits and fewer replicas for faster iteration, while prod uses higher replica count and stricter resource limits for stability and performance. + +### Sync policy differences and rationale + +Dev is configured with automated sync including self-heal and prune for continuous deployment, while prod uses manual sync to ensure controlled and reviewed releases. + +### Namespace separation + +Dev and prod are deployed into separate namespaces to isolate environments, prevent interference, and ensure independent lifecycle management. + +## Self-Healing Evidence + +### Manual scale test with before/after + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 30m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=5 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 31m +``` + +### Pod deletion test + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-g97xn 1/1 Running 1 (31m ago) 32m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl delete pod python-app-dev-app-python-59fdf484d5-g97xn -n dev +pod "python-app-dev-app-python-59fdf484d5-g97xn" deleted +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 0/1 Running 0 15s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n dev +NAME READY STATUS RESTARTS AGE +python-app-dev-app-python-59fdf484d5-249xv 1/1 Running 0 37s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Configuration drift test + +Here you can see the drift +![](./../docs/screenshots/lab13-shots/drift.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % argocd app get python-app-dev +Name: argocd/python-app-dev +Project: default +Server: https://kubernetes.default.svc +Namespace: dev +URL: https://argocd.example.com/applications/python-app-dev +Source: +- Repo: https://github.com/ffountainer/DevOps-Core-Course + Target: lab13 + Path: app_python/k8s/app_python + Helm Values: values-dev.yaml +SyncWindow: Sync Allowed +Sync Policy: Automated (Prune) +Sync Status: Synced to lab13 (27593a9) +Health Status: Healthy + +GROUP KIND NAMESPACE NAME STATUS HEALTH HOOK MESSAGE +batch Job dev python-app-dev-app-python-pre-install Succeeded PreSync Reached expected number of succeeded pods + Secret dev app-credentials Synced secret/app-credentials configured + ConfigMap dev python-app-dev-app-python-env Synced configmap/python-app-dev-app-python-env unchanged + ConfigMap dev python-app-dev-app-python-config Synced configmap/python-app-dev-app-python-config unchanged + PersistentVolumeClaim dev python-app-dev-app-python-data Synced Healthy persistentvolumeclaim/python-app-dev-app-python-data unchanged + Service dev python-app-dev-app-python-service Synced Healthy service/python-app-dev-app-python-service unchanged +apps Deployment dev python-app-dev-app-python Synced Healthy deployment.apps/python-app-dev-app-python configured +batch Job dev python-app-dev-app-python-post-install Succeeded PostSync Reached expected number of succeeded pods +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl scale deployment python-app-dev-app-python -n dev --replicas=2 +deployment.apps/python-app-dev-app-python scaled +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get deploy -n dev +NAME READY UP-TO-DATE AVAILABLE AGE +python-app-dev-app-python 1/1 1 1 52m +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl describe deployment python-app-dev-app-python -n dev | grep Replicas +Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable + Available True MinimumReplicasAvailable +``` + +### Explanation of behaviors + +- Explain when ArgoCD syncs vs when Kubernetes heals + +ArgoCD references changes in git, and Kubernetes monitors the cluster (it will heal if the pod crushes, etc) + +- What triggers ArgoCD sync? + +git repo changes, manual sync triggered, auto-sync enabled, drift detected + self-heal enabled + +- What is the sync interval? + +the default sync is every 3 minutes + +## Screenshots + +### ArgoCD UI showing the applications + +![](./../docs/screenshots/lab13-shots/application.png) + +### Sync status + +![](./../docs/screenshots/lab13-shots/sync%20status.png) +![](./../docs/screenshots/lab13-shots/sync.png) + +### Application details view + +![](./../docs/screenshots/lab13-shots/details.png) \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/CONFIGMAPS.md b/labs/lab18/app_python/k8s/CONFIGMAPS.md new file mode 100644 index 0000000000..f79a158422 --- /dev/null +++ b/labs/lab18/app_python/k8s/CONFIGMAPS.md @@ -0,0 +1,141 @@ +# Documentation + +## Application Changes + +### Description of visits counter implementation + +visits counter is a global integer that increases every time / endpoint is called. it writes the value into a file (visits) so it can survive pod restarts via pvc. + +### New endpoint documentation + +/visits returns the current stored counter value from data/visits file + +### Local testing evidence with Docker + +Here you can see the counter value persistence across restarts. +![](./../docs/screenshots/lab12-shots/counter.png) + +## ConfigMap Implementation + +### ConfigMap template structure + +Helm ConfigMap loads a local config.json file via .Files.Get, so the whole json is injected into the cluster as one config object + +### config.json content + +it contains basic app metadata like name, environment, and feature flags (debug, metrics) plus log level settings + +### How ConfigMap is mounted as file + +the ConfigMap is mounted as a volume at /config, so inside the container we can read /config/config.json as a normal file. + +### How ConfigMap provides environment variables + +by using envFrom: configMapRef to inject keys like APP_NAME, APP_ENV, and LOG_LEVEL directly as environment variables. + + +### Verification outputs + +- File content inside pod (cat /config/config.json) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- cat /config/config.json +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +}% +``` + +- Environment variables in pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep APP_ +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +APP_ENV=dev +APP_NAME=my-app +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PROTO=tcp +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_PORT=80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_SERVICE_HOST=10.103.123.105 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT=tcp://10.103.123.105:80 +MYSECRETRELEASE_APP_PYTHON_SERVICE_PORT_80_TCP_ADDR=10.103.123.105 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-696f97599c-5llgh -- printenv | grep LOG_LEVEL +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +LOG_LEVEL=info +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + +## Persistent Volume + +### PVC configuration explanation + +PVC requests a small storage size (100Mi) and creates a persistent volume that is mounted into the pod at /app/data to store the visits file. + +### Access modes and storage class discussion + +ReadWriteOnce is used because only one pod needs to write to the volume, and storageClass is optional so it uses the cluster default. + +### Volume mount configuration + +the volume is mounted into the container at /app/data, and the app writes visits file there so data stays after pod restarts/recreations + +### Persistence test evidence: + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-z6r7b -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl delete pod mysecretrelease-app-python-7b6579656c-z6r7b +pod "mysecretrelease-app-python-7b6579656c-z6r7b" deleted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7b6579656c-b2tzf 2/2 Running 0 60s +vault-0 1/1 Running 3 (84m ago) 8d +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 3 (85m ago) 8d +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec mysecretrelease-app-python-7b6579656c-b2tzf -- cat /app/data/visits +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +22% +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## ConfigMap vs Secret + +### When to use ConfigMap + +ConfigMap is used for non-sensitive configuration like app name, environment, log level, and feature flags. + +### When to use Secret + +secret is used for sensitive data like username, password, or anything that should not be visible in plain text + +### Key differences + +configMap is plain text and not encrypted, while Secret is base64 encoded and used for sensitive data. + +## Additional Outputs: + +### kubectl get configmap,pvc output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get configmap,pvc +NAME DATA AGE +configmap/kube-root-ca.crt 1 22d +configmap/mysecretrelease-app-python-config 1 7m2s +configmap/mysecretrelease-app-python-env 3 7m2s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/mysecretrelease-app-python-data Bound pvc-42d4685f-8463-4434-8959-0bacd5d972b6 100Mi RWO standard 7m2s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + + diff --git a/labs/lab18/app_python/k8s/HELM.md b/labs/lab18/app_python/k8s/HELM.md new file mode 100644 index 0000000000..e3f80de3ab --- /dev/null +++ b/labs/lab18/app_python/k8s/HELM.md @@ -0,0 +1,499 @@ +# Documentation + +## Chart Overview + +### Chart structure explanation + +- the chart has templates/ for kubectl manifests, values.yaml for default settings, and _helpers.tpl for reusable template functions. Hooks are in templates/hooks/ for pre and post-install jobs + +### Key template files and their purpose + +- deployment.yaml defines the app pods and containers +- service.yaml exposes them, and hooks run tasks before or after install +- helpers in _helpers.tpl build names, labels, and selectors consistently, and then can be reused in different config files + +### Values organization strategy + +- values.yaml has defaults, while values-dev.yaml and values-prod.yaml override settings for different environments. +This keeps environment configs separate and easy to manage. + +## Configuration Guide + +### Important values and their purpose + +- replicaCount sets pod number, resources manage CPU/memory, service.type controls what role will the service have (NodePort vs LoadBalancer, etc). LivenessProbe and readinessProbe check pod health. + +### How to customize for different environments + +- you can use values-dev.yaml for dev with 1 replica and NodePort, values-prod.yaml for prod with more replicas and LoadBalancer. and you can also override values on install with --set + +### Example installations with different configurations + +- dev + +```bash +helm install myapp-dev k8s/app_python -f k8s/app_python/values-dev.yaml +``` + +- prod + +```bash +helm install myapp-prod k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +## Hook Implementation + +### What hooks you implemented and why + +- I implemented a pre-install job for setup tasks and a post-install job for validation, to simulate real lifecycle tasks + +### Hook execution order and weights + +- Pre-install has weight -5 to run early, post-install has weight 5 to run after deployment. + +### Deletion policies explanation + +- both hooks have hook-succeeded policy so they auto-delete after finishing successfully + +## Installation Evidence + +### Terminal output showing Helm installation and version (should be 4.x) + +```bash +==> Fetching downloads for: helm +✔︎ Bottle Manifest helm (4.1.3) Downloaded 7.4KB/ 7.4KB +✔︎ Bottle helm (4.1.3) Downloaded 18.1MB/ 18.1MB +==> Pouring helm--4.1.3.arm64_tahoe.bottle.tar.gz +🍺 /opt/homebrew/Cellar/helm/4.1.3: 69 files, 61.3MB +==> Running `brew cleanup helm`... +Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`. +Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). +==> Caveats +zsh completions have been installed to: + /opt/homebrew/share/zsh/site-functions +``` +### Output of exploring a public chart (e.g., helm show chart prometheus-community/prometheus) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm show chart prometheus-community/prometheus +annotations: + artifacthub.io/license: Apache-2.0 + artifacthub.io/links: | + - name: Chart Source + url: https://github.com/prometheus-community/helm-charts + - name: Upstream Project + url: https://github.com/prometheus/prometheus +apiVersion: v2 +appVersion: v3.11.0 +dependencies: +- condition: alertmanager.enabled + name: alertmanager + repository: https://prometheus-community.github.io/helm-charts + version: 1.34.* +- condition: kube-state-metrics.enabled + name: kube-state-metrics + repository: https://prometheus-community.github.io/helm-charts + version: 7.2.* +- condition: prometheus-node-exporter.enabled + name: prometheus-node-exporter + repository: https://prometheus-community.github.io/helm-charts + version: 4.52.* +- condition: prometheus-pushgateway.enabled + name: prometheus-pushgateway + repository: https://prometheus-community.github.io/helm-charts + version: 3.6.* +description: Prometheus is a monitoring system and time series database. +home: https://prometheus.io/ +icon: https://raw.githubusercontent.com/prometheus/prometheus.github.io/master/assets/prometheus_logo-cb55bb5c346.png +keywords: +- monitoring +- prometheus +kubeVersion: '>=1.19.0-0' +maintainers: +- email: gianrubio@gmail.com + name: gianrubio + url: https://github.com/gianrubio +- email: zanhsieh@gmail.com + name: zanhsieh + url: https://github.com/zanhsieh +- email: miroslav.hadzhiev@gmail.com + name: Xtigyro + url: https://github.com/Xtigyro +- email: naseem@transit.app + name: naseemkullah + url: https://github.com/naseemkullah +- email: rootsandtrees@posteo.de + name: zeritti + url: https://github.com/zeritti +name: prometheus +sources: +- https://github.com/prometheus/alertmanager +- https://github.com/prometheus/prometheus +- https://github.com/prometheus/pushgateway +- https://github.com/prometheus/node_exporter +- https://github.com/kubernetes/kube-state-metrics +type: application +version: 28.15.0 +``` + +### helm list output + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm list +NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION +my-python-app-dev default 1 2026-04-02 22:00:55.999506 +0300 MSK deployed app_python-0.1.0 1.0 +my-python-app-prod default 1 2026-04-02 22:12:24.157572 +0300 MSK deployed app_python-0.1.0 1.0 +myrelease default 1 2026-04-02 22:40:56.562009 +0300 MSK deployed app_python-0.1.0 1.0 +``` + +### kubectl get all showing deployed resources + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 43m +pod/my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 31m +pod/my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 31m +pod/myrelease-app-python-569fb4b645-6v9dt 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-8ws5n 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-glt5r 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-qtg4j 1/1 Running 0 2m32s +pod/myrelease-app-python-569fb4b645-rgppk 1/1 Running 0 2m32s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 8d +service/my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 43m +service/my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 31m +service/myrelease-app-python-service NodePort 10.107.17.3 80:30009/TCP 2m32s + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-python-app-dev-app-python 1/1 1 1 43m +deployment.apps/my-python-app-prod-app-python 0/5 5 0 31m +deployment.apps/myrelease-app-python 5/5 5 5 2m32s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-python-app-dev-app-python-7d7f699d85 1 1 1 43m +replicaset.apps/my-python-app-prod-app-python-74c5b97dd5 5 5 0 31m +replicaset.apps/myrelease-app-python-569fb4b645 5 5 5 2m32s +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Hook execution output (kubectl get jobs) + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get jobs -w +NAME STATUS COMPLETIONS DURATION AGE +myrelease-app-python-pre-install Running 0/1 0s +myrelease-app-python-pre-install Running 0/1 0s 0s +myrelease-app-python-pre-install Running 0/1 33s 33s +myrelease-app-python-pre-install Running 0/1 43s 43s +myrelease-app-python-pre-install SuccessCriteriaMet 0/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-pre-install Complete 1/1 44s 44s +myrelease-app-python-post-install Running 0/1 0s +myrelease-app-python-post-install Running 0/1 0s 0s +myrelease-app-python-post-install Running 0/1 5s 5s +myrelease-app-python-post-install Running 0/1 15s 15s +myrelease-app-python-post-install SuccessCriteriaMet 0/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +myrelease-app-python-post-install Complete 1/1 16s 16s +``` + +### Different environment deployments (dev vs prod) + +- Dev + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-dev k8s/app_python -f k8s/app_python/values-dev.yaml +NAME: my-python-app-dev +LAST DEPLOYED: Thu Apr 2 22:00:55 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 2m20s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 2m27s +``` + +- Prod + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install my-python-app-prod k8s/app_python -f k8s/app_python/values-prod.yaml +NAME: my-python-app-prod +LAST DEPLOYED: Thu Apr 2 22:12:24 2026 +NAMESPACE: default +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod +NAME READY STATUS RESTARTS AGE +my-python-app-dev-app-python-7d7f699d85-kklth 1/1 Running 0 12m +my-python-app-prod-app-python-74c5b97dd5-4mjjr 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-6pm7l 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-75dvc 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-9v58s 0/1 Running 0 38s +my-python-app-prod-app-python-74c5b97dd5-xktsb 0/1 Running 0 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get svc +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 7d23h +my-python-app-dev-app-python-service NodePort 10.104.238.26 80:30007/TCP 12m +my-python-app-prod-app-python-service LoadBalancer 10.101.156.227 80:30008/TCP 49s +``` + + +## Operations + +### Installation commands used + +```bash +helm install name-of-new-release k8s/app_python -f k8s/app_python/values-for-new-release.yaml +``` +### How to upgrade a release + +```bash +helm upgrade myrelease k8s/app_python -f k8s/app_python/values-prod.yaml +``` + +### How to rollback + +```bash +helm rollback myrelease 1 +``` + +### How to uninstall + +```bash +helm uninstall name-of-release +``` + +## Testing & Validation + +### helm lint output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % helm lint app_python +==> Linting app_python +[INFO] Chart.yaml: icon is recommended + +1 chart(s) linted, 0 chart(s) failed +``` +### helm template verification + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm template app-python k8s/app_python +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: app-python-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: app-python-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: app-python + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 +``` + +### Dry-run output + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm install --dry-run --debug test-release k8s/app_python +level=WARN msg="--dry-run is deprecated and should be replaced with '--dry-run=client'" +level=DEBUG msg="Original chart version" version="" +level=DEBUG msg="Chart path" path=/Users/fountainer/uni/devops/DevOps-Core-Course/app_python/k8s/app_python +level=DEBUG msg="number of dependencies in the chart" chart=app_python dependencies=0 +NAME: test-release +LAST DEPLOYED: Thu Apr 2 21:46:17 2026 +NAMESPACE: default +STATUS: pending-install +REVISION: 1 +DESCRIPTION: Dry run complete +TEST SUITE: None +USER-SUPPLIED VALUES: +{} + +COMPUTED VALUES: +container: + port: 12345 +image: + pullPolicy: IfNotPresent + repository: fountainer/my-app + tag: 2026.03.26 +livenessProbe: + failureThreshold: 5 + initialDelaySeconds: 10 + path: /health + periodSeconds: 10 + timeoutSeconds: 5 +readinessProbe: + initialDelaySeconds: 5 + path: /ready + periodSeconds: 5 +replicaCount: 5 +resources: + limits: + cpu: 500m + memory: 256Mi + requests: + cpu: 100m + memory: 128Mi +service: + nodePort: 30007 + port: 80 + protocol: TCP + targetPort: 12345 + type: NodePort +strategy: + maxSurge: 1 + maxUnavailable: 1 + +HOOKS: +MANIFEST: +--- +# Source: app_python/templates/service.yaml +apiVersion: v1 +kind: Service +metadata: + name: test-release-app-python-service +spec: + type: NodePort + selector: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + ports: + - protocol: TCP + port: 80 + targetPort: 12345 + nodePort: 30007 +--- +# Source: app_python/templates/deployment.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-release-app-python + labels: + helm.sh/chart: app_python-0.1.0 + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + app.kubernetes.io/version: "1.0" + app.kubernetes.io/managed-by: Helm +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + template: + metadata: + labels: + app.kubernetes.io/name: app-python + app.kubernetes.io/instance: test-release + spec: + containers: + - name: app-python + image: "fountainer/my-app:2026.03.26" + imagePullPolicy: IfNotPresent + ports: + - containerPort: 12345 + resources: + requests: + cpu: 100m + memory: 128Mi + limits: + cpu: 500m + memory: 256Mi + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 + +``` +### Application accessibility verification + +![](./../docs/screenshots/lab10-shots/app-working.png) \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/MONITORING.md b/labs/lab18/app_python/k8s/MONITORING.md new file mode 100644 index 0000000000..b71aebd57e --- /dev/null +++ b/labs/lab18/app_python/k8s/MONITORING.md @@ -0,0 +1,227 @@ +# Documentation + +## Stack Components + +### Descriptions in your own words + +- Prometheus Operator: it's a kubernates tool that allows you to automate prometheus deployment and management. it provides a set of custom resource definitions, and you can make your own configuration with those. + +- Prometheus: it's a tool for monitoring and alerting, it stores metrics as a series of timestampts. + +- Alertmanager: an instruments to manage alerts. when metrics reach invalid state, alertmanager will receive alerts, group and send them to asignees. you can configure when to silence alerts or start reaching out to the next person if the first one is not responding (it's an escalation), and create other custom settings. + +- Grafana: it is a dashboard for tracking the current state of the system by visualising logs and metrics. you can define alert rules there to see if the new metric value is out of the valid tresholds. + +- kube-state-metrics: it's a service that exposes metrics related to kubernates objects, they are created automatically and describe your pods current state. + +- node-exporter: it's an agent that exposes internal metrics for a node (like cpu, memory, etc), then prometheus can scrape those metrics. + +## Installation Evidence + +### kubectl get po,svc -n monitoring + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo add prometheus-community https://prometheus-community.github.io/helm-charts +"prometheus-community" already exists with the same configuration, skipping +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm repo update +Hang tight while we grab the latest from your chart repositories... +...Successfully got an update from the "hashicorp" chart repository +...Successfully got an update from the "argo" chart repository +...Successfully got an update from the "prometheus-community" chart repository +Update Complete. ⎈Happy Helming!⎈ +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get po,svc -n monitoring +NAME READY STATUS RESTARTS AGE +pod/alertmanager-monitoring-kube-prometheus-alertmanager-0 2/2 Running 0 2m34s +pod/monitoring-grafana-bbc5c674-8cbd9 3/3 Running 0 2m56s +pod/monitoring-kube-prometheus-operator-54f68d65b4-99ck2 1/1 Running 0 2m56s +pod/monitoring-kube-state-metrics-5957bd45bc-5rpqr 1/1 Running 0 2m56s +pod/monitoring-prometheus-node-exporter-c8fg6 1/1 Running 0 2m57s +pod/prometheus-monitoring-kube-prometheus-prometheus-0 2/2 Running 0 2m34s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/alertmanager-operated ClusterIP None 9093/TCP,9094/TCP,9094/UDP 2m34s +service/monitoring-grafana ClusterIP 10.110.156.182 80/TCP 2m57s +service/monitoring-kube-prometheus-alertmanager ClusterIP 10.111.243.229 9093/TCP,8080/TCP 2m57s +service/monitoring-kube-prometheus-operator ClusterIP 10.99.16.80 443/TCP 2m57s +service/monitoring-kube-prometheus-prometheus ClusterIP 10.106.17.206 9090/TCP,8080/TCP 2m57s +service/monitoring-kube-state-metrics ClusterIP 10.102.26.186 8080/TCP 2m57s +service/monitoring-prometheus-node-exporter ClusterIP 10.100.205.92 9100/TCP 2m57s +service/prometheus-operated ClusterIP None 9090/TCP 2m34s +``` + +## Dashboard Answers + +### Pod Resources: CPU/memory usage of your StatefulSet + +Due to the pods and the app itself being very lightweight, CPU and memory usage never went higher than initially allocated resources (100m CPU and 128Mi memory). Even under high load (I used multiple loops with curl), the initial resources were enough. You will see more detailed usages info in the next question. + +Example for pod 2: + +![](./../docs/screenshots/lab16-shots/cpupod2.png) + +![](./../docs/screenshots/lab16-shots/memorypod2.png) + +### Namespace Analysis: Which pods use most/least CPU in default namespace? + +I decided to use Prometheus for evidence, since the resource usage was really low, and didn't show up properly in Grafana. + +curl I used (the first count is much bigger since I previously tested only with pod 2) + +![](./../docs/screenshots/lab16-shots/curl.png) + +usage + +![](./../docs/screenshots/lab16-shots/namespace%20usage.png) + +As we can see, all statefulset pods used roughly the same amount of CPU and memory resources. This is anticipated, because load balancing is used for routing traffic to different pods. + +### Node Metrics: Memory usage (% and MB), CPU cores + +CPU and Memory usage for the whole minikube node was: + +![](./../docs/screenshots/lab16-shots/node%20cpu%20memory.png) + +It is much higher than resources used in statefulset since the node contains all different namespaces in my minikube cluster. + +### Kubelet: How many pods/containers managed? + +16 pods and 41 containers + +![](./../docs/screenshots/lab16-shots/pods%20managed.png) + +### Network: Traffic for pods in default namespace + +![](./../docs/screenshots/lab16-shots/network.png) + +### Alerts: How many active alerts? Check Alertmanager UI + +![](./../docs/screenshots/lab16-shots/alert.png) + +## Init Containers: Implementation and proof of success + +I implemented two init container patterns. First one downloads a file with wget into a shared emptyDir volume and the main container successfully accessed it from /data/index.html. Second one uses a wait-for-service init container that continuously checks the nginx service with wget and only starts the main container after the service becomes reachable. + +### Init container + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 Init:0/1 0 2s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 0/1 PodInitializing 0 4s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 5s +myapp-app-python-0 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-1 1/1 Running 3 (6h15m ago) 7d20h +myapp-app-python-2 1/1 Running 3 (6h15m ago) 7d20h +``` +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl logs init-download-pod -c init-download +Connecting to example.com (172.66.147.243:443) +wget: note: TLS certificate validation not implemented +saving to '/work-dir/index.html' +index.html 100% |********************************| 528 0:00:00 ETA +'/work-dir/index.html' saved +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec init-download-pod -- cat /data/index.html +Defaulted container "main-app" out of: main-app, init-download (init) +Example Domain

Example Domain

This domain is for use in documentation examples without needing permission. Avoid use in operations.

Learn more

+``` + +### Waiting for service container + +```bash +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f waiting.yaml +pod/wait-service-pod created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +wait-service-pod 0/1 Init:0/1 0 22s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl logs wait-service-pod -c wait-for-service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +wget: bad address 'myservice.default.svc.cluster.local' +waiting for service +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl apply -f nginx.yaml +deployment.apps/myservice created +service/myservice created +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 3s +wait-service-pod 0/1 Init:0/1 0 55s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 5s +wait-service-pod 0/1 PodInitializing 0 57s +(devops) fountainer@Veronicas-MacBook-Air templates % kubectl get pod +NAME READY STATUS RESTARTS AGE +init-download-pod 1/1 Running 0 51m +myapp-app-python-0 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-1 1/1 Running 3 (7h6m ago) 7d21h +myapp-app-python-2 1/1 Running 3 (7h6m ago) 7d21h +myservice-ffc6675d7-pmjfr 1/1 Running 0 7s +wait-service-pod 1/1 Running 0 59s +``` + +After service was discovered: + +```bash +waiting for service + + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, nginx is successfully installed and working. +Further configuration is required for the web server, reverse proxy, +API gateway, load balancer, content cache, or other features.

+ +

For online documentation and support please refer to +nginx.org.
+To engage with the community please visit +community.nginx.org.
+For enterprise grade support, professional services, additional +security features and capabilities please refer to +f5.com/nginx.

+ +

Thank you for using nginx.

+ + +``` \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/README.md b/labs/lab18/app_python/k8s/README.md new file mode 100644 index 0000000000..20ea3e883d --- /dev/null +++ b/labs/lab18/app_python/k8s/README.md @@ -0,0 +1,301 @@ +# Documentation + +## Architecture Overview + +### Diagram or description of your deployment architecture + +```mermaid +flowchart LR + +subgraph Kubernetes_Cluster["Minikube Kubernetes Cluster"] + + S[Service node port
port 80 -> nodePort 30007] + + subgraph Deployment["Deployment: my-app (5 replicas)"] + P1[Pod 1
app-python] + P2[Pod 2
app-python] + P3[Pod 3
app-python] + P4[Pod 4
app-python] + P5[Pod 5
app-python] + end + +end + +User[User / curl / browser] + +User -->|HTTP request
http://nodeIP:30007| S + +S -->|routes traffic via selector
app: my-app| P1 +S --> P2 +S --> P3 +S --> P4 +S --> P5 + +P1 -->|/health /ready /metrics| AppLogic[(Flask App)] +P2 --> AppLogic +P3 --> AppLogic +P4 --> AppLogic +P5 --> AppLogic + +``` + +### How many Pods, which Services, networking flow + +- I used 5 pods managed by a deployment and one NodePort service, where traffic goes from the service (port 80 / nodePort 30007) to the pods using the app: my-app label selector. + +### Resource allocation strategy + +- I defined small cpu and memory requests/limits (100m–500m cpu, 128Mi–256Mi memory) to keep the app stable and prevent it from using too many cluster resources. + +### Brief explanation of your chosen tool (minikube/kind) and why + +I used minikube because it’s easy to set up locally and lets me run a full kubernetes cluster on my machine, which is enough for testing deployments without needing a real cloud setup. + +## Manifest Files + +### Brief description of each manifest + +- Deployment: deployment.yml defines how my app runs in Kubernetes, including how many pods, which image to use, and how they are configured. + +- Service: service.yml exposes the app inside and outside the cluster by routing traffic to the pods created by the deployment. + +### Key configuration choices + +- Deployment: I set 3 replicas, added resource limits/requests, configured liveness and readiness probes, used labels for selection, and set a rolling update strategy + +- Service: I used NodePort type, matched the selector with app: my-app, set port 80 as the service port, and mapped it to container port 12345 with a fixed nodePort. + +### Why you chose specific values (replicas, resources, etc.) + +- Deployment: I used 3 replicas for basic availability, small cpu/memory values since the app is lightweight, and probes to make sure kubernetes can detect when the app is healthy and ready to serve traffic. + +- Service: I used NodePort so i can access the app locally with minikube, port 80 for convenience, targetPort 12345 to match the app, and a fixed nodePort (30007) to make testing easier. + +## Deployment Evidence + +### Successful cluster setup + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % minikube start + +😄 minikube v1.38.1 on Darwin 26.3 (arm64) +✨ Using the docker driver based on existing profile +👍 Starting "minikube" primary control-plane node in "minikube" cluster +🚜 Pulling base image v0.0.50 ... +🏃 Updating the running docker "minikube" container ... +🐳 Preparing Kubernetes v1.35.1 on Docker 29.2.1 ... +🔎 Verifying Kubernetes components... + ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5 +🌟 Enabled addons: default-storageclass, storage-provisioner + +❗ /Applications/Docker.app/Contents/Resources/bin/kubectl is version 1.32.2, which may have incompatibilities with Kubernetes 1.35.1. + ▪ Want kubectl v1.35.1? Try 'minikube kubectl -- get pods -A' +🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default +``` +### Output of kubectl cluster-info and kubectl get nodes + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl cluster-info +Kubernetes control plane is running at https://127.0.0.1:51390 +CoreDNS is running at https://127.0.0.1:51390/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy + +To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get nodes +NAME STATUS ROLES AGE VERSION +minikube Ready control-plane 6h45m v1.35.1 +``` + +### kubectl get all output + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get all +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m11s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m11s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h58m + +NAME READY UP-TO-DATE AVAILABLE AGE +deployment.apps/my-app-deployment 3/3 3 3 3m11s + +NAME DESIRED CURRENT READY AGE +replicaset.apps/my-app-deployment-6f67848dfb 3 3 3 3m11s +``` +### kubectl get pods,svc with detailed view + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods,svc +NAME READY STATUS RESTARTS AGE +pod/my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 3m35s +pod/my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 3m35s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 6h59m +``` + +### kubectl describe deployment showing replicas and strategy + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-6f67848dfb-kxbtv 1/1 Running 0 29s +my-app-deployment-6f67848dfb-mjq8x 1/1 Running 0 29s +my-app-deployment-6f67848dfb-vx95p 1/1 Running 0 29s +``` +### Screenshot or curl output showing app working + +![](./../docs/screenshots/lab09-shots/curl%20app%20.png) + +### Service deployment + +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 38m +my-app-service NodePort 10.98.179.244 80:30007/TCP 41s +(devops) fountainer@Veronicas-MacBook-Air k8s % minikube service my-app-service +┌───────────┬────────────────┬─────────────┬───────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼───────────────────────────┤ +│ default │ my-app-service │ 80 │ http://192.168.49.2:30007 │ +└───────────┴────────────────┴─────────────┴───────────────────────────┘ +🔗 Starting tunnel for service my-app-service. +┌───────────┬────────────────┬─────────────┬────────────────────────┐ +│ NAMESPACE │ NAME │ TARGET PORT │ URL │ +├───────────┼────────────────┼─────────────┼────────────────────────┤ +│ default │ my-app-service │ │ http://127.0.0.1:57348 │ +└───────────┴────────────────┴─────────────┴────────────────────────┘ +🎉 Opening service default/my-app-service in default browser... +❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it. +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get services +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +kubernetes ClusterIP 10.96.0.1 443/TCP 42m +my-app-service NodePort 10.98.179.244 80:30007/TCP 4m16s +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl describe service my-app-service +Name: my-app-service +Namespace: default +Labels: +Annotations: +Selector: app=my-app +Type: NodePort +IP Family Policy: SingleStack +IP Families: IPv4 +IP: 10.98.179.244 +IPs: 10.98.179.244 +Port: 80/TCP +TargetPort: 12345/TCP +NodePort: 30007/TCP +Endpoints: 10.244.0.16:12345,10.244.0.14:12345,10.244.0.15:12345 +Session Affinity: None +External Traffic Policy: Cluster +Internal Traffic Policy: Cluster +Events: +(devops) fountainer@Veronicas-MacBook-Air k8s % kubectl get endpoints +Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice +NAME ENDPOINTS AGE +kubernetes 192.168.49.2:8443 42m +my-app-service 10.244.0.14:12345,10.244.0.15:12345,10.244.0.16:12345 4m36s +(devops) fountainer@Veronicas-MacBook-Air k8s % +``` + +## Operations Performed + +### Commands used to deploy + +- ```bash kubectl apply -f k8s/deployment.yml``` +- ```bash kubectl apply -f k8s/service.yml``` +- ```bash kubectl get pods``` +- ```bash kubectl get services ``` + +### Scaling demonstration output + +![](./../docs/screenshots/lab09-shots/scaling.png) + +### Rolling update demonstration output + +I changed ```bash image: fountainer/my-app:latest``` to ```bash image: fountainer/my-app:2026.03.26```. + +![](./../docs/screenshots/lab09-shots/rollout.png) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout history deployment/my-app-deployment +deployment.apps/my-app-deployment +REVISION CHANGE-CAUSE +1 +2 +3 + +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout undo deployment/my-app-deployment +deployment.apps/my-app-deployment rolled back +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl rollout status deployment/my-app-deployment +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 2 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 3 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 4 out of 5 new replicas have been updated... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 1 old replicas are pending termination... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +Waiting for deployment "my-app-deployment" rollout to finish: 4 of 5 updated replicas are available... +deployment "my-app-deployment" successfully rolled out +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-deployment-7b5479788b-bj4pt 1/1 Running 0 37s +my-app-deployment-7b5479788b-n2pnb 1/1 Running 0 12s +my-app-deployment-7b5479788b-nvzdr 1/1 Running 0 22s +my-app-deployment-7b5479788b-q9g66 1/1 Running 0 36s +my-app-deployment-7b5479788b-w2nc6 1/1 Running 0 22s +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % +``` + +### Service access method and verification + +I accessed the app using ```bash minikube service my-app-service ``` and verified it by sending requests with curl to endpoints like /health and /ready. + +## Production Considerations + +### What health checks did you implement and why? + +I implemented a liveness probe on /health to restart unhealthy containers and a readiness probe on /ready to ensure pods start receiving traffic only when they are ready to work. + +### Resource limits rationale + +- I set limits to prevent resource overuse, and requests to guarantee the pod gets enough cpu and memory to run reliably. + +### How would you improve this for production? + +- I would add proper logging/monitoring like we did in the previous labs, add autoscaling, consider other update strategies (like canary update). + +### Monitoring and observability strategy + +- In previous labs we used Prometheus for metrics and loki & promtail for logs, also Grafana for dashboard representation + +## Challenges & Solutions + +### Issues encountered + +- I didn't work with NodePort before so I has to stydy it a little bit. Also I didn't know about minikube. + +### How you debugged (logs, describe, events) + +- I researched StackOverflow and other sources, such as documentation for kubernetes and minikube + +### What you learned about Kubernetes + +- I studied kubernetes in the SRE course last semester so I was already pretty familiar with it. We didn't use NodePort service though, and also didn't set up our own cluster since the course team provided us with it. + diff --git a/labs/lab18/app_python/k8s/ROLLOUTS.md b/labs/lab18/app_python/k8s/ROLLOUTS.md new file mode 100644 index 0000000000..7a37244b4c --- /dev/null +++ b/labs/lab18/app_python/k8s/ROLLOUTS.md @@ -0,0 +1,252 @@ +# Documentation + +## Argo Rollouts Setup + +### Installation verification + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl apply -n argo-rollouts -f https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml +customresourcedefinition.apiextensions.k8s.io/analysisruns.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/analysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/clusteranalysistemplates.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/experiments.argoproj.io created +customresourcedefinition.apiextensions.k8s.io/rollouts.argoproj.io created +serviceaccount/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-admin created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-edit created +clusterrole.rbac.authorization.k8s.io/argo-rollouts-aggregate-to-view created +clusterrolebinding.rbac.authorization.k8s.io/argo-rollouts created +configmap/argo-rollouts-config created +secret/argo-rollouts-notification-secret created +service/argo-rollouts-metrics created +deployment.apps/argo-rollouts created +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 54s +``` +```bash +==> Fetching downloads for: kubectl-argo-rollouts +✔︎ Formula kubectl-argo-rollouts (v1.8.3) Verified 130.1MB/130.1MB +==> Installing kubectl-argo-rollouts from argoproj/tap +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl argo rollouts version +kubectl-argo-rollouts: v1.8.3+49fa151 + BuildDate: 2025-06-04T22:19:21Z + GitCommit: 49fa1516cf71672b69e265267da4e1d16e1fe114 + GitTreeState: clean + GoVersion: go1.23.9 + Compiler: gc + Platform: darwin/amd64 +``` + +### Dashboard access + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pods -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 12m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 28s +``` + +![](./../docs/screenshots/lab14-shots/argo-dashboard-access.png) + +### Understand Rollout vs Deployment + +Rollout CRD vs Deployment + +- Rollout and Deployment are kinda similar and both have replicas, selector, template, strategy fields, they manage pod creation. But rollout has additional fields for strategy that allow to perform more controllable rollouts with specific configurations, like rolling an update for a group of users, not for all. + +Additional fields for progressive delivery + +- canary: allows gradual traffic shifting to a new version using steps (e.g., setWeight, pause) +- blueGreen: supports switching between old and new versions using separate services +- steps: defines staged rollout progression +- analysis: integrates automated checks (metrics, tests) during rollout +- pause: enables manual or timed pauses between steps +- trafficRouting: controls how traffic is split between versions (with ingress/service mesh) + + +## Canary Deployment + +### Strategy configuration explained + +The rollout uses a canary strategy to gradually shift traffic from the old version to the new one. It is configured in steps (20%, 40%, 60%, 80%, 100%) with pauses to allow validation and manual control. This approach reduces risk by exposing the new version to a small part of users before full deployment. + +### Step-by-step rollout progression (screenshots from dashboard) + +![](./../docs/screenshots/lab14-shots/canary-prom-1.png) +![](./../docs/screenshots/lab14-shots/canary-prom-2.png) +![](./../docs/screenshots/lab14-shots/canary-prom-3.png) + +### Promotion and abort demonstration + +Promotion (screenshots can be seen in the prev step) + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ॥ Paused +Message: CanaryPauseStep +Strategy: Canary + Step: 1/9 + SetWeight: 20 + ActualWeight: 25 +Images: fountainer/my-app:16-04 (canary, stable) +Replicas: + Desired: 3 + Current: 4 + Updated: 1 + Ready: 4 + Available: 4 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ॥ Paused 17m +├──# revision:2 +│ └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 69s canary +│ └──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 68s ready:1/1 +└──# revision:1 + └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet ✔ Healthy 17m stable + ├──□ myapp-app-python-5bc87cfdf6-2tzkc Pod ✔ Running 17m ready:1/1 + ├──□ myapp-app-python-5bc87cfdf6-bnpd6 Pod ✔ Running 17m ready:1/1 + └──□ myapp-app-python-5bc87cfdf6-qfg9s Pod ✔ Running 17m ready:1/1 +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts promote myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' promoted +``` + +Abort + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get rollouts -n argo-rollouts +NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE +myapp-app-python 3 4 1 4 31m +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts abort myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' aborted +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts get rollout myapp-app-python -n argo-rollouts +Name: myapp-app-python +Namespace: argo-rollouts +Status: ✖ Degraded +Message: RolloutAborted: Rollout aborted update to revision 3 +Strategy: Canary + Step: 0/9 + SetWeight: 0 + ActualWeight: 0 +Images: fountainer/my-app:16-04 (stable) +Replicas: + Desired: 3 + Current: 3 + Updated: 0 + Ready: 3 + Available: 3 + +NAME KIND STATUS AGE INFO +⟳ myapp-app-python Rollout ✖ Degraded 32m +├──# revision:3 +│ └──⧉ myapp-app-python-5bc87cfdf6 ReplicaSet • ScaledDown 32m canary +└──# revision:2 + └──⧉ myapp-app-python-76b59b6c66 ReplicaSet ✔ Healthy 16m stable + ├──□ myapp-app-python-76b59b6c66-pgtgq Pod ✔ Running 16m ready:1/1 + ├──□ myapp-app-python-76b59b6c66-7cwr4 Pod ✔ Running 10m ready:1/1 + └──□ myapp-app-python-76b59b6c66-skfdd Pod ✔ Running 10m ready:1/1 +``` +![](./../docs/screenshots/lab14-shots/canary-abort.png) + +## Blue-Green Deployment + +### Strategy configuration explained + +The blue-green strategy uses two environments: active and preview. The preview service runs the new version while the active service continues serving production traffic. After testing, the active service is switched to the new version instantly when promoted. This allows safe testing before release and quick rollback if needed. + +### Preview vs active service + +The active service is used by users in production and always points to the stable version. The preview service is used to test the new version before it is promoted. This separation ensures the new version can be verified without affecting real users. + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-preview 8081:80 -n argo-rollouts +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl port-forward svc/myapp-app-python-service 8080:80 -n argo-rollouts +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +``` + +### Promotion process + +Promotion + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % helm upgrade --install myapp . -n argo-rollouts +Release "myapp" has been upgraded. Happy Helming! +NAME: myapp +LAST DEPLOYED: Thu Apr 30 23:12:57 2026 +NAMESPACE: argo-rollouts +STATUS: deployed +REVISION: 9 +DESCRIPTION: Upgrade complete +TEST SUITE: None +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods -n argo-rollouts +kubectl get svc -n argo-rollouts +NAME READY STATUS RESTARTS AGE +argo-rollouts-5f64f8d68-zxx5z 1/1 Running 0 6h24m +argo-rollouts-dashboard-755bbc64c-pnkl6 1/1 Running 0 6h12m +myapp-app-python-76b59b6c66-7cwr4 1/1 Running 0 37m +myapp-app-python-76b59b6c66-pgtgq 1/1 Running 0 43m +myapp-app-python-76b59b6c66-skfdd 1/1 Running 0 37m +myapp-app-python-f7cddd7c7-5nvtx 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-xng4z 1/1 Running 0 12m +myapp-app-python-f7cddd7c7-zjfpv 1/1 Running 0 12m +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +argo-rollouts-dashboard ClusterIP 10.106.240.192 3100/TCP 6h12m +argo-rollouts-metrics ClusterIP 10.109.176.51 8090/TCP 6h24m +myapp-app-python-preview ClusterIP 10.97.144.248 80/TCP 16m +myapp-app-python-service NodePort 10.101.217.107 80:30009/TCP 59m +``` + +![](./../docs/screenshots/lab14-shots/blue-green-1.png) +![](./../docs/screenshots/lab14-shots/bg-2.png) +![](./../docs/screenshots/lab14-shots/bg-4.png) + +Rollback + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl argo rollouts undo myapp-app-python -n argo-rollouts +rollout 'myapp-app-python' undo +``` +![](./../docs/screenshots/lab14-shots/bg-5.png) + + +## Strategy Comparison + +### When to use canary vs blue-green + +canary is used when you want to slowly roll out changes to users and reduce risk step by step. blue-green is used when you want an instant switch between versions after testing + +### Pros and cons of each + +- canary is safer for production because it exposes changes gradually, but it takes longer and is more complex to monitor + +- blue-green is faster and simpler at switch time, but requires double resources and has less gradual control. + +### Your recommendation for different scenarios + +use canary for production systems where stability is critical. use blue-green for fast releases or when you want quick testing and instant rollback. + +## CLI Commands Reference + +### Commands you used + +```kubectl argo rollouts get rollout -w``` is used to watch rollout progress. ```kubectl argo rollouts promote``` is used to move to the next step in canary or switch in blue-green. ```kubectl argo rollouts undo``` is used to rollback to the previous version. + +### Monitoring and troubleshooting + +```kubectl get pods```, ```kubectl get svc```, and ```kubectl describe rollout``` are used to check cluster state and debug issues. dashboard is used to visually monitor rollout progress and traffic changes. \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/SECRETS.md b/labs/lab18/app_python/k8s/SECRETS.md new file mode 100644 index 0000000000..1c62eae2de --- /dev/null +++ b/labs/lab18/app_python/k8s/SECRETS.md @@ -0,0 +1,197 @@ +# Documentation + +## Kubernetes Secrets + +### Output of creating and viewing your secret + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl create secret generic app-credentials --from-literal=username=fountainer --from-literal=password=‘mypass293i20@@nekf’ +secret/app-credentials created +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get secret app-credentials -o yaml +apiVersion: v1 +data: + password: 4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ + username: Zm91bnRhaW5lcg== +kind: Secret +metadata: + creationTimestamp: "2026-04-07T14:46:16Z" + name: app-credentials + namespace: default + resourceVersion: "24859" + uid: 6997ca85-68fa-4278-9d51-a6531df977e9 +type: Opaque +``` +### Decoded secret values demonstration + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "4oCYbXlwYXNzMjkzaTIwQEBuZWtm4oCZ" | base64 -d +‘mypass293i20@@nekf’% +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % echo "Zm91bnRhaW5lcg==" | base64 -d +fountainer% +``` +### Explanation of base64 encoding vs encryption + +- Encoding is when we use some publicly accesible algorithm to encode our data. The goal is keeping integrity and usability of the data, it is not really about security. + +- In turn, Encryption is about securuty. It envolves encrypting with an algorithm that can be only resolved by a user who has an encryption key. + +## Helm Secret Integration + +### Chart structure showing secrets.yaml + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % tree app_python/k8s/app_python +app_python/k8s/app_python +├── Chart.yaml +├── charts +├── templates +│ ├── _helpers.tpl +│ ├── deployment.yaml +│ ├── hooks +│ │ ├── post-install-job.yaml +│ │ └── pre-install-job.yaml +│ ├── secrets.yaml +│ └── service.yaml +├── values-dev.yaml +├── values-prod.yaml +└── values.yaml +``` + +### How secrets are consumed in deployment + +- I have $secretName variable that is dynamically set to the name from the values.yaml and values I provide in the helm install command OR defaults to the value from helper. + +### Verification output (env vars in pod, excluding actual values) + +- in pod I have correct env vars: + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-7975557578-6zc9m -- sh +$ echo $PASSWORD +mypass293i20@@nekf +$ echo $USERNAME +fountainer +``` + +- and outside the secrets are hidden + +from ```bash kubectl describe pod mysecretrelease-app-python-7975557578-6zc9m``` + +```bash +Environment: + PASSWORD: Optional: false + USERNAME: Optional: false +``` + + +## Resource Management + +### Resource limits configuration + +```bash +resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} +``` +- in values.yaml I have + +```bash +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" +``` + +### Explanation of requests vs limits + +- requests is a setting that shows kubernates how much resources are needed for a container to run +- limits show how much resources a container is allowed to use (max) + +### How to choose appropriate values + +- you should analyze what processes does your container run and how many cpu/memory it may need +- values can be adjusted by observing the running container +- if you have multiple containers/pods you should constraint them in such a way that they all can work without throttling +- if the memory limit is too low the container can be killed right away + +## Vault Integration + +### Vault installation verification (kubectl get pods) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl get pod +NAME READY STATUS RESTARTS AGE +mysecretrelease-app-python-7975557578-6zc9m 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-7l4tv 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-bqnpd 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-cjjcb 1/1 Running 0 63m +mysecretrelease-app-python-7975557578-st2jd 1/1 Running 0 63m +vault-0 1/1 Running 0 8m23s +vault-agent-injector-848dd747d7-qvgl2 1/1 Running 0 8m23s +``` + +### Policy and role configuration (sanitized) + +- policy + +```bash +/ $ vault policy write myapp-policy /tmp/myapp-policy.hcl +Success! Uploaded policy: myapp-policy +/ $ vault policy read myapp-policy +path "secret/data/myapp/config" { + capabilities = ["read"] +} +``` + +- role config + +```bash +vault write auth/kubernetes/role/myapp-role \ + bound_service_account_names=default \ + bound_service_account_namespaces=default \ + policies=myapp-policy \ + ttl=48h +``` + + +### Proof of secret injection (show file exists, path structure) + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl exec -it mysecretrelease-app-python-558b98bb9d-8299m -- /bin/sh +Defaulted container "app-python" out of: app-python, vault-agent, vault-agent-init (init) +$ ls -l /vault/secrets +total 4 +-rw-r--r-- 1 100 newuser 180 Apr 7 23:55 config +$ cat /vault/secrets/config +data: map[password:mypass293i20@@nekf username:fountainer] +metadata: map[created_time:2026-04-07T23:32:33.85543147Z custom_metadata: deletion_time: destroyed:false version:1] +$ +``` + +### Explanation of the sidecar injection pattern + +- now every pod contains not only my app container but also vault sidecar container +- vault is able to authenticate in kubernates and inject secrets into the pod + +## Security Analysis + +### Comparison: K8s Secrets vs Vault + +- kubernates secrets are just encoded into base 64 and everyone who gets access to the cluster can decode them and get sensitive data, on the other hand, vault provides data encryption that is much more safer since you need an encryption key to encrypt it + +### When to use each approach + +- encoding is good for keeping data usability and integrity, so different machines can use it (like for seeing special symbols on a web page), it is like... more secure than nothing, but not reeally secure + +- vault encryption is needed for keeping sensitive data secure, like for storing passwords for the services on the virtual machines, etc + +### Production recommendations + +- in production you should always try to use strong encryption algorithms to keep your data secure diff --git a/labs/lab18/app_python/k8s/STATEFULSET.md b/labs/lab18/app_python/k8s/STATEFULSET.md new file mode 100644 index 0000000000..b5a5b60b72 --- /dev/null +++ b/labs/lab18/app_python/k8s/STATEFULSET.md @@ -0,0 +1,168 @@ +# Documentation + +## StatefulSet Overview + +### Why StatefulSet + +It is used when pods need stable identity and storage, like each pod keeping its own data and name even after restart. + +### Differences from Deployment + +Key differences: +- deployment pods are interchangeable and can change names/storage after restarts, while statefulset pods have fixed names (pod-0, pod-1) and their own persistent storage. + +When to use Deployment vs StatefulSet: +- deployment is used for stateless apps (like web servers), and statefulset for apps that need stable data and identity (like databases). + +Examples of stateful workloads: +- databases like mysql/postgresql, message queues, systems like elasticsearch + +### Headless Services + +What is a headless service (clusterIP: None)? +- a service without a cluster ip that lets you directly access individual pods instead of load balancing + +How DNS works with StatefulSets? +- each pod gets its own dns name like pod-0.service-name.namespace.svc.cluster.local, and they can be addressed individually + +## Resource Verification + +### Output of kubectl get pod,sts,svc,pvc + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get statefulset +NAME READY AGE +myapp-app-python 3/3 38s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pods +NAME READY STATUS RESTARTS AGE +my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +myapp-app-python-0 1/1 Running 0 42s +myapp-app-python-1 1/1 Running 0 25s +myapp-app-python-2 1/1 Running 0 16s +myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 42s +myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 42s +myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 42s +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pvc +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 53s +data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 36s +data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 27s +my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 53s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl get pod,sts,svc,pvc +NAME READY STATUS RESTARTS AGE +pod/my-app-app-python-5f57899757-4phmz 1/1 Running 1 (7d4h ago) 7d6h +pod/my-app-app-python-5f57899757-6sj7k 1/1 Running 1 (7d4h ago) 7d5h +pod/my-app-app-python-5f57899757-75mlj 1/1 Running 1 (7d4h ago) 7d5h +pod/myapp-app-python-0 1/1 Running 0 99s +pod/myapp-app-python-1 1/1 Running 0 82s +pod/myapp-app-python-2 1/1 Running 0 73s +pod/myapp-app-python-5bc87cfdf6-dhkt6 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-mxpkd 1/1 Running 0 99s +pod/myapp-app-python-5bc87cfdf6-wpc68 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-46pbw 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-9krh8 1/1 Running 0 99s +pod/myapp-app-python-7dc6cbf89f-lp4rh 1/1 Running 0 99s + +NAME READY AGE +statefulset.apps/myapp-app-python 3/3 99s + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/kubernetes ClusterIP 10.96.0.1 443/TCP 7d8h +service/myapp-app-python-preview ClusterIP 10.98.51.97 80/TCP 99s +service/myapp-app-python-service NodePort 10.110.182.139 80:30009/TCP 99s + +NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE +persistentvolumeclaim/data-volume-myapp-app-python-0 Bound pvc-2a043668-bbae-4c8d-86dc-ae89242a4b28 100Mi RWO standard 99s +persistentvolumeclaim/data-volume-myapp-app-python-1 Bound pvc-f9152007-9fff-4292-bc28-d1bc16b0214e 100Mi RWO standard 82s +persistentvolumeclaim/data-volume-myapp-app-python-2 Bound pvc-cec60c23-bdb5-44df-bf9f-9e2938726dc6 100Mi RWO standard 73s +persistentvolumeclaim/my-app-app-python-data Bound pvc-a5009930-2af6-4223-8fad-16257b59e9aa 100Mi RWO standard 7d6h +persistentvolumeclaim/myapp-app-python-data Bound pvc-22a66b4f-e1f6-486f-a528-76f27f090535 100Mi RWO standard 99s +(devops) fountainer@Veronicas-MacBook-Air app_python % +``` + +## Network Identity + +### DNS resolution outputs + +the naming pattern is ```.``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % kubectl exec -it myapp-app-python-0 -- /bin/sh +$ getent hosts myapp-app-python-0.myapp-app-python-headless +10.244.0.177 myapp-app-python-0.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-1.myapp-app-python-headless +10.244.0.179 myapp-app-python-1.myapp-app-python-headless.default.svc.cluster.local +$ getent hosts myapp-app-python-2.myapp-app-python-headless +10.244.0.180 myapp-app-python-2.myapp-app-python-headless.default.svc.cluster.local +``` + +## Per-Pod Storage Evidence + +### Different visit counts per pod + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-0 8080:12345 +Forwarding from 127.0.0.1:8080 -> 12345 +Forwarding from [::1]:8080 -> 12345 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +Handling connection for 8080 +``` + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % pyenv shell devops +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-1 8081:12345 +Forwarding from 127.0.0.1:8081 -> 12345 +Forwarding from [::1]:8081 -> 12345 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +Handling connection for 8081 +``` + +```bash +(devops) fountainer@Veronicas-MacBook-Air DevOps-Core-Course % kubectl port-forward pod/myapp-app-python-2 8082:12345 +Forwarding from 127.0.0.1:8082 -> 12345 +Forwarding from [::1]:8082 -> 12345 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +Handling connection for 8082 +``` +```bash +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8080/visits +{ + "visits": 13 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8081/visits +{ + "visits": 7 +} +(devops) fountainer@Veronicas-MacBook-Air app_python % curl localhost:8082/visits +{ + "visits": 2 +} +``` + +![](./../docs/screenshots/lab15-shots/visits-diff.png) + +## Persistence Test + +### data survives pod deletion + +![](./../docs/screenshots/lab15-shots/pers_test.png) \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/.helmignore b/labs/lab18/app_python/k8s/app_python/.helmignore new file mode 100644 index 0000000000..0e8a0eb36f --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/labs/lab18/app_python/k8s/app_python/Chart.yaml b/labs/lab18/app_python/k8s/app_python/Chart.yaml new file mode 100644 index 0000000000..3e280aeaaf --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/Chart.yaml @@ -0,0 +1,16 @@ +apiVersion: v2 +name: app_python +description: My Python application Helm chart + +type: application +version: 0.1.0 +appVersion: "1.0" + +keywords: + - python + - web +maintainers: + - name: Veronika Levasheva + email: veronikalev2005@gmail.com +sources: + - https://github.com/ffountainer/DevOps-Core-Course diff --git a/labs/lab18/app_python/k8s/app_python/files/config.json b/labs/lab18/app_python/k8s/app_python/files/config.json new file mode 100644 index 0000000000..ba8808215e --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/files/config.json @@ -0,0 +1,11 @@ +{ + "app_name": "my-app", + "environment": "dev", + "feature_flags": { + "debug": true, + "metrics": true + }, + "settings": { + "log_level": "info" + } +} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/service-headless.yaml b/labs/lab18/app_python/k8s/app_python/service-headless.yaml new file mode 100644 index 0000000000..04d26061e8 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/service-headless.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-headless +spec: + clusterIP: None + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/_helpers.tpl b/labs/lab18/app_python/k8s/app_python/templates/_helpers.tpl new file mode 100644 index 0000000000..2697f80e58 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/_helpers.tpl @@ -0,0 +1,50 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "mychart.name" -}} +{{- default .Chart.Name .Values.nameOverride | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +*/}} +{{- define "mychart.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := include "mychart.name" . }} +{{- printf "%s-%s" .Release.Name $name | replace "_" "-" | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} + +{{/* +Chart name and version. +*/}} +{{- define "mychart.chart" -}} +{{ .Chart.Name }}-{{ .Chart.Version }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "mychart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "mychart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Common labels. +*/}} +{{- define "mychart.labels" -}} +helm.sh/chart: {{ include "mychart.chart" . }} +{{ include "mychart.selectorLabels" . }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Service name helper. +*/}} +{{- define "mychart.serviceName" -}} +{{ include "mychart.fullname" . }}-service +{{- end }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/configmap-env.yaml b/labs/lab18/app_python/k8s/app_python/templates/configmap-env.yaml new file mode 100644 index 0000000000..f41c2df7d3 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/configmap-env.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "mychart.fullname" . }}-env + labels: + app: {{ include "mychart.name" . }} +data: + APP_NAME: {{ .Values.appName | quote }} + APP_ENV: {{ .Values.environment | quote }} + LOG_LEVEL: {{ .Values.logLevel | quote }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/configmap.yaml b/labs/lab18/app_python/k8s/app_python/templates/configmap.yaml new file mode 100644 index 0000000000..a037d8bd60 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/configmap.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "mychart.fullname" . }}-config + labels: + app: {{ include "mychart.name" . }} +data: + config.json: |- +{{ .Files.Get "files/config.json" | indent 4 }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/deployment.yaml b/labs/lab18/app_python/k8s/app_python/templates/deployment.yaml new file mode 100644 index 0000000000..c3cada620d --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/deployment.yaml @@ -0,0 +1,82 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: {{ .Values.strategy.maxUnavailable }} + maxSurge: {{ .Values.strategy.maxSurge }} + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + - name: data-volume + persistentVolumeClaim: + claimName: {{ include "mychart.fullname" . }}-data \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/headless-service.yaml b/labs/lab18/app_python/k8s/app_python/templates/headless-service.yaml new file mode 100644 index 0000000000..04d26061e8 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/headless-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-headless +spec: + clusterIP: None + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/hooks/post-install-job.yaml b/labs/lab18/app_python/k8s/app_python/templates/hooks/post-install-job.yaml new file mode 100644 index 0000000000..578cdf58a7 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/hooks/post-install-job.yaml @@ -0,0 +1,26 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ include "mychart.fullname" . }}-post-install" + labels: + {{- include "mychart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": post-install + "helm.sh/hook-weight": "5" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + name: "{{ include "mychart.fullname" . }}-post-install" + spec: + restartPolicy: Never + containers: + - name: post-install-job + image: busybox + command: + - sh + - -c + - | + echo "Post-install validation running" + sleep 10 + echo "Validation passed" \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml b/labs/lab18/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml new file mode 100644 index 0000000000..90018bbb06 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/hooks/pre-install-job.yaml @@ -0,0 +1,26 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ include "mychart.fullname" . }}-pre-install" + labels: + {{- include "mychart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": pre-install + "helm.sh/hook-weight": "-5" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + name: "{{ include "mychart.fullname" . }}-pre-install" + spec: + restartPolicy: Never + containers: + - name: pre-install-job + image: busybox + command: + - sh + - -c + - | + echo "Pre-install task running" + sleep 10 + echo "Pre-install completed" \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/init.yaml b/labs/lab18/app_python/k8s/app_python/templates/init.yaml new file mode 100644 index 0000000000..e7b959f9de --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/init.yaml @@ -0,0 +1,31 @@ +apiVersion: v1 +kind: Pod +metadata: + name: init-download-pod + namespace: default +spec: + initContainers: + - name: init-download + image: busybox:1.36 + command: + - sh + - -c + - wget -O /work-dir/index.html https://example.com + volumeMounts: + - name: workdir + mountPath: /work-dir + + containers: + - name: main-app + image: busybox:1.36 + command: + - sh + - -c + - sleep 3600 + volumeMounts: + - name: workdir + mountPath: /data + + volumes: + - name: workdir + emptyDir: {} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/nginx.yaml b/labs/lab18/app_python/k8s/app_python/templates/nginx.yaml new file mode 100644 index 0000000000..cc07ba1e93 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/nginx.yaml @@ -0,0 +1,32 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myservice + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: myservice + template: + metadata: + labels: + app: myservice + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 +--- +apiVersion: v1 +kind: Service +metadata: + name: myservice + namespace: default +spec: + selector: + app: myservice + ports: + - port: 80 + targetPort: 80 \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/preview-service.yaml b/labs/lab18/app_python/k8s/app_python/templates/preview-service.yaml new file mode 100644 index 0000000000..e59503c982 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/preview-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-preview +spec: + selector: + {{- include "mychart.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.service.targetPort }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/pvc.yaml b/labs/lab18/app_python/k8s/app_python/templates/pvc.yaml new file mode 100644 index 0000000000..a930a3cb18 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/pvc.yaml @@ -0,0 +1,17 @@ +{{- if .Values.persistence.enabled }} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "mychart.fullname" . }}-data + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.persistence.size }} + {{- if .Values.persistence.storageClass }} + storageClassName: {{ .Values.persistence.storageClass }} + {{- end }} +{{- end }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/rollout.yaml b/labs/lab18/app_python/k8s/app_python/templates/rollout.yaml new file mode 100644 index 0000000000..6d4e687d75 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/rollout.yaml @@ -0,0 +1,86 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: argoproj.io/v1alpha1 +kind: Rollout +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + - name: data-volume + persistentVolumeClaim: + claimName: {{ include "mychart.fullname" . }}-data + + strategy: + blueGreen: + activeService: {{ include "mychart.fullname" . }}-service + previewService: {{ include "mychart.fullname" . }}-preview + autoPromotionEnabled: false \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/secrets.yaml b/labs/lab18/app_python/k8s/app_python/templates/secrets.yaml new file mode 100644 index 0000000000..2460f91d72 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/secrets.yaml @@ -0,0 +1,12 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: v1 +kind: Secret +metadata: + name: {{ $secretName }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +type: Opaque +stringData: + username: {{ .Values.secret.data.username | quote }} + password: {{ .Values.secret.data.password | quote }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/service.yaml b/labs/lab18/app_python/k8s/app_python/templates/service.yaml new file mode 100644 index 0000000000..558f0675e3 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mychart.fullname" . }}-service +spec: + type: {{ .Values.service.type }} + selector: + {{- include "mychart.selectorLabels" . | nindent 6 }} + ports: + - protocol: {{ .Values.service.protocol }} + port: {{ .Values.service.port }} + targetPort: {{ .Values.service.targetPort }} + nodePort: {{ .Values.service.nodePort }} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/templates/statefulset.yaml b/labs/lab18/app_python/k8s/app_python/templates/statefulset.yaml new file mode 100644 index 0000000000..333b200ca0 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/statefulset.yaml @@ -0,0 +1,84 @@ +{{- $secretName := .Values.secret.name | default (include "mychart.fullname" .) }} + +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: {{ include "mychart.fullname" . }} + labels: + {{- include "mychart.labels" . | nindent 4 }} +spec: + serviceName: {{ include "mychart.fullname" . }}-headless + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "mychart.selectorLabels" . | nindent 6 }} + + template: + metadata: + labels: + {{- include "mychart.selectorLabels" . | nindent 8 }} + annotations: + vault.hashicorp.com/agent-inject: "true" + vault.hashicorp.com/role: "myapp-role" + vault.hashicorp.com/agent-inject-secret-config: "secret/data/myapp/config" + spec: + containers: + - name: {{ include "mychart.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + env: + - name: PASSWORD + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: password + - name: USERNAME + valueFrom: + secretKeyRef: + name: {{ $secretName }} + key: username + - name: DATA_DIR + value: /app/data + volumeMounts: + - name: config-volume + mountPath: /config + - name: data-volume + mountPath: /app/data + envFrom: + - configMapRef: + name: {{ include "mychart.fullname" . }}-env + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.container.port }} + resources: + requests: + cpu: {{ .Values.resources.requests.cpu }} + memory: {{ .Values.resources.requests.memory }} + limits: + cpu: {{ .Values.resources.limits.cpu }} + memory: {{ .Values.resources.limits.memory }} + livenessProbe: + httpGet: + path: {{ .Values.livenessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + failureThreshold: {{ .Values.livenessProbe.failureThreshold }} + readinessProbe: + httpGet: + path: {{ .Values.readinessProbe.path }} + port: {{ .Values.container.port }} + initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + volumes: + - name: config-volume + configMap: + name: {{ include "mychart.fullname" . }}-config + volumeClaimTemplates: + - metadata: + name: data-volume + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: {{ .Values.persistence.size }} diff --git a/labs/lab18/app_python/k8s/app_python/templates/waiting.yaml b/labs/lab18/app_python/k8s/app_python/templates/waiting.yaml new file mode 100644 index 0000000000..12a9e5575b --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/templates/waiting.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Pod +metadata: + name: wait-service-pod + namespace: default +spec: + initContainers: + - name: wait-for-service + image: busybox:1.36 + command: + - sh + - -c + - until wget -qO- http://myservice.default.svc.cluster.local; do echo waiting for service; sleep 2; done + containers: + - name: main-app + image: busybox:1.36 + command: + - sh + - -c + - echo Service found! && sleep 3600 \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/values-dev.yaml b/labs/lab18/app_python/k8s/app_python/values-dev.yaml new file mode 100644 index 0000000000..81c63488ec --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/values-dev.yaml @@ -0,0 +1,40 @@ +replicaCount: 1 + +image: + repository: fountainer/my-app + tag: "16-04" + pullPolicy: Always + +container: + port: 12345 + +resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 5 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 3 + +readinessProbe: + path: /ready + initialDelaySeconds: 2 + periodSeconds: 5 + +service: + type: NodePort + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30007 \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/values-prod.yaml b/labs/lab18/app_python/k8s/app_python/values-prod.yaml new file mode 100644 index 0000000000..0c3548c5d2 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/values-prod.yaml @@ -0,0 +1,40 @@ +replicaCount: 3 + +image: + repository: fountainer/my-app + tag: "16-04" + pullPolicy: Always + +container: + port: 12345 + +resources: + requests: + cpu: "200m" + memory: "256Mi" + limits: + cpu: "500m" + memory: "512Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 30 + periodSeconds: 5 + timeoutSeconds: 5 + failureThreshold: 5 + +readinessProbe: + path: /ready + initialDelaySeconds: 10 + periodSeconds: 3 + +service: + type: LoadBalancer + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30008 \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/values.yaml b/labs/lab18/app_python/k8s/app_python/values.yaml new file mode 100644 index 0000000000..b0ce838464 --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/values.yaml @@ -0,0 +1,60 @@ +replicaCount: 3 + +image: + repository: fountainer/my-app + tag: "2026.05.01" + pullPolicy: Always + +container: + port: 12345 + +persistence: + size: 1Gi + +storageClass: "" + +resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" + +strategy: + maxUnavailable: 1 + maxSurge: 1 + +livenessProbe: + path: /health + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + +readinessProbe: + path: /ready + initialDelaySeconds: 5 + periodSeconds: 5 + +service: + type: NodePort + port: 80 + targetPort: 12345 + protocol: TCP + nodePort: 30009 + +secret: + name: app-credentials + data: + username: "placeholder-user" + password: "placeholder-password" + +appName: my-app +environment: dev +logLevel: info + +persistence: + enabled: true + size: 100Mi + storageClass: "" \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/app_python/vault/myapp-policy.hcl b/labs/lab18/app_python/k8s/app_python/vault/myapp-policy.hcl new file mode 100644 index 0000000000..230c2125cb --- /dev/null +++ b/labs/lab18/app_python/k8s/app_python/vault/myapp-policy.hcl @@ -0,0 +1,3 @@ +path "secret/data/myapp/config" { + capabilities = ["read"] +} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/argocd/application-dev.yaml b/labs/lab18/app_python/k8s/argocd/application-dev.yaml new file mode 100644 index 0000000000..587c67c97d --- /dev/null +++ b/labs/lab18/app_python/k8s/argocd/application-dev.yaml @@ -0,0 +1,26 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: python-app-dev + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values-dev.yaml + + destination: + server: https://kubernetes.default.svc + namespace: dev + + syncPolicy: + automated: + prune: true + selfHeal: true + syncOptions: + - CreateNamespace=true \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/argocd/application-prod.yaml b/labs/lab18/app_python/k8s/argocd/application-prod.yaml new file mode 100644 index 0000000000..9ada9f09eb --- /dev/null +++ b/labs/lab18/app_python/k8s/argocd/application-prod.yaml @@ -0,0 +1,23 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: python-app-prod + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values-prod.yaml + + destination: + server: https://kubernetes.default.svc + namespace: prod + + syncPolicy: + syncOptions: + - CreateNamespace=true \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/argocd/application.yaml b/labs/lab18/app_python/k8s/argocd/application.yaml new file mode 100644 index 0000000000..72e9fd692c --- /dev/null +++ b/labs/lab18/app_python/k8s/argocd/application.yaml @@ -0,0 +1,21 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: my-app + namespace: argocd +spec: + project: default + + source: + repoURL: https://github.com/ffountainer/DevOps-Core-Course + targetRevision: lab13 + path: app_python/k8s/app_python + helm: + valueFiles: + - values.yaml + + destination: + server: https://kubernetes.default.svc + namespace: default + + syncPolicy: {} \ No newline at end of file diff --git a/labs/lab18/app_python/k8s/deployment.yml b/labs/lab18/app_python/k8s/deployment.yml new file mode 100644 index 0000000000..8be175e432 --- /dev/null +++ b/labs/lab18/app_python/k8s/deployment.yml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-app-deployment + labels: + app: my-app +spec: + replicas: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 1 + selector: + matchLabels: + app: my-app + template: + metadata: + labels: + app: my-app + spec: + containers: + - name: app-python + image: fountainer/my-app:2026.03.26 + ports: + - containerPort: 12345 + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "256Mi" + livenessProbe: + httpGet: + path: /health + port: 12345 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 5 + readinessProbe: + httpGet: + path: /ready + port: 12345 + initialDelaySeconds: 5 + periodSeconds: 5 \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/.env b/labs/lab18/app_python/monitoring/.env new file mode 100644 index 0000000000..350f57c8c2 --- /dev/null +++ b/labs/lab18/app_python/monitoring/.env @@ -0,0 +1,2 @@ +GF_PASSWORD=somth432ejw221@ +GF_EMAIL=coolme@localhost \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/docker-compose.yml b/labs/lab18/app_python/monitoring/docker-compose.yml new file mode 100644 index 0000000000..84f8d57b49 --- /dev/null +++ b/labs/lab18/app_python/monitoring/docker-compose.yml @@ -0,0 +1,138 @@ +services: + loki: + image: grafana/loki:3.0.0 + container_name: loki + ports: + - "3100:3100" + command: -config.file=/etc/loki/config.yml + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + volumes: + - ./loki/config.yml:/etc/loki/config.yml + - loki-data:/loki + networks: + - logging + deploy: + resources: + limits: + cpus: "1.0" + memory: 1G + reservations: + cpus: "0.50" + memory: 512M + promtail: + image: grafana/promtail:3.0.0 + container_name: promtail + ports: + - "9080:9080" + command: -config.file=/etc/promtail/config.yml + volumes: + - ./promtail/config.yml:/etc/promtail/config.yml + - /var/lib/docker/containers:/var/lib/docker/containers:ro + - /var/run/docker.sock:/var/run/docker.sock:ro + depends_on: + - loki + networks: + - logging + deploy: + resources: + limits: + cpus: "0.25" + memory: 256M + reservations: + cpus: "0.10" + memory: 128M + grafana: + image: grafana/grafana:12.3.1 + container_name: grafana + ports: + - "3000:3000" + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + environment: + - GF_AUTH_ANONYMOUS_ENABLED=false + - GF_SECURITY_ADMIN_PASSWORD=${GF_PASSWORD} + - GF_SECURITY_ADMIN_EMAIL=${GF_EMAIL} + volumes: + - grafana-data:/var/lib/grafana + depends_on: + - loki + networks: + - logging + deploy: + resources: + limits: + cpus: "0.5" + memory: 512M + reservations: + cpus: "0.25" + memory: 256M + app-python: + image: fountainer/my-app:latest + container_name: app-python + ports: + - "1999:12345" + healthcheck: + test: ["CMD-SHELL", "curl -f http://localhost:12345/health || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + networks: + - logging + labels: + logging: "promtail" + app: "my-app" + deploy: + resources: + limits: + cpus: "0.5" + memory: 256M + reservations: + cpus: "0.10" + memory: 128M + prometheus: + image: prom/prometheus:v3.9.0 + container_name: prometheus + ports: + - "9090:9090" + healthcheck: + test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:9090/-/healthy || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + command: + - --config.file=/etc/prometheus/prometheus.yml + - --storage.tsdb.retention.time=15d + - --storage.tsdb.retention.size=10GB + volumes: + - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml + - prometheus-data:/prometheus + networks: + - logging + depends_on: + - app-python + - loki + - grafana + deploy: + resources: + limits: + cpus: "1.0" + memory: 1G +volumes: + loki-data: + grafana-data: + prometheus-data: +networks: + logging: + driver: bridge + + + diff --git a/labs/lab18/app_python/monitoring/docs/LAB07.md b/labs/lab18/app_python/monitoring/docs/LAB07.md new file mode 100644 index 0000000000..02946794ee --- /dev/null +++ b/labs/lab18/app_python/monitoring/docs/LAB07.md @@ -0,0 +1 @@ +!!!!!! real LAB07.md documentation lies in the app_python/docs/LAB07.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/docs/LAB08.md b/labs/lab18/app_python/monitoring/docs/LAB08.md new file mode 100644 index 0000000000..8da919fdae --- /dev/null +++ b/labs/lab18/app_python/monitoring/docs/LAB08.md @@ -0,0 +1 @@ +!!!!!! real LAB08.md documentation lies in the app_python/docs/LAB08.md, since all prev labs documentation files were there, and I wanted to remain consistent. Please check this file, it has everything required by the task \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/loki/config.yml b/labs/lab18/app_python/monitoring/loki/config.yml new file mode 100644 index 0000000000..9a27a4c15d --- /dev/null +++ b/labs/lab18/app_python/monitoring/loki/config.yml @@ -0,0 +1,31 @@ +auth_enabled: false +server: + http_listen_port: 3100 +common: + path_prefix: /loki + storage: + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + replication_factor: 1 + ring: + kvstore: + store: inmemory +schema_config: + configs: + - from: 2026-03-01 + store: tsdb + object_store: filesystem + schema: v13 + index: + prefix: index_ + period: 24h +storage_config: + filesystem: + directory: /loki/chunks +limits_config: + retention_period: 168h +compactor: + working_directory: /loki/compactor + retention_enabled: true + delete_request_store: filesystem \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/prometheus/dashboard.json b/labs/lab18/app_python/monitoring/prometheus/dashboard.json new file mode 100644 index 0000000000..d63d022ade --- /dev/null +++ b/labs/lab18/app_python/monitoring/prometheus/dashboard.json @@ -0,0 +1,630 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 0, + "links": [], + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, + "id": 1, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "editorMode": "code", + "expr": "sum(rate(http_requests_total[5m])) by (endpoint)", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "title": "request rate", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "fixedColor": "red", + "mode": "fixed" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 0 + } + ] + } + }, + "overrides": [ + { + "__systemRef": "hideSeriesFrom", + "matcher": { + "id": "byNames", + "options": { + "mode": "exclude", + "names": [ + "sum(rate(http_requests_total{status_code=~\"[45]..\"}[5m]))" + ], + "prefix": "All except:", + "readOnly": true + } + }, + "properties": [ + { + "id": "custom.hideFrom", + "value": { + "legend": false, + "tooltip": true, + "viz": true + } + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 0 + }, + "id": 2, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "sum(rate(http_requests_total{status_code=~\"[45]..\"}[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "error rate", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + } + }, + "mappings": [] + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "404" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "light-red", + "mode": "fixed" + } + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 8 + }, + "id": 5, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "pieType": "pie", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "sort": "desc", + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "sum by (status_code) (rate(http_requests_total[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "status code distribution", + "type": "piechart" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + }, + "unit": "arcsec" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 8 + }, + "id": 3, + "options": { + "calculate": false, + "cellGap": 1, + "color": { + "exponent": 0.5, + "fill": "dark-orange", + "mode": "scheme", + "reverse": false, + "scale": "exponential", + "scheme": "Oranges", + "steps": 64 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": false, + "yHistogram": false + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "rate(http_request_duration_seconds_bucket[5m])", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "latency heatmap", + "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 16 + }, + "id": 7, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "p95 Latency", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 16 + }, + "id": 4, + "options": { + "minVizHeight": 75, + "minVizWidth": 75, + "orientation": "auto", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showThresholdLabels": false, + "showThresholdMarkers": true, + "sizing": "auto" + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "http_requests_in_progress", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "active requests", + "type": "gauge" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ffgik5dl3u2o0c" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 0 + }, + { + "color": "semi-dark-green", + "value": 1 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 24 + }, + "id": 6, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "auto", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.3.1", + "targets": [ + { + "editorMode": "code", + "expr": "up{job=\"app\"}", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "service health", + "type": "stat" + } + ], + "preload": false, + "schemaVersion": 42, + "tags": [], + "templating": { + "list": [] + }, + "time": { + "from": "now-5m", + "to": "now" + }, + "timepicker": {}, + "timezone": "browser", + "title": "devops lab8", + "uid": "adt5g7d", + "version": 11 +} \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/prometheus/prometheus.yml b/labs/lab18/app_python/monitoring/prometheus/prometheus.yml new file mode 100644 index 0000000000..442e8ae576 --- /dev/null +++ b/labs/lab18/app_python/monitoring/prometheus/prometheus.yml @@ -0,0 +1,23 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + +scrape_configs: + - job_name: 'prometheus' + static_configs: + - targets: ['localhost:9090'] + + - job_name: 'app' + static_configs: + - targets: ['app-python:12345'] + metrics_path: /metrics + + - job_name: 'loki' + static_configs: + - targets: ['loki:3100'] + metrics_path: /metrics + + - job_name: 'grafana' + static_configs: + - targets: ['grafana:3000'] + metrics_path: /metrics \ No newline at end of file diff --git a/labs/lab18/app_python/monitoring/promtail/config.yml b/labs/lab18/app_python/monitoring/promtail/config.yml new file mode 100644 index 0000000000..02c6e0501c --- /dev/null +++ b/labs/lab18/app_python/monitoring/promtail/config.yml @@ -0,0 +1,22 @@ +server: + http_listen_port: 9080 +positions: + filename: "/tmp/positions.yaml" + sync_period: 10s + ignore_invalid_yaml: false +clients: + - url: http://loki:3100/loki/api/v1/push +scrape_configs: + - job_name: docker + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + regex: '/(.*)' + target_label: 'container' + - target_label: job + replacement: docker + - source_labels: ['__meta_docker_container_label_logging'] + regex: promtail + action: keep \ No newline at end of file diff --git a/labs/lab18/app_python/pulumi/.gitignore b/labs/lab18/app_python/pulumi/.gitignore new file mode 100644 index 0000000000..a3807e5bdb --- /dev/null +++ b/labs/lab18/app_python/pulumi/.gitignore @@ -0,0 +1,2 @@ +*.pyc +venv/ diff --git a/labs/lab18/app_python/pulumi/Pulumi.dev.yaml b/labs/lab18/app_python/pulumi/Pulumi.dev.yaml new file mode 100644 index 0000000000..87ad76aa1d --- /dev/null +++ b/labs/lab18/app_python/pulumi/Pulumi.dev.yaml @@ -0,0 +1,9 @@ +encryptionsalt: v1:ibiWcVbfzPE=:v1:JGYI/AyYOtO6LyQX:wm2sULkxKAuOHYQjzutkotLh1kDMWQ== +config: + pulumi-vm:zone: "ru-central1-a" + pulumi-vm:appPort: "1999" + pulumi-vm:sshUser: "ubuntu" + pulumi-vm:sshPublicKeyPath: "/Users/fountainer/.ssh/terraform-vm-key.pub" + yandex:cloudId: "b1g4lhssapi17vlk0t0m" + yandex:folderId: "b1gcu87lbfoq3i0quvi9" + yandex:serviceAccountKeyFile: "/Users/fountainer/.yc/pulumi-key.json" \ No newline at end of file diff --git a/labs/lab18/app_python/pulumi/Pulumi.yaml b/labs/lab18/app_python/pulumi/Pulumi.yaml new file mode 100644 index 0000000000..f1628db528 --- /dev/null +++ b/labs/lab18/app_python/pulumi/Pulumi.yaml @@ -0,0 +1,11 @@ +name: pulumi-vm +description: A minimal Python Pulumi program +runtime: + name: python + options: + toolchain: pip + virtualenv: venv +config: + pulumi:tags: + value: + pulumi:template: python diff --git a/labs/lab18/app_python/pulumi/__main__.py b/labs/lab18/app_python/pulumi/__main__.py new file mode 100644 index 0000000000..b519111013 --- /dev/null +++ b/labs/lab18/app_python/pulumi/__main__.py @@ -0,0 +1,64 @@ +"""A Python Pulumi program""" + +import pulumi +import pulumi_yandex as yandex +import os + +config = pulumi.Config() +zone = config.require("zone") +app_port = config.require_int("appPort") +ssh_user = config.require("sshUser") +ssh_key_path = config.require("sshPublicKeyPath") + +with open(os.path.expanduser(ssh_key_path)) as f: + public_key = f.read().strip() + +network = yandex.VpcNetwork("network", name="network") + +subnet = yandex.VpcSubnet( + "subnet", + name="subnet1", + zone=zone, + network_id=network.id, + v4_cidr_blocks=["192.168.10.0/24"] +) + +vm_sg = yandex.VpcSecurityGroup( + "vm-sg", + description="VM security group", + network_id=network.id, + ingresses=[ + yandex.VpcSecurityGroupIngressArgs(port=22, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + yandex.VpcSecurityGroupIngressArgs(port=80, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + yandex.VpcSecurityGroupIngressArgs(port=app_port, protocol="TCP", v4_cidr_blocks=["0.0.0.0/0"]), + ], + egresses=[ + yandex.VpcSecurityGroupEgressArgs(protocol="ANY", from_port=0, to_port=0, v4_cidr_blocks=["0.0.0.0/0"]) + ] +) + +image = yandex.get_compute_image(family="ubuntu-2204-lts") + +vm = yandex.ComputeInstance( + "vm", + name="pulumi", + zone=zone, + resources={"cores": 2, "memory": 2}, + boot_disk={ + "initialize_params": { + "name": "boot-disk", + "size": 20, + "type": "network-hdd", + "image_id": image.id, + } + }, + network_interfaces=[{ + "subnet_id": subnet.id, + "nat": True, + "security_group_ids": [vm_sg.id], + }], + metadata={"ssh-keys": f"{ssh_user}:{public_key}"} +) + +pulumi.export("internal_ip", vm.network_interfaces[0]["ip_address"]) +pulumi.export("external_ip", vm.network_interfaces[0]["nat_ip_address"]) diff --git a/labs/lab18/app_python/pulumi/requirements.txt b/labs/lab18/app_python/pulumi/requirements.txt new file mode 100644 index 0000000000..938e7bfb54 --- /dev/null +++ b/labs/lab18/app_python/pulumi/requirements.txt @@ -0,0 +1,4 @@ +pulumi>=3.223.0 +pulumi_yandex==0.13.0 +PyYAML +setuptools \ No newline at end of file diff --git a/labs/lab18/app_python/reminder.md b/labs/lab18/app_python/reminder.md new file mode 100644 index 0000000000..98befa6862 --- /dev/null +++ b/labs/lab18/app_python/reminder.md @@ -0,0 +1,87 @@ +# File to remind myself about stuff + +## SSH keys + +### Outdated (doesn't use anywhere) +- terraform-vm-key (in ~/.ssh) + +### Current +- devops-terraform-passwordless (two directories: uni/devops for terraform vm config and ~/.ssh for ansible and CI/CD) + +## Ports + +### Main stack +- app port: 1999 +- container port: 12345 + +### Loki stack +- loki: 3100 +- grafana: 3000 +- promtail: 9080 +- prometheus: 9090 + +```bash +# Test Loki +curl http://localhost:3100/ready + +# Check Promtail targets +curl http://localhost:9080/targets + +# Access Grafana +open http://localhost:3000 +``` + +## VM commands + +### ssh into vm +```bash +ssh -l ubuntu 93.77.189.231 +``` + +### check curl + +```bash +curl http://127.0.0.1:1999/health +curl http://127.0.0.1:1999/ +``` + +## Local commands + +### run app.py locally +```bash +python app.py +``` + +### kube stack + +```bash +fountainer@Veronicas-MacBook-Air DevOps-Core-Course % helm install monitoring prometheus-community/kube-prometheus-stack \ + --namespace monitoring \ + --create-namespace +NAME: monitoring +LAST DEPLOYED: Sat May 2 22:35:24 2026 +NAMESPACE: monitoring +STATUS: deployed +REVISION: 1 +DESCRIPTION: Install complete +TEST SUITE: None +NOTES: +kube-prometheus-stack has been installed. Check its status by running: + kubectl --namespace monitoring get pods -l "release=monitoring" + +Get Grafana 'admin' user password by running: + + kubectl --namespace monitoring get secrets monitoring-grafana -o jsonpath="{.data.admin-password}" | base64 -d ; echo + +Access Grafana local instance: + + export POD_NAME=$(kubectl --namespace monitoring get pod -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=monitoring" -oname) + kubectl --namespace monitoring port-forward $POD_NAME 3000 + +Get your grafana admin user password by running: + + kubectl get secret --namespace monitoring -l app.kubernetes.io/component=admin-secret -o jsonpath="{.items[0].data.admin-password}" | base64 --decode ; echo + + +Visit https://github.com/prometheus-operator/kube-prometheus for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator. +``` diff --git a/labs/lab18/app_python/requirements-unpinned.txt b/labs/lab18/app_python/requirements-unpinned.txt new file mode 100644 index 0000000000..7e1060246f --- /dev/null +++ b/labs/lab18/app_python/requirements-unpinned.txt @@ -0,0 +1 @@ +flask diff --git a/labs/lab18/app_python/requirements.txt b/labs/lab18/app_python/requirements.txt new file mode 100644 index 0000000000..47296a42b8 --- /dev/null +++ b/labs/lab18/app_python/requirements.txt @@ -0,0 +1,6 @@ +# Web Framework +flask==3.1.2 +pytest==9.0.2 +python-json-logger==4.0.0 +python-dotenv==1.2.2 +prometheus-client==0.23.1 \ No newline at end of file diff --git a/labs/lab18/app_python/result b/labs/lab18/app_python/result new file mode 120000 index 0000000000..a82c3c29f0 --- /dev/null +++ b/labs/lab18/app_python/result @@ -0,0 +1 @@ +/nix/store/yn27ffk55ydg2qr4xls48a46720x5g8r-devops-info-service-nix.tar.gz \ No newline at end of file diff --git a/labs/lab18/app_python/terraform/.gitignore b/labs/lab18/app_python/terraform/.gitignore new file mode 100644 index 0000000000..b7cf2bdd62 --- /dev/null +++ b/labs/lab18/app_python/terraform/.gitignore @@ -0,0 +1,4 @@ +.terraform/ +terraform.tfstate +terraform.tfstate.backup +terraform.tfvars \ No newline at end of file diff --git a/labs/lab18/app_python/terraform/.terraform.lock.hcl b/labs/lab18/app_python/terraform/.terraform.lock.hcl new file mode 100644 index 0000000000..280b0e4395 --- /dev/null +++ b/labs/lab18/app_python/terraform/.terraform.lock.hcl @@ -0,0 +1,9 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/yandex-cloud/yandex" { + version = "0.191.0" + hashes = [ + "h1:MGHtJSlDigrSSCHWdl28B+XXnH87C82Qu0978/lbJtM=", + ] +} diff --git a/labs/lab18/app_python/terraform/README.md b/labs/lab18/app_python/terraform/README.md new file mode 100644 index 0000000000..22dafe1be3 --- /dev/null +++ b/labs/lab18/app_python/terraform/README.md @@ -0,0 +1,33 @@ +## SETUP INSTRUCTIONS: TERRAFORM + +### Set environment variables +```bash +export YC_TOKEN=$(yc iam create-token --impersonate-service-account-id ) +export YC_CLOUD_ID=$(yc config get cloud-id) +export YC_FOLDER_ID=$(yc config get folder-id) +``` + +### Initialize Terraform +```bash +terraform init +``` + +### Plan infrastructure +```bash +terraform plan +``` + +### Apply infrastructure +```bash +terraform apply +``` + +### Access VM via SSH +```bash +ssh -i /path/to/terraform-vm-key ubuntu@ +``` + +### Destroy resources +```bash +terraform destroy +``` diff --git a/labs/lab18/app_python/terraform/main.tf b/labs/lab18/app_python/terraform/main.tf new file mode 100644 index 0000000000..6d122555f9 --- /dev/null +++ b/labs/lab18/app_python/terraform/main.tf @@ -0,0 +1,95 @@ +terraform { + required_providers { + yandex = { + source = "yandex-cloud/yandex" + } + } + required_version = ">= 0.13" +} + +provider "yandex" { + service_account_key_file = "/Users/fountainer/.yandexcloud/my-sa-key.json" + zone = var.zone + cloud_id = "b1g4lhssapi17vlk0t0m" + folder_id = "b1gcu87lbfoq3i0quvi9" +} + +data "yandex_compute_image" "ubuntu_2204" { + family = "ubuntu-2204-lts" +} + +resource "yandex_compute_disk" "boot-disk" { + name = "boot-disk-terraform" + type = "network-hdd" + zone = var.zone + size = "20" + image_id = data.yandex_compute_image.ubuntu_2204.id +} + +resource "yandex_compute_instance" "vm" { + name = "terraform" + + resources { + cores = 2 + memory = 2 + } + + boot_disk { + disk_id = yandex_compute_disk.boot-disk.id + } + + network_interface { + subnet_id = yandex_vpc_subnet.subnet.id + nat = true + security_group_ids = [yandex_vpc_security_group.vm_sg.id] + + } + + metadata = { + ssh-keys = "ubuntu:${file(var.ssh_key_path)}" + } +} + +resource "yandex_vpc_network" "network" { + name = "network" +} + +resource "yandex_vpc_subnet" "subnet" { + name = "subnet1" + zone = var.zone + network_id = yandex_vpc_network.network.id + v4_cidr_blocks = ["192.168.10.0/24"] +} + +resource "yandex_vpc_security_group" "vm_sg" { + name = "vm-security-group" + network_id = yandex_vpc_network.network.id + + ingress { + protocol = "TCP" + description = "SSH" + port = 22 + v4_cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "TCP" + description = "HTTP" + port = 80 + v4_cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "TCP" + description = "App port" + port = var.app_port + v4_cidr_blocks = ["0.0.0.0/0"] + } + + egress { + protocol = "ANY" + description = "Allow all outgoing traffic" + v4_cidr_blocks = ["0.0.0.0/0"] + } +} + diff --git a/labs/lab18/app_python/terraform/outputs.tf b/labs/lab18/app_python/terraform/outputs.tf new file mode 100644 index 0000000000..17df178a5f --- /dev/null +++ b/labs/lab18/app_python/terraform/outputs.tf @@ -0,0 +1,10 @@ +output "internal_ip_address_vm" { + description = "Internal IP address of the VM" + value = yandex_compute_instance.vm.network_interface.0.ip_address +} + +output "external_ip_address_vm" { + description = "External (public) IP address of the VM" + value = yandex_compute_instance.vm.network_interface.0.nat_ip_address +} + diff --git a/labs/lab18/app_python/terraform/variables.tf b/labs/lab18/app_python/terraform/variables.tf new file mode 100644 index 0000000000..c30b81c0b3 --- /dev/null +++ b/labs/lab18/app_python/terraform/variables.tf @@ -0,0 +1,15 @@ +variable "zone" { + description = "Yandex Cloud availability zone" +} + +variable "ssh_key_path" { + description = "Path to the SSH key for VM access" +} + +variable "ssh_source_ip" { + description = "Your IP address to allow SSH access" +} + +variable "app_port" { + description = "Custom app port to open in the firewall" +} diff --git a/labs/lab18/app_python/tests/__init__.py b/labs/lab18/app_python/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/tests/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/tests/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..11a37662ee Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/__init__.cpython-312.pyc b/labs/lab18/app_python/tests/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000..9a552d4022 Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/__init__.cpython-312.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/test_error_404.cpython-312-pytest-9.0.2.pyc b/labs/lab18/app_python/tests/__pycache__/test_error_404.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000000..979e4e3322 Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/test_error_404.cpython-312-pytest-9.0.2.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-311-pytest-9.0.2.pyc b/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-311-pytest-9.0.2.pyc new file mode 100644 index 0000000000..97f48acd6a Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-311-pytest-9.0.2.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-312-pytest-9.0.2.pyc b/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000000..286c9b4313 Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/test_health_endpoint.cpython-312-pytest-9.0.2.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-311-pytest-9.0.2.pyc b/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-311-pytest-9.0.2.pyc new file mode 100644 index 0000000000..8ee2261f10 Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-311-pytest-9.0.2.pyc differ diff --git a/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-312-pytest-9.0.2.pyc b/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000000..10a42704e9 Binary files /dev/null and b/labs/lab18/app_python/tests/__pycache__/test_home_endpoint.cpython-312-pytest-9.0.2.pyc differ diff --git a/labs/lab18/app_python/tests/test_health_endpoint.py b/labs/lab18/app_python/tests/test_health_endpoint.py new file mode 100644 index 0000000000..74f99cefe8 --- /dev/null +++ b/labs/lab18/app_python/tests/test_health_endpoint.py @@ -0,0 +1,40 @@ +from app import app +import pytest + + +def test_health_endpoint(): + client = app.test_client() + response = client.get("/health") + + assert ( + response.status_code == 200 + or response.status_code == 404 + or response.status_code == 500 + ) + + resp = response.get_json() + + if response.status_code == 200: + + status = resp.get("status") + assert status + assert isinstance(status, str) + + timestamp = resp.get("timestamp") + assert timestamp + assert isinstance(timestamp, str) + + uptime_seconds = resp.get("uptime_seconds") + assert uptime_seconds >= 0 + assert isinstance(uptime_seconds, int) + + elif response.status_code == 404 or response.status_code == 500: + assert isinstance(resp, dict) + + error = resp.get("error") + assert error + assert isinstance(error, str) + + message = resp.get("message") + assert message + assert isinstance(message, str) diff --git a/labs/lab18/app_python/tests/test_home_endpoint.py b/labs/lab18/app_python/tests/test_home_endpoint.py new file mode 100644 index 0000000000..2bd12651a9 --- /dev/null +++ b/labs/lab18/app_python/tests/test_home_endpoint.py @@ -0,0 +1,74 @@ +from app import app +import pytest + + +def test_home_endpoint(): + client = app.test_client() + response = client.get("/") + + assert ( + response.status_code == 200 + or response.status_code == 404 + or response.status_code == 500 + ) + resp = response.get_json() + + if response.status_code == 200: + message = resp.get("message") + assert message + assert isinstance(message, dict) + + service = message.get("service") + assert service + assert isinstance(service, dict) + assert service.get("name") + assert service.get("version") + assert service.get("description") + assert service.get("framework") + assert service.get("debug status") + + system = message.get("system") + assert system + assert isinstance(system, dict) + assert system.get("hostname") + assert system.get("platform") + assert system.get("platform_version") + assert system.get("architecture") + assert system.get("cpu_count") + assert system.get("python_version") + + runtime = message.get("runtime") + assert runtime + assert isinstance(runtime, dict) + assert runtime.get("uptime_seconds") + assert runtime.get("uptime_human") + assert runtime.get("current_time") + assert runtime.get("timezone") + + request = message.get("request") + assert request + assert isinstance(request, dict) + assert request.get("client_ip") + assert request.get("port") + assert request.get("user_agent") + assert request.get("method") + assert request.get("path") + + endpoints = message.get("endpoints") + assert endpoints + assert isinstance(endpoints, list) + assert endpoints[0] + assert endpoints[0].get("path") + assert endpoints[1] + assert endpoints[1].get("path") + + elif response.status_code == 404 or response.status_code == 500: + assert isinstance(resp, dict) + + error = resp.get("error") + assert error + assert isinstance(error, str) + + message = resp.get("message") + assert message + assert isinstance(message, str) diff --git a/labs/lab18/app_python/venv1/bin/Activate.ps1 b/labs/lab18/app_python/venv1/bin/Activate.ps1 new file mode 100644 index 0000000000..b49d77ba44 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/Activate.ps1 @@ -0,0 +1,247 @@ +<# +.Synopsis +Activate a Python virtual environment for the current PowerShell session. + +.Description +Pushes the python executable for a virtual environment to the front of the +$Env:PATH environment variable and sets the prompt to signify that you are +in a Python virtual environment. Makes use of the command line switches as +well as the `pyvenv.cfg` file values present in the virtual environment. + +.Parameter VenvDir +Path to the directory that contains the virtual environment to activate. The +default value for this is the parent of the directory that the Activate.ps1 +script is located within. + +.Parameter Prompt +The prompt prefix to display when this virtual environment is activated. By +default, this prompt is the name of the virtual environment folder (VenvDir) +surrounded by parentheses and followed by a single space (ie. '(.venv) '). + +.Example +Activate.ps1 +Activates the Python virtual environment that contains the Activate.ps1 script. + +.Example +Activate.ps1 -Verbose +Activates the Python virtual environment that contains the Activate.ps1 script, +and shows extra information about the activation as it executes. + +.Example +Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv +Activates the Python virtual environment located in the specified location. + +.Example +Activate.ps1 -Prompt "MyPython" +Activates the Python virtual environment that contains the Activate.ps1 script, +and prefixes the current prompt with the specified string (surrounded in +parentheses) while the virtual environment is active. + +.Notes +On Windows, it may be required to enable this Activate.ps1 script by setting the +execution policy for the user. You can do this by issuing the following PowerShell +command: + +PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser + +For more information on Execution Policies: +https://go.microsoft.com/fwlink/?LinkID=135170 + +#> +Param( + [Parameter(Mandatory = $false)] + [String] + $VenvDir, + [Parameter(Mandatory = $false)] + [String] + $Prompt +) + +<# Function declarations --------------------------------------------------- #> + +<# +.Synopsis +Remove all shell session elements added by the Activate script, including the +addition of the virtual environment's Python executable from the beginning of +the PATH variable. + +.Parameter NonDestructive +If present, do not remove this function from the global namespace for the +session. + +#> +function global:deactivate ([switch]$NonDestructive) { + # Revert to original values + + # The prior prompt: + if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { + Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt + Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT + } + + # The prior PYTHONHOME: + if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { + Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME + Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME + } + + # The prior PATH: + if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { + Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH + Remove-Item -Path Env:_OLD_VIRTUAL_PATH + } + + # Just remove the VIRTUAL_ENV altogether: + if (Test-Path -Path Env:VIRTUAL_ENV) { + Remove-Item -Path env:VIRTUAL_ENV + } + + # Just remove VIRTUAL_ENV_PROMPT altogether. + if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { + Remove-Item -Path env:VIRTUAL_ENV_PROMPT + } + + # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: + if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { + Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force + } + + # Leave deactivate function in the global namespace if requested: + if (-not $NonDestructive) { + Remove-Item -Path function:deactivate + } +} + +<# +.Description +Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the +given folder, and returns them in a map. + +For each line in the pyvenv.cfg file, if that line can be parsed into exactly +two strings separated by `=` (with any amount of whitespace surrounding the =) +then it is considered a `key = value` line. The left hand string is the key, +the right hand is the value. + +If the value starts with a `'` or a `"` then the first and last character is +stripped from the value before being captured. + +.Parameter ConfigDir +Path to the directory that contains the `pyvenv.cfg` file. +#> +function Get-PyVenvConfig( + [String] + $ConfigDir +) { + Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" + + # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). + $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue + + # An empty map will be returned if no config file is found. + $pyvenvConfig = @{ } + + if ($pyvenvConfigPath) { + + Write-Verbose "File exists, parse `key = value` lines" + $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath + + $pyvenvConfigContent | ForEach-Object { + $keyval = $PSItem -split "\s*=\s*", 2 + if ($keyval[0] -and $keyval[1]) { + $val = $keyval[1] + + # Remove extraneous quotations around a string value. + if ("'""".Contains($val.Substring(0, 1))) { + $val = $val.Substring(1, $val.Length - 2) + } + + $pyvenvConfig[$keyval[0]] = $val + Write-Verbose "Adding Key: '$($keyval[0])'='$val'" + } + } + } + return $pyvenvConfig +} + + +<# Begin Activate script --------------------------------------------------- #> + +# Determine the containing directory of this script +$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition +$VenvExecDir = Get-Item -Path $VenvExecPath + +Write-Verbose "Activation script is located in path: '$VenvExecPath'" +Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" +Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" + +# Set values required in priority: CmdLine, ConfigFile, Default +# First, get the location of the virtual environment, it might not be +# VenvExecDir if specified on the command line. +if ($VenvDir) { + Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" +} +else { + Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." + $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") + Write-Verbose "VenvDir=$VenvDir" +} + +# Next, read the `pyvenv.cfg` file to determine any required value such +# as `prompt`. +$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir + +# Next, set the prompt from the command line, or the config file, or +# just use the name of the virtual environment folder. +if ($Prompt) { + Write-Verbose "Prompt specified as argument, using '$Prompt'" +} +else { + Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" + if ($pyvenvCfg -and $pyvenvCfg['prompt']) { + Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" + $Prompt = $pyvenvCfg['prompt']; + } + else { + Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" + Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" + $Prompt = Split-Path -Path $venvDir -Leaf + } +} + +Write-Verbose "Prompt = '$Prompt'" +Write-Verbose "VenvDir='$VenvDir'" + +# Deactivate any currently active virtual environment, but leave the +# deactivate function in place. +deactivate -nondestructive + +# Now set the environment variable VIRTUAL_ENV, used by many tools to determine +# that there is an activated venv. +$env:VIRTUAL_ENV = $VenvDir + +if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { + + Write-Verbose "Setting prompt to '$Prompt'" + + # Set the prompt to include the env name + # Make sure _OLD_VIRTUAL_PROMPT is global + function global:_OLD_VIRTUAL_PROMPT { "" } + Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT + New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt + + function global:prompt { + Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " + _OLD_VIRTUAL_PROMPT + } + $env:VIRTUAL_ENV_PROMPT = $Prompt +} + +# Clear PYTHONHOME +if (Test-Path -Path Env:PYTHONHOME) { + Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME + Remove-Item -Path Env:PYTHONHOME +} + +# Add the venv to the PATH +Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH +$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/labs/lab18/app_python/venv1/bin/activate b/labs/lab18/app_python/venv1/bin/activate new file mode 100644 index 0000000000..8e1c7d357d --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/activate @@ -0,0 +1,63 @@ +# This file must be used with "source bin/activate" *from bash* +# you cannot run it directly + +deactivate () { + # reset old environment variables + if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then + PATH="${_OLD_VIRTUAL_PATH:-}" + export PATH + unset _OLD_VIRTUAL_PATH + fi + if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then + PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" + export PYTHONHOME + unset _OLD_VIRTUAL_PYTHONHOME + fi + + # Call hash to forget past commands. Without forgetting + # past commands the $PATH changes we made may not be respected + hash -r 2> /dev/null + + if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then + PS1="${_OLD_VIRTUAL_PS1:-}" + export PS1 + unset _OLD_VIRTUAL_PS1 + fi + + unset VIRTUAL_ENV + unset VIRTUAL_ENV_PROMPT + if [ ! "${1:-}" = "nondestructive" ] ; then + # Self destruct! + unset -f deactivate + fi +} + +# unset irrelevant variables +deactivate nondestructive + +VIRTUAL_ENV="/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1" +export VIRTUAL_ENV + +_OLD_VIRTUAL_PATH="$PATH" +PATH="$VIRTUAL_ENV/bin:$PATH" +export PATH + +# unset PYTHONHOME if set +# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) +# could use `if (set -u; : $PYTHONHOME) ;` in bash +if [ -n "${PYTHONHOME:-}" ] ; then + _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" + unset PYTHONHOME +fi + +if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then + _OLD_VIRTUAL_PS1="${PS1:-}" + PS1="(venv1) ${PS1:-}" + export PS1 + VIRTUAL_ENV_PROMPT="(venv1) " + export VIRTUAL_ENV_PROMPT +fi + +# Call hash to forget past commands. Without forgetting +# past commands the $PATH changes we made may not be respected +hash -r 2> /dev/null diff --git a/labs/lab18/app_python/venv1/bin/activate.csh b/labs/lab18/app_python/venv1/bin/activate.csh new file mode 100644 index 0000000000..c1d7620a3f --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/activate.csh @@ -0,0 +1,26 @@ +# This file must be used with "source bin/activate.csh" *from csh*. +# You cannot run it directly. +# Created by Davide Di Blasi . +# Ported to Python 3.3 venv by Andrew Svetlov + +alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' + +# Unset irrelevant variables. +deactivate nondestructive + +setenv VIRTUAL_ENV "/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1" + +set _OLD_VIRTUAL_PATH="$PATH" +setenv PATH "$VIRTUAL_ENV/bin:$PATH" + + +set _OLD_VIRTUAL_PROMPT="$prompt" + +if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then + set prompt = "(venv1) $prompt" + setenv VIRTUAL_ENV_PROMPT "(venv1) " +endif + +alias pydoc python -m pydoc + +rehash diff --git a/labs/lab18/app_python/venv1/bin/activate.fish b/labs/lab18/app_python/venv1/bin/activate.fish new file mode 100644 index 0000000000..8d9fbb4f01 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/activate.fish @@ -0,0 +1,69 @@ +# This file must be used with "source /bin/activate.fish" *from fish* +# (https://fishshell.com/); you cannot run it directly. + +function deactivate -d "Exit virtual environment and return to normal shell environment" + # reset old environment variables + if test -n "$_OLD_VIRTUAL_PATH" + set -gx PATH $_OLD_VIRTUAL_PATH + set -e _OLD_VIRTUAL_PATH + end + if test -n "$_OLD_VIRTUAL_PYTHONHOME" + set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME + set -e _OLD_VIRTUAL_PYTHONHOME + end + + if test -n "$_OLD_FISH_PROMPT_OVERRIDE" + set -e _OLD_FISH_PROMPT_OVERRIDE + # prevents error when using nested fish instances (Issue #93858) + if functions -q _old_fish_prompt + functions -e fish_prompt + functions -c _old_fish_prompt fish_prompt + functions -e _old_fish_prompt + end + end + + set -e VIRTUAL_ENV + set -e VIRTUAL_ENV_PROMPT + if test "$argv[1]" != "nondestructive" + # Self-destruct! + functions -e deactivate + end +end + +# Unset irrelevant variables. +deactivate nondestructive + +set -gx VIRTUAL_ENV "/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1" + +set -gx _OLD_VIRTUAL_PATH $PATH +set -gx PATH "$VIRTUAL_ENV/bin" $PATH + +# Unset PYTHONHOME if set. +if set -q PYTHONHOME + set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME + set -e PYTHONHOME +end + +if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" + # fish uses a function instead of an env var to generate the prompt. + + # Save the current fish_prompt function as the function _old_fish_prompt. + functions -c fish_prompt _old_fish_prompt + + # With the original prompt function renamed, we can override with our own. + function fish_prompt + # Save the return status of the last command. + set -l old_status $status + + # Output the venv prompt; color taken from the blue of the Python logo. + printf "%s%s%s" (set_color 4B8BBE) "(venv1) " (set_color normal) + + # Restore the return status of the previous command. + echo "exit $old_status" | . + # Output the original/"old" prompt. + _old_fish_prompt + end + + set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" + set -gx VIRTUAL_ENV_PROMPT "(venv1) " +end diff --git a/labs/lab18/app_python/venv1/bin/flask b/labs/lab18/app_python/venv1/bin/flask new file mode 100755 index 0000000000..3308359a9d --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/flask @@ -0,0 +1,8 @@ +#!/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from flask.cli import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/labs/lab18/app_python/venv1/bin/pip b/labs/lab18/app_python/venv1/bin/pip new file mode 100755 index 0000000000..55bfe2a5f8 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/pip @@ -0,0 +1,8 @@ +#!/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/labs/lab18/app_python/venv1/bin/pip3 b/labs/lab18/app_python/venv1/bin/pip3 new file mode 100755 index 0000000000..55bfe2a5f8 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/pip3 @@ -0,0 +1,8 @@ +#!/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/labs/lab18/app_python/venv1/bin/pip3.11 b/labs/lab18/app_python/venv1/bin/pip3.11 new file mode 100755 index 0000000000..55bfe2a5f8 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/pip3.11 @@ -0,0 +1,8 @@ +#!/Users/fountainer/uni/devops/DevOps-Core-Course/labs/lab18/app_python/venv1/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/labs/lab18/app_python/venv1/bin/python b/labs/lab18/app_python/venv1/bin/python new file mode 120000 index 0000000000..6e7f3c7dd2 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/python @@ -0,0 +1 @@ +python3.11 \ No newline at end of file diff --git a/labs/lab18/app_python/venv1/bin/python3 b/labs/lab18/app_python/venv1/bin/python3 new file mode 120000 index 0000000000..6e7f3c7dd2 --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/python3 @@ -0,0 +1 @@ +python3.11 \ No newline at end of file diff --git a/labs/lab18/app_python/venv1/bin/python3.11 b/labs/lab18/app_python/venv1/bin/python3.11 new file mode 120000 index 0000000000..73a059d49d --- /dev/null +++ b/labs/lab18/app_python/venv1/bin/python3.11 @@ -0,0 +1 @@ +/Users/fountainer/.pyenv/versions/3.11.9/bin/python3.11 \ No newline at end of file diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__init__.py new file mode 100644 index 0000000000..f987a5367f --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__init__.py @@ -0,0 +1,222 @@ +# don't import any costly modules +import sys +import os + + +is_pypy = '__pypy__' in sys.builtin_module_names + + +def warn_distutils_present(): + if 'distutils' not in sys.modules: + return + if is_pypy and sys.version_info < (3, 7): + # PyPy for 3.6 unconditionally imports distutils, so bypass the warning + # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250 + return + import warnings + + warnings.warn( + "Distutils was imported before Setuptools, but importing Setuptools " + "also replaces the `distutils` module in `sys.modules`. This may lead " + "to undesirable behaviors or errors. To avoid these issues, avoid " + "using distutils directly, ensure that setuptools is installed in the " + "traditional way (e.g. not an editable install), and/or make sure " + "that setuptools is always imported before distutils." + ) + + +def clear_distutils(): + if 'distutils' not in sys.modules: + return + import warnings + + warnings.warn("Setuptools is replacing distutils.") + mods = [ + name + for name in sys.modules + if name == "distutils" or name.startswith("distutils.") + ] + for name in mods: + del sys.modules[name] + + +def enabled(): + """ + Allow selection of distutils by environment variable. + """ + which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local') + return which == 'local' + + +def ensure_local_distutils(): + import importlib + + clear_distutils() + + # With the DistutilsMetaFinder in place, + # perform an import to cause distutils to be + # loaded from setuptools._distutils. Ref #2906. + with shim(): + importlib.import_module('distutils') + + # check that submodules load as expected + core = importlib.import_module('distutils.core') + assert '_distutils' in core.__file__, core.__file__ + assert 'setuptools._distutils.log' not in sys.modules + + +def do_override(): + """ + Ensure that the local copy of distutils is preferred over stdlib. + + See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401 + for more motivation. + """ + if enabled(): + warn_distutils_present() + ensure_local_distutils() + + +class _TrivialRe: + def __init__(self, *patterns): + self._patterns = patterns + + def match(self, string): + return all(pat in string for pat in self._patterns) + + +class DistutilsMetaFinder: + def find_spec(self, fullname, path, target=None): + # optimization: only consider top level modules and those + # found in the CPython test suite. + if path is not None and not fullname.startswith('test.'): + return + + method_name = 'spec_for_{fullname}'.format(**locals()) + method = getattr(self, method_name, lambda: None) + return method() + + def spec_for_distutils(self): + if self.is_cpython(): + return + + import importlib + import importlib.abc + import importlib.util + + try: + mod = importlib.import_module('setuptools._distutils') + except Exception: + # There are a couple of cases where setuptools._distutils + # may not be present: + # - An older Setuptools without a local distutils is + # taking precedence. Ref #2957. + # - Path manipulation during sitecustomize removes + # setuptools from the path but only after the hook + # has been loaded. Ref #2980. + # In either case, fall back to stdlib behavior. + return + + class DistutilsLoader(importlib.abc.Loader): + def create_module(self, spec): + mod.__name__ = 'distutils' + return mod + + def exec_module(self, module): + pass + + return importlib.util.spec_from_loader( + 'distutils', DistutilsLoader(), origin=mod.__file__ + ) + + @staticmethod + def is_cpython(): + """ + Suppress supplying distutils for CPython (build and tests). + Ref #2965 and #3007. + """ + return os.path.isfile('pybuilddir.txt') + + def spec_for_pip(self): + """ + Ensure stdlib distutils when running under pip. + See pypa/pip#8761 for rationale. + """ + if self.pip_imported_during_build(): + return + clear_distutils() + self.spec_for_distutils = lambda: None + + @classmethod + def pip_imported_during_build(cls): + """ + Detect if pip is being imported in a build script. Ref #2355. + """ + import traceback + + return any( + cls.frame_file_is_setup(frame) for frame, line in traceback.walk_stack(None) + ) + + @staticmethod + def frame_file_is_setup(frame): + """ + Return True if the indicated frame suggests a setup.py file. + """ + # some frames may not have __file__ (#2940) + return frame.f_globals.get('__file__', '').endswith('setup.py') + + def spec_for_sensitive_tests(self): + """ + Ensure stdlib distutils when running select tests under CPython. + + python/cpython#91169 + """ + clear_distutils() + self.spec_for_distutils = lambda: None + + sensitive_tests = ( + [ + 'test.test_distutils', + 'test.test_peg_generator', + 'test.test_importlib', + ] + if sys.version_info < (3, 10) + else [ + 'test.test_distutils', + ] + ) + + +for name in DistutilsMetaFinder.sensitive_tests: + setattr( + DistutilsMetaFinder, + f'spec_for_{name}', + DistutilsMetaFinder.spec_for_sensitive_tests, + ) + + +DISTUTILS_FINDER = DistutilsMetaFinder() + + +def add_shim(): + DISTUTILS_FINDER in sys.meta_path or insert_shim() + + +class shim: + def __enter__(self): + insert_shim() + + def __exit__(self, exc, value, tb): + remove_shim() + + +def insert_shim(): + sys.meta_path.insert(0, DISTUTILS_FINDER) + + +def remove_shim(): + try: + sys.meta_path.remove(DISTUTILS_FINDER) + except ValueError: + pass diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..66f118fb9d Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/override.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/override.cpython-311.pyc new file mode 100644 index 0000000000..06b37ca414 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/__pycache__/override.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/override.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/override.py new file mode 100644 index 0000000000..2cc433a4a5 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/_distutils_hack/override.py @@ -0,0 +1 @@ +__import__('_distutils_hack').do_override() diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/INSTALLER b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/INSTALLER new file mode 100644 index 0000000000..a1b589e38a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/INSTALLER @@ -0,0 +1 @@ +pip diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/LICENSE.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/LICENSE.txt new file mode 100644 index 0000000000..79c9825adb --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright 2010 Jason Kirtland + +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. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/METADATA b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/METADATA new file mode 100644 index 0000000000..6d343f5718 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/METADATA @@ -0,0 +1,60 @@ +Metadata-Version: 2.3 +Name: blinker +Version: 1.9.0 +Summary: Fast, simple object-to-object and broadcast signaling +Author: Jason Kirtland +Maintainer-email: Pallets Ecosystem +Requires-Python: >=3.9 +Description-Content-Type: text/markdown +Classifier: Development Status :: 5 - Production/Stable +Classifier: License :: OSI Approved :: MIT License +Classifier: Programming Language :: Python +Classifier: Typing :: Typed +Project-URL: Chat, https://discord.gg/pallets +Project-URL: Documentation, https://blinker.readthedocs.io +Project-URL: Source, https://github.com/pallets-eco/blinker/ + +# Blinker + +Blinker provides a fast dispatching system that allows any number of +interested parties to subscribe to events, or "signals". + + +## Pallets Community Ecosystem + +> [!IMPORTANT]\ +> This project is part of the Pallets Community Ecosystem. Pallets is the open +> source organization that maintains Flask; Pallets-Eco enables community +> maintenance of related projects. If you are interested in helping maintain +> this project, please reach out on [the Pallets Discord server][discord]. +> +> [discord]: https://discord.gg/pallets + + +## Example + +Signal receivers can subscribe to specific senders or receive signals +sent by any sender. + +```pycon +>>> from blinker import signal +>>> started = signal('round-started') +>>> def each(round): +... print(f"Round {round}") +... +>>> started.connect(each) + +>>> def round_two(round): +... print("This is round two.") +... +>>> started.connect(round_two, sender=2) + +>>> for round in range(1, 4): +... started.send(round) +... +Round 1! +Round 2! +This is round two. +Round 3! +``` + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/RECORD b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/RECORD new file mode 100644 index 0000000000..52d1667f11 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/RECORD @@ -0,0 +1,12 @@ +blinker-1.9.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 +blinker-1.9.0.dist-info/LICENSE.txt,sha256=nrc6HzhZekqhcCXSrhvjg5Ykx5XphdTw6Xac4p-spGc,1054 +blinker-1.9.0.dist-info/METADATA,sha256=uIRiM8wjjbHkCtbCyTvctU37IAZk0kEe5kxAld1dvzA,1633 +blinker-1.9.0.dist-info/RECORD,, +blinker-1.9.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82 +blinker/__init__.py,sha256=I2EdZqpy4LyjX17Hn1yzJGWCjeLaVaPzsMgHkLfj_cQ,317 +blinker/__pycache__/__init__.cpython-311.pyc,, +blinker/__pycache__/_utilities.cpython-311.pyc,, +blinker/__pycache__/base.cpython-311.pyc,, +blinker/_utilities.py,sha256=0J7eeXXTUx0Ivf8asfpx0ycVkp0Eqfqnj117x2mYX9E,1675 +blinker/base.py,sha256=QpDuvXXcwJF49lUBcH5BiST46Rz9wSG7VW_p7N_027M,19132 +blinker/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/WHEEL b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/WHEEL new file mode 100644 index 0000000000..e3c6feefa2 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker-1.9.0.dist-info/WHEEL @@ -0,0 +1,4 @@ +Wheel-Version: 1.0 +Generator: flit 3.10.1 +Root-Is-Purelib: true +Tag: py3-none-any diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__init__.py new file mode 100644 index 0000000000..1772fa4a54 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__init__.py @@ -0,0 +1,17 @@ +from __future__ import annotations + +from .base import ANY +from .base import default_namespace +from .base import NamedSignal +from .base import Namespace +from .base import Signal +from .base import signal + +__all__ = [ + "ANY", + "default_namespace", + "NamedSignal", + "Namespace", + "Signal", + "signal", +] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..55b2f1c64a Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/_utilities.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/_utilities.cpython-311.pyc new file mode 100644 index 0000000000..9a817d3852 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/_utilities.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/base.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/base.cpython-311.pyc new file mode 100644 index 0000000000..1dc0820188 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/__pycache__/base.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/_utilities.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/_utilities.py new file mode 100644 index 0000000000..000c902a25 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/_utilities.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +import collections.abc as c +import inspect +import typing as t +from weakref import ref +from weakref import WeakMethod + +T = t.TypeVar("T") + + +class Symbol: + """A constant symbol, nicer than ``object()``. Repeated calls return the + same instance. + + >>> Symbol('foo') is Symbol('foo') + True + >>> Symbol('foo') + foo + """ + + symbols: t.ClassVar[dict[str, Symbol]] = {} + + def __new__(cls, name: str) -> Symbol: + if name in cls.symbols: + return cls.symbols[name] + + obj = super().__new__(cls) + cls.symbols[name] = obj + return obj + + def __init__(self, name: str) -> None: + self.name = name + + def __repr__(self) -> str: + return self.name + + def __getnewargs__(self) -> tuple[t.Any, ...]: + return (self.name,) + + +def make_id(obj: object) -> c.Hashable: + """Get a stable identifier for a receiver or sender, to be used as a dict + key or in a set. + """ + if inspect.ismethod(obj): + # The id of a bound method is not stable, but the id of the unbound + # function and instance are. + return id(obj.__func__), id(obj.__self__) + + if isinstance(obj, (str, int)): + # Instances with the same value always compare equal and have the same + # hash, even if the id may change. + return obj + + # Assume other types are not hashable but will always be the same instance. + return id(obj) + + +def make_ref(obj: T, callback: c.Callable[[ref[T]], None] | None = None) -> ref[T]: + if inspect.ismethod(obj): + return WeakMethod(obj, callback) # type: ignore[arg-type, return-value] + + return ref(obj, callback) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/base.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/base.py new file mode 100644 index 0000000000..d051b94a32 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/base.py @@ -0,0 +1,512 @@ +from __future__ import annotations + +import collections.abc as c +import sys +import typing as t +import weakref +from collections import defaultdict +from contextlib import contextmanager +from functools import cached_property +from inspect import iscoroutinefunction + +from ._utilities import make_id +from ._utilities import make_ref +from ._utilities import Symbol + +F = t.TypeVar("F", bound=c.Callable[..., t.Any]) + +ANY = Symbol("ANY") +"""Symbol for "any sender".""" + +ANY_ID = 0 + + +class Signal: + """A notification emitter. + + :param doc: The docstring for the signal. + """ + + ANY = ANY + """An alias for the :data:`~blinker.ANY` sender symbol.""" + + set_class: type[set[t.Any]] = set + """The set class to use for tracking connected receivers and senders. + Python's ``set`` is unordered. If receivers must be dispatched in the order + they were connected, an ordered set implementation can be used. + + .. versionadded:: 1.7 + """ + + @cached_property + def receiver_connected(self) -> Signal: + """Emitted at the end of each :meth:`connect` call. + + The signal sender is the signal instance, and the :meth:`connect` + arguments are passed through: ``receiver``, ``sender``, and ``weak``. + + .. versionadded:: 1.2 + """ + return Signal(doc="Emitted after a receiver connects.") + + @cached_property + def receiver_disconnected(self) -> Signal: + """Emitted at the end of each :meth:`disconnect` call. + + The sender is the signal instance, and the :meth:`disconnect` arguments + are passed through: ``receiver`` and ``sender``. + + This signal is emitted **only** when :meth:`disconnect` is called + explicitly. This signal cannot be emitted by an automatic disconnect + when a weakly referenced receiver or sender goes out of scope, as the + instance is no longer be available to be used as the sender for this + signal. + + An alternative approach is available by subscribing to + :attr:`receiver_connected` and setting up a custom weakref cleanup + callback on weak receivers and senders. + + .. versionadded:: 1.2 + """ + return Signal(doc="Emitted after a receiver disconnects.") + + def __init__(self, doc: str | None = None) -> None: + if doc: + self.__doc__ = doc + + self.receivers: dict[ + t.Any, weakref.ref[c.Callable[..., t.Any]] | c.Callable[..., t.Any] + ] = {} + """The map of connected receivers. Useful to quickly check if any + receivers are connected to the signal: ``if s.receivers:``. The + structure and data is not part of the public API, but checking its + boolean value is. + """ + + self.is_muted: bool = False + self._by_receiver: dict[t.Any, set[t.Any]] = defaultdict(self.set_class) + self._by_sender: dict[t.Any, set[t.Any]] = defaultdict(self.set_class) + self._weak_senders: dict[t.Any, weakref.ref[t.Any]] = {} + + def connect(self, receiver: F, sender: t.Any = ANY, weak: bool = True) -> F: + """Connect ``receiver`` to be called when the signal is sent by + ``sender``. + + :param receiver: The callable to call when :meth:`send` is called with + the given ``sender``, passing ``sender`` as a positional argument + along with any extra keyword arguments. + :param sender: Any object or :data:`ANY`. ``receiver`` will only be + called when :meth:`send` is called with this sender. If ``ANY``, the + receiver will be called for any sender. A receiver may be connected + to multiple senders by calling :meth:`connect` multiple times. + :param weak: Track the receiver with a :mod:`weakref`. The receiver will + be automatically disconnected when it is garbage collected. When + connecting a receiver defined within a function, set to ``False``, + otherwise it will be disconnected when the function scope ends. + """ + receiver_id = make_id(receiver) + sender_id = ANY_ID if sender is ANY else make_id(sender) + + if weak: + self.receivers[receiver_id] = make_ref( + receiver, self._make_cleanup_receiver(receiver_id) + ) + else: + self.receivers[receiver_id] = receiver + + self._by_sender[sender_id].add(receiver_id) + self._by_receiver[receiver_id].add(sender_id) + + if sender is not ANY and sender_id not in self._weak_senders: + # store a cleanup for weakref-able senders + try: + self._weak_senders[sender_id] = make_ref( + sender, self._make_cleanup_sender(sender_id) + ) + except TypeError: + pass + + if "receiver_connected" in self.__dict__ and self.receiver_connected.receivers: + try: + self.receiver_connected.send( + self, receiver=receiver, sender=sender, weak=weak + ) + except TypeError: + # TODO no explanation or test for this + self.disconnect(receiver, sender) + raise + + return receiver + + def connect_via(self, sender: t.Any, weak: bool = False) -> c.Callable[[F], F]: + """Connect the decorated function to be called when the signal is sent + by ``sender``. + + The decorated function will be called when :meth:`send` is called with + the given ``sender``, passing ``sender`` as a positional argument along + with any extra keyword arguments. + + :param sender: Any object or :data:`ANY`. ``receiver`` will only be + called when :meth:`send` is called with this sender. If ``ANY``, the + receiver will be called for any sender. A receiver may be connected + to multiple senders by calling :meth:`connect` multiple times. + :param weak: Track the receiver with a :mod:`weakref`. The receiver will + be automatically disconnected when it is garbage collected. When + connecting a receiver defined within a function, set to ``False``, + otherwise it will be disconnected when the function scope ends.= + + .. versionadded:: 1.1 + """ + + def decorator(fn: F) -> F: + self.connect(fn, sender, weak) + return fn + + return decorator + + @contextmanager + def connected_to( + self, receiver: c.Callable[..., t.Any], sender: t.Any = ANY + ) -> c.Generator[None, None, None]: + """A context manager that temporarily connects ``receiver`` to the + signal while a ``with`` block executes. When the block exits, the + receiver is disconnected. Useful for tests. + + :param receiver: The callable to call when :meth:`send` is called with + the given ``sender``, passing ``sender`` as a positional argument + along with any extra keyword arguments. + :param sender: Any object or :data:`ANY`. ``receiver`` will only be + called when :meth:`send` is called with this sender. If ``ANY``, the + receiver will be called for any sender. + + .. versionadded:: 1.1 + """ + self.connect(receiver, sender=sender, weak=False) + + try: + yield None + finally: + self.disconnect(receiver) + + @contextmanager + def muted(self) -> c.Generator[None, None, None]: + """A context manager that temporarily disables the signal. No receivers + will be called if the signal is sent, until the ``with`` block exits. + Useful for tests. + """ + self.is_muted = True + + try: + yield None + finally: + self.is_muted = False + + def send( + self, + sender: t.Any | None = None, + /, + *, + _async_wrapper: c.Callable[ + [c.Callable[..., c.Coroutine[t.Any, t.Any, t.Any]]], c.Callable[..., t.Any] + ] + | None = None, + **kwargs: t.Any, + ) -> list[tuple[c.Callable[..., t.Any], t.Any]]: + """Call all receivers that are connected to the given ``sender`` + or :data:`ANY`. Each receiver is called with ``sender`` as a positional + argument along with any extra keyword arguments. Return a list of + ``(receiver, return value)`` tuples. + + The order receivers are called is undefined, but can be influenced by + setting :attr:`set_class`. + + If a receiver raises an exception, that exception will propagate up. + This makes debugging straightforward, with an assumption that correctly + implemented receivers will not raise. + + :param sender: Call receivers connected to this sender, in addition to + those connected to :data:`ANY`. + :param _async_wrapper: Will be called on any receivers that are async + coroutines to turn them into sync callables. For example, could run + the receiver with an event loop. + :param kwargs: Extra keyword arguments to pass to each receiver. + + .. versionchanged:: 1.7 + Added the ``_async_wrapper`` argument. + """ + if self.is_muted: + return [] + + results = [] + + for receiver in self.receivers_for(sender): + if iscoroutinefunction(receiver): + if _async_wrapper is None: + raise RuntimeError("Cannot send to a coroutine function.") + + result = _async_wrapper(receiver)(sender, **kwargs) + else: + result = receiver(sender, **kwargs) + + results.append((receiver, result)) + + return results + + async def send_async( + self, + sender: t.Any | None = None, + /, + *, + _sync_wrapper: c.Callable[ + [c.Callable[..., t.Any]], c.Callable[..., c.Coroutine[t.Any, t.Any, t.Any]] + ] + | None = None, + **kwargs: t.Any, + ) -> list[tuple[c.Callable[..., t.Any], t.Any]]: + """Await all receivers that are connected to the given ``sender`` + or :data:`ANY`. Each receiver is called with ``sender`` as a positional + argument along with any extra keyword arguments. Return a list of + ``(receiver, return value)`` tuples. + + The order receivers are called is undefined, but can be influenced by + setting :attr:`set_class`. + + If a receiver raises an exception, that exception will propagate up. + This makes debugging straightforward, with an assumption that correctly + implemented receivers will not raise. + + :param sender: Call receivers connected to this sender, in addition to + those connected to :data:`ANY`. + :param _sync_wrapper: Will be called on any receivers that are sync + callables to turn them into async coroutines. For example, + could call the receiver in a thread. + :param kwargs: Extra keyword arguments to pass to each receiver. + + .. versionadded:: 1.7 + """ + if self.is_muted: + return [] + + results = [] + + for receiver in self.receivers_for(sender): + if not iscoroutinefunction(receiver): + if _sync_wrapper is None: + raise RuntimeError("Cannot send to a non-coroutine function.") + + result = await _sync_wrapper(receiver)(sender, **kwargs) + else: + result = await receiver(sender, **kwargs) + + results.append((receiver, result)) + + return results + + def has_receivers_for(self, sender: t.Any) -> bool: + """Check if there is at least one receiver that will be called with the + given ``sender``. A receiver connected to :data:`ANY` will always be + called, regardless of sender. Does not check if weakly referenced + receivers are still live. See :meth:`receivers_for` for a stronger + search. + + :param sender: Check for receivers connected to this sender, in addition + to those connected to :data:`ANY`. + """ + if not self.receivers: + return False + + if self._by_sender[ANY_ID]: + return True + + if sender is ANY: + return False + + return make_id(sender) in self._by_sender + + def receivers_for( + self, sender: t.Any + ) -> c.Generator[c.Callable[..., t.Any], None, None]: + """Yield each receiver to be called for ``sender``, in addition to those + to be called for :data:`ANY`. Weakly referenced receivers that are not + live will be disconnected and skipped. + + :param sender: Yield receivers connected to this sender, in addition + to those connected to :data:`ANY`. + """ + # TODO: test receivers_for(ANY) + if not self.receivers: + return + + sender_id = make_id(sender) + + if sender_id in self._by_sender: + ids = self._by_sender[ANY_ID] | self._by_sender[sender_id] + else: + ids = self._by_sender[ANY_ID].copy() + + for receiver_id in ids: + receiver = self.receivers.get(receiver_id) + + if receiver is None: + continue + + if isinstance(receiver, weakref.ref): + strong = receiver() + + if strong is None: + self._disconnect(receiver_id, ANY_ID) + continue + + yield strong + else: + yield receiver + + def disconnect(self, receiver: c.Callable[..., t.Any], sender: t.Any = ANY) -> None: + """Disconnect ``receiver`` from being called when the signal is sent by + ``sender``. + + :param receiver: A connected receiver callable. + :param sender: Disconnect from only this sender. By default, disconnect + from all senders. + """ + sender_id: c.Hashable + + if sender is ANY: + sender_id = ANY_ID + else: + sender_id = make_id(sender) + + receiver_id = make_id(receiver) + self._disconnect(receiver_id, sender_id) + + if ( + "receiver_disconnected" in self.__dict__ + and self.receiver_disconnected.receivers + ): + self.receiver_disconnected.send(self, receiver=receiver, sender=sender) + + def _disconnect(self, receiver_id: c.Hashable, sender_id: c.Hashable) -> None: + if sender_id == ANY_ID: + if self._by_receiver.pop(receiver_id, None) is not None: + for bucket in self._by_sender.values(): + bucket.discard(receiver_id) + + self.receivers.pop(receiver_id, None) + else: + self._by_sender[sender_id].discard(receiver_id) + self._by_receiver[receiver_id].discard(sender_id) + + def _make_cleanup_receiver( + self, receiver_id: c.Hashable + ) -> c.Callable[[weakref.ref[c.Callable[..., t.Any]]], None]: + """Create a callback function to disconnect a weakly referenced + receiver when it is garbage collected. + """ + + def cleanup(ref: weakref.ref[c.Callable[..., t.Any]]) -> None: + # If the interpreter is shutting down, disconnecting can result in a + # weird ignored exception. Don't call it in that case. + if not sys.is_finalizing(): + self._disconnect(receiver_id, ANY_ID) + + return cleanup + + def _make_cleanup_sender( + self, sender_id: c.Hashable + ) -> c.Callable[[weakref.ref[t.Any]], None]: + """Create a callback function to disconnect all receivers for a weakly + referenced sender when it is garbage collected. + """ + assert sender_id != ANY_ID + + def cleanup(ref: weakref.ref[t.Any]) -> None: + self._weak_senders.pop(sender_id, None) + + for receiver_id in self._by_sender.pop(sender_id, ()): + self._by_receiver[receiver_id].discard(sender_id) + + return cleanup + + def _cleanup_bookkeeping(self) -> None: + """Prune unused sender/receiver bookkeeping. Not threadsafe. + + Connecting & disconnecting leaves behind a small amount of bookkeeping + data. Typical workloads using Blinker, for example in most web apps, + Flask, CLI scripts, etc., are not adversely affected by this + bookkeeping. + + With a long-running process performing dynamic signal routing with high + volume, e.g. connecting to function closures, senders are all unique + object instances. Doing all of this over and over may cause memory usage + to grow due to extraneous bookkeeping. (An empty ``set`` for each stale + sender/receiver pair.) + + This method will prune that bookkeeping away, with the caveat that such + pruning is not threadsafe. The risk is that cleanup of a fully + disconnected receiver/sender pair occurs while another thread is + connecting that same pair. If you are in the highly dynamic, unique + receiver/sender situation that has lead you to this method, that failure + mode is perhaps not a big deal for you. + """ + for mapping in (self._by_sender, self._by_receiver): + for ident, bucket in list(mapping.items()): + if not bucket: + mapping.pop(ident, None) + + def _clear_state(self) -> None: + """Disconnect all receivers and senders. Useful for tests.""" + self._weak_senders.clear() + self.receivers.clear() + self._by_sender.clear() + self._by_receiver.clear() + + +class NamedSignal(Signal): + """A named generic notification emitter. The name is not used by the signal + itself, but matches the key in the :class:`Namespace` that it belongs to. + + :param name: The name of the signal within the namespace. + :param doc: The docstring for the signal. + """ + + def __init__(self, name: str, doc: str | None = None) -> None: + super().__init__(doc) + + #: The name of this signal. + self.name: str = name + + def __repr__(self) -> str: + base = super().__repr__() + return f"{base[:-1]}; {self.name!r}>" # noqa: E702 + + +class Namespace(dict[str, NamedSignal]): + """A dict mapping names to signals.""" + + def signal(self, name: str, doc: str | None = None) -> NamedSignal: + """Return the :class:`NamedSignal` for the given ``name``, creating it + if required. Repeated calls with the same name return the same signal. + + :param name: The name of the signal. + :param doc: The docstring of the signal. + """ + if name not in self: + self[name] = NamedSignal(name, doc) + + return self[name] + + +class _PNamespaceSignal(t.Protocol): + def __call__(self, name: str, doc: str | None = None) -> NamedSignal: ... + + +default_namespace: Namespace = Namespace() +"""A default :class:`Namespace` for creating named signals. :func:`signal` +creates a :class:`NamedSignal` in this namespace. +""" + +signal: _PNamespaceSignal = default_namespace.signal +"""Return a :class:`NamedSignal` in :data:`default_namespace` with the given +``name``, creating it if required. Repeated calls with the same name return the +same signal. +""" diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/py.typed b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/blinker/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/INSTALLER b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/INSTALLER new file mode 100644 index 0000000000..a1b589e38a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/INSTALLER @@ -0,0 +1 @@ +pip diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/METADATA b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/METADATA new file mode 100644 index 0000000000..30fbba6dd7 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/METADATA @@ -0,0 +1,84 @@ +Metadata-Version: 2.4 +Name: click +Version: 8.3.3 +Summary: Composable command line interface toolkit +Maintainer-email: Pallets +Requires-Python: >=3.10 +Description-Content-Type: text/markdown +License-Expression: BSD-3-Clause +Classifier: Development Status :: 5 - Production/Stable +Classifier: Intended Audience :: Developers +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Typing :: Typed +License-File: LICENSE.txt +Requires-Dist: colorama; platform_system == 'Windows' +Project-URL: Changes, https://click.palletsprojects.com/page/changes/ +Project-URL: Chat, https://discord.gg/pallets +Project-URL: Documentation, https://click.palletsprojects.com/ +Project-URL: Donate, https://palletsprojects.com/donate +Project-URL: Source, https://github.com/pallets/click/ + +
+ +# Click + +Click is a Python package for creating beautiful command line interfaces +in a composable way with as little code as necessary. It's the "Command +Line Interface Creation Kit". It's highly configurable but comes with +sensible defaults out of the box. + +It aims to make the process of writing command line tools quick and fun +while also preventing any frustration caused by the inability to +implement an intended CLI API. + +Click in three points: + +- Arbitrary nesting of commands +- Automatic help page generation +- Supports lazy loading of subcommands at runtime + + +## A Simple Example + +```python +import click + +@click.command() +@click.option("--count", default=1, help="Number of greetings.") +@click.option("--name", prompt="Your name", help="The person to greet.") +def hello(count, name): + """Simple program that greets NAME for a total of COUNT times.""" + for _ in range(count): + click.echo(f"Hello, {name}!") + +if __name__ == '__main__': + hello() +``` + +``` +$ python hello.py --count=3 +Your name: Click +Hello, Click! +Hello, Click! +Hello, Click! +``` + + +## Donate + +The Pallets organization develops and supports Click and other popular +packages. In order to grow the community of contributors and users, and +allow the maintainers to devote more time to the projects, [please +donate today][]. + +[please donate today]: https://palletsprojects.com/donate + +## Contributing + +See our [detailed contributing documentation][contrib] for many ways to +contribute, including reporting issues, requesting features, asking or answering +questions, and making PRs. + +[contrib]: https://palletsprojects.com/contributing/ + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/RECORD b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/RECORD new file mode 100644 index 0000000000..398e059ddd --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/RECORD @@ -0,0 +1,40 @@ +click-8.3.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 +click-8.3.3.dist-info/METADATA,sha256=_sjhT7_ZKYkyseqbWJtQA8eAcV2CeDJdcNrX_92pSCY,2621 +click-8.3.3.dist-info/RECORD,, +click-8.3.3.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82 +click-8.3.3.dist-info/licenses/LICENSE.txt,sha256=morRBqOU6FO_4h9C9OctWSgZoigF2ZG18ydQKSkrZY0,1475 +click/__init__.py,sha256=tq5olp0lVuUNbwOj0p5nMVdqhOyYz4aZzF8S5T0aFMU,4526 +click/__pycache__/__init__.cpython-311.pyc,, +click/__pycache__/_compat.cpython-311.pyc,, +click/__pycache__/_termui_impl.cpython-311.pyc,, +click/__pycache__/_textwrap.cpython-311.pyc,, +click/__pycache__/_utils.cpython-311.pyc,, +click/__pycache__/_winconsole.cpython-311.pyc,, +click/__pycache__/core.cpython-311.pyc,, +click/__pycache__/decorators.cpython-311.pyc,, +click/__pycache__/exceptions.cpython-311.pyc,, +click/__pycache__/formatting.cpython-311.pyc,, +click/__pycache__/globals.cpython-311.pyc,, +click/__pycache__/parser.cpython-311.pyc,, +click/__pycache__/shell_completion.cpython-311.pyc,, +click/__pycache__/termui.cpython-311.pyc,, +click/__pycache__/testing.cpython-311.pyc,, +click/__pycache__/types.cpython-311.pyc,, +click/__pycache__/utils.cpython-311.pyc,, +click/_compat.py,sha256=v3xBZkFbvA1BXPRkFfBJc6-pIwPI7345m-kQEnpVAs4,18693 +click/_termui_impl.py,sha256=R3aYjPD2x1xgNXiXEqHSR-jAVhbXKkLKeqgr-4eDOkA,28066 +click/_textwrap.py,sha256=BOae0RQ6vg3FkNgSJyOoGzG1meGMxJ_ukWVZKx_v-0o,1400 +click/_utils.py,sha256=kZwtTf5gMuCilJJceS2iTCvRvCY-0aN5rJq8gKw7p8g,943 +click/_winconsole.py,sha256=_vxUuUaxwBhoR0vUWCNuHY8VUefiMdCIyU2SXPqoF-A,8465 +click/core.py,sha256=wzDpH4Wh45hBfsdw3BHqn_095457WRrb92dyaeIGkHk,134478 +click/decorators.py,sha256=5P7abhJtAQYp_KHgjUvhMv464ERwOzrv2enNknlwHyQ,18461 +click/exceptions.py,sha256=8utf8w6V5hJXMnO_ic1FNrtbwuEn1NUu1aDwV8UqnG4,9954 +click/formatting.py,sha256=RVfwwr0rwWNpgGr8NaHodPzkIr7_tUyVh_nDdanLMNc,9730 +click/globals.py,sha256=gM-Nh6A4M0HB_SgkaF5M4ncGGMDHc_flHXu9_oh4GEU,1923 +click/parser.py,sha256=Q31pH0FlQZEq-UXE_ABRzlygEfvxPTuZbWNh4xfXmzw,19010 +click/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 +click/shell_completion.py,sha256=Cc4GQUFuWpfQBa9sF5qXeeYI7n3tI_1k6ZdSn4BZbT0,20994 +click/termui.py,sha256=0xjUaEZJz1_1y8eqon2kOe2OXrmHzZCJoVohFb43yTs,31434 +click/testing.py,sha256=PYLYW4ZjCAP6HRWtNhFpr3imJxQVEPFq3EW3JL0LTfA,22714 +click/types.py,sha256=ek54BNSFwPKsqtfT7jsqcc4WHui8AIFVMKM4oVZIXhc,39927 +click/utils.py,sha256=vep6qqMkv3WkzA9OCu7f6Ku-3beZHNpo8qJqL-uzUk8,20282 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/WHEEL b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/WHEEL new file mode 100644 index 0000000000..d8b9936dad --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/WHEEL @@ -0,0 +1,4 @@ +Wheel-Version: 1.0 +Generator: flit 3.12.0 +Root-Is-Purelib: true +Tag: py3-none-any diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/licenses/LICENSE.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/licenses/LICENSE.txt new file mode 100644 index 0000000000..d12a849186 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click-8.3.3.dist-info/licenses/LICENSE.txt @@ -0,0 +1,28 @@ +Copyright 2014 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__init__.py new file mode 100644 index 0000000000..fa485a0715 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__init__.py @@ -0,0 +1,124 @@ +""" +Click is a simple Python module inspired by the stdlib optparse to make +writing command line scripts fun. Unlike other modules, it's based +around a simple API that does not come with too much magic and is +composable. +""" + +from __future__ import annotations + +from .core import Argument as Argument +from .core import Command as Command +from .core import CommandCollection as CommandCollection +from .core import Context as Context +from .core import Group as Group +from .core import Option as Option +from .core import Parameter as Parameter +from .core import ParameterSource as ParameterSource +from .decorators import argument as argument +from .decorators import command as command +from .decorators import confirmation_option as confirmation_option +from .decorators import group as group +from .decorators import help_option as help_option +from .decorators import make_pass_decorator as make_pass_decorator +from .decorators import option as option +from .decorators import pass_context as pass_context +from .decorators import pass_obj as pass_obj +from .decorators import password_option as password_option +from .decorators import version_option as version_option +from .exceptions import Abort as Abort +from .exceptions import BadArgumentUsage as BadArgumentUsage +from .exceptions import BadOptionUsage as BadOptionUsage +from .exceptions import BadParameter as BadParameter +from .exceptions import ClickException as ClickException +from .exceptions import FileError as FileError +from .exceptions import MissingParameter as MissingParameter +from .exceptions import NoSuchOption as NoSuchOption +from .exceptions import UsageError as UsageError +from .formatting import HelpFormatter as HelpFormatter +from .formatting import wrap_text as wrap_text +from .globals import get_current_context as get_current_context +from .termui import clear as clear +from .termui import confirm as confirm +from .termui import echo_via_pager as echo_via_pager +from .termui import edit as edit +from .termui import getchar as getchar +from .termui import launch as launch +from .termui import pause as pause +from .termui import progressbar as progressbar +from .termui import prompt as prompt +from .termui import secho as secho +from .termui import style as style +from .termui import unstyle as unstyle +from .types import BOOL as BOOL +from .types import Choice as Choice +from .types import DateTime as DateTime +from .types import File as File +from .types import FLOAT as FLOAT +from .types import FloatRange as FloatRange +from .types import INT as INT +from .types import IntRange as IntRange +from .types import ParamType as ParamType +from .types import Path as Path +from .types import STRING as STRING +from .types import Tuple as Tuple +from .types import UNPROCESSED as UNPROCESSED +from .types import UUID as UUID +from .utils import echo as echo +from .utils import format_filename as format_filename +from .utils import get_app_dir as get_app_dir +from .utils import get_binary_stream as get_binary_stream +from .utils import get_text_stream as get_text_stream +from .utils import open_file as open_file + + +def __getattr__(name: str) -> object: + import warnings + + if name == "BaseCommand": + from .core import _BaseCommand + + warnings.warn( + "'BaseCommand' is deprecated and will be removed in Click 9.0. Use" + " 'Command' instead.", + DeprecationWarning, + stacklevel=2, + ) + return _BaseCommand + + if name == "MultiCommand": + from .core import _MultiCommand + + warnings.warn( + "'MultiCommand' is deprecated and will be removed in Click 9.0. Use" + " 'Group' instead.", + DeprecationWarning, + stacklevel=2, + ) + return _MultiCommand + + if name == "OptionParser": + from .parser import _OptionParser + + warnings.warn( + "'OptionParser' is deprecated and will be removed in Click 9.0. The" + " old parser is available in 'optparse'.", + DeprecationWarning, + stacklevel=2, + ) + return _OptionParser + + if name == "__version__": + import importlib.metadata + import warnings + + warnings.warn( + "The '__version__' attribute is deprecated and will be removed in" + " Click 9.1. Use feature detection or" + " 'importlib.metadata.version(\"click\")' instead.", + DeprecationWarning, + stacklevel=2, + ) + return importlib.metadata.version("click") + + raise AttributeError(name) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..7cb73e79e8 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_compat.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_compat.cpython-311.pyc new file mode 100644 index 0000000000..dc23e8f59b Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_compat.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_termui_impl.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_termui_impl.cpython-311.pyc new file mode 100644 index 0000000000..ca1a83e515 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_termui_impl.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_textwrap.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_textwrap.cpython-311.pyc new file mode 100644 index 0000000000..5c60adfaca Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_textwrap.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_utils.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_utils.cpython-311.pyc new file mode 100644 index 0000000000..6c173306eb Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_utils.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_winconsole.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_winconsole.cpython-311.pyc new file mode 100644 index 0000000000..fb9ef2ce52 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/_winconsole.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/core.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/core.cpython-311.pyc new file mode 100644 index 0000000000..6b5d10e301 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/core.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/decorators.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/decorators.cpython-311.pyc new file mode 100644 index 0000000000..8b0f1e196c Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/decorators.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/exceptions.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/exceptions.cpython-311.pyc new file mode 100644 index 0000000000..ee7681f102 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/exceptions.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/formatting.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/formatting.cpython-311.pyc new file mode 100644 index 0000000000..c2660c77b1 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/formatting.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/globals.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/globals.cpython-311.pyc new file mode 100644 index 0000000000..27161cb079 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/globals.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/parser.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/parser.cpython-311.pyc new file mode 100644 index 0000000000..113f0a8d65 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/parser.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/shell_completion.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/shell_completion.cpython-311.pyc new file mode 100644 index 0000000000..1b49fb5993 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/shell_completion.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/termui.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/termui.cpython-311.pyc new file mode 100644 index 0000000000..2a60151595 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/termui.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/testing.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/testing.cpython-311.pyc new file mode 100644 index 0000000000..9b7fa61ddc Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/testing.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/types.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/types.cpython-311.pyc new file mode 100644 index 0000000000..845240f82a Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/types.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/utils.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/utils.cpython-311.pyc new file mode 100644 index 0000000000..491466a0e4 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/__pycache__/utils.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_compat.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_compat.py new file mode 100644 index 0000000000..f2726b93af --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_compat.py @@ -0,0 +1,622 @@ +from __future__ import annotations + +import codecs +import collections.abc as cabc +import io +import os +import re +import sys +import typing as t +from types import TracebackType +from weakref import WeakKeyDictionary + +CYGWIN = sys.platform.startswith("cygwin") +WIN = sys.platform.startswith("win") +auto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None +_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]") + + +def _make_text_stream( + stream: t.BinaryIO, + encoding: str | None, + errors: str | None, + force_readable: bool = False, + force_writable: bool = False, +) -> t.TextIO: + if encoding is None: + encoding = get_best_encoding(stream) + if errors is None: + errors = "replace" + return _NonClosingTextIOWrapper( + stream, + encoding, + errors, + line_buffering=True, + force_readable=force_readable, + force_writable=force_writable, + ) + + +def is_ascii_encoding(encoding: str) -> bool: + """Checks if a given encoding is ascii.""" + try: + return codecs.lookup(encoding).name == "ascii" + except LookupError: + return False + + +def get_best_encoding(stream: t.IO[t.Any]) -> str: + """Returns the default stream encoding if not found.""" + rv = getattr(stream, "encoding", None) or sys.getdefaultencoding() + if is_ascii_encoding(rv): + return "utf-8" + return rv + + +class _NonClosingTextIOWrapper(io.TextIOWrapper): + def __init__( + self, + stream: t.BinaryIO, + encoding: str | None, + errors: str | None, + force_readable: bool = False, + force_writable: bool = False, + **extra: t.Any, + ) -> None: + self._stream = stream = t.cast( + t.BinaryIO, _FixupStream(stream, force_readable, force_writable) + ) + super().__init__(stream, encoding, errors, **extra) + + def __del__(self) -> None: + try: + self.detach() + except Exception: + pass + + def isatty(self) -> bool: + # https://bitbucket.org/pypy/pypy/issue/1803 + return self._stream.isatty() + + +class _FixupStream: + """The new io interface needs more from streams than streams + traditionally implement. As such, this fix-up code is necessary in + some circumstances. + + The forcing of readable and writable flags are there because some tools + put badly patched objects on sys (one such offender are certain version + of jupyter notebook). + """ + + def __init__( + self, + stream: t.BinaryIO, + force_readable: bool = False, + force_writable: bool = False, + ): + self._stream = stream + self._force_readable = force_readable + self._force_writable = force_writable + + def __getattr__(self, name: str) -> t.Any: + return getattr(self._stream, name) + + def read1(self, size: int) -> bytes: + f = getattr(self._stream, "read1", None) + + if f is not None: + return t.cast(bytes, f(size)) + + return self._stream.read(size) + + def readable(self) -> bool: + if self._force_readable: + return True + x = getattr(self._stream, "readable", None) + if x is not None: + return t.cast(bool, x()) + try: + self._stream.read(0) + except Exception: + return False + return True + + def writable(self) -> bool: + if self._force_writable: + return True + x = getattr(self._stream, "writable", None) + if x is not None: + return t.cast(bool, x()) + try: + self._stream.write(b"") + except Exception: + try: + self._stream.write(b"") + except Exception: + return False + return True + + def seekable(self) -> bool: + x = getattr(self._stream, "seekable", None) + if x is not None: + return t.cast(bool, x()) + try: + self._stream.seek(self._stream.tell()) + except Exception: + return False + return True + + +def _is_binary_reader(stream: t.IO[t.Any], default: bool = False) -> bool: + try: + return isinstance(stream.read(0), bytes) + except Exception: + return default + # This happens in some cases where the stream was already + # closed. In this case, we assume the default. + + +def _is_binary_writer(stream: t.IO[t.Any], default: bool = False) -> bool: + try: + stream.write(b"") + except Exception: + try: + stream.write("") + return False + except Exception: + pass + return default + return True + + +def _find_binary_reader(stream: t.IO[t.Any]) -> t.BinaryIO | None: + # We need to figure out if the given stream is already binary. + # This can happen because the official docs recommend detaching + # the streams to get binary streams. Some code might do this, so + # we need to deal with this case explicitly. + if _is_binary_reader(stream, False): + return t.cast(t.BinaryIO, stream) + + buf = getattr(stream, "buffer", None) + + # Same situation here; this time we assume that the buffer is + # actually binary in case it's closed. + if buf is not None and _is_binary_reader(buf, True): + return t.cast(t.BinaryIO, buf) + + return None + + +def _find_binary_writer(stream: t.IO[t.Any]) -> t.BinaryIO | None: + # We need to figure out if the given stream is already binary. + # This can happen because the official docs recommend detaching + # the streams to get binary streams. Some code might do this, so + # we need to deal with this case explicitly. + if _is_binary_writer(stream, False): + return t.cast(t.BinaryIO, stream) + + buf = getattr(stream, "buffer", None) + + # Same situation here; this time we assume that the buffer is + # actually binary in case it's closed. + if buf is not None and _is_binary_writer(buf, True): + return t.cast(t.BinaryIO, buf) + + return None + + +def _stream_is_misconfigured(stream: t.TextIO) -> bool: + """A stream is misconfigured if its encoding is ASCII.""" + # If the stream does not have an encoding set, we assume it's set + # to ASCII. This appears to happen in certain unittest + # environments. It's not quite clear what the correct behavior is + # but this at least will force Click to recover somehow. + return is_ascii_encoding(getattr(stream, "encoding", None) or "ascii") + + +def _is_compat_stream_attr(stream: t.TextIO, attr: str, value: str | None) -> bool: + """A stream attribute is compatible if it is equal to the + desired value or the desired value is unset and the attribute + has a value. + """ + stream_value = getattr(stream, attr, None) + return stream_value == value or (value is None and stream_value is not None) + + +def _is_compatible_text_stream( + stream: t.TextIO, encoding: str | None, errors: str | None +) -> bool: + """Check if a stream's encoding and errors attributes are + compatible with the desired values. + """ + return _is_compat_stream_attr( + stream, "encoding", encoding + ) and _is_compat_stream_attr(stream, "errors", errors) + + +def _force_correct_text_stream( + text_stream: t.IO[t.Any], + encoding: str | None, + errors: str | None, + is_binary: t.Callable[[t.IO[t.Any], bool], bool], + find_binary: t.Callable[[t.IO[t.Any]], t.BinaryIO | None], + force_readable: bool = False, + force_writable: bool = False, +) -> t.TextIO: + if is_binary(text_stream, False): + binary_reader = t.cast(t.BinaryIO, text_stream) + else: + text_stream = t.cast(t.TextIO, text_stream) + # If the stream looks compatible, and won't default to a + # misconfigured ascii encoding, return it as-is. + if _is_compatible_text_stream(text_stream, encoding, errors) and not ( + encoding is None and _stream_is_misconfigured(text_stream) + ): + return text_stream + + # Otherwise, get the underlying binary reader. + possible_binary_reader = find_binary(text_stream) + + # If that's not possible, silently use the original reader + # and get mojibake instead of exceptions. + if possible_binary_reader is None: + return text_stream + + binary_reader = possible_binary_reader + + # Default errors to replace instead of strict in order to get + # something that works. + if errors is None: + errors = "replace" + + # Wrap the binary stream in a text stream with the correct + # encoding parameters. + return _make_text_stream( + binary_reader, + encoding, + errors, + force_readable=force_readable, + force_writable=force_writable, + ) + + +def _force_correct_text_reader( + text_reader: t.IO[t.Any], + encoding: str | None, + errors: str | None, + force_readable: bool = False, +) -> t.TextIO: + return _force_correct_text_stream( + text_reader, + encoding, + errors, + _is_binary_reader, + _find_binary_reader, + force_readable=force_readable, + ) + + +def _force_correct_text_writer( + text_writer: t.IO[t.Any], + encoding: str | None, + errors: str | None, + force_writable: bool = False, +) -> t.TextIO: + return _force_correct_text_stream( + text_writer, + encoding, + errors, + _is_binary_writer, + _find_binary_writer, + force_writable=force_writable, + ) + + +def get_binary_stdin() -> t.BinaryIO: + reader = _find_binary_reader(sys.stdin) + if reader is None: + raise RuntimeError("Was not able to determine binary stream for sys.stdin.") + return reader + + +def get_binary_stdout() -> t.BinaryIO: + writer = _find_binary_writer(sys.stdout) + if writer is None: + raise RuntimeError("Was not able to determine binary stream for sys.stdout.") + return writer + + +def get_binary_stderr() -> t.BinaryIO: + writer = _find_binary_writer(sys.stderr) + if writer is None: + raise RuntimeError("Was not able to determine binary stream for sys.stderr.") + return writer + + +def get_text_stdin(encoding: str | None = None, errors: str | None = None) -> t.TextIO: + rv = _get_windows_console_stream(sys.stdin, encoding, errors) + if rv is not None: + return rv + return _force_correct_text_reader(sys.stdin, encoding, errors, force_readable=True) + + +def get_text_stdout(encoding: str | None = None, errors: str | None = None) -> t.TextIO: + rv = _get_windows_console_stream(sys.stdout, encoding, errors) + if rv is not None: + return rv + return _force_correct_text_writer(sys.stdout, encoding, errors, force_writable=True) + + +def get_text_stderr(encoding: str | None = None, errors: str | None = None) -> t.TextIO: + rv = _get_windows_console_stream(sys.stderr, encoding, errors) + if rv is not None: + return rv + return _force_correct_text_writer(sys.stderr, encoding, errors, force_writable=True) + + +def _wrap_io_open( + file: str | os.PathLike[str] | int, + mode: str, + encoding: str | None, + errors: str | None, +) -> t.IO[t.Any]: + """Handles not passing ``encoding`` and ``errors`` in binary mode.""" + if "b" in mode: + return open(file, mode) + + return open(file, mode, encoding=encoding, errors=errors) + + +def open_stream( + filename: str | os.PathLike[str], + mode: str = "r", + encoding: str | None = None, + errors: str | None = "strict", + atomic: bool = False, +) -> tuple[t.IO[t.Any], bool]: + binary = "b" in mode + filename = os.fspath(filename) + + # Standard streams first. These are simple because they ignore the + # atomic flag. Use fsdecode to handle Path("-"). + if os.fsdecode(filename) == "-": + if any(m in mode for m in ["w", "a", "x"]): + if binary: + return get_binary_stdout(), False + return get_text_stdout(encoding=encoding, errors=errors), False + if binary: + return get_binary_stdin(), False + return get_text_stdin(encoding=encoding, errors=errors), False + + # Non-atomic writes directly go out through the regular open functions. + if not atomic: + return _wrap_io_open(filename, mode, encoding, errors), True + + # Some usability stuff for atomic writes + if "a" in mode: + raise ValueError( + "Appending to an existing file is not supported, because that" + " would involve an expensive `copy`-operation to a temporary" + " file. Open the file in normal `w`-mode and copy explicitly" + " if that's what you're after." + ) + if "x" in mode: + raise ValueError("Use the `overwrite`-parameter instead.") + if "w" not in mode: + raise ValueError("Atomic writes only make sense with `w`-mode.") + + # Atomic writes are more complicated. They work by opening a file + # as a proxy in the same folder and then using the fdopen + # functionality to wrap it in a Python file. Then we wrap it in an + # atomic file that moves the file over on close. + import errno + import random + + try: + perm: int | None = os.stat(filename).st_mode + except OSError: + perm = None + + flags = os.O_RDWR | os.O_CREAT | os.O_EXCL + + if binary: + flags |= getattr(os, "O_BINARY", 0) + + while True: + tmp_filename = os.path.join( + os.path.dirname(filename), + f".__atomic-write{random.randrange(1 << 32):08x}", + ) + try: + fd = os.open(tmp_filename, flags, 0o666 if perm is None else perm) + break + except OSError as e: + if e.errno == errno.EEXIST or ( + os.name == "nt" + and e.errno == errno.EACCES + and os.path.isdir(e.filename) + and os.access(e.filename, os.W_OK) + ): + continue + raise + + if perm is not None: + os.chmod(tmp_filename, perm) # in case perm includes bits in umask + + f = _wrap_io_open(fd, mode, encoding, errors) + af = _AtomicFile(f, tmp_filename, os.path.realpath(filename)) + return t.cast(t.IO[t.Any], af), True + + +class _AtomicFile: + def __init__(self, f: t.IO[t.Any], tmp_filename: str, real_filename: str) -> None: + self._f = f + self._tmp_filename = tmp_filename + self._real_filename = real_filename + self.closed = False + + @property + def name(self) -> str: + return self._real_filename + + def close(self, delete: bool = False) -> None: + if self.closed: + return + self._f.close() + os.replace(self._tmp_filename, self._real_filename) + self.closed = True + + def __getattr__(self, name: str) -> t.Any: + return getattr(self._f, name) + + def __enter__(self) -> _AtomicFile: + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.close(delete=exc_type is not None) + + def __repr__(self) -> str: + return repr(self._f) + + +def strip_ansi(value: str) -> str: + return _ansi_re.sub("", value) + + +def _is_jupyter_kernel_output(stream: t.IO[t.Any]) -> bool: + while isinstance(stream, (_FixupStream, _NonClosingTextIOWrapper)): + stream = stream._stream + + return stream.__class__.__module__.startswith("ipykernel.") + + +def should_strip_ansi( + stream: t.IO[t.Any] | None = None, color: bool | None = None +) -> bool: + if color is None: + if stream is None: + stream = sys.stdin + return not isatty(stream) and not _is_jupyter_kernel_output(stream) + return not color + + +# On Windows, wrap the output streams with colorama to support ANSI +# color codes. +# NOTE: double check is needed so mypy does not analyze this on Linux +if sys.platform.startswith("win") and WIN: + from ._winconsole import _get_windows_console_stream + + def _get_argv_encoding() -> str: + import locale + + return locale.getpreferredencoding() + + _ansi_stream_wrappers: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary() + + def auto_wrap_for_ansi(stream: t.TextIO, color: bool | None = None) -> t.TextIO: + """Support ANSI color and style codes on Windows by wrapping a + stream with colorama. + """ + try: + cached = _ansi_stream_wrappers.get(stream) + except Exception: + cached = None + + if cached is not None: + return cached + + import colorama + + strip = should_strip_ansi(stream, color) + ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip) + rv = t.cast(t.TextIO, ansi_wrapper.stream) + _write = rv.write + + def _safe_write(s: str) -> int: + try: + return _write(s) + except BaseException: + ansi_wrapper.reset_all() + raise + + rv.write = _safe_write # type: ignore[method-assign] + + try: + _ansi_stream_wrappers[stream] = rv + except Exception: + pass + + return rv + +else: + + def _get_argv_encoding() -> str: + return getattr(sys.stdin, "encoding", None) or sys.getfilesystemencoding() + + def _get_windows_console_stream( + f: t.TextIO, encoding: str | None, errors: str | None + ) -> t.TextIO | None: + return None + + +def term_len(x: str) -> int: + return len(strip_ansi(x)) + + +def isatty(stream: t.IO[t.Any]) -> bool: + try: + return stream.isatty() + except Exception: + return False + + +def _make_cached_stream_func( + src_func: t.Callable[[], t.TextIO | None], + wrapper_func: t.Callable[[], t.TextIO], +) -> t.Callable[[], t.TextIO | None]: + cache: cabc.MutableMapping[t.TextIO, t.TextIO] = WeakKeyDictionary() + + def func() -> t.TextIO | None: + stream = src_func() + + if stream is None: + return None + + try: + rv = cache.get(stream) + except Exception: + rv = None + if rv is not None: + return rv + rv = wrapper_func() + try: + cache[stream] = rv + except Exception: + pass + return rv + + return func + + +_default_text_stdin = _make_cached_stream_func(lambda: sys.stdin, get_text_stdin) +_default_text_stdout = _make_cached_stream_func(lambda: sys.stdout, get_text_stdout) +_default_text_stderr = _make_cached_stream_func(lambda: sys.stderr, get_text_stderr) + + +binary_streams: cabc.Mapping[str, t.Callable[[], t.BinaryIO]] = { + "stdin": get_binary_stdin, + "stdout": get_binary_stdout, + "stderr": get_binary_stderr, +} + +text_streams: cabc.Mapping[str, t.Callable[[str | None, str | None], t.TextIO]] = { + "stdin": get_text_stdin, + "stdout": get_text_stdout, + "stderr": get_text_stderr, +} diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_termui_impl.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_termui_impl.py new file mode 100644 index 0000000000..c9f1e1c530 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_termui_impl.py @@ -0,0 +1,870 @@ +""" +This module contains implementations for the termui module. To keep the +import time of Click down, some infrequently used functionality is +placed in this module and only imported as needed. +""" + +from __future__ import annotations + +import collections.abc as cabc +import contextlib +import math +import os +import shlex +import sys +import time +import typing as t +from gettext import gettext as _ +from io import StringIO +from pathlib import Path +from types import TracebackType + +from ._compat import _default_text_stdout +from ._compat import CYGWIN +from ._compat import get_best_encoding +from ._compat import isatty +from ._compat import open_stream +from ._compat import strip_ansi +from ._compat import term_len +from ._compat import WIN +from .exceptions import ClickException +from .utils import echo + +V = t.TypeVar("V") + +if os.name == "nt": + BEFORE_BAR = "\r" + AFTER_BAR = "\n" +else: + BEFORE_BAR = "\r\033[?25l" + AFTER_BAR = "\033[?25h\n" + + +class ProgressBar(t.Generic[V]): + def __init__( + self, + iterable: cabc.Iterable[V] | None, + length: int | None = None, + fill_char: str = "#", + empty_char: str = " ", + bar_template: str = "%(bar)s", + info_sep: str = " ", + hidden: bool = False, + show_eta: bool = True, + show_percent: bool | None = None, + show_pos: bool = False, + item_show_func: t.Callable[[V | None], str | None] | None = None, + label: str | None = None, + file: t.TextIO | None = None, + color: bool | None = None, + update_min_steps: int = 1, + width: int = 30, + ) -> None: + self.fill_char = fill_char + self.empty_char = empty_char + self.bar_template = bar_template + self.info_sep = info_sep + self.hidden = hidden + self.show_eta = show_eta + self.show_percent = show_percent + self.show_pos = show_pos + self.item_show_func = item_show_func + self.label: str = label or "" + + if file is None: + file = _default_text_stdout() + + # There are no standard streams attached to write to. For example, + # pythonw on Windows. + if file is None: + file = StringIO() + + self.file = file + self.color = color + self.update_min_steps = update_min_steps + self._completed_intervals = 0 + self.width: int = width + self.autowidth: bool = width == 0 + + if length is None: + from operator import length_hint + + length = length_hint(iterable, -1) + + if length == -1: + length = None + if iterable is None: + if length is None: + raise TypeError("iterable or length is required") + iterable = t.cast("cabc.Iterable[V]", range(length)) + self.iter: cabc.Iterable[V] = iter(iterable) + self.length = length + self.pos: int = 0 + self.avg: list[float] = [] + self.last_eta: float + self.start: float + self.start = self.last_eta = time.time() + self.eta_known: bool = False + self.finished: bool = False + self.max_width: int | None = None + self.entered: bool = False + self.current_item: V | None = None + self._is_atty = isatty(self.file) + self._last_line: str | None = None + + def __enter__(self) -> ProgressBar[V]: + self.entered = True + self.render_progress() + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.render_finish() + + def __iter__(self) -> cabc.Iterator[V]: + if not self.entered: + raise RuntimeError("You need to use progress bars in a with block.") + self.render_progress() + return self.generator() + + def __next__(self) -> V: + # Iteration is defined in terms of a generator function, + # returned by iter(self); use that to define next(). This works + # because `self.iter` is an iterable consumed by that generator, + # so it is re-entry safe. Calling `next(self.generator())` + # twice works and does "what you want". + return next(iter(self)) + + def render_finish(self) -> None: + if self.hidden or not self._is_atty: + return + self.file.write(AFTER_BAR) + self.file.flush() + + @property + def pct(self) -> float: + if self.finished: + return 1.0 + return min(self.pos / (float(self.length or 1) or 1), 1.0) + + @property + def time_per_iteration(self) -> float: + if not self.avg: + return 0.0 + return sum(self.avg) / float(len(self.avg)) + + @property + def eta(self) -> float: + if self.length is not None and not self.finished: + return self.time_per_iteration * (self.length - self.pos) + return 0.0 + + def format_eta(self) -> str: + if self.eta_known: + t = int(self.eta) + seconds = t % 60 + t //= 60 + minutes = t % 60 + t //= 60 + hours = t % 24 + t //= 24 + if t > 0: + return f"{t}d {hours:02}:{minutes:02}:{seconds:02}" + else: + return f"{hours:02}:{minutes:02}:{seconds:02}" + return "" + + def format_pos(self) -> str: + pos = str(self.pos) + if self.length is not None: + pos += f"/{self.length}" + return pos + + def format_pct(self) -> str: + return f"{int(self.pct * 100): 4}%"[1:] + + def format_bar(self) -> str: + if self.length is not None: + bar_length = int(self.pct * self.width) + bar = self.fill_char * bar_length + bar += self.empty_char * (self.width - bar_length) + elif self.finished: + bar = self.fill_char * self.width + else: + chars = list(self.empty_char * (self.width or 1)) + if self.time_per_iteration != 0: + chars[ + int( + (math.cos(self.pos * self.time_per_iteration) / 2.0 + 0.5) + * self.width + ) + ] = self.fill_char + bar = "".join(chars) + return bar + + def format_progress_line(self) -> str: + show_percent = self.show_percent + + info_bits = [] + if self.length is not None and show_percent is None: + show_percent = not self.show_pos + + if self.show_pos: + info_bits.append(self.format_pos()) + if show_percent: + info_bits.append(self.format_pct()) + if self.show_eta and self.eta_known and not self.finished: + info_bits.append(self.format_eta()) + if self.item_show_func is not None: + item_info = self.item_show_func(self.current_item) + if item_info is not None: + info_bits.append(item_info) + + return ( + self.bar_template + % { + "label": self.label, + "bar": self.format_bar(), + "info": self.info_sep.join(info_bits), + } + ).rstrip() + + def render_progress(self) -> None: + if self.hidden: + return + + if not self._is_atty: + # Only output the label once if the output is not a TTY. + if self._last_line != self.label: + self._last_line = self.label + echo(self.label, file=self.file, color=self.color) + return + + buf = [] + # Update width in case the terminal has been resized + if self.autowidth: + import shutil + + old_width = self.width + self.width = 0 + clutter_length = term_len(self.format_progress_line()) + new_width = max(0, shutil.get_terminal_size().columns - clutter_length) + if new_width < old_width and self.max_width is not None: + buf.append(BEFORE_BAR) + buf.append(" " * self.max_width) + self.max_width = new_width + self.width = new_width + + clear_width = self.width + if self.max_width is not None: + clear_width = self.max_width + + buf.append(BEFORE_BAR) + line = self.format_progress_line() + line_len = term_len(line) + if self.max_width is None or self.max_width < line_len: + self.max_width = line_len + + buf.append(line) + buf.append(" " * (clear_width - line_len)) + line = "".join(buf) + # Render the line only if it changed. + + if line != self._last_line: + self._last_line = line + echo(line, file=self.file, color=self.color, nl=False) + self.file.flush() + + def make_step(self, n_steps: int) -> None: + self.pos += n_steps + if self.length is not None and self.pos >= self.length: + self.finished = True + + if (time.time() - self.last_eta) < 1.0: + return + + self.last_eta = time.time() + + # self.avg is a rolling list of length <= 7 of steps where steps are + # defined as time elapsed divided by the total progress through + # self.length. + if self.pos: + step = (time.time() - self.start) / self.pos + else: + step = time.time() - self.start + + self.avg = self.avg[-6:] + [step] + + self.eta_known = self.length is not None + + def update(self, n_steps: int, current_item: V | None = None) -> None: + """Update the progress bar by advancing a specified number of + steps, and optionally set the ``current_item`` for this new + position. + + :param n_steps: Number of steps to advance. + :param current_item: Optional item to set as ``current_item`` + for the updated position. + + .. versionchanged:: 8.0 + Added the ``current_item`` optional parameter. + + .. versionchanged:: 8.0 + Only render when the number of steps meets the + ``update_min_steps`` threshold. + """ + if current_item is not None: + self.current_item = current_item + + self._completed_intervals += n_steps + + if self._completed_intervals >= self.update_min_steps: + self.make_step(self._completed_intervals) + self.render_progress() + self._completed_intervals = 0 + + def finish(self) -> None: + self.eta_known = False + self.current_item = None + self.finished = True + + def generator(self) -> cabc.Iterator[V]: + """Return a generator which yields the items added to the bar + during construction, and updates the progress bar *after* the + yielded block returns. + """ + # WARNING: the iterator interface for `ProgressBar` relies on + # this and only works because this is a simple generator which + # doesn't create or manage additional state. If this function + # changes, the impact should be evaluated both against + # `iter(bar)` and `next(bar)`. `next()` in particular may call + # `self.generator()` repeatedly, and this must remain safe in + # order for that interface to work. + if not self.entered: + raise RuntimeError("You need to use progress bars in a with block.") + + if not self._is_atty: + yield from self.iter + else: + for rv in self.iter: + self.current_item = rv + + # This allows show_item_func to be updated before the + # item is processed. Only trigger at the beginning of + # the update interval. + if self._completed_intervals == 0: + self.render_progress() + + yield rv + self.update(1) + + self.finish() + self.render_progress() + + +def pager(generator: cabc.Iterable[str], color: bool | None = None) -> None: + """Decide what method to use for paging through text.""" + stdout = _default_text_stdout() + + # There are no standard streams attached to write to. For example, + # pythonw on Windows. + if stdout is None: + stdout = StringIO() + + if not isatty(sys.stdin) or not isatty(stdout): + return _nullpager(stdout, generator, color) + + # Split using POSIX mode (the default) so that quote characters are + # stripped from tokens and quoted Windows paths are preserved. + # Non-POSIX mode retains quotes in tokens, and wrapping tokens + # with shlex.quote re-introduces quoting issues on Windows. + pager_cmd_parts = shlex.split(os.environ.get("PAGER", "")) + if pager_cmd_parts: + if WIN: + if _tempfilepager(generator, pager_cmd_parts, color): + return + elif _pipepager(generator, pager_cmd_parts, color): + return + + if os.environ.get("TERM") in ("dumb", "emacs"): + return _nullpager(stdout, generator, color) + if (WIN or sys.platform.startswith("os2")) and _tempfilepager( + generator, ["more"], color + ): + return + if _pipepager(generator, ["less"], color): + return + + import tempfile + + fd, filename = tempfile.mkstemp() + os.close(fd) + try: + if _pipepager(generator, ["more"], color): + return + return _nullpager(stdout, generator, color) + finally: + os.unlink(filename) + + +def _pipepager( + generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None +) -> bool: + """Page through text by feeding it to another program. + + Invokes the pager via :class:`subprocess.Popen` with an ``argv`` list + produced by :func:`shlex.split`. The command is resolved to an absolute + path with :func:`shutil.which` as recommended by the + :mod:`subprocess` docs for Windows compatibility. + + Invoking a pager through this might support colors: if piping to + ``less`` and the user hasn't decided on colors, ``LESS=-R`` is set + automatically. + + Returns ``True`` if the command was found and executed, ``False`` + otherwise so another pager can be attempted. + """ + # Split the command into the invoked CLI and its parameters. + if not cmd_parts: + return False + + import shutil + + cmd = cmd_parts[0] + cmd_params = cmd_parts[1:] + + cmd_filepath = shutil.which(cmd) + if not cmd_filepath: + return False + + # Produces a normalized absolute path string. + # multi-call binaries such as busybox derive their identity from the symlink + # less -> busybox. resolve() causes them to misbehave. (eg. less becomes busybox) + cmd_path = Path(cmd_filepath).absolute() + cmd_name = cmd_path.name + + import subprocess + + # Make a local copy of the environment to not affect the global one. + env = dict(os.environ) + + # If we're piping to less and the user hasn't decided on colors, we enable + # them by default we find the -R flag in the command line arguments. + if color is None and cmd_name == "less": + less_flags = f"{os.environ.get('LESS', '')}{' '.join(cmd_params)}" + if not less_flags: + env["LESS"] = "-R" + color = True + elif "r" in less_flags or "R" in less_flags: + color = True + + c = subprocess.Popen( + [str(cmd_path)] + cmd_params, + shell=False, + stdin=subprocess.PIPE, + env=env, + errors="replace", + text=True, + ) + assert c.stdin is not None + try: + for text in generator: + if not color: + text = strip_ansi(text) + + c.stdin.write(text) + except BrokenPipeError: + # In case the pager exited unexpectedly, ignore the broken pipe error. + pass + except Exception as e: + # In case there is an exception we want to close the pager immediately + # and let the caller handle it. + # Otherwise the pager will keep running, and the user may not notice + # the error message, or worse yet it may leave the terminal in a broken state. + c.terminate() + raise e + finally: + # We must close stdin and wait for the pager to exit before we continue + try: + c.stdin.close() + # Close implies flush, so it might throw a BrokenPipeError if the pager + # process exited already. + except BrokenPipeError: + pass + + # Less doesn't respect ^C, but catches it for its own UI purposes (aborting + # search or other commands inside less). + # + # That means when the user hits ^C, the parent process (click) terminates, + # but less is still alive, paging the output and messing up the terminal. + # + # If the user wants to make the pager exit on ^C, they should set + # `LESS='-K'`. It's not our decision to make. + while True: + try: + c.wait() + except KeyboardInterrupt: + pass + else: + break + + return True + + +def _tempfilepager( + generator: cabc.Iterable[str], cmd_parts: list[str], color: bool | None +) -> bool: + """Page through text by invoking a program on a temporary file. + + Used as the primary pager strategy on Windows (where piping to + ``more`` adds spurious ``\\r\\n``), and as a fallback on other + platforms. The command is resolved to an absolute path with + :func:`shutil.which`. + + Returns ``True`` if the command was found and executed, ``False`` + otherwise so another pager can be attempted. + """ + # Split the command into the invoked CLI and its parameters. + if not cmd_parts: + return False + + import shutil + + cmd = cmd_parts[0] + + cmd_filepath = shutil.which(cmd) + if not cmd_filepath: + return False + # Produces a normalized absolute path string. + # multi-call binaries such as busybox derive their identity from the symlink + # less -> busybox. resolve() causes them to misbehave. (eg. less becomes busybox) + cmd_path = Path(cmd_filepath).absolute() + + import subprocess + import tempfile + + fd, filename = tempfile.mkstemp() + # TODO: This never terminates if the passed generator never terminates. + text = "".join(generator) + if not color: + text = strip_ansi(text) + encoding = get_best_encoding(sys.stdout) + with open_stream(filename, "wb")[0] as f: + f.write(text.encode(encoding)) + try: + subprocess.call([str(cmd_path), filename]) + except OSError: + # Command not found + pass + finally: + os.close(fd) + os.unlink(filename) + + return True + + +def _nullpager( + stream: t.TextIO, generator: cabc.Iterable[str], color: bool | None +) -> None: + """Simply print unformatted text. This is the ultimate fallback.""" + for text in generator: + if not color: + text = strip_ansi(text) + stream.write(text) + + +class Editor: + def __init__( + self, + editor: str | None = None, + env: cabc.Mapping[str, str] | None = None, + require_save: bool = True, + extension: str = ".txt", + ) -> None: + self.editor = editor + self.env = env + self.require_save = require_save + self.extension = extension + + def get_editor(self) -> str: + if self.editor is not None: + return self.editor + for key in "VISUAL", "EDITOR": + rv = os.environ.get(key) + if rv: + return rv + if WIN: + return "notepad" + + from shutil import which + + for editor in "sensible-editor", "vim", "nano": + if which(editor) is not None: + return editor + return "vi" + + def edit_files(self, filenames: cabc.Iterable[str]) -> None: + """Open files in the user's editor.""" + import shlex + import subprocess + + editor = self.get_editor() + environ: dict[str, str] | None = None + + if self.env: + environ = os.environ.copy() + environ.update(self.env) + + try: + # Split in POSIX mode (the default) for the same reasons as + # in pager(): strips quotes from tokens and preserves quoted + # Windows paths. + c = subprocess.Popen( + args=shlex.split(editor) + list(filenames), + env=environ, + ) + exit_code = c.wait() + if exit_code != 0: + raise ClickException( + _("{editor}: Editing failed").format(editor=editor) + ) + except OSError as e: + raise ClickException( + _("{editor}: Editing failed: {e}").format(editor=editor, e=e) + ) from e + + @t.overload + def edit(self, text: bytes | bytearray) -> bytes | None: ... + + # We cannot know whether or not the type expected is str or bytes when None + # is passed, so str is returned as that was what was done before. + @t.overload + def edit(self, text: str | None) -> str | None: ... + + def edit(self, text: str | bytes | bytearray | None) -> str | bytes | None: + import tempfile + + if text is None: + data: bytes | bytearray = b"" + elif isinstance(text, (bytes, bytearray)): + data = text + else: + if text and not text.endswith("\n"): + text += "\n" + + if WIN: + data = text.replace("\n", "\r\n").encode("utf-8-sig") + else: + data = text.encode("utf-8") + + fd, name = tempfile.mkstemp(prefix="editor-", suffix=self.extension) + f: t.BinaryIO + + try: + with os.fdopen(fd, "wb") as f: + f.write(data) + + # If the filesystem resolution is 1 second, like Mac OS + # 10.12 Extended, or 2 seconds, like FAT32, and the editor + # closes very fast, require_save can fail. Set the modified + # time to be 2 seconds in the past to work around this. + os.utime(name, (os.path.getatime(name), os.path.getmtime(name) - 2)) + # Depending on the resolution, the exact value might not be + # recorded, so get the new recorded value. + timestamp = os.path.getmtime(name) + + self.edit_files((name,)) + + if self.require_save and os.path.getmtime(name) == timestamp: + return None + + with open(name, "rb") as f: + rv = f.read() + + if isinstance(text, (bytes, bytearray)): + return rv + + return rv.decode("utf-8-sig").replace("\r\n", "\n") + finally: + os.unlink(name) + + +def open_url(url: str, wait: bool = False, locate: bool = False) -> int: + import subprocess + + def _unquote_file(url: str) -> str: + from urllib.parse import unquote + + if url.startswith("file://"): + url = unquote(url[7:]) + + return url + + if sys.platform == "darwin": + args = ["open"] + if wait: + args.append("-W") + if locate: + args.append("-R") + args.append(_unquote_file(url)) + null = open("/dev/null", "w") + try: + return subprocess.Popen(args, stderr=null).wait() + finally: + null.close() + elif WIN: + if locate: + url = _unquote_file(url) + args = ["explorer", f"/select,{url}"] + else: + args = ["start"] + if wait: + args.append("/WAIT") + args.append("") + args.append(url) + try: + return subprocess.call(args) + except OSError: + # Command not found + return 127 + elif CYGWIN: + if locate: + url = _unquote_file(url) + args = ["cygstart", os.path.dirname(url)] + else: + args = ["cygstart"] + if wait: + args.append("-w") + args.append(url) + try: + return subprocess.call(args) + except OSError: + # Command not found + return 127 + + try: + if locate: + url = os.path.dirname(_unquote_file(url)) or "." + else: + url = _unquote_file(url) + c = subprocess.Popen(["xdg-open", url]) + if wait: + return c.wait() + return 0 + except OSError: + if url.startswith(("http://", "https://")) and not locate and not wait: + import webbrowser + + webbrowser.open(url) + return 0 + return 1 + + +def _translate_ch_to_exc(ch: str) -> None: + if ch == "\x03": + raise KeyboardInterrupt() + + if ch == "\x04" and not WIN: # Unix-like, Ctrl+D + raise EOFError() + + if ch == "\x1a" and WIN: # Windows, Ctrl+Z + raise EOFError() + + +if sys.platform == "win32": + import msvcrt + + @contextlib.contextmanager + def raw_terminal() -> cabc.Iterator[int]: + yield -1 + + def getchar(echo: bool) -> str: + # The function `getch` will return a bytes object corresponding to + # the pressed character. Since Windows 10 build 1803, it will also + # return \x00 when called a second time after pressing a regular key. + # + # `getwch` does not share this probably-bugged behavior. Moreover, it + # returns a Unicode object by default, which is what we want. + # + # Either of these functions will return \x00 or \xe0 to indicate + # a special key, and you need to call the same function again to get + # the "rest" of the code. The fun part is that \u00e0 is + # "latin small letter a with grave", so if you type that on a French + # keyboard, you _also_ get a \xe0. + # E.g., consider the Up arrow. This returns \xe0 and then \x48. The + # resulting Unicode string reads as "a with grave" + "capital H". + # This is indistinguishable from when the user actually types + # "a with grave" and then "capital H". + # + # When \xe0 is returned, we assume it's part of a special-key sequence + # and call `getwch` again, but that means that when the user types + # the \u00e0 character, `getchar` doesn't return until a second + # character is typed. + # The alternative is returning immediately, but that would mess up + # cross-platform handling of arrow keys and others that start with + # \xe0. Another option is using `getch`, but then we can't reliably + # read non-ASCII characters, because return values of `getch` are + # limited to the current 8-bit codepage. + # + # Anyway, Click doesn't claim to do this Right(tm), and using `getwch` + # is doing the right thing in more situations than with `getch`. + + if echo: + func = t.cast(t.Callable[[], str], msvcrt.getwche) + else: + func = t.cast(t.Callable[[], str], msvcrt.getwch) + + rv = func() + + if rv in ("\x00", "\xe0"): + # \x00 and \xe0 are control characters that indicate special key, + # see above. + rv += func() + + _translate_ch_to_exc(rv) + return rv + +else: + import termios + import tty + + @contextlib.contextmanager + def raw_terminal() -> cabc.Iterator[int]: + f: t.TextIO | None + fd: int + + if not isatty(sys.stdin): + f = open("/dev/tty") + fd = f.fileno() + else: + fd = sys.stdin.fileno() + f = None + + try: + old_settings = termios.tcgetattr(fd) + + try: + tty.setraw(fd) + yield fd + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + sys.stdout.flush() + + if f is not None: + f.close() + except termios.error: + pass + + def getchar(echo: bool) -> str: + with raw_terminal() as fd: + ch = os.read(fd, 32).decode(get_best_encoding(sys.stdin), "replace") + + if echo and isatty(sys.stdout): + sys.stdout.write(ch) + + _translate_ch_to_exc(ch) + return ch diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_textwrap.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_textwrap.py new file mode 100644 index 0000000000..97fbee3dc6 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_textwrap.py @@ -0,0 +1,51 @@ +from __future__ import annotations + +import collections.abc as cabc +import textwrap +from contextlib import contextmanager + + +class TextWrapper(textwrap.TextWrapper): + def _handle_long_word( + self, + reversed_chunks: list[str], + cur_line: list[str], + cur_len: int, + width: int, + ) -> None: + space_left = max(width - cur_len, 1) + + if self.break_long_words: + last = reversed_chunks[-1] + cut = last[:space_left] + res = last[space_left:] + cur_line.append(cut) + reversed_chunks[-1] = res + elif not cur_line: + cur_line.append(reversed_chunks.pop()) + + @contextmanager + def extra_indent(self, indent: str) -> cabc.Iterator[None]: + old_initial_indent = self.initial_indent + old_subsequent_indent = self.subsequent_indent + self.initial_indent += indent + self.subsequent_indent += indent + + try: + yield + finally: + self.initial_indent = old_initial_indent + self.subsequent_indent = old_subsequent_indent + + def indent_only(self, text: str) -> str: + rv = [] + + for idx, line in enumerate(text.splitlines()): + indent = self.initial_indent + + if idx > 0: + indent = self.subsequent_indent + + rv.append(f"{indent}{line}") + + return "\n".join(rv) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_utils.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_utils.py new file mode 100644 index 0000000000..09fb00855e --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_utils.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import enum +import typing as t + + +class Sentinel(enum.Enum): + """Enum used to define sentinel values. + + .. seealso:: + + `PEP 661 - Sentinel Values `_. + """ + + UNSET = object() + FLAG_NEEDS_VALUE = object() + + def __repr__(self) -> str: + return f"{self.__class__.__name__}.{self.name}" + + +UNSET = Sentinel.UNSET +"""Sentinel used to indicate that a value is not set.""" + +FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE +"""Sentinel used to indicate an option was passed as a flag without a +value but is not a flag option. + +``Option.consume_value`` uses this to prompt or use the ``flag_value``. +""" + +T_UNSET = t.Literal[UNSET] # type: ignore[valid-type] +"""Type hint for the :data:`UNSET` sentinel value.""" + +T_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE] # type: ignore[valid-type] +"""Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value.""" diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_winconsole.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_winconsole.py new file mode 100644 index 0000000000..e56c7c6ae7 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/_winconsole.py @@ -0,0 +1,296 @@ +# This module is based on the excellent work by Adam Bartoš who +# provided a lot of what went into the implementation here in +# the discussion to issue1602 in the Python bug tracker. +# +# There are some general differences in regards to how this works +# compared to the original patches as we do not need to patch +# the entire interpreter but just work in our little world of +# echo and prompt. +from __future__ import annotations + +import collections.abc as cabc +import io +import sys +import time +import typing as t +from ctypes import Array +from ctypes import byref +from ctypes import c_char +from ctypes import c_char_p +from ctypes import c_int +from ctypes import c_ssize_t +from ctypes import c_ulong +from ctypes import c_void_p +from ctypes import POINTER +from ctypes import py_object +from ctypes import Structure +from ctypes.wintypes import DWORD +from ctypes.wintypes import HANDLE +from ctypes.wintypes import LPCWSTR +from ctypes.wintypes import LPWSTR + +from ._compat import _NonClosingTextIOWrapper + +assert sys.platform == "win32" +import msvcrt # noqa: E402 +from ctypes import windll # noqa: E402 +from ctypes import WINFUNCTYPE # noqa: E402 + +c_ssize_p = POINTER(c_ssize_t) + +kernel32 = windll.kernel32 +GetStdHandle = kernel32.GetStdHandle +ReadConsoleW = kernel32.ReadConsoleW +WriteConsoleW = kernel32.WriteConsoleW +GetConsoleMode = kernel32.GetConsoleMode +GetLastError = kernel32.GetLastError +GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32)) +CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))( + ("CommandLineToArgvW", windll.shell32) +) +LocalFree = WINFUNCTYPE(c_void_p, c_void_p)(("LocalFree", windll.kernel32)) + +STDIN_HANDLE = GetStdHandle(-10) +STDOUT_HANDLE = GetStdHandle(-11) +STDERR_HANDLE = GetStdHandle(-12) + +PyBUF_SIMPLE = 0 +PyBUF_WRITABLE = 1 + +ERROR_SUCCESS = 0 +ERROR_NOT_ENOUGH_MEMORY = 8 +ERROR_OPERATION_ABORTED = 995 + +STDIN_FILENO = 0 +STDOUT_FILENO = 1 +STDERR_FILENO = 2 + +EOF = b"\x1a" +MAX_BYTES_WRITTEN = 32767 + +if t.TYPE_CHECKING: + try: + # Using `typing_extensions.Buffer` instead of `collections.abc` + # on Windows for some reason does not have `Sized` implemented. + from collections.abc import Buffer # type: ignore + except ImportError: + from typing_extensions import Buffer + +try: + from ctypes import pythonapi +except ImportError: + # On PyPy we cannot get buffers so our ability to operate here is + # severely limited. + get_buffer = None +else: + + class Py_buffer(Structure): + _fields_ = [ # noqa: RUF012 + ("buf", c_void_p), + ("obj", py_object), + ("len", c_ssize_t), + ("itemsize", c_ssize_t), + ("readonly", c_int), + ("ndim", c_int), + ("format", c_char_p), + ("shape", c_ssize_p), + ("strides", c_ssize_p), + ("suboffsets", c_ssize_p), + ("internal", c_void_p), + ] + + PyObject_GetBuffer = pythonapi.PyObject_GetBuffer + PyBuffer_Release = pythonapi.PyBuffer_Release + + def get_buffer(obj: Buffer, writable: bool = False) -> Array[c_char]: + buf = Py_buffer() + flags: int = PyBUF_WRITABLE if writable else PyBUF_SIMPLE + PyObject_GetBuffer(py_object(obj), byref(buf), flags) + + try: + buffer_type = c_char * buf.len + out: Array[c_char] = buffer_type.from_address(buf.buf) + return out + finally: + PyBuffer_Release(byref(buf)) + + +class _WindowsConsoleRawIOBase(io.RawIOBase): + def __init__(self, handle: int | None) -> None: + self.handle = handle + + def isatty(self) -> t.Literal[True]: + super().isatty() + return True + + +class _WindowsConsoleReader(_WindowsConsoleRawIOBase): + def readable(self) -> t.Literal[True]: + return True + + def readinto(self, b: Buffer) -> int: + bytes_to_be_read = len(b) + if not bytes_to_be_read: + return 0 + elif bytes_to_be_read % 2: + raise ValueError( + "cannot read odd number of bytes from UTF-16-LE encoded console" + ) + + buffer = get_buffer(b, writable=True) + code_units_to_be_read = bytes_to_be_read // 2 + code_units_read = c_ulong() + + rv = ReadConsoleW( + HANDLE(self.handle), + buffer, + code_units_to_be_read, + byref(code_units_read), + None, + ) + if GetLastError() == ERROR_OPERATION_ABORTED: + # wait for KeyboardInterrupt + time.sleep(0.1) + if not rv: + raise OSError(f"Windows error: {GetLastError()}") + + if buffer[0] == EOF: + return 0 + return 2 * code_units_read.value + + +class _WindowsConsoleWriter(_WindowsConsoleRawIOBase): + def writable(self) -> t.Literal[True]: + return True + + @staticmethod + def _get_error_message(errno: int) -> str: + if errno == ERROR_SUCCESS: + return "ERROR_SUCCESS" + elif errno == ERROR_NOT_ENOUGH_MEMORY: + return "ERROR_NOT_ENOUGH_MEMORY" + return f"Windows error {errno}" + + def write(self, b: Buffer) -> int: + bytes_to_be_written = len(b) + buf = get_buffer(b) + code_units_to_be_written = min(bytes_to_be_written, MAX_BYTES_WRITTEN) // 2 + code_units_written = c_ulong() + + WriteConsoleW( + HANDLE(self.handle), + buf, + code_units_to_be_written, + byref(code_units_written), + None, + ) + bytes_written = 2 * code_units_written.value + + if bytes_written == 0 and bytes_to_be_written > 0: + raise OSError(self._get_error_message(GetLastError())) + return bytes_written + + +class ConsoleStream: + def __init__(self, text_stream: t.TextIO, byte_stream: t.BinaryIO) -> None: + self._text_stream = text_stream + self.buffer = byte_stream + + @property + def name(self) -> str: + return self.buffer.name + + def write(self, x: t.AnyStr) -> int: + if isinstance(x, str): + return self._text_stream.write(x) + try: + self.flush() + except Exception: + pass + return self.buffer.write(x) + + def writelines(self, lines: cabc.Iterable[t.AnyStr]) -> None: + for line in lines: + self.write(line) + + def __getattr__(self, name: str) -> t.Any: + return getattr(self._text_stream, name) + + def isatty(self) -> bool: + return self.buffer.isatty() + + def __repr__(self) -> str: + return f"" + + +def _get_text_stdin(buffer_stream: t.BinaryIO) -> t.TextIO: + text_stream = _NonClosingTextIOWrapper( + io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)), + "utf-16-le", + "strict", + line_buffering=True, + ) + return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream)) + + +def _get_text_stdout(buffer_stream: t.BinaryIO) -> t.TextIO: + text_stream = _NonClosingTextIOWrapper( + io.BufferedWriter(_WindowsConsoleWriter(STDOUT_HANDLE)), + "utf-16-le", + "strict", + line_buffering=True, + ) + return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream)) + + +def _get_text_stderr(buffer_stream: t.BinaryIO) -> t.TextIO: + text_stream = _NonClosingTextIOWrapper( + io.BufferedWriter(_WindowsConsoleWriter(STDERR_HANDLE)), + "utf-16-le", + "strict", + line_buffering=True, + ) + return t.cast(t.TextIO, ConsoleStream(text_stream, buffer_stream)) + + +_stream_factories: cabc.Mapping[int, t.Callable[[t.BinaryIO], t.TextIO]] = { + 0: _get_text_stdin, + 1: _get_text_stdout, + 2: _get_text_stderr, +} + + +def _is_console(f: t.TextIO) -> bool: + if not hasattr(f, "fileno"): + return False + + try: + fileno = f.fileno() + except (OSError, io.UnsupportedOperation): + return False + + handle = msvcrt.get_osfhandle(fileno) + return bool(GetConsoleMode(handle, byref(DWORD()))) + + +def _get_windows_console_stream( + f: t.TextIO, encoding: str | None, errors: str | None +) -> t.TextIO | None: + if ( + get_buffer is None + or encoding not in {"utf-16-le", None} + or errors not in {"strict", None} + or not _is_console(f) + ): + return None + + func = _stream_factories.get(f.fileno()) + if func is None: + return None + + b = getattr(f, "buffer", None) + + if b is None: + return None + + return func(b) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/core.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/core.py new file mode 100644 index 0000000000..d940dd80e1 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/core.py @@ -0,0 +1,3476 @@ +from __future__ import annotations + +import collections.abc as cabc +import enum +import errno +import inspect +import os +import sys +import typing as t +from collections import abc +from collections import Counter +from contextlib import AbstractContextManager +from contextlib import contextmanager +from contextlib import ExitStack +from functools import update_wrapper +from gettext import gettext as _ +from gettext import ngettext +from itertools import repeat +from types import TracebackType + +from . import types +from ._utils import FLAG_NEEDS_VALUE +from ._utils import UNSET +from .exceptions import Abort +from .exceptions import BadParameter +from .exceptions import ClickException +from .exceptions import Exit +from .exceptions import MissingParameter +from .exceptions import NoArgsIsHelpError +from .exceptions import UsageError +from .formatting import HelpFormatter +from .formatting import join_options +from .globals import pop_context +from .globals import push_context +from .parser import _OptionParser +from .parser import _split_opt +from .termui import confirm +from .termui import prompt +from .termui import style +from .utils import _detect_program_name +from .utils import _expand_args +from .utils import echo +from .utils import make_default_short_help +from .utils import make_str +from .utils import PacifyFlushWrapper + +if t.TYPE_CHECKING: + from .shell_completion import CompletionItem + +F = t.TypeVar("F", bound="t.Callable[..., t.Any]") +V = t.TypeVar("V") + + +def _complete_visible_commands( + ctx: Context, incomplete: str +) -> cabc.Iterator[tuple[str, Command]]: + """List all the subcommands of a group that start with the + incomplete value and aren't hidden. + + :param ctx: Invocation context for the group. + :param incomplete: Value being completed. May be empty. + """ + multi = t.cast(Group, ctx.command) + + for name in multi.list_commands(ctx): + if name.startswith(incomplete): + command = multi.get_command(ctx, name) + + if command is not None and not command.hidden: + yield name, command + + +def _check_nested_chain( + base_command: Group, cmd_name: str, cmd: Command, register: bool = False +) -> None: + if not base_command.chain or not isinstance(cmd, Group): + return + + if register: + message = ( + f"It is not possible to add the group {cmd_name!r} to another" + f" group {base_command.name!r} that is in chain mode." + ) + else: + message = ( + f"Found the group {cmd_name!r} as subcommand to another group " + f" {base_command.name!r} that is in chain mode. This is not supported." + ) + + raise RuntimeError(message) + + +def batch(iterable: cabc.Iterable[V], batch_size: int) -> list[tuple[V, ...]]: + return list(zip(*repeat(iter(iterable), batch_size), strict=False)) + + +@contextmanager +def augment_usage_errors( + ctx: Context, param: Parameter | None = None +) -> cabc.Iterator[None]: + """Context manager that attaches extra information to exceptions.""" + try: + yield + except BadParameter as e: + if e.ctx is None: + e.ctx = ctx + if param is not None and e.param is None: + e.param = param + raise + except UsageError as e: + if e.ctx is None: + e.ctx = ctx + raise + + +def iter_params_for_processing( + invocation_order: cabc.Sequence[Parameter], + declaration_order: cabc.Sequence[Parameter], +) -> list[Parameter]: + """Returns all declared parameters in the order they should be processed. + + The declared parameters are re-shuffled depending on the order in which + they were invoked, as well as the eagerness of each parameters. + + The invocation order takes precedence over the declaration order. I.e. the + order in which the user provided them to the CLI is respected. + + This behavior and its effect on callback evaluation is detailed at: + https://click.palletsprojects.com/en/stable/advanced/#callback-evaluation-order + """ + + def sort_key(item: Parameter) -> tuple[bool, float]: + try: + idx: float = invocation_order.index(item) + except ValueError: + idx = float("inf") + + return not item.is_eager, idx + + return sorted(declaration_order, key=sort_key) + + +class ParameterSource(enum.IntEnum): + """This is an :class:`~enum.IntEnum` that indicates the source of a + parameter's value. + + Use :meth:`click.Context.get_parameter_source` to get the + source for a parameter by name. + + Members are ordered from most explicit to least explicit source. + This allows comparison to check if a value was explicitly provided: + + .. code-block:: python + + source = ctx.get_parameter_source("port") + if source < click.ParameterSource.DEFAULT_MAP: + ... # value was explicitly set + + .. versionchanged:: 8.3.3 + Use :class:`~enum.IntEnum` and reorder members from most to + least explicit. Supports comparison operators. + + .. versionchanged:: 8.0 + Use :class:`~enum.Enum` and drop the ``validate`` method. + + .. versionchanged:: 8.0 + Added the ``PROMPT`` value. + """ + + PROMPT = enum.auto() + """Used a prompt to confirm a default or provide a value.""" + COMMANDLINE = enum.auto() + """The value was provided by the command line args.""" + ENVIRONMENT = enum.auto() + """The value was provided with an environment variable.""" + DEFAULT_MAP = enum.auto() + """Used a default provided by :attr:`Context.default_map`.""" + DEFAULT = enum.auto() + """Used the default specified by the parameter.""" + + +class Context: + """The context is a special internal object that holds state relevant + for the script execution at every single level. It's normally invisible + to commands unless they opt-in to getting access to it. + + The context is useful as it can pass internal objects around and can + control special execution features such as reading data from + environment variables. + + A context can be used as context manager in which case it will call + :meth:`close` on teardown. + + :param command: the command class for this context. + :param parent: the parent context. + :param info_name: the info name for this invocation. Generally this + is the most descriptive name for the script or + command. For the toplevel script it is usually + the name of the script, for commands below it it's + the name of the script. + :param obj: an arbitrary object of user data. + :param auto_envvar_prefix: the prefix to use for automatic environment + variables. If this is `None` then reading + from environment variables is disabled. This + does not affect manually set environment + variables which are always read. + :param default_map: a dictionary (like object) with default values + for parameters. + :param terminal_width: the width of the terminal. The default is + inherit from parent context. If no context + defines the terminal width then auto + detection will be applied. + :param max_content_width: the maximum width for content rendered by + Click (this currently only affects help + pages). This defaults to 80 characters if + not overridden. In other words: even if the + terminal is larger than that, Click will not + format things wider than 80 characters by + default. In addition to that, formatters might + add some safety mapping on the right. + :param resilient_parsing: if this flag is enabled then Click will + parse without any interactivity or callback + invocation. Default values will also be + ignored. This is useful for implementing + things such as completion support. + :param allow_extra_args: if this is set to `True` then extra arguments + at the end will not raise an error and will be + kept on the context. The default is to inherit + from the command. + :param allow_interspersed_args: if this is set to `False` then options + and arguments cannot be mixed. The + default is to inherit from the command. + :param ignore_unknown_options: instructs click to ignore options it does + not know and keeps them for later + processing. + :param help_option_names: optionally a list of strings that define how + the default help parameter is named. The + default is ``['--help']``. + :param token_normalize_func: an optional function that is used to + normalize tokens (options, choices, + etc.). This for instance can be used to + implement case insensitive behavior. + :param color: controls if the terminal supports ANSI colors or not. The + default is autodetection. This is only needed if ANSI + codes are used in texts that Click prints which is by + default not the case. This for instance would affect + help output. + :param show_default: Show the default value for commands. If this + value is not set, it defaults to the value from the parent + context. ``Command.show_default`` overrides this default for the + specific command. + + .. versionchanged:: 8.2 + The ``protected_args`` attribute is deprecated and will be removed in + Click 9.0. ``args`` will contain remaining unparsed tokens. + + .. versionchanged:: 8.1 + The ``show_default`` parameter is overridden by + ``Command.show_default``, instead of the other way around. + + .. versionchanged:: 8.0 + The ``show_default`` parameter defaults to the value from the + parent context. + + .. versionchanged:: 7.1 + Added the ``show_default`` parameter. + + .. versionchanged:: 4.0 + Added the ``color``, ``ignore_unknown_options``, and + ``max_content_width`` parameters. + + .. versionchanged:: 3.0 + Added the ``allow_extra_args`` and ``allow_interspersed_args`` + parameters. + + .. versionchanged:: 2.0 + Added the ``resilient_parsing``, ``help_option_names``, and + ``token_normalize_func`` parameters. + """ + + #: The formatter class to create with :meth:`make_formatter`. + #: + #: .. versionadded:: 8.0 + formatter_class: type[HelpFormatter] = HelpFormatter + + def __init__( + self, + command: Command, + parent: Context | None = None, + info_name: str | None = None, + obj: t.Any | None = None, + auto_envvar_prefix: str | None = None, + default_map: cabc.MutableMapping[str, t.Any] | None = None, + terminal_width: int | None = None, + max_content_width: int | None = None, + resilient_parsing: bool = False, + allow_extra_args: bool | None = None, + allow_interspersed_args: bool | None = None, + ignore_unknown_options: bool | None = None, + help_option_names: list[str] | None = None, + token_normalize_func: t.Callable[[str], str] | None = None, + color: bool | None = None, + show_default: bool | None = None, + ) -> None: + #: the parent context or `None` if none exists. + self.parent = parent + #: the :class:`Command` for this context. + self.command = command + #: the descriptive information name + self.info_name = info_name + #: Map of parameter names to their parsed values. Parameters + #: with ``expose_value=False`` are not stored. + self.params: dict[str, t.Any] = {} + #: the leftover arguments. + self.args: list[str] = [] + #: protected arguments. These are arguments that are prepended + #: to `args` when certain parsing scenarios are encountered but + #: must be never propagated to another arguments. This is used + #: to implement nested parsing. + self._protected_args: list[str] = [] + #: the collected prefixes of the command's options. + self._opt_prefixes: set[str] = set(parent._opt_prefixes) if parent else set() + + if obj is None and parent is not None: + obj = parent.obj + + #: the user object stored. + self.obj: t.Any = obj + self._meta: dict[str, t.Any] = getattr(parent, "meta", {}) + + #: A dictionary (-like object) with defaults for parameters. + if ( + default_map is None + and info_name is not None + and parent is not None + and parent.default_map is not None + ): + default_map = parent.default_map.get(info_name) + + self.default_map: cabc.MutableMapping[str, t.Any] | None = default_map + + #: This flag indicates if a subcommand is going to be executed. A + #: group callback can use this information to figure out if it's + #: being executed directly or because the execution flow passes + #: onwards to a subcommand. By default it's None, but it can be + #: the name of the subcommand to execute. + #: + #: If chaining is enabled this will be set to ``'*'`` in case + #: any commands are executed. It is however not possible to + #: figure out which ones. If you require this knowledge you + #: should use a :func:`result_callback`. + self.invoked_subcommand: str | None = None + + if terminal_width is None and parent is not None: + terminal_width = parent.terminal_width + + #: The width of the terminal (None is autodetection). + self.terminal_width: int | None = terminal_width + + if max_content_width is None and parent is not None: + max_content_width = parent.max_content_width + + #: The maximum width of formatted content (None implies a sensible + #: default which is 80 for most things). + self.max_content_width: int | None = max_content_width + + if allow_extra_args is None: + allow_extra_args = command.allow_extra_args + + #: Indicates if the context allows extra args or if it should + #: fail on parsing. + #: + #: .. versionadded:: 3.0 + self.allow_extra_args = allow_extra_args + + if allow_interspersed_args is None: + allow_interspersed_args = command.allow_interspersed_args + + #: Indicates if the context allows mixing of arguments and + #: options or not. + #: + #: .. versionadded:: 3.0 + self.allow_interspersed_args: bool = allow_interspersed_args + + if ignore_unknown_options is None: + ignore_unknown_options = command.ignore_unknown_options + + #: Instructs click to ignore options that a command does not + #: understand and will store it on the context for later + #: processing. This is primarily useful for situations where you + #: want to call into external programs. Generally this pattern is + #: strongly discouraged because it's not possibly to losslessly + #: forward all arguments. + #: + #: .. versionadded:: 4.0 + self.ignore_unknown_options: bool = ignore_unknown_options + + if help_option_names is None: + if parent is not None: + help_option_names = parent.help_option_names + else: + help_option_names = ["--help"] + + #: The names for the help options. + self.help_option_names: list[str] = help_option_names + + if token_normalize_func is None and parent is not None: + token_normalize_func = parent.token_normalize_func + + #: An optional normalization function for tokens. This is + #: options, choices, commands etc. + self.token_normalize_func: t.Callable[[str], str] | None = token_normalize_func + + #: Indicates if resilient parsing is enabled. In that case Click + #: will do its best to not cause any failures and default values + #: will be ignored. Useful for completion. + self.resilient_parsing: bool = resilient_parsing + + # If there is no envvar prefix yet, but the parent has one and + # the command on this level has a name, we can expand the envvar + # prefix automatically. + if auto_envvar_prefix is None: + if ( + parent is not None + and parent.auto_envvar_prefix is not None + and self.info_name is not None + ): + auto_envvar_prefix = ( + f"{parent.auto_envvar_prefix}_{self.info_name.upper()}" + ) + else: + auto_envvar_prefix = auto_envvar_prefix.upper() + + if auto_envvar_prefix is not None: + auto_envvar_prefix = auto_envvar_prefix.replace("-", "_") + + self.auto_envvar_prefix: str | None = auto_envvar_prefix + + if color is None and parent is not None: + color = parent.color + + #: Controls if styling output is wanted or not. + self.color: bool | None = color + + if show_default is None and parent is not None: + show_default = parent.show_default + + #: Show option default values when formatting help text. + self.show_default: bool | None = show_default + + self._close_callbacks: list[t.Callable[[], t.Any]] = [] + self._depth = 0 + self._parameter_source: dict[str, ParameterSource] = {} + self._exit_stack = ExitStack() + + @property + def protected_args(self) -> list[str]: + import warnings + + warnings.warn( + "'protected_args' is deprecated and will be removed in Click 9.0." + " 'args' will contain remaining unparsed tokens.", + DeprecationWarning, + stacklevel=2, + ) + return self._protected_args + + def to_info_dict(self) -> dict[str, t.Any]: + """Gather information that could be useful for a tool generating + user-facing documentation. This traverses the entire CLI + structure. + + .. code-block:: python + + with Context(cli) as ctx: + info = ctx.to_info_dict() + + .. versionadded:: 8.0 + """ + return { + "command": self.command.to_info_dict(self), + "info_name": self.info_name, + "allow_extra_args": self.allow_extra_args, + "allow_interspersed_args": self.allow_interspersed_args, + "ignore_unknown_options": self.ignore_unknown_options, + "auto_envvar_prefix": self.auto_envvar_prefix, + } + + def __enter__(self) -> Context: + self._depth += 1 + push_context(self) + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> bool | None: + self._depth -= 1 + exit_result: bool | None = None + if self._depth == 0: + exit_result = self._close_with_exception_info(exc_type, exc_value, tb) + pop_context() + + return exit_result + + @contextmanager + def scope(self, cleanup: bool = True) -> cabc.Iterator[Context]: + """This helper method can be used with the context object to promote + it to the current thread local (see :func:`get_current_context`). + The default behavior of this is to invoke the cleanup functions which + can be disabled by setting `cleanup` to `False`. The cleanup + functions are typically used for things such as closing file handles. + + If the cleanup is intended the context object can also be directly + used as a context manager. + + Example usage:: + + with ctx.scope(): + assert get_current_context() is ctx + + This is equivalent:: + + with ctx: + assert get_current_context() is ctx + + .. versionadded:: 5.0 + + :param cleanup: controls if the cleanup functions should be run or + not. The default is to run these functions. In + some situations the context only wants to be + temporarily pushed in which case this can be disabled. + Nested pushes automatically defer the cleanup. + """ + if not cleanup: + self._depth += 1 + try: + with self as rv: + yield rv + finally: + if not cleanup: + self._depth -= 1 + + @property + def meta(self) -> dict[str, t.Any]: + """This is a dictionary which is shared with all the contexts + that are nested. It exists so that click utilities can store some + state here if they need to. It is however the responsibility of + that code to manage this dictionary well. + + The keys are supposed to be unique dotted strings. For instance + module paths are a good choice for it. What is stored in there is + irrelevant for the operation of click. However what is important is + that code that places data here adheres to the general semantics of + the system. + + Example usage:: + + LANG_KEY = f'{__name__}.lang' + + def set_language(value): + ctx = get_current_context() + ctx.meta[LANG_KEY] = value + + def get_language(): + return get_current_context().meta.get(LANG_KEY, 'en_US') + + .. versionadded:: 5.0 + """ + return self._meta + + def make_formatter(self) -> HelpFormatter: + """Creates the :class:`~click.HelpFormatter` for the help and + usage output. + + To quickly customize the formatter class used without overriding + this method, set the :attr:`formatter_class` attribute. + + .. versionchanged:: 8.0 + Added the :attr:`formatter_class` attribute. + """ + return self.formatter_class( + width=self.terminal_width, max_width=self.max_content_width + ) + + def with_resource(self, context_manager: AbstractContextManager[V]) -> V: + """Register a resource as if it were used in a ``with`` + statement. The resource will be cleaned up when the context is + popped. + + Uses :meth:`contextlib.ExitStack.enter_context`. It calls the + resource's ``__enter__()`` method and returns the result. When + the context is popped, it closes the stack, which calls the + resource's ``__exit__()`` method. + + To register a cleanup function for something that isn't a + context manager, use :meth:`call_on_close`. Or use something + from :mod:`contextlib` to turn it into a context manager first. + + .. code-block:: python + + @click.group() + @click.option("--name") + @click.pass_context + def cli(ctx): + ctx.obj = ctx.with_resource(connect_db(name)) + + :param context_manager: The context manager to enter. + :return: Whatever ``context_manager.__enter__()`` returns. + + .. versionadded:: 8.0 + """ + return self._exit_stack.enter_context(context_manager) + + def call_on_close(self, f: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]: + """Register a function to be called when the context tears down. + + This can be used to close resources opened during the script + execution. Resources that support Python's context manager + protocol which would be used in a ``with`` statement should be + registered with :meth:`with_resource` instead. + + :param f: The function to execute on teardown. + """ + return self._exit_stack.callback(f) + + def close(self) -> None: + """Invoke all close callbacks registered with + :meth:`call_on_close`, and exit all context managers entered + with :meth:`with_resource`. + """ + self._close_with_exception_info(None, None, None) + + def _close_with_exception_info( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> bool | None: + """Unwind the exit stack by calling its :meth:`__exit__` providing the exception + information to allow for exception handling by the various resources registered + using :meth;`with_resource` + + :return: Whatever ``exit_stack.__exit__()`` returns. + """ + exit_result = self._exit_stack.__exit__(exc_type, exc_value, tb) + # In case the context is reused, create a new exit stack. + self._exit_stack = ExitStack() + + return exit_result + + @property + def command_path(self) -> str: + """The computed command path. This is used for the ``usage`` + information on the help page. It's automatically created by + combining the info names of the chain of contexts to the root. + """ + rv = "" + if self.info_name is not None: + rv = self.info_name + if self.parent is not None: + parent_command_path = [self.parent.command_path] + + if isinstance(self.parent.command, Command): + for param in self.parent.command.get_params(self): + parent_command_path.extend(param.get_usage_pieces(self)) + + rv = f"{' '.join(parent_command_path)} {rv}" + return rv.lstrip() + + def find_root(self) -> Context: + """Finds the outermost context.""" + node = self + while node.parent is not None: + node = node.parent + return node + + def find_object(self, object_type: type[V]) -> V | None: + """Finds the closest object of a given type.""" + node: Context | None = self + + while node is not None: + if isinstance(node.obj, object_type): + return node.obj + + node = node.parent + + return None + + def ensure_object(self, object_type: type[V]) -> V: + """Like :meth:`find_object` but sets the innermost object to a + new instance of `object_type` if it does not exist. + """ + rv = self.find_object(object_type) + if rv is None: + self.obj = rv = object_type() + return rv + + def _default_map_has(self, name: str | None) -> bool: + """Check if :attr:`default_map` contains a real value for ``name``. + + Returns ``False`` when the key is absent, the map is ``None``, + ``name`` is ``None``, or the stored value is the internal + :data:`UNSET` sentinel. + """ + return ( + name is not None + and self.default_map is not None + and name in self.default_map + and self.default_map[name] is not UNSET + ) + + @t.overload + def lookup_default( + self, name: str, call: t.Literal[True] = True + ) -> t.Any | None: ... + + @t.overload + def lookup_default( + self, name: str, call: t.Literal[False] = ... + ) -> t.Any | t.Callable[[], t.Any] | None: ... + + def lookup_default(self, name: str, call: bool = True) -> t.Any | None: + """Get the default for a parameter from :attr:`default_map`. + + :param name: Name of the parameter. + :param call: If the default is a callable, call it. Disable to + return the callable instead. + + .. versionchanged:: 8.0 + Added the ``call`` parameter. + """ + if not self._default_map_has(name): + return None + + # Assert to make the type checker happy. + assert self.default_map is not None + value = self.default_map[name] + + if call and callable(value): + return value() + + return value + + def fail(self, message: str) -> t.NoReturn: + """Aborts the execution of the program with a specific error + message. + + :param message: the error message to fail with. + """ + raise UsageError(message, self) + + def abort(self) -> t.NoReturn: + """Aborts the script.""" + raise Abort() + + def exit(self, code: int = 0) -> t.NoReturn: + """Exits the application with a given exit code. + + .. versionchanged:: 8.2 + Callbacks and context managers registered with :meth:`call_on_close` + and :meth:`with_resource` are closed before exiting. + """ + self.close() + raise Exit(code) + + def get_usage(self) -> str: + """Helper method to get formatted usage string for the current + context and command. + """ + return self.command.get_usage(self) + + def get_help(self) -> str: + """Helper method to get formatted help page for the current + context and command. + """ + return self.command.get_help(self) + + def _make_sub_context(self, command: Command) -> Context: + """Create a new context of the same type as this context, but + for a new command. + + :meta private: + """ + return type(self)(command, info_name=command.name, parent=self) + + @t.overload + def invoke( + self, callback: t.Callable[..., V], /, *args: t.Any, **kwargs: t.Any + ) -> V: ... + + @t.overload + def invoke(self, callback: Command, /, *args: t.Any, **kwargs: t.Any) -> t.Any: ... + + def invoke( + self, callback: Command | t.Callable[..., V], /, *args: t.Any, **kwargs: t.Any + ) -> t.Any | V: + """Invokes a command callback in exactly the way it expects. There + are two ways to invoke this method: + + 1. the first argument can be a callback and all other arguments and + keyword arguments are forwarded directly to the function. + 2. the first argument is a click command object. In that case all + arguments are forwarded as well but proper click parameters + (options and click arguments) must be keyword arguments and Click + will fill in defaults. + + .. versionchanged:: 8.0 + All ``kwargs`` are tracked in :attr:`params` so they will be + passed if :meth:`forward` is called at multiple levels. + + .. versionchanged:: 3.2 + A new context is created, and missing arguments use default values. + """ + if isinstance(callback, Command): + other_cmd = callback + + if other_cmd.callback is None: + raise TypeError( + "The given command does not have a callback that can be invoked." + ) + else: + callback = t.cast("t.Callable[..., V]", other_cmd.callback) + + ctx = self._make_sub_context(other_cmd) + + for param in other_cmd.params: + if param.name not in kwargs and param.expose_value: + default_value = param.get_default(ctx) + # We explicitly hide the :attr:`UNSET` value to the user, as we + # choose to make it an implementation detail. And because ``invoke`` + # has been designed as part of Click public API, we return ``None`` + # instead. Refs: + # https://github.com/pallets/click/issues/3066 + # https://github.com/pallets/click/issues/3065 + # https://github.com/pallets/click/pull/3068 + if default_value is UNSET: + default_value = None + kwargs[param.name] = param.type_cast_value( # type: ignore + ctx, default_value + ) + + # Track all kwargs as params, so that forward() will pass + # them on in subsequent calls. + ctx.params.update(kwargs) + else: + ctx = self + + with augment_usage_errors(self): + with ctx: + return callback(*args, **kwargs) + + def forward(self, cmd: Command, /, *args: t.Any, **kwargs: t.Any) -> t.Any: + """Similar to :meth:`invoke` but fills in default keyword + arguments from the current context if the other command expects + it. This cannot invoke callbacks directly, only other commands. + + .. versionchanged:: 8.0 + All ``kwargs`` are tracked in :attr:`params` so they will be + passed if ``forward`` is called at multiple levels. + """ + # Can only forward to other commands, not direct callbacks. + if not isinstance(cmd, Command): + raise TypeError("Callback is not a command.") + + for param in self.params: + if param not in kwargs: + kwargs[param] = self.params[param] + + return self.invoke(cmd, *args, **kwargs) + + def set_parameter_source(self, name: str, source: ParameterSource) -> None: + """Set the source of a parameter. This indicates the location + from which the value of the parameter was obtained. + + :param name: The name of the parameter. + :param source: A member of :class:`~click.core.ParameterSource`. + """ + self._parameter_source[name] = source + + def get_parameter_source(self, name: str) -> ParameterSource | None: + """Get the source of a parameter. This indicates the location + from which the value of the parameter was obtained. + + This can be useful for determining when a user specified a value + on the command line that is the same as the default value. It + will be :attr:`~click.core.ParameterSource.DEFAULT` only if the + value was actually taken from the default. + + :param name: The name of the parameter. + :rtype: ParameterSource + + .. versionchanged:: 8.0 + Returns ``None`` if the parameter was not provided from any + source. + """ + return self._parameter_source.get(name) + + +class Command: + """Commands are the basic building block of command line interfaces in + Click. A basic command handles command line parsing and might dispatch + more parsing to commands nested below it. + + :param name: the name of the command to use unless a group overrides it. + :param context_settings: an optional dictionary with defaults that are + passed to the context object. + :param callback: the callback to invoke. This is optional. + :param params: the parameters to register with this command. This can + be either :class:`Option` or :class:`Argument` objects. + :param help: the help string to use for this command. + :param epilog: like the help string but it's printed at the end of the + help page after everything else. + :param short_help: the short help to use for this command. This is + shown on the command listing of the parent command. + :param add_help_option: by default each command registers a ``--help`` + option. This can be disabled by this parameter. + :param no_args_is_help: this controls what happens if no arguments are + provided. This option is disabled by default. + If enabled this will add ``--help`` as argument + if no arguments are passed + :param hidden: hide this command from help outputs. + :param deprecated: If ``True`` or non-empty string, issues a message + indicating that the command is deprecated and highlights + its deprecation in --help. The message can be customized + by using a string as the value. + + .. versionchanged:: 8.2 + This is the base class for all commands, not ``BaseCommand``. + ``deprecated`` can be set to a string as well to customize the + deprecation message. + + .. versionchanged:: 8.1 + ``help``, ``epilog``, and ``short_help`` are stored unprocessed, + all formatting is done when outputting help text, not at init, + and is done even if not using the ``@command`` decorator. + + .. versionchanged:: 8.0 + Added a ``repr`` showing the command name. + + .. versionchanged:: 7.1 + Added the ``no_args_is_help`` parameter. + + .. versionchanged:: 2.0 + Added the ``context_settings`` parameter. + """ + + #: The context class to create with :meth:`make_context`. + #: + #: .. versionadded:: 8.0 + context_class: type[Context] = Context + + #: the default for the :attr:`Context.allow_extra_args` flag. + allow_extra_args = False + + #: the default for the :attr:`Context.allow_interspersed_args` flag. + allow_interspersed_args = True + + #: the default for the :attr:`Context.ignore_unknown_options` flag. + ignore_unknown_options = False + + def __init__( + self, + name: str | None, + context_settings: cabc.MutableMapping[str, t.Any] | None = None, + callback: t.Callable[..., t.Any] | None = None, + params: list[Parameter] | None = None, + help: str | None = None, + epilog: str | None = None, + short_help: str | None = None, + options_metavar: str | None = "[OPTIONS]", + add_help_option: bool = True, + no_args_is_help: bool = False, + hidden: bool = False, + deprecated: bool | str = False, + ) -> None: + #: the name the command thinks it has. Upon registering a command + #: on a :class:`Group` the group will default the command name + #: with this information. You should instead use the + #: :class:`Context`\'s :attr:`~Context.info_name` attribute. + self.name = name + + if context_settings is None: + context_settings = {} + + #: an optional dictionary with defaults passed to the context. + self.context_settings: cabc.MutableMapping[str, t.Any] = context_settings + + #: the callback to execute when the command fires. This might be + #: `None` in which case nothing happens. + self.callback = callback + #: the list of parameters for this command in the order they + #: should show up in the help page and execute. Eager parameters + #: will automatically be handled before non eager ones. + self.params: list[Parameter] = params or [] + self.help = help + self.epilog = epilog + self.options_metavar = options_metavar + self.short_help = short_help + self.add_help_option = add_help_option + self._help_option = None + self.no_args_is_help = no_args_is_help + self.hidden = hidden + self.deprecated = deprecated + + def to_info_dict(self, ctx: Context) -> dict[str, t.Any]: + return { + "name": self.name, + "params": [param.to_info_dict() for param in self.get_params(ctx)], + "help": self.help, + "epilog": self.epilog, + "short_help": self.short_help, + "hidden": self.hidden, + "deprecated": self.deprecated, + } + + def __repr__(self) -> str: + return f"<{self.__class__.__name__} {self.name}>" + + def get_usage(self, ctx: Context) -> str: + """Formats the usage line into a string and returns it. + + Calls :meth:`format_usage` internally. + """ + formatter = ctx.make_formatter() + self.format_usage(ctx, formatter) + return formatter.getvalue().rstrip("\n") + + def get_params(self, ctx: Context) -> list[Parameter]: + params = self.params + help_option = self.get_help_option(ctx) + + if help_option is not None: + params = [*params, help_option] + + if __debug__: + import warnings + + opts = [opt for param in params for opt in param.opts] + opts_counter = Counter(opts) + duplicate_opts = (opt for opt, count in opts_counter.items() if count > 1) + + for duplicate_opt in duplicate_opts: + warnings.warn( + ( + f"The parameter {duplicate_opt} is used more than once. " + "Remove its duplicate as parameters should be unique." + ), + stacklevel=3, + ) + + return params + + def format_usage(self, ctx: Context, formatter: HelpFormatter) -> None: + """Writes the usage line into the formatter. + + This is a low-level method called by :meth:`get_usage`. + """ + pieces = self.collect_usage_pieces(ctx) + formatter.write_usage(ctx.command_path, " ".join(pieces)) + + def collect_usage_pieces(self, ctx: Context) -> list[str]: + """Returns all the pieces that go into the usage line and returns + it as a list of strings. + """ + rv = [self.options_metavar] if self.options_metavar else [] + + for param in self.get_params(ctx): + rv.extend(param.get_usage_pieces(ctx)) + + return rv + + def get_help_option_names(self, ctx: Context) -> list[str]: + """Returns the names for the help option.""" + all_names = set(ctx.help_option_names) + for param in self.params: + all_names.difference_update(param.opts) + all_names.difference_update(param.secondary_opts) + return list(all_names) + + def get_help_option(self, ctx: Context) -> Option | None: + """Returns the help option object. + + Skipped if :attr:`add_help_option` is ``False``. + + .. versionchanged:: 8.1.8 + The help option is now cached to avoid creating it multiple times. + """ + help_option_names = self.get_help_option_names(ctx) + + if not help_option_names or not self.add_help_option: + return None + + # Cache the help option object in private _help_option attribute to + # avoid creating it multiple times. Not doing this will break the + # callback odering by iter_params_for_processing(), which relies on + # object comparison. + if self._help_option is None: + # Avoid circular import. + from .decorators import help_option + + # Apply help_option decorator and pop resulting option + help_option(*help_option_names)(self) + self._help_option = self.params.pop() # type: ignore[assignment] + + return self._help_option + + def make_parser(self, ctx: Context) -> _OptionParser: + """Creates the underlying option parser for this command.""" + parser = _OptionParser(ctx) + for param in self.get_params(ctx): + param.add_to_parser(parser, ctx) + return parser + + def get_help(self, ctx: Context) -> str: + """Formats the help into a string and returns it. + + Calls :meth:`format_help` internally. + """ + formatter = ctx.make_formatter() + self.format_help(ctx, formatter) + return formatter.getvalue().rstrip("\n") + + def get_short_help_str(self, limit: int = 45) -> str: + """Gets short help for the command or makes it by shortening the + long help string. + """ + if self.short_help: + text = inspect.cleandoc(self.short_help) + elif self.help: + text = make_default_short_help(self.help, limit) + else: + text = "" + + if self.deprecated: + deprecated_message = ( + f"(DEPRECATED: {self.deprecated})" + if isinstance(self.deprecated, str) + else "(DEPRECATED)" + ) + text = _("{text} {deprecated_message}").format( + text=text, deprecated_message=deprecated_message + ) + + return text.strip() + + def format_help(self, ctx: Context, formatter: HelpFormatter) -> None: + """Writes the help into the formatter if it exists. + + This is a low-level method called by :meth:`get_help`. + + This calls the following methods: + + - :meth:`format_usage` + - :meth:`format_help_text` + - :meth:`format_options` + - :meth:`format_epilog` + """ + self.format_usage(ctx, formatter) + self.format_help_text(ctx, formatter) + self.format_options(ctx, formatter) + self.format_epilog(ctx, formatter) + + def format_help_text(self, ctx: Context, formatter: HelpFormatter) -> None: + """Writes the help text to the formatter if it exists.""" + if self.help is not None: + # truncate the help text to the first form feed + text = inspect.cleandoc(self.help).partition("\f")[0] + else: + text = "" + + if self.deprecated: + deprecated_message = ( + f"(DEPRECATED: {self.deprecated})" + if isinstance(self.deprecated, str) + else "(DEPRECATED)" + ) + text = _("{text} {deprecated_message}").format( + text=text, deprecated_message=deprecated_message + ) + + if text: + formatter.write_paragraph() + + with formatter.indentation(): + formatter.write_text(text) + + def format_options(self, ctx: Context, formatter: HelpFormatter) -> None: + """Writes all the options into the formatter if they exist.""" + opts = [] + for param in self.get_params(ctx): + rv = param.get_help_record(ctx) + if rv is not None: + opts.append(rv) + + if opts: + with formatter.section(_("Options")): + formatter.write_dl(opts) + + def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None: + """Writes the epilog into the formatter if it exists.""" + if self.epilog: + epilog = inspect.cleandoc(self.epilog) + formatter.write_paragraph() + + with formatter.indentation(): + formatter.write_text(epilog) + + def make_context( + self, + info_name: str | None, + args: list[str], + parent: Context | None = None, + **extra: t.Any, + ) -> Context: + """This function when given an info name and arguments will kick + off the parsing and create a new :class:`Context`. It does not + invoke the actual command callback though. + + To quickly customize the context class used without overriding + this method, set the :attr:`context_class` attribute. + + :param info_name: the info name for this invocation. Generally this + is the most descriptive name for the script or + command. For the toplevel script it's usually + the name of the script, for commands below it's + the name of the command. + :param args: the arguments to parse as list of strings. + :param parent: the parent context if available. + :param extra: extra keyword arguments forwarded to the context + constructor. + + .. versionchanged:: 8.0 + Added the :attr:`context_class` attribute. + """ + for key, value in self.context_settings.items(): + if key not in extra: + extra[key] = value + + ctx = self.context_class(self, info_name=info_name, parent=parent, **extra) + + with ctx.scope(cleanup=False): + self.parse_args(ctx, args) + return ctx + + def parse_args(self, ctx: Context, args: list[str]) -> list[str]: + if not args and self.no_args_is_help and not ctx.resilient_parsing: + raise NoArgsIsHelpError(ctx) + + parser = self.make_parser(ctx) + opts, args, param_order = parser.parse_args(args=args) + + for param in iter_params_for_processing(param_order, self.get_params(ctx)): + _, args = param.handle_parse_result(ctx, opts, args) + + # We now have all parameters' values into `ctx.params`, but the data may contain + # the `UNSET` sentinel. + # Convert `UNSET` to `None` to ensure that the user doesn't see `UNSET`. + # + # Waiting until after the initial parse to convert allows us to treat `UNSET` + # more like a missing value when multiple params use the same name. + # Refs: + # https://github.com/pallets/click/issues/3071 + # https://github.com/pallets/click/pull/3079 + for name, value in ctx.params.items(): + if value is UNSET: + ctx.params[name] = None + + if args and not ctx.allow_extra_args and not ctx.resilient_parsing: + ctx.fail( + ngettext( + "Got unexpected extra argument ({args})", + "Got unexpected extra arguments ({args})", + len(args), + ).format(args=" ".join(map(str, args))) + ) + + ctx.args = args + ctx._opt_prefixes.update(parser._opt_prefixes) + return args + + def invoke(self, ctx: Context) -> t.Any: + """Given a context, this invokes the attached callback (if it exists) + in the right way. + """ + if self.deprecated: + extra_message = ( + f" {self.deprecated}" if isinstance(self.deprecated, str) else "" + ) + message = _( + "DeprecationWarning: The command {name!r} is deprecated.{extra_message}" + ).format(name=self.name, extra_message=extra_message) + echo(style(message, fg="red"), err=True) + + if self.callback is not None: + return ctx.invoke(self.callback, **ctx.params) + + def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]: + """Return a list of completions for the incomplete value. Looks + at the names of options and chained multi-commands. + + Any command could be part of a chained multi-command, so sibling + commands are valid at any point during command completion. + + :param ctx: Invocation context for this command. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + from click.shell_completion import CompletionItem + + results: list[CompletionItem] = [] + + if incomplete and not incomplete[0].isalnum(): + for param in self.get_params(ctx): + if ( + not isinstance(param, Option) + or param.hidden + or ( + not param.multiple + and ctx.get_parameter_source(param.name) # type: ignore + is ParameterSource.COMMANDLINE + ) + ): + continue + + results.extend( + CompletionItem(name, help=param.help) + for name in [*param.opts, *param.secondary_opts] + if name.startswith(incomplete) + ) + + while ctx.parent is not None: + ctx = ctx.parent + + if isinstance(ctx.command, Group) and ctx.command.chain: + results.extend( + CompletionItem(name, help=command.get_short_help_str()) + for name, command in _complete_visible_commands(ctx, incomplete) + if name not in ctx._protected_args + ) + + return results + + @t.overload + def main( + self, + args: cabc.Sequence[str] | None = None, + prog_name: str | None = None, + complete_var: str | None = None, + standalone_mode: t.Literal[True] = True, + **extra: t.Any, + ) -> t.NoReturn: ... + + @t.overload + def main( + self, + args: cabc.Sequence[str] | None = None, + prog_name: str | None = None, + complete_var: str | None = None, + standalone_mode: bool = ..., + **extra: t.Any, + ) -> t.Any: ... + + def main( + self, + args: cabc.Sequence[str] | None = None, + prog_name: str | None = None, + complete_var: str | None = None, + standalone_mode: bool = True, + windows_expand_args: bool = True, + **extra: t.Any, + ) -> t.Any: + """This is the way to invoke a script with all the bells and + whistles as a command line application. This will always terminate + the application after a call. If this is not wanted, ``SystemExit`` + needs to be caught. + + This method is also available by directly calling the instance of + a :class:`Command`. + + :param args: the arguments that should be used for parsing. If not + provided, ``sys.argv[1:]`` is used. + :param prog_name: the program name that should be used. By default + the program name is constructed by taking the file + name from ``sys.argv[0]``. + :param complete_var: the environment variable that controls the + bash completion support. The default is + ``"__COMPLETE"`` with prog_name in + uppercase. + :param standalone_mode: the default behavior is to invoke the script + in standalone mode. Click will then + handle exceptions and convert them into + error messages and the function will never + return but shut down the interpreter. If + this is set to `False` they will be + propagated to the caller and the return + value of this function is the return value + of :meth:`invoke`. + :param windows_expand_args: Expand glob patterns, user dir, and + env vars in command line args on Windows. + :param extra: extra keyword arguments are forwarded to the context + constructor. See :class:`Context` for more information. + + .. versionchanged:: 8.0.1 + Added the ``windows_expand_args`` parameter to allow + disabling command line arg expansion on Windows. + + .. versionchanged:: 8.0 + When taking arguments from ``sys.argv`` on Windows, glob + patterns, user dir, and env vars are expanded. + + .. versionchanged:: 3.0 + Added the ``standalone_mode`` parameter. + """ + if args is None: + args = sys.argv[1:] + + if os.name == "nt" and windows_expand_args: + args = _expand_args(args) + else: + args = list(args) + + if prog_name is None: + prog_name = _detect_program_name() + + # Process shell completion requests and exit early. + self._main_shell_completion(extra, prog_name, complete_var) + + try: + try: + with self.make_context(prog_name, args, **extra) as ctx: + rv = self.invoke(ctx) + if not standalone_mode: + return rv + # it's not safe to `ctx.exit(rv)` here! + # note that `rv` may actually contain data like "1" which + # has obvious effects + # more subtle case: `rv=[None, None]` can come out of + # chained commands which all returned `None` -- so it's not + # even always obvious that `rv` indicates success/failure + # by its truthiness/falsiness + ctx.exit() + except (EOFError, KeyboardInterrupt) as e: + echo(file=sys.stderr) + raise Abort() from e + except ClickException as e: + if not standalone_mode: + raise + e.show() + sys.exit(e.exit_code) + except OSError as e: + if e.errno == errno.EPIPE: + sys.stdout = t.cast(t.TextIO, PacifyFlushWrapper(sys.stdout)) + sys.stderr = t.cast(t.TextIO, PacifyFlushWrapper(sys.stderr)) + sys.exit(1) + else: + raise + except Exit as e: + if standalone_mode: + sys.exit(e.exit_code) + else: + # in non-standalone mode, return the exit code + # note that this is only reached if `self.invoke` above raises + # an Exit explicitly -- thus bypassing the check there which + # would return its result + # the results of non-standalone execution may therefore be + # somewhat ambiguous: if there are codepaths which lead to + # `ctx.exit(1)` and to `return 1`, the caller won't be able to + # tell the difference between the two + return e.exit_code + except Abort: + if not standalone_mode: + raise + echo(_("Aborted!"), file=sys.stderr) + sys.exit(1) + + def _main_shell_completion( + self, + ctx_args: cabc.MutableMapping[str, t.Any], + prog_name: str, + complete_var: str | None = None, + ) -> None: + """Check if the shell is asking for tab completion, process + that, then exit early. Called from :meth:`main` before the + program is invoked. + + :param prog_name: Name of the executable in the shell. + :param complete_var: Name of the environment variable that holds + the completion instruction. Defaults to + ``_{PROG_NAME}_COMPLETE``. + + .. versionchanged:: 8.2.0 + Dots (``.``) in ``prog_name`` are replaced with underscores (``_``). + """ + if complete_var is None: + complete_name = prog_name.replace("-", "_").replace(".", "_") + complete_var = f"_{complete_name}_COMPLETE".upper() + + instruction = os.environ.get(complete_var) + + if not instruction: + return + + from .shell_completion import shell_complete + + rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction) + sys.exit(rv) + + def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any: + """Alias for :meth:`main`.""" + return self.main(*args, **kwargs) + + +class _FakeSubclassCheck(type): + def __subclasscheck__(cls, subclass: type) -> bool: + return issubclass(subclass, cls.__bases__[0]) + + def __instancecheck__(cls, instance: t.Any) -> bool: + return isinstance(instance, cls.__bases__[0]) + + +class _BaseCommand(Command, metaclass=_FakeSubclassCheck): + """ + .. deprecated:: 8.2 + Will be removed in Click 9.0. Use ``Command`` instead. + """ + + +class Group(Command): + """A group is a command that nests other commands (or more groups). + + :param name: The name of the group command. + :param commands: Map names to :class:`Command` objects. Can be a list, which + will use :attr:`Command.name` as the keys. + :param invoke_without_command: Invoke the group's callback even if a + subcommand is not given. + :param no_args_is_help: If no arguments are given, show the group's help and + exit. Defaults to the opposite of ``invoke_without_command``. + :param subcommand_metavar: How to represent the subcommand argument in help. + The default will represent whether ``chain`` is set or not. + :param chain: Allow passing more than one subcommand argument. After parsing + a command's arguments, if any arguments remain another command will be + matched, and so on. + :param result_callback: A function to call after the group's and + subcommand's callbacks. The value returned by the subcommand is passed. + If ``chain`` is enabled, the value will be a list of values returned by + all the commands. If ``invoke_without_command`` is enabled, the value + will be the value returned by the group's callback, or an empty list if + ``chain`` is enabled. + :param kwargs: Other arguments passed to :class:`Command`. + + .. versionchanged:: 8.0 + The ``commands`` argument can be a list of command objects. + + .. versionchanged:: 8.2 + Merged with and replaces the ``MultiCommand`` base class. + """ + + allow_extra_args = True + allow_interspersed_args = False + + #: If set, this is used by the group's :meth:`command` decorator + #: as the default :class:`Command` class. This is useful to make all + #: subcommands use a custom command class. + #: + #: .. versionadded:: 8.0 + command_class: type[Command] | None = None + + #: If set, this is used by the group's :meth:`group` decorator + #: as the default :class:`Group` class. This is useful to make all + #: subgroups use a custom group class. + #: + #: If set to the special value :class:`type` (literally + #: ``group_class = type``), this group's class will be used as the + #: default class. This makes a custom group class continue to make + #: custom groups. + #: + #: .. versionadded:: 8.0 + group_class: type[Group] | type[type] | None = None + # Literal[type] isn't valid, so use Type[type] + + def __init__( + self, + name: str | None = None, + commands: cabc.MutableMapping[str, Command] + | cabc.Sequence[Command] + | None = None, + invoke_without_command: bool = False, + no_args_is_help: bool | None = None, + subcommand_metavar: str | None = None, + chain: bool = False, + result_callback: t.Callable[..., t.Any] | None = None, + **kwargs: t.Any, + ) -> None: + super().__init__(name, **kwargs) + + if commands is None: + commands = {} + elif isinstance(commands, abc.Sequence): + commands = {c.name: c for c in commands if c.name is not None} + + #: The registered subcommands by their exported names. + self.commands: cabc.MutableMapping[str, Command] = commands + + if no_args_is_help is None: + no_args_is_help = not invoke_without_command + + self.no_args_is_help = no_args_is_help + self.invoke_without_command = invoke_without_command + + if subcommand_metavar is None: + if chain: + subcommand_metavar = "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..." + else: + subcommand_metavar = "COMMAND [ARGS]..." + + self.subcommand_metavar = subcommand_metavar + self.chain = chain + # The result callback that is stored. This can be set or + # overridden with the :func:`result_callback` decorator. + self._result_callback = result_callback + + if self.chain: + for param in self.params: + if isinstance(param, Argument) and not param.required: + raise RuntimeError( + "A group in chain mode cannot have optional arguments." + ) + + def to_info_dict(self, ctx: Context) -> dict[str, t.Any]: + info_dict = super().to_info_dict(ctx) + commands = {} + + for name in self.list_commands(ctx): + command = self.get_command(ctx, name) + + if command is None: + continue + + sub_ctx = ctx._make_sub_context(command) + + with sub_ctx.scope(cleanup=False): + commands[name] = command.to_info_dict(sub_ctx) + + info_dict.update(commands=commands, chain=self.chain) + return info_dict + + def add_command(self, cmd: Command, name: str | None = None) -> None: + """Registers another :class:`Command` with this group. If the name + is not provided, the name of the command is used. + """ + name = name or cmd.name + if name is None: + raise TypeError("Command has no name.") + _check_nested_chain(self, name, cmd, register=True) + self.commands[name] = cmd + + @t.overload + def command(self, __func: t.Callable[..., t.Any]) -> Command: ... + + @t.overload + def command( + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], Command]: ... + + def command( + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], Command] | Command: + """A shortcut decorator for declaring and attaching a command to + the group. This takes the same arguments as :func:`command` and + immediately registers the created command with this group by + calling :meth:`add_command`. + + To customize the command class used, set the + :attr:`command_class` attribute. + + .. versionchanged:: 8.1 + This decorator can be applied without parentheses. + + .. versionchanged:: 8.0 + Added the :attr:`command_class` attribute. + """ + from .decorators import command + + func: t.Callable[..., t.Any] | None = None + + if args and callable(args[0]): + assert len(args) == 1 and not kwargs, ( + "Use 'command(**kwargs)(callable)' to provide arguments." + ) + (func,) = args + args = () + + if self.command_class and kwargs.get("cls") is None: + kwargs["cls"] = self.command_class + + def decorator(f: t.Callable[..., t.Any]) -> Command: + cmd: Command = command(*args, **kwargs)(f) + self.add_command(cmd) + return cmd + + if func is not None: + return decorator(func) + + return decorator + + @t.overload + def group(self, __func: t.Callable[..., t.Any]) -> Group: ... + + @t.overload + def group( + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], Group]: ... + + def group( + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], Group] | Group: + """A shortcut decorator for declaring and attaching a group to + the group. This takes the same arguments as :func:`group` and + immediately registers the created group with this group by + calling :meth:`add_command`. + + To customize the group class used, set the :attr:`group_class` + attribute. + + .. versionchanged:: 8.1 + This decorator can be applied without parentheses. + + .. versionchanged:: 8.0 + Added the :attr:`group_class` attribute. + """ + from .decorators import group + + func: t.Callable[..., t.Any] | None = None + + if args and callable(args[0]): + assert len(args) == 1 and not kwargs, ( + "Use 'group(**kwargs)(callable)' to provide arguments." + ) + (func,) = args + args = () + + if self.group_class is not None and kwargs.get("cls") is None: + if self.group_class is type: + kwargs["cls"] = type(self) + else: + kwargs["cls"] = self.group_class + + def decorator(f: t.Callable[..., t.Any]) -> Group: + cmd: Group = group(*args, **kwargs)(f) + self.add_command(cmd) + return cmd + + if func is not None: + return decorator(func) + + return decorator + + def result_callback(self, replace: bool = False) -> t.Callable[[F], F]: + """Adds a result callback to the command. By default if a + result callback is already registered this will chain them but + this can be disabled with the `replace` parameter. The result + callback is invoked with the return value of the subcommand + (or the list of return values from all subcommands if chaining + is enabled) as well as the parameters as they would be passed + to the main callback. + + Example:: + + @click.group() + @click.option('-i', '--input', default=23) + def cli(input): + return 42 + + @cli.result_callback() + def process_result(result, input): + return result + input + + :param replace: if set to `True` an already existing result + callback will be removed. + + .. versionchanged:: 8.0 + Renamed from ``resultcallback``. + + .. versionadded:: 3.0 + """ + + def decorator(f: F) -> F: + old_callback = self._result_callback + + if old_callback is None or replace: + self._result_callback = f + return f + + def function(value: t.Any, /, *args: t.Any, **kwargs: t.Any) -> t.Any: + inner = old_callback(value, *args, **kwargs) + return f(inner, *args, **kwargs) + + self._result_callback = rv = update_wrapper(t.cast(F, function), f) + return rv # type: ignore[return-value] + + return decorator + + def get_command(self, ctx: Context, cmd_name: str) -> Command | None: + """Given a context and a command name, this returns a :class:`Command` + object if it exists or returns ``None``. + """ + return self.commands.get(cmd_name) + + def list_commands(self, ctx: Context) -> list[str]: + """Returns a list of subcommand names in the order they should appear.""" + return sorted(self.commands) + + def collect_usage_pieces(self, ctx: Context) -> list[str]: + rv = super().collect_usage_pieces(ctx) + rv.append(self.subcommand_metavar) + return rv + + def format_options(self, ctx: Context, formatter: HelpFormatter) -> None: + super().format_options(ctx, formatter) + self.format_commands(ctx, formatter) + + def format_commands(self, ctx: Context, formatter: HelpFormatter) -> None: + """Extra format methods for multi methods that adds all the commands + after the options. + """ + commands = [] + for subcommand in self.list_commands(ctx): + cmd = self.get_command(ctx, subcommand) + # What is this, the tool lied about a command. Ignore it + if cmd is None: + continue + if cmd.hidden: + continue + + commands.append((subcommand, cmd)) + + # allow for 3 times the default spacing + if len(commands): + limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands) + + rows = [] + for subcommand, cmd in commands: + help = cmd.get_short_help_str(limit) + rows.append((subcommand, help)) + + if rows: + with formatter.section(_("Commands")): + formatter.write_dl(rows) + + def parse_args(self, ctx: Context, args: list[str]) -> list[str]: + if not args and self.no_args_is_help and not ctx.resilient_parsing: + raise NoArgsIsHelpError(ctx) + + rest = super().parse_args(ctx, args) + + if self.chain: + ctx._protected_args = rest + ctx.args = [] + elif rest: + ctx._protected_args, ctx.args = rest[:1], rest[1:] + + return ctx.args + + def invoke(self, ctx: Context) -> t.Any: + def _process_result(value: t.Any) -> t.Any: + if self._result_callback is not None: + value = ctx.invoke(self._result_callback, value, **ctx.params) + return value + + if not ctx._protected_args: + if self.invoke_without_command: + # No subcommand was invoked, so the result callback is + # invoked with the group return value for regular + # groups, or an empty list for chained groups. + with ctx: + rv = super().invoke(ctx) + return _process_result([] if self.chain else rv) + ctx.fail(_("Missing command.")) + + # Fetch args back out + args = [*ctx._protected_args, *ctx.args] + ctx.args = [] + ctx._protected_args = [] + + # If we're not in chain mode, we only allow the invocation of a + # single command but we also inform the current context about the + # name of the command to invoke. + if not self.chain: + # Make sure the context is entered so we do not clean up + # resources until the result processor has worked. + with ctx: + cmd_name, cmd, args = self.resolve_command(ctx, args) + assert cmd is not None + ctx.invoked_subcommand = cmd_name + super().invoke(ctx) + sub_ctx = cmd.make_context(cmd_name, args, parent=ctx) + with sub_ctx: + return _process_result(sub_ctx.command.invoke(sub_ctx)) + + # In chain mode we create the contexts step by step, but after the + # base command has been invoked. Because at that point we do not + # know the subcommands yet, the invoked subcommand attribute is + # set to ``*`` to inform the command that subcommands are executed + # but nothing else. + with ctx: + ctx.invoked_subcommand = "*" if args else None + super().invoke(ctx) + + # Otherwise we make every single context and invoke them in a + # chain. In that case the return value to the result processor + # is the list of all invoked subcommand's results. + contexts = [] + while args: + cmd_name, cmd, args = self.resolve_command(ctx, args) + assert cmd is not None + sub_ctx = cmd.make_context( + cmd_name, + args, + parent=ctx, + allow_extra_args=True, + allow_interspersed_args=False, + ) + contexts.append(sub_ctx) + args, sub_ctx.args = sub_ctx.args, [] + + rv = [] + for sub_ctx in contexts: + with sub_ctx: + rv.append(sub_ctx.command.invoke(sub_ctx)) + return _process_result(rv) + + def resolve_command( + self, ctx: Context, args: list[str] + ) -> tuple[str | None, Command | None, list[str]]: + cmd_name = make_str(args[0]) + original_cmd_name = cmd_name + + # Get the command + cmd = self.get_command(ctx, cmd_name) + + # If we can't find the command but there is a normalization + # function available, we try with that one. + if cmd is None and ctx.token_normalize_func is not None: + cmd_name = ctx.token_normalize_func(cmd_name) + cmd = self.get_command(ctx, cmd_name) + + # If we don't find the command we want to show an error message + # to the user that it was not provided. However, there is + # something else we should do: if the first argument looks like + # an option we want to kick off parsing again for arguments to + # resolve things like --help which now should go to the main + # place. + if cmd is None and not ctx.resilient_parsing: + if _split_opt(cmd_name)[0]: + self.parse_args(ctx, args) + ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name)) + return cmd_name if cmd else None, cmd, args[1:] + + def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]: + """Return a list of completions for the incomplete value. Looks + at the names of options, subcommands, and chained + multi-commands. + + :param ctx: Invocation context for this command. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + from click.shell_completion import CompletionItem + + results = [ + CompletionItem(name, help=command.get_short_help_str()) + for name, command in _complete_visible_commands(ctx, incomplete) + ] + results.extend(super().shell_complete(ctx, incomplete)) + return results + + +class _MultiCommand(Group, metaclass=_FakeSubclassCheck): + """ + .. deprecated:: 8.2 + Will be removed in Click 9.0. Use ``Group`` instead. + """ + + +class CommandCollection(Group): + """A :class:`Group` that looks up subcommands on other groups. If a command + is not found on this group, each registered source is checked in order. + Parameters on a source are not added to this group, and a source's callback + is not invoked when invoking its commands. In other words, this "flattens" + commands in many groups into this one group. + + :param name: The name of the group command. + :param sources: A list of :class:`Group` objects to look up commands from. + :param kwargs: Other arguments passed to :class:`Group`. + + .. versionchanged:: 8.2 + This is a subclass of ``Group``. Commands are looked up first on this + group, then each of its sources. + """ + + def __init__( + self, + name: str | None = None, + sources: list[Group] | None = None, + **kwargs: t.Any, + ) -> None: + super().__init__(name, **kwargs) + #: The list of registered groups. + self.sources: list[Group] = sources or [] + + def add_source(self, group: Group) -> None: + """Add a group as a source of commands.""" + self.sources.append(group) + + def get_command(self, ctx: Context, cmd_name: str) -> Command | None: + rv = super().get_command(ctx, cmd_name) + + if rv is not None: + return rv + + for source in self.sources: + rv = source.get_command(ctx, cmd_name) + + if rv is not None: + if self.chain: + _check_nested_chain(self, cmd_name, rv) + + return rv + + return None + + def list_commands(self, ctx: Context) -> list[str]: + rv: set[str] = set(super().list_commands(ctx)) + + for source in self.sources: + rv.update(source.list_commands(ctx)) + + return sorted(rv) + + +def _check_iter(value: t.Any) -> cabc.Iterator[t.Any]: + """Check if the value is iterable but not a string. Raises a type + error, or return an iterator over the value. + """ + if isinstance(value, str): + raise TypeError + + return iter(value) + + +class Parameter: + r"""A parameter to a command comes in two versions: they are either + :class:`Option`\s or :class:`Argument`\s. Other subclasses are currently + not supported by design as some of the internals for parsing are + intentionally not finalized. + + Some settings are supported by both options and arguments. + + :param param_decls: the parameter declarations for this option or + argument. This is a list of flags or argument + names. + :param type: the type that should be used. Either a :class:`ParamType` + or a Python type. The latter is converted into the former + automatically if supported. + :param required: controls if this is optional or not. + :param default: the default value if omitted. This can also be a callable, + in which case it's invoked when the default is needed + without any arguments. + :param callback: A function to further process or validate the value + after type conversion. It is called as ``f(ctx, param, value)`` + and must return the value. It is called for all sources, + including prompts. + :param nargs: the number of arguments to match. If not ``1`` the return + value is a tuple instead of single value. The default for + nargs is ``1`` (except if the type is a tuple, then it's + the arity of the tuple). If ``nargs=-1``, all remaining + parameters are collected. + :param metavar: how the value is represented in the help page. + :param expose_value: if this is `True` then the value is passed onwards + to the command callback and stored on the context, + otherwise it's skipped. + :param is_eager: eager values are processed before non eager ones. This + should not be set for arguments or it will inverse the + order of processing. + :param envvar: environment variable(s) that are used to provide a default value for + this parameter. This can be a string or a sequence of strings. If a sequence is + given, only the first non-empty environment variable is used for the parameter. + :param shell_complete: A function that returns custom shell + completions. Used instead of the param's type completion if + given. Takes ``ctx, param, incomplete`` and must return a list + of :class:`~click.shell_completion.CompletionItem` or a list of + strings. + :param deprecated: If ``True`` or non-empty string, issues a message + indicating that the argument is deprecated and highlights + its deprecation in --help. The message can be customized + by using a string as the value. A deprecated parameter + cannot be required, a ValueError will be raised otherwise. + + .. versionchanged:: 8.2.0 + Introduction of ``deprecated``. + + .. versionchanged:: 8.2 + Adding duplicate parameter names to a :class:`~click.core.Command` will + result in a ``UserWarning`` being shown. + + .. versionchanged:: 8.2 + Adding duplicate parameter names to a :class:`~click.core.Command` will + result in a ``UserWarning`` being shown. + + .. versionchanged:: 8.0 + ``process_value`` validates required parameters and bounded + ``nargs``, and invokes the parameter callback before returning + the value. This allows the callback to validate prompts. + ``full_process_value`` is removed. + + .. versionchanged:: 8.0 + ``autocompletion`` is renamed to ``shell_complete`` and has new + semantics described above. The old name is deprecated and will + be removed in 8.1, until then it will be wrapped to match the + new requirements. + + .. versionchanged:: 8.0 + For ``multiple=True, nargs>1``, the default must be a list of + tuples. + + .. versionchanged:: 8.0 + Setting a default is no longer required for ``nargs>1``, it will + default to ``None``. ``multiple=True`` or ``nargs=-1`` will + default to ``()``. + + .. versionchanged:: 7.1 + Empty environment variables are ignored rather than taking the + empty string value. This makes it possible for scripts to clear + variables if they can't unset them. + + .. versionchanged:: 2.0 + Changed signature for parameter callback to also be passed the + parameter. The old callback format will still work, but it will + raise a warning to give you a chance to migrate the code easier. + """ + + param_type_name = "parameter" + + def __init__( + self, + param_decls: cabc.Sequence[str] | None = None, + type: types.ParamType | t.Any | None = None, + required: bool = False, + # XXX The default historically embed two concepts: + # - the declaration of a Parameter object carrying the default (handy to + # arbitrage the default value of coupled Parameters sharing the same + # self.name, like flag options), + # - and the actual value of the default. + # It is confusing and is the source of many issues discussed in: + # https://github.com/pallets/click/pull/3030 + # In the future, we might think of splitting it in two, not unlike + # Option.is_flag and Option.flag_value: we could have something like + # Parameter.is_default and Parameter.default_value. + default: t.Any | t.Callable[[], t.Any] | None = UNSET, + callback: t.Callable[[Context, Parameter, t.Any], t.Any] | None = None, + nargs: int | None = None, + multiple: bool = False, + metavar: str | None = None, + expose_value: bool = True, + is_eager: bool = False, + envvar: str | cabc.Sequence[str] | None = None, + shell_complete: t.Callable[ + [Context, Parameter, str], list[CompletionItem] | list[str] + ] + | None = None, + deprecated: bool | str = False, + ) -> None: + self.name: str | None + self.opts: list[str] + self.secondary_opts: list[str] + self.name, self.opts, self.secondary_opts = self._parse_decls( + param_decls or (), expose_value + ) + self.type: types.ParamType = types.convert_type(type, default) + + # Default nargs to what the type tells us if we have that + # information available. + if nargs is None: + if self.type.is_composite: + nargs = self.type.arity + else: + nargs = 1 + + self.required = required + self.callback = callback + self.nargs = nargs + self.multiple = multiple + self.expose_value = expose_value + self.default: t.Any | t.Callable[[], t.Any] | None = default + self.is_eager = is_eager + self.metavar = metavar + self.envvar = envvar + self._custom_shell_complete = shell_complete + self.deprecated = deprecated + + if __debug__: + if self.type.is_composite and nargs != self.type.arity: + raise ValueError( + f"'nargs' must be {self.type.arity} (or None) for" + f" type {self.type!r}, but it was {nargs}." + ) + + if required and deprecated: + raise ValueError( + f"The {self.param_type_name} '{self.human_readable_name}' " + "is deprecated and still required. A deprecated " + f"{self.param_type_name} cannot be required." + ) + + def to_info_dict(self) -> dict[str, t.Any]: + """Gather information that could be useful for a tool generating + user-facing documentation. + + Use :meth:`click.Context.to_info_dict` to traverse the entire + CLI structure. + + .. versionchanged:: 8.3.0 + Returns ``None`` for the :attr:`default` if it was not set. + + .. versionadded:: 8.0 + """ + return { + "name": self.name, + "param_type_name": self.param_type_name, + "opts": self.opts, + "secondary_opts": self.secondary_opts, + "type": self.type.to_info_dict(), + "required": self.required, + "nargs": self.nargs, + "multiple": self.multiple, + # We explicitly hide the :attr:`UNSET` value to the user, as we choose to + # make it an implementation detail. And because ``to_info_dict`` has been + # designed for documentation purposes, we return ``None`` instead. + "default": self.default if self.default is not UNSET else None, + "envvar": self.envvar, + } + + def __repr__(self) -> str: + return f"<{self.__class__.__name__} {self.name}>" + + def _parse_decls( + self, decls: cabc.Sequence[str], expose_value: bool + ) -> tuple[str | None, list[str], list[str]]: + raise NotImplementedError() + + @property + def human_readable_name(self) -> str: + """Returns the human readable name of this parameter. This is the + same as the name for options, but the metavar for arguments. + """ + return self.name # type: ignore + + def make_metavar(self, ctx: Context) -> str: + if self.metavar is not None: + return self.metavar + + metavar = self.type.get_metavar(param=self, ctx=ctx) + + if metavar is None: + metavar = self.type.name.upper() + + if self.nargs != 1: + metavar += "..." + + return metavar + + @t.overload + def get_default( + self, ctx: Context, call: t.Literal[True] = True + ) -> t.Any | None: ... + + @t.overload + def get_default( + self, ctx: Context, call: bool = ... + ) -> t.Any | t.Callable[[], t.Any] | None: ... + + def get_default( + self, ctx: Context, call: bool = True + ) -> t.Any | t.Callable[[], t.Any] | None: + """Get the default for the parameter. Tries + :meth:`Context.lookup_default` first, then the local default. + + :param ctx: Current context. + :param call: If the default is a callable, call it. Disable to + return the callable instead. + + .. versionchanged:: 8.0.2 + Type casting is no longer performed when getting a default. + + .. versionchanged:: 8.0.1 + Type casting can fail in resilient parsing mode. Invalid + defaults will not prevent showing help text. + + .. versionchanged:: 8.0 + Looks at ``ctx.default_map`` first. + + .. versionchanged:: 8.0 + Added the ``call`` parameter. + """ + name = self.name + value = ctx.lookup_default(name, call=False) if name is not None else None + + if value is None and not ctx._default_map_has(name): + value = self.default + + if call and callable(value): + value = value() + + return value + + def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None: + raise NotImplementedError() + + def consume_value( + self, ctx: Context, opts: cabc.Mapping[str, t.Any] + ) -> tuple[t.Any, ParameterSource]: + """Returns the parameter value produced by the parser. + + If the parser did not produce a value from user input, the value is either + sourced from the environment variable, the default map, or the parameter's + default value. In that order of precedence. + + If no value is found, an internal sentinel value is returned. + + :meta private: + """ + # Collect from the parse the value passed by the user to the CLI. + value = opts.get(self.name, UNSET) # type: ignore + # If the value is set, it means it was sourced from the command line by the + # parser, otherwise it left unset by default. + source = ( + ParameterSource.COMMANDLINE + if value is not UNSET + else ParameterSource.DEFAULT + ) + + if value is UNSET: + envvar_value = self.value_from_envvar(ctx) + if envvar_value is not None: + value = envvar_value + source = ParameterSource.ENVIRONMENT + + if value is UNSET: + default_map_value = ctx.lookup_default(self.name) # type: ignore[arg-type] + if default_map_value is not None or ctx._default_map_has(self.name): + value = default_map_value + source = ParameterSource.DEFAULT_MAP + + if value is UNSET: + default_value = self.get_default(ctx) + if default_value is not UNSET: + value = default_value + source = ParameterSource.DEFAULT + + return value, source + + def type_cast_value(self, ctx: Context, value: t.Any) -> t.Any: + """Convert and validate a value against the parameter's + :attr:`type`, :attr:`multiple`, and :attr:`nargs`. + """ + if value is None: + if self.multiple or self.nargs == -1: + return () + else: + return value + + def check_iter(value: t.Any) -> cabc.Iterator[t.Any]: + try: + return _check_iter(value) + except TypeError: + # This should only happen when passing in args manually, + # the parser should construct an iterable when parsing + # the command line. + raise BadParameter( + _("Value must be an iterable."), ctx=ctx, param=self + ) from None + + # Define the conversion function based on nargs and type. + + if self.nargs == 1 or self.type.is_composite: + + def convert(value: t.Any) -> t.Any: + return self.type(value, param=self, ctx=ctx) + + elif self.nargs == -1: + + def convert(value: t.Any) -> t.Any: # tuple[t.Any, ...] + return tuple(self.type(x, self, ctx) for x in check_iter(value)) + + else: # nargs > 1 + + def convert(value: t.Any) -> t.Any: # tuple[t.Any, ...] + value = tuple(check_iter(value)) + + if len(value) != self.nargs: + raise BadParameter( + ngettext( + "Takes {nargs} values but 1 was given.", + "Takes {nargs} values but {len} were given.", + len(value), + ).format(nargs=self.nargs, len=len(value)), + ctx=ctx, + param=self, + ) + + return tuple(self.type(x, self, ctx) for x in value) + + if self.multiple: + return tuple(convert(x) for x in check_iter(value)) + + return convert(value) + + def value_is_missing(self, value: t.Any) -> bool: + """A value is considered missing if: + + - it is :attr:`UNSET`, + - or if it is an empty sequence while the parameter is suppose to have + non-single value (i.e. :attr:`nargs` is not ``1`` or :attr:`multiple` is + set). + + :meta private: + """ + if value is UNSET: + return True + + if (self.nargs != 1 or self.multiple) and value == (): + return True + + return False + + def process_value(self, ctx: Context, value: t.Any) -> t.Any: + """Process the value of this parameter: + + 1. Type cast the value using :meth:`type_cast_value`. + 2. Check if the value is missing (see: :meth:`value_is_missing`), and raise + :exc:`MissingParameter` if it is required. + 3. If a :attr:`callback` is set, call it to have the value replaced by the + result of the callback. If the value was not set, the callback receive + ``None``. This keep the legacy behavior as it was before the introduction of + the :attr:`UNSET` sentinel. + + :meta private: + """ + # shelter `type_cast_value` from ever seeing an `UNSET` value by handling the + # cases in which `UNSET` gets special treatment explicitly at this layer + # + # Refs: + # https://github.com/pallets/click/issues/3069 + if value is UNSET: + if self.multiple or self.nargs == -1: + value = () + else: + value = self.type_cast_value(ctx, value) + + if self.required and self.value_is_missing(value): + raise MissingParameter(ctx=ctx, param=self) + + if self.callback is not None: + # Legacy case: UNSET is not exposed directly to the callback, but converted + # to None. + if value is UNSET: + value = None + + # Search for parameters with UNSET values in the context. + unset_keys = {k: None for k, v in ctx.params.items() if v is UNSET} + # No UNSET values, call the callback as usual. + if not unset_keys: + value = self.callback(ctx, self, value) + + # Legacy case: provide a temporarily manipulated context to the callback + # to hide UNSET values as None. + # + # Refs: + # https://github.com/pallets/click/issues/3136 + # https://github.com/pallets/click/pull/3137 + else: + # Add another layer to the context stack to clearly hint that the + # context is temporarily modified. + with ctx: + # Update the context parameters to replace UNSET with None. + ctx.params.update(unset_keys) + # Feed these fake context parameters to the callback. + value = self.callback(ctx, self, value) + # Restore the UNSET values in the context parameters. + ctx.params.update( + { + k: UNSET + for k in unset_keys + # Only restore keys that are present and still None, in case + # the callback modified other parameters. + if k in ctx.params and ctx.params[k] is None + } + ) + + return value + + def resolve_envvar_value(self, ctx: Context) -> str | None: + """Returns the value found in the environment variable(s) attached to this + parameter. + + Environment variables values are `always returned as strings + `_. + + This method returns ``None`` if: + + - the :attr:`envvar` property is not set on the :class:`Parameter`, + - the environment variable is not found in the environment, + - the variable is found in the environment but its value is empty (i.e. the + environment variable is present but has an empty string). + + If :attr:`envvar` is setup with multiple environment variables, + then only the first non-empty value is returned. + + .. caution:: + + The raw value extracted from the environment is not normalized and is + returned as-is. Any normalization or reconciliation is performed later by + the :class:`Parameter`'s :attr:`type`. + + :meta private: + """ + if not self.envvar: + return None + + if isinstance(self.envvar, str): + rv = os.environ.get(self.envvar) + + if rv: + return rv + else: + for envvar in self.envvar: + rv = os.environ.get(envvar) + + # Return the first non-empty value of the list of environment variables. + if rv: + return rv + # Else, absence of value is interpreted as an environment variable that + # is not set, so proceed to the next one. + + return None + + def value_from_envvar(self, ctx: Context) -> str | cabc.Sequence[str] | None: + """Process the raw environment variable string for this parameter. + + Returns the string as-is or splits it into a sequence of strings if the + parameter is expecting multiple values (i.e. its :attr:`nargs` property is set + to a value other than ``1``). + + :meta private: + """ + rv = self.resolve_envvar_value(ctx) + + if rv is not None and self.nargs != 1: + return self.type.split_envvar_value(rv) + + return rv + + def handle_parse_result( + self, ctx: Context, opts: cabc.Mapping[str, t.Any], args: list[str] + ) -> tuple[t.Any, list[str]]: + """Process the value produced by the parser from user input. + + Always process the value through the Parameter's :attr:`type`, wherever it + comes from. + + If the parameter is deprecated, this method warn the user about it. But only if + the value has been explicitly set by the user (and as such, is not coming from + a default). + + :meta private: + """ + with augment_usage_errors(ctx, param=self): + value, source = self.consume_value(ctx, opts) + + ctx.set_parameter_source(self.name, source) # type: ignore + + # Display a deprecation warning if necessary. + if ( + self.deprecated + and value is not UNSET + and source < ParameterSource.DEFAULT_MAP + ): + extra_message = ( + f" {self.deprecated}" if isinstance(self.deprecated, str) else "" + ) + message = _( + "DeprecationWarning: The {param_type} {name!r} is deprecated." + "{extra_message}" + ).format( + param_type=self.param_type_name, + name=self.human_readable_name, + extra_message=extra_message, + ) + echo(style(message, fg="red"), err=True) + + # Process the value through the parameter's type. + try: + value = self.process_value(ctx, value) + except Exception: + if not ctx.resilient_parsing: + raise + # In resilient parsing mode, we do not want to fail the command if the + # value is incompatible with the parameter type, so we reset the value + # to UNSET, which will be interpreted as a missing value. + value = UNSET + + # Add parameter's value to the context. + if ( + self.expose_value + # We skip adding the value if it was previously set by another parameter + # targeting the same variable name. This prevents parameters competing for + # the same name to override each other. + and (self.name not in ctx.params or ctx.params[self.name] is UNSET) + ): + # Click is logically enforcing that the name is None if the parameter is + # not to be exposed. We still assert it here to please the type checker. + assert self.name is not None, ( + f"{self!r} parameter's name should not be None when exposing value." + ) + ctx.params[self.name] = value + + return value, args + + def get_help_record(self, ctx: Context) -> tuple[str, str] | None: + pass + + def get_usage_pieces(self, ctx: Context) -> list[str]: + return [] + + def get_error_hint(self, ctx: Context) -> str: + """Get a stringified version of the param for use in error messages to + indicate which param caused the error. + """ + hint_list = self.opts or [self.human_readable_name] + return " / ".join(f"'{x}'" for x in hint_list) + + def shell_complete(self, ctx: Context, incomplete: str) -> list[CompletionItem]: + """Return a list of completions for the incomplete value. If a + ``shell_complete`` function was given during init, it is used. + Otherwise, the :attr:`type` + :meth:`~click.types.ParamType.shell_complete` function is used. + + :param ctx: Invocation context for this command. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + if self._custom_shell_complete is not None: + results = self._custom_shell_complete(ctx, self, incomplete) + + if results and isinstance(results[0], str): + from click.shell_completion import CompletionItem + + results = [CompletionItem(c) for c in results] + + return t.cast("list[CompletionItem]", results) + + return self.type.shell_complete(ctx, self, incomplete) + + +class Option(Parameter): + """Options are usually optional values on the command line and + have some extra features that arguments don't have. + + All other parameters are passed onwards to the parameter constructor. + + :param show_default: Show the default value for this option in its + help text. Values are not shown by default, unless + :attr:`Context.show_default` is ``True``. If this value is a + string, it shows that string in parentheses instead of the + actual value. This is particularly useful for dynamic options. + For single option boolean flags, the default remains hidden if + its value is ``False``. + :param show_envvar: Controls if an environment variable should be + shown on the help page and error messages. + Normally, environment variables are not shown. + :param prompt: If set to ``True`` or a non empty string then the + user will be prompted for input. If set to ``True`` the prompt + will be the option name capitalized. A deprecated option cannot be + prompted. + :param confirmation_prompt: Prompt a second time to confirm the + value if it was prompted for. Can be set to a string instead of + ``True`` to customize the message. + :param prompt_required: If set to ``False``, the user will be + prompted for input only when the option was specified as a flag + without a value. + :param hide_input: If this is ``True`` then the input on the prompt + will be hidden from the user. This is useful for password input. + :param is_flag: forces this option to act as a flag. The default is + auto detection. + :param flag_value: which value should be used for this flag if it's + enabled. This is set to a boolean automatically if + the option string contains a slash to mark two options. + :param multiple: if this is set to `True` then the argument is accepted + multiple times and recorded. This is similar to ``nargs`` + in how it works but supports arbitrary number of + arguments. + :param count: this flag makes an option increment an integer. + :param allow_from_autoenv: if this is enabled then the value of this + parameter will be pulled from an environment + variable in case a prefix is defined on the + context. + :param help: the help string. + :param hidden: hide this option from help outputs. + :param attrs: Other command arguments described in :class:`Parameter`. + + .. versionchanged:: 8.2 + ``envvar`` used with ``flag_value`` will always use the ``flag_value``, + previously it would use the value of the environment variable. + + .. versionchanged:: 8.1 + Help text indentation is cleaned here instead of only in the + ``@option`` decorator. + + .. versionchanged:: 8.1 + The ``show_default`` parameter overrides + ``Context.show_default``. + + .. versionchanged:: 8.1 + The default of a single option boolean flag is not shown if the + default value is ``False``. + + .. versionchanged:: 8.0.1 + ``type`` is detected from ``flag_value`` if given. + """ + + param_type_name = "option" + + def __init__( + self, + param_decls: cabc.Sequence[str] | None = None, + show_default: bool | str | None = None, + prompt: bool | str = False, + confirmation_prompt: bool | str = False, + prompt_required: bool = True, + hide_input: bool = False, + is_flag: bool | None = None, + flag_value: t.Any = UNSET, + multiple: bool = False, + count: bool = False, + allow_from_autoenv: bool = True, + type: types.ParamType | t.Any | None = None, + help: str | None = None, + hidden: bool = False, + show_choices: bool = True, + show_envvar: bool = False, + deprecated: bool | str = False, + **attrs: t.Any, + ) -> None: + if help: + help = inspect.cleandoc(help) + + super().__init__( + param_decls, type=type, multiple=multiple, deprecated=deprecated, **attrs + ) + + if prompt is True: + if self.name is None: + raise TypeError("'name' is required with 'prompt=True'.") + + prompt_text: str | None = self.name.replace("_", " ").capitalize() + elif prompt is False: + prompt_text = None + else: + prompt_text = prompt + + if deprecated: + deprecated_message = ( + f"(DEPRECATED: {deprecated})" + if isinstance(deprecated, str) + else "(DEPRECATED)" + ) + help = help + deprecated_message if help is not None else deprecated_message + + self.prompt = prompt_text + self.confirmation_prompt = confirmation_prompt + self.prompt_required = prompt_required + self.hide_input = hide_input + self.hidden = hidden + + # The _flag_needs_value property tells the parser that this option is a flag + # that cannot be used standalone and needs a value. With this information, the + # parser can determine whether to consider the next user-provided argument in + # the CLI as a value for this flag or as a new option. + # If prompt is enabled but not required, then it opens the possibility for the + # option to gets its value from the user. + self._flag_needs_value = self.prompt is not None and not self.prompt_required + + # Auto-detect if this is a flag or not. + if is_flag is None: + # Implicitly a flag because flag_value was set. + if flag_value is not UNSET: + is_flag = True + # Not a flag, but when used as a flag it shows a prompt. + elif self._flag_needs_value: + is_flag = False + # Implicitly a flag because secondary options names were given. + elif self.secondary_opts: + is_flag = True + + # The option is explicitly not a flag, but to determine whether or not it needs + # value, we need to check if `flag_value` or `default` was set. Either one is + # sufficient. + # Ref: https://github.com/pallets/click/issues/3084 + elif is_flag is False and not self._flag_needs_value: + self._flag_needs_value = flag_value is not UNSET or self.default is UNSET + + if is_flag: + # Set missing default for flags if not explicitly required or prompted. + if self.default is UNSET and not self.required and not self.prompt: + if multiple: + self.default = () + + # Auto-detect the type of the flag based on the flag_value. + if type is None: + # A flag without a flag_value is a boolean flag. + if flag_value is UNSET: + self.type: types.ParamType = types.BoolParamType() + # If the flag value is a boolean, use BoolParamType. + elif isinstance(flag_value, bool): + self.type = types.BoolParamType() + # Otherwise, guess the type from the flag value. + else: + self.type = types.convert_type(None, flag_value) + + self.is_flag: bool = bool(is_flag) + self.is_bool_flag: bool = bool( + is_flag and isinstance(self.type, types.BoolParamType) + ) + self.flag_value: t.Any = flag_value + + # Set boolean flag default to False if unset and not required. + if self.is_bool_flag: + if self.default is UNSET and not self.required: + self.default = False + + # The alignement of default to the flag_value is resolved lazily in + # get_default() to prevent callable flag_values (like classes) from + # being instantiated. Refs: + # https://github.com/pallets/click/issues/3121 + # https://github.com/pallets/click/issues/3024#issuecomment-3146199461 + # https://github.com/pallets/click/pull/3030/commits/06847da + + # Set the default flag_value if it is not set. + if self.flag_value is UNSET: + if self.is_flag: + self.flag_value = True + else: + self.flag_value = None + + # Counting. + self.count = count + if count: + if type is None: + self.type = types.IntRange(min=0) + if self.default is UNSET: + self.default = 0 + + self.allow_from_autoenv = allow_from_autoenv + self.help = help + self.show_default = show_default + self.show_choices = show_choices + self.show_envvar = show_envvar + + if __debug__: + if deprecated and prompt: + raise ValueError("`deprecated` options cannot use `prompt`.") + + if self.nargs == -1: + raise TypeError("nargs=-1 is not supported for options.") + + if not self.is_bool_flag and self.secondary_opts: + raise TypeError("Secondary flag is not valid for non-boolean flag.") + + if self.is_bool_flag and self.hide_input and self.prompt is not None: + raise TypeError( + "'prompt' with 'hide_input' is not valid for boolean flag." + ) + + if self.count: + if self.multiple: + raise TypeError("'count' is not valid with 'multiple'.") + + if self.is_flag: + raise TypeError("'count' is not valid with 'is_flag'.") + + def to_info_dict(self) -> dict[str, t.Any]: + """ + .. versionchanged:: 8.3.0 + Returns ``None`` for the :attr:`flag_value` if it was not set. + """ + info_dict = super().to_info_dict() + info_dict.update( + help=self.help, + prompt=self.prompt, + is_flag=self.is_flag, + # We explicitly hide the :attr:`UNSET` value to the user, as we choose to + # make it an implementation detail. And because ``to_info_dict`` has been + # designed for documentation purposes, we return ``None`` instead. + flag_value=self.flag_value if self.flag_value is not UNSET else None, + count=self.count, + hidden=self.hidden, + ) + return info_dict + + def get_default( + self, ctx: Context, call: bool = True + ) -> t.Any | t.Callable[[], t.Any] | None: + """Return the default value for this option. + + For non-boolean flag options, ``default=True`` is treated as a sentinel + meaning "activate this flag by default" and is resolved to + :attr:`flag_value`. For example, with ``--upper/--lower`` feature + switches where ``flag_value="upper"`` and ``default=True``, the default + resolves to ``"upper"``. + + .. caution:: + This substitution only applies to non-boolean flags + (:attr:`is_bool_flag` is ``False``). For boolean flags, ``True`` is + a legitimate Python value and ``default=True`` is returned as-is. + + .. versionchanged:: 8.3.3 + ``default=True`` is no longer substituted with ``flag_value`` for + boolean flags, fixing negative boolean flags like + ``flag_value=False, default=True``. + """ + value = super().get_default(ctx, call=False) + + # Resolve default=True to flag_value lazily (here instead of + # __init__) to prevent callable flag_values (like classes) from + # being instantiated by the callable check below. + if value is True and self.is_flag and not self.is_bool_flag: + value = self.flag_value + elif call and callable(value): + value = value() + + return value + + def get_error_hint(self, ctx: Context) -> str: + result = super().get_error_hint(ctx) + if self.show_envvar and self.envvar is not None: + result += f" (env var: '{self.envvar}')" + return result + + def _parse_decls( + self, decls: cabc.Sequence[str], expose_value: bool + ) -> tuple[str | None, list[str], list[str]]: + opts = [] + secondary_opts = [] + name = None + possible_names = [] + + for decl in decls: + if decl.isidentifier(): + if name is not None: + raise TypeError(f"Name '{name}' defined twice") + name = decl + else: + split_char = ";" if decl[:1] == "/" else "/" + if split_char in decl: + first, second = decl.split(split_char, 1) + first = first.rstrip() + if first: + possible_names.append(_split_opt(first)) + opts.append(first) + second = second.lstrip() + if second: + secondary_opts.append(second.lstrip()) + if first == second: + raise ValueError( + f"Boolean option {decl!r} cannot use the" + " same flag for true/false." + ) + else: + possible_names.append(_split_opt(decl)) + opts.append(decl) + + if name is None and possible_names: + possible_names.sort(key=lambda x: -len(x[0])) # group long options first + name = possible_names[0][1].replace("-", "_").lower() + if not name.isidentifier(): + name = None + + if name is None: + if not expose_value: + return None, opts, secondary_opts + raise TypeError( + f"Could not determine name for option with declarations {decls!r}" + ) + + if not opts and not secondary_opts: + raise TypeError( + f"No options defined but a name was passed ({name})." + " Did you mean to declare an argument instead? Did" + f" you mean to pass '--{name}'?" + ) + + return name, opts, secondary_opts + + def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None: + if self.multiple: + action = "append" + elif self.count: + action = "count" + else: + action = "store" + + if self.is_flag: + action = f"{action}_const" + + if self.is_bool_flag and self.secondary_opts: + parser.add_option( + obj=self, opts=self.opts, dest=self.name, action=action, const=True + ) + parser.add_option( + obj=self, + opts=self.secondary_opts, + dest=self.name, + action=action, + const=False, + ) + else: + parser.add_option( + obj=self, + opts=self.opts, + dest=self.name, + action=action, + const=self.flag_value, + ) + else: + parser.add_option( + obj=self, + opts=self.opts, + dest=self.name, + action=action, + nargs=self.nargs, + ) + + def get_help_record(self, ctx: Context) -> tuple[str, str] | None: + if self.hidden: + return None + + any_prefix_is_slash = False + + def _write_opts(opts: cabc.Sequence[str]) -> str: + nonlocal any_prefix_is_slash + + rv, any_slashes = join_options(opts) + + if any_slashes: + any_prefix_is_slash = True + + if not self.is_flag and not self.count: + rv += f" {self.make_metavar(ctx=ctx)}" + + return rv + + rv = [_write_opts(self.opts)] + + if self.secondary_opts: + rv.append(_write_opts(self.secondary_opts)) + + help = self.help or "" + + extra = self.get_help_extra(ctx) + extra_items = [] + if "envvars" in extra: + extra_items.append( + _("env var: {var}").format(var=", ".join(extra["envvars"])) + ) + if "default" in extra: + extra_items.append(_("default: {default}").format(default=extra["default"])) + if "range" in extra: + extra_items.append(extra["range"]) + if "required" in extra: + extra_items.append(_(extra["required"])) + + if extra_items: + extra_str = "; ".join(extra_items) + help = f"{help} [{extra_str}]" if help else f"[{extra_str}]" + + return ("; " if any_prefix_is_slash else " / ").join(rv), help + + def get_help_extra(self, ctx: Context) -> types.OptionHelpExtra: + extra: types.OptionHelpExtra = {} + + if self.show_envvar: + envvar = self.envvar + + if envvar is None: + if ( + self.allow_from_autoenv + and ctx.auto_envvar_prefix is not None + and self.name is not None + ): + envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}" + + if envvar is not None: + if isinstance(envvar, str): + extra["envvars"] = (envvar,) + else: + extra["envvars"] = tuple(str(d) for d in envvar) + + # Temporarily enable resilient parsing to avoid type casting + # failing for the default. Might be possible to extend this to + # help formatting in general. + resilient = ctx.resilient_parsing + ctx.resilient_parsing = True + + try: + default_value = self.get_default(ctx, call=False) + finally: + ctx.resilient_parsing = resilient + + show_default = False + show_default_is_str = False + + if self.show_default is not None: + if isinstance(self.show_default, str): + show_default_is_str = show_default = True + else: + show_default = self.show_default + elif ctx.show_default is not None: + show_default = ctx.show_default + + if show_default_is_str or ( + show_default and (default_value not in (None, UNSET)) + ): + if show_default_is_str: + default_string = f"({self.show_default})" + elif isinstance(default_value, (list, tuple)): + default_string = ", ".join(str(d) for d in default_value) + elif isinstance(default_value, enum.Enum): + default_string = default_value.name + elif inspect.isfunction(default_value): + default_string = _("(dynamic)") + elif self.is_bool_flag and self.secondary_opts: + # For boolean flags that have distinct True/False opts, + # use the opt without prefix instead of the value. + default_string = _split_opt( + (self.opts if default_value else self.secondary_opts)[0] + )[1] + elif self.is_bool_flag and not self.secondary_opts and not default_value: + default_string = "" + elif isinstance(default_value, str) and default_value == "": + default_string = '""' + else: + default_string = str(default_value) + + if default_string: + extra["default"] = default_string + + if ( + isinstance(self.type, types._NumberRangeBase) + # skip count with default range type + and not (self.count and self.type.min == 0 and self.type.max is None) + ): + range_str = self.type._describe_range() + + if range_str: + extra["range"] = range_str + + if self.required: + extra["required"] = "required" + + return extra + + def prompt_for_value(self, ctx: Context) -> t.Any: + """This is an alternative flow that can be activated in the full + value processing if a value does not exist. It will prompt the + user until a valid value exists and then returns the processed + value as result. + """ + assert self.prompt is not None + + # Calculate the default before prompting anything to lock in the value before + # attempting any user interaction. + default = self.get_default(ctx) + + # A boolean flag can use a simplified [y/n] confirmation prompt. + if self.is_bool_flag: + # If we have no boolean default, we force the user to explicitly provide + # one. + if default in (UNSET, None): + default = None + # Nothing prevent you to declare an option that is simultaneously: + # 1) auto-detected as a boolean flag, + # 2) allowed to prompt, and + # 3) still declare a non-boolean default. + # This forced casting into a boolean is necessary to align any non-boolean + # default to the prompt, which is going to be a [y/n]-style confirmation + # because the option is still a boolean flag. That way, instead of [y/n], + # we get [Y/n] or [y/N] depending on the truthy value of the default. + # Refs: https://github.com/pallets/click/pull/3030#discussion_r2289180249 + else: + default = bool(default) + return confirm(self.prompt, default) + + # If show_default is given, provide this to `prompt` as well, + # otherwise we use `prompt`'s default behavior + prompt_kwargs: t.Any = {} + if self.show_default is not None: + prompt_kwargs["show_default"] = self.show_default + + return prompt( + self.prompt, + # Use ``None`` to inform the prompt() function to reiterate until a valid + # value is provided by the user if we have no default. + default=None if default is UNSET else default, + type=self.type, + hide_input=self.hide_input, + show_choices=self.show_choices, + confirmation_prompt=self.confirmation_prompt, + value_proc=lambda x: self.process_value(ctx, x), + **prompt_kwargs, + ) + + def resolve_envvar_value(self, ctx: Context) -> str | None: + """:class:`Option` resolves its environment variable the same way as + :func:`Parameter.resolve_envvar_value`, but it also supports + :attr:`Context.auto_envvar_prefix`. If we could not find an environment from + the :attr:`envvar` property, we fallback on :attr:`Context.auto_envvar_prefix` + to build dynamiccaly the environment variable name using the + :python:`{ctx.auto_envvar_prefix}_{self.name.upper()}` template. + + :meta private: + """ + rv = super().resolve_envvar_value(ctx) + + if rv is not None: + return rv + + if ( + self.allow_from_autoenv + and ctx.auto_envvar_prefix is not None + and self.name is not None + ): + envvar = f"{ctx.auto_envvar_prefix}_{self.name.upper()}" + rv = os.environ.get(envvar) + + if rv: + return rv + + return None + + def value_from_envvar(self, ctx: Context) -> t.Any: + """For :class:`Option`, this method processes the raw environment variable + string the same way as :func:`Parameter.value_from_envvar` does. + + But in the case of non-boolean flags, the value is analyzed to determine if the + flag is activated or not, and returns a boolean of its activation, or the + :attr:`flag_value` if the latter is set. + + This method also takes care of repeated options (i.e. options with + :attr:`multiple` set to ``True``). + + :meta private: + """ + rv = self.resolve_envvar_value(ctx) + + # Absent environment variable or an empty string is interpreted as unset. + if rv is None: + return None + + # Non-boolean flags are more liberal in what they accept. But a flag being a + # flag, its envvar value still needs to be analyzed to determine if the flag is + # activated or not. + if self.is_flag and not self.is_bool_flag: + # If the flag_value is set and match the envvar value, return it + # directly. + if self.flag_value is not UNSET and rv == self.flag_value: + return self.flag_value + # Analyze the envvar value as a boolean to know if the flag is + # activated or not. + return types.BoolParamType.str_to_bool(rv) + + # Split the envvar value if it is allowed to be repeated. + value_depth = (self.nargs != 1) + bool(self.multiple) + if value_depth > 0: + multi_rv = self.type.split_envvar_value(rv) + if self.multiple and self.nargs != 1: + multi_rv = batch(multi_rv, self.nargs) # type: ignore[assignment] + + return multi_rv + + return rv + + def consume_value( + self, ctx: Context, opts: cabc.Mapping[str, Parameter] + ) -> tuple[t.Any, ParameterSource]: + """For :class:`Option`, the value can be collected from an interactive prompt + if the option is a flag that needs a value (and the :attr:`prompt` property is + set). + + Additionally, this method handles flag option that are activated without a + value, in which case the :attr:`flag_value` is returned. + + :meta private: + """ + value, source = super().consume_value(ctx, opts) + + # The parser will emit a sentinel value if the option is allowed to as a flag + # without a value. + if value is FLAG_NEEDS_VALUE: + # If the option allows for a prompt, we start an interaction with the user. + if self.prompt is not None and not ctx.resilient_parsing: + value = self.prompt_for_value(ctx) + source = ParameterSource.PROMPT + # Else the flag takes its flag_value as value. + else: + value = self.flag_value + source = ParameterSource.COMMANDLINE + + # A flag which is activated always returns the flag value, unless the value + # comes from the explicitly sets default. + elif ( + self.is_flag + and value is True + and not self.is_bool_flag + and source < ParameterSource.DEFAULT_MAP + ): + value = self.flag_value + + # Re-interpret a multiple option which has been sent as-is by the parser. + # Here we replace each occurrence of value-less flags (marked by the + # FLAG_NEEDS_VALUE sentinel) with the flag_value. + elif ( + self.multiple + and value is not UNSET + and source < ParameterSource.DEFAULT_MAP + and any(v is FLAG_NEEDS_VALUE for v in value) + ): + value = [self.flag_value if v is FLAG_NEEDS_VALUE else v for v in value] + source = ParameterSource.COMMANDLINE + + # The value wasn't set, or used the param's default, prompt for one to the user + # if prompting is enabled. + elif ( + (value is UNSET or source >= ParameterSource.DEFAULT_MAP) + and self.prompt is not None + and (self.required or self.prompt_required) + and not ctx.resilient_parsing + ): + value = self.prompt_for_value(ctx) + source = ParameterSource.PROMPT + + return value, source + + def process_value(self, ctx: Context, value: t.Any) -> t.Any: + # process_value has to be overridden on Options in order to capture + # `value == UNSET` cases before `type_cast_value()` gets called. + # + # Refs: + # https://github.com/pallets/click/issues/3069 + if self.is_flag and not self.required and self.is_bool_flag and value is UNSET: + value = False + + if self.callback is not None: + value = self.callback(ctx, self, value) + + return value + + # in the normal case, rely on Parameter.process_value + return super().process_value(ctx, value) + + +class Argument(Parameter): + """Arguments are positional parameters to a command. They generally + provide fewer features than options but can have infinite ``nargs`` + and are required by default. + + All parameters are passed onwards to the constructor of :class:`Parameter`. + """ + + param_type_name = "argument" + + def __init__( + self, + param_decls: cabc.Sequence[str], + required: bool | None = None, + **attrs: t.Any, + ) -> None: + # Auto-detect the requirement status of the argument if not explicitly set. + if required is None: + # The argument gets automatically required if it has no explicit default + # value set and is setup to match at least one value. + if attrs.get("default", UNSET) is UNSET: + required = attrs.get("nargs", 1) > 0 + # If the argument has a default value, it is not required. + else: + required = False + + if "multiple" in attrs: + raise TypeError("__init__() got an unexpected keyword argument 'multiple'.") + + super().__init__(param_decls, required=required, **attrs) + + @property + def human_readable_name(self) -> str: + if self.metavar is not None: + return self.metavar + return self.name.upper() # type: ignore + + def make_metavar(self, ctx: Context) -> str: + if self.metavar is not None: + return self.metavar + var = self.type.get_metavar(param=self, ctx=ctx) + if not var: + var = self.name.upper() # type: ignore + if self.deprecated: + var += "!" + if not self.required: + var = f"[{var}]" + if self.nargs != 1: + var += "..." + return var + + def _parse_decls( + self, decls: cabc.Sequence[str], expose_value: bool + ) -> tuple[str | None, list[str], list[str]]: + if not decls: + if not expose_value: + return None, [], [] + raise TypeError("Argument is marked as exposed, but does not have a name.") + if len(decls) == 1: + name = arg = decls[0] + name = name.replace("-", "_").lower() + else: + raise TypeError( + "Arguments take exactly one parameter declaration, got" + f" {len(decls)}: {decls}." + ) + return name, [arg], [] + + def get_usage_pieces(self, ctx: Context) -> list[str]: + return [self.make_metavar(ctx)] + + def get_error_hint(self, ctx: Context) -> str: + return f"'{self.make_metavar(ctx)}'" + + def add_to_parser(self, parser: _OptionParser, ctx: Context) -> None: + parser.add_argument(dest=self.name, nargs=self.nargs, obj=self) + + +def __getattr__(name: str) -> object: + import warnings + + if name == "BaseCommand": + warnings.warn( + "'BaseCommand' is deprecated and will be removed in Click 9.0. Use" + " 'Command' instead.", + DeprecationWarning, + stacklevel=2, + ) + return _BaseCommand + + if name == "MultiCommand": + warnings.warn( + "'MultiCommand' is deprecated and will be removed in Click 9.0. Use" + " 'Group' instead.", + DeprecationWarning, + stacklevel=2, + ) + return _MultiCommand + + raise AttributeError(name) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/decorators.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/decorators.py new file mode 100644 index 0000000000..21f4c34224 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/decorators.py @@ -0,0 +1,551 @@ +from __future__ import annotations + +import inspect +import typing as t +from functools import update_wrapper +from gettext import gettext as _ + +from .core import Argument +from .core import Command +from .core import Context +from .core import Group +from .core import Option +from .core import Parameter +from .globals import get_current_context +from .utils import echo + +if t.TYPE_CHECKING: + import typing_extensions as te + + P = te.ParamSpec("P") + +R = t.TypeVar("R") +T = t.TypeVar("T") +_AnyCallable = t.Callable[..., t.Any] +FC = t.TypeVar("FC", bound="_AnyCallable | Command") + + +def pass_context(f: t.Callable[te.Concatenate[Context, P], R]) -> t.Callable[P, R]: + """Marks a callback as wanting to receive the current context + object as first argument. + """ + + def new_func(*args: P.args, **kwargs: P.kwargs) -> R: + return f(get_current_context(), *args, **kwargs) + + return update_wrapper(new_func, f) + + +def pass_obj(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]: + """Similar to :func:`pass_context`, but only pass the object on the + context onwards (:attr:`Context.obj`). This is useful if that object + represents the state of a nested system. + """ + + def new_func(*args: P.args, **kwargs: P.kwargs) -> R: + return f(get_current_context().obj, *args, **kwargs) + + return update_wrapper(new_func, f) + + +def make_pass_decorator( + object_type: type[T], ensure: bool = False +) -> t.Callable[[t.Callable[te.Concatenate[T, P], R]], t.Callable[P, R]]: + """Given an object type this creates a decorator that will work + similar to :func:`pass_obj` but instead of passing the object of the + current context, it will find the innermost context of type + :func:`object_type`. + + This generates a decorator that works roughly like this:: + + from functools import update_wrapper + + def decorator(f): + @pass_context + def new_func(ctx, *args, **kwargs): + obj = ctx.find_object(object_type) + return ctx.invoke(f, obj, *args, **kwargs) + return update_wrapper(new_func, f) + return decorator + + :param object_type: the type of the object to pass. + :param ensure: if set to `True`, a new object will be created and + remembered on the context if it's not there yet. + """ + + def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]: + def new_func(*args: P.args, **kwargs: P.kwargs) -> R: + ctx = get_current_context() + + obj: T | None + if ensure: + obj = ctx.ensure_object(object_type) + else: + obj = ctx.find_object(object_type) + + if obj is None: + raise RuntimeError( + "Managed to invoke callback without a context" + f" object of type {object_type.__name__!r}" + " existing." + ) + + return ctx.invoke(f, obj, *args, **kwargs) + + return update_wrapper(new_func, f) + + return decorator + + +def pass_meta_key( + key: str, *, doc_description: str | None = None +) -> t.Callable[[t.Callable[te.Concatenate[T, P], R]], t.Callable[P, R]]: + """Create a decorator that passes a key from + :attr:`click.Context.meta` as the first argument to the decorated + function. + + :param key: Key in ``Context.meta`` to pass. + :param doc_description: Description of the object being passed, + inserted into the decorator's docstring. Defaults to "the 'key' + key from Context.meta". + + .. versionadded:: 8.0 + """ + + def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, R]: + def new_func(*args: P.args, **kwargs: P.kwargs) -> R: + ctx = get_current_context() + obj = ctx.meta[key] + return ctx.invoke(f, obj, *args, **kwargs) + + return update_wrapper(new_func, f) + + if doc_description is None: + doc_description = f"the {key!r} key from :attr:`click.Context.meta`" + + decorator.__doc__ = ( + f"Decorator that passes {doc_description} as the first argument" + " to the decorated function." + ) + return decorator + + +CmdType = t.TypeVar("CmdType", bound=Command) + + +# variant: no call, directly as decorator for a function. +@t.overload +def command(name: _AnyCallable) -> Command: ... + + +# variant: with positional name and with positional or keyword cls argument: +# @command(namearg, CommandCls, ...) or @command(namearg, cls=CommandCls, ...) +@t.overload +def command( + name: str | None, + cls: type[CmdType], + **attrs: t.Any, +) -> t.Callable[[_AnyCallable], CmdType]: ... + + +# variant: name omitted, cls _must_ be a keyword argument, @command(cls=CommandCls, ...) +@t.overload +def command( + name: None = None, + *, + cls: type[CmdType], + **attrs: t.Any, +) -> t.Callable[[_AnyCallable], CmdType]: ... + + +# variant: with optional string name, no cls argument provided. +@t.overload +def command( + name: str | None = ..., cls: None = None, **attrs: t.Any +) -> t.Callable[[_AnyCallable], Command]: ... + + +def command( + name: str | _AnyCallable | None = None, + cls: type[CmdType] | None = None, + **attrs: t.Any, +) -> Command | t.Callable[[_AnyCallable], Command | CmdType]: + r"""Creates a new :class:`Command` and uses the decorated function as + callback. This will also automatically attach all decorated + :func:`option`\s and :func:`argument`\s as parameters to the command. + + The name of the command defaults to the name of the function, converted to + lowercase, with underscores ``_`` replaced by dashes ``-``, and the suffixes + ``_command``, ``_cmd``, ``_group``, and ``_grp`` are removed. For example, + ``init_data_command`` becomes ``init-data``. + + All keyword arguments are forwarded to the underlying command class. + For the ``params`` argument, any decorated params are appended to + the end of the list. + + Once decorated the function turns into a :class:`Command` instance + that can be invoked as a command line utility or be attached to a + command :class:`Group`. + + :param name: The name of the command. Defaults to modifying the function's + name as described above. + :param cls: The command class to create. Defaults to :class:`Command`. + + .. versionchanged:: 8.2 + The suffixes ``_command``, ``_cmd``, ``_group``, and ``_grp`` are + removed when generating the name. + + .. versionchanged:: 8.1 + This decorator can be applied without parentheses. + + .. versionchanged:: 8.1 + The ``params`` argument can be used. Decorated params are + appended to the end of the list. + """ + + func: t.Callable[[_AnyCallable], t.Any] | None = None + + if callable(name): + func = name + name = None + assert cls is None, "Use 'command(cls=cls)(callable)' to specify a class." + assert not attrs, "Use 'command(**kwargs)(callable)' to provide arguments." + + if cls is None: + cls = t.cast("type[CmdType]", Command) + + def decorator(f: _AnyCallable) -> CmdType: + if isinstance(f, Command): + raise TypeError("Attempted to convert a callback into a command twice.") + + attr_params = attrs.pop("params", None) + params = attr_params if attr_params is not None else [] + + try: + decorator_params = f.__click_params__ # type: ignore + except AttributeError: + pass + else: + del f.__click_params__ # type: ignore + params.extend(reversed(decorator_params)) + + if attrs.get("help") is None: + attrs["help"] = f.__doc__ + + if t.TYPE_CHECKING: + assert cls is not None + assert not callable(name) + + if name is not None: + cmd_name = name + else: + cmd_name = f.__name__.lower().replace("_", "-") + cmd_left, sep, suffix = cmd_name.rpartition("-") + + if sep and suffix in {"command", "cmd", "group", "grp"}: + cmd_name = cmd_left + + cmd = cls(name=cmd_name, callback=f, params=params, **attrs) + cmd.__doc__ = f.__doc__ + return cmd + + if func is not None: + return decorator(func) + + return decorator + + +GrpType = t.TypeVar("GrpType", bound=Group) + + +# variant: no call, directly as decorator for a function. +@t.overload +def group(name: _AnyCallable) -> Group: ... + + +# variant: with positional name and with positional or keyword cls argument: +# @group(namearg, GroupCls, ...) or @group(namearg, cls=GroupCls, ...) +@t.overload +def group( + name: str | None, + cls: type[GrpType], + **attrs: t.Any, +) -> t.Callable[[_AnyCallable], GrpType]: ... + + +# variant: name omitted, cls _must_ be a keyword argument, @group(cmd=GroupCls, ...) +@t.overload +def group( + name: None = None, + *, + cls: type[GrpType], + **attrs: t.Any, +) -> t.Callable[[_AnyCallable], GrpType]: ... + + +# variant: with optional string name, no cls argument provided. +@t.overload +def group( + name: str | None = ..., cls: None = None, **attrs: t.Any +) -> t.Callable[[_AnyCallable], Group]: ... + + +def group( + name: str | _AnyCallable | None = None, + cls: type[GrpType] | None = None, + **attrs: t.Any, +) -> Group | t.Callable[[_AnyCallable], Group | GrpType]: + """Creates a new :class:`Group` with a function as callback. This + works otherwise the same as :func:`command` just that the `cls` + parameter is set to :class:`Group`. + + .. versionchanged:: 8.1 + This decorator can be applied without parentheses. + """ + if cls is None: + cls = t.cast("type[GrpType]", Group) + + if callable(name): + return command(cls=cls, **attrs)(name) + + return command(name, cls, **attrs) + + +def _param_memo(f: t.Callable[..., t.Any], param: Parameter) -> None: + if isinstance(f, Command): + f.params.append(param) + else: + if not hasattr(f, "__click_params__"): + f.__click_params__ = [] # type: ignore + + f.__click_params__.append(param) # type: ignore + + +def argument( + *param_decls: str, cls: type[Argument] | None = None, **attrs: t.Any +) -> t.Callable[[FC], FC]: + """Attaches an argument to the command. All positional arguments are + passed as parameter declarations to :class:`Argument`; all keyword + arguments are forwarded unchanged (except ``cls``). + This is equivalent to creating an :class:`Argument` instance manually + and attaching it to the :attr:`Command.params` list. + + For the default argument class, refer to :class:`Argument` and + :class:`Parameter` for descriptions of parameters. + + :param cls: the argument class to instantiate. This defaults to + :class:`Argument`. + :param param_decls: Passed as positional arguments to the constructor of + ``cls``. + :param attrs: Passed as keyword arguments to the constructor of ``cls``. + """ + if cls is None: + cls = Argument + + def decorator(f: FC) -> FC: + _param_memo(f, cls(param_decls, **attrs)) + return f + + return decorator + + +def option( + *param_decls: str, cls: type[Option] | None = None, **attrs: t.Any +) -> t.Callable[[FC], FC]: + """Attaches an option to the command. All positional arguments are + passed as parameter declarations to :class:`Option`; all keyword + arguments are forwarded unchanged (except ``cls``). + This is equivalent to creating an :class:`Option` instance manually + and attaching it to the :attr:`Command.params` list. + + For the default option class, refer to :class:`Option` and + :class:`Parameter` for descriptions of parameters. + + :param cls: the option class to instantiate. This defaults to + :class:`Option`. + :param param_decls: Passed as positional arguments to the constructor of + ``cls``. + :param attrs: Passed as keyword arguments to the constructor of ``cls``. + """ + if cls is None: + cls = Option + + def decorator(f: FC) -> FC: + _param_memo(f, cls(param_decls, **attrs)) + return f + + return decorator + + +def confirmation_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]: + """Add a ``--yes`` option which shows a prompt before continuing if + not passed. If the prompt is declined, the program will exit. + + :param param_decls: One or more option names. Defaults to the single + value ``"--yes"``. + :param kwargs: Extra arguments are passed to :func:`option`. + """ + + def callback(ctx: Context, param: Parameter, value: bool) -> None: + if not value: + ctx.abort() + + if not param_decls: + param_decls = ("--yes",) + + kwargs.setdefault("is_flag", True) + kwargs.setdefault("callback", callback) + kwargs.setdefault("expose_value", False) + kwargs.setdefault("prompt", "Do you want to continue?") + kwargs.setdefault("help", "Confirm the action without prompting.") + return option(*param_decls, **kwargs) + + +def password_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]: + """Add a ``--password`` option which prompts for a password, hiding + input and asking to enter the value again for confirmation. + + :param param_decls: One or more option names. Defaults to the single + value ``"--password"``. + :param kwargs: Extra arguments are passed to :func:`option`. + """ + if not param_decls: + param_decls = ("--password",) + + kwargs.setdefault("prompt", True) + kwargs.setdefault("confirmation_prompt", True) + kwargs.setdefault("hide_input", True) + return option(*param_decls, **kwargs) + + +def version_option( + version: str | None = None, + *param_decls: str, + package_name: str | None = None, + prog_name: str | None = None, + message: str | None = None, + **kwargs: t.Any, +) -> t.Callable[[FC], FC]: + """Add a ``--version`` option which immediately prints the version + number and exits the program. + + If ``version`` is not provided, Click will try to detect it using + :func:`importlib.metadata.version` to get the version for the + ``package_name``. + + If ``package_name`` is not provided, Click will try to detect it by + inspecting the stack frames. This will be used to detect the + version, so it must match the name of the installed package. + + :param version: The version number to show. If not provided, Click + will try to detect it. + :param param_decls: One or more option names. Defaults to the single + value ``"--version"``. + :param package_name: The package name to detect the version from. If + not provided, Click will try to detect it. + :param prog_name: The name of the CLI to show in the message. If not + provided, it will be detected from the command. + :param message: The message to show. The values ``%(prog)s``, + ``%(package)s``, and ``%(version)s`` are available. Defaults to + ``"%(prog)s, version %(version)s"``. + :param kwargs: Extra arguments are passed to :func:`option`. + :raise RuntimeError: ``version`` could not be detected. + + .. versionchanged:: 8.0 + Add the ``package_name`` parameter, and the ``%(package)s`` + value for messages. + + .. versionchanged:: 8.0 + Use :mod:`importlib.metadata` instead of ``pkg_resources``. The + version is detected based on the package name, not the entry + point name. The Python package name must match the installed + package name, or be passed with ``package_name=``. + """ + if message is None: + message = _("%(prog)s, version %(version)s") + + if version is None and package_name is None: + frame = inspect.currentframe() + f_back = frame.f_back if frame is not None else None + f_globals = f_back.f_globals if f_back is not None else None + # break reference cycle + # https://docs.python.org/3/library/inspect.html#the-interpreter-stack + del frame + + if f_globals is not None: + package_name = f_globals.get("__name__") + + if package_name == "__main__": + package_name = f_globals.get("__package__") + + if package_name: + package_name = package_name.partition(".")[0] + + def callback(ctx: Context, param: Parameter, value: bool) -> None: + if not value or ctx.resilient_parsing: + return + + nonlocal prog_name + nonlocal version + + if prog_name is None: + prog_name = ctx.find_root().info_name + + if version is None and package_name is not None: + import importlib.metadata + + try: + version = importlib.metadata.version(package_name) + except importlib.metadata.PackageNotFoundError: + raise RuntimeError( + f"{package_name!r} is not installed. Try passing" + " 'package_name' instead." + ) from None + + if version is None: + raise RuntimeError( + f"Could not determine the version for {package_name!r} automatically." + ) + + echo( + message % {"prog": prog_name, "package": package_name, "version": version}, + color=ctx.color, + ) + ctx.exit() + + if not param_decls: + param_decls = ("--version",) + + kwargs.setdefault("is_flag", True) + kwargs.setdefault("expose_value", False) + kwargs.setdefault("is_eager", True) + kwargs.setdefault("help", _("Show the version and exit.")) + kwargs["callback"] = callback + return option(*param_decls, **kwargs) + + +def help_option(*param_decls: str, **kwargs: t.Any) -> t.Callable[[FC], FC]: + """Pre-configured ``--help`` option which immediately prints the help page + and exits the program. + + :param param_decls: One or more option names. Defaults to the single + value ``"--help"``. + :param kwargs: Extra arguments are passed to :func:`option`. + """ + + def show_help(ctx: Context, param: Parameter, value: bool) -> None: + """Callback that print the help page on ```` and exits.""" + if value and not ctx.resilient_parsing: + echo(ctx.get_help(), color=ctx.color) + ctx.exit() + + if not param_decls: + param_decls = ("--help",) + + kwargs.setdefault("is_flag", True) + kwargs.setdefault("expose_value", False) + kwargs.setdefault("is_eager", True) + kwargs.setdefault("help", _("Show this message and exit.")) + kwargs.setdefault("callback", show_help) + + return option(*param_decls, **kwargs) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/exceptions.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/exceptions.py new file mode 100644 index 0000000000..4d782ee361 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/exceptions.py @@ -0,0 +1,308 @@ +from __future__ import annotations + +import collections.abc as cabc +import typing as t +from gettext import gettext as _ +from gettext import ngettext + +from ._compat import get_text_stderr +from .globals import resolve_color_default +from .utils import echo +from .utils import format_filename + +if t.TYPE_CHECKING: + from .core import Command + from .core import Context + from .core import Parameter + + +def _join_param_hints(param_hint: cabc.Sequence[str] | str | None) -> str | None: + if param_hint is not None and not isinstance(param_hint, str): + return " / ".join(repr(x) for x in param_hint) + + return param_hint + + +class ClickException(Exception): + """An exception that Click can handle and show to the user.""" + + #: The exit code for this exception. + exit_code = 1 + + def __init__(self, message: str) -> None: + super().__init__(message) + # The context will be removed by the time we print the message, so cache + # the color settings here to be used later on (in `show`) + self.show_color: bool | None = resolve_color_default() + self.message = message + + def format_message(self) -> str: + return self.message + + def __str__(self) -> str: + return self.message + + def show(self, file: t.IO[t.Any] | None = None) -> None: + if file is None: + file = get_text_stderr() + + echo( + _("Error: {message}").format(message=self.format_message()), + file=file, + color=self.show_color, + ) + + +class UsageError(ClickException): + """An internal exception that signals a usage error. This typically + aborts any further handling. + + :param message: the error message to display. + :param ctx: optionally the context that caused this error. Click will + fill in the context automatically in some situations. + """ + + exit_code = 2 + + def __init__(self, message: str, ctx: Context | None = None) -> None: + super().__init__(message) + self.ctx = ctx + self.cmd: Command | None = self.ctx.command if self.ctx else None + + def show(self, file: t.IO[t.Any] | None = None) -> None: + if file is None: + file = get_text_stderr() + color = None + hint = "" + if ( + self.ctx is not None + and self.ctx.command.get_help_option(self.ctx) is not None + ): + hint = _("Try '{command} {option}' for help.").format( + command=self.ctx.command_path, option=self.ctx.help_option_names[0] + ) + hint = f"{hint}\n" + if self.ctx is not None: + color = self.ctx.color + echo(f"{self.ctx.get_usage()}\n{hint}", file=file, color=color) + echo( + _("Error: {message}").format(message=self.format_message()), + file=file, + color=color, + ) + + +class BadParameter(UsageError): + """An exception that formats out a standardized error message for a + bad parameter. This is useful when thrown from a callback or type as + Click will attach contextual information to it (for instance, which + parameter it is). + + .. versionadded:: 2.0 + + :param param: the parameter object that caused this error. This can + be left out, and Click will attach this info itself + if possible. + :param param_hint: a string that shows up as parameter name. This + can be used as alternative to `param` in cases + where custom validation should happen. If it is + a string it's used as such, if it's a list then + each item is quoted and separated. + """ + + def __init__( + self, + message: str, + ctx: Context | None = None, + param: Parameter | None = None, + param_hint: cabc.Sequence[str] | str | None = None, + ) -> None: + super().__init__(message, ctx) + self.param = param + self.param_hint = param_hint + + def format_message(self) -> str: + if self.param_hint is not None: + param_hint = self.param_hint + elif self.param is not None: + param_hint = self.param.get_error_hint(self.ctx) # type: ignore + else: + return _("Invalid value: {message}").format(message=self.message) + + return _("Invalid value for {param_hint}: {message}").format( + param_hint=_join_param_hints(param_hint), message=self.message + ) + + +class MissingParameter(BadParameter): + """Raised if click required an option or argument but it was not + provided when invoking the script. + + .. versionadded:: 4.0 + + :param param_type: a string that indicates the type of the parameter. + The default is to inherit the parameter type from + the given `param`. Valid values are ``'parameter'``, + ``'option'`` or ``'argument'``. + """ + + def __init__( + self, + message: str | None = None, + ctx: Context | None = None, + param: Parameter | None = None, + param_hint: cabc.Sequence[str] | str | None = None, + param_type: str | None = None, + ) -> None: + super().__init__(message or "", ctx, param, param_hint) + self.param_type = param_type + + def format_message(self) -> str: + if self.param_hint is not None: + param_hint: cabc.Sequence[str] | str | None = self.param_hint + elif self.param is not None: + param_hint = self.param.get_error_hint(self.ctx) # type: ignore + else: + param_hint = None + + param_hint = _join_param_hints(param_hint) + param_hint = f" {param_hint}" if param_hint else "" + + param_type = self.param_type + if param_type is None and self.param is not None: + param_type = self.param.param_type_name + + msg = self.message + if self.param is not None: + msg_extra = self.param.type.get_missing_message( + param=self.param, ctx=self.ctx + ) + if msg_extra: + if msg: + msg += f". {msg_extra}" + else: + msg = msg_extra + + msg = f" {msg}" if msg else "" + + # Translate param_type for known types. + if param_type == "argument": + missing = _("Missing argument") + elif param_type == "option": + missing = _("Missing option") + elif param_type == "parameter": + missing = _("Missing parameter") + else: + missing = _("Missing {param_type}").format(param_type=param_type) + + return f"{missing}{param_hint}.{msg}" + + def __str__(self) -> str: + if not self.message: + param_name = self.param.name if self.param else None + return _("Missing parameter: {param_name}").format(param_name=param_name) + else: + return self.message + + +class NoSuchOption(UsageError): + """Raised if click attempted to handle an option that does not + exist. + + .. versionadded:: 4.0 + """ + + def __init__( + self, + option_name: str, + message: str | None = None, + possibilities: cabc.Sequence[str] | None = None, + ctx: Context | None = None, + ) -> None: + if message is None: + message = _("No such option: {name}").format(name=option_name) + + super().__init__(message, ctx) + self.option_name = option_name + self.possibilities = possibilities + + def format_message(self) -> str: + if not self.possibilities: + return self.message + + possibility_str = ", ".join(sorted(self.possibilities)) + suggest = ngettext( + "Did you mean {possibility}?", + "(Possible options: {possibilities})", + len(self.possibilities), + ).format(possibility=possibility_str, possibilities=possibility_str) + return f"{self.message} {suggest}" + + +class BadOptionUsage(UsageError): + """Raised if an option is generally supplied but the use of the option + was incorrect. This is for instance raised if the number of arguments + for an option is not correct. + + .. versionadded:: 4.0 + + :param option_name: the name of the option being used incorrectly. + """ + + def __init__( + self, option_name: str, message: str, ctx: Context | None = None + ) -> None: + super().__init__(message, ctx) + self.option_name = option_name + + +class BadArgumentUsage(UsageError): + """Raised if an argument is generally supplied but the use of the argument + was incorrect. This is for instance raised if the number of values + for an argument is not correct. + + .. versionadded:: 6.0 + """ + + +class NoArgsIsHelpError(UsageError): + def __init__(self, ctx: Context) -> None: + self.ctx: Context + super().__init__(ctx.get_help(), ctx=ctx) + + def show(self, file: t.IO[t.Any] | None = None) -> None: + echo(self.format_message(), file=file, err=True, color=self.ctx.color) + + +class FileError(ClickException): + """Raised if a file cannot be opened.""" + + def __init__(self, filename: str, hint: str | None = None) -> None: + if hint is None: + hint = _("unknown error") + + super().__init__(hint) + self.ui_filename: str = format_filename(filename) + self.filename = filename + + def format_message(self) -> str: + return _("Could not open file {filename!r}: {message}").format( + filename=self.ui_filename, message=self.message + ) + + +class Abort(RuntimeError): + """An internal signalling exception that signals Click to abort.""" + + +class Exit(RuntimeError): + """An exception that indicates that the application should exit with some + status code. + + :param code: the status code to exit with. + """ + + __slots__ = ("exit_code",) + + def __init__(self, code: int = 0) -> None: + self.exit_code: int = code diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/formatting.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/formatting.py new file mode 100644 index 0000000000..0b64f831b5 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/formatting.py @@ -0,0 +1,301 @@ +from __future__ import annotations + +import collections.abc as cabc +from contextlib import contextmanager +from gettext import gettext as _ + +from ._compat import term_len +from .parser import _split_opt + +# Can force a width. This is used by the test system +FORCED_WIDTH: int | None = None + + +def measure_table(rows: cabc.Iterable[tuple[str, str]]) -> tuple[int, ...]: + widths: dict[int, int] = {} + + for row in rows: + for idx, col in enumerate(row): + widths[idx] = max(widths.get(idx, 0), term_len(col)) + + return tuple(y for x, y in sorted(widths.items())) + + +def iter_rows( + rows: cabc.Iterable[tuple[str, str]], col_count: int +) -> cabc.Iterator[tuple[str, ...]]: + for row in rows: + yield row + ("",) * (col_count - len(row)) + + +def wrap_text( + text: str, + width: int = 78, + initial_indent: str = "", + subsequent_indent: str = "", + preserve_paragraphs: bool = False, +) -> str: + """A helper function that intelligently wraps text. By default, it + assumes that it operates on a single paragraph of text but if the + `preserve_paragraphs` parameter is provided it will intelligently + handle paragraphs (defined by two empty lines). + + If paragraphs are handled, a paragraph can be prefixed with an empty + line containing the ``\\b`` character (``\\x08``) to indicate that + no rewrapping should happen in that block. + + :param text: the text that should be rewrapped. + :param width: the maximum width for the text. + :param initial_indent: the initial indent that should be placed on the + first line as a string. + :param subsequent_indent: the indent string that should be placed on + each consecutive line. + :param preserve_paragraphs: if this flag is set then the wrapping will + intelligently handle paragraphs. + """ + from ._textwrap import TextWrapper + + text = text.expandtabs() + wrapper = TextWrapper( + width, + initial_indent=initial_indent, + subsequent_indent=subsequent_indent, + replace_whitespace=False, + ) + if not preserve_paragraphs: + return wrapper.fill(text) + + p: list[tuple[int, bool, str]] = [] + buf: list[str] = [] + indent = None + + def _flush_par() -> None: + if not buf: + return + if buf[0].strip() == "\b": + p.append((indent or 0, True, "\n".join(buf[1:]))) + else: + p.append((indent or 0, False, " ".join(buf))) + del buf[:] + + for line in text.splitlines(): + if not line: + _flush_par() + indent = None + else: + if indent is None: + orig_len = term_len(line) + line = line.lstrip() + indent = orig_len - term_len(line) + buf.append(line) + _flush_par() + + rv = [] + for indent, raw, text in p: + with wrapper.extra_indent(" " * indent): + if raw: + rv.append(wrapper.indent_only(text)) + else: + rv.append(wrapper.fill(text)) + + return "\n\n".join(rv) + + +class HelpFormatter: + """This class helps with formatting text-based help pages. It's + usually just needed for very special internal cases, but it's also + exposed so that developers can write their own fancy outputs. + + At present, it always writes into memory. + + :param indent_increment: the additional increment for each level. + :param width: the width for the text. This defaults to the terminal + width clamped to a maximum of 78. + """ + + def __init__( + self, + indent_increment: int = 2, + width: int | None = None, + max_width: int | None = None, + ) -> None: + self.indent_increment = indent_increment + if max_width is None: + max_width = 80 + if width is None: + import shutil + + width = FORCED_WIDTH + if width is None: + width = max(min(shutil.get_terminal_size().columns, max_width) - 2, 50) + self.width = width + self.current_indent: int = 0 + self.buffer: list[str] = [] + + def write(self, string: str) -> None: + """Writes a unicode string into the internal buffer.""" + self.buffer.append(string) + + def indent(self) -> None: + """Increases the indentation.""" + self.current_indent += self.indent_increment + + def dedent(self) -> None: + """Decreases the indentation.""" + self.current_indent -= self.indent_increment + + def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> None: + """Writes a usage line into the buffer. + + :param prog: the program name. + :param args: whitespace separated list of arguments. + :param prefix: The prefix for the first line. Defaults to + ``"Usage: "``. + """ + if prefix is None: + prefix = f"{_('Usage:')} " + + usage_prefix = f"{prefix:>{self.current_indent}}{prog} " + text_width = self.width - self.current_indent + + if text_width >= (term_len(usage_prefix) + 20): + # The arguments will fit to the right of the prefix. + indent = " " * term_len(usage_prefix) + self.write( + wrap_text( + args, + text_width, + initial_indent=usage_prefix, + subsequent_indent=indent, + ) + ) + else: + # The prefix is too long, put the arguments on the next line. + self.write(usage_prefix) + self.write("\n") + indent = " " * (max(self.current_indent, term_len(prefix)) + 4) + self.write( + wrap_text( + args, text_width, initial_indent=indent, subsequent_indent=indent + ) + ) + + self.write("\n") + + def write_heading(self, heading: str) -> None: + """Writes a heading into the buffer.""" + self.write(f"{'':>{self.current_indent}}{heading}:\n") + + def write_paragraph(self) -> None: + """Writes a paragraph into the buffer.""" + if self.buffer: + self.write("\n") + + def write_text(self, text: str) -> None: + """Writes re-indented text into the buffer. This rewraps and + preserves paragraphs. + """ + indent = " " * self.current_indent + self.write( + wrap_text( + text, + self.width, + initial_indent=indent, + subsequent_indent=indent, + preserve_paragraphs=True, + ) + ) + self.write("\n") + + def write_dl( + self, + rows: cabc.Sequence[tuple[str, str]], + col_max: int = 30, + col_spacing: int = 2, + ) -> None: + """Writes a definition list into the buffer. This is how options + and commands are usually formatted. + + :param rows: a list of two item tuples for the terms and values. + :param col_max: the maximum width of the first column. + :param col_spacing: the number of spaces between the first and + second column. + """ + rows = list(rows) + widths = measure_table(rows) + if len(widths) != 2: + raise TypeError("Expected two columns for definition list") + + first_col = min(widths[0], col_max) + col_spacing + + for first, second in iter_rows(rows, len(widths)): + self.write(f"{'':>{self.current_indent}}{first}") + if not second: + self.write("\n") + continue + if term_len(first) <= first_col - col_spacing: + self.write(" " * (first_col - term_len(first))) + else: + self.write("\n") + self.write(" " * (first_col + self.current_indent)) + + text_width = max(self.width - first_col - 2, 10) + wrapped_text = wrap_text(second, text_width, preserve_paragraphs=True) + lines = wrapped_text.splitlines() + + if lines: + self.write(f"{lines[0]}\n") + + for line in lines[1:]: + self.write(f"{'':>{first_col + self.current_indent}}{line}\n") + else: + self.write("\n") + + @contextmanager + def section(self, name: str) -> cabc.Iterator[None]: + """Helpful context manager that writes a paragraph, a heading, + and the indents. + + :param name: the section name that is written as heading. + """ + self.write_paragraph() + self.write_heading(name) + self.indent() + try: + yield + finally: + self.dedent() + + @contextmanager + def indentation(self) -> cabc.Iterator[None]: + """A context manager that increases the indentation.""" + self.indent() + try: + yield + finally: + self.dedent() + + def getvalue(self) -> str: + """Returns the buffer contents.""" + return "".join(self.buffer) + + +def join_options(options: cabc.Sequence[str]) -> tuple[str, bool]: + """Given a list of option strings this joins them in the most appropriate + way and returns them in the form ``(formatted_string, + any_prefix_is_slash)`` where the second item in the tuple is a flag that + indicates if any of the option prefixes was a slash. + """ + rv = [] + any_prefix_is_slash = False + + for opt in options: + prefix = _split_opt(opt)[0] + + if prefix == "/": + any_prefix_is_slash = True + + rv.append((len(prefix), opt)) + + rv.sort(key=lambda x: x[0]) + return ", ".join(x[1] for x in rv), any_prefix_is_slash diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/globals.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/globals.py new file mode 100644 index 0000000000..a2f91723d2 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/globals.py @@ -0,0 +1,67 @@ +from __future__ import annotations + +import typing as t +from threading import local + +if t.TYPE_CHECKING: + from .core import Context + +_local = local() + + +@t.overload +def get_current_context(silent: t.Literal[False] = False) -> Context: ... + + +@t.overload +def get_current_context(silent: bool = ...) -> Context | None: ... + + +def get_current_context(silent: bool = False) -> Context | None: + """Returns the current click context. This can be used as a way to + access the current context object from anywhere. This is a more implicit + alternative to the :func:`pass_context` decorator. This function is + primarily useful for helpers such as :func:`echo` which might be + interested in changing its behavior based on the current context. + + To push the current context, :meth:`Context.scope` can be used. + + .. versionadded:: 5.0 + + :param silent: if set to `True` the return value is `None` if no context + is available. The default behavior is to raise a + :exc:`RuntimeError`. + """ + try: + return t.cast("Context", _local.stack[-1]) + except (AttributeError, IndexError) as e: + if not silent: + raise RuntimeError("There is no active click context.") from e + + return None + + +def push_context(ctx: Context) -> None: + """Pushes a new context to the current stack.""" + _local.__dict__.setdefault("stack", []).append(ctx) + + +def pop_context() -> None: + """Removes the top level from the stack.""" + _local.stack.pop() + + +def resolve_color_default(color: bool | None = None) -> bool | None: + """Internal helper to get the default value of the color flag. If a + value is passed it's returned unchanged, otherwise it's looked up from + the current context. + """ + if color is not None: + return color + + ctx = get_current_context(silent=True) + + if ctx is not None: + return ctx.color + + return None diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/parser.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/parser.py new file mode 100644 index 0000000000..1ea1f7166e --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/parser.py @@ -0,0 +1,532 @@ +""" +This module started out as largely a copy paste from the stdlib's +optparse module with the features removed that we do not need from +optparse because we implement them in Click on a higher level (for +instance type handling, help formatting and a lot more). + +The plan is to remove more and more from here over time. + +The reason this is a different module and not optparse from the stdlib +is that there are differences in 2.x and 3.x about the error messages +generated and optparse in the stdlib uses gettext for no good reason +and might cause us issues. + +Click uses parts of optparse written by Gregory P. Ward and maintained +by the Python Software Foundation. This is limited to code in parser.py. + +Copyright 2001-2006 Gregory P. Ward. All rights reserved. +Copyright 2002-2006 Python Software Foundation. All rights reserved. +""" + +# This code uses parts of optparse written by Gregory P. Ward and +# maintained by the Python Software Foundation. +# Copyright 2001-2006 Gregory P. Ward +# Copyright 2002-2006 Python Software Foundation +from __future__ import annotations + +import collections.abc as cabc +import typing as t +from collections import deque +from gettext import gettext as _ +from gettext import ngettext + +from ._utils import FLAG_NEEDS_VALUE +from ._utils import UNSET +from .exceptions import BadArgumentUsage +from .exceptions import BadOptionUsage +from .exceptions import NoSuchOption +from .exceptions import UsageError + +if t.TYPE_CHECKING: + from ._utils import T_FLAG_NEEDS_VALUE + from ._utils import T_UNSET + from .core import Argument as CoreArgument + from .core import Context + from .core import Option as CoreOption + from .core import Parameter as CoreParameter + +V = t.TypeVar("V") + + +def _unpack_args( + args: cabc.Sequence[str], nargs_spec: cabc.Sequence[int] +) -> tuple[cabc.Sequence[str | cabc.Sequence[str | None] | None], list[str]]: + """Given an iterable of arguments and an iterable of nargs specifications, + it returns a tuple with all the unpacked arguments at the first index + and all remaining arguments as the second. + + The nargs specification is the number of arguments that should be consumed + or `-1` to indicate that this position should eat up all the remainders. + + Missing items are filled with ``UNSET``. + """ + args = deque(args) + nargs_spec = deque(nargs_spec) + rv: list[str | tuple[str | T_UNSET, ...] | T_UNSET] = [] + spos: int | None = None + + def _fetch(c: deque[V]) -> V | T_UNSET: + try: + if spos is None: + return c.popleft() + else: + return c.pop() + except IndexError: + return UNSET + + while nargs_spec: + nargs = _fetch(nargs_spec) + + if nargs is None: + continue + + if nargs == 1: + rv.append(_fetch(args)) # type: ignore[arg-type] + elif nargs > 1: + x = [_fetch(args) for _ in range(nargs)] + + # If we're reversed, we're pulling in the arguments in reverse, + # so we need to turn them around. + if spos is not None: + x.reverse() + + rv.append(tuple(x)) + elif nargs < 0: + if spos is not None: + raise TypeError("Cannot have two nargs < 0") + + spos = len(rv) + rv.append(UNSET) + + # spos is the position of the wildcard (star). If it's not `None`, + # we fill it with the remainder. + if spos is not None: + rv[spos] = tuple(args) + args = [] + rv[spos + 1 :] = reversed(rv[spos + 1 :]) + + return tuple(rv), list(args) + + +def _split_opt(opt: str) -> tuple[str, str]: + first = opt[:1] + if first.isalnum(): + return "", opt + if opt[1:2] == first: + return opt[:2], opt[2:] + return first, opt[1:] + + +def _normalize_opt(opt: str, ctx: Context | None) -> str: + if ctx is None or ctx.token_normalize_func is None: + return opt + prefix, opt = _split_opt(opt) + return f"{prefix}{ctx.token_normalize_func(opt)}" + + +class _Option: + def __init__( + self, + obj: CoreOption, + opts: cabc.Sequence[str], + dest: str | None, + action: str | None = None, + nargs: int = 1, + const: t.Any | None = None, + ): + self._short_opts = [] + self._long_opts = [] + self.prefixes: set[str] = set() + + for opt in opts: + prefix, value = _split_opt(opt) + if not prefix: + raise ValueError(f"Invalid start character for option ({opt})") + self.prefixes.add(prefix[0]) + if len(prefix) == 1 and len(value) == 1: + self._short_opts.append(opt) + else: + self._long_opts.append(opt) + self.prefixes.add(prefix) + + if action is None: + action = "store" + + self.dest = dest + self.action = action + self.nargs = nargs + self.const = const + self.obj = obj + + @property + def takes_value(self) -> bool: + return self.action in ("store", "append") + + def process(self, value: t.Any, state: _ParsingState) -> None: + if self.action == "store": + state.opts[self.dest] = value # type: ignore + elif self.action == "store_const": + state.opts[self.dest] = self.const # type: ignore + elif self.action == "append": + state.opts.setdefault(self.dest, []).append(value) # type: ignore + elif self.action == "append_const": + state.opts.setdefault(self.dest, []).append(self.const) # type: ignore + elif self.action == "count": + state.opts[self.dest] = state.opts.get(self.dest, 0) + 1 # type: ignore + else: + raise ValueError(f"unknown action '{self.action}'") + state.order.append(self.obj) + + +class _Argument: + def __init__(self, obj: CoreArgument, dest: str | None, nargs: int = 1): + self.dest = dest + self.nargs = nargs + self.obj = obj + + def process( + self, + value: str | cabc.Sequence[str | None] | None | T_UNSET, + state: _ParsingState, + ) -> None: + if self.nargs > 1: + assert isinstance(value, cabc.Sequence) + holes = sum(1 for x in value if x is UNSET) + if holes == len(value): + value = UNSET + elif holes != 0: + raise BadArgumentUsage( + _("Argument {name!r} takes {nargs} values.").format( + name=self.dest, nargs=self.nargs + ) + ) + + # We failed to collect any argument value so we consider the argument as unset. + if value == (): + value = UNSET + + state.opts[self.dest] = value # type: ignore + state.order.append(self.obj) + + +class _ParsingState: + def __init__(self, rargs: list[str]) -> None: + self.opts: dict[str, t.Any] = {} + self.largs: list[str] = [] + self.rargs = rargs + self.order: list[CoreParameter] = [] + + +class _OptionParser: + """The option parser is an internal class that is ultimately used to + parse options and arguments. It's modelled after optparse and brings + a similar but vastly simplified API. It should generally not be used + directly as the high level Click classes wrap it for you. + + It's not nearly as extensible as optparse or argparse as it does not + implement features that are implemented on a higher level (such as + types or defaults). + + :param ctx: optionally the :class:`~click.Context` where this parser + should go with. + + .. deprecated:: 8.2 + Will be removed in Click 9.0. + """ + + def __init__(self, ctx: Context | None = None) -> None: + #: The :class:`~click.Context` for this parser. This might be + #: `None` for some advanced use cases. + self.ctx = ctx + #: This controls how the parser deals with interspersed arguments. + #: If this is set to `False`, the parser will stop on the first + #: non-option. Click uses this to implement nested subcommands + #: safely. + self.allow_interspersed_args: bool = True + #: This tells the parser how to deal with unknown options. By + #: default it will error out (which is sensible), but there is a + #: second mode where it will ignore it and continue processing + #: after shifting all the unknown options into the resulting args. + self.ignore_unknown_options: bool = False + + if ctx is not None: + self.allow_interspersed_args = ctx.allow_interspersed_args + self.ignore_unknown_options = ctx.ignore_unknown_options + + self._short_opt: dict[str, _Option] = {} + self._long_opt: dict[str, _Option] = {} + self._opt_prefixes = {"-", "--"} + self._args: list[_Argument] = [] + + def add_option( + self, + obj: CoreOption, + opts: cabc.Sequence[str], + dest: str | None, + action: str | None = None, + nargs: int = 1, + const: t.Any | None = None, + ) -> None: + """Adds a new option named `dest` to the parser. The destination + is not inferred (unlike with optparse) and needs to be explicitly + provided. Action can be any of ``store``, ``store_const``, + ``append``, ``append_const`` or ``count``. + + The `obj` can be used to identify the option in the order list + that is returned from the parser. + """ + opts = [_normalize_opt(opt, self.ctx) for opt in opts] + option = _Option(obj, opts, dest, action=action, nargs=nargs, const=const) + self._opt_prefixes.update(option.prefixes) + for opt in option._short_opts: + self._short_opt[opt] = option + for opt in option._long_opts: + self._long_opt[opt] = option + + def add_argument(self, obj: CoreArgument, dest: str | None, nargs: int = 1) -> None: + """Adds a positional argument named `dest` to the parser. + + The `obj` can be used to identify the option in the order list + that is returned from the parser. + """ + self._args.append(_Argument(obj, dest=dest, nargs=nargs)) + + def parse_args( + self, args: list[str] + ) -> tuple[dict[str, t.Any], list[str], list[CoreParameter]]: + """Parses positional arguments and returns ``(values, args, order)`` + for the parsed options and arguments as well as the leftover + arguments if there are any. The order is a list of objects as they + appear on the command line. If arguments appear multiple times they + will be memorized multiple times as well. + """ + state = _ParsingState(args) + try: + self._process_args_for_options(state) + self._process_args_for_args(state) + except UsageError: + if self.ctx is None or not self.ctx.resilient_parsing: + raise + return state.opts, state.largs, state.order + + def _process_args_for_args(self, state: _ParsingState) -> None: + pargs, args = _unpack_args( + state.largs + state.rargs, [x.nargs for x in self._args] + ) + + for idx, arg in enumerate(self._args): + arg.process(pargs[idx], state) + + state.largs = args + state.rargs = [] + + def _process_args_for_options(self, state: _ParsingState) -> None: + while state.rargs: + arg = state.rargs.pop(0) + arglen = len(arg) + # Double dashes always handled explicitly regardless of what + # prefixes are valid. + if arg == "--": + return + elif arg[:1] in self._opt_prefixes and arglen > 1: + self._process_opts(arg, state) + elif self.allow_interspersed_args: + state.largs.append(arg) + else: + state.rargs.insert(0, arg) + return + + # Say this is the original argument list: + # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)] + # ^ + # (we are about to process arg(i)). + # + # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of + # [arg0, ..., arg(i-1)] (any options and their arguments will have + # been removed from largs). + # + # The while loop will usually consume 1 or more arguments per pass. + # If it consumes 1 (eg. arg is an option that takes no arguments), + # then after _process_arg() is done the situation is: + # + # largs = subset of [arg0, ..., arg(i)] + # rargs = [arg(i+1), ..., arg(N-1)] + # + # If allow_interspersed_args is false, largs will always be + # *empty* -- still a subset of [arg0, ..., arg(i-1)], but + # not a very interesting subset! + + def _match_long_opt( + self, opt: str, explicit_value: str | None, state: _ParsingState + ) -> None: + if opt not in self._long_opt: + from difflib import get_close_matches + + possibilities = get_close_matches(opt, self._long_opt) + raise NoSuchOption(opt, possibilities=possibilities, ctx=self.ctx) + + option = self._long_opt[opt] + if option.takes_value: + # At this point it's safe to modify rargs by injecting the + # explicit value, because no exception is raised in this + # branch. This means that the inserted value will be fully + # consumed. + if explicit_value is not None: + state.rargs.insert(0, explicit_value) + + value = self._get_value_from_state(opt, option, state) + + elif explicit_value is not None: + raise BadOptionUsage( + opt, _("Option {name!r} does not take a value.").format(name=opt) + ) + + else: + value = UNSET + + option.process(value, state) + + def _match_short_opt(self, arg: str, state: _ParsingState) -> None: + stop = False + i = 1 + prefix = arg[0] + unknown_options = [] + + for ch in arg[1:]: + opt = _normalize_opt(f"{prefix}{ch}", self.ctx) + option = self._short_opt.get(opt) + i += 1 + + if not option: + if self.ignore_unknown_options: + unknown_options.append(ch) + continue + raise NoSuchOption(opt, ctx=self.ctx) + if option.takes_value: + # Any characters left in arg? Pretend they're the + # next arg, and stop consuming characters of arg. + if i < len(arg): + state.rargs.insert(0, arg[i:]) + stop = True + + value = self._get_value_from_state(opt, option, state) + + else: + value = UNSET + + option.process(value, state) + + if stop: + break + + # If we got any unknown options we recombine the string of the + # remaining options and re-attach the prefix, then report that + # to the state as new larg. This way there is basic combinatorics + # that can be achieved while still ignoring unknown arguments. + if self.ignore_unknown_options and unknown_options: + state.largs.append(f"{prefix}{''.join(unknown_options)}") + + def _get_value_from_state( + self, option_name: str, option: _Option, state: _ParsingState + ) -> str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE: + nargs = option.nargs + + value: str | cabc.Sequence[str] | T_FLAG_NEEDS_VALUE + + if len(state.rargs) < nargs: + if option.obj._flag_needs_value: + # Option allows omitting the value. + value = FLAG_NEEDS_VALUE + else: + raise BadOptionUsage( + option_name, + ngettext( + "Option {name!r} requires an argument.", + "Option {name!r} requires {nargs} arguments.", + nargs, + ).format(name=option_name, nargs=nargs), + ) + elif nargs == 1: + next_rarg = state.rargs[0] + + if ( + option.obj._flag_needs_value + and isinstance(next_rarg, str) + and next_rarg[:1] in self._opt_prefixes + and len(next_rarg) > 1 + ): + # The next arg looks like the start of an option, don't + # use it as the value if omitting the value is allowed. + value = FLAG_NEEDS_VALUE + else: + value = state.rargs.pop(0) + else: + value = tuple(state.rargs[:nargs]) + del state.rargs[:nargs] + + return value + + def _process_opts(self, arg: str, state: _ParsingState) -> None: + explicit_value = None + # Long option handling happens in two parts. The first part is + # supporting explicitly attached values. In any case, we will try + # to long match the option first. + if "=" in arg: + long_opt, explicit_value = arg.split("=", 1) + else: + long_opt = arg + norm_long_opt = _normalize_opt(long_opt, self.ctx) + + # At this point we will match the (assumed) long option through + # the long option matching code. Note that this allows options + # like "-foo" to be matched as long options. + try: + self._match_long_opt(norm_long_opt, explicit_value, state) + except NoSuchOption: + # At this point the long option matching failed, and we need + # to try with short options. However there is a special rule + # which says, that if we have a two character options prefix + # (applies to "--foo" for instance), we do not dispatch to the + # short option code and will instead raise the no option + # error. + if arg[:2] not in self._opt_prefixes: + self._match_short_opt(arg, state) + return + + if not self.ignore_unknown_options: + raise + + state.largs.append(arg) + + +def __getattr__(name: str) -> object: + import warnings + + if name in { + "OptionParser", + "Argument", + "Option", + "split_opt", + "normalize_opt", + "ParsingState", + }: + warnings.warn( + f"'parser.{name}' is deprecated and will be removed in Click 9.0." + " The old parser is available in 'optparse'.", + DeprecationWarning, + stacklevel=2, + ) + return globals()[f"_{name}"] + + if name == "split_arg_string": + from .shell_completion import split_arg_string + + warnings.warn( + "Importing 'parser.split_arg_string' is deprecated, it will only be" + " available in 'shell_completion' in Click 9.0.", + DeprecationWarning, + stacklevel=2, + ) + return split_arg_string + + raise AttributeError(name) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/py.typed b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/shell_completion.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/shell_completion.py new file mode 100644 index 0000000000..8f1564c49b --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/shell_completion.py @@ -0,0 +1,667 @@ +from __future__ import annotations + +import collections.abc as cabc +import os +import re +import typing as t +from gettext import gettext as _ + +from .core import Argument +from .core import Command +from .core import Context +from .core import Group +from .core import Option +from .core import Parameter +from .core import ParameterSource +from .utils import echo + + +def shell_complete( + cli: Command, + ctx_args: cabc.MutableMapping[str, t.Any], + prog_name: str, + complete_var: str, + instruction: str, +) -> int: + """Perform shell completion for the given CLI program. + + :param cli: Command being called. + :param ctx_args: Extra arguments to pass to + ``cli.make_context``. + :param prog_name: Name of the executable in the shell. + :param complete_var: Name of the environment variable that holds + the completion instruction. + :param instruction: Value of ``complete_var`` with the completion + instruction and shell, in the form ``instruction_shell``. + :return: Status code to exit with. + """ + shell, _, instruction = instruction.partition("_") + comp_cls = get_completion_class(shell) + + if comp_cls is None: + return 1 + + comp = comp_cls(cli, ctx_args, prog_name, complete_var) + + if instruction == "source": + echo(comp.source()) + return 0 + + if instruction == "complete": + echo(comp.complete()) + return 0 + + return 1 + + +class CompletionItem: + """Represents a completion value and metadata about the value. The + default metadata is ``type`` to indicate special shell handling, + and ``help`` if a shell supports showing a help string next to the + value. + + Arbitrary parameters can be passed when creating the object, and + accessed using ``item.attr``. If an attribute wasn't passed, + accessing it returns ``None``. + + :param value: The completion suggestion. + :param type: Tells the shell script to provide special completion + support for the type. Click uses ``"dir"`` and ``"file"``. + :param help: String shown next to the value if supported. + :param kwargs: Arbitrary metadata. The built-in implementations + don't use this, but custom type completions paired with custom + shell support could use it. + """ + + __slots__ = ("value", "type", "help", "_info") + + def __init__( + self, + value: t.Any, + type: str = "plain", + help: str | None = None, + **kwargs: t.Any, + ) -> None: + self.value: t.Any = value + self.type: str = type + self.help: str | None = help + self._info = kwargs + + def __getattr__(self, name: str) -> t.Any: + return self._info.get(name) + + +# Only Bash >= 4.4 has the nosort option. +_SOURCE_BASH = """\ +%(complete_func)s() { + local IFS=$'\\n' + local response + + response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD \ +%(complete_var)s=bash_complete $1) + + for completion in $response; do + IFS=',' read type value <<< "$completion" + + if [[ $type == 'dir' ]]; then + COMPREPLY=() + compopt -o dirnames + elif [[ $type == 'file' ]]; then + COMPREPLY=() + compopt -o default + elif [[ $type == 'plain' ]]; then + COMPREPLY+=($value) + fi + done + + return 0 +} + +%(complete_func)s_setup() { + complete -o nosort -F %(complete_func)s %(prog_name)s +} + +%(complete_func)s_setup; +""" + +# See ZshComplete.format_completion below, and issue #2703, before +# changing this script. +# +# (TL;DR: _describe is picky about the format, but this Zsh script snippet +# is already widely deployed. So freeze this script, and use clever-ish +# handling of colons in ZshComplet.format_completion.) +_SOURCE_ZSH = """\ +#compdef %(prog_name)s + +%(complete_func)s() { + local -a completions + local -a completions_with_descriptions + local -a response + (( ! $+commands[%(prog_name)s] )) && return 1 + + response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) \ +%(complete_var)s=zsh_complete %(prog_name)s)}") + + for type key descr in ${response}; do + if [[ "$type" == "plain" ]]; then + if [[ "$descr" == "_" ]]; then + completions+=("$key") + else + completions_with_descriptions+=("$key":"$descr") + fi + elif [[ "$type" == "dir" ]]; then + _path_files -/ + elif [[ "$type" == "file" ]]; then + _path_files -f + fi + done + + if [ -n "$completions_with_descriptions" ]; then + _describe -V unsorted completions_with_descriptions -U + fi + + if [ -n "$completions" ]; then + compadd -U -V unsorted -a completions + fi +} + +if [[ $zsh_eval_context[-1] == loadautofunc ]]; then + # autoload from fpath, call function directly + %(complete_func)s "$@" +else + # eval/source/. command, register function for later + compdef %(complete_func)s %(prog_name)s +fi +""" + +_SOURCE_FISH = """\ +function %(complete_func)s; + set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \ +COMP_CWORD=(commandline -t) %(prog_name)s); + + for completion in $response; + set -l metadata (string split "," $completion); + + if test $metadata[1] = "dir"; + __fish_complete_directories $metadata[2]; + else if test $metadata[1] = "file"; + __fish_complete_path $metadata[2]; + else if test $metadata[1] = "plain"; + echo $metadata[2]; + end; + end; +end; + +complete --no-files --command %(prog_name)s --arguments \ +"(%(complete_func)s)"; +""" + + +class ShellComplete: + """Base class for providing shell completion support. A subclass for + a given shell will override attributes and methods to implement the + completion instructions (``source`` and ``complete``). + + :param cli: Command being called. + :param prog_name: Name of the executable in the shell. + :param complete_var: Name of the environment variable that holds + the completion instruction. + + .. versionadded:: 8.0 + """ + + name: t.ClassVar[str] + """Name to register the shell as with :func:`add_completion_class`. + This is used in completion instructions (``{name}_source`` and + ``{name}_complete``). + """ + + source_template: t.ClassVar[str] + """Completion script template formatted by :meth:`source`. This must + be provided by subclasses. + """ + + def __init__( + self, + cli: Command, + ctx_args: cabc.MutableMapping[str, t.Any], + prog_name: str, + complete_var: str, + ) -> None: + self.cli = cli + self.ctx_args = ctx_args + self.prog_name = prog_name + self.complete_var = complete_var + + @property + def func_name(self) -> str: + """The name of the shell function defined by the completion + script. + """ + safe_name = re.sub(r"\W*", "", self.prog_name.replace("-", "_"), flags=re.ASCII) + return f"_{safe_name}_completion" + + def source_vars(self) -> dict[str, t.Any]: + """Vars for formatting :attr:`source_template`. + + By default this provides ``complete_func``, ``complete_var``, + and ``prog_name``. + """ + return { + "complete_func": self.func_name, + "complete_var": self.complete_var, + "prog_name": self.prog_name, + } + + def source(self) -> str: + """Produce the shell script that defines the completion + function. By default this ``%``-style formats + :attr:`source_template` with the dict returned by + :meth:`source_vars`. + """ + return self.source_template % self.source_vars() + + def get_completion_args(self) -> tuple[list[str], str]: + """Use the env vars defined by the shell script to return a + tuple of ``args, incomplete``. This must be implemented by + subclasses. + """ + raise NotImplementedError + + def get_completions(self, args: list[str], incomplete: str) -> list[CompletionItem]: + """Determine the context and last complete command or parameter + from the complete args. Call that object's ``shell_complete`` + method to get the completions for the incomplete value. + + :param args: List of complete args before the incomplete value. + :param incomplete: Value being completed. May be empty. + """ + ctx = _resolve_context(self.cli, self.ctx_args, self.prog_name, args) + obj, incomplete = _resolve_incomplete(ctx, args, incomplete) + return obj.shell_complete(ctx, incomplete) + + def format_completion(self, item: CompletionItem) -> str: + """Format a completion item into the form recognized by the + shell script. This must be implemented by subclasses. + + :param item: Completion item to format. + """ + raise NotImplementedError + + def complete(self) -> str: + """Produce the completion data to send back to the shell. + + By default this calls :meth:`get_completion_args`, gets the + completions, then calls :meth:`format_completion` for each + completion. + """ + args, incomplete = self.get_completion_args() + completions = self.get_completions(args, incomplete) + out = [self.format_completion(item) for item in completions] + return "\n".join(out) + + +class BashComplete(ShellComplete): + """Shell completion for Bash.""" + + name = "bash" + source_template = _SOURCE_BASH + + @staticmethod + def _check_version() -> None: + import shutil + import subprocess + + bash_exe = shutil.which("bash") + + if bash_exe is None: + match = None + else: + output = subprocess.run( + [bash_exe, "--norc", "-c", 'echo "${BASH_VERSION}"'], + stdout=subprocess.PIPE, + ) + match = re.search(r"^(\d+)\.(\d+)\.\d+", output.stdout.decode()) + + if match is not None: + major, minor = match.groups() + + if major < "4" or major == "4" and minor < "4": + echo( + _( + "Shell completion is not supported for Bash" + " versions older than 4.4." + ), + err=True, + ) + else: + echo( + _("Couldn't detect Bash version, shell completion is not supported."), + err=True, + ) + + def source(self) -> str: + self._check_version() + return super().source() + + def get_completion_args(self) -> tuple[list[str], str]: + cwords = split_arg_string(os.environ["COMP_WORDS"]) + cword = int(os.environ["COMP_CWORD"]) + args = cwords[1:cword] + + try: + incomplete = cwords[cword] + except IndexError: + incomplete = "" + + return args, incomplete + + def format_completion(self, item: CompletionItem) -> str: + return f"{item.type},{item.value}" + + +class ZshComplete(ShellComplete): + """Shell completion for Zsh.""" + + name = "zsh" + source_template = _SOURCE_ZSH + + def get_completion_args(self) -> tuple[list[str], str]: + cwords = split_arg_string(os.environ["COMP_WORDS"]) + cword = int(os.environ["COMP_CWORD"]) + args = cwords[1:cword] + + try: + incomplete = cwords[cword] + except IndexError: + incomplete = "" + + return args, incomplete + + def format_completion(self, item: CompletionItem) -> str: + help_ = item.help or "_" + # The zsh completion script uses `_describe` on items with help + # texts (which splits the item help from the item value at the + # first unescaped colon) and `compadd` on items without help + # text (which uses the item value as-is and does not support + # colon escaping). So escape colons in the item value if and + # only if the item help is not the sentinel "_" value, as used + # by the completion script. + # + # (The zsh completion script is potentially widely deployed, and + # thus harder to fix than this method.) + # + # See issue #1812 and issue #2703 for further context. + value = item.value.replace(":", r"\:") if help_ != "_" else item.value + return f"{item.type}\n{value}\n{help_}" + + +class FishComplete(ShellComplete): + """Shell completion for Fish.""" + + name = "fish" + source_template = _SOURCE_FISH + + def get_completion_args(self) -> tuple[list[str], str]: + cwords = split_arg_string(os.environ["COMP_WORDS"]) + incomplete = os.environ["COMP_CWORD"] + if incomplete: + incomplete = split_arg_string(incomplete)[0] + args = cwords[1:] + + # Fish stores the partial word in both COMP_WORDS and + # COMP_CWORD, remove it from complete args. + if incomplete and args and args[-1] == incomplete: + args.pop() + + return args, incomplete + + def format_completion(self, item: CompletionItem) -> str: + if item.help: + return f"{item.type},{item.value}\t{item.help}" + + return f"{item.type},{item.value}" + + +ShellCompleteType = t.TypeVar("ShellCompleteType", bound="type[ShellComplete]") + + +_available_shells: dict[str, type[ShellComplete]] = { + "bash": BashComplete, + "fish": FishComplete, + "zsh": ZshComplete, +} + + +def add_completion_class( + cls: ShellCompleteType, name: str | None = None +) -> ShellCompleteType: + """Register a :class:`ShellComplete` subclass under the given name. + The name will be provided by the completion instruction environment + variable during completion. + + :param cls: The completion class that will handle completion for the + shell. + :param name: Name to register the class under. Defaults to the + class's ``name`` attribute. + """ + if name is None: + name = cls.name + + _available_shells[name] = cls + + return cls + + +def get_completion_class(shell: str) -> type[ShellComplete] | None: + """Look up a registered :class:`ShellComplete` subclass by the name + provided by the completion instruction environment variable. If the + name isn't registered, returns ``None``. + + :param shell: Name the class is registered under. + """ + return _available_shells.get(shell) + + +def split_arg_string(string: str) -> list[str]: + """Split an argument string as with :func:`shlex.split`, but don't + fail if the string is incomplete. Ignores a missing closing quote or + incomplete escape sequence and uses the partial token as-is. + + .. code-block:: python + + split_arg_string("example 'my file") + ["example", "my file"] + + split_arg_string("example my\\") + ["example", "my"] + + :param string: String to split. + + .. versionchanged:: 8.2 + Moved to ``shell_completion`` from ``parser``. + """ + import shlex + + lex = shlex.shlex(string, posix=True) + lex.whitespace_split = True + lex.commenters = "" + out = [] + + try: + for token in lex: + out.append(token) + except ValueError: + # Raised when end-of-string is reached in an invalid state. Use + # the partial token as-is. The quote or escape character is in + # lex.state, not lex.token. + out.append(lex.token) + + return out + + +def _is_incomplete_argument(ctx: Context, param: Parameter) -> bool: + """Determine if the given parameter is an argument that can still + accept values. + + :param ctx: Invocation context for the command represented by the + parsed complete args. + :param param: Argument object being checked. + """ + if not isinstance(param, Argument): + return False + + assert param.name is not None + # Will be None if expose_value is False. + value = ctx.params.get(param.name) + return ( + param.nargs == -1 + or ctx.get_parameter_source(param.name) is not ParameterSource.COMMANDLINE + or ( + param.nargs > 1 + and isinstance(value, (tuple, list)) + and len(value) < param.nargs + ) + ) + + +def _start_of_option(ctx: Context, value: str) -> bool: + """Check if the value looks like the start of an option.""" + if not value: + return False + + c = value[0] + return c in ctx._opt_prefixes + + +def _is_incomplete_option(ctx: Context, args: list[str], param: Parameter) -> bool: + """Determine if the given parameter is an option that needs a value. + + :param args: List of complete args before the incomplete value. + :param param: Option object being checked. + """ + if not isinstance(param, Option): + return False + + if param.is_flag or param.count: + return False + + last_option = None + + for index, arg in enumerate(reversed(args)): + if index + 1 > param.nargs: + break + + if _start_of_option(ctx, arg): + last_option = arg + break + + return last_option is not None and last_option in param.opts + + +def _resolve_context( + cli: Command, + ctx_args: cabc.MutableMapping[str, t.Any], + prog_name: str, + args: list[str], +) -> Context: + """Produce the context hierarchy starting with the command and + traversing the complete arguments. This only follows the commands, + it doesn't trigger input prompts or callbacks. + + :param cli: Command being called. + :param prog_name: Name of the executable in the shell. + :param args: List of complete args before the incomplete value. + """ + ctx_args["resilient_parsing"] = True + with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx: + args = ctx._protected_args + ctx.args + + while args: + command = ctx.command + + if isinstance(command, Group): + if not command.chain: + name, cmd, args = command.resolve_command(ctx, args) + + if cmd is None: + return ctx + + with cmd.make_context( + name, args, parent=ctx, resilient_parsing=True + ) as sub_ctx: + ctx = sub_ctx + args = ctx._protected_args + ctx.args + else: + sub_ctx = ctx + + while args: + name, cmd, args = command.resolve_command(ctx, args) + + if cmd is None: + return ctx + + with cmd.make_context( + name, + args, + parent=ctx, + allow_extra_args=True, + allow_interspersed_args=False, + resilient_parsing=True, + ) as sub_sub_ctx: + sub_ctx = sub_sub_ctx + args = sub_ctx.args + + ctx = sub_ctx + args = [*sub_ctx._protected_args, *sub_ctx.args] + else: + break + + return ctx + + +def _resolve_incomplete( + ctx: Context, args: list[str], incomplete: str +) -> tuple[Command | Parameter, str]: + """Find the Click object that will handle the completion of the + incomplete value. Return the object and the incomplete value. + + :param ctx: Invocation context for the command represented by + the parsed complete args. + :param args: List of complete args before the incomplete value. + :param incomplete: Value being completed. May be empty. + """ + # Different shells treat an "=" between a long option name and + # value differently. Might keep the value joined, return the "=" + # as a separate item, or return the split name and value. Always + # split and discard the "=" to make completion easier. + if incomplete == "=": + incomplete = "" + elif "=" in incomplete and _start_of_option(ctx, incomplete): + name, _, incomplete = incomplete.partition("=") + args.append(name) + + # The "--" marker tells Click to stop treating values as options + # even if they start with the option character. If it hasn't been + # given and the incomplete arg looks like an option, the current + # command will provide option name completions. + if "--" not in args and _start_of_option(ctx, incomplete): + return ctx.command, incomplete + + params = ctx.command.get_params(ctx) + + # If the last complete arg is an option name with an incomplete + # value, the option will provide value completions. + for param in params: + if _is_incomplete_option(ctx, args, param): + return param, incomplete + + # It's not an option name or value. The first argument without a + # parsed value will provide value completions. + for param in params: + if _is_incomplete_argument(ctx, param): + return param, incomplete + + # There were no unparsed arguments, the command may be a group that + # will provide command name completions. + return ctx.command, incomplete diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/termui.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/termui.py new file mode 100644 index 0000000000..48f671b217 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/termui.py @@ -0,0 +1,891 @@ +from __future__ import annotations + +import collections.abc as cabc +import inspect +import io +import itertools +import sys +import typing as t +from contextlib import AbstractContextManager +from gettext import gettext as _ + +from ._compat import isatty +from ._compat import strip_ansi +from .exceptions import Abort +from .exceptions import UsageError +from .globals import resolve_color_default +from .types import Choice +from .types import convert_type +from .types import ParamType +from .utils import echo +from .utils import LazyFile + +if t.TYPE_CHECKING: + from ._termui_impl import ProgressBar + +V = t.TypeVar("V") + +# The prompt functions to use. The doc tools currently override these +# functions to customize how they work. +visible_prompt_func: t.Callable[[str], str] = input + +_ansi_colors = { + "black": 30, + "red": 31, + "green": 32, + "yellow": 33, + "blue": 34, + "magenta": 35, + "cyan": 36, + "white": 37, + "reset": 39, + "bright_black": 90, + "bright_red": 91, + "bright_green": 92, + "bright_yellow": 93, + "bright_blue": 94, + "bright_magenta": 95, + "bright_cyan": 96, + "bright_white": 97, +} +_ansi_reset_all = "\033[0m" + + +def hidden_prompt_func(prompt: str) -> str: + import getpass + + return getpass.getpass(prompt) + + +def _build_prompt( + text: str, + suffix: str, + show_default: bool | str = False, + default: t.Any | None = None, + show_choices: bool = True, + type: ParamType | None = None, +) -> str: + prompt = text + if type is not None and show_choices and isinstance(type, Choice): + prompt += f" ({', '.join(map(str, type.choices))})" + if isinstance(show_default, str): + default = f"({show_default})" + if default is not None and show_default: + prompt = f"{prompt} [{_format_default(default)}]" + return f"{prompt}{suffix}" + + +def _format_default(default: t.Any) -> t.Any: + if isinstance(default, (io.IOBase, LazyFile)) and hasattr(default, "name"): + return default.name + + return default + + +def prompt( + text: str, + default: t.Any | None = None, + hide_input: bool = False, + confirmation_prompt: bool | str = False, + type: ParamType | t.Any | None = None, + value_proc: t.Callable[[str], t.Any] | None = None, + prompt_suffix: str = ": ", + show_default: bool | str = True, + err: bool = False, + show_choices: bool = True, +) -> t.Any: + """Prompts a user for input. This is a convenience function that can + be used to prompt a user for input later. + + If the user aborts the input by sending an interrupt signal, this + function will catch it and raise a :exc:`Abort` exception. + + :param text: the text to show for the prompt. + :param default: the default value to use if no input happens. If this + is not given it will prompt until it's aborted. + :param hide_input: if this is set to true then the input value will + be hidden. + :param confirmation_prompt: Prompt a second time to confirm the + value. Can be set to a string instead of ``True`` to customize + the message. + :param type: the type to use to check the value against. + :param value_proc: if this parameter is provided it's a function that + is invoked instead of the type conversion to + convert a value. + :param prompt_suffix: a suffix that should be added to the prompt. + :param show_default: shows or hides the default value in the prompt. + If this value is a string, it shows that string + in parentheses instead of the actual value. + :param err: if set to true the file defaults to ``stderr`` instead of + ``stdout``, the same as with echo. + :param show_choices: Show or hide choices if the passed type is a Choice. + For example if type is a Choice of either day or week, + show_choices is true and text is "Group by" then the + prompt will be "Group by (day, week): ". + + .. versionchanged:: 8.3.3 + ``show_default`` can be a string to show a custom value instead + of the actual default, matching the help text behavior. + + .. versionchanged:: 8.3.1 + A space is no longer appended to the prompt. + + .. versionadded:: 8.0 + ``confirmation_prompt`` can be a custom string. + + .. versionadded:: 7.0 + Added the ``show_choices`` parameter. + + .. versionadded:: 6.0 + Added unicode support for cmd.exe on Windows. + + .. versionadded:: 4.0 + Added the `err` parameter. + + """ + + def prompt_func(text: str) -> str: + f = hidden_prompt_func if hide_input else visible_prompt_func + try: + # Write the prompt separately so that we get nice + # coloring through colorama on Windows + echo(text[:-1], nl=False, err=err) + # Echo the last character to stdout to work around an issue where + # readline causes backspace to clear the whole line. + return f(text[-1:]) + except (KeyboardInterrupt, EOFError): + # getpass doesn't print a newline if the user aborts input with ^C. + # Allegedly this behavior is inherited from getpass(3). + # A doc bug has been filed at https://bugs.python.org/issue24711 + if hide_input: + echo(None, err=err) + raise Abort() from None + + if value_proc is None: + value_proc = convert_type(type, default) + + prompt = _build_prompt( + text, prompt_suffix, show_default, default, show_choices, type + ) + + if confirmation_prompt: + if confirmation_prompt is True: + confirmation_prompt = _("Repeat for confirmation") + + confirmation_prompt = _build_prompt(confirmation_prompt, prompt_suffix) + + while True: + while True: + value = prompt_func(prompt) + if value: + break + elif default is not None: + value = default + break + try: + result = value_proc(value) + except UsageError as e: + if hide_input: + echo(_("Error: The value you entered was invalid."), err=err) + else: + echo(_("Error: {e.message}").format(e=e), err=err) + continue + if not confirmation_prompt: + return result + while True: + value2 = prompt_func(confirmation_prompt) + is_empty = not value and not value2 + if value2 or is_empty: + break + if value == value2: + return result + echo(_("Error: The two entered values do not match."), err=err) + + +def confirm( + text: str, + default: bool | None = False, + abort: bool = False, + prompt_suffix: str = ": ", + show_default: bool = True, + err: bool = False, +) -> bool: + """Prompts for confirmation (yes/no question). + + If the user aborts the input by sending a interrupt signal this + function will catch it and raise a :exc:`Abort` exception. + + :param text: the question to ask. + :param default: The default value to use when no input is given. If + ``None``, repeat until input is given. + :param abort: if this is set to `True` a negative answer aborts the + exception by raising :exc:`Abort`. + :param prompt_suffix: a suffix that should be added to the prompt. + :param show_default: shows or hides the default value in the prompt. + :param err: if set to true the file defaults to ``stderr`` instead of + ``stdout``, the same as with echo. + + .. versionchanged:: 8.3.1 + A space is no longer appended to the prompt. + + .. versionchanged:: 8.0 + Repeat until input is given if ``default`` is ``None``. + + .. versionadded:: 4.0 + Added the ``err`` parameter. + """ + prompt = _build_prompt( + text, + prompt_suffix, + show_default, + "y/n" if default is None else ("Y/n" if default else "y/N"), + ) + + while True: + try: + # Write the prompt separately so that we get nice + # coloring through colorama on Windows + echo(prompt[:-1], nl=False, err=err) + # Echo the last character to stdout to work around an issue where + # readline causes backspace to clear the whole line. + value = visible_prompt_func(prompt[-1:]).lower().strip() + except (KeyboardInterrupt, EOFError): + raise Abort() from None + if value in ("y", "yes"): + rv = True + elif value in ("n", "no"): + rv = False + elif default is not None and value == "": + rv = default + else: + echo(_("Error: invalid input"), err=err) + continue + break + if abort and not rv: + raise Abort() + return rv + + +def echo_via_pager( + text_or_generator: cabc.Iterable[str] | t.Callable[[], cabc.Iterable[str]] | str, + color: bool | None = None, +) -> None: + """This function takes a text and shows it via an environment specific + pager on stdout. + + .. versionchanged:: 3.0 + Added the `color` flag. + + :param text_or_generator: the text to page, or alternatively, a + generator emitting the text to page. + :param color: controls if the pager supports ANSI colors or not. The + default is autodetection. + """ + color = resolve_color_default(color) + + if inspect.isgeneratorfunction(text_or_generator): + i = t.cast("t.Callable[[], cabc.Iterable[str]]", text_or_generator)() + elif isinstance(text_or_generator, str): + i = [text_or_generator] + else: + i = iter(t.cast("cabc.Iterable[str]", text_or_generator)) + + # convert every element of i to a text type if necessary + text_generator = (el if isinstance(el, str) else str(el) for el in i) + + from ._termui_impl import pager + + return pager(itertools.chain(text_generator, "\n"), color) + + +@t.overload +def progressbar( + *, + length: int, + label: str | None = None, + hidden: bool = False, + show_eta: bool = True, + show_percent: bool | None = None, + show_pos: bool = False, + fill_char: str = "#", + empty_char: str = "-", + bar_template: str = "%(label)s [%(bar)s] %(info)s", + info_sep: str = " ", + width: int = 36, + file: t.TextIO | None = None, + color: bool | None = None, + update_min_steps: int = 1, +) -> ProgressBar[int]: ... + + +@t.overload +def progressbar( + iterable: cabc.Iterable[V] | None = None, + length: int | None = None, + label: str | None = None, + hidden: bool = False, + show_eta: bool = True, + show_percent: bool | None = None, + show_pos: bool = False, + item_show_func: t.Callable[[V | None], str | None] | None = None, + fill_char: str = "#", + empty_char: str = "-", + bar_template: str = "%(label)s [%(bar)s] %(info)s", + info_sep: str = " ", + width: int = 36, + file: t.TextIO | None = None, + color: bool | None = None, + update_min_steps: int = 1, +) -> ProgressBar[V]: ... + + +def progressbar( + iterable: cabc.Iterable[V] | None = None, + length: int | None = None, + label: str | None = None, + hidden: bool = False, + show_eta: bool = True, + show_percent: bool | None = None, + show_pos: bool = False, + item_show_func: t.Callable[[V | None], str | None] | None = None, + fill_char: str = "#", + empty_char: str = "-", + bar_template: str = "%(label)s [%(bar)s] %(info)s", + info_sep: str = " ", + width: int = 36, + file: t.TextIO | None = None, + color: bool | None = None, + update_min_steps: int = 1, +) -> ProgressBar[V]: + """This function creates an iterable context manager that can be used + to iterate over something while showing a progress bar. It will + either iterate over the `iterable` or `length` items (that are counted + up). While iteration happens, this function will print a rendered + progress bar to the given `file` (defaults to stdout) and will attempt + to calculate remaining time and more. By default, this progress bar + will not be rendered if the file is not a terminal. + + The context manager creates the progress bar. When the context + manager is entered the progress bar is already created. With every + iteration over the progress bar, the iterable passed to the bar is + advanced and the bar is updated. When the context manager exits, + a newline is printed and the progress bar is finalized on screen. + + Note: The progress bar is currently designed for use cases where the + total progress can be expected to take at least several seconds. + Because of this, the ProgressBar class object won't display + progress that is considered too fast, and progress where the time + between steps is less than a second. + + No printing must happen or the progress bar will be unintentionally + destroyed. + + Example usage:: + + with progressbar(items) as bar: + for item in bar: + do_something_with(item) + + Alternatively, if no iterable is specified, one can manually update the + progress bar through the `update()` method instead of directly + iterating over the progress bar. The update method accepts the number + of steps to increment the bar with:: + + with progressbar(length=chunks.total_bytes) as bar: + for chunk in chunks: + process_chunk(chunk) + bar.update(chunks.bytes) + + The ``update()`` method also takes an optional value specifying the + ``current_item`` at the new position. This is useful when used + together with ``item_show_func`` to customize the output for each + manual step:: + + with click.progressbar( + length=total_size, + label='Unzipping archive', + item_show_func=lambda a: a.filename + ) as bar: + for archive in zip_file: + archive.extract() + bar.update(archive.size, archive) + + :param iterable: an iterable to iterate over. If not provided the length + is required. + :param length: the number of items to iterate over. By default the + progressbar will attempt to ask the iterator about its + length, which might or might not work. If an iterable is + also provided this parameter can be used to override the + length. If an iterable is not provided the progress bar + will iterate over a range of that length. + :param label: the label to show next to the progress bar. + :param hidden: hide the progressbar. Defaults to ``False``. When no tty is + detected, it will only print the progressbar label. Setting this to + ``False`` also disables that. + :param show_eta: enables or disables the estimated time display. This is + automatically disabled if the length cannot be + determined. + :param show_percent: enables or disables the percentage display. The + default is `True` if the iterable has a length or + `False` if not. + :param show_pos: enables or disables the absolute position display. The + default is `False`. + :param item_show_func: A function called with the current item which + can return a string to show next to the progress bar. If the + function returns ``None`` nothing is shown. The current item can + be ``None``, such as when entering and exiting the bar. + :param fill_char: the character to use to show the filled part of the + progress bar. + :param empty_char: the character to use to show the non-filled part of + the progress bar. + :param bar_template: the format string to use as template for the bar. + The parameters in it are ``label`` for the label, + ``bar`` for the progress bar and ``info`` for the + info section. + :param info_sep: the separator between multiple info items (eta etc.) + :param width: the width of the progress bar in characters, 0 means full + terminal width + :param file: The file to write to. If this is not a terminal then + only the label is printed. + :param color: controls if the terminal supports ANSI colors or not. The + default is autodetection. This is only needed if ANSI + codes are included anywhere in the progress bar output + which is not the case by default. + :param update_min_steps: Render only when this many updates have + completed. This allows tuning for very fast iterators. + + .. versionadded:: 8.2 + The ``hidden`` argument. + + .. versionchanged:: 8.0 + Output is shown even if execution time is less than 0.5 seconds. + + .. versionchanged:: 8.0 + ``item_show_func`` shows the current item, not the previous one. + + .. versionchanged:: 8.0 + Labels are echoed if the output is not a TTY. Reverts a change + in 7.0 that removed all output. + + .. versionadded:: 8.0 + The ``update_min_steps`` parameter. + + .. versionadded:: 4.0 + The ``color`` parameter and ``update`` method. + + .. versionadded:: 2.0 + """ + from ._termui_impl import ProgressBar + + color = resolve_color_default(color) + return ProgressBar( + iterable=iterable, + length=length, + hidden=hidden, + show_eta=show_eta, + show_percent=show_percent, + show_pos=show_pos, + item_show_func=item_show_func, + fill_char=fill_char, + empty_char=empty_char, + bar_template=bar_template, + info_sep=info_sep, + file=file, + label=label, + width=width, + color=color, + update_min_steps=update_min_steps, + ) + + +def clear() -> None: + """Clears the terminal screen. This will have the effect of clearing + the whole visible space of the terminal and moving the cursor to the + top left. This does not do anything if not connected to a terminal. + + .. versionadded:: 2.0 + """ + if not isatty(sys.stdout): + return + + # ANSI escape \033[2J clears the screen, \033[1;1H moves the cursor + echo("\033[2J\033[1;1H", nl=False) + + +def _interpret_color(color: int | tuple[int, int, int] | str, offset: int = 0) -> str: + if isinstance(color, int): + return f"{38 + offset};5;{color:d}" + + if isinstance(color, (tuple, list)): + r, g, b = color + return f"{38 + offset};2;{r:d};{g:d};{b:d}" + + return str(_ansi_colors[color] + offset) + + +def style( + text: t.Any, + fg: int | tuple[int, int, int] | str | None = None, + bg: int | tuple[int, int, int] | str | None = None, + bold: bool | None = None, + dim: bool | None = None, + underline: bool | None = None, + overline: bool | None = None, + italic: bool | None = None, + blink: bool | None = None, + reverse: bool | None = None, + strikethrough: bool | None = None, + reset: bool = True, +) -> str: + """Styles a text with ANSI styles and returns the new string. By + default the styling is self contained which means that at the end + of the string a reset code is issued. This can be prevented by + passing ``reset=False``. + + Examples:: + + click.echo(click.style('Hello World!', fg='green')) + click.echo(click.style('ATTENTION!', blink=True)) + click.echo(click.style('Some things', reverse=True, fg='cyan')) + click.echo(click.style('More colors', fg=(255, 12, 128), bg=117)) + + Supported color names: + + * ``black`` (might be a gray) + * ``red`` + * ``green`` + * ``yellow`` (might be an orange) + * ``blue`` + * ``magenta`` + * ``cyan`` + * ``white`` (might be light gray) + * ``bright_black`` + * ``bright_red`` + * ``bright_green`` + * ``bright_yellow`` + * ``bright_blue`` + * ``bright_magenta`` + * ``bright_cyan`` + * ``bright_white`` + * ``reset`` (reset the color code only) + + If the terminal supports it, color may also be specified as: + + - An integer in the interval [0, 255]. The terminal must support + 8-bit/256-color mode. + - An RGB tuple of three integers in [0, 255]. The terminal must + support 24-bit/true-color mode. + + See https://en.wikipedia.org/wiki/ANSI_color and + https://gist.github.com/XVilka/8346728 for more information. + + :param text: the string to style with ansi codes. + :param fg: if provided this will become the foreground color. + :param bg: if provided this will become the background color. + :param bold: if provided this will enable or disable bold mode. + :param dim: if provided this will enable or disable dim mode. This is + badly supported. + :param underline: if provided this will enable or disable underline. + :param overline: if provided this will enable or disable overline. + :param italic: if provided this will enable or disable italic. + :param blink: if provided this will enable or disable blinking. + :param reverse: if provided this will enable or disable inverse + rendering (foreground becomes background and the + other way round). + :param strikethrough: if provided this will enable or disable + striking through text. + :param reset: by default a reset-all code is added at the end of the + string which means that styles do not carry over. This + can be disabled to compose styles. + + .. versionchanged:: 8.0 + A non-string ``message`` is converted to a string. + + .. versionchanged:: 8.0 + Added support for 256 and RGB color codes. + + .. versionchanged:: 8.0 + Added the ``strikethrough``, ``italic``, and ``overline`` + parameters. + + .. versionchanged:: 7.0 + Added support for bright colors. + + .. versionadded:: 2.0 + """ + if not isinstance(text, str): + text = str(text) + + bits = [] + + if fg: + try: + bits.append(f"\033[{_interpret_color(fg)}m") + except KeyError: + raise TypeError(f"Unknown color {fg!r}") from None + + if bg: + try: + bits.append(f"\033[{_interpret_color(bg, 10)}m") + except KeyError: + raise TypeError(f"Unknown color {bg!r}") from None + + if bold is not None: + bits.append(f"\033[{1 if bold else 22}m") + if dim is not None: + bits.append(f"\033[{2 if dim else 22}m") + if underline is not None: + bits.append(f"\033[{4 if underline else 24}m") + if overline is not None: + bits.append(f"\033[{53 if overline else 55}m") + if italic is not None: + bits.append(f"\033[{3 if italic else 23}m") + if blink is not None: + bits.append(f"\033[{5 if blink else 25}m") + if reverse is not None: + bits.append(f"\033[{7 if reverse else 27}m") + if strikethrough is not None: + bits.append(f"\033[{9 if strikethrough else 29}m") + bits.append(text) + if reset: + bits.append(_ansi_reset_all) + return "".join(bits) + + +def unstyle(text: str) -> str: + """Removes ANSI styling information from a string. Usually it's not + necessary to use this function as Click's echo function will + automatically remove styling if necessary. + + .. versionadded:: 2.0 + + :param text: the text to remove style information from. + """ + return strip_ansi(text) + + +def secho( + message: t.Any | None = None, + file: t.IO[t.AnyStr] | None = None, + nl: bool = True, + err: bool = False, + color: bool | None = None, + **styles: t.Any, +) -> None: + """This function combines :func:`echo` and :func:`style` into one + call. As such the following two calls are the same:: + + click.secho('Hello World!', fg='green') + click.echo(click.style('Hello World!', fg='green')) + + All keyword arguments are forwarded to the underlying functions + depending on which one they go with. + + Non-string types will be converted to :class:`str`. However, + :class:`bytes` are passed directly to :meth:`echo` without applying + style. If you want to style bytes that represent text, call + :meth:`bytes.decode` first. + + .. versionchanged:: 8.0 + A non-string ``message`` is converted to a string. Bytes are + passed through without style applied. + + .. versionadded:: 2.0 + """ + if message is not None and not isinstance(message, (bytes, bytearray)): + message = style(message, **styles) + + return echo(message, file=file, nl=nl, err=err, color=color) + + +@t.overload +def edit( + text: bytes | bytearray, + editor: str | None = None, + env: cabc.Mapping[str, str] | None = None, + require_save: bool = False, + extension: str = ".txt", +) -> bytes | None: ... + + +@t.overload +def edit( + text: str, + editor: str | None = None, + env: cabc.Mapping[str, str] | None = None, + require_save: bool = True, + extension: str = ".txt", +) -> str | None: ... + + +@t.overload +def edit( + text: None = None, + editor: str | None = None, + env: cabc.Mapping[str, str] | None = None, + require_save: bool = True, + extension: str = ".txt", + filename: str | cabc.Iterable[str] | None = None, +) -> None: ... + + +def edit( + text: str | bytes | bytearray | None = None, + editor: str | None = None, + env: cabc.Mapping[str, str] | None = None, + require_save: bool = True, + extension: str = ".txt", + filename: str | cabc.Iterable[str] | None = None, +) -> str | bytes | bytearray | None: + r"""Edits the given text in the defined editor. If an editor is given + (should be the full path to the executable but the regular operating + system search path is used for finding the executable) it overrides + the detected editor. Optionally, some environment variables can be + used. If the editor is closed without changes, `None` is returned. In + case a file is edited directly the return value is always `None` and + `require_save` and `extension` are ignored. + + If the editor cannot be opened a :exc:`UsageError` is raised. + + Note for Windows: to simplify cross-platform usage, the newlines are + automatically converted from POSIX to Windows and vice versa. As such, + the message here will have ``\n`` as newline markers. + + :param text: the text to edit. + :param editor: optionally the editor to use. Defaults to automatic + detection. + :param env: environment variables to forward to the editor. + :param require_save: if this is true, then not saving in the editor + will make the return value become `None`. + :param extension: the extension to tell the editor about. This defaults + to `.txt` but changing this might change syntax + highlighting. + :param filename: if provided it will edit this file instead of the + provided text contents. It will not use a temporary + file as an indirection in that case. If the editor supports + editing multiple files at once, a sequence of files may be + passed as well. Invoke `click.file` once per file instead + if multiple files cannot be managed at once or editing the + files serially is desired. + + .. versionchanged:: 8.2.0 + ``filename`` now accepts any ``Iterable[str]`` in addition to a ``str`` + if the ``editor`` supports editing multiple files at once. + + """ + from ._termui_impl import Editor + + ed = Editor(editor=editor, env=env, require_save=require_save, extension=extension) + + if filename is None: + return ed.edit(text) + + if isinstance(filename, str): + filename = (filename,) + + ed.edit_files(filenames=filename) + return None + + +def launch(url: str, wait: bool = False, locate: bool = False) -> int: + """This function launches the given URL (or filename) in the default + viewer application for this file type. If this is an executable, it + might launch the executable in a new session. The return value is + the exit code of the launched application. Usually, ``0`` indicates + success. + + Examples:: + + click.launch('https://click.palletsprojects.com/') + click.launch('/my/downloaded/file', locate=True) + + .. versionadded:: 2.0 + + :param url: URL or filename of the thing to launch. + :param wait: Wait for the program to exit before returning. This + only works if the launched program blocks. In particular, + ``xdg-open`` on Linux does not block. + :param locate: if this is set to `True` then instead of launching the + application associated with the URL it will attempt to + launch a file manager with the file located. This + might have weird effects if the URL does not point to + the filesystem. + """ + from ._termui_impl import open_url + + return open_url(url, wait=wait, locate=locate) + + +# If this is provided, getchar() calls into this instead. This is used +# for unittesting purposes. +_getchar: t.Callable[[bool], str] | None = None + + +def getchar(echo: bool = False) -> str: + """Fetches a single character from the terminal and returns it. This + will always return a unicode character and under certain rare + circumstances this might return more than one character. The + situations which more than one character is returned is when for + whatever reason multiple characters end up in the terminal buffer or + standard input was not actually a terminal. + + Note that this will always read from the terminal, even if something + is piped into the standard input. + + Note for Windows: in rare cases when typing non-ASCII characters, this + function might wait for a second character and then return both at once. + This is because certain Unicode characters look like special-key markers. + + .. versionadded:: 2.0 + + :param echo: if set to `True`, the character read will also show up on + the terminal. The default is to not show it. + """ + global _getchar + + if _getchar is None: + from ._termui_impl import getchar as f + + _getchar = f + + return _getchar(echo) + + +def raw_terminal() -> AbstractContextManager[int]: + from ._termui_impl import raw_terminal as f + + return f() + + +def pause(info: str | None = None, err: bool = False) -> None: + """This command stops execution and waits for the user to press any + key to continue. This is similar to the Windows batch "pause" + command. If the program is not run through a terminal, this command + will instead do nothing. + + .. versionadded:: 2.0 + + .. versionadded:: 4.0 + Added the `err` parameter. + + :param info: The message to print before pausing. Defaults to + ``"Press any key to continue..."``. + :param err: if set to message goes to ``stderr`` instead of + ``stdout``, the same as with echo. + """ + if not isatty(sys.stdin) or not isatty(sys.stdout): + return + + if info is None: + info = _("Press any key to continue...") + + try: + if info: + echo(info, nl=False, err=err) + try: + getchar() + except (KeyboardInterrupt, EOFError): + pass + finally: + if info: + echo(err=err) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/testing.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/testing.py new file mode 100644 index 0000000000..04e7f1d925 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/testing.py @@ -0,0 +1,669 @@ +from __future__ import annotations + +import collections.abc as cabc +import contextlib +import io +import os +import pdb +import shlex +import sys +import tempfile +import typing as t +from types import TracebackType + +from . import _compat +from . import formatting +from . import termui +from . import utils +from ._compat import _find_binary_reader + +if t.TYPE_CHECKING: + from _typeshed import ReadableBuffer + + from .core import Command + + +class EchoingStdin: + def __init__(self, input: t.BinaryIO, output: t.BinaryIO) -> None: + self._input = input + self._output = output + self._paused = False + + def __getattr__(self, x: str) -> t.Any: + return getattr(self._input, x) + + def _echo(self, rv: bytes) -> bytes: + if not self._paused: + self._output.write(rv) + + return rv + + def read(self, n: int = -1) -> bytes: + return self._echo(self._input.read(n)) + + def read1(self, n: int = -1) -> bytes: + return self._echo(self._input.read1(n)) # type: ignore + + def readline(self, n: int = -1) -> bytes: + return self._echo(self._input.readline(n)) + + def readlines(self) -> list[bytes]: + return [self._echo(x) for x in self._input.readlines()] + + def __iter__(self) -> cabc.Iterator[bytes]: + return iter(self._echo(x) for x in self._input) + + def __repr__(self) -> str: + return repr(self._input) + + +@contextlib.contextmanager +def _pause_echo(stream: EchoingStdin | None) -> cabc.Iterator[None]: + if stream is None: + yield + else: + stream._paused = True + yield + stream._paused = False + + +class BytesIOCopy(io.BytesIO): + """Patch ``io.BytesIO`` to let the written stream be copied to another. + + .. versionadded:: 8.2 + """ + + def __init__(self, copy_to: io.BytesIO) -> None: + super().__init__() + self.copy_to = copy_to + + def flush(self) -> None: + super().flush() + self.copy_to.flush() + + def write(self, b: ReadableBuffer) -> int: + self.copy_to.write(b) + return super().write(b) + + +class StreamMixer: + """Mixes `` and `` streams. + + The result is available in the ``output`` attribute. + + .. versionadded:: 8.2 + """ + + def __init__(self) -> None: + self.output: io.BytesIO = io.BytesIO() + self.stdout: io.BytesIO = BytesIOCopy(copy_to=self.output) + self.stderr: io.BytesIO = BytesIOCopy(copy_to=self.output) + + +class _NamedTextIOWrapper(io.TextIOWrapper): + """A :class:`~io.TextIOWrapper` with custom ``name`` and ``mode`` + that does not close its underlying buffer. + + An optional ``original_fd`` preserves the file descriptor of the + stream being replaced, so that C-level consumers that call + :meth:`fileno` (``faulthandler``, ``subprocess``, ...) still work. + Inspired by pytest's ``capsys``/``capfd`` split: see :doc:`/testing` + for details. + + .. versionchanged:: 8.3.3 + Added ``original_fd`` parameter and :meth:`fileno` override. + """ + + def __init__( + self, + buffer: t.BinaryIO, + name: str, + mode: str, + *, + original_fd: int = -1, + **kwargs: t.Any, + ) -> None: + super().__init__(buffer, **kwargs) + self._name = name + self._mode = mode + self._original_fd = original_fd + + def close(self) -> None: + """The buffer this object contains belongs to some other object, + so prevent the default ``__del__`` implementation from closing + that buffer. + + .. versionadded:: 8.3.2 + """ + + def fileno(self) -> int: + """Return the file descriptor of the original stream, if one was + provided at construction time. + + This allows C-level consumers (``faulthandler``, ``subprocess``, + signal handlers, ...) to obtain a valid fd without crashing, even + though the Python-level writes are redirected to an in-memory + buffer. + + .. versionadded:: 8.3.3 + """ + if self._original_fd >= 0: + return self._original_fd + return super().fileno() + + @property + def name(self) -> str: + return self._name + + @property + def mode(self) -> str: + return self._mode + + +def make_input_stream( + input: str | bytes | t.IO[t.Any] | None, charset: str +) -> t.BinaryIO: + # Is already an input stream. + if hasattr(input, "read"): + rv = _find_binary_reader(t.cast("t.IO[t.Any]", input)) + + if rv is not None: + return rv + + raise TypeError("Could not find binary reader for input stream.") + + if input is None: + input = b"" + elif isinstance(input, str): + input = input.encode(charset) + + return io.BytesIO(input) + + +class Result: + """Holds the captured result of an invoked CLI script. + + :param runner: The runner that created the result + :param stdout_bytes: The standard output as bytes. + :param stderr_bytes: The standard error as bytes. + :param output_bytes: A mix of ``stdout_bytes`` and ``stderr_bytes``, as the + user would see it in its terminal. + :param return_value: The value returned from the invoked command. + :param exit_code: The exit code as integer. + :param exception: The exception that happened if one did. + :param exc_info: Exception information (exception type, exception instance, + traceback type). + + .. versionchanged:: 8.2 + ``stderr_bytes`` no longer optional, ``output_bytes`` introduced and + ``mix_stderr`` has been removed. + + .. versionadded:: 8.0 + Added ``return_value``. + """ + + def __init__( + self, + runner: CliRunner, + stdout_bytes: bytes, + stderr_bytes: bytes, + output_bytes: bytes, + return_value: t.Any, + exit_code: int, + exception: BaseException | None, + exc_info: tuple[type[BaseException], BaseException, TracebackType] + | None = None, + ): + self.runner = runner + self.stdout_bytes = stdout_bytes + self.stderr_bytes = stderr_bytes + self.output_bytes = output_bytes + self.return_value = return_value + self.exit_code = exit_code + self.exception = exception + self.exc_info = exc_info + + @property + def output(self) -> str: + """The terminal output as unicode string, as the user would see it. + + .. versionchanged:: 8.2 + No longer a proxy for ``self.stdout``. Now has its own independent stream + that is mixing `` and ``, in the order they were written. + """ + return self.output_bytes.decode(self.runner.charset, "replace").replace( + "\r\n", "\n" + ) + + @property + def stdout(self) -> str: + """The standard output as unicode string.""" + return self.stdout_bytes.decode(self.runner.charset, "replace").replace( + "\r\n", "\n" + ) + + @property + def stderr(self) -> str: + """The standard error as unicode string. + + .. versionchanged:: 8.2 + No longer raise an exception, always returns the `` string. + """ + return self.stderr_bytes.decode(self.runner.charset, "replace").replace( + "\r\n", "\n" + ) + + def __repr__(self) -> str: + exc_str = repr(self.exception) if self.exception else "okay" + return f"<{type(self).__name__} {exc_str}>" + + +class CliRunner: + """The CLI runner provides functionality to invoke a Click command line + script for unittesting purposes in a isolated environment. This only + works in single-threaded systems without any concurrency as it changes the + global interpreter state. + + :param charset: the character set for the input and output data. + :param env: a dictionary with environment variables for overriding. + :param echo_stdin: if this is set to `True`, then reading from `` writes + to ``. This is useful for showing examples in + some circumstances. Note that regular prompts + will automatically echo the input. + :param catch_exceptions: Whether to catch any exceptions other than + ``SystemExit`` when running :meth:`~CliRunner.invoke`. + + .. versionchanged:: 8.2 + Added the ``catch_exceptions`` parameter. + + .. versionchanged:: 8.2 + ``mix_stderr`` parameter has been removed. + """ + + def __init__( + self, + charset: str = "utf-8", + env: cabc.Mapping[str, str | None] | None = None, + echo_stdin: bool = False, + catch_exceptions: bool = True, + ) -> None: + self.charset = charset + self.env: cabc.Mapping[str, str | None] = env or {} + self.echo_stdin = echo_stdin + self.catch_exceptions = catch_exceptions + + def get_default_prog_name(self, cli: Command) -> str: + """Given a command object it will return the default program name + for it. The default is the `name` attribute or ``"root"`` if not + set. + """ + return cli.name or "root" + + def make_env( + self, overrides: cabc.Mapping[str, str | None] | None = None + ) -> cabc.Mapping[str, str | None]: + """Returns the environment overrides for invoking a script.""" + rv = dict(self.env) + if overrides: + rv.update(overrides) + return rv + + @contextlib.contextmanager + def isolation( + self, + input: str | bytes | t.IO[t.Any] | None = None, + env: cabc.Mapping[str, str | None] | None = None, + color: bool = False, + ) -> cabc.Iterator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]]: + """A context manager that sets up the isolation for invoking of a + command line tool. This sets up `` with the given input data + and `os.environ` with the overrides from the given dictionary. + This also rebinds some internals in Click to be mocked (like the + prompt functionality). + + This is automatically done in the :meth:`invoke` method. + + :param input: the input stream to put into `sys.stdin`. + :param env: the environment overrides as dictionary. + :param color: whether the output should contain color codes. The + application can still override this explicitly. + + .. versionadded:: 8.2 + An additional output stream is returned, which is a mix of + `` and `` streams. + + .. versionchanged:: 8.2 + Always returns the `` stream. + + .. versionchanged:: 8.0 + `` is opened with ``errors="backslashreplace"`` + instead of the default ``"strict"``. + + .. versionchanged:: 4.0 + Added the ``color`` parameter. + """ + bytes_input = make_input_stream(input, self.charset) + echo_input = None + + old_stdin = sys.stdin + old_stdout = sys.stdout + old_stderr = sys.stderr + old_forced_width = formatting.FORCED_WIDTH + formatting.FORCED_WIDTH = 80 + + env = self.make_env(env) + + stream_mixer = StreamMixer() + + # Preserve the original file descriptors so that C-level + # consumers (faulthandler, subprocess, etc.) can still obtain a + # valid fd from the redirected streams. The original streams + # may themselves lack a fileno() (e.g. when CliRunner is used + # inside pytest's capsys), so we fall back to -1. + def _safe_fileno(stream: t.IO[t.Any]) -> int: + try: + return stream.fileno() + except (AttributeError, io.UnsupportedOperation): + return -1 + + old_stdout_fd = _safe_fileno(old_stdout) + old_stderr_fd = _safe_fileno(old_stderr) + + if self.echo_stdin: + bytes_input = echo_input = t.cast( + t.BinaryIO, EchoingStdin(bytes_input, stream_mixer.stdout) + ) + + sys.stdin = text_input = _NamedTextIOWrapper( + bytes_input, encoding=self.charset, name="", mode="r" + ) + + if self.echo_stdin: + # Force unbuffered reads, otherwise TextIOWrapper reads a + # large chunk which is echoed early. + text_input._CHUNK_SIZE = 1 # type: ignore + + sys.stdout = _NamedTextIOWrapper( + stream_mixer.stdout, + encoding=self.charset, + name="", + mode="w", + original_fd=old_stdout_fd, + ) + + sys.stderr = _NamedTextIOWrapper( + stream_mixer.stderr, + encoding=self.charset, + name="", + mode="w", + errors="backslashreplace", + original_fd=old_stderr_fd, + ) + + @_pause_echo(echo_input) # type: ignore + def visible_input(prompt: str | None = None) -> str: + sys.stdout.write(prompt or "") + try: + val = next(text_input).rstrip("\r\n") + except StopIteration as e: + raise EOFError() from e + sys.stdout.write(f"{val}\n") + sys.stdout.flush() + return val + + @_pause_echo(echo_input) # type: ignore + def hidden_input(prompt: str | None = None) -> str: + sys.stdout.write(f"{prompt or ''}\n") + sys.stdout.flush() + try: + return next(text_input).rstrip("\r\n") + except StopIteration as e: + raise EOFError() from e + + @_pause_echo(echo_input) # type: ignore + def _getchar(echo: bool) -> str: + char = sys.stdin.read(1) + + if echo: + sys.stdout.write(char) + + sys.stdout.flush() + return char + + default_color = color + + def should_strip_ansi( + stream: t.IO[t.Any] | None = None, color: bool | None = None + ) -> bool: + if color is None: + return not default_color + return not color + + old_visible_prompt_func = termui.visible_prompt_func + old_hidden_prompt_func = termui.hidden_prompt_func + old__getchar_func = termui._getchar + old_should_strip_ansi = utils.should_strip_ansi # type: ignore + old__compat_should_strip_ansi = _compat.should_strip_ansi + old_pdb_init = pdb.Pdb.__init__ + termui.visible_prompt_func = visible_input + termui.hidden_prompt_func = hidden_input + termui._getchar = _getchar + utils.should_strip_ansi = should_strip_ansi # type: ignore + _compat.should_strip_ansi = should_strip_ansi + + def _patched_pdb_init( + self: pdb.Pdb, + completekey: str = "tab", + stdin: t.IO[str] | None = None, + stdout: t.IO[str] | None = None, + **kwargs: t.Any, + ) -> None: + """Default ``pdb.Pdb`` to real terminal streams during + ``CliRunner`` isolation. + + Without this patch, ``pdb.Pdb.__init__`` inherits from + ``cmd.Cmd`` which falls back to ``sys.stdin``/``sys.stdout`` + when no explicit streams are provided. During isolation + those are ``BytesIO``-backed wrappers, so the debugger + reads from an empty buffer and writes to captured output, + making interactive debugging impossible. + + By defaulting to ``sys.__stdin__``/``sys.__stdout__`` (the + original terminal streams Python preserves regardless of + redirection), debuggers can interact with the user while + ``click.echo`` output is still captured normally. + + This covers ``pdb.set_trace()``, ``breakpoint()``, + ``pdb.post_mortem()``, and debuggers that subclass + ``pdb.Pdb`` (ipdb, pdbpp). Explicit ``stdin``/``stdout`` + arguments are honored and not overridden. Debuggers that + do not subclass ``pdb.Pdb`` (pudb, debugpy) are not + covered. + """ + if stdin is None: + stdin = sys.__stdin__ + if stdout is None: + stdout = sys.__stdout__ + old_pdb_init( + self, completekey=completekey, stdin=stdin, stdout=stdout, **kwargs + ) + + pdb.Pdb.__init__ = _patched_pdb_init # type: ignore[assignment] + + old_env = {} + try: + for key, value in env.items(): + old_env[key] = os.environ.get(key) + if value is None: + try: + del os.environ[key] + except Exception: + pass + else: + os.environ[key] = value + yield (stream_mixer.stdout, stream_mixer.stderr, stream_mixer.output) + finally: + for key, value in old_env.items(): + if value is None: + try: + del os.environ[key] + except Exception: + pass + else: + os.environ[key] = value + sys.stdout = old_stdout + sys.stderr = old_stderr + sys.stdin = old_stdin + termui.visible_prompt_func = old_visible_prompt_func + termui.hidden_prompt_func = old_hidden_prompt_func + termui._getchar = old__getchar_func + utils.should_strip_ansi = old_should_strip_ansi # type: ignore + _compat.should_strip_ansi = old__compat_should_strip_ansi + formatting.FORCED_WIDTH = old_forced_width + pdb.Pdb.__init__ = old_pdb_init # type: ignore[method-assign] + + def invoke( + self, + cli: Command, + args: str | cabc.Sequence[str] | None = None, + input: str | bytes | t.IO[t.Any] | None = None, + env: cabc.Mapping[str, str | None] | None = None, + catch_exceptions: bool | None = None, + color: bool = False, + **extra: t.Any, + ) -> Result: + """Invokes a command in an isolated environment. The arguments are + forwarded directly to the command line script, the `extra` keyword + arguments are passed to the :meth:`~clickpkg.Command.main` function of + the command. + + This returns a :class:`Result` object. + + :param cli: the command to invoke + :param args: the arguments to invoke. It may be given as an iterable + or a string. When given as string it will be interpreted + as a Unix shell command. More details at + :func:`shlex.split`. + :param input: the input data for `sys.stdin`. + :param env: the environment overrides. + :param catch_exceptions: Whether to catch any other exceptions than + ``SystemExit``. If :data:`None`, the value + from :class:`CliRunner` is used. + :param extra: the keyword arguments to pass to :meth:`main`. + :param color: whether the output should contain color codes. The + application can still override this explicitly. + + .. versionadded:: 8.2 + The result object has the ``output_bytes`` attribute with + the mix of ``stdout_bytes`` and ``stderr_bytes``, as the user would + see it in its terminal. + + .. versionchanged:: 8.2 + The result object always returns the ``stderr_bytes`` stream. + + .. versionchanged:: 8.0 + The result object has the ``return_value`` attribute with + the value returned from the invoked command. + + .. versionchanged:: 4.0 + Added the ``color`` parameter. + + .. versionchanged:: 3.0 + Added the ``catch_exceptions`` parameter. + + .. versionchanged:: 3.0 + The result object has the ``exc_info`` attribute with the + traceback if available. + """ + exc_info = None + if catch_exceptions is None: + catch_exceptions = self.catch_exceptions + + with self.isolation(input=input, env=env, color=color) as outstreams: + return_value = None + exception: BaseException | None = None + exit_code = 0 + + if isinstance(args, str): + args = shlex.split(args) + + try: + prog_name = extra.pop("prog_name") + except KeyError: + prog_name = self.get_default_prog_name(cli) + + try: + return_value = cli.main(args=args or (), prog_name=prog_name, **extra) + except SystemExit as e: + exc_info = sys.exc_info() + e_code = t.cast("int | t.Any | None", e.code) + + if e_code is None: + e_code = 0 + + if e_code != 0: + exception = e + + if not isinstance(e_code, int): + sys.stdout.write(str(e_code)) + sys.stdout.write("\n") + e_code = 1 + + exit_code = e_code + + except Exception as e: + if not catch_exceptions: + raise + exception = e + exit_code = 1 + exc_info = sys.exc_info() + finally: + sys.stdout.flush() + sys.stderr.flush() + stdout = outstreams[0].getvalue() + stderr = outstreams[1].getvalue() + output = outstreams[2].getvalue() + + return Result( + runner=self, + stdout_bytes=stdout, + stderr_bytes=stderr, + output_bytes=output, + return_value=return_value, + exit_code=exit_code, + exception=exception, + exc_info=exc_info, # type: ignore + ) + + @contextlib.contextmanager + def isolated_filesystem( + self, temp_dir: str | os.PathLike[str] | None = None + ) -> cabc.Iterator[str]: + """A context manager that creates a temporary directory and + changes the current working directory to it. This isolates tests + that affect the contents of the CWD to prevent them from + interfering with each other. + + :param temp_dir: Create the temporary directory under this + directory. If given, the created directory is not removed + when exiting. + + .. versionchanged:: 8.0 + Added the ``temp_dir`` parameter. + """ + cwd = os.getcwd() + dt = tempfile.mkdtemp(dir=temp_dir) + os.chdir(dt) + + try: + yield dt + finally: + os.chdir(cwd) + + if temp_dir is None: + import shutil + + try: + shutil.rmtree(dt) + except OSError: + pass diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/types.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/types.py new file mode 100644 index 0000000000..e71c1c21e4 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/types.py @@ -0,0 +1,1209 @@ +from __future__ import annotations + +import collections.abc as cabc +import enum +import os +import stat +import sys +import typing as t +from datetime import datetime +from gettext import gettext as _ +from gettext import ngettext + +from ._compat import _get_argv_encoding +from ._compat import open_stream +from .exceptions import BadParameter +from .utils import format_filename +from .utils import LazyFile +from .utils import safecall + +if t.TYPE_CHECKING: + import typing_extensions as te + + from .core import Context + from .core import Parameter + from .shell_completion import CompletionItem + +ParamTypeValue = t.TypeVar("ParamTypeValue") + + +class ParamType: + """Represents the type of a parameter. Validates and converts values + from the command line or Python into the correct type. + + To implement a custom type, subclass and implement at least the + following: + + - The :attr:`name` class attribute must be set. + - Calling an instance of the type with ``None`` must return + ``None``. This is already implemented by default. + - :meth:`convert` must convert string values to the correct type. + - :meth:`convert` must accept values that are already the correct + type. + - It must be able to convert a value if the ``ctx`` and ``param`` + arguments are ``None``. This can occur when converting prompt + input. + """ + + is_composite: t.ClassVar[bool] = False + arity: t.ClassVar[int] = 1 + + #: the descriptive name of this type + name: str + + #: if a list of this type is expected and the value is pulled from a + #: string environment variable, this is what splits it up. `None` + #: means any whitespace. For all parameters the general rule is that + #: whitespace splits them up. The exception are paths and files which + #: are split by ``os.path.pathsep`` by default (":" on Unix and ";" on + #: Windows). + envvar_list_splitter: t.ClassVar[str | None] = None + + def to_info_dict(self) -> dict[str, t.Any]: + """Gather information that could be useful for a tool generating + user-facing documentation. + + Use :meth:`click.Context.to_info_dict` to traverse the entire + CLI structure. + + .. versionadded:: 8.0 + """ + # The class name without the "ParamType" suffix. + param_type = type(self).__name__.partition("ParamType")[0] + param_type = param_type.partition("ParameterType")[0] + + # Custom subclasses might not remember to set a name. + if hasattr(self, "name"): + name = self.name + else: + name = param_type + + return {"param_type": param_type, "name": name} + + def __call__( + self, + value: t.Any, + param: Parameter | None = None, + ctx: Context | None = None, + ) -> t.Any: + if value is not None: + return self.convert(value, param, ctx) + + def get_metavar(self, param: Parameter, ctx: Context) -> str | None: + """Returns the metavar default for this param if it provides one.""" + + def get_missing_message(self, param: Parameter, ctx: Context | None) -> str | None: + """Optionally might return extra information about a missing + parameter. + + .. versionadded:: 2.0 + """ + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + """Convert the value to the correct type. This is not called if + the value is ``None`` (the missing value). + + This must accept string values from the command line, as well as + values that are already the correct type. It may also convert + other compatible types. + + The ``param`` and ``ctx`` arguments may be ``None`` in certain + situations, such as when converting prompt input. + + If the value cannot be converted, call :meth:`fail` with a + descriptive message. + + :param value: The value to convert. + :param param: The parameter that is using this type to convert + its value. May be ``None``. + :param ctx: The current context that arrived at this value. May + be ``None``. + """ + return value + + def split_envvar_value(self, rv: str) -> cabc.Sequence[str]: + """Given a value from an environment variable this splits it up + into small chunks depending on the defined envvar list splitter. + + If the splitter is set to `None`, which means that whitespace splits, + then leading and trailing whitespace is ignored. Otherwise, leading + and trailing splitters usually lead to empty items being included. + """ + return (rv or "").split(self.envvar_list_splitter) + + def fail( + self, + message: str, + param: Parameter | None = None, + ctx: Context | None = None, + ) -> t.NoReturn: + """Helper method to fail with an invalid value message.""" + raise BadParameter(message, ctx=ctx, param=param) + + def shell_complete( + self, ctx: Context, param: Parameter, incomplete: str + ) -> list[CompletionItem]: + """Return a list of + :class:`~click.shell_completion.CompletionItem` objects for the + incomplete value. Most types do not provide completions, but + some do, and this allows custom types to provide custom + completions as well. + + :param ctx: Invocation context for this command. + :param param: The parameter that is requesting completion. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + return [] + + +class CompositeParamType(ParamType): + is_composite = True + + @property + def arity(self) -> int: # type: ignore + raise NotImplementedError() + + +class FuncParamType(ParamType): + def __init__(self, func: t.Callable[[t.Any], t.Any]) -> None: + self.name: str = func.__name__ + self.func = func + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict["func"] = self.func + return info_dict + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + try: + return self.func(value) + except ValueError: + try: + value = str(value) + except UnicodeError: + value = value.decode("utf-8", "replace") + + self.fail(value, param, ctx) + + +class UnprocessedParamType(ParamType): + name = "text" + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + return value + + def __repr__(self) -> str: + return "UNPROCESSED" + + +class StringParamType(ParamType): + name = "text" + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + if isinstance(value, bytes): + enc = _get_argv_encoding() + try: + value = value.decode(enc) + except UnicodeError: + fs_enc = sys.getfilesystemencoding() + if fs_enc != enc: + try: + value = value.decode(fs_enc) + except UnicodeError: + value = value.decode("utf-8", "replace") + else: + value = value.decode("utf-8", "replace") + return value + return str(value) + + def __repr__(self) -> str: + return "STRING" + + +class Choice(ParamType, t.Generic[ParamTypeValue]): + """The choice type allows a value to be checked against a fixed set + of supported values. + + You may pass any iterable value which will be converted to a tuple + and thus will only be iterated once. + + The resulting value will always be one of the originally passed choices. + See :meth:`normalize_choice` for more info on the mapping of strings + to choices. See :ref:`choice-opts` for an example. + + :param case_sensitive: Set to false to make choices case + insensitive. Defaults to true. + + .. versionchanged:: 8.2.0 + Non-``str`` ``choices`` are now supported. It can additionally be any + iterable. Before you were not recommended to pass anything but a list or + tuple. + + .. versionadded:: 8.2.0 + Choice normalization can be overridden via :meth:`normalize_choice`. + """ + + name = "choice" + + def __init__( + self, choices: cabc.Iterable[ParamTypeValue], case_sensitive: bool = True + ) -> None: + self.choices: cabc.Sequence[ParamTypeValue] = tuple(choices) + self.case_sensitive = case_sensitive + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict["choices"] = self.choices + info_dict["case_sensitive"] = self.case_sensitive + return info_dict + + def _normalized_mapping( + self, ctx: Context | None = None + ) -> cabc.Mapping[ParamTypeValue, str]: + """ + Returns mapping where keys are the original choices and the values are + the normalized values that are accepted via the command line. + + This is a simple wrapper around :meth:`normalize_choice`, use that + instead which is supported. + """ + return { + choice: self.normalize_choice( + choice=choice, + ctx=ctx, + ) + for choice in self.choices + } + + def normalize_choice(self, choice: ParamTypeValue, ctx: Context | None) -> str: + """ + Normalize a choice value, used to map a passed string to a choice. + Each choice must have a unique normalized value. + + By default uses :meth:`Context.token_normalize_func` and if not case + sensitive, convert it to a casefolded value. + + .. versionadded:: 8.2.0 + """ + normed_value = choice.name if isinstance(choice, enum.Enum) else str(choice) + + if ctx is not None and ctx.token_normalize_func is not None: + normed_value = ctx.token_normalize_func(normed_value) + + if not self.case_sensitive: + normed_value = normed_value.casefold() + + return normed_value + + def get_metavar(self, param: Parameter, ctx: Context) -> str | None: + if param.param_type_name == "option" and not param.show_choices: # type: ignore + choice_metavars = [ + convert_type(type(choice)).name.upper() for choice in self.choices + ] + choices_str = "|".join([*dict.fromkeys(choice_metavars)]) + else: + choices_str = "|".join( + [str(i) for i in self._normalized_mapping(ctx=ctx).values()] + ) + + # Use curly braces to indicate a required argument. + if param.required and param.param_type_name == "argument": + return f"{{{choices_str}}}" + + # Use square braces to indicate an option or optional argument. + return f"[{choices_str}]" + + def get_missing_message(self, param: Parameter, ctx: Context | None) -> str: + """ + Message shown when no choice is passed. + + .. versionchanged:: 8.2.0 Added ``ctx`` argument. + """ + return _("Choose from:\n\t{choices}").format( + choices=",\n\t".join(self._normalized_mapping(ctx=ctx).values()) + ) + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> ParamTypeValue: + """ + For a given value from the parser, normalize it and find its + matching normalized value in the list of choices. Then return the + matched "original" choice. + """ + normed_value = self.normalize_choice(choice=value, ctx=ctx) + normalized_mapping = self._normalized_mapping(ctx=ctx) + + try: + return next( + original + for original, normalized in normalized_mapping.items() + if normalized == normed_value + ) + except StopIteration: + self.fail( + self.get_invalid_choice_message(value=value, ctx=ctx), + param=param, + ctx=ctx, + ) + + def get_invalid_choice_message(self, value: t.Any, ctx: Context | None) -> str: + """Get the error message when the given choice is invalid. + + :param value: The invalid value. + + .. versionadded:: 8.2 + """ + choices_str = ", ".join(map(repr, self._normalized_mapping(ctx=ctx).values())) + return ngettext( + "{value!r} is not {choice}.", + "{value!r} is not one of {choices}.", + len(self.choices), + ).format(value=value, choice=choices_str, choices=choices_str) + + def __repr__(self) -> str: + return f"Choice({list(self.choices)})" + + def shell_complete( + self, ctx: Context, param: Parameter, incomplete: str + ) -> list[CompletionItem]: + """Complete choices that start with the incomplete value. + + :param ctx: Invocation context for this command. + :param param: The parameter that is requesting completion. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + from click.shell_completion import CompletionItem + + str_choices = map(str, self.choices) + + if self.case_sensitive: + matched = (c for c in str_choices if c.startswith(incomplete)) + else: + incomplete = incomplete.lower() + matched = (c for c in str_choices if c.lower().startswith(incomplete)) + + return [CompletionItem(c) for c in matched] + + +class DateTime(ParamType): + """The DateTime type converts date strings into `datetime` objects. + + The format strings which are checked are configurable, but default to some + common (non-timezone aware) ISO 8601 formats. + + When specifying *DateTime* formats, you should only pass a list or a tuple. + Other iterables, like generators, may lead to surprising results. + + The format strings are processed using ``datetime.strptime``, and this + consequently defines the format strings which are allowed. + + Parsing is tried using each format, in order, and the first format which + parses successfully is used. + + :param formats: A list or tuple of date format strings, in the order in + which they should be tried. Defaults to + ``'%Y-%m-%d'``, ``'%Y-%m-%dT%H:%M:%S'``, + ``'%Y-%m-%d %H:%M:%S'``. + """ + + name = "datetime" + + def __init__(self, formats: cabc.Sequence[str] | None = None): + self.formats: cabc.Sequence[str] = formats or [ + "%Y-%m-%d", + "%Y-%m-%dT%H:%M:%S", + "%Y-%m-%d %H:%M:%S", + ] + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict["formats"] = self.formats + return info_dict + + def get_metavar(self, param: Parameter, ctx: Context) -> str | None: + return f"[{'|'.join(self.formats)}]" + + def _try_to_convert_date(self, value: t.Any, format: str) -> datetime | None: + try: + return datetime.strptime(value, format) + except ValueError: + return None + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + if isinstance(value, datetime): + return value + + for format in self.formats: + converted = self._try_to_convert_date(value, format) + + if converted is not None: + return converted + + formats_str = ", ".join(map(repr, self.formats)) + self.fail( + ngettext( + "{value!r} does not match the format {format}.", + "{value!r} does not match the formats {formats}.", + len(self.formats), + ).format(value=value, format=formats_str, formats=formats_str), + param, + ctx, + ) + + def __repr__(self) -> str: + return "DateTime" + + +class _NumberParamTypeBase(ParamType): + _number_class: t.ClassVar[type[t.Any]] + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + try: + return self._number_class(value) + except ValueError: + self.fail( + _("{value!r} is not a valid {number_type}.").format( + value=value, number_type=self.name + ), + param, + ctx, + ) + + +class _NumberRangeBase(_NumberParamTypeBase): + def __init__( + self, + min: float | None = None, + max: float | None = None, + min_open: bool = False, + max_open: bool = False, + clamp: bool = False, + ) -> None: + self.min = min + self.max = max + self.min_open = min_open + self.max_open = max_open + self.clamp = clamp + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict.update( + min=self.min, + max=self.max, + min_open=self.min_open, + max_open=self.max_open, + clamp=self.clamp, + ) + return info_dict + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + import operator + + rv = super().convert(value, param, ctx) + lt_min: bool = self.min is not None and ( + operator.le if self.min_open else operator.lt + )(rv, self.min) + gt_max: bool = self.max is not None and ( + operator.ge if self.max_open else operator.gt + )(rv, self.max) + + if self.clamp: + if lt_min: + return self._clamp(self.min, 1, self.min_open) # type: ignore + + if gt_max: + return self._clamp(self.max, -1, self.max_open) # type: ignore + + if lt_min or gt_max: + self.fail( + _("{value} is not in the range {range}.").format( + value=rv, range=self._describe_range() + ), + param, + ctx, + ) + + return rv + + def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float: + """Find the valid value to clamp to bound in the given + direction. + + :param bound: The boundary value. + :param dir: 1 or -1 indicating the direction to move. + :param open: If true, the range does not include the bound. + """ + raise NotImplementedError + + def _describe_range(self) -> str: + """Describe the range for use in help text.""" + if self.min is None: + op = "<" if self.max_open else "<=" + return f"x{op}{self.max}" + + if self.max is None: + op = ">" if self.min_open else ">=" + return f"x{op}{self.min}" + + lop = "<" if self.min_open else "<=" + rop = "<" if self.max_open else "<=" + return f"{self.min}{lop}x{rop}{self.max}" + + def __repr__(self) -> str: + clamp = " clamped" if self.clamp else "" + return f"<{type(self).__name__} {self._describe_range()}{clamp}>" + + +class IntParamType(_NumberParamTypeBase): + name = "integer" + _number_class = int + + def __repr__(self) -> str: + return "INT" + + +class IntRange(_NumberRangeBase, IntParamType): + """Restrict an :data:`click.INT` value to a range of accepted + values. See :ref:`ranges`. + + If ``min`` or ``max`` are not passed, any value is accepted in that + direction. If ``min_open`` or ``max_open`` are enabled, the + corresponding boundary is not included in the range. + + If ``clamp`` is enabled, a value outside the range is clamped to the + boundary instead of failing. + + .. versionchanged:: 8.0 + Added the ``min_open`` and ``max_open`` parameters. + """ + + name = "integer range" + + def _clamp( # type: ignore + self, bound: int, dir: t.Literal[1, -1], open: bool + ) -> int: + if not open: + return bound + + return bound + dir + + +class FloatParamType(_NumberParamTypeBase): + name = "float" + _number_class = float + + def __repr__(self) -> str: + return "FLOAT" + + +class FloatRange(_NumberRangeBase, FloatParamType): + """Restrict a :data:`click.FLOAT` value to a range of accepted + values. See :ref:`ranges`. + + If ``min`` or ``max`` are not passed, any value is accepted in that + direction. If ``min_open`` or ``max_open`` are enabled, the + corresponding boundary is not included in the range. + + If ``clamp`` is enabled, a value outside the range is clamped to the + boundary instead of failing. This is not supported if either + boundary is marked ``open``. + + .. versionchanged:: 8.0 + Added the ``min_open`` and ``max_open`` parameters. + """ + + name = "float range" + + def __init__( + self, + min: float | None = None, + max: float | None = None, + min_open: bool = False, + max_open: bool = False, + clamp: bool = False, + ) -> None: + super().__init__( + min=min, max=max, min_open=min_open, max_open=max_open, clamp=clamp + ) + + if (min_open or max_open) and clamp: + raise TypeError("Clamping is not supported for open bounds.") + + def _clamp(self, bound: float, dir: t.Literal[1, -1], open: bool) -> float: + if not open: + return bound + + # Could use math.nextafter here, but clamping an + # open float range doesn't seem to be particularly useful. It's + # left up to the user to write a callback to do it if needed. + raise RuntimeError("Clamping is not supported for open bounds.") + + +class BoolParamType(ParamType): + name = "boolean" + + bool_states: dict[str, bool] = { + "1": True, + "0": False, + "yes": True, + "no": False, + "true": True, + "false": False, + "on": True, + "off": False, + "t": True, + "f": False, + "y": True, + "n": False, + # Absence of value is considered False. + "": False, + } + """A mapping of string values to boolean states. + + Mapping is inspired by :py:attr:`configparser.ConfigParser.BOOLEAN_STATES` + and extends it. + + .. caution:: + String values are lower-cased, as the ``str_to_bool`` comparison function + below is case-insensitive. + + .. warning:: + The mapping is not exhaustive, and does not cover all possible boolean strings + representations. It will remains as it is to avoid endless bikeshedding. + + Future work my be considered to make this mapping user-configurable from public + API. + """ + + @staticmethod + def str_to_bool(value: str | bool) -> bool | None: + """Convert a string to a boolean value. + + If the value is already a boolean, it is returned as-is. If the value is a + string, it is stripped of whitespaces and lower-cased, then checked against + the known boolean states pre-defined in the `BoolParamType.bool_states` mapping + above. + + Returns `None` if the value does not match any known boolean state. + """ + if isinstance(value, bool): + return value + return BoolParamType.bool_states.get(value.strip().lower()) + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> bool: + normalized = self.str_to_bool(value) + if normalized is None: + self.fail( + _( + "{value!r} is not a valid boolean. Recognized values: {states}" + ).format(value=value, states=", ".join(sorted(self.bool_states))), + param, + ctx, + ) + return normalized + + def __repr__(self) -> str: + return "BOOL" + + +class UUIDParameterType(ParamType): + name = "uuid" + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + import uuid + + if isinstance(value, uuid.UUID): + return value + + value = value.strip() + + try: + return uuid.UUID(value) + except ValueError: + self.fail( + _("{value!r} is not a valid UUID.").format(value=value), param, ctx + ) + + def __repr__(self) -> str: + return "UUID" + + +class File(ParamType): + """Declares a parameter to be a file for reading or writing. The file + is automatically closed once the context tears down (after the command + finished working). + + Files can be opened for reading or writing. The special value ``-`` + indicates stdin or stdout depending on the mode. + + By default, the file is opened for reading text data, but it can also be + opened in binary mode or for writing. The encoding parameter can be used + to force a specific encoding. + + The `lazy` flag controls if the file should be opened immediately or upon + first IO. The default is to be non-lazy for standard input and output + streams as well as files opened for reading, `lazy` otherwise. When opening a + file lazily for reading, it is still opened temporarily for validation, but + will not be held open until first IO. lazy is mainly useful when opening + for writing to avoid creating the file until it is needed. + + Files can also be opened atomically in which case all writes go into a + separate file in the same folder and upon completion the file will + be moved over to the original location. This is useful if a file + regularly read by other users is modified. + + See :ref:`file-args` for more information. + + .. versionchanged:: 2.0 + Added the ``atomic`` parameter. + """ + + name = "filename" + envvar_list_splitter: t.ClassVar[str] = os.path.pathsep + + def __init__( + self, + mode: str = "r", + encoding: str | None = None, + errors: str | None = "strict", + lazy: bool | None = None, + atomic: bool = False, + ) -> None: + self.mode = mode + self.encoding = encoding + self.errors = errors + self.lazy = lazy + self.atomic = atomic + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict.update(mode=self.mode, encoding=self.encoding) + return info_dict + + def resolve_lazy_flag(self, value: str | os.PathLike[str]) -> bool: + if self.lazy is not None: + return self.lazy + if os.fspath(value) == "-": + return False + elif "w" in self.mode: + return True + return False + + def convert( + self, + value: str | os.PathLike[str] | t.IO[t.Any], + param: Parameter | None, + ctx: Context | None, + ) -> t.IO[t.Any]: + if _is_file_like(value): + return value + + value = t.cast("str | os.PathLike[str]", value) + + try: + lazy = self.resolve_lazy_flag(value) + + if lazy: + lf = LazyFile( + value, self.mode, self.encoding, self.errors, atomic=self.atomic + ) + + if ctx is not None: + ctx.call_on_close(lf.close_intelligently) + + return t.cast("t.IO[t.Any]", lf) + + f, should_close = open_stream( + value, self.mode, self.encoding, self.errors, atomic=self.atomic + ) + + # If a context is provided, we automatically close the file + # at the end of the context execution (or flush out). If a + # context does not exist, it's the caller's responsibility to + # properly close the file. This for instance happens when the + # type is used with prompts. + if ctx is not None: + if should_close: + ctx.call_on_close(safecall(f.close)) + else: + ctx.call_on_close(safecall(f.flush)) + + return f + except OSError as e: + self.fail(f"'{format_filename(value)}': {e.strerror}", param, ctx) + + def shell_complete( + self, ctx: Context, param: Parameter, incomplete: str + ) -> list[CompletionItem]: + """Return a special completion marker that tells the completion + system to use the shell to provide file path completions. + + :param ctx: Invocation context for this command. + :param param: The parameter that is requesting completion. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + from click.shell_completion import CompletionItem + + return [CompletionItem(incomplete, type="file")] + + +def _is_file_like(value: t.Any) -> te.TypeGuard[t.IO[t.Any]]: + return hasattr(value, "read") or hasattr(value, "write") + + +class Path(ParamType): + """The ``Path`` type is similar to the :class:`File` type, but + returns the filename instead of an open file. Various checks can be + enabled to validate the type of file and permissions. + + :param exists: The file or directory needs to exist for the value to + be valid. If this is not set to ``True``, and the file does not + exist, then all further checks are silently skipped. + :param file_okay: Allow a file as a value. + :param dir_okay: Allow a directory as a value. + :param readable: if true, a readable check is performed. + :param writable: if true, a writable check is performed. + :param executable: if true, an executable check is performed. + :param resolve_path: Make the value absolute and resolve any + symlinks. A ``~`` is not expanded, as this is supposed to be + done by the shell only. + :param allow_dash: Allow a single dash as a value, which indicates + a standard stream (but does not open it). Use + :func:`~click.open_file` to handle opening this value. + :param path_type: Convert the incoming path value to this type. If + ``None``, keep Python's default, which is ``str``. Useful to + convert to :class:`pathlib.Path`. + + .. versionchanged:: 8.1 + Added the ``executable`` parameter. + + .. versionchanged:: 8.0 + Allow passing ``path_type=pathlib.Path``. + + .. versionchanged:: 6.0 + Added the ``allow_dash`` parameter. + """ + + envvar_list_splitter: t.ClassVar[str] = os.path.pathsep + + def __init__( + self, + exists: bool = False, + file_okay: bool = True, + dir_okay: bool = True, + writable: bool = False, + readable: bool = True, + resolve_path: bool = False, + allow_dash: bool = False, + path_type: type[t.Any] | None = None, + executable: bool = False, + ): + self.exists = exists + self.file_okay = file_okay + self.dir_okay = dir_okay + self.readable = readable + self.writable = writable + self.executable = executable + self.resolve_path = resolve_path + self.allow_dash = allow_dash + self.type = path_type + + if self.file_okay and not self.dir_okay: + self.name: str = _("file") + elif self.dir_okay and not self.file_okay: + self.name = _("directory") + else: + self.name = _("path") + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict.update( + exists=self.exists, + file_okay=self.file_okay, + dir_okay=self.dir_okay, + writable=self.writable, + readable=self.readable, + allow_dash=self.allow_dash, + ) + return info_dict + + def coerce_path_result( + self, value: str | os.PathLike[str] + ) -> str | bytes | os.PathLike[str]: + if self.type is not None and not isinstance(value, self.type): + if self.type is str: + return os.fsdecode(value) + elif self.type is bytes: + return os.fsencode(value) + else: + return t.cast("os.PathLike[str]", self.type(value)) + + return value + + def convert( + self, + value: str | os.PathLike[str], + param: Parameter | None, + ctx: Context | None, + ) -> str | bytes | os.PathLike[str]: + rv = value + + is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-") + + if not is_dash: + if self.resolve_path: + rv = os.path.realpath(rv) + + try: + st = os.stat(rv) + except OSError: + if not self.exists: + return self.coerce_path_result(rv) + self.fail( + _("{name} {filename!r} does not exist.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + + if not self.file_okay and stat.S_ISREG(st.st_mode): + self.fail( + _("{name} {filename!r} is a file.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + if not self.dir_okay and stat.S_ISDIR(st.st_mode): + self.fail( + _("{name} {filename!r} is a directory.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + + if self.readable and not os.access(rv, os.R_OK): + self.fail( + _("{name} {filename!r} is not readable.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + + if self.writable and not os.access(rv, os.W_OK): + self.fail( + _("{name} {filename!r} is not writable.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + + if self.executable and not os.access(value, os.X_OK): + self.fail( + _("{name} {filename!r} is not executable.").format( + name=self.name.title(), filename=format_filename(value) + ), + param, + ctx, + ) + + return self.coerce_path_result(rv) + + def shell_complete( + self, ctx: Context, param: Parameter, incomplete: str + ) -> list[CompletionItem]: + """Return a special completion marker that tells the completion + system to use the shell to provide path completions for only + directories or any paths. + + :param ctx: Invocation context for this command. + :param param: The parameter that is requesting completion. + :param incomplete: Value being completed. May be empty. + + .. versionadded:: 8.0 + """ + from click.shell_completion import CompletionItem + + type = "dir" if self.dir_okay and not self.file_okay else "file" + return [CompletionItem(incomplete, type=type)] + + +class Tuple(CompositeParamType): + """The default behavior of Click is to apply a type on a value directly. + This works well in most cases, except for when `nargs` is set to a fixed + count and different types should be used for different items. In this + case the :class:`Tuple` type can be used. This type can only be used + if `nargs` is set to a fixed number. + + For more information see :ref:`tuple-type`. + + This can be selected by using a Python tuple literal as a type. + + :param types: a list of types that should be used for the tuple items. + """ + + def __init__(self, types: cabc.Sequence[type[t.Any] | ParamType]) -> None: + self.types: cabc.Sequence[ParamType] = [convert_type(ty) for ty in types] + + def to_info_dict(self) -> dict[str, t.Any]: + info_dict = super().to_info_dict() + info_dict["types"] = [t.to_info_dict() for t in self.types] + return info_dict + + @property + def name(self) -> str: # type: ignore + return f"<{' '.join(ty.name for ty in self.types)}>" + + @property + def arity(self) -> int: # type: ignore + return len(self.types) + + def convert( + self, value: t.Any, param: Parameter | None, ctx: Context | None + ) -> t.Any: + len_type = len(self.types) + len_value = len(value) + + if len_value != len_type: + self.fail( + ngettext( + "{len_type} values are required, but {len_value} was given.", + "{len_type} values are required, but {len_value} were given.", + len_value, + ).format(len_type=len_type, len_value=len_value), + param=param, + ctx=ctx, + ) + + return tuple( + ty(x, param, ctx) for ty, x in zip(self.types, value, strict=False) + ) + + +def convert_type(ty: t.Any | None, default: t.Any | None = None) -> ParamType: + """Find the most appropriate :class:`ParamType` for the given Python + type. If the type isn't provided, it can be inferred from a default + value. + """ + guessed_type = False + + if ty is None and default is not None: + if isinstance(default, (tuple, list)): + # If the default is empty, ty will remain None and will + # return STRING. + if default: + item = default[0] + + # A tuple of tuples needs to detect the inner types. + # Can't call convert recursively because that would + # incorrectly unwind the tuple to a single type. + if isinstance(item, (tuple, list)): + ty = tuple(map(type, item)) + else: + ty = type(item) + else: + ty = type(default) + + guessed_type = True + + if isinstance(ty, tuple): + return Tuple(ty) + + if isinstance(ty, ParamType): + return ty + + if ty is str or ty is None: + return STRING + + if ty is int: + return INT + + if ty is float: + return FLOAT + + if ty is bool: + return BOOL + + if guessed_type: + return STRING + + if __debug__: + try: + if issubclass(ty, ParamType): + raise AssertionError( + f"Attempted to use an uninstantiated parameter type ({ty})." + ) + except TypeError: + # ty is an instance (correct), so issubclass fails. + pass + + return FuncParamType(ty) + + +#: A dummy parameter type that just does nothing. From a user's +#: perspective this appears to just be the same as `STRING` but +#: internally no string conversion takes place if the input was bytes. +#: This is usually useful when working with file paths as they can +#: appear in bytes and unicode. +#: +#: For path related uses the :class:`Path` type is a better choice but +#: there are situations where an unprocessed type is useful which is why +#: it is is provided. +#: +#: .. versionadded:: 4.0 +UNPROCESSED = UnprocessedParamType() + +#: A unicode string parameter type which is the implicit default. This +#: can also be selected by using ``str`` as type. +STRING = StringParamType() + +#: An integer parameter. This can also be selected by using ``int`` as +#: type. +INT = IntParamType() + +#: A floating point value parameter. This can also be selected by using +#: ``float`` as type. +FLOAT = FloatParamType() + +#: A boolean parameter. This is the default for boolean flags. This can +#: also be selected by using ``bool`` as a type. +BOOL = BoolParamType() + +#: A UUID parameter. +UUID = UUIDParameterType() + + +class OptionHelpExtra(t.TypedDict, total=False): + envvars: tuple[str, ...] + default: str + range: str + required: str diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/utils.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/utils.py new file mode 100644 index 0000000000..89cae3b99c --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/click/utils.py @@ -0,0 +1,630 @@ +from __future__ import annotations + +import collections.abc as cabc +import os +import re +import sys +import typing as t +from functools import update_wrapper +from types import ModuleType +from types import TracebackType + +from ._compat import _default_text_stderr +from ._compat import _default_text_stdout +from ._compat import _find_binary_writer +from ._compat import auto_wrap_for_ansi +from ._compat import binary_streams +from ._compat import open_stream +from ._compat import should_strip_ansi +from ._compat import strip_ansi +from ._compat import text_streams +from ._compat import WIN +from .globals import resolve_color_default + +if t.TYPE_CHECKING: + import typing_extensions as te + + P = te.ParamSpec("P") + +R = t.TypeVar("R") + + +def _posixify(name: str) -> str: + return "-".join(name.split()).lower() + + +def safecall(func: t.Callable[P, R]) -> t.Callable[P, R | None]: + """Wraps a function so that it swallows exceptions.""" + + def wrapper(*args: P.args, **kwargs: P.kwargs) -> R | None: + try: + return func(*args, **kwargs) + except Exception: + pass + return None + + return update_wrapper(wrapper, func) + + +def make_str(value: t.Any) -> str: + """Converts a value into a valid string.""" + if isinstance(value, bytes): + try: + return value.decode(sys.getfilesystemencoding()) + except UnicodeError: + return value.decode("utf-8", "replace") + return str(value) + + +def make_default_short_help(help: str, max_length: int = 45) -> str: + """Returns a condensed version of help string. + + :meta private: + """ + # Consider only the first paragraph. + paragraph_end = help.find("\n\n") + + if paragraph_end != -1: + help = help[:paragraph_end] + + # Collapse newlines, tabs, and spaces. + words = help.split() + + if not words: + return "" + + # The first paragraph started with a "no rewrap" marker, ignore it. + if words[0] == "\b": + words = words[1:] + + total_length = 0 + last_index = len(words) - 1 + + for i, word in enumerate(words): + total_length += len(word) + (i > 0) + + if total_length > max_length: # too long, truncate + break + + if word[-1] == ".": # sentence end, truncate without "..." + return " ".join(words[: i + 1]) + + if total_length == max_length and i != last_index: + break # not at sentence end, truncate with "..." + else: + return " ".join(words) # no truncation needed + + # Account for the length of the suffix. + total_length += len("...") + + # remove words until the length is short enough + while i > 0: + total_length -= len(words[i]) + (i > 0) + + if total_length <= max_length: + break + + i -= 1 + + return " ".join(words[:i]) + "..." + + +class LazyFile: + """A lazy file works like a regular file but it does not fully open + the file but it does perform some basic checks early to see if the + filename parameter does make sense. This is useful for safely opening + files for writing. + """ + + def __init__( + self, + filename: str | os.PathLike[str], + mode: str = "r", + encoding: str | None = None, + errors: str | None = "strict", + atomic: bool = False, + ): + self.name: str = os.fspath(filename) + self.mode = mode + self.encoding = encoding + self.errors = errors + self.atomic = atomic + self._f: t.IO[t.Any] | None + self.should_close: bool + + if self.name == "-": + self._f, self.should_close = open_stream(filename, mode, encoding, errors) + else: + if "r" in mode: + # Open and close the file in case we're opening it for + # reading so that we can catch at least some errors in + # some cases early. + open(filename, mode).close() + self._f = None + self.should_close = True + + def __getattr__(self, name: str) -> t.Any: + return getattr(self.open(), name) + + def __repr__(self) -> str: + if self._f is not None: + return repr(self._f) + return f"" + + def open(self) -> t.IO[t.Any]: + """Opens the file if it's not yet open. This call might fail with + a :exc:`FileError`. Not handling this error will produce an error + that Click shows. + """ + if self._f is not None: + return self._f + try: + rv, self.should_close = open_stream( + self.name, self.mode, self.encoding, self.errors, atomic=self.atomic + ) + except OSError as e: + from .exceptions import FileError + + raise FileError(self.name, hint=e.strerror) from e + self._f = rv + return rv + + def close(self) -> None: + """Closes the underlying file, no matter what.""" + if self._f is not None: + self._f.close() + + def close_intelligently(self) -> None: + """This function only closes the file if it was opened by the lazy + file wrapper. For instance this will never close stdin. + """ + if self.should_close: + self.close() + + def __enter__(self) -> LazyFile: + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.close_intelligently() + + def __iter__(self) -> cabc.Iterator[t.AnyStr]: + self.open() + return iter(self._f) # type: ignore + + +class KeepOpenFile: + def __init__(self, file: t.IO[t.Any]) -> None: + self._file: t.IO[t.Any] = file + + def __getattr__(self, name: str) -> t.Any: + return getattr(self._file, name) + + def __enter__(self) -> KeepOpenFile: + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + pass + + def __repr__(self) -> str: + return repr(self._file) + + def __iter__(self) -> cabc.Iterator[t.AnyStr]: + return iter(self._file) + + +def echo( + message: t.Any | None = None, + file: t.IO[t.Any] | None = None, + nl: bool = True, + err: bool = False, + color: bool | None = None, +) -> None: + """Print a message and newline to stdout or a file. This should be + used instead of :func:`print` because it provides better support + for different data, files, and environments. + + Compared to :func:`print`, this does the following: + + - Ensures that the output encoding is not misconfigured on Linux. + - Supports Unicode in the Windows console. + - Supports writing to binary outputs, and supports writing bytes + to text outputs. + - Supports colors and styles on Windows. + - Removes ANSI color and style codes if the output does not look + like an interactive terminal. + - Always flushes the output. + + :param message: The string or bytes to output. Other objects are + converted to strings. + :param file: The file to write to. Defaults to ``stdout``. + :param err: Write to ``stderr`` instead of ``stdout``. + :param nl: Print a newline after the message. Enabled by default. + :param color: Force showing or hiding colors and other styles. By + default Click will remove color if the output does not look like + an interactive terminal. + + .. versionchanged:: 6.0 + Support Unicode output on the Windows console. Click does not + modify ``sys.stdout``, so ``sys.stdout.write()`` and ``print()`` + will still not support Unicode. + + .. versionchanged:: 4.0 + Added the ``color`` parameter. + + .. versionadded:: 3.0 + Added the ``err`` parameter. + + .. versionchanged:: 2.0 + Support colors on Windows if colorama is installed. + """ + if file is None: + if err: + file = _default_text_stderr() + else: + file = _default_text_stdout() + + # There are no standard streams attached to write to. For example, + # pythonw on Windows. + if file is None: + return + + # Convert non bytes/text into the native string type. + if message is not None and not isinstance(message, (str, bytes, bytearray)): + out: str | bytes | bytearray | None = str(message) + else: + out = message + + if nl: + out = out or "" + if isinstance(out, str): + out += "\n" + else: + out += b"\n" + + if not out: + file.flush() + return + + # If there is a message and the value looks like bytes, we manually + # need to find the binary stream and write the message in there. + # This is done separately so that most stream types will work as you + # would expect. Eg: you can write to StringIO for other cases. + if isinstance(out, (bytes, bytearray)): + binary_file = _find_binary_writer(file) + + if binary_file is not None: + file.flush() + binary_file.write(out) + binary_file.flush() + return + + # ANSI style code support. For no message or bytes, nothing happens. + # When outputting to a file instead of a terminal, strip codes. + else: + color = resolve_color_default(color) + + if should_strip_ansi(file, color): + out = strip_ansi(out) + elif WIN: + if auto_wrap_for_ansi is not None: + file = auto_wrap_for_ansi(file, color) # type: ignore + elif not color: + out = strip_ansi(out) + + file.write(out) # type: ignore + file.flush() + + +def get_binary_stream(name: t.Literal["stdin", "stdout", "stderr"]) -> t.BinaryIO: + """Returns a system stream for byte processing. + + :param name: the name of the stream to open. Valid names are ``'stdin'``, + ``'stdout'`` and ``'stderr'`` + """ + opener = binary_streams.get(name) + if opener is None: + raise TypeError(f"Unknown standard stream '{name}'") + return opener() + + +def get_text_stream( + name: t.Literal["stdin", "stdout", "stderr"], + encoding: str | None = None, + errors: str | None = "strict", +) -> t.TextIO: + """Returns a system stream for text processing. This usually returns + a wrapped stream around a binary stream returned from + :func:`get_binary_stream` but it also can take shortcuts for already + correctly configured streams. + + :param name: the name of the stream to open. Valid names are ``'stdin'``, + ``'stdout'`` and ``'stderr'`` + :param encoding: overrides the detected default encoding. + :param errors: overrides the default error mode. + """ + opener = text_streams.get(name) + if opener is None: + raise TypeError(f"Unknown standard stream '{name}'") + return opener(encoding, errors) + + +def open_file( + filename: str | os.PathLike[str], + mode: str = "r", + encoding: str | None = None, + errors: str | None = "strict", + lazy: bool = False, + atomic: bool = False, +) -> t.IO[t.Any]: + """Open a file, with extra behavior to handle ``'-'`` to indicate + a standard stream, lazy open on write, and atomic write. Similar to + the behavior of the :class:`~click.File` param type. + + If ``'-'`` is given to open ``stdout`` or ``stdin``, the stream is + wrapped so that using it in a context manager will not close it. + This makes it possible to use the function without accidentally + closing a standard stream: + + .. code-block:: python + + with open_file(filename) as f: + ... + + :param filename: The name or Path of the file to open, or ``'-'`` for + ``stdin``/``stdout``. + :param mode: The mode in which to open the file. + :param encoding: The encoding to decode or encode a file opened in + text mode. + :param errors: The error handling mode. + :param lazy: Wait to open the file until it is accessed. For read + mode, the file is temporarily opened to raise access errors + early, then closed until it is read again. + :param atomic: Write to a temporary file and replace the given file + on close. + + .. versionadded:: 3.0 + """ + if lazy: + return t.cast( + "t.IO[t.Any]", LazyFile(filename, mode, encoding, errors, atomic=atomic) + ) + + f, should_close = open_stream(filename, mode, encoding, errors, atomic=atomic) + + if not should_close: + f = t.cast("t.IO[t.Any]", KeepOpenFile(f)) + + return f + + +def format_filename( + filename: str | bytes | os.PathLike[str] | os.PathLike[bytes], + shorten: bool = False, +) -> str: + """Format a filename as a string for display. Ensures the filename can be + displayed by replacing any invalid bytes or surrogate escapes in the name + with the replacement character ``�``. + + Invalid bytes or surrogate escapes will raise an error when written to a + stream with ``errors="strict"``. This will typically happen with ``stdout`` + when the locale is something like ``en_GB.UTF-8``. + + Many scenarios *are* safe to write surrogates though, due to PEP 538 and + PEP 540, including: + + - Writing to ``stderr``, which uses ``errors="backslashreplace"``. + - The system has ``LANG=C.UTF-8``, ``C``, or ``POSIX``. Python opens + stdout and stderr with ``errors="surrogateescape"``. + - None of ``LANG/LC_*`` are set. Python assumes ``LANG=C.UTF-8``. + - Python is started in UTF-8 mode with ``PYTHONUTF8=1`` or ``-X utf8``. + Python opens stdout and stderr with ``errors="surrogateescape"``. + + :param filename: formats a filename for UI display. This will also convert + the filename into unicode without failing. + :param shorten: this optionally shortens the filename to strip of the + path that leads up to it. + """ + if shorten: + filename = os.path.basename(filename) + else: + filename = os.fspath(filename) + + if isinstance(filename, bytes): + filename = filename.decode(sys.getfilesystemencoding(), "replace") + else: + filename = filename.encode("utf-8", "surrogateescape").decode( + "utf-8", "replace" + ) + + return filename + + +def get_app_dir(app_name: str, roaming: bool = True, force_posix: bool = False) -> str: + r"""Returns the config folder for the application. The default behavior + is to return whatever is most appropriate for the operating system. + + To give you an idea, for an app called ``"Foo Bar"``, something like + the following folders could be returned: + + Mac OS X: + ``~/Library/Application Support/Foo Bar`` + Mac OS X (POSIX): + ``~/.foo-bar`` + Unix: + ``~/.config/foo-bar`` + Unix (POSIX): + ``~/.foo-bar`` + Windows (roaming): + ``C:\Users\\AppData\Roaming\Foo Bar`` + Windows (not roaming): + ``C:\Users\\AppData\Local\Foo Bar`` + + .. versionadded:: 2.0 + + :param app_name: the application name. This should be properly capitalized + and can contain whitespace. + :param roaming: controls if the folder should be roaming or not on Windows. + Has no effect otherwise. + :param force_posix: if this is set to `True` then on any POSIX system the + folder will be stored in the home folder with a leading + dot instead of the XDG config home or darwin's + application support folder. + """ + if WIN: + key = "APPDATA" if roaming else "LOCALAPPDATA" + folder = os.environ.get(key) + if folder is None: + folder = os.path.expanduser("~") + return os.path.join(folder, app_name) + if force_posix: + return os.path.join(os.path.expanduser(f"~/.{_posixify(app_name)}")) + if sys.platform == "darwin": + return os.path.join( + os.path.expanduser("~/Library/Application Support"), app_name + ) + return os.path.join( + os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), + _posixify(app_name), + ) + + +class PacifyFlushWrapper: + """This wrapper is used to catch and suppress BrokenPipeErrors resulting + from ``.flush()`` being called on broken pipe during the shutdown/final-GC + of the Python interpreter. Notably ``.flush()`` is always called on + ``sys.stdout`` and ``sys.stderr``. So as to have minimal impact on any + other cleanup code, and the case where the underlying file is not a broken + pipe, all calls and attributes are proxied. + """ + + def __init__(self, wrapped: t.IO[t.Any]) -> None: + self.wrapped = wrapped + + def flush(self) -> None: + try: + self.wrapped.flush() + except OSError as e: + import errno + + if e.errno != errno.EPIPE: + raise + + def __getattr__(self, attr: str) -> t.Any: + return getattr(self.wrapped, attr) + + +def _detect_program_name( + path: str | None = None, _main: ModuleType | None = None +) -> str: + """Determine the command used to run the program, for use in help + text. If a file or entry point was executed, the file name is + returned. If ``python -m`` was used to execute a module or package, + ``python -m name`` is returned. + + This doesn't try to be too precise, the goal is to give a concise + name for help text. Files are only shown as their name without the + path. ``python`` is only shown for modules, and the full path to + ``sys.executable`` is not shown. + + :param path: The Python file being executed. Python puts this in + ``sys.argv[0]``, which is used by default. + :param _main: The ``__main__`` module. This should only be passed + during internal testing. + + .. versionadded:: 8.0 + Based on command args detection in the Werkzeug reloader. + + :meta private: + """ + if _main is None: + _main = sys.modules["__main__"] + + if not path: + path = sys.argv[0] + + # The value of __package__ indicates how Python was called. It may + # not exist if a setuptools script is installed as an egg. It may be + # set incorrectly for entry points created with pip on Windows. + # It is set to "" inside a Shiv or PEX zipapp. + if getattr(_main, "__package__", None) in {None, ""} or ( + os.name == "nt" + and _main.__package__ == "" + and not os.path.exists(path) + and os.path.exists(f"{path}.exe") + ): + # Executed a file, like "python app.py". + return os.path.basename(path) + + # Executed a module, like "python -m example". + # Rewritten by Python from "-m script" to "/path/to/script.py". + # Need to look at main module to determine how it was executed. + py_module = t.cast(str, _main.__package__) + name = os.path.splitext(os.path.basename(path))[0] + + # A submodule like "example.cli". + if name != "__main__": + py_module = f"{py_module}.{name}" + + return f"python -m {py_module.lstrip('.')}" + + +def _expand_args( + args: cabc.Iterable[str], + *, + user: bool = True, + env: bool = True, + glob_recursive: bool = True, +) -> list[str]: + """Simulate Unix shell expansion with Python functions. + + See :func:`glob.glob`, :func:`os.path.expanduser`, and + :func:`os.path.expandvars`. + + This is intended for use on Windows, where the shell does not do any + expansion. It may not exactly match what a Unix shell would do. + + :param args: List of command line arguments to expand. + :param user: Expand user home directory. + :param env: Expand environment variables. + :param glob_recursive: ``**`` matches directories recursively. + + .. versionchanged:: 8.1 + Invalid glob patterns are treated as empty expansions rather + than raising an error. + + .. versionadded:: 8.0 + + :meta private: + """ + from glob import glob + + out = [] + + for arg in args: + if user: + arg = os.path.expanduser(arg) + + if env: + arg = os.path.expandvars(arg) + + try: + matches = glob(arg, recursive=glob_recursive) + except re.error: + matches = [] + + if not matches: + out.append(arg) + else: + out.extend(matches) + + return out diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/distutils-precedence.pth b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/distutils-precedence.pth new file mode 100644 index 0000000000..7f009fe9bb --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/distutils-precedence.pth @@ -0,0 +1 @@ +import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'local') == 'local'; enabled and __import__('_distutils_hack').add_shim(); diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/INSTALLER b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/INSTALLER new file mode 100644 index 0000000000..a1b589e38a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/INSTALLER @@ -0,0 +1 @@ +pip diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/METADATA b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/METADATA new file mode 100644 index 0000000000..9d8623c9ae --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/METADATA @@ -0,0 +1,91 @@ +Metadata-Version: 2.4 +Name: Flask +Version: 3.1.3 +Summary: A simple framework for building complex web applications. +Maintainer-email: Pallets +Requires-Python: >=3.9 +Description-Content-Type: text/markdown +License-Expression: BSD-3-Clause +Classifier: Development Status :: 5 - Production/Stable +Classifier: Environment :: Web Environment +Classifier: Framework :: Flask +Classifier: Intended Audience :: Developers +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content +Classifier: Topic :: Internet :: WWW/HTTP :: WSGI +Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application +Classifier: Topic :: Software Development :: Libraries :: Application Frameworks +Classifier: Typing :: Typed +License-File: LICENSE.txt +Requires-Dist: blinker>=1.9.0 +Requires-Dist: click>=8.1.3 +Requires-Dist: importlib-metadata>=3.6.0; python_version < '3.10' +Requires-Dist: itsdangerous>=2.2.0 +Requires-Dist: jinja2>=3.1.2 +Requires-Dist: markupsafe>=2.1.1 +Requires-Dist: werkzeug>=3.1.0 +Requires-Dist: asgiref>=3.2 ; extra == "async" +Requires-Dist: python-dotenv ; extra == "dotenv" +Project-URL: Changes, https://flask.palletsprojects.com/page/changes/ +Project-URL: Chat, https://discord.gg/pallets +Project-URL: Documentation, https://flask.palletsprojects.com/ +Project-URL: Donate, https://palletsprojects.com/donate +Project-URL: Source, https://github.com/pallets/flask/ +Provides-Extra: async +Provides-Extra: dotenv + +
+ +# Flask + +Flask is a lightweight [WSGI] web application framework. It is designed +to make getting started quick and easy, with the ability to scale up to +complex applications. It began as a simple wrapper around [Werkzeug] +and [Jinja], and has become one of the most popular Python web +application frameworks. + +Flask offers suggestions, but doesn't enforce any dependencies or +project layout. It is up to the developer to choose the tools and +libraries they want to use. There are many extensions provided by the +community that make adding new functionality easy. + +[WSGI]: https://wsgi.readthedocs.io/ +[Werkzeug]: https://werkzeug.palletsprojects.com/ +[Jinja]: https://jinja.palletsprojects.com/ + +## A Simple Example + +```python +# save this as app.py +from flask import Flask + +app = Flask(__name__) + +@app.route("/") +def hello(): + return "Hello, World!" +``` + +``` +$ flask run + * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) +``` + +## Donate + +The Pallets organization develops and supports Flask and the libraries +it uses. In order to grow the community of contributors and users, and +allow the maintainers to devote more time to the projects, [please +donate today]. + +[please donate today]: https://palletsprojects.com/donate + +## Contributing + +See our [detailed contributing documentation][contrib] for many ways to +contribute, including reporting issues, requesting features, asking or answering +questions, and making PRs. + +[contrib]: https://palletsprojects.com/contributing/ + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/RECORD b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/RECORD new file mode 100644 index 0000000000..8ad9a50faa --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/RECORD @@ -0,0 +1,58 @@ +../../../bin/flask,sha256=bTgyHqCl6I_XD7MmxNjx3RiM4XRF-0YoXPhBHwiZnq8,282 +flask-3.1.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 +flask-3.1.3.dist-info/METADATA,sha256=qmdg7W9UVwRHTXBzPkpjp_FIHjdpc-3IlqE9AqciTHw,3167 +flask-3.1.3.dist-info/RECORD,, +flask-3.1.3.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 +flask-3.1.3.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82 +flask-3.1.3.dist-info/entry_points.txt,sha256=bBP7hTOS5fz9zLtC7sPofBZAlMkEvBxu7KqS6l5lvc4,40 +flask-3.1.3.dist-info/licenses/LICENSE.txt,sha256=SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o,1475 +flask/__init__.py,sha256=mHvJN9Swtl1RDtjCqCIYyIniK_SZ_l_hqUynOzgpJ9o,2701 +flask/__main__.py,sha256=bYt9eEaoRQWdejEHFD8REx9jxVEdZptECFsV7F49Ink,30 +flask/__pycache__/__init__.cpython-311.pyc,, +flask/__pycache__/__main__.cpython-311.pyc,, +flask/__pycache__/app.cpython-311.pyc,, +flask/__pycache__/blueprints.cpython-311.pyc,, +flask/__pycache__/cli.cpython-311.pyc,, +flask/__pycache__/config.cpython-311.pyc,, +flask/__pycache__/ctx.cpython-311.pyc,, +flask/__pycache__/debughelpers.cpython-311.pyc,, +flask/__pycache__/globals.cpython-311.pyc,, +flask/__pycache__/helpers.cpython-311.pyc,, +flask/__pycache__/logging.cpython-311.pyc,, +flask/__pycache__/sessions.cpython-311.pyc,, +flask/__pycache__/signals.cpython-311.pyc,, +flask/__pycache__/templating.cpython-311.pyc,, +flask/__pycache__/testing.cpython-311.pyc,, +flask/__pycache__/typing.cpython-311.pyc,, +flask/__pycache__/views.cpython-311.pyc,, +flask/__pycache__/wrappers.cpython-311.pyc,, +flask/app.py,sha256=k7tW8LHRSldUi6zKsFKK7Axa_WL4zu1e2wPNthIsu7o,61719 +flask/blueprints.py,sha256=p5QE2lY18GItbdr_RKRpZ8Do17g0PvQGIgZkSUDhX2k,4541 +flask/cli.py,sha256=Pfh72-BxlvoH0QHCDOc1HvXG7Kq5Xetf3zzNz2kNSHk,37184 +flask/config.py,sha256=PiqF0DPam6HW0FH4CH1hpXTBe30NSzjPEOwrz1b6kt0,13219 +flask/ctx.py,sha256=oMe0TRsScW0qdaIqavVsk8P9qiEvAY5VHn1FAgkX8nk,15521 +flask/debughelpers.py,sha256=PGIDhStW_efRjpaa3zHIpo-htStJOR41Ip3OJWPYBwo,6080 +flask/globals.py,sha256=XdQZmStBmPIs8t93tjx6pO7Bm3gobAaONWkFcUHaGas,1713 +flask/helpers.py,sha256=rJZge7_J288J1UQv5-kNf4oEaw332PP8NTW0QRIBbXE,23517 +flask/json/__init__.py,sha256=hLNR898paqoefdeAhraa5wyJy-bmRB2k2dV4EgVy2Z8,5602 +flask/json/__pycache__/__init__.cpython-311.pyc,, +flask/json/__pycache__/provider.cpython-311.pyc,, +flask/json/__pycache__/tag.cpython-311.pyc,, +flask/json/provider.py,sha256=5imEzY5HjV2HoUVrQbJLqXCzMNpZXfD0Y1XqdLV2XBA,7672 +flask/json/tag.py,sha256=DhaNwuIOhdt2R74oOC9Y4Z8ZprxFYiRb5dUP5byyINw,9281 +flask/logging.py,sha256=8sM3WMTubi1cBb2c_lPkWpN0J8dMAqrgKRYLLi1dCVI,2377 +flask/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 +flask/sansio/README.md,sha256=-0X1tECnilmz1cogx-YhNw5d7guK7GKrq_DEV2OzlU0,228 +flask/sansio/__pycache__/app.cpython-311.pyc,, +flask/sansio/__pycache__/blueprints.cpython-311.pyc,, +flask/sansio/__pycache__/scaffold.cpython-311.pyc,, +flask/sansio/app.py,sha256=whGURQDkN0jmhS4CHO7DQ96GGlZS0kETkKkAkoRjl4U,38106 +flask/sansio/blueprints.py,sha256=Tqe-7EkZ-tbWchm8iDoCfD848f0_3nLv6NNjeIPvHwM,24637 +flask/sansio/scaffold.py,sha256=wSASXYdFRWJmqcL0Xq-T7N-PDVUSiFGvjO9kPZg58bk,30371 +flask/sessions.py,sha256=eywRqmytTmYnX_EC78-YBGJoTc5XD_lRphQG5LbN1d0,14969 +flask/signals.py,sha256=V7lMUww7CqgJ2ThUBn1PiatZtQanOyt7OZpu2GZI-34,750 +flask/templating.py,sha256=vbIkwYAxsSEfDxQID1gKRvBQQcGWEuWYCnH0XK3EqOI,7678 +flask/testing.py,sha256=zzC7XxhBWOP9H697IV_4SG7Lg3Lzb5PWiyEP93_KQXE,10117 +flask/typing.py,sha256=L-L5t2jKgS0aOmVhioQ_ylqcgiVFnA6yxO-RLNhq-GU,3293 +flask/views.py,sha256=xzJx6oJqGElThtEghZN7ZQGMw5TDFyuRxUkecwRuAoA,6962 +flask/wrappers.py,sha256=jUkv4mVek2Iq4hwxd4RvqrIMb69Bv0PElDgWLmd5ORo,9406 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/REQUESTED b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/REQUESTED new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/WHEEL b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/WHEEL new file mode 100644 index 0000000000..d8b9936dad --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/WHEEL @@ -0,0 +1,4 @@ +Wheel-Version: 1.0 +Generator: flit 3.12.0 +Root-Is-Purelib: true +Tag: py3-none-any diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/entry_points.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/entry_points.txt new file mode 100644 index 0000000000..eec6733e57 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/entry_points.txt @@ -0,0 +1,3 @@ +[console_scripts] +flask=flask.cli:main + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/licenses/LICENSE.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/licenses/LICENSE.txt new file mode 100644 index 0000000000..9d227a0cc4 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask-3.1.3.dist-info/licenses/LICENSE.txt @@ -0,0 +1,28 @@ +Copyright 2010 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__init__.py new file mode 100644 index 0000000000..1fdc50cea1 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__init__.py @@ -0,0 +1,61 @@ +from __future__ import annotations + +import typing as t + +from . import json as json +from .app import Flask as Flask +from .blueprints import Blueprint as Blueprint +from .config import Config as Config +from .ctx import after_this_request as after_this_request +from .ctx import copy_current_request_context as copy_current_request_context +from .ctx import has_app_context as has_app_context +from .ctx import has_request_context as has_request_context +from .globals import current_app as current_app +from .globals import g as g +from .globals import request as request +from .globals import session as session +from .helpers import abort as abort +from .helpers import flash as flash +from .helpers import get_flashed_messages as get_flashed_messages +from .helpers import get_template_attribute as get_template_attribute +from .helpers import make_response as make_response +from .helpers import redirect as redirect +from .helpers import send_file as send_file +from .helpers import send_from_directory as send_from_directory +from .helpers import stream_with_context as stream_with_context +from .helpers import url_for as url_for +from .json import jsonify as jsonify +from .signals import appcontext_popped as appcontext_popped +from .signals import appcontext_pushed as appcontext_pushed +from .signals import appcontext_tearing_down as appcontext_tearing_down +from .signals import before_render_template as before_render_template +from .signals import got_request_exception as got_request_exception +from .signals import message_flashed as message_flashed +from .signals import request_finished as request_finished +from .signals import request_started as request_started +from .signals import request_tearing_down as request_tearing_down +from .signals import template_rendered as template_rendered +from .templating import render_template as render_template +from .templating import render_template_string as render_template_string +from .templating import stream_template as stream_template +from .templating import stream_template_string as stream_template_string +from .wrappers import Request as Request +from .wrappers import Response as Response + +if not t.TYPE_CHECKING: + + def __getattr__(name: str) -> t.Any: + if name == "__version__": + import importlib.metadata + import warnings + + warnings.warn( + "The '__version__' attribute is deprecated and will be removed in" + " Flask 3.2. Use feature detection or" + " 'importlib.metadata.version(\"flask\")' instead.", + DeprecationWarning, + stacklevel=2, + ) + return importlib.metadata.version("flask") + + raise AttributeError(name) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__main__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__main__.py new file mode 100644 index 0000000000..4e28416e10 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__main__.py @@ -0,0 +1,3 @@ +from .cli import main + +main() diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..13f616c734 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__main__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__main__.cpython-311.pyc new file mode 100644 index 0000000000..9e760bcb77 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/__main__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/app.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000000..8ecfc0a15d Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/app.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/blueprints.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/blueprints.cpython-311.pyc new file mode 100644 index 0000000000..8e4f412d45 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/blueprints.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/cli.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/cli.cpython-311.pyc new file mode 100644 index 0000000000..014450da83 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/cli.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/config.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/config.cpython-311.pyc new file mode 100644 index 0000000000..eb600fd465 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/config.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/ctx.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/ctx.cpython-311.pyc new file mode 100644 index 0000000000..3f286429d0 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/ctx.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/debughelpers.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/debughelpers.cpython-311.pyc new file mode 100644 index 0000000000..ac60d9b5a9 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/debughelpers.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/globals.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/globals.cpython-311.pyc new file mode 100644 index 0000000000..177f18453d Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/globals.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/helpers.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/helpers.cpython-311.pyc new file mode 100644 index 0000000000..9190f0c56e Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/helpers.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/logging.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/logging.cpython-311.pyc new file mode 100644 index 0000000000..3d0c1d06c3 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/logging.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/sessions.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/sessions.cpython-311.pyc new file mode 100644 index 0000000000..57c3b100b6 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/sessions.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/signals.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/signals.cpython-311.pyc new file mode 100644 index 0000000000..44eabd1877 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/signals.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/templating.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/templating.cpython-311.pyc new file mode 100644 index 0000000000..c866f86e06 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/templating.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/testing.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/testing.cpython-311.pyc new file mode 100644 index 0000000000..2671860fe4 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/testing.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/typing.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/typing.cpython-311.pyc new file mode 100644 index 0000000000..886e5c034c Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/typing.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/views.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/views.cpython-311.pyc new file mode 100644 index 0000000000..7f0c0352d3 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/views.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/wrappers.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/wrappers.cpython-311.pyc new file mode 100644 index 0000000000..04bf88f297 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/__pycache__/wrappers.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/app.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/app.py new file mode 100644 index 0000000000..cc326dbe3c --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/app.py @@ -0,0 +1,1536 @@ +from __future__ import annotations + +import collections.abc as cabc +import os +import sys +import typing as t +import weakref +from datetime import timedelta +from inspect import iscoroutinefunction +from itertools import chain +from types import TracebackType +from urllib.parse import quote as _url_quote + +import click +from werkzeug.datastructures import Headers +from werkzeug.datastructures import ImmutableDict +from werkzeug.exceptions import BadRequestKeyError +from werkzeug.exceptions import HTTPException +from werkzeug.exceptions import InternalServerError +from werkzeug.routing import BuildError +from werkzeug.routing import MapAdapter +from werkzeug.routing import RequestRedirect +from werkzeug.routing import RoutingException +from werkzeug.routing import Rule +from werkzeug.serving import is_running_from_reloader +from werkzeug.wrappers import Response as BaseResponse +from werkzeug.wsgi import get_host + +from . import cli +from . import typing as ft +from .ctx import AppContext +from .ctx import RequestContext +from .globals import _cv_app +from .globals import _cv_request +from .globals import current_app +from .globals import g +from .globals import request +from .globals import request_ctx +from .globals import session +from .helpers import get_debug_flag +from .helpers import get_flashed_messages +from .helpers import get_load_dotenv +from .helpers import send_from_directory +from .sansio.app import App +from .sansio.scaffold import _sentinel +from .sessions import SecureCookieSessionInterface +from .sessions import SessionInterface +from .signals import appcontext_tearing_down +from .signals import got_request_exception +from .signals import request_finished +from .signals import request_started +from .signals import request_tearing_down +from .templating import Environment +from .wrappers import Request +from .wrappers import Response + +if t.TYPE_CHECKING: # pragma: no cover + from _typeshed.wsgi import StartResponse + from _typeshed.wsgi import WSGIEnvironment + + from .testing import FlaskClient + from .testing import FlaskCliRunner + from .typing import HeadersValue + +T_shell_context_processor = t.TypeVar( + "T_shell_context_processor", bound=ft.ShellContextProcessorCallable +) +T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable) +T_template_filter = t.TypeVar("T_template_filter", bound=ft.TemplateFilterCallable) +T_template_global = t.TypeVar("T_template_global", bound=ft.TemplateGlobalCallable) +T_template_test = t.TypeVar("T_template_test", bound=ft.TemplateTestCallable) + + +def _make_timedelta(value: timedelta | int | None) -> timedelta | None: + if value is None or isinstance(value, timedelta): + return value + + return timedelta(seconds=value) + + +class Flask(App): + """The flask object implements a WSGI application and acts as the central + object. It is passed the name of the module or package of the + application. Once it is created it will act as a central registry for + the view functions, the URL rules, template configuration and much more. + + The name of the package is used to resolve resources from inside the + package or the folder the module is contained in depending on if the + package parameter resolves to an actual python package (a folder with + an :file:`__init__.py` file inside) or a standard module (just a ``.py`` file). + + For more information about resource loading, see :func:`open_resource`. + + Usually you create a :class:`Flask` instance in your main module or + in the :file:`__init__.py` file of your package like this:: + + from flask import Flask + app = Flask(__name__) + + .. admonition:: About the First Parameter + + The idea of the first parameter is to give Flask an idea of what + belongs to your application. This name is used to find resources + on the filesystem, can be used by extensions to improve debugging + information and a lot more. + + So it's important what you provide there. If you are using a single + module, `__name__` is always the correct value. If you however are + using a package, it's usually recommended to hardcode the name of + your package there. + + For example if your application is defined in :file:`yourapplication/app.py` + you should create it with one of the two versions below:: + + app = Flask('yourapplication') + app = Flask(__name__.split('.')[0]) + + Why is that? The application will work even with `__name__`, thanks + to how resources are looked up. However it will make debugging more + painful. Certain extensions can make assumptions based on the + import name of your application. For example the Flask-SQLAlchemy + extension will look for the code in your application that triggered + an SQL query in debug mode. If the import name is not properly set + up, that debugging information is lost. (For example it would only + pick up SQL queries in `yourapplication.app` and not + `yourapplication.views.frontend`) + + .. versionadded:: 0.7 + The `static_url_path`, `static_folder`, and `template_folder` + parameters were added. + + .. versionadded:: 0.8 + The `instance_path` and `instance_relative_config` parameters were + added. + + .. versionadded:: 0.11 + The `root_path` parameter was added. + + .. versionadded:: 1.0 + The ``host_matching`` and ``static_host`` parameters were added. + + .. versionadded:: 1.0 + The ``subdomain_matching`` parameter was added. Subdomain + matching needs to be enabled manually now. Setting + :data:`SERVER_NAME` does not implicitly enable it. + + :param import_name: the name of the application package + :param static_url_path: can be used to specify a different path for the + static files on the web. Defaults to the name + of the `static_folder` folder. + :param static_folder: The folder with static files that is served at + ``static_url_path``. Relative to the application ``root_path`` + or an absolute path. Defaults to ``'static'``. + :param static_host: the host to use when adding the static route. + Defaults to None. Required when using ``host_matching=True`` + with a ``static_folder`` configured. + :param host_matching: set ``url_map.host_matching`` attribute. + Defaults to False. + :param subdomain_matching: consider the subdomain relative to + :data:`SERVER_NAME` when matching routes. Defaults to False. + :param template_folder: the folder that contains the templates that should + be used by the application. Defaults to + ``'templates'`` folder in the root path of the + application. + :param instance_path: An alternative instance path for the application. + By default the folder ``'instance'`` next to the + package or module is assumed to be the instance + path. + :param instance_relative_config: if set to ``True`` relative filenames + for loading the config are assumed to + be relative to the instance path instead + of the application root. + :param root_path: The path to the root of the application files. + This should only be set manually when it can't be detected + automatically, such as for namespace packages. + """ + + default_config = ImmutableDict( + { + "DEBUG": None, + "TESTING": False, + "PROPAGATE_EXCEPTIONS": None, + "SECRET_KEY": None, + "SECRET_KEY_FALLBACKS": None, + "PERMANENT_SESSION_LIFETIME": timedelta(days=31), + "USE_X_SENDFILE": False, + "TRUSTED_HOSTS": None, + "SERVER_NAME": None, + "APPLICATION_ROOT": "/", + "SESSION_COOKIE_NAME": "session", + "SESSION_COOKIE_DOMAIN": None, + "SESSION_COOKIE_PATH": None, + "SESSION_COOKIE_HTTPONLY": True, + "SESSION_COOKIE_SECURE": False, + "SESSION_COOKIE_PARTITIONED": False, + "SESSION_COOKIE_SAMESITE": None, + "SESSION_REFRESH_EACH_REQUEST": True, + "MAX_CONTENT_LENGTH": None, + "MAX_FORM_MEMORY_SIZE": 500_000, + "MAX_FORM_PARTS": 1_000, + "SEND_FILE_MAX_AGE_DEFAULT": None, + "TRAP_BAD_REQUEST_ERRORS": None, + "TRAP_HTTP_EXCEPTIONS": False, + "EXPLAIN_TEMPLATE_LOADING": False, + "PREFERRED_URL_SCHEME": "http", + "TEMPLATES_AUTO_RELOAD": None, + "MAX_COOKIE_SIZE": 4093, + "PROVIDE_AUTOMATIC_OPTIONS": True, + } + ) + + #: The class that is used for request objects. See :class:`~flask.Request` + #: for more information. + request_class: type[Request] = Request + + #: The class that is used for response objects. See + #: :class:`~flask.Response` for more information. + response_class: type[Response] = Response + + #: the session interface to use. By default an instance of + #: :class:`~flask.sessions.SecureCookieSessionInterface` is used here. + #: + #: .. versionadded:: 0.8 + session_interface: SessionInterface = SecureCookieSessionInterface() + + def __init__( + self, + import_name: str, + static_url_path: str | None = None, + static_folder: str | os.PathLike[str] | None = "static", + static_host: str | None = None, + host_matching: bool = False, + subdomain_matching: bool = False, + template_folder: str | os.PathLike[str] | None = "templates", + instance_path: str | None = None, + instance_relative_config: bool = False, + root_path: str | None = None, + ): + super().__init__( + import_name=import_name, + static_url_path=static_url_path, + static_folder=static_folder, + static_host=static_host, + host_matching=host_matching, + subdomain_matching=subdomain_matching, + template_folder=template_folder, + instance_path=instance_path, + instance_relative_config=instance_relative_config, + root_path=root_path, + ) + + #: The Click command group for registering CLI commands for this + #: object. The commands are available from the ``flask`` command + #: once the application has been discovered and blueprints have + #: been registered. + self.cli = cli.AppGroup() + + # Set the name of the Click group in case someone wants to add + # the app's commands to another CLI tool. + self.cli.name = self.name + + # Add a static route using the provided static_url_path, static_host, + # and static_folder if there is a configured static_folder. + # Note we do this without checking if static_folder exists. + # For one, it might be created while the server is running (e.g. during + # development). Also, Google App Engine stores static files somewhere + if self.has_static_folder: + assert bool(static_host) == host_matching, ( + "Invalid static_host/host_matching combination" + ) + # Use a weakref to avoid creating a reference cycle between the app + # and the view function (see #3761). + self_ref = weakref.ref(self) + self.add_url_rule( + f"{self.static_url_path}/", + endpoint="static", + host=static_host, + view_func=lambda **kw: self_ref().send_static_file(**kw), # type: ignore + ) + + def get_send_file_max_age(self, filename: str | None) -> int | None: + """Used by :func:`send_file` to determine the ``max_age`` cache + value for a given file path if it wasn't passed. + + By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from + the configuration of :data:`~flask.current_app`. This defaults + to ``None``, which tells the browser to use conditional requests + instead of a timed cache, which is usually preferable. + + Note this is a duplicate of the same method in the Flask + class. + + .. versionchanged:: 2.0 + The default configuration is ``None`` instead of 12 hours. + + .. versionadded:: 0.9 + """ + value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"] + + if value is None: + return None + + if isinstance(value, timedelta): + return int(value.total_seconds()) + + return value # type: ignore[no-any-return] + + def send_static_file(self, filename: str) -> Response: + """The view function used to serve files from + :attr:`static_folder`. A route is automatically registered for + this view at :attr:`static_url_path` if :attr:`static_folder` is + set. + + Note this is a duplicate of the same method in the Flask + class. + + .. versionadded:: 0.5 + + """ + if not self.has_static_folder: + raise RuntimeError("'static_folder' must be set to serve static_files.") + + # send_file only knows to call get_send_file_max_age on the app, + # call it here so it works for blueprints too. + max_age = self.get_send_file_max_age(filename) + return send_from_directory( + t.cast(str, self.static_folder), filename, max_age=max_age + ) + + def open_resource( + self, resource: str, mode: str = "rb", encoding: str | None = None + ) -> t.IO[t.AnyStr]: + """Open a resource file relative to :attr:`root_path` for reading. + + For example, if the file ``schema.sql`` is next to the file + ``app.py`` where the ``Flask`` app is defined, it can be opened + with: + + .. code-block:: python + + with app.open_resource("schema.sql") as f: + conn.executescript(f.read()) + + :param resource: Path to the resource relative to :attr:`root_path`. + :param mode: Open the file in this mode. Only reading is supported, + valid values are ``"r"`` (or ``"rt"``) and ``"rb"``. + :param encoding: Open the file with this encoding when opening in text + mode. This is ignored when opening in binary mode. + + .. versionchanged:: 3.1 + Added the ``encoding`` parameter. + """ + if mode not in {"r", "rt", "rb"}: + raise ValueError("Resources can only be opened for reading.") + + path = os.path.join(self.root_path, resource) + + if mode == "rb": + return open(path, mode) # pyright: ignore + + return open(path, mode, encoding=encoding) + + def open_instance_resource( + self, resource: str, mode: str = "rb", encoding: str | None = "utf-8" + ) -> t.IO[t.AnyStr]: + """Open a resource file relative to the application's instance folder + :attr:`instance_path`. Unlike :meth:`open_resource`, files in the + instance folder can be opened for writing. + + :param resource: Path to the resource relative to :attr:`instance_path`. + :param mode: Open the file in this mode. + :param encoding: Open the file with this encoding when opening in text + mode. This is ignored when opening in binary mode. + + .. versionchanged:: 3.1 + Added the ``encoding`` parameter. + """ + path = os.path.join(self.instance_path, resource) + + if "b" in mode: + return open(path, mode) + + return open(path, mode, encoding=encoding) + + def create_jinja_environment(self) -> Environment: + """Create the Jinja environment based on :attr:`jinja_options` + and the various Jinja-related methods of the app. Changing + :attr:`jinja_options` after this will have no effect. Also adds + Flask-related globals and filters to the environment. + + .. versionchanged:: 0.11 + ``Environment.auto_reload`` set in accordance with + ``TEMPLATES_AUTO_RELOAD`` configuration option. + + .. versionadded:: 0.5 + """ + options = dict(self.jinja_options) + + if "autoescape" not in options: + options["autoescape"] = self.select_jinja_autoescape + + if "auto_reload" not in options: + auto_reload = self.config["TEMPLATES_AUTO_RELOAD"] + + if auto_reload is None: + auto_reload = self.debug + + options["auto_reload"] = auto_reload + + rv = self.jinja_environment(self, **options) + rv.globals.update( + url_for=self.url_for, + get_flashed_messages=get_flashed_messages, + config=self.config, + # request, session and g are normally added with the + # context processor for efficiency reasons but for imported + # templates we also want the proxies in there. + request=request, + session=session, + g=g, + ) + rv.policies["json.dumps_function"] = self.json.dumps + return rv + + def create_url_adapter(self, request: Request | None) -> MapAdapter | None: + """Creates a URL adapter for the given request. The URL adapter + is created at a point where the request context is not yet set + up so the request is passed explicitly. + + .. versionchanged:: 3.1 + If :data:`SERVER_NAME` is set, it does not restrict requests to + only that domain, for both ``subdomain_matching`` and + ``host_matching``. + + .. versionchanged:: 1.0 + :data:`SERVER_NAME` no longer implicitly enables subdomain + matching. Use :attr:`subdomain_matching` instead. + + .. versionchanged:: 0.9 + This can be called outside a request when the URL adapter is created + for an application context. + + .. versionadded:: 0.6 + """ + if request is not None: + if (trusted_hosts := self.config["TRUSTED_HOSTS"]) is not None: + request.trusted_hosts = trusted_hosts + + # Check trusted_hosts here until bind_to_environ does. + request.host = get_host(request.environ, request.trusted_hosts) # pyright: ignore + subdomain = None + server_name = self.config["SERVER_NAME"] + + if self.url_map.host_matching: + # Don't pass SERVER_NAME, otherwise it's used and the actual + # host is ignored, which breaks host matching. + server_name = None + elif not self.subdomain_matching: + # Werkzeug doesn't implement subdomain matching yet. Until then, + # disable it by forcing the current subdomain to the default, or + # the empty string. + subdomain = self.url_map.default_subdomain or "" + + return self.url_map.bind_to_environ( + request.environ, server_name=server_name, subdomain=subdomain + ) + + # Need at least SERVER_NAME to match/build outside a request. + if self.config["SERVER_NAME"] is not None: + return self.url_map.bind( + self.config["SERVER_NAME"], + script_name=self.config["APPLICATION_ROOT"], + url_scheme=self.config["PREFERRED_URL_SCHEME"], + ) + + return None + + def raise_routing_exception(self, request: Request) -> t.NoReturn: + """Intercept routing exceptions and possibly do something else. + + In debug mode, intercept a routing redirect and replace it with + an error if the body will be discarded. + + With modern Werkzeug this shouldn't occur, since it now uses a + 308 status which tells the browser to resend the method and + body. + + .. versionchanged:: 2.1 + Don't intercept 307 and 308 redirects. + + :meta private: + :internal: + """ + if ( + not self.debug + or not isinstance(request.routing_exception, RequestRedirect) + or request.routing_exception.code in {307, 308} + or request.method in {"GET", "HEAD", "OPTIONS"} + ): + raise request.routing_exception # type: ignore[misc] + + from .debughelpers import FormDataRoutingRedirect + + raise FormDataRoutingRedirect(request) + + def update_template_context(self, context: dict[str, t.Any]) -> None: + """Update the template context with some commonly used variables. + This injects request, session, config and g into the template + context as well as everything template context processors want + to inject. Note that the as of Flask 0.6, the original values + in the context will not be overridden if a context processor + decides to return a value with the same key. + + :param context: the context as a dictionary that is updated in place + to add extra variables. + """ + names: t.Iterable[str | None] = (None,) + + # A template may be rendered outside a request context. + if request: + names = chain(names, reversed(request.blueprints)) + + # The values passed to render_template take precedence. Keep a + # copy to re-apply after all context functions. + orig_ctx = context.copy() + + for name in names: + if name in self.template_context_processors: + for func in self.template_context_processors[name]: + context.update(self.ensure_sync(func)()) + + context.update(orig_ctx) + + def make_shell_context(self) -> dict[str, t.Any]: + """Returns the shell context for an interactive shell for this + application. This runs all the registered shell context + processors. + + .. versionadded:: 0.11 + """ + rv = {"app": self, "g": g} + for processor in self.shell_context_processors: + rv.update(processor()) + return rv + + def run( + self, + host: str | None = None, + port: int | None = None, + debug: bool | None = None, + load_dotenv: bool = True, + **options: t.Any, + ) -> None: + """Runs the application on a local development server. + + Do not use ``run()`` in a production setting. It is not intended to + meet security and performance requirements for a production server. + Instead, see :doc:`/deploying/index` for WSGI server recommendations. + + If the :attr:`debug` flag is set the server will automatically reload + for code changes and show a debugger in case an exception happened. + + If you want to run the application in debug mode, but disable the + code execution on the interactive debugger, you can pass + ``use_evalex=False`` as parameter. This will keep the debugger's + traceback screen active, but disable code execution. + + It is not recommended to use this function for development with + automatic reloading as this is badly supported. Instead you should + be using the :command:`flask` command line script's ``run`` support. + + .. admonition:: Keep in Mind + + Flask will suppress any server error with a generic error page + unless it is in debug mode. As such to enable just the + interactive debugger without the code reloading, you have to + invoke :meth:`run` with ``debug=True`` and ``use_reloader=False``. + Setting ``use_debugger`` to ``True`` without being in debug mode + won't catch any exceptions because there won't be any to + catch. + + :param host: the hostname to listen on. Set this to ``'0.0.0.0'`` to + have the server available externally as well. Defaults to + ``'127.0.0.1'`` or the host in the ``SERVER_NAME`` config variable + if present. + :param port: the port of the webserver. Defaults to ``5000`` or the + port defined in the ``SERVER_NAME`` config variable if present. + :param debug: if given, enable or disable debug mode. See + :attr:`debug`. + :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv` + files to set environment variables. Will also change the working + directory to the directory containing the first file found. + :param options: the options to be forwarded to the underlying Werkzeug + server. See :func:`werkzeug.serving.run_simple` for more + information. + + .. versionchanged:: 1.0 + If installed, python-dotenv will be used to load environment + variables from :file:`.env` and :file:`.flaskenv` files. + + The :envvar:`FLASK_DEBUG` environment variable will override :attr:`debug`. + + Threaded mode is enabled by default. + + .. versionchanged:: 0.10 + The default port is now picked from the ``SERVER_NAME`` + variable. + """ + # Ignore this call so that it doesn't start another server if + # the 'flask run' command is used. + if os.environ.get("FLASK_RUN_FROM_CLI") == "true": + if not is_running_from_reloader(): + click.secho( + " * Ignoring a call to 'app.run()' that would block" + " the current 'flask' CLI command.\n" + " Only call 'app.run()' in an 'if __name__ ==" + ' "__main__"\' guard.', + fg="red", + ) + + return + + if get_load_dotenv(load_dotenv): + cli.load_dotenv() + + # if set, env var overrides existing value + if "FLASK_DEBUG" in os.environ: + self.debug = get_debug_flag() + + # debug passed to method overrides all other sources + if debug is not None: + self.debug = bool(debug) + + server_name = self.config.get("SERVER_NAME") + sn_host = sn_port = None + + if server_name: + sn_host, _, sn_port = server_name.partition(":") + + if not host: + if sn_host: + host = sn_host + else: + host = "127.0.0.1" + + if port or port == 0: + port = int(port) + elif sn_port: + port = int(sn_port) + else: + port = 5000 + + options.setdefault("use_reloader", self.debug) + options.setdefault("use_debugger", self.debug) + options.setdefault("threaded", True) + + cli.show_server_banner(self.debug, self.name) + + from werkzeug.serving import run_simple + + try: + run_simple(t.cast(str, host), port, self, **options) + finally: + # reset the first request information if the development server + # reset normally. This makes it possible to restart the server + # without reloader and that stuff from an interactive shell. + self._got_first_request = False + + def test_client(self, use_cookies: bool = True, **kwargs: t.Any) -> FlaskClient: + """Creates a test client for this application. For information + about unit testing head over to :doc:`/testing`. + + Note that if you are testing for assertions or exceptions in your + application code, you must set ``app.testing = True`` in order for the + exceptions to propagate to the test client. Otherwise, the exception + will be handled by the application (not visible to the test client) and + the only indication of an AssertionError or other exception will be a + 500 status code response to the test client. See the :attr:`testing` + attribute. For example:: + + app.testing = True + client = app.test_client() + + The test client can be used in a ``with`` block to defer the closing down + of the context until the end of the ``with`` block. This is useful if + you want to access the context locals for testing:: + + with app.test_client() as c: + rv = c.get('/?vodka=42') + assert request.args['vodka'] == '42' + + Additionally, you may pass optional keyword arguments that will then + be passed to the application's :attr:`test_client_class` constructor. + For example:: + + from flask.testing import FlaskClient + + class CustomClient(FlaskClient): + def __init__(self, *args, **kwargs): + self._authentication = kwargs.pop("authentication") + super(CustomClient,self).__init__( *args, **kwargs) + + app.test_client_class = CustomClient + client = app.test_client(authentication='Basic ....') + + See :class:`~flask.testing.FlaskClient` for more information. + + .. versionchanged:: 0.4 + added support for ``with`` block usage for the client. + + .. versionadded:: 0.7 + The `use_cookies` parameter was added as well as the ability + to override the client to be used by setting the + :attr:`test_client_class` attribute. + + .. versionchanged:: 0.11 + Added `**kwargs` to support passing additional keyword arguments to + the constructor of :attr:`test_client_class`. + """ + cls = self.test_client_class + if cls is None: + from .testing import FlaskClient as cls + return cls( # type: ignore + self, self.response_class, use_cookies=use_cookies, **kwargs + ) + + def test_cli_runner(self, **kwargs: t.Any) -> FlaskCliRunner: + """Create a CLI runner for testing CLI commands. + See :ref:`testing-cli`. + + Returns an instance of :attr:`test_cli_runner_class`, by default + :class:`~flask.testing.FlaskCliRunner`. The Flask app object is + passed as the first argument. + + .. versionadded:: 1.0 + """ + cls = self.test_cli_runner_class + + if cls is None: + from .testing import FlaskCliRunner as cls + + return cls(self, **kwargs) # type: ignore + + def handle_http_exception( + self, e: HTTPException + ) -> HTTPException | ft.ResponseReturnValue: + """Handles an HTTP exception. By default this will invoke the + registered error handlers and fall back to returning the + exception as response. + + .. versionchanged:: 1.0.3 + ``RoutingException``, used internally for actions such as + slash redirects during routing, is not passed to error + handlers. + + .. versionchanged:: 1.0 + Exceptions are looked up by code *and* by MRO, so + ``HTTPException`` subclasses can be handled with a catch-all + handler for the base ``HTTPException``. + + .. versionadded:: 0.3 + """ + # Proxy exceptions don't have error codes. We want to always return + # those unchanged as errors + if e.code is None: + return e + + # RoutingExceptions are used internally to trigger routing + # actions, such as slash redirects raising RequestRedirect. They + # are not raised or handled in user code. + if isinstance(e, RoutingException): + return e + + handler = self._find_error_handler(e, request.blueprints) + if handler is None: + return e + return self.ensure_sync(handler)(e) # type: ignore[no-any-return] + + def handle_user_exception( + self, e: Exception + ) -> HTTPException | ft.ResponseReturnValue: + """This method is called whenever an exception occurs that + should be handled. A special case is :class:`~werkzeug + .exceptions.HTTPException` which is forwarded to the + :meth:`handle_http_exception` method. This function will either + return a response value or reraise the exception with the same + traceback. + + .. versionchanged:: 1.0 + Key errors raised from request data like ``form`` show the + bad key in debug mode rather than a generic bad request + message. + + .. versionadded:: 0.7 + """ + if isinstance(e, BadRequestKeyError) and ( + self.debug or self.config["TRAP_BAD_REQUEST_ERRORS"] + ): + e.show_exception = True + + if isinstance(e, HTTPException) and not self.trap_http_exception(e): + return self.handle_http_exception(e) + + handler = self._find_error_handler(e, request.blueprints) + + if handler is None: + raise + + return self.ensure_sync(handler)(e) # type: ignore[no-any-return] + + def handle_exception(self, e: Exception) -> Response: + """Handle an exception that did not have an error handler + associated with it, or that was raised from an error handler. + This always causes a 500 ``InternalServerError``. + + Always sends the :data:`got_request_exception` signal. + + If :data:`PROPAGATE_EXCEPTIONS` is ``True``, such as in debug + mode, the error will be re-raised so that the debugger can + display it. Otherwise, the original exception is logged, and + an :exc:`~werkzeug.exceptions.InternalServerError` is returned. + + If an error handler is registered for ``InternalServerError`` or + ``500``, it will be used. For consistency, the handler will + always receive the ``InternalServerError``. The original + unhandled exception is available as ``e.original_exception``. + + .. versionchanged:: 1.1.0 + Always passes the ``InternalServerError`` instance to the + handler, setting ``original_exception`` to the unhandled + error. + + .. versionchanged:: 1.1.0 + ``after_request`` functions and other finalization is done + even for the default 500 response when there is no handler. + + .. versionadded:: 0.3 + """ + exc_info = sys.exc_info() + got_request_exception.send(self, _async_wrapper=self.ensure_sync, exception=e) + propagate = self.config["PROPAGATE_EXCEPTIONS"] + + if propagate is None: + propagate = self.testing or self.debug + + if propagate: + # Re-raise if called with an active exception, otherwise + # raise the passed in exception. + if exc_info[1] is e: + raise + + raise e + + self.log_exception(exc_info) + server_error: InternalServerError | ft.ResponseReturnValue + server_error = InternalServerError(original_exception=e) + handler = self._find_error_handler(server_error, request.blueprints) + + if handler is not None: + server_error = self.ensure_sync(handler)(server_error) + + return self.finalize_request(server_error, from_error_handler=True) + + def log_exception( + self, + exc_info: (tuple[type, BaseException, TracebackType] | tuple[None, None, None]), + ) -> None: + """Logs an exception. This is called by :meth:`handle_exception` + if debugging is disabled and right before the handler is called. + The default implementation logs the exception as error on the + :attr:`logger`. + + .. versionadded:: 0.8 + """ + self.logger.error( + f"Exception on {request.path} [{request.method}]", exc_info=exc_info + ) + + def dispatch_request(self) -> ft.ResponseReturnValue: + """Does the request dispatching. Matches the URL and returns the + return value of the view or error handler. This does not have to + be a response object. In order to convert the return value to a + proper response object, call :func:`make_response`. + + .. versionchanged:: 0.7 + This no longer does the exception handling, this code was + moved to the new :meth:`full_dispatch_request`. + """ + req = request_ctx.request + if req.routing_exception is not None: + self.raise_routing_exception(req) + rule: Rule = req.url_rule # type: ignore[assignment] + # if we provide automatic options for this URL and the + # request came with the OPTIONS method, reply automatically + if ( + getattr(rule, "provide_automatic_options", False) + and req.method == "OPTIONS" + ): + return self.make_default_options_response() + # otherwise dispatch to the handler for that endpoint + view_args: dict[str, t.Any] = req.view_args # type: ignore[assignment] + return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] + + def full_dispatch_request(self) -> Response: + """Dispatches the request and on top of that performs request + pre and postprocessing as well as HTTP exception catching and + error handling. + + .. versionadded:: 0.7 + """ + self._got_first_request = True + + try: + request_started.send(self, _async_wrapper=self.ensure_sync) + rv = self.preprocess_request() + if rv is None: + rv = self.dispatch_request() + except Exception as e: + rv = self.handle_user_exception(e) + return self.finalize_request(rv) + + def finalize_request( + self, + rv: ft.ResponseReturnValue | HTTPException, + from_error_handler: bool = False, + ) -> Response: + """Given the return value from a view function this finalizes + the request by converting it into a response and invoking the + postprocessing functions. This is invoked for both normal + request dispatching as well as error handlers. + + Because this means that it might be called as a result of a + failure a special safe mode is available which can be enabled + with the `from_error_handler` flag. If enabled, failures in + response processing will be logged and otherwise ignored. + + :internal: + """ + response = self.make_response(rv) + try: + response = self.process_response(response) + request_finished.send( + self, _async_wrapper=self.ensure_sync, response=response + ) + except Exception: + if not from_error_handler: + raise + self.logger.exception( + "Request finalizing failed with an error while handling an error" + ) + return response + + def make_default_options_response(self) -> Response: + """This method is called to create the default ``OPTIONS`` response. + This can be changed through subclassing to change the default + behavior of ``OPTIONS`` responses. + + .. versionadded:: 0.7 + """ + adapter = request_ctx.url_adapter + methods = adapter.allowed_methods() # type: ignore[union-attr] + rv = self.response_class() + rv.allow.update(methods) + return rv + + def ensure_sync(self, func: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]: + """Ensure that the function is synchronous for WSGI workers. + Plain ``def`` functions are returned as-is. ``async def`` + functions are wrapped to run and wait for the response. + + Override this method to change how the app runs async views. + + .. versionadded:: 2.0 + """ + if iscoroutinefunction(func): + return self.async_to_sync(func) + + return func + + def async_to_sync( + self, func: t.Callable[..., t.Coroutine[t.Any, t.Any, t.Any]] + ) -> t.Callable[..., t.Any]: + """Return a sync function that will run the coroutine function. + + .. code-block:: python + + result = app.async_to_sync(func)(*args, **kwargs) + + Override this method to change how the app converts async code + to be synchronously callable. + + .. versionadded:: 2.0 + """ + try: + from asgiref.sync import async_to_sync as asgiref_async_to_sync + except ImportError: + raise RuntimeError( + "Install Flask with the 'async' extra in order to use async views." + ) from None + + return asgiref_async_to_sync(func) + + def url_for( + self, + /, + endpoint: str, + *, + _anchor: str | None = None, + _method: str | None = None, + _scheme: str | None = None, + _external: bool | None = None, + **values: t.Any, + ) -> str: + """Generate a URL to the given endpoint with the given values. + + This is called by :func:`flask.url_for`, and can be called + directly as well. + + An *endpoint* is the name of a URL rule, usually added with + :meth:`@app.route() `, and usually the same name as the + view function. A route defined in a :class:`~flask.Blueprint` + will prepend the blueprint's name separated by a ``.`` to the + endpoint. + + In some cases, such as email messages, you want URLs to include + the scheme and domain, like ``https://example.com/hello``. When + not in an active request, URLs will be external by default, but + this requires setting :data:`SERVER_NAME` so Flask knows what + domain to use. :data:`APPLICATION_ROOT` and + :data:`PREFERRED_URL_SCHEME` should also be configured as + needed. This config is only used when not in an active request. + + Functions can be decorated with :meth:`url_defaults` to modify + keyword arguments before the URL is built. + + If building fails for some reason, such as an unknown endpoint + or incorrect values, the app's :meth:`handle_url_build_error` + method is called. If that returns a string, that is returned, + otherwise a :exc:`~werkzeug.routing.BuildError` is raised. + + :param endpoint: The endpoint name associated with the URL to + generate. If this starts with a ``.``, the current blueprint + name (if any) will be used. + :param _anchor: If given, append this as ``#anchor`` to the URL. + :param _method: If given, generate the URL associated with this + method for the endpoint. + :param _scheme: If given, the URL will have this scheme if it + is external. + :param _external: If given, prefer the URL to be internal + (False) or require it to be external (True). External URLs + include the scheme and domain. When not in an active + request, URLs are external by default. + :param values: Values to use for the variable parts of the URL + rule. Unknown keys are appended as query string arguments, + like ``?a=b&c=d``. + + .. versionadded:: 2.2 + Moved from ``flask.url_for``, which calls this method. + """ + req_ctx = _cv_request.get(None) + + if req_ctx is not None: + url_adapter = req_ctx.url_adapter + blueprint_name = req_ctx.request.blueprint + + # If the endpoint starts with "." and the request matches a + # blueprint, the endpoint is relative to the blueprint. + if endpoint[:1] == ".": + if blueprint_name is not None: + endpoint = f"{blueprint_name}{endpoint}" + else: + endpoint = endpoint[1:] + + # When in a request, generate a URL without scheme and + # domain by default, unless a scheme is given. + if _external is None: + _external = _scheme is not None + else: + app_ctx = _cv_app.get(None) + + # If called by helpers.url_for, an app context is active, + # use its url_adapter. Otherwise, app.url_for was called + # directly, build an adapter. + if app_ctx is not None: + url_adapter = app_ctx.url_adapter + else: + url_adapter = self.create_url_adapter(None) + + if url_adapter is None: + raise RuntimeError( + "Unable to build URLs outside an active request" + " without 'SERVER_NAME' configured. Also configure" + " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as" + " needed." + ) + + # When outside a request, generate a URL with scheme and + # domain by default. + if _external is None: + _external = True + + # It is an error to set _scheme when _external=False, in order + # to avoid accidental insecure URLs. + if _scheme is not None and not _external: + raise ValueError("When specifying '_scheme', '_external' must be True.") + + self.inject_url_defaults(endpoint, values) + + try: + rv = url_adapter.build( # type: ignore[union-attr] + endpoint, + values, + method=_method, + url_scheme=_scheme, + force_external=_external, + ) + except BuildError as error: + values.update( + _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external + ) + return self.handle_url_build_error(error, endpoint, values) + + if _anchor is not None: + _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@") + rv = f"{rv}#{_anchor}" + + return rv + + def make_response(self, rv: ft.ResponseReturnValue) -> Response: + """Convert the return value from a view function to an instance of + :attr:`response_class`. + + :param rv: the return value from the view function. The view function + must return a response. Returning ``None``, or the view ending + without returning, is not allowed. The following types are allowed + for ``view_rv``: + + ``str`` + A response object is created with the string encoded to UTF-8 + as the body. + + ``bytes`` + A response object is created with the bytes as the body. + + ``dict`` + A dictionary that will be jsonify'd before being returned. + + ``list`` + A list that will be jsonify'd before being returned. + + ``generator`` or ``iterator`` + A generator that returns ``str`` or ``bytes`` to be + streamed as the response. + + ``tuple`` + Either ``(body, status, headers)``, ``(body, status)``, or + ``(body, headers)``, where ``body`` is any of the other types + allowed here, ``status`` is a string or an integer, and + ``headers`` is a dictionary or a list of ``(key, value)`` + tuples. If ``body`` is a :attr:`response_class` instance, + ``status`` overwrites the exiting value and ``headers`` are + extended. + + :attr:`response_class` + The object is returned unchanged. + + other :class:`~werkzeug.wrappers.Response` class + The object is coerced to :attr:`response_class`. + + :func:`callable` + The function is called as a WSGI application. The result is + used to create a response object. + + .. versionchanged:: 2.2 + A generator will be converted to a streaming response. + A list will be converted to a JSON response. + + .. versionchanged:: 1.1 + A dict will be converted to a JSON response. + + .. versionchanged:: 0.9 + Previously a tuple was interpreted as the arguments for the + response object. + """ + + status: int | None = None + headers: HeadersValue | None = None + + # unpack tuple returns + if isinstance(rv, tuple): + len_rv = len(rv) + + # a 3-tuple is unpacked directly + if len_rv == 3: + rv, status, headers = rv # type: ignore[misc] + # decide if a 2-tuple has status or headers + elif len_rv == 2: + if isinstance(rv[1], (Headers, dict, tuple, list)): + rv, headers = rv # pyright: ignore + else: + rv, status = rv # type: ignore[assignment,misc] + # other sized tuples are not allowed + else: + raise TypeError( + "The view function did not return a valid response tuple." + " The tuple must have the form (body, status, headers)," + " (body, status), or (body, headers)." + ) + + # the body must not be None + if rv is None: + raise TypeError( + f"The view function for {request.endpoint!r} did not" + " return a valid response. The function either returned" + " None or ended without a return statement." + ) + + # make sure the body is an instance of the response class + if not isinstance(rv, self.response_class): + if isinstance(rv, (str, bytes, bytearray)) or isinstance(rv, cabc.Iterator): + # let the response class set the status and headers instead of + # waiting to do it manually, so that the class can handle any + # special logic + rv = self.response_class( + rv, # pyright: ignore + status=status, + headers=headers, # type: ignore[arg-type] + ) + status = headers = None + elif isinstance(rv, (dict, list)): + rv = self.json.response(rv) + elif isinstance(rv, BaseResponse) or callable(rv): + # evaluate a WSGI callable, or coerce a different response + # class to the correct type + try: + rv = self.response_class.force_type( + rv, # type: ignore[arg-type] + request.environ, + ) + except TypeError as e: + raise TypeError( + f"{e}\nThe view function did not return a valid" + " response. The return type must be a string," + " dict, list, tuple with headers or status," + " Response instance, or WSGI callable, but it" + f" was a {type(rv).__name__}." + ).with_traceback(sys.exc_info()[2]) from None + else: + raise TypeError( + "The view function did not return a valid" + " response. The return type must be a string," + " dict, list, tuple with headers or status," + " Response instance, or WSGI callable, but it was a" + f" {type(rv).__name__}." + ) + + rv = t.cast(Response, rv) + # prefer the status if it was provided + if status is not None: + if isinstance(status, (str, bytes, bytearray)): + rv.status = status + else: + rv.status_code = status + + # extend existing headers with provided headers + if headers: + rv.headers.update(headers) + + return rv + + def preprocess_request(self) -> ft.ResponseReturnValue | None: + """Called before the request is dispatched. Calls + :attr:`url_value_preprocessors` registered with the app and the + current blueprint (if any). Then calls :attr:`before_request_funcs` + registered with the app and the blueprint. + + If any :meth:`before_request` handler returns a non-None value, the + value is handled as if it was the return value from the view, and + further request handling is stopped. + """ + names = (None, *reversed(request.blueprints)) + + for name in names: + if name in self.url_value_preprocessors: + for url_func in self.url_value_preprocessors[name]: + url_func(request.endpoint, request.view_args) + + for name in names: + if name in self.before_request_funcs: + for before_func in self.before_request_funcs[name]: + rv = self.ensure_sync(before_func)() + + if rv is not None: + return rv # type: ignore[no-any-return] + + return None + + def process_response(self, response: Response) -> Response: + """Can be overridden in order to modify the response object + before it's sent to the WSGI server. By default this will + call all the :meth:`after_request` decorated functions. + + .. versionchanged:: 0.5 + As of Flask 0.5 the functions registered for after request + execution are called in reverse order of registration. + + :param response: a :attr:`response_class` object. + :return: a new response object or the same, has to be an + instance of :attr:`response_class`. + """ + ctx = request_ctx._get_current_object() # type: ignore[attr-defined] + + for func in ctx._after_request_functions: + response = self.ensure_sync(func)(response) + + for name in chain(request.blueprints, (None,)): + if name in self.after_request_funcs: + for func in reversed(self.after_request_funcs[name]): + response = self.ensure_sync(func)(response) + + if not self.session_interface.is_null_session(ctx._session): + self.session_interface.save_session(self, ctx._session, response) + + return response + + def do_teardown_request( + self, + exc: BaseException | None = _sentinel, # type: ignore[assignment] + ) -> None: + """Called after the request is dispatched and the response is + returned, right before the request context is popped. + + This calls all functions decorated with + :meth:`teardown_request`, and :meth:`Blueprint.teardown_request` + if a blueprint handled the request. Finally, the + :data:`request_tearing_down` signal is sent. + + This is called by + :meth:`RequestContext.pop() `, + which may be delayed during testing to maintain access to + resources. + + :param exc: An unhandled exception raised while dispatching the + request. Detected from the current exception information if + not passed. Passed to each teardown function. + + .. versionchanged:: 0.9 + Added the ``exc`` argument. + """ + if exc is _sentinel: + exc = sys.exc_info()[1] + + for name in chain(request.blueprints, (None,)): + if name in self.teardown_request_funcs: + for func in reversed(self.teardown_request_funcs[name]): + self.ensure_sync(func)(exc) + + request_tearing_down.send(self, _async_wrapper=self.ensure_sync, exc=exc) + + def do_teardown_appcontext( + self, + exc: BaseException | None = _sentinel, # type: ignore[assignment] + ) -> None: + """Called right before the application context is popped. + + When handling a request, the application context is popped + after the request context. See :meth:`do_teardown_request`. + + This calls all functions decorated with + :meth:`teardown_appcontext`. Then the + :data:`appcontext_tearing_down` signal is sent. + + This is called by + :meth:`AppContext.pop() `. + + .. versionadded:: 0.9 + """ + if exc is _sentinel: + exc = sys.exc_info()[1] + + for func in reversed(self.teardown_appcontext_funcs): + self.ensure_sync(func)(exc) + + appcontext_tearing_down.send(self, _async_wrapper=self.ensure_sync, exc=exc) + + def app_context(self) -> AppContext: + """Create an :class:`~flask.ctx.AppContext`. Use as a ``with`` + block to push the context, which will make :data:`current_app` + point at this application. + + An application context is automatically pushed by + :meth:`RequestContext.push() ` + when handling a request, and when running a CLI command. Use + this to manually create a context outside of these situations. + + :: + + with app.app_context(): + init_db() + + See :doc:`/appcontext`. + + .. versionadded:: 0.9 + """ + return AppContext(self) + + def request_context(self, environ: WSGIEnvironment) -> RequestContext: + """Create a :class:`~flask.ctx.RequestContext` representing a + WSGI environment. Use a ``with`` block to push the context, + which will make :data:`request` point at this request. + + See :doc:`/reqcontext`. + + Typically you should not call this from your own code. A request + context is automatically pushed by the :meth:`wsgi_app` when + handling a request. Use :meth:`test_request_context` to create + an environment and context instead of this method. + + :param environ: a WSGI environment + """ + return RequestContext(self, environ) + + def test_request_context(self, *args: t.Any, **kwargs: t.Any) -> RequestContext: + """Create a :class:`~flask.ctx.RequestContext` for a WSGI + environment created from the given values. This is mostly useful + during testing, where you may want to run a function that uses + request data without dispatching a full request. + + See :doc:`/reqcontext`. + + Use a ``with`` block to push the context, which will make + :data:`request` point at the request for the created + environment. :: + + with app.test_request_context(...): + generate_report() + + When using the shell, it may be easier to push and pop the + context manually to avoid indentation. :: + + ctx = app.test_request_context(...) + ctx.push() + ... + ctx.pop() + + Takes the same arguments as Werkzeug's + :class:`~werkzeug.test.EnvironBuilder`, with some defaults from + the application. See the linked Werkzeug docs for most of the + available arguments. Flask-specific behavior is listed here. + + :param path: URL path being requested. + :param base_url: Base URL where the app is being served, which + ``path`` is relative to. If not given, built from + :data:`PREFERRED_URL_SCHEME`, ``subdomain``, + :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`. + :param subdomain: Subdomain name to append to + :data:`SERVER_NAME`. + :param url_scheme: Scheme to use instead of + :data:`PREFERRED_URL_SCHEME`. + :param data: The request body, either as a string or a dict of + form keys and values. + :param json: If given, this is serialized as JSON and passed as + ``data``. Also defaults ``content_type`` to + ``application/json``. + :param args: other positional arguments passed to + :class:`~werkzeug.test.EnvironBuilder`. + :param kwargs: other keyword arguments passed to + :class:`~werkzeug.test.EnvironBuilder`. + """ + from .testing import EnvironBuilder + + builder = EnvironBuilder(self, *args, **kwargs) + + try: + return self.request_context(builder.get_environ()) + finally: + builder.close() + + def wsgi_app( + self, environ: WSGIEnvironment, start_response: StartResponse + ) -> cabc.Iterable[bytes]: + """The actual WSGI application. This is not implemented in + :meth:`__call__` so that middlewares can be applied without + losing a reference to the app object. Instead of doing this:: + + app = MyMiddleware(app) + + It's a better idea to do this instead:: + + app.wsgi_app = MyMiddleware(app.wsgi_app) + + Then you still have the original application object around and + can continue to call methods on it. + + .. versionchanged:: 0.7 + Teardown events for the request and app contexts are called + even if an unhandled error occurs. Other events may not be + called depending on when an error occurs during dispatch. + See :ref:`callbacks-and-errors`. + + :param environ: A WSGI environment. + :param start_response: A callable accepting a status code, + a list of headers, and an optional exception context to + start the response. + """ + ctx = self.request_context(environ) + error: BaseException | None = None + try: + try: + ctx.push() + response = self.full_dispatch_request() + except Exception as e: + error = e + response = self.handle_exception(e) + except: + error = sys.exc_info()[1] + raise + return response(environ, start_response) + finally: + if "werkzeug.debug.preserve_context" in environ: + environ["werkzeug.debug.preserve_context"](_cv_app.get()) + environ["werkzeug.debug.preserve_context"](_cv_request.get()) + + if error is not None and self.should_ignore_error(error): + error = None + + ctx.pop(error) + + def __call__( + self, environ: WSGIEnvironment, start_response: StartResponse + ) -> cabc.Iterable[bytes]: + """The WSGI server calls the Flask application object as the + WSGI application. This calls :meth:`wsgi_app`, which can be + wrapped to apply middleware. + """ + return self.wsgi_app(environ, start_response) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/blueprints.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/blueprints.py new file mode 100644 index 0000000000..b6d4e43339 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/blueprints.py @@ -0,0 +1,128 @@ +from __future__ import annotations + +import os +import typing as t +from datetime import timedelta + +from .cli import AppGroup +from .globals import current_app +from .helpers import send_from_directory +from .sansio.blueprints import Blueprint as SansioBlueprint +from .sansio.blueprints import BlueprintSetupState as BlueprintSetupState # noqa +from .sansio.scaffold import _sentinel + +if t.TYPE_CHECKING: # pragma: no cover + from .wrappers import Response + + +class Blueprint(SansioBlueprint): + def __init__( + self, + name: str, + import_name: str, + static_folder: str | os.PathLike[str] | None = None, + static_url_path: str | None = None, + template_folder: str | os.PathLike[str] | None = None, + url_prefix: str | None = None, + subdomain: str | None = None, + url_defaults: dict[str, t.Any] | None = None, + root_path: str | None = None, + cli_group: str | None = _sentinel, # type: ignore + ) -> None: + super().__init__( + name, + import_name, + static_folder, + static_url_path, + template_folder, + url_prefix, + subdomain, + url_defaults, + root_path, + cli_group, + ) + + #: The Click command group for registering CLI commands for this + #: object. The commands are available from the ``flask`` command + #: once the application has been discovered and blueprints have + #: been registered. + self.cli = AppGroup() + + # Set the name of the Click group in case someone wants to add + # the app's commands to another CLI tool. + self.cli.name = self.name + + def get_send_file_max_age(self, filename: str | None) -> int | None: + """Used by :func:`send_file` to determine the ``max_age`` cache + value for a given file path if it wasn't passed. + + By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from + the configuration of :data:`~flask.current_app`. This defaults + to ``None``, which tells the browser to use conditional requests + instead of a timed cache, which is usually preferable. + + Note this is a duplicate of the same method in the Flask + class. + + .. versionchanged:: 2.0 + The default configuration is ``None`` instead of 12 hours. + + .. versionadded:: 0.9 + """ + value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"] + + if value is None: + return None + + if isinstance(value, timedelta): + return int(value.total_seconds()) + + return value # type: ignore[no-any-return] + + def send_static_file(self, filename: str) -> Response: + """The view function used to serve files from + :attr:`static_folder`. A route is automatically registered for + this view at :attr:`static_url_path` if :attr:`static_folder` is + set. + + Note this is a duplicate of the same method in the Flask + class. + + .. versionadded:: 0.5 + + """ + if not self.has_static_folder: + raise RuntimeError("'static_folder' must be set to serve static_files.") + + # send_file only knows to call get_send_file_max_age on the app, + # call it here so it works for blueprints too. + max_age = self.get_send_file_max_age(filename) + return send_from_directory( + t.cast(str, self.static_folder), filename, max_age=max_age + ) + + def open_resource( + self, resource: str, mode: str = "rb", encoding: str | None = "utf-8" + ) -> t.IO[t.AnyStr]: + """Open a resource file relative to :attr:`root_path` for reading. The + blueprint-relative equivalent of the app's :meth:`~.Flask.open_resource` + method. + + :param resource: Path to the resource relative to :attr:`root_path`. + :param mode: Open the file in this mode. Only reading is supported, + valid values are ``"r"`` (or ``"rt"``) and ``"rb"``. + :param encoding: Open the file with this encoding when opening in text + mode. This is ignored when opening in binary mode. + + .. versionchanged:: 3.1 + Added the ``encoding`` parameter. + """ + if mode not in {"r", "rt", "rb"}: + raise ValueError("Resources can only be opened for reading.") + + path = os.path.join(self.root_path, resource) + + if mode == "rb": + return open(path, mode) # pyright: ignore + + return open(path, mode, encoding=encoding) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/cli.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/cli.py new file mode 100644 index 0000000000..ed11f256a1 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/cli.py @@ -0,0 +1,1135 @@ +from __future__ import annotations + +import ast +import collections.abc as cabc +import importlib.metadata +import inspect +import os +import platform +import re +import sys +import traceback +import typing as t +from functools import update_wrapper +from operator import itemgetter +from types import ModuleType + +import click +from click.core import ParameterSource +from werkzeug import run_simple +from werkzeug.serving import is_running_from_reloader +from werkzeug.utils import import_string + +from .globals import current_app +from .helpers import get_debug_flag +from .helpers import get_load_dotenv + +if t.TYPE_CHECKING: + import ssl + + from _typeshed.wsgi import StartResponse + from _typeshed.wsgi import WSGIApplication + from _typeshed.wsgi import WSGIEnvironment + + from .app import Flask + + +class NoAppException(click.UsageError): + """Raised if an application cannot be found or loaded.""" + + +def find_best_app(module: ModuleType) -> Flask: + """Given a module instance this tries to find the best possible + application in the module or raises an exception. + """ + from . import Flask + + # Search for the most common names first. + for attr_name in ("app", "application"): + app = getattr(module, attr_name, None) + + if isinstance(app, Flask): + return app + + # Otherwise find the only object that is a Flask instance. + matches = [v for v in module.__dict__.values() if isinstance(v, Flask)] + + if len(matches) == 1: + return matches[0] + elif len(matches) > 1: + raise NoAppException( + "Detected multiple Flask applications in module" + f" '{module.__name__}'. Use '{module.__name__}:name'" + " to specify the correct one." + ) + + # Search for app factory functions. + for attr_name in ("create_app", "make_app"): + app_factory = getattr(module, attr_name, None) + + if inspect.isfunction(app_factory): + try: + app = app_factory() + + if isinstance(app, Flask): + return app + except TypeError as e: + if not _called_with_wrong_args(app_factory): + raise + + raise NoAppException( + f"Detected factory '{attr_name}' in module '{module.__name__}'," + " but could not call it without arguments. Use" + f" '{module.__name__}:{attr_name}(args)'" + " to specify arguments." + ) from e + + raise NoAppException( + "Failed to find Flask application or factory in module" + f" '{module.__name__}'. Use '{module.__name__}:name'" + " to specify one." + ) + + +def _called_with_wrong_args(f: t.Callable[..., Flask]) -> bool: + """Check whether calling a function raised a ``TypeError`` because + the call failed or because something in the factory raised the + error. + + :param f: The function that was called. + :return: ``True`` if the call failed. + """ + tb = sys.exc_info()[2] + + try: + while tb is not None: + if tb.tb_frame.f_code is f.__code__: + # In the function, it was called successfully. + return False + + tb = tb.tb_next + + # Didn't reach the function. + return True + finally: + # Delete tb to break a circular reference. + # https://docs.python.org/2/library/sys.html#sys.exc_info + del tb + + +def find_app_by_string(module: ModuleType, app_name: str) -> Flask: + """Check if the given string is a variable name or a function. Call + a function to get the app instance, or return the variable directly. + """ + from . import Flask + + # Parse app_name as a single expression to determine if it's a valid + # attribute name or function call. + try: + expr = ast.parse(app_name.strip(), mode="eval").body + except SyntaxError: + raise NoAppException( + f"Failed to parse {app_name!r} as an attribute name or function call." + ) from None + + if isinstance(expr, ast.Name): + name = expr.id + args = [] + kwargs = {} + elif isinstance(expr, ast.Call): + # Ensure the function name is an attribute name only. + if not isinstance(expr.func, ast.Name): + raise NoAppException( + f"Function reference must be a simple name: {app_name!r}." + ) + + name = expr.func.id + + # Parse the positional and keyword arguments as literals. + try: + args = [ast.literal_eval(arg) for arg in expr.args] + kwargs = { + kw.arg: ast.literal_eval(kw.value) + for kw in expr.keywords + if kw.arg is not None + } + except ValueError: + # literal_eval gives cryptic error messages, show a generic + # message with the full expression instead. + raise NoAppException( + f"Failed to parse arguments as literal values: {app_name!r}." + ) from None + else: + raise NoAppException( + f"Failed to parse {app_name!r} as an attribute name or function call." + ) + + try: + attr = getattr(module, name) + except AttributeError as e: + raise NoAppException( + f"Failed to find attribute {name!r} in {module.__name__!r}." + ) from e + + # If the attribute is a function, call it with any args and kwargs + # to get the real application. + if inspect.isfunction(attr): + try: + app = attr(*args, **kwargs) + except TypeError as e: + if not _called_with_wrong_args(attr): + raise + + raise NoAppException( + f"The factory {app_name!r} in module" + f" {module.__name__!r} could not be called with the" + " specified arguments." + ) from e + else: + app = attr + + if isinstance(app, Flask): + return app + + raise NoAppException( + "A valid Flask application was not obtained from" + f" '{module.__name__}:{app_name}'." + ) + + +def prepare_import(path: str) -> str: + """Given a filename this will try to calculate the python path, add it + to the search path and return the actual module name that is expected. + """ + path = os.path.realpath(path) + + fname, ext = os.path.splitext(path) + if ext == ".py": + path = fname + + if os.path.basename(path) == "__init__": + path = os.path.dirname(path) + + module_name = [] + + # move up until outside package structure (no __init__.py) + while True: + path, name = os.path.split(path) + module_name.append(name) + + if not os.path.exists(os.path.join(path, "__init__.py")): + break + + if sys.path[0] != path: + sys.path.insert(0, path) + + return ".".join(module_name[::-1]) + + +@t.overload +def locate_app( + module_name: str, app_name: str | None, raise_if_not_found: t.Literal[True] = True +) -> Flask: ... + + +@t.overload +def locate_app( + module_name: str, app_name: str | None, raise_if_not_found: t.Literal[False] = ... +) -> Flask | None: ... + + +def locate_app( + module_name: str, app_name: str | None, raise_if_not_found: bool = True +) -> Flask | None: + try: + __import__(module_name) + except ImportError: + # Reraise the ImportError if it occurred within the imported module. + # Determine this by checking whether the trace has a depth > 1. + if sys.exc_info()[2].tb_next: # type: ignore[union-attr] + raise NoAppException( + f"While importing {module_name!r}, an ImportError was" + f" raised:\n\n{traceback.format_exc()}" + ) from None + elif raise_if_not_found: + raise NoAppException(f"Could not import {module_name!r}.") from None + else: + return None + + module = sys.modules[module_name] + + if app_name is None: + return find_best_app(module) + else: + return find_app_by_string(module, app_name) + + +def get_version(ctx: click.Context, param: click.Parameter, value: t.Any) -> None: + if not value or ctx.resilient_parsing: + return + + flask_version = importlib.metadata.version("flask") + werkzeug_version = importlib.metadata.version("werkzeug") + + click.echo( + f"Python {platform.python_version()}\n" + f"Flask {flask_version}\n" + f"Werkzeug {werkzeug_version}", + color=ctx.color, + ) + ctx.exit() + + +version_option = click.Option( + ["--version"], + help="Show the Flask version.", + expose_value=False, + callback=get_version, + is_flag=True, + is_eager=True, +) + + +class ScriptInfo: + """Helper object to deal with Flask applications. This is usually not + necessary to interface with as it's used internally in the dispatching + to click. In future versions of Flask this object will most likely play + a bigger role. Typically it's created automatically by the + :class:`FlaskGroup` but you can also manually create it and pass it + onwards as click object. + + .. versionchanged:: 3.1 + Added the ``load_dotenv_defaults`` parameter and attribute. + """ + + def __init__( + self, + app_import_path: str | None = None, + create_app: t.Callable[..., Flask] | None = None, + set_debug_flag: bool = True, + load_dotenv_defaults: bool = True, + ) -> None: + #: Optionally the import path for the Flask application. + self.app_import_path = app_import_path + #: Optionally a function that is passed the script info to create + #: the instance of the application. + self.create_app = create_app + #: A dictionary with arbitrary data that can be associated with + #: this script info. + self.data: dict[t.Any, t.Any] = {} + self.set_debug_flag = set_debug_flag + + self.load_dotenv_defaults = get_load_dotenv(load_dotenv_defaults) + """Whether default ``.flaskenv`` and ``.env`` files should be loaded. + + ``ScriptInfo`` doesn't load anything, this is for reference when doing + the load elsewhere during processing. + + .. versionadded:: 3.1 + """ + + self._loaded_app: Flask | None = None + + def load_app(self) -> Flask: + """Loads the Flask app (if not yet loaded) and returns it. Calling + this multiple times will just result in the already loaded app to + be returned. + """ + if self._loaded_app is not None: + return self._loaded_app + app: Flask | None = None + if self.create_app is not None: + app = self.create_app() + else: + if self.app_import_path: + path, name = ( + re.split(r":(?![\\/])", self.app_import_path, maxsplit=1) + [None] + )[:2] + import_name = prepare_import(path) + app = locate_app(import_name, name) + else: + for path in ("wsgi.py", "app.py"): + import_name = prepare_import(path) + app = locate_app(import_name, None, raise_if_not_found=False) + + if app is not None: + break + + if app is None: + raise NoAppException( + "Could not locate a Flask application. Use the" + " 'flask --app' option, 'FLASK_APP' environment" + " variable, or a 'wsgi.py' or 'app.py' file in the" + " current directory." + ) + + if self.set_debug_flag: + # Update the app's debug flag through the descriptor so that + # other values repopulate as well. + app.debug = get_debug_flag() + + self._loaded_app = app + return app + + +pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True) + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + + +def with_appcontext(f: F) -> F: + """Wraps a callback so that it's guaranteed to be executed with the + script's application context. + + Custom commands (and their options) registered under ``app.cli`` or + ``blueprint.cli`` will always have an app context available, this + decorator is not required in that case. + + .. versionchanged:: 2.2 + The app context is active for subcommands as well as the + decorated callback. The app context is always available to + ``app.cli`` command and parameter callbacks. + """ + + @click.pass_context + def decorator(ctx: click.Context, /, *args: t.Any, **kwargs: t.Any) -> t.Any: + if not current_app: + app = ctx.ensure_object(ScriptInfo).load_app() + ctx.with_resource(app.app_context()) + + return ctx.invoke(f, *args, **kwargs) + + return update_wrapper(decorator, f) # type: ignore[return-value] + + +class AppGroup(click.Group): + """This works similar to a regular click :class:`~click.Group` but it + changes the behavior of the :meth:`command` decorator so that it + automatically wraps the functions in :func:`with_appcontext`. + + Not to be confused with :class:`FlaskGroup`. + """ + + def command( # type: ignore[override] + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], click.Command]: + """This works exactly like the method of the same name on a regular + :class:`click.Group` but it wraps callbacks in :func:`with_appcontext` + unless it's disabled by passing ``with_appcontext=False``. + """ + wrap_for_ctx = kwargs.pop("with_appcontext", True) + + def decorator(f: t.Callable[..., t.Any]) -> click.Command: + if wrap_for_ctx: + f = with_appcontext(f) + return super(AppGroup, self).command(*args, **kwargs)(f) # type: ignore[no-any-return] + + return decorator + + def group( # type: ignore[override] + self, *args: t.Any, **kwargs: t.Any + ) -> t.Callable[[t.Callable[..., t.Any]], click.Group]: + """This works exactly like the method of the same name on a regular + :class:`click.Group` but it defaults the group class to + :class:`AppGroup`. + """ + kwargs.setdefault("cls", AppGroup) + return super().group(*args, **kwargs) # type: ignore[no-any-return] + + +def _set_app(ctx: click.Context, param: click.Option, value: str | None) -> str | None: + if value is None: + return None + + info = ctx.ensure_object(ScriptInfo) + info.app_import_path = value + return value + + +# This option is eager so the app will be available if --help is given. +# --help is also eager, so --app must be before it in the param list. +# no_args_is_help bypasses eager processing, so this option must be +# processed manually in that case to ensure FLASK_APP gets picked up. +_app_option = click.Option( + ["-A", "--app"], + metavar="IMPORT", + help=( + "The Flask application or factory function to load, in the form 'module:name'." + " Module can be a dotted import or file path. Name is not required if it is" + " 'app', 'application', 'create_app', or 'make_app', and can be 'name(args)' to" + " pass arguments." + ), + is_eager=True, + expose_value=False, + callback=_set_app, +) + + +def _set_debug(ctx: click.Context, param: click.Option, value: bool) -> bool | None: + # If the flag isn't provided, it will default to False. Don't use + # that, let debug be set by env in that case. + source = ctx.get_parameter_source(param.name) # type: ignore[arg-type] + + if source is not None and source in ( + ParameterSource.DEFAULT, + ParameterSource.DEFAULT_MAP, + ): + return None + + # Set with env var instead of ScriptInfo.load so that it can be + # accessed early during a factory function. + os.environ["FLASK_DEBUG"] = "1" if value else "0" + return value + + +_debug_option = click.Option( + ["--debug/--no-debug"], + help="Set debug mode.", + expose_value=False, + callback=_set_debug, +) + + +def _env_file_callback( + ctx: click.Context, param: click.Option, value: str | None +) -> str | None: + try: + import dotenv # noqa: F401 + except ImportError: + # Only show an error if a value was passed, otherwise we still want to + # call load_dotenv and show a message without exiting. + if value is not None: + raise click.BadParameter( + "python-dotenv must be installed to load an env file.", + ctx=ctx, + param=param, + ) from None + + # Load if a value was passed, or we want to load default files, or both. + if value is not None or ctx.obj.load_dotenv_defaults: + load_dotenv(value, load_defaults=ctx.obj.load_dotenv_defaults) + + return value + + +# This option is eager so env vars are loaded as early as possible to be +# used by other options. +_env_file_option = click.Option( + ["-e", "--env-file"], + type=click.Path(exists=True, dir_okay=False), + help=( + "Load environment variables from this file, taking precedence over" + " those set by '.env' and '.flaskenv'. Variables set directly in the" + " environment take highest precedence. python-dotenv must be installed." + ), + is_eager=True, + expose_value=False, + callback=_env_file_callback, +) + + +class FlaskGroup(AppGroup): + """Special subclass of the :class:`AppGroup` group that supports + loading more commands from the configured Flask app. Normally a + developer does not have to interface with this class but there are + some very advanced use cases for which it makes sense to create an + instance of this. see :ref:`custom-scripts`. + + :param add_default_commands: if this is True then the default run and + shell commands will be added. + :param add_version_option: adds the ``--version`` option. + :param create_app: an optional callback that is passed the script info and + returns the loaded app. + :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv` + files to set environment variables. Will also change the working + directory to the directory containing the first file found. + :param set_debug_flag: Set the app's debug flag. + + .. versionchanged:: 3.1 + ``-e path`` takes precedence over default ``.env`` and ``.flaskenv`` files. + + .. versionchanged:: 2.2 + Added the ``-A/--app``, ``--debug/--no-debug``, ``-e/--env-file`` options. + + .. versionchanged:: 2.2 + An app context is pushed when running ``app.cli`` commands, so + ``@with_appcontext`` is no longer required for those commands. + + .. versionchanged:: 1.0 + If installed, python-dotenv will be used to load environment variables + from :file:`.env` and :file:`.flaskenv` files. + """ + + def __init__( + self, + add_default_commands: bool = True, + create_app: t.Callable[..., Flask] | None = None, + add_version_option: bool = True, + load_dotenv: bool = True, + set_debug_flag: bool = True, + **extra: t.Any, + ) -> None: + params: list[click.Parameter] = list(extra.pop("params", None) or ()) + # Processing is done with option callbacks instead of a group + # callback. This allows users to make a custom group callback + # without losing the behavior. --env-file must come first so + # that it is eagerly evaluated before --app. + params.extend((_env_file_option, _app_option, _debug_option)) + + if add_version_option: + params.append(version_option) + + if "context_settings" not in extra: + extra["context_settings"] = {} + + extra["context_settings"].setdefault("auto_envvar_prefix", "FLASK") + + super().__init__(params=params, **extra) + + self.create_app = create_app + self.load_dotenv = load_dotenv + self.set_debug_flag = set_debug_flag + + if add_default_commands: + self.add_command(run_command) + self.add_command(shell_command) + self.add_command(routes_command) + + self._loaded_plugin_commands = False + + def _load_plugin_commands(self) -> None: + if self._loaded_plugin_commands: + return + + if sys.version_info >= (3, 10): + from importlib import metadata + else: + # Use a backport on Python < 3.10. We technically have + # importlib.metadata on 3.8+, but the API changed in 3.10, + # so use the backport for consistency. + import importlib_metadata as metadata # pyright: ignore + + for ep in metadata.entry_points(group="flask.commands"): + self.add_command(ep.load(), ep.name) + + self._loaded_plugin_commands = True + + def get_command(self, ctx: click.Context, name: str) -> click.Command | None: + self._load_plugin_commands() + # Look up built-in and plugin commands, which should be + # available even if the app fails to load. + rv = super().get_command(ctx, name) + + if rv is not None: + return rv + + info = ctx.ensure_object(ScriptInfo) + + # Look up commands provided by the app, showing an error and + # continuing if the app couldn't be loaded. + try: + app = info.load_app() + except NoAppException as e: + click.secho(f"Error: {e.format_message()}\n", err=True, fg="red") + return None + + # Push an app context for the loaded app unless it is already + # active somehow. This makes the context available to parameter + # and command callbacks without needing @with_appcontext. + if not current_app or current_app._get_current_object() is not app: # type: ignore[attr-defined] + ctx.with_resource(app.app_context()) + + return app.cli.get_command(ctx, name) + + def list_commands(self, ctx: click.Context) -> list[str]: + self._load_plugin_commands() + # Start with the built-in and plugin commands. + rv = set(super().list_commands(ctx)) + info = ctx.ensure_object(ScriptInfo) + + # Add commands provided by the app, showing an error and + # continuing if the app couldn't be loaded. + try: + rv.update(info.load_app().cli.list_commands(ctx)) + except NoAppException as e: + # When an app couldn't be loaded, show the error message + # without the traceback. + click.secho(f"Error: {e.format_message()}\n", err=True, fg="red") + except Exception: + # When any other errors occurred during loading, show the + # full traceback. + click.secho(f"{traceback.format_exc()}\n", err=True, fg="red") + + return sorted(rv) + + def make_context( + self, + info_name: str | None, + args: list[str], + parent: click.Context | None = None, + **extra: t.Any, + ) -> click.Context: + # Set a flag to tell app.run to become a no-op. If app.run was + # not in a __name__ == __main__ guard, it would start the server + # when importing, blocking whatever command is being called. + os.environ["FLASK_RUN_FROM_CLI"] = "true" + + if "obj" not in extra and "obj" not in self.context_settings: + extra["obj"] = ScriptInfo( + create_app=self.create_app, + set_debug_flag=self.set_debug_flag, + load_dotenv_defaults=self.load_dotenv, + ) + + return super().make_context(info_name, args, parent=parent, **extra) + + def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]: + if (not args and self.no_args_is_help) or ( + len(args) == 1 and args[0] in self.get_help_option_names(ctx) + ): + # Attempt to load --env-file and --app early in case they + # were given as env vars. Otherwise no_args_is_help will not + # see commands from app.cli. + _env_file_option.handle_parse_result(ctx, {}, []) + _app_option.handle_parse_result(ctx, {}, []) + + return super().parse_args(ctx, args) + + +def _path_is_ancestor(path: str, other: str) -> bool: + """Take ``other`` and remove the length of ``path`` from it. Then join it + to ``path``. If it is the original value, ``path`` is an ancestor of + ``other``.""" + return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other + + +def load_dotenv( + path: str | os.PathLike[str] | None = None, load_defaults: bool = True +) -> bool: + """Load "dotenv" files to set environment variables. A given path takes + precedence over ``.env``, which takes precedence over ``.flaskenv``. After + loading and combining these files, values are only set if the key is not + already set in ``os.environ``. + + This is a no-op if `python-dotenv`_ is not installed. + + .. _python-dotenv: https://github.com/theskumar/python-dotenv#readme + + :param path: Load the file at this location. + :param load_defaults: Search for and load the default ``.flaskenv`` and + ``.env`` files. + :return: ``True`` if at least one env var was loaded. + + .. versionchanged:: 3.1 + Added the ``load_defaults`` parameter. A given path takes precedence + over default files. + + .. versionchanged:: 2.0 + The current directory is not changed to the location of the + loaded file. + + .. versionchanged:: 2.0 + When loading the env files, set the default encoding to UTF-8. + + .. versionchanged:: 1.1.0 + Returns ``False`` when python-dotenv is not installed, or when + the given path isn't a file. + + .. versionadded:: 1.0 + """ + try: + import dotenv + except ImportError: + if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"): + click.secho( + " * Tip: There are .env files present. Install python-dotenv" + " to use them.", + fg="yellow", + err=True, + ) + + return False + + data: dict[str, str | None] = {} + + if load_defaults: + for default_name in (".flaskenv", ".env"): + if not (default_path := dotenv.find_dotenv(default_name, usecwd=True)): + continue + + data |= dotenv.dotenv_values(default_path, encoding="utf-8") + + if path is not None and os.path.isfile(path): + data |= dotenv.dotenv_values(path, encoding="utf-8") + + for key, value in data.items(): + if key in os.environ or value is None: + continue + + os.environ[key] = value + + return bool(data) # True if at least one env var was loaded. + + +def show_server_banner(debug: bool, app_import_path: str | None) -> None: + """Show extra startup messages the first time the server is run, + ignoring the reloader. + """ + if is_running_from_reloader(): + return + + if app_import_path is not None: + click.echo(f" * Serving Flask app '{app_import_path}'") + + if debug is not None: + click.echo(f" * Debug mode: {'on' if debug else 'off'}") + + +class CertParamType(click.ParamType): + """Click option type for the ``--cert`` option. Allows either an + existing file, the string ``'adhoc'``, or an import for a + :class:`~ssl.SSLContext` object. + """ + + name = "path" + + def __init__(self) -> None: + self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True) + + def convert( + self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None + ) -> t.Any: + try: + import ssl + except ImportError: + raise click.BadParameter( + 'Using "--cert" requires Python to be compiled with SSL support.', + ctx, + param, + ) from None + + try: + return self.path_type(value, param, ctx) + except click.BadParameter: + value = click.STRING(value, param, ctx).lower() + + if value == "adhoc": + try: + import cryptography # noqa: F401 + except ImportError: + raise click.BadParameter( + "Using ad-hoc certificates requires the cryptography library.", + ctx, + param, + ) from None + + return value + + obj = import_string(value, silent=True) + + if isinstance(obj, ssl.SSLContext): + return obj + + raise + + +def _validate_key(ctx: click.Context, param: click.Parameter, value: t.Any) -> t.Any: + """The ``--key`` option must be specified when ``--cert`` is a file. + Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed. + """ + cert = ctx.params.get("cert") + is_adhoc = cert == "adhoc" + + try: + import ssl + except ImportError: + is_context = False + else: + is_context = isinstance(cert, ssl.SSLContext) + + if value is not None: + if is_adhoc: + raise click.BadParameter( + 'When "--cert" is "adhoc", "--key" is not used.', ctx, param + ) + + if is_context: + raise click.BadParameter( + 'When "--cert" is an SSLContext object, "--key" is not used.', + ctx, + param, + ) + + if not cert: + raise click.BadParameter('"--cert" must also be specified.', ctx, param) + + ctx.params["cert"] = cert, value + + else: + if cert and not (is_adhoc or is_context): + raise click.BadParameter('Required when using "--cert".', ctx, param) + + return value + + +class SeparatedPathType(click.Path): + """Click option type that accepts a list of values separated by the + OS's path separator (``:``, ``;`` on Windows). Each value is + validated as a :class:`click.Path` type. + """ + + def convert( + self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None + ) -> t.Any: + items = self.split_envvar_value(value) + # can't call no-arg super() inside list comprehension until Python 3.12 + super_convert = super().convert + return [super_convert(item, param, ctx) for item in items] + + +@click.command("run", short_help="Run a development server.") +@click.option("--host", "-h", default="127.0.0.1", help="The interface to bind to.") +@click.option("--port", "-p", default=5000, help="The port to bind to.") +@click.option( + "--cert", + type=CertParamType(), + help="Specify a certificate file to use HTTPS.", + is_eager=True, +) +@click.option( + "--key", + type=click.Path(exists=True, dir_okay=False, resolve_path=True), + callback=_validate_key, + expose_value=False, + help="The key file to use when specifying a certificate.", +) +@click.option( + "--reload/--no-reload", + default=None, + help="Enable or disable the reloader. By default the reloader " + "is active if debug is enabled.", +) +@click.option( + "--debugger/--no-debugger", + default=None, + help="Enable or disable the debugger. By default the debugger " + "is active if debug is enabled.", +) +@click.option( + "--with-threads/--without-threads", + default=True, + help="Enable or disable multithreading.", +) +@click.option( + "--extra-files", + default=None, + type=SeparatedPathType(), + help=( + "Extra files that trigger a reload on change. Multiple paths" + f" are separated by {os.path.pathsep!r}." + ), +) +@click.option( + "--exclude-patterns", + default=None, + type=SeparatedPathType(), + help=( + "Files matching these fnmatch patterns will not trigger a reload" + " on change. Multiple patterns are separated by" + f" {os.path.pathsep!r}." + ), +) +@pass_script_info +def run_command( + info: ScriptInfo, + host: str, + port: int, + reload: bool, + debugger: bool, + with_threads: bool, + cert: ssl.SSLContext | tuple[str, str | None] | t.Literal["adhoc"] | None, + extra_files: list[str] | None, + exclude_patterns: list[str] | None, +) -> None: + """Run a local development server. + + This server is for development purposes only. It does not provide + the stability, security, or performance of production WSGI servers. + + The reloader and debugger are enabled by default with the '--debug' + option. + """ + try: + app: WSGIApplication = info.load_app() # pyright: ignore + except Exception as e: + if is_running_from_reloader(): + # When reloading, print out the error immediately, but raise + # it later so the debugger or server can handle it. + traceback.print_exc() + err = e + + def app( + environ: WSGIEnvironment, start_response: StartResponse + ) -> cabc.Iterable[bytes]: + raise err from None + + else: + # When not reloading, raise the error immediately so the + # command fails. + raise e from None + + debug = get_debug_flag() + + if reload is None: + reload = debug + + if debugger is None: + debugger = debug + + show_server_banner(debug, info.app_import_path) + + run_simple( + host, + port, + app, + use_reloader=reload, + use_debugger=debugger, + threaded=with_threads, + ssl_context=cert, + extra_files=extra_files, + exclude_patterns=exclude_patterns, + ) + + +run_command.params.insert(0, _debug_option) + + +@click.command("shell", short_help="Run a shell in the app context.") +@with_appcontext +def shell_command() -> None: + """Run an interactive Python shell in the context of a given + Flask application. The application will populate the default + namespace of this shell according to its configuration. + + This is useful for executing small snippets of management code + without having to manually configure the application. + """ + import code + + banner = ( + f"Python {sys.version} on {sys.platform}\n" + f"App: {current_app.import_name}\n" + f"Instance: {current_app.instance_path}" + ) + ctx: dict[str, t.Any] = {} + + # Support the regular Python interpreter startup script if someone + # is using it. + startup = os.environ.get("PYTHONSTARTUP") + if startup and os.path.isfile(startup): + with open(startup) as f: + eval(compile(f.read(), startup, "exec"), ctx) + + ctx.update(current_app.make_shell_context()) + + # Site, customize, or startup script can set a hook to call when + # entering interactive mode. The default one sets up readline with + # tab and history completion. + interactive_hook = getattr(sys, "__interactivehook__", None) + + if interactive_hook is not None: + try: + import readline + from rlcompleter import Completer + except ImportError: + pass + else: + # rlcompleter uses __main__.__dict__ by default, which is + # flask.__main__. Use the shell context instead. + readline.set_completer(Completer(ctx).complete) + + interactive_hook() + + code.interact(banner=banner, local=ctx) + + +@click.command("routes", short_help="Show the routes for the app.") +@click.option( + "--sort", + "-s", + type=click.Choice(("endpoint", "methods", "domain", "rule", "match")), + default="endpoint", + help=( + "Method to sort routes by. 'match' is the order that Flask will match routes" + " when dispatching a request." + ), +) +@click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.") +@with_appcontext +def routes_command(sort: str, all_methods: bool) -> None: + """Show all registered routes with endpoints and methods.""" + rules = list(current_app.url_map.iter_rules()) + + if not rules: + click.echo("No routes were registered.") + return + + ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"} + host_matching = current_app.url_map.host_matching + has_domain = any(rule.host if host_matching else rule.subdomain for rule in rules) + rows = [] + + for rule in rules: + row = [ + rule.endpoint, + ", ".join(sorted((rule.methods or set()) - ignored_methods)), + ] + + if has_domain: + row.append((rule.host if host_matching else rule.subdomain) or "") + + row.append(rule.rule) + rows.append(row) + + headers = ["Endpoint", "Methods"] + sorts = ["endpoint", "methods"] + + if has_domain: + headers.append("Host" if host_matching else "Subdomain") + sorts.append("domain") + + headers.append("Rule") + sorts.append("rule") + + try: + rows.sort(key=itemgetter(sorts.index(sort))) + except ValueError: + pass + + rows.insert(0, headers) + widths = [max(len(row[i]) for row in rows) for i in range(len(headers))] + rows.insert(1, ["-" * w for w in widths]) + template = " ".join(f"{{{i}:<{w}}}" for i, w in enumerate(widths)) + + for row in rows: + click.echo(template.format(*row)) + + +cli = FlaskGroup( + name="flask", + help="""\ +A general utility script for Flask applications. + +An application to load must be given with the '--app' option, +'FLASK_APP' environment variable, or with a 'wsgi.py' or 'app.py' file +in the current directory. +""", +) + + +def main() -> None: + cli.main() + + +if __name__ == "__main__": + main() diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/config.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/config.py new file mode 100644 index 0000000000..34ef1a5721 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/config.py @@ -0,0 +1,367 @@ +from __future__ import annotations + +import errno +import json +import os +import types +import typing as t + +from werkzeug.utils import import_string + +if t.TYPE_CHECKING: + import typing_extensions as te + + from .sansio.app import App + + +T = t.TypeVar("T") + + +class ConfigAttribute(t.Generic[T]): + """Makes an attribute forward to the config""" + + def __init__( + self, name: str, get_converter: t.Callable[[t.Any], T] | None = None + ) -> None: + self.__name__ = name + self.get_converter = get_converter + + @t.overload + def __get__(self, obj: None, owner: None) -> te.Self: ... + + @t.overload + def __get__(self, obj: App, owner: type[App]) -> T: ... + + def __get__(self, obj: App | None, owner: type[App] | None = None) -> T | te.Self: + if obj is None: + return self + + rv = obj.config[self.__name__] + + if self.get_converter is not None: + rv = self.get_converter(rv) + + return rv # type: ignore[no-any-return] + + def __set__(self, obj: App, value: t.Any) -> None: + obj.config[self.__name__] = value + + +class Config(dict): # type: ignore[type-arg] + """Works exactly like a dict but provides ways to fill it from files + or special dictionaries. There are two common patterns to populate the + config. + + Either you can fill the config from a config file:: + + app.config.from_pyfile('yourconfig.cfg') + + Or alternatively you can define the configuration options in the + module that calls :meth:`from_object` or provide an import path to + a module that should be loaded. It is also possible to tell it to + use the same module and with that provide the configuration values + just before the call:: + + DEBUG = True + SECRET_KEY = 'development key' + app.config.from_object(__name__) + + In both cases (loading from any Python file or loading from modules), + only uppercase keys are added to the config. This makes it possible to use + lowercase values in the config file for temporary values that are not added + to the config or to define the config keys in the same file that implements + the application. + + Probably the most interesting way to load configurations is from an + environment variable pointing to a file:: + + app.config.from_envvar('YOURAPPLICATION_SETTINGS') + + In this case before launching the application you have to set this + environment variable to the file you want to use. On Linux and OS X + use the export statement:: + + export YOURAPPLICATION_SETTINGS='/path/to/config/file' + + On windows use `set` instead. + + :param root_path: path to which files are read relative from. When the + config object is created by the application, this is + the application's :attr:`~flask.Flask.root_path`. + :param defaults: an optional dictionary of default values + """ + + def __init__( + self, + root_path: str | os.PathLike[str], + defaults: dict[str, t.Any] | None = None, + ) -> None: + super().__init__(defaults or {}) + self.root_path = root_path + + def from_envvar(self, variable_name: str, silent: bool = False) -> bool: + """Loads a configuration from an environment variable pointing to + a configuration file. This is basically just a shortcut with nicer + error messages for this line of code:: + + app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS']) + + :param variable_name: name of the environment variable + :param silent: set to ``True`` if you want silent failure for missing + files. + :return: ``True`` if the file was loaded successfully. + """ + rv = os.environ.get(variable_name) + if not rv: + if silent: + return False + raise RuntimeError( + f"The environment variable {variable_name!r} is not set" + " and as such configuration could not be loaded. Set" + " this variable and make it point to a configuration" + " file" + ) + return self.from_pyfile(rv, silent=silent) + + def from_prefixed_env( + self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads + ) -> bool: + """Load any environment variables that start with ``FLASK_``, + dropping the prefix from the env key for the config key. Values + are passed through a loading function to attempt to convert them + to more specific types than strings. + + Keys are loaded in :func:`sorted` order. + + The default loading function attempts to parse values as any + valid JSON type, including dicts and lists. + + Specific items in nested dicts can be set by separating the + keys with double underscores (``__``). If an intermediate key + doesn't exist, it will be initialized to an empty dict. + + :param prefix: Load env vars that start with this prefix, + separated with an underscore (``_``). + :param loads: Pass each string value to this function and use + the returned value as the config value. If any error is + raised it is ignored and the value remains a string. The + default is :func:`json.loads`. + + .. versionadded:: 2.1 + """ + prefix = f"{prefix}_" + + for key in sorted(os.environ): + if not key.startswith(prefix): + continue + + value = os.environ[key] + key = key.removeprefix(prefix) + + try: + value = loads(value) + except Exception: + # Keep the value as a string if loading failed. + pass + + if "__" not in key: + # A non-nested key, set directly. + self[key] = value + continue + + # Traverse nested dictionaries with keys separated by "__". + current = self + *parts, tail = key.split("__") + + for part in parts: + # If an intermediate dict does not exist, create it. + if part not in current: + current[part] = {} + + current = current[part] + + current[tail] = value + + return True + + def from_pyfile( + self, filename: str | os.PathLike[str], silent: bool = False + ) -> bool: + """Updates the values in the config from a Python file. This function + behaves as if the file was imported as module with the + :meth:`from_object` function. + + :param filename: the filename of the config. This can either be an + absolute filename or a filename relative to the + root path. + :param silent: set to ``True`` if you want silent failure for missing + files. + :return: ``True`` if the file was loaded successfully. + + .. versionadded:: 0.7 + `silent` parameter. + """ + filename = os.path.join(self.root_path, filename) + d = types.ModuleType("config") + d.__file__ = filename + try: + with open(filename, mode="rb") as config_file: + exec(compile(config_file.read(), filename, "exec"), d.__dict__) + except OSError as e: + if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR): + return False + e.strerror = f"Unable to load configuration file ({e.strerror})" + raise + self.from_object(d) + return True + + def from_object(self, obj: object | str) -> None: + """Updates the values from the given object. An object can be of one + of the following two types: + + - a string: in this case the object with that name will be imported + - an actual object reference: that object is used directly + + Objects are usually either modules or classes. :meth:`from_object` + loads only the uppercase attributes of the module/class. A ``dict`` + object will not work with :meth:`from_object` because the keys of a + ``dict`` are not attributes of the ``dict`` class. + + Example of module-based configuration:: + + app.config.from_object('yourapplication.default_config') + from yourapplication import default_config + app.config.from_object(default_config) + + Nothing is done to the object before loading. If the object is a + class and has ``@property`` attributes, it needs to be + instantiated before being passed to this method. + + You should not use this function to load the actual configuration but + rather configuration defaults. The actual config should be loaded + with :meth:`from_pyfile` and ideally from a location not within the + package because the package might be installed system wide. + + See :ref:`config-dev-prod` for an example of class-based configuration + using :meth:`from_object`. + + :param obj: an import name or object + """ + if isinstance(obj, str): + obj = import_string(obj) + for key in dir(obj): + if key.isupper(): + self[key] = getattr(obj, key) + + def from_file( + self, + filename: str | os.PathLike[str], + load: t.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]], + silent: bool = False, + text: bool = True, + ) -> bool: + """Update the values in the config from a file that is loaded + using the ``load`` parameter. The loaded data is passed to the + :meth:`from_mapping` method. + + .. code-block:: python + + import json + app.config.from_file("config.json", load=json.load) + + import tomllib + app.config.from_file("config.toml", load=tomllib.load, text=False) + + :param filename: The path to the data file. This can be an + absolute path or relative to the config root path. + :param load: A callable that takes a file handle and returns a + mapping of loaded data from the file. + :type load: ``Callable[[Reader], Mapping]`` where ``Reader`` + implements a ``read`` method. + :param silent: Ignore the file if it doesn't exist. + :param text: Open the file in text or binary mode. + :return: ``True`` if the file was loaded successfully. + + .. versionchanged:: 2.3 + The ``text`` parameter was added. + + .. versionadded:: 2.0 + """ + filename = os.path.join(self.root_path, filename) + + try: + with open(filename, "r" if text else "rb") as f: + obj = load(f) + except OSError as e: + if silent and e.errno in (errno.ENOENT, errno.EISDIR): + return False + + e.strerror = f"Unable to load configuration file ({e.strerror})" + raise + + return self.from_mapping(obj) + + def from_mapping( + self, mapping: t.Mapping[str, t.Any] | None = None, **kwargs: t.Any + ) -> bool: + """Updates the config like :meth:`update` ignoring items with + non-upper keys. + + :return: Always returns ``True``. + + .. versionadded:: 0.11 + """ + mappings: dict[str, t.Any] = {} + if mapping is not None: + mappings.update(mapping) + mappings.update(kwargs) + for key, value in mappings.items(): + if key.isupper(): + self[key] = value + return True + + def get_namespace( + self, namespace: str, lowercase: bool = True, trim_namespace: bool = True + ) -> dict[str, t.Any]: + """Returns a dictionary containing a subset of configuration options + that match the specified namespace/prefix. Example usage:: + + app.config['IMAGE_STORE_TYPE'] = 'fs' + app.config['IMAGE_STORE_PATH'] = '/var/app/images' + app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com' + image_store_config = app.config.get_namespace('IMAGE_STORE_') + + The resulting dictionary `image_store_config` would look like:: + + { + 'type': 'fs', + 'path': '/var/app/images', + 'base_url': 'http://img.website.com' + } + + This is often useful when configuration options map directly to + keyword arguments in functions or class constructors. + + :param namespace: a configuration namespace + :param lowercase: a flag indicating if the keys of the resulting + dictionary should be lowercase + :param trim_namespace: a flag indicating if the keys of the resulting + dictionary should not include the namespace + + .. versionadded:: 0.11 + """ + rv = {} + for k, v in self.items(): + if not k.startswith(namespace): + continue + if trim_namespace: + key = k[len(namespace) :] + else: + key = k + if lowercase: + key = key.lower() + rv[key] = v + return rv + + def __repr__(self) -> str: + return f"<{type(self).__name__} {dict.__repr__(self)}>" diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/ctx.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/ctx.py new file mode 100644 index 0000000000..5f7b1f1db0 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/ctx.py @@ -0,0 +1,459 @@ +from __future__ import annotations + +import contextvars +import sys +import typing as t +from functools import update_wrapper +from types import TracebackType + +from werkzeug.exceptions import HTTPException + +from . import typing as ft +from .globals import _cv_app +from .globals import _cv_request +from .signals import appcontext_popped +from .signals import appcontext_pushed + +if t.TYPE_CHECKING: # pragma: no cover + from _typeshed.wsgi import WSGIEnvironment + + from .app import Flask + from .sessions import SessionMixin + from .wrappers import Request + + +# a singleton sentinel value for parameter defaults +_sentinel = object() + + +class _AppCtxGlobals: + """A plain object. Used as a namespace for storing data during an + application context. + + Creating an app context automatically creates this object, which is + made available as the :data:`g` proxy. + + .. describe:: 'key' in g + + Check whether an attribute is present. + + .. versionadded:: 0.10 + + .. describe:: iter(g) + + Return an iterator over the attribute names. + + .. versionadded:: 0.10 + """ + + # Define attr methods to let mypy know this is a namespace object + # that has arbitrary attributes. + + def __getattr__(self, name: str) -> t.Any: + try: + return self.__dict__[name] + except KeyError: + raise AttributeError(name) from None + + def __setattr__(self, name: str, value: t.Any) -> None: + self.__dict__[name] = value + + def __delattr__(self, name: str) -> None: + try: + del self.__dict__[name] + except KeyError: + raise AttributeError(name) from None + + def get(self, name: str, default: t.Any | None = None) -> t.Any: + """Get an attribute by name, or a default value. Like + :meth:`dict.get`. + + :param name: Name of attribute to get. + :param default: Value to return if the attribute is not present. + + .. versionadded:: 0.10 + """ + return self.__dict__.get(name, default) + + def pop(self, name: str, default: t.Any = _sentinel) -> t.Any: + """Get and remove an attribute by name. Like :meth:`dict.pop`. + + :param name: Name of attribute to pop. + :param default: Value to return if the attribute is not present, + instead of raising a ``KeyError``. + + .. versionadded:: 0.11 + """ + if default is _sentinel: + return self.__dict__.pop(name) + else: + return self.__dict__.pop(name, default) + + def setdefault(self, name: str, default: t.Any = None) -> t.Any: + """Get the value of an attribute if it is present, otherwise + set and return a default value. Like :meth:`dict.setdefault`. + + :param name: Name of attribute to get. + :param default: Value to set and return if the attribute is not + present. + + .. versionadded:: 0.11 + """ + return self.__dict__.setdefault(name, default) + + def __contains__(self, item: str) -> bool: + return item in self.__dict__ + + def __iter__(self) -> t.Iterator[str]: + return iter(self.__dict__) + + def __repr__(self) -> str: + ctx = _cv_app.get(None) + if ctx is not None: + return f"" + return object.__repr__(self) + + +def after_this_request( + f: ft.AfterRequestCallable[t.Any], +) -> ft.AfterRequestCallable[t.Any]: + """Executes a function after this request. This is useful to modify + response objects. The function is passed the response object and has + to return the same or a new one. + + Example:: + + @app.route('/') + def index(): + @after_this_request + def add_header(response): + response.headers['X-Foo'] = 'Parachute' + return response + return 'Hello World!' + + This is more useful if a function other than the view function wants to + modify a response. For instance think of a decorator that wants to add + some headers without converting the return value into a response object. + + .. versionadded:: 0.9 + """ + ctx = _cv_request.get(None) + + if ctx is None: + raise RuntimeError( + "'after_this_request' can only be used when a request" + " context is active, such as in a view function." + ) + + ctx._after_request_functions.append(f) + return f + + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + + +def copy_current_request_context(f: F) -> F: + """A helper function that decorates a function to retain the current + request context. This is useful when working with greenlets. The moment + the function is decorated a copy of the request context is created and + then pushed when the function is called. The current session is also + included in the copied request context. + + Example:: + + import gevent + from flask import copy_current_request_context + + @app.route('/') + def index(): + @copy_current_request_context + def do_some_work(): + # do some work here, it can access flask.request or + # flask.session like you would otherwise in the view function. + ... + gevent.spawn(do_some_work) + return 'Regular response' + + .. versionadded:: 0.10 + """ + ctx = _cv_request.get(None) + + if ctx is None: + raise RuntimeError( + "'copy_current_request_context' can only be used when a" + " request context is active, such as in a view function." + ) + + ctx = ctx.copy() + + def wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any: + with ctx: + return ctx.app.ensure_sync(f)(*args, **kwargs) + + return update_wrapper(wrapper, f) # type: ignore[return-value] + + +def has_request_context() -> bool: + """If you have code that wants to test if a request context is there or + not this function can be used. For instance, you may want to take advantage + of request information if the request object is available, but fail + silently if it is unavailable. + + :: + + class User(db.Model): + + def __init__(self, username, remote_addr=None): + self.username = username + if remote_addr is None and has_request_context(): + remote_addr = request.remote_addr + self.remote_addr = remote_addr + + Alternatively you can also just test any of the context bound objects + (such as :class:`request` or :class:`g`) for truthness:: + + class User(db.Model): + + def __init__(self, username, remote_addr=None): + self.username = username + if remote_addr is None and request: + remote_addr = request.remote_addr + self.remote_addr = remote_addr + + .. versionadded:: 0.7 + """ + return _cv_request.get(None) is not None + + +def has_app_context() -> bool: + """Works like :func:`has_request_context` but for the application + context. You can also just do a boolean check on the + :data:`current_app` object instead. + + .. versionadded:: 0.9 + """ + return _cv_app.get(None) is not None + + +class AppContext: + """The app context contains application-specific information. An app + context is created and pushed at the beginning of each request if + one is not already active. An app context is also pushed when + running CLI commands. + """ + + def __init__(self, app: Flask) -> None: + self.app = app + self.url_adapter = app.create_url_adapter(None) + self.g: _AppCtxGlobals = app.app_ctx_globals_class() + self._cv_tokens: list[contextvars.Token[AppContext]] = [] + + def push(self) -> None: + """Binds the app context to the current context.""" + self._cv_tokens.append(_cv_app.set(self)) + appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync) + + def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ignore + """Pops the app context.""" + try: + if len(self._cv_tokens) == 1: + if exc is _sentinel: + exc = sys.exc_info()[1] + self.app.do_teardown_appcontext(exc) + finally: + ctx = _cv_app.get() + _cv_app.reset(self._cv_tokens.pop()) + + if ctx is not self: + raise AssertionError( + f"Popped wrong app context. ({ctx!r} instead of {self!r})" + ) + + appcontext_popped.send(self.app, _async_wrapper=self.app.ensure_sync) + + def __enter__(self) -> AppContext: + self.push() + return self + + def __exit__( + self, + exc_type: type | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.pop(exc_value) + + +class RequestContext: + """The request context contains per-request information. The Flask + app creates and pushes it at the beginning of the request, then pops + it at the end of the request. It will create the URL adapter and + request object for the WSGI environment provided. + + Do not attempt to use this class directly, instead use + :meth:`~flask.Flask.test_request_context` and + :meth:`~flask.Flask.request_context` to create this object. + + When the request context is popped, it will evaluate all the + functions registered on the application for teardown execution + (:meth:`~flask.Flask.teardown_request`). + + The request context is automatically popped at the end of the + request. When using the interactive debugger, the context will be + restored so ``request`` is still accessible. Similarly, the test + client can preserve the context after the request ends. However, + teardown functions may already have closed some resources such as + database connections. + """ + + def __init__( + self, + app: Flask, + environ: WSGIEnvironment, + request: Request | None = None, + session: SessionMixin | None = None, + ) -> None: + self.app = app + if request is None: + request = app.request_class(environ) + request.json_module = app.json + self.request: Request = request + self.url_adapter = None + try: + self.url_adapter = app.create_url_adapter(self.request) + except HTTPException as e: + self.request.routing_exception = e + self.flashes: list[tuple[str, str]] | None = None + self._session: SessionMixin | None = session + # Functions that should be executed after the request on the response + # object. These will be called before the regular "after_request" + # functions. + self._after_request_functions: list[ft.AfterRequestCallable[t.Any]] = [] + + self._cv_tokens: list[ + tuple[contextvars.Token[RequestContext], AppContext | None] + ] = [] + + def copy(self) -> RequestContext: + """Creates a copy of this request context with the same request object. + This can be used to move a request context to a different greenlet. + Because the actual request object is the same this cannot be used to + move a request context to a different thread unless access to the + request object is locked. + + .. versionadded:: 0.10 + + .. versionchanged:: 1.1 + The current session object is used instead of reloading the original + data. This prevents `flask.session` pointing to an out-of-date object. + """ + return self.__class__( + self.app, + environ=self.request.environ, + request=self.request, + session=self._session, + ) + + def match_request(self) -> None: + """Can be overridden by a subclass to hook into the matching + of the request. + """ + try: + result = self.url_adapter.match(return_rule=True) # type: ignore + self.request.url_rule, self.request.view_args = result # type: ignore + except HTTPException as e: + self.request.routing_exception = e + + @property + def session(self) -> SessionMixin: + """The session data associated with this request. Not available until + this context has been pushed. Accessing this property, also accessed by + the :data:`~flask.session` proxy, sets :attr:`.SessionMixin.accessed`. + """ + assert self._session is not None, "The session has not yet been opened." + self._session.accessed = True + return self._session + + def push(self) -> None: + # Before we push the request context we have to ensure that there + # is an application context. + app_ctx = _cv_app.get(None) + + if app_ctx is None or app_ctx.app is not self.app: + app_ctx = self.app.app_context() + app_ctx.push() + else: + app_ctx = None + + self._cv_tokens.append((_cv_request.set(self), app_ctx)) + + # Open the session at the moment that the request context is available. + # This allows a custom open_session method to use the request context. + # Only open a new session if this is the first time the request was + # pushed, otherwise stream_with_context loses the session. + if self._session is None: + session_interface = self.app.session_interface + self._session = session_interface.open_session(self.app, self.request) + + if self._session is None: + self._session = session_interface.make_null_session(self.app) + + # Match the request URL after loading the session, so that the + # session is available in custom URL converters. + if self.url_adapter is not None: + self.match_request() + + def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ignore + """Pops the request context and unbinds it by doing that. This will + also trigger the execution of functions registered by the + :meth:`~flask.Flask.teardown_request` decorator. + + .. versionchanged:: 0.9 + Added the `exc` argument. + """ + clear_request = len(self._cv_tokens) == 1 + + try: + if clear_request: + if exc is _sentinel: + exc = sys.exc_info()[1] + self.app.do_teardown_request(exc) + + request_close = getattr(self.request, "close", None) + if request_close is not None: + request_close() + finally: + ctx = _cv_request.get() + token, app_ctx = self._cv_tokens.pop() + _cv_request.reset(token) + + # get rid of circular dependencies at the end of the request + # so that we don't require the GC to be active. + if clear_request: + ctx.request.environ["werkzeug.request"] = None + + if app_ctx is not None: + app_ctx.pop(exc) + + if ctx is not self: + raise AssertionError( + f"Popped wrong request context. ({ctx!r} instead of {self!r})" + ) + + def __enter__(self) -> RequestContext: + self.push() + return self + + def __exit__( + self, + exc_type: type | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.pop(exc_value) + + def __repr__(self) -> str: + return ( + f"<{type(self).__name__} {self.request.url!r}" + f" [{self.request.method}] of {self.app.name}>" + ) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/debughelpers.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/debughelpers.py new file mode 100644 index 0000000000..2c8c4c4836 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/debughelpers.py @@ -0,0 +1,178 @@ +from __future__ import annotations + +import typing as t + +from jinja2.loaders import BaseLoader +from werkzeug.routing import RequestRedirect + +from .blueprints import Blueprint +from .globals import request_ctx +from .sansio.app import App + +if t.TYPE_CHECKING: + from .sansio.scaffold import Scaffold + from .wrappers import Request + + +class UnexpectedUnicodeError(AssertionError, UnicodeError): + """Raised in places where we want some better error reporting for + unexpected unicode or binary data. + """ + + +class DebugFilesKeyError(KeyError, AssertionError): + """Raised from request.files during debugging. The idea is that it can + provide a better error message than just a generic KeyError/BadRequest. + """ + + def __init__(self, request: Request, key: str) -> None: + form_matches = request.form.getlist(key) + buf = [ + f"You tried to access the file {key!r} in the request.files" + " dictionary but it does not exist. The mimetype for the" + f" request is {request.mimetype!r} instead of" + " 'multipart/form-data' which means that no file contents" + " were transmitted. To fix this error you should provide" + ' enctype="multipart/form-data" in your form.' + ] + if form_matches: + names = ", ".join(repr(x) for x in form_matches) + buf.append( + "\n\nThe browser instead transmitted some file names. " + f"This was submitted: {names}" + ) + self.msg = "".join(buf) + + def __str__(self) -> str: + return self.msg + + +class FormDataRoutingRedirect(AssertionError): + """This exception is raised in debug mode if a routing redirect + would cause the browser to drop the method or body. This happens + when method is not GET, HEAD or OPTIONS and the status code is not + 307 or 308. + """ + + def __init__(self, request: Request) -> None: + exc = request.routing_exception + assert isinstance(exc, RequestRedirect) + buf = [ + f"A request was sent to '{request.url}', but routing issued" + f" a redirect to the canonical URL '{exc.new_url}'." + ] + + if f"{request.base_url}/" == exc.new_url.partition("?")[0]: + buf.append( + " The URL was defined with a trailing slash. Flask" + " will redirect to the URL with a trailing slash if it" + " was accessed without one." + ) + + buf.append( + " Send requests to the canonical URL, or use 307 or 308 for" + " routing redirects. Otherwise, browsers will drop form" + " data.\n\n" + "This exception is only raised in debug mode." + ) + super().__init__("".join(buf)) + + +def attach_enctype_error_multidict(request: Request) -> None: + """Patch ``request.files.__getitem__`` to raise a descriptive error + about ``enctype=multipart/form-data``. + + :param request: The request to patch. + :meta private: + """ + oldcls = request.files.__class__ + + class newcls(oldcls): # type: ignore[valid-type, misc] + def __getitem__(self, key: str) -> t.Any: + try: + return super().__getitem__(key) + except KeyError as e: + if key not in request.form: + raise + + raise DebugFilesKeyError(request, key).with_traceback( + e.__traceback__ + ) from None + + newcls.__name__ = oldcls.__name__ + newcls.__module__ = oldcls.__module__ + request.files.__class__ = newcls + + +def _dump_loader_info(loader: BaseLoader) -> t.Iterator[str]: + yield f"class: {type(loader).__module__}.{type(loader).__name__}" + for key, value in sorted(loader.__dict__.items()): + if key.startswith("_"): + continue + if isinstance(value, (tuple, list)): + if not all(isinstance(x, str) for x in value): + continue + yield f"{key}:" + for item in value: + yield f" - {item}" + continue + elif not isinstance(value, (str, int, float, bool)): + continue + yield f"{key}: {value!r}" + + +def explain_template_loading_attempts( + app: App, + template: str, + attempts: list[ + tuple[ + BaseLoader, + Scaffold, + tuple[str, str | None, t.Callable[[], bool] | None] | None, + ] + ], +) -> None: + """This should help developers understand what failed""" + info = [f"Locating template {template!r}:"] + total_found = 0 + blueprint = None + if request_ctx and request_ctx.request.blueprint is not None: + blueprint = request_ctx.request.blueprint + + for idx, (loader, srcobj, triple) in enumerate(attempts): + if isinstance(srcobj, App): + src_info = f"application {srcobj.import_name!r}" + elif isinstance(srcobj, Blueprint): + src_info = f"blueprint {srcobj.name!r} ({srcobj.import_name})" + else: + src_info = repr(srcobj) + + info.append(f"{idx + 1:5}: trying loader of {src_info}") + + for line in _dump_loader_info(loader): + info.append(f" {line}") + + if triple is None: + detail = "no match" + else: + detail = f"found ({triple[1] or ''!r})" + total_found += 1 + info.append(f" -> {detail}") + + seems_fishy = False + if total_found == 0: + info.append("Error: the template could not be found.") + seems_fishy = True + elif total_found > 1: + info.append("Warning: multiple loaders returned a match for the template.") + seems_fishy = True + + if blueprint is not None and seems_fishy: + info.append( + " The template was looked up from an endpoint that belongs" + f" to the blueprint {blueprint!r}." + ) + info.append(" Maybe you did not place a template in the right folder?") + info.append(" See https://flask.palletsprojects.com/blueprints/#templates") + + app.logger.info("\n".join(info)) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/globals.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/globals.py new file mode 100644 index 0000000000..e2c410cc5b --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/globals.py @@ -0,0 +1,51 @@ +from __future__ import annotations + +import typing as t +from contextvars import ContextVar + +from werkzeug.local import LocalProxy + +if t.TYPE_CHECKING: # pragma: no cover + from .app import Flask + from .ctx import _AppCtxGlobals + from .ctx import AppContext + from .ctx import RequestContext + from .sessions import SessionMixin + from .wrappers import Request + + +_no_app_msg = """\ +Working outside of application context. + +This typically means that you attempted to use functionality that needed +the current application. To solve this, set up an application context +with app.app_context(). See the documentation for more information.\ +""" +_cv_app: ContextVar[AppContext] = ContextVar("flask.app_ctx") +app_ctx: AppContext = LocalProxy( # type: ignore[assignment] + _cv_app, unbound_message=_no_app_msg +) +current_app: Flask = LocalProxy( # type: ignore[assignment] + _cv_app, "app", unbound_message=_no_app_msg +) +g: _AppCtxGlobals = LocalProxy( # type: ignore[assignment] + _cv_app, "g", unbound_message=_no_app_msg +) + +_no_req_msg = """\ +Working outside of request context. + +This typically means that you attempted to use functionality that needed +an active HTTP request. Consult the documentation on testing for +information about how to avoid this problem.\ +""" +_cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx") +request_ctx: RequestContext = LocalProxy( # type: ignore[assignment] + _cv_request, unbound_message=_no_req_msg +) +request: Request = LocalProxy( # type: ignore[assignment] + _cv_request, "request", unbound_message=_no_req_msg +) +session: SessionMixin = LocalProxy( # type: ignore[assignment] + _cv_request, "session", unbound_message=_no_req_msg +) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/helpers.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/helpers.py new file mode 100644 index 0000000000..5d412c90f2 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/helpers.py @@ -0,0 +1,641 @@ +from __future__ import annotations + +import importlib.util +import os +import sys +import typing as t +from datetime import datetime +from functools import cache +from functools import update_wrapper + +import werkzeug.utils +from werkzeug.exceptions import abort as _wz_abort +from werkzeug.utils import redirect as _wz_redirect +from werkzeug.wrappers import Response as BaseResponse + +from .globals import _cv_app +from .globals import _cv_request +from .globals import current_app +from .globals import request +from .globals import request_ctx +from .globals import session +from .signals import message_flashed + +if t.TYPE_CHECKING: # pragma: no cover + from .wrappers import Response + + +def get_debug_flag() -> bool: + """Get whether debug mode should be enabled for the app, indicated by the + :envvar:`FLASK_DEBUG` environment variable. The default is ``False``. + """ + val = os.environ.get("FLASK_DEBUG") + return bool(val and val.lower() not in {"0", "false", "no"}) + + +def get_load_dotenv(default: bool = True) -> bool: + """Get whether the user has disabled loading default dotenv files by + setting :envvar:`FLASK_SKIP_DOTENV`. The default is ``True``, load + the files. + + :param default: What to return if the env var isn't set. + """ + val = os.environ.get("FLASK_SKIP_DOTENV") + + if not val: + return default + + return val.lower() in ("0", "false", "no") + + +@t.overload +def stream_with_context( + generator_or_function: t.Iterator[t.AnyStr], +) -> t.Iterator[t.AnyStr]: ... + + +@t.overload +def stream_with_context( + generator_or_function: t.Callable[..., t.Iterator[t.AnyStr]], +) -> t.Callable[[t.Iterator[t.AnyStr]], t.Iterator[t.AnyStr]]: ... + + +def stream_with_context( + generator_or_function: t.Iterator[t.AnyStr] | t.Callable[..., t.Iterator[t.AnyStr]], +) -> t.Iterator[t.AnyStr] | t.Callable[[t.Iterator[t.AnyStr]], t.Iterator[t.AnyStr]]: + """Wrap a response generator function so that it runs inside the current + request context. This keeps :data:`request`, :data:`session`, and :data:`g` + available, even though at the point the generator runs the request context + will typically have ended. + + Use it as a decorator on a generator function: + + .. code-block:: python + + from flask import stream_with_context, request, Response + + @app.get("/stream") + def streamed_response(): + @stream_with_context + def generate(): + yield "Hello " + yield request.args["name"] + yield "!" + + return Response(generate()) + + Or use it as a wrapper around a created generator: + + .. code-block:: python + + from flask import stream_with_context, request, Response + + @app.get("/stream") + def streamed_response(): + def generate(): + yield "Hello " + yield request.args["name"] + yield "!" + + return Response(stream_with_context(generate())) + + .. versionadded:: 0.9 + """ + try: + gen = iter(generator_or_function) # type: ignore[arg-type] + except TypeError: + + def decorator(*args: t.Any, **kwargs: t.Any) -> t.Any: + gen = generator_or_function(*args, **kwargs) # type: ignore[operator] + return stream_with_context(gen) + + return update_wrapper(decorator, generator_or_function) # type: ignore[arg-type] + + def generator() -> t.Iterator[t.AnyStr]: + if (req_ctx := _cv_request.get(None)) is None: + raise RuntimeError( + "'stream_with_context' can only be used when a request" + " context is active, such as in a view function." + ) + + app_ctx = _cv_app.get() + # Setup code below will run the generator to this point, so that the + # current contexts are recorded. The contexts must be pushed after, + # otherwise their ContextVar will record the wrong event loop during + # async view functions. + yield None # type: ignore[misc] + + # Push the app context first, so that the request context does not + # automatically create and push a different app context. + with app_ctx, req_ctx: + try: + yield from gen + finally: + # Clean up in case the user wrapped a WSGI iterator. + if hasattr(gen, "close"): + gen.close() + + # Execute the generator to the sentinel value. This ensures the context is + # preserved in the generator's state. Further iteration will push the + # context and yield from the original iterator. + wrapped_g = generator() + next(wrapped_g) + return wrapped_g + + +def make_response(*args: t.Any) -> Response: + """Sometimes it is necessary to set additional headers in a view. Because + views do not have to return response objects but can return a value that + is converted into a response object by Flask itself, it becomes tricky to + add headers to it. This function can be called instead of using a return + and you will get a response object which you can use to attach headers. + + If view looked like this and you want to add a new header:: + + def index(): + return render_template('index.html', foo=42) + + You can now do something like this:: + + def index(): + response = make_response(render_template('index.html', foo=42)) + response.headers['X-Parachutes'] = 'parachutes are cool' + return response + + This function accepts the very same arguments you can return from a + view function. This for example creates a response with a 404 error + code:: + + response = make_response(render_template('not_found.html'), 404) + + The other use case of this function is to force the return value of a + view function into a response which is helpful with view + decorators:: + + response = make_response(view_function()) + response.headers['X-Parachutes'] = 'parachutes are cool' + + Internally this function does the following things: + + - if no arguments are passed, it creates a new response argument + - if one argument is passed, :meth:`flask.Flask.make_response` + is invoked with it. + - if more than one argument is passed, the arguments are passed + to the :meth:`flask.Flask.make_response` function as tuple. + + .. versionadded:: 0.6 + """ + if not args: + return current_app.response_class() + if len(args) == 1: + args = args[0] + return current_app.make_response(args) + + +def url_for( + endpoint: str, + *, + _anchor: str | None = None, + _method: str | None = None, + _scheme: str | None = None, + _external: bool | None = None, + **values: t.Any, +) -> str: + """Generate a URL to the given endpoint with the given values. + + This requires an active request or application context, and calls + :meth:`current_app.url_for() `. See that method + for full documentation. + + :param endpoint: The endpoint name associated with the URL to + generate. If this starts with a ``.``, the current blueprint + name (if any) will be used. + :param _anchor: If given, append this as ``#anchor`` to the URL. + :param _method: If given, generate the URL associated with this + method for the endpoint. + :param _scheme: If given, the URL will have this scheme if it is + external. + :param _external: If given, prefer the URL to be internal (False) or + require it to be external (True). External URLs include the + scheme and domain. When not in an active request, URLs are + external by default. + :param values: Values to use for the variable parts of the URL rule. + Unknown keys are appended as query string arguments, like + ``?a=b&c=d``. + + .. versionchanged:: 2.2 + Calls ``current_app.url_for``, allowing an app to override the + behavior. + + .. versionchanged:: 0.10 + The ``_scheme`` parameter was added. + + .. versionchanged:: 0.9 + The ``_anchor`` and ``_method`` parameters were added. + + .. versionchanged:: 0.9 + Calls ``app.handle_url_build_error`` on build errors. + """ + return current_app.url_for( + endpoint, + _anchor=_anchor, + _method=_method, + _scheme=_scheme, + _external=_external, + **values, + ) + + +def redirect( + location: str, code: int = 302, Response: type[BaseResponse] | None = None +) -> BaseResponse: + """Create a redirect response object. + + If :data:`~flask.current_app` is available, it will use its + :meth:`~flask.Flask.redirect` method, otherwise it will use + :func:`werkzeug.utils.redirect`. + + :param location: The URL to redirect to. + :param code: The status code for the redirect. + :param Response: The response class to use. Not used when + ``current_app`` is active, which uses ``app.response_class``. + + .. versionadded:: 2.2 + Calls ``current_app.redirect`` if available instead of always + using Werkzeug's default ``redirect``. + """ + if current_app: + return current_app.redirect(location, code=code) + + return _wz_redirect(location, code=code, Response=Response) + + +def abort(code: int | BaseResponse, *args: t.Any, **kwargs: t.Any) -> t.NoReturn: + """Raise an :exc:`~werkzeug.exceptions.HTTPException` for the given + status code. + + If :data:`~flask.current_app` is available, it will call its + :attr:`~flask.Flask.aborter` object, otherwise it will use + :func:`werkzeug.exceptions.abort`. + + :param code: The status code for the exception, which must be + registered in ``app.aborter``. + :param args: Passed to the exception. + :param kwargs: Passed to the exception. + + .. versionadded:: 2.2 + Calls ``current_app.aborter`` if available instead of always + using Werkzeug's default ``abort``. + """ + if current_app: + current_app.aborter(code, *args, **kwargs) + + _wz_abort(code, *args, **kwargs) + + +def get_template_attribute(template_name: str, attribute: str) -> t.Any: + """Loads a macro (or variable) a template exports. This can be used to + invoke a macro from within Python code. If you for example have a + template named :file:`_cider.html` with the following contents: + + .. sourcecode:: html+jinja + + {% macro hello(name) %}Hello {{ name }}!{% endmacro %} + + You can access this from Python code like this:: + + hello = get_template_attribute('_cider.html', 'hello') + return hello('World') + + .. versionadded:: 0.2 + + :param template_name: the name of the template + :param attribute: the name of the variable of macro to access + """ + return getattr(current_app.jinja_env.get_template(template_name).module, attribute) + + +def flash(message: str, category: str = "message") -> None: + """Flashes a message to the next request. In order to remove the + flashed message from the session and to display it to the user, + the template has to call :func:`get_flashed_messages`. + + .. versionchanged:: 0.3 + `category` parameter added. + + :param message: the message to be flashed. + :param category: the category for the message. The following values + are recommended: ``'message'`` for any kind of message, + ``'error'`` for errors, ``'info'`` for information + messages and ``'warning'`` for warnings. However any + kind of string can be used as category. + """ + # Original implementation: + # + # session.setdefault('_flashes', []).append((category, message)) + # + # This assumed that changes made to mutable structures in the session are + # always in sync with the session object, which is not true for session + # implementations that use external storage for keeping their keys/values. + flashes = session.get("_flashes", []) + flashes.append((category, message)) + session["_flashes"] = flashes + app = current_app._get_current_object() # type: ignore + message_flashed.send( + app, + _async_wrapper=app.ensure_sync, + message=message, + category=category, + ) + + +def get_flashed_messages( + with_categories: bool = False, category_filter: t.Iterable[str] = () +) -> list[str] | list[tuple[str, str]]: + """Pulls all flashed messages from the session and returns them. + Further calls in the same request to the function will return + the same messages. By default just the messages are returned, + but when `with_categories` is set to ``True``, the return value will + be a list of tuples in the form ``(category, message)`` instead. + + Filter the flashed messages to one or more categories by providing those + categories in `category_filter`. This allows rendering categories in + separate html blocks. The `with_categories` and `category_filter` + arguments are distinct: + + * `with_categories` controls whether categories are returned with message + text (``True`` gives a tuple, where ``False`` gives just the message text). + * `category_filter` filters the messages down to only those matching the + provided categories. + + See :doc:`/patterns/flashing` for examples. + + .. versionchanged:: 0.3 + `with_categories` parameter added. + + .. versionchanged:: 0.9 + `category_filter` parameter added. + + :param with_categories: set to ``True`` to also receive categories. + :param category_filter: filter of categories to limit return values. Only + categories in the list will be returned. + """ + flashes = request_ctx.flashes + if flashes is None: + flashes = session.pop("_flashes") if "_flashes" in session else [] + request_ctx.flashes = flashes + if category_filter: + flashes = list(filter(lambda f: f[0] in category_filter, flashes)) + if not with_categories: + return [x[1] for x in flashes] + return flashes + + +def _prepare_send_file_kwargs(**kwargs: t.Any) -> dict[str, t.Any]: + if kwargs.get("max_age") is None: + kwargs["max_age"] = current_app.get_send_file_max_age + + kwargs.update( + environ=request.environ, + use_x_sendfile=current_app.config["USE_X_SENDFILE"], + response_class=current_app.response_class, + _root_path=current_app.root_path, + ) + return kwargs + + +def send_file( + path_or_file: os.PathLike[t.AnyStr] | str | t.IO[bytes], + mimetype: str | None = None, + as_attachment: bool = False, + download_name: str | None = None, + conditional: bool = True, + etag: bool | str = True, + last_modified: datetime | int | float | None = None, + max_age: None | (int | t.Callable[[str | None], int | None]) = None, +) -> Response: + """Send the contents of a file to the client. + + The first argument can be a file path or a file-like object. Paths + are preferred in most cases because Werkzeug can manage the file and + get extra information from the path. Passing a file-like object + requires that the file is opened in binary mode, and is mostly + useful when building a file in memory with :class:`io.BytesIO`. + + Never pass file paths provided by a user. The path is assumed to be + trusted, so a user could craft a path to access a file you didn't + intend. Use :func:`send_from_directory` to safely serve + user-requested paths from within a directory. + + If the WSGI server sets a ``file_wrapper`` in ``environ``, it is + used, otherwise Werkzeug's built-in wrapper is used. Alternatively, + if the HTTP server supports ``X-Sendfile``, configuring Flask with + ``USE_X_SENDFILE = True`` will tell the server to send the given + path, which is much more efficient than reading it in Python. + + :param path_or_file: The path to the file to send, relative to the + current working directory if a relative path is given. + Alternatively, a file-like object opened in binary mode. Make + sure the file pointer is seeked to the start of the data. + :param mimetype: The MIME type to send for the file. If not + provided, it will try to detect it from the file name. + :param as_attachment: Indicate to a browser that it should offer to + save the file instead of displaying it. + :param download_name: The default name browsers will use when saving + the file. Defaults to the passed file name. + :param conditional: Enable conditional and range responses based on + request headers. Requires passing a file path and ``environ``. + :param etag: Calculate an ETag for the file, which requires passing + a file path. Can also be a string to use instead. + :param last_modified: The last modified time to send for the file, + in seconds. If not provided, it will try to detect it from the + file path. + :param max_age: How long the client should cache the file, in + seconds. If set, ``Cache-Control`` will be ``public``, otherwise + it will be ``no-cache`` to prefer conditional caching. + + .. versionchanged:: 2.0 + ``download_name`` replaces the ``attachment_filename`` + parameter. If ``as_attachment=False``, it is passed with + ``Content-Disposition: inline`` instead. + + .. versionchanged:: 2.0 + ``max_age`` replaces the ``cache_timeout`` parameter. + ``conditional`` is enabled and ``max_age`` is not set by + default. + + .. versionchanged:: 2.0 + ``etag`` replaces the ``add_etags`` parameter. It can be a + string to use instead of generating one. + + .. versionchanged:: 2.0 + Passing a file-like object that inherits from + :class:`~io.TextIOBase` will raise a :exc:`ValueError` rather + than sending an empty file. + + .. versionadded:: 2.0 + Moved the implementation to Werkzeug. This is now a wrapper to + pass some Flask-specific arguments. + + .. versionchanged:: 1.1 + ``filename`` may be a :class:`~os.PathLike` object. + + .. versionchanged:: 1.1 + Passing a :class:`~io.BytesIO` object supports range requests. + + .. versionchanged:: 1.0.3 + Filenames are encoded with ASCII instead of Latin-1 for broader + compatibility with WSGI servers. + + .. versionchanged:: 1.0 + UTF-8 filenames as specified in :rfc:`2231` are supported. + + .. versionchanged:: 0.12 + The filename is no longer automatically inferred from file + objects. If you want to use automatic MIME and etag support, + pass a filename via ``filename_or_fp`` or + ``attachment_filename``. + + .. versionchanged:: 0.12 + ``attachment_filename`` is preferred over ``filename`` for MIME + detection. + + .. versionchanged:: 0.9 + ``cache_timeout`` defaults to + :meth:`Flask.get_send_file_max_age`. + + .. versionchanged:: 0.7 + MIME guessing and etag support for file-like objects was + removed because it was unreliable. Pass a filename if you are + able to, otherwise attach an etag yourself. + + .. versionchanged:: 0.5 + The ``add_etags``, ``cache_timeout`` and ``conditional`` + parameters were added. The default behavior is to add etags. + + .. versionadded:: 0.2 + """ + return werkzeug.utils.send_file( # type: ignore[return-value] + **_prepare_send_file_kwargs( + path_or_file=path_or_file, + environ=request.environ, + mimetype=mimetype, + as_attachment=as_attachment, + download_name=download_name, + conditional=conditional, + etag=etag, + last_modified=last_modified, + max_age=max_age, + ) + ) + + +def send_from_directory( + directory: os.PathLike[str] | str, + path: os.PathLike[str] | str, + **kwargs: t.Any, +) -> Response: + """Send a file from within a directory using :func:`send_file`. + + .. code-block:: python + + @app.route("/uploads/") + def download_file(name): + return send_from_directory( + app.config['UPLOAD_FOLDER'], name, as_attachment=True + ) + + This is a secure way to serve files from a folder, such as static + files or uploads. Uses :func:`~werkzeug.security.safe_join` to + ensure the path coming from the client is not maliciously crafted to + point outside the specified directory. + + If the final path does not point to an existing regular file, + raises a 404 :exc:`~werkzeug.exceptions.NotFound` error. + + :param directory: The directory that ``path`` must be located under, + relative to the current application's root path. This *must not* + be a value provided by the client, otherwise it becomes insecure. + :param path: The path to the file to send, relative to + ``directory``. + :param kwargs: Arguments to pass to :func:`send_file`. + + .. versionchanged:: 2.0 + ``path`` replaces the ``filename`` parameter. + + .. versionadded:: 2.0 + Moved the implementation to Werkzeug. This is now a wrapper to + pass some Flask-specific arguments. + + .. versionadded:: 0.5 + """ + return werkzeug.utils.send_from_directory( # type: ignore[return-value] + directory, path, **_prepare_send_file_kwargs(**kwargs) + ) + + +def get_root_path(import_name: str) -> str: + """Find the root path of a package, or the path that contains a + module. If it cannot be found, returns the current working + directory. + + Not to be confused with the value returned by :func:`find_package`. + + :meta private: + """ + # Module already imported and has a file attribute. Use that first. + mod = sys.modules.get(import_name) + + if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None: + return os.path.dirname(os.path.abspath(mod.__file__)) + + # Next attempt: check the loader. + try: + spec = importlib.util.find_spec(import_name) + + if spec is None: + raise ValueError + except (ImportError, ValueError): + loader = None + else: + loader = spec.loader + + # Loader does not exist or we're referring to an unloaded main + # module or a main module without path (interactive sessions), go + # with the current working directory. + if loader is None: + return os.getcwd() + + if hasattr(loader, "get_filename"): + filepath = loader.get_filename(import_name) # pyright: ignore + else: + # Fall back to imports. + __import__(import_name) + mod = sys.modules[import_name] + filepath = getattr(mod, "__file__", None) + + # If we don't have a file path it might be because it is a + # namespace package. In this case pick the root path from the + # first module that is contained in the package. + if filepath is None: + raise RuntimeError( + "No root path can be found for the provided module" + f" {import_name!r}. This can happen because the module" + " came from an import hook that does not provide file" + " name information or because it's a namespace package." + " In this case the root path needs to be explicitly" + " provided." + ) + + # filepath is import_name.py for a module, or __init__.py for a package. + return os.path.dirname(os.path.abspath(filepath)) # type: ignore[no-any-return] + + +@cache +def _split_blueprint_path(name: str) -> list[str]: + out: list[str] = [name] + + if "." in name: + out.extend(_split_blueprint_path(name.rpartition(".")[0])) + + return out diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__init__.py new file mode 100644 index 0000000000..c0941d049e --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__init__.py @@ -0,0 +1,170 @@ +from __future__ import annotations + +import json as _json +import typing as t + +from ..globals import current_app +from .provider import _default + +if t.TYPE_CHECKING: # pragma: no cover + from ..wrappers import Response + + +def dumps(obj: t.Any, **kwargs: t.Any) -> str: + """Serialize data as JSON. + + If :data:`~flask.current_app` is available, it will use its + :meth:`app.json.dumps() ` + method, otherwise it will use :func:`json.dumps`. + + :param obj: The data to serialize. + :param kwargs: Arguments passed to the ``dumps`` implementation. + + .. versionchanged:: 2.3 + The ``app`` parameter was removed. + + .. versionchanged:: 2.2 + Calls ``current_app.json.dumps``, allowing an app to override + the behavior. + + .. versionchanged:: 2.0.2 + :class:`decimal.Decimal` is supported by converting to a string. + + .. versionchanged:: 2.0 + ``encoding`` will be removed in Flask 2.1. + + .. versionchanged:: 1.0.3 + ``app`` can be passed directly, rather than requiring an app + context for configuration. + """ + if current_app: + return current_app.json.dumps(obj, **kwargs) + + kwargs.setdefault("default", _default) + return _json.dumps(obj, **kwargs) + + +def dump(obj: t.Any, fp: t.IO[str], **kwargs: t.Any) -> None: + """Serialize data as JSON and write to a file. + + If :data:`~flask.current_app` is available, it will use its + :meth:`app.json.dump() ` + method, otherwise it will use :func:`json.dump`. + + :param obj: The data to serialize. + :param fp: A file opened for writing text. Should use the UTF-8 + encoding to be valid JSON. + :param kwargs: Arguments passed to the ``dump`` implementation. + + .. versionchanged:: 2.3 + The ``app`` parameter was removed. + + .. versionchanged:: 2.2 + Calls ``current_app.json.dump``, allowing an app to override + the behavior. + + .. versionchanged:: 2.0 + Writing to a binary file, and the ``encoding`` argument, will be + removed in Flask 2.1. + """ + if current_app: + current_app.json.dump(obj, fp, **kwargs) + else: + kwargs.setdefault("default", _default) + _json.dump(obj, fp, **kwargs) + + +def loads(s: str | bytes, **kwargs: t.Any) -> t.Any: + """Deserialize data as JSON. + + If :data:`~flask.current_app` is available, it will use its + :meth:`app.json.loads() ` + method, otherwise it will use :func:`json.loads`. + + :param s: Text or UTF-8 bytes. + :param kwargs: Arguments passed to the ``loads`` implementation. + + .. versionchanged:: 2.3 + The ``app`` parameter was removed. + + .. versionchanged:: 2.2 + Calls ``current_app.json.loads``, allowing an app to override + the behavior. + + .. versionchanged:: 2.0 + ``encoding`` will be removed in Flask 2.1. The data must be a + string or UTF-8 bytes. + + .. versionchanged:: 1.0.3 + ``app`` can be passed directly, rather than requiring an app + context for configuration. + """ + if current_app: + return current_app.json.loads(s, **kwargs) + + return _json.loads(s, **kwargs) + + +def load(fp: t.IO[t.AnyStr], **kwargs: t.Any) -> t.Any: + """Deserialize data as JSON read from a file. + + If :data:`~flask.current_app` is available, it will use its + :meth:`app.json.load() ` + method, otherwise it will use :func:`json.load`. + + :param fp: A file opened for reading text or UTF-8 bytes. + :param kwargs: Arguments passed to the ``load`` implementation. + + .. versionchanged:: 2.3 + The ``app`` parameter was removed. + + .. versionchanged:: 2.2 + Calls ``current_app.json.load``, allowing an app to override + the behavior. + + .. versionchanged:: 2.2 + The ``app`` parameter will be removed in Flask 2.3. + + .. versionchanged:: 2.0 + ``encoding`` will be removed in Flask 2.1. The file must be text + mode, or binary mode with UTF-8 bytes. + """ + if current_app: + return current_app.json.load(fp, **kwargs) + + return _json.load(fp, **kwargs) + + +def jsonify(*args: t.Any, **kwargs: t.Any) -> Response: + """Serialize the given arguments as JSON, and return a + :class:`~flask.Response` object with the ``application/json`` + mimetype. A dict or list returned from a view will be converted to a + JSON response automatically without needing to call this. + + This requires an active request or application context, and calls + :meth:`app.json.response() `. + + In debug mode, the output is formatted with indentation to make it + easier to read. This may also be controlled by the provider. + + Either positional or keyword arguments can be given, not both. + If no arguments are given, ``None`` is serialized. + + :param args: A single value to serialize, or multiple values to + treat as a list to serialize. + :param kwargs: Treat as a dict to serialize. + + .. versionchanged:: 2.2 + Calls ``current_app.json.response``, allowing an app to override + the behavior. + + .. versionchanged:: 2.0.2 + :class:`decimal.Decimal` is supported by converting to a string. + + .. versionchanged:: 0.11 + Added support for serializing top-level arrays. This was a + security risk in ancient browsers. See :ref:`security-json`. + + .. versionadded:: 0.2 + """ + return current_app.json.response(*args, **kwargs) # type: ignore[return-value] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..3607d64746 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/provider.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/provider.cpython-311.pyc new file mode 100644 index 0000000000..cf7abd712a Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/provider.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/tag.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/tag.cpython-311.pyc new file mode 100644 index 0000000000..101b066413 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/__pycache__/tag.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/provider.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/provider.py new file mode 100644 index 0000000000..ea7e4753ff --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/provider.py @@ -0,0 +1,215 @@ +from __future__ import annotations + +import dataclasses +import decimal +import json +import typing as t +import uuid +import weakref +from datetime import date + +from werkzeug.http import http_date + +if t.TYPE_CHECKING: # pragma: no cover + from werkzeug.sansio.response import Response + + from ..sansio.app import App + + +class JSONProvider: + """A standard set of JSON operations for an application. Subclasses + of this can be used to customize JSON behavior or use different + JSON libraries. + + To implement a provider for a specific library, subclass this base + class and implement at least :meth:`dumps` and :meth:`loads`. All + other methods have default implementations. + + To use a different provider, either subclass ``Flask`` and set + :attr:`~flask.Flask.json_provider_class` to a provider class, or set + :attr:`app.json ` to an instance of the class. + + :param app: An application instance. This will be stored as a + :class:`weakref.proxy` on the :attr:`_app` attribute. + + .. versionadded:: 2.2 + """ + + def __init__(self, app: App) -> None: + self._app: App = weakref.proxy(app) + + def dumps(self, obj: t.Any, **kwargs: t.Any) -> str: + """Serialize data as JSON. + + :param obj: The data to serialize. + :param kwargs: May be passed to the underlying JSON library. + """ + raise NotImplementedError + + def dump(self, obj: t.Any, fp: t.IO[str], **kwargs: t.Any) -> None: + """Serialize data as JSON and write to a file. + + :param obj: The data to serialize. + :param fp: A file opened for writing text. Should use the UTF-8 + encoding to be valid JSON. + :param kwargs: May be passed to the underlying JSON library. + """ + fp.write(self.dumps(obj, **kwargs)) + + def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any: + """Deserialize data as JSON. + + :param s: Text or UTF-8 bytes. + :param kwargs: May be passed to the underlying JSON library. + """ + raise NotImplementedError + + def load(self, fp: t.IO[t.AnyStr], **kwargs: t.Any) -> t.Any: + """Deserialize data as JSON read from a file. + + :param fp: A file opened for reading text or UTF-8 bytes. + :param kwargs: May be passed to the underlying JSON library. + """ + return self.loads(fp.read(), **kwargs) + + def _prepare_response_obj( + self, args: tuple[t.Any, ...], kwargs: dict[str, t.Any] + ) -> t.Any: + if args and kwargs: + raise TypeError("app.json.response() takes either args or kwargs, not both") + + if not args and not kwargs: + return None + + if len(args) == 1: + return args[0] + + return args or kwargs + + def response(self, *args: t.Any, **kwargs: t.Any) -> Response: + """Serialize the given arguments as JSON, and return a + :class:`~flask.Response` object with the ``application/json`` + mimetype. + + The :func:`~flask.json.jsonify` function calls this method for + the current application. + + Either positional or keyword arguments can be given, not both. + If no arguments are given, ``None`` is serialized. + + :param args: A single value to serialize, or multiple values to + treat as a list to serialize. + :param kwargs: Treat as a dict to serialize. + """ + obj = self._prepare_response_obj(args, kwargs) + return self._app.response_class(self.dumps(obj), mimetype="application/json") + + +def _default(o: t.Any) -> t.Any: + if isinstance(o, date): + return http_date(o) + + if isinstance(o, (decimal.Decimal, uuid.UUID)): + return str(o) + + if dataclasses and dataclasses.is_dataclass(o): + return dataclasses.asdict(o) # type: ignore[arg-type] + + if hasattr(o, "__html__"): + return str(o.__html__()) + + raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable") + + +class DefaultJSONProvider(JSONProvider): + """Provide JSON operations using Python's built-in :mod:`json` + library. Serializes the following additional data types: + + - :class:`datetime.datetime` and :class:`datetime.date` are + serialized to :rfc:`822` strings. This is the same as the HTTP + date format. + - :class:`uuid.UUID` is serialized to a string. + - :class:`dataclasses.dataclass` is passed to + :func:`dataclasses.asdict`. + - :class:`~markupsafe.Markup` (or any object with a ``__html__`` + method) will call the ``__html__`` method to get a string. + """ + + default: t.Callable[[t.Any], t.Any] = staticmethod(_default) # type: ignore[assignment] + """Apply this function to any object that :meth:`json.dumps` does + not know how to serialize. It should return a valid JSON type or + raise a ``TypeError``. + """ + + ensure_ascii = True + """Replace non-ASCII characters with escape sequences. This may be + more compatible with some clients, but can be disabled for better + performance and size. + """ + + sort_keys = True + """Sort the keys in any serialized dicts. This may be useful for + some caching situations, but can be disabled for better performance. + When enabled, keys must all be strings, they are not converted + before sorting. + """ + + compact: bool | None = None + """If ``True``, or ``None`` out of debug mode, the :meth:`response` + output will not add indentation, newlines, or spaces. If ``False``, + or ``None`` in debug mode, it will use a non-compact representation. + """ + + mimetype = "application/json" + """The mimetype set in :meth:`response`.""" + + def dumps(self, obj: t.Any, **kwargs: t.Any) -> str: + """Serialize data as JSON to a string. + + Keyword arguments are passed to :func:`json.dumps`. Sets some + parameter defaults from the :attr:`default`, + :attr:`ensure_ascii`, and :attr:`sort_keys` attributes. + + :param obj: The data to serialize. + :param kwargs: Passed to :func:`json.dumps`. + """ + kwargs.setdefault("default", self.default) + kwargs.setdefault("ensure_ascii", self.ensure_ascii) + kwargs.setdefault("sort_keys", self.sort_keys) + return json.dumps(obj, **kwargs) + + def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any: + """Deserialize data as JSON from a string or bytes. + + :param s: Text or UTF-8 bytes. + :param kwargs: Passed to :func:`json.loads`. + """ + return json.loads(s, **kwargs) + + def response(self, *args: t.Any, **kwargs: t.Any) -> Response: + """Serialize the given arguments as JSON, and return a + :class:`~flask.Response` object with it. The response mimetype + will be "application/json" and can be changed with + :attr:`mimetype`. + + If :attr:`compact` is ``False`` or debug mode is enabled, the + output will be formatted to be easier to read. + + Either positional or keyword arguments can be given, not both. + If no arguments are given, ``None`` is serialized. + + :param args: A single value to serialize, or multiple values to + treat as a list to serialize. + :param kwargs: Treat as a dict to serialize. + """ + obj = self._prepare_response_obj(args, kwargs) + dump_args: dict[str, t.Any] = {} + + if (self.compact is None and self._app.debug) or self.compact is False: + dump_args.setdefault("indent", 2) + else: + dump_args.setdefault("separators", (",", ":")) + + return self._app.response_class( + f"{self.dumps(obj, **dump_args)}\n", mimetype=self.mimetype + ) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/tag.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/tag.py new file mode 100644 index 0000000000..8dc3629bf9 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/json/tag.py @@ -0,0 +1,327 @@ +""" +Tagged JSON +~~~~~~~~~~~ + +A compact representation for lossless serialization of non-standard JSON +types. :class:`~flask.sessions.SecureCookieSessionInterface` uses this +to serialize the session data, but it may be useful in other places. It +can be extended to support other types. + +.. autoclass:: TaggedJSONSerializer + :members: + +.. autoclass:: JSONTag + :members: + +Let's see an example that adds support for +:class:`~collections.OrderedDict`. Dicts don't have an order in JSON, so +to handle this we will dump the items as a list of ``[key, value]`` +pairs. Subclass :class:`JSONTag` and give it the new key ``' od'`` to +identify the type. The session serializer processes dicts first, so +insert the new tag at the front of the order since ``OrderedDict`` must +be processed before ``dict``. + +.. code-block:: python + + from flask.json.tag import JSONTag + + class TagOrderedDict(JSONTag): + __slots__ = ('serializer',) + key = ' od' + + def check(self, value): + return isinstance(value, OrderedDict) + + def to_json(self, value): + return [[k, self.serializer.tag(v)] for k, v in iteritems(value)] + + def to_python(self, value): + return OrderedDict(value) + + app.session_interface.serializer.register(TagOrderedDict, index=0) +""" + +from __future__ import annotations + +import typing as t +from base64 import b64decode +from base64 import b64encode +from datetime import datetime +from uuid import UUID + +from markupsafe import Markup +from werkzeug.http import http_date +from werkzeug.http import parse_date + +from ..json import dumps +from ..json import loads + + +class JSONTag: + """Base class for defining type tags for :class:`TaggedJSONSerializer`.""" + + __slots__ = ("serializer",) + + #: The tag to mark the serialized object with. If empty, this tag is + #: only used as an intermediate step during tagging. + key: str = "" + + def __init__(self, serializer: TaggedJSONSerializer) -> None: + """Create a tagger for the given serializer.""" + self.serializer = serializer + + def check(self, value: t.Any) -> bool: + """Check if the given value should be tagged by this tag.""" + raise NotImplementedError + + def to_json(self, value: t.Any) -> t.Any: + """Convert the Python object to an object that is a valid JSON type. + The tag will be added later.""" + raise NotImplementedError + + def to_python(self, value: t.Any) -> t.Any: + """Convert the JSON representation back to the correct type. The tag + will already be removed.""" + raise NotImplementedError + + def tag(self, value: t.Any) -> dict[str, t.Any]: + """Convert the value to a valid JSON type and add the tag structure + around it.""" + return {self.key: self.to_json(value)} + + +class TagDict(JSONTag): + """Tag for 1-item dicts whose only key matches a registered tag. + + Internally, the dict key is suffixed with `__`, and the suffix is removed + when deserializing. + """ + + __slots__ = () + key = " di" + + def check(self, value: t.Any) -> bool: + return ( + isinstance(value, dict) + and len(value) == 1 + and next(iter(value)) in self.serializer.tags + ) + + def to_json(self, value: t.Any) -> t.Any: + key = next(iter(value)) + return {f"{key}__": self.serializer.tag(value[key])} + + def to_python(self, value: t.Any) -> t.Any: + key = next(iter(value)) + return {key[:-2]: value[key]} + + +class PassDict(JSONTag): + __slots__ = () + + def check(self, value: t.Any) -> bool: + return isinstance(value, dict) + + def to_json(self, value: t.Any) -> t.Any: + # JSON objects may only have string keys, so don't bother tagging the + # key here. + return {k: self.serializer.tag(v) for k, v in value.items()} + + tag = to_json + + +class TagTuple(JSONTag): + __slots__ = () + key = " t" + + def check(self, value: t.Any) -> bool: + return isinstance(value, tuple) + + def to_json(self, value: t.Any) -> t.Any: + return [self.serializer.tag(item) for item in value] + + def to_python(self, value: t.Any) -> t.Any: + return tuple(value) + + +class PassList(JSONTag): + __slots__ = () + + def check(self, value: t.Any) -> bool: + return isinstance(value, list) + + def to_json(self, value: t.Any) -> t.Any: + return [self.serializer.tag(item) for item in value] + + tag = to_json + + +class TagBytes(JSONTag): + __slots__ = () + key = " b" + + def check(self, value: t.Any) -> bool: + return isinstance(value, bytes) + + def to_json(self, value: t.Any) -> t.Any: + return b64encode(value).decode("ascii") + + def to_python(self, value: t.Any) -> t.Any: + return b64decode(value) + + +class TagMarkup(JSONTag): + """Serialize anything matching the :class:`~markupsafe.Markup` API by + having a ``__html__`` method to the result of that method. Always + deserializes to an instance of :class:`~markupsafe.Markup`.""" + + __slots__ = () + key = " m" + + def check(self, value: t.Any) -> bool: + return callable(getattr(value, "__html__", None)) + + def to_json(self, value: t.Any) -> t.Any: + return str(value.__html__()) + + def to_python(self, value: t.Any) -> t.Any: + return Markup(value) + + +class TagUUID(JSONTag): + __slots__ = () + key = " u" + + def check(self, value: t.Any) -> bool: + return isinstance(value, UUID) + + def to_json(self, value: t.Any) -> t.Any: + return value.hex + + def to_python(self, value: t.Any) -> t.Any: + return UUID(value) + + +class TagDateTime(JSONTag): + __slots__ = () + key = " d" + + def check(self, value: t.Any) -> bool: + return isinstance(value, datetime) + + def to_json(self, value: t.Any) -> t.Any: + return http_date(value) + + def to_python(self, value: t.Any) -> t.Any: + return parse_date(value) + + +class TaggedJSONSerializer: + """Serializer that uses a tag system to compactly represent objects that + are not JSON types. Passed as the intermediate serializer to + :class:`itsdangerous.Serializer`. + + The following extra types are supported: + + * :class:`dict` + * :class:`tuple` + * :class:`bytes` + * :class:`~markupsafe.Markup` + * :class:`~uuid.UUID` + * :class:`~datetime.datetime` + """ + + __slots__ = ("tags", "order") + + #: Tag classes to bind when creating the serializer. Other tags can be + #: added later using :meth:`~register`. + default_tags = [ + TagDict, + PassDict, + TagTuple, + PassList, + TagBytes, + TagMarkup, + TagUUID, + TagDateTime, + ] + + def __init__(self) -> None: + self.tags: dict[str, JSONTag] = {} + self.order: list[JSONTag] = [] + + for cls in self.default_tags: + self.register(cls) + + def register( + self, + tag_class: type[JSONTag], + force: bool = False, + index: int | None = None, + ) -> None: + """Register a new tag with this serializer. + + :param tag_class: tag class to register. Will be instantiated with this + serializer instance. + :param force: overwrite an existing tag. If false (default), a + :exc:`KeyError` is raised. + :param index: index to insert the new tag in the tag order. Useful when + the new tag is a special case of an existing tag. If ``None`` + (default), the tag is appended to the end of the order. + + :raise KeyError: if the tag key is already registered and ``force`` is + not true. + """ + tag = tag_class(self) + key = tag.key + + if key: + if not force and key in self.tags: + raise KeyError(f"Tag '{key}' is already registered.") + + self.tags[key] = tag + + if index is None: + self.order.append(tag) + else: + self.order.insert(index, tag) + + def tag(self, value: t.Any) -> t.Any: + """Convert a value to a tagged representation if necessary.""" + for tag in self.order: + if tag.check(value): + return tag.tag(value) + + return value + + def untag(self, value: dict[str, t.Any]) -> t.Any: + """Convert a tagged representation back to the original type.""" + if len(value) != 1: + return value + + key = next(iter(value)) + + if key not in self.tags: + return value + + return self.tags[key].to_python(value[key]) + + def _untag_scan(self, value: t.Any) -> t.Any: + if isinstance(value, dict): + # untag each item recursively + value = {k: self._untag_scan(v) for k, v in value.items()} + # untag the dict itself + value = self.untag(value) + elif isinstance(value, list): + # untag each item recursively + value = [self._untag_scan(item) for item in value] + + return value + + def dumps(self, value: t.Any) -> str: + """Tag the value and dump it to a compact JSON string.""" + return dumps(self.tag(value), separators=(",", ":")) + + def loads(self, value: str) -> t.Any: + """Load data from a JSON string and deserialized any tagged objects.""" + return self._untag_scan(loads(value)) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/logging.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/logging.py new file mode 100644 index 0000000000..0cb8f43746 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/logging.py @@ -0,0 +1,79 @@ +from __future__ import annotations + +import logging +import sys +import typing as t + +from werkzeug.local import LocalProxy + +from .globals import request + +if t.TYPE_CHECKING: # pragma: no cover + from .sansio.app import App + + +@LocalProxy +def wsgi_errors_stream() -> t.TextIO: + """Find the most appropriate error stream for the application. If a request + is active, log to ``wsgi.errors``, otherwise use ``sys.stderr``. + + If you configure your own :class:`logging.StreamHandler`, you may want to + use this for the stream. If you are using file or dict configuration and + can't import this directly, you can refer to it as + ``ext://flask.logging.wsgi_errors_stream``. + """ + if request: + return request.environ["wsgi.errors"] # type: ignore[no-any-return] + + return sys.stderr + + +def has_level_handler(logger: logging.Logger) -> bool: + """Check if there is a handler in the logging chain that will handle the + given logger's :meth:`effective level <~logging.Logger.getEffectiveLevel>`. + """ + level = logger.getEffectiveLevel() + current = logger + + while current: + if any(handler.level <= level for handler in current.handlers): + return True + + if not current.propagate: + break + + current = current.parent # type: ignore + + return False + + +#: Log messages to :func:`~flask.logging.wsgi_errors_stream` with the format +#: ``[%(asctime)s] %(levelname)s in %(module)s: %(message)s``. +default_handler = logging.StreamHandler(wsgi_errors_stream) # type: ignore +default_handler.setFormatter( + logging.Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s") +) + + +def create_logger(app: App) -> logging.Logger: + """Get the Flask app's logger and configure it if needed. + + The logger name will be the same as + :attr:`app.import_name `. + + When :attr:`~flask.Flask.debug` is enabled, set the logger level to + :data:`logging.DEBUG` if it is not set. + + If there is no handler for the logger's effective level, add a + :class:`~logging.StreamHandler` for + :func:`~flask.logging.wsgi_errors_stream` with a basic format. + """ + logger = logging.getLogger(app.name) + + if app.debug and not logger.level: + logger.setLevel(logging.DEBUG) + + if not has_level_handler(logger): + logger.addHandler(default_handler) + + return logger diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/py.typed b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/README.md b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/README.md new file mode 100644 index 0000000000..623ac19823 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/README.md @@ -0,0 +1,6 @@ +# Sansio + +This folder contains code that can be used by alternative Flask +implementations, for example Quart. The code therefore cannot do any +IO, nor be part of a likely IO path. Finally this code cannot use the +Flask globals. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/app.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000000..109948d329 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/app.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/blueprints.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/blueprints.cpython-311.pyc new file mode 100644 index 0000000000..b1f05c134c Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/blueprints.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/scaffold.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/scaffold.cpython-311.pyc new file mode 100644 index 0000000000..e9822becf3 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/__pycache__/scaffold.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/app.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/app.py new file mode 100644 index 0000000000..58cb873062 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/app.py @@ -0,0 +1,964 @@ +from __future__ import annotations + +import logging +import os +import sys +import typing as t +from datetime import timedelta +from itertools import chain + +from werkzeug.exceptions import Aborter +from werkzeug.exceptions import BadRequest +from werkzeug.exceptions import BadRequestKeyError +from werkzeug.routing import BuildError +from werkzeug.routing import Map +from werkzeug.routing import Rule +from werkzeug.sansio.response import Response +from werkzeug.utils import cached_property +from werkzeug.utils import redirect as _wz_redirect + +from .. import typing as ft +from ..config import Config +from ..config import ConfigAttribute +from ..ctx import _AppCtxGlobals +from ..helpers import _split_blueprint_path +from ..helpers import get_debug_flag +from ..json.provider import DefaultJSONProvider +from ..json.provider import JSONProvider +from ..logging import create_logger +from ..templating import DispatchingJinjaLoader +from ..templating import Environment +from .scaffold import _endpoint_from_view_func +from .scaffold import find_package +from .scaffold import Scaffold +from .scaffold import setupmethod + +if t.TYPE_CHECKING: # pragma: no cover + from werkzeug.wrappers import Response as BaseResponse + + from ..testing import FlaskClient + from ..testing import FlaskCliRunner + from .blueprints import Blueprint + +T_shell_context_processor = t.TypeVar( + "T_shell_context_processor", bound=ft.ShellContextProcessorCallable +) +T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable) +T_template_filter = t.TypeVar("T_template_filter", bound=ft.TemplateFilterCallable) +T_template_global = t.TypeVar("T_template_global", bound=ft.TemplateGlobalCallable) +T_template_test = t.TypeVar("T_template_test", bound=ft.TemplateTestCallable) + + +def _make_timedelta(value: timedelta | int | None) -> timedelta | None: + if value is None or isinstance(value, timedelta): + return value + + return timedelta(seconds=value) + + +class App(Scaffold): + """The flask object implements a WSGI application and acts as the central + object. It is passed the name of the module or package of the + application. Once it is created it will act as a central registry for + the view functions, the URL rules, template configuration and much more. + + The name of the package is used to resolve resources from inside the + package or the folder the module is contained in depending on if the + package parameter resolves to an actual python package (a folder with + an :file:`__init__.py` file inside) or a standard module (just a ``.py`` file). + + For more information about resource loading, see :func:`open_resource`. + + Usually you create a :class:`Flask` instance in your main module or + in the :file:`__init__.py` file of your package like this:: + + from flask import Flask + app = Flask(__name__) + + .. admonition:: About the First Parameter + + The idea of the first parameter is to give Flask an idea of what + belongs to your application. This name is used to find resources + on the filesystem, can be used by extensions to improve debugging + information and a lot more. + + So it's important what you provide there. If you are using a single + module, `__name__` is always the correct value. If you however are + using a package, it's usually recommended to hardcode the name of + your package there. + + For example if your application is defined in :file:`yourapplication/app.py` + you should create it with one of the two versions below:: + + app = Flask('yourapplication') + app = Flask(__name__.split('.')[0]) + + Why is that? The application will work even with `__name__`, thanks + to how resources are looked up. However it will make debugging more + painful. Certain extensions can make assumptions based on the + import name of your application. For example the Flask-SQLAlchemy + extension will look for the code in your application that triggered + an SQL query in debug mode. If the import name is not properly set + up, that debugging information is lost. (For example it would only + pick up SQL queries in `yourapplication.app` and not + `yourapplication.views.frontend`) + + .. versionadded:: 0.7 + The `static_url_path`, `static_folder`, and `template_folder` + parameters were added. + + .. versionadded:: 0.8 + The `instance_path` and `instance_relative_config` parameters were + added. + + .. versionadded:: 0.11 + The `root_path` parameter was added. + + .. versionadded:: 1.0 + The ``host_matching`` and ``static_host`` parameters were added. + + .. versionadded:: 1.0 + The ``subdomain_matching`` parameter was added. Subdomain + matching needs to be enabled manually now. Setting + :data:`SERVER_NAME` does not implicitly enable it. + + :param import_name: the name of the application package + :param static_url_path: can be used to specify a different path for the + static files on the web. Defaults to the name + of the `static_folder` folder. + :param static_folder: The folder with static files that is served at + ``static_url_path``. Relative to the application ``root_path`` + or an absolute path. Defaults to ``'static'``. + :param static_host: the host to use when adding the static route. + Defaults to None. Required when using ``host_matching=True`` + with a ``static_folder`` configured. + :param host_matching: set ``url_map.host_matching`` attribute. + Defaults to False. + :param subdomain_matching: consider the subdomain relative to + :data:`SERVER_NAME` when matching routes. Defaults to False. + :param template_folder: the folder that contains the templates that should + be used by the application. Defaults to + ``'templates'`` folder in the root path of the + application. + :param instance_path: An alternative instance path for the application. + By default the folder ``'instance'`` next to the + package or module is assumed to be the instance + path. + :param instance_relative_config: if set to ``True`` relative filenames + for loading the config are assumed to + be relative to the instance path instead + of the application root. + :param root_path: The path to the root of the application files. + This should only be set manually when it can't be detected + automatically, such as for namespace packages. + """ + + #: The class of the object assigned to :attr:`aborter`, created by + #: :meth:`create_aborter`. That object is called by + #: :func:`flask.abort` to raise HTTP errors, and can be + #: called directly as well. + #: + #: Defaults to :class:`werkzeug.exceptions.Aborter`. + #: + #: .. versionadded:: 2.2 + aborter_class = Aborter + + #: The class that is used for the Jinja environment. + #: + #: .. versionadded:: 0.11 + jinja_environment = Environment + + #: The class that is used for the :data:`~flask.g` instance. + #: + #: Example use cases for a custom class: + #: + #: 1. Store arbitrary attributes on flask.g. + #: 2. Add a property for lazy per-request database connectors. + #: 3. Return None instead of AttributeError on unexpected attributes. + #: 4. Raise exception if an unexpected attr is set, a "controlled" flask.g. + #: + #: In Flask 0.9 this property was called `request_globals_class` but it + #: was changed in 0.10 to :attr:`app_ctx_globals_class` because the + #: flask.g object is now application context scoped. + #: + #: .. versionadded:: 0.10 + app_ctx_globals_class = _AppCtxGlobals + + #: The class that is used for the ``config`` attribute of this app. + #: Defaults to :class:`~flask.Config`. + #: + #: Example use cases for a custom class: + #: + #: 1. Default values for certain config options. + #: 2. Access to config values through attributes in addition to keys. + #: + #: .. versionadded:: 0.11 + config_class = Config + + #: The testing flag. Set this to ``True`` to enable the test mode of + #: Flask extensions (and in the future probably also Flask itself). + #: For example this might activate test helpers that have an + #: additional runtime cost which should not be enabled by default. + #: + #: If this is enabled and PROPAGATE_EXCEPTIONS is not changed from the + #: default it's implicitly enabled. + #: + #: This attribute can also be configured from the config with the + #: ``TESTING`` configuration key. Defaults to ``False``. + testing = ConfigAttribute[bool]("TESTING") + + #: If a secret key is set, cryptographic components can use this to + #: sign cookies and other things. Set this to a complex random value + #: when you want to use the secure cookie for instance. + #: + #: This attribute can also be configured from the config with the + #: :data:`SECRET_KEY` configuration key. Defaults to ``None``. + secret_key = ConfigAttribute[t.Union[str, bytes, None]]("SECRET_KEY") + + #: A :class:`~datetime.timedelta` which is used to set the expiration + #: date of a permanent session. The default is 31 days which makes a + #: permanent session survive for roughly one month. + #: + #: This attribute can also be configured from the config with the + #: ``PERMANENT_SESSION_LIFETIME`` configuration key. Defaults to + #: ``timedelta(days=31)`` + permanent_session_lifetime = ConfigAttribute[timedelta]( + "PERMANENT_SESSION_LIFETIME", + get_converter=_make_timedelta, # type: ignore[arg-type] + ) + + json_provider_class: type[JSONProvider] = DefaultJSONProvider + """A subclass of :class:`~flask.json.provider.JSONProvider`. An + instance is created and assigned to :attr:`app.json` when creating + the app. + + The default, :class:`~flask.json.provider.DefaultJSONProvider`, uses + Python's built-in :mod:`json` library. A different provider can use + a different JSON library. + + .. versionadded:: 2.2 + """ + + #: Options that are passed to the Jinja environment in + #: :meth:`create_jinja_environment`. Changing these options after + #: the environment is created (accessing :attr:`jinja_env`) will + #: have no effect. + #: + #: .. versionchanged:: 1.1.0 + #: This is a ``dict`` instead of an ``ImmutableDict`` to allow + #: easier configuration. + #: + jinja_options: dict[str, t.Any] = {} + + #: The rule object to use for URL rules created. This is used by + #: :meth:`add_url_rule`. Defaults to :class:`werkzeug.routing.Rule`. + #: + #: .. versionadded:: 0.7 + url_rule_class = Rule + + #: The map object to use for storing the URL rules and routing + #: configuration parameters. Defaults to :class:`werkzeug.routing.Map`. + #: + #: .. versionadded:: 1.1.0 + url_map_class = Map + + #: The :meth:`test_client` method creates an instance of this test + #: client class. Defaults to :class:`~flask.testing.FlaskClient`. + #: + #: .. versionadded:: 0.7 + test_client_class: type[FlaskClient] | None = None + + #: The :class:`~click.testing.CliRunner` subclass, by default + #: :class:`~flask.testing.FlaskCliRunner` that is used by + #: :meth:`test_cli_runner`. Its ``__init__`` method should take a + #: Flask app object as the first argument. + #: + #: .. versionadded:: 1.0 + test_cli_runner_class: type[FlaskCliRunner] | None = None + + default_config: dict[str, t.Any] + response_class: type[Response] + + def __init__( + self, + import_name: str, + static_url_path: str | None = None, + static_folder: str | os.PathLike[str] | None = "static", + static_host: str | None = None, + host_matching: bool = False, + subdomain_matching: bool = False, + template_folder: str | os.PathLike[str] | None = "templates", + instance_path: str | None = None, + instance_relative_config: bool = False, + root_path: str | None = None, + ) -> None: + super().__init__( + import_name=import_name, + static_folder=static_folder, + static_url_path=static_url_path, + template_folder=template_folder, + root_path=root_path, + ) + + if instance_path is None: + instance_path = self.auto_find_instance_path() + elif not os.path.isabs(instance_path): + raise ValueError( + "If an instance path is provided it must be absolute." + " A relative path was given instead." + ) + + #: Holds the path to the instance folder. + #: + #: .. versionadded:: 0.8 + self.instance_path = instance_path + + #: The configuration dictionary as :class:`Config`. This behaves + #: exactly like a regular dictionary but supports additional methods + #: to load a config from files. + self.config = self.make_config(instance_relative_config) + + #: An instance of :attr:`aborter_class` created by + #: :meth:`make_aborter`. This is called by :func:`flask.abort` + #: to raise HTTP errors, and can be called directly as well. + #: + #: .. versionadded:: 2.2 + #: Moved from ``flask.abort``, which calls this object. + self.aborter = self.make_aborter() + + self.json: JSONProvider = self.json_provider_class(self) + """Provides access to JSON methods. Functions in ``flask.json`` + will call methods on this provider when the application context + is active. Used for handling JSON requests and responses. + + An instance of :attr:`json_provider_class`. Can be customized by + changing that attribute on a subclass, or by assigning to this + attribute afterwards. + + The default, :class:`~flask.json.provider.DefaultJSONProvider`, + uses Python's built-in :mod:`json` library. A different provider + can use a different JSON library. + + .. versionadded:: 2.2 + """ + + #: A list of functions that are called by + #: :meth:`handle_url_build_error` when :meth:`.url_for` raises a + #: :exc:`~werkzeug.routing.BuildError`. Each function is called + #: with ``error``, ``endpoint`` and ``values``. If a function + #: returns ``None`` or raises a ``BuildError``, it is skipped. + #: Otherwise, its return value is returned by ``url_for``. + #: + #: .. versionadded:: 0.9 + self.url_build_error_handlers: list[ + t.Callable[[Exception, str, dict[str, t.Any]], str] + ] = [] + + #: A list of functions that are called when the application context + #: is destroyed. Since the application context is also torn down + #: if the request ends this is the place to store code that disconnects + #: from databases. + #: + #: .. versionadded:: 0.9 + self.teardown_appcontext_funcs: list[ft.TeardownCallable] = [] + + #: A list of shell context processor functions that should be run + #: when a shell context is created. + #: + #: .. versionadded:: 0.11 + self.shell_context_processors: list[ft.ShellContextProcessorCallable] = [] + + #: Maps registered blueprint names to blueprint objects. The + #: dict retains the order the blueprints were registered in. + #: Blueprints can be registered multiple times, this dict does + #: not track how often they were attached. + #: + #: .. versionadded:: 0.7 + self.blueprints: dict[str, Blueprint] = {} + + #: a place where extensions can store application specific state. For + #: example this is where an extension could store database engines and + #: similar things. + #: + #: The key must match the name of the extension module. For example in + #: case of a "Flask-Foo" extension in `flask_foo`, the key would be + #: ``'foo'``. + #: + #: .. versionadded:: 0.7 + self.extensions: dict[str, t.Any] = {} + + #: The :class:`~werkzeug.routing.Map` for this instance. You can use + #: this to change the routing converters after the class was created + #: but before any routes are connected. Example:: + #: + #: from werkzeug.routing import BaseConverter + #: + #: class ListConverter(BaseConverter): + #: def to_python(self, value): + #: return value.split(',') + #: def to_url(self, values): + #: return ','.join(super(ListConverter, self).to_url(value) + #: for value in values) + #: + #: app = Flask(__name__) + #: app.url_map.converters['list'] = ListConverter + self.url_map = self.url_map_class(host_matching=host_matching) + + self.subdomain_matching = subdomain_matching + + # tracks internally if the application already handled at least one + # request. + self._got_first_request = False + + def _check_setup_finished(self, f_name: str) -> None: + if self._got_first_request: + raise AssertionError( + f"The setup method '{f_name}' can no longer be called" + " on the application. It has already handled its first" + " request, any changes will not be applied" + " consistently.\n" + "Make sure all imports, decorators, functions, etc." + " needed to set up the application are done before" + " running it." + ) + + @cached_property + def name(self) -> str: + """The name of the application. This is usually the import name + with the difference that it's guessed from the run file if the + import name is main. This name is used as a display name when + Flask needs the name of the application. It can be set and overridden + to change the value. + + .. versionadded:: 0.8 + """ + if self.import_name == "__main__": + fn: str | None = getattr(sys.modules["__main__"], "__file__", None) + if fn is None: + return "__main__" + return os.path.splitext(os.path.basename(fn))[0] + return self.import_name + + @cached_property + def logger(self) -> logging.Logger: + """A standard Python :class:`~logging.Logger` for the app, with + the same name as :attr:`name`. + + In debug mode, the logger's :attr:`~logging.Logger.level` will + be set to :data:`~logging.DEBUG`. + + If there are no handlers configured, a default handler will be + added. See :doc:`/logging` for more information. + + .. versionchanged:: 1.1.0 + The logger takes the same name as :attr:`name` rather than + hard-coding ``"flask.app"``. + + .. versionchanged:: 1.0.0 + Behavior was simplified. The logger is always named + ``"flask.app"``. The level is only set during configuration, + it doesn't check ``app.debug`` each time. Only one format is + used, not different ones depending on ``app.debug``. No + handlers are removed, and a handler is only added if no + handlers are already configured. + + .. versionadded:: 0.3 + """ + return create_logger(self) + + @cached_property + def jinja_env(self) -> Environment: + """The Jinja environment used to load templates. + + The environment is created the first time this property is + accessed. Changing :attr:`jinja_options` after that will have no + effect. + """ + return self.create_jinja_environment() + + def create_jinja_environment(self) -> Environment: + raise NotImplementedError() + + def make_config(self, instance_relative: bool = False) -> Config: + """Used to create the config attribute by the Flask constructor. + The `instance_relative` parameter is passed in from the constructor + of Flask (there named `instance_relative_config`) and indicates if + the config should be relative to the instance path or the root path + of the application. + + .. versionadded:: 0.8 + """ + root_path = self.root_path + if instance_relative: + root_path = self.instance_path + defaults = dict(self.default_config) + defaults["DEBUG"] = get_debug_flag() + return self.config_class(root_path, defaults) + + def make_aborter(self) -> Aborter: + """Create the object to assign to :attr:`aborter`. That object + is called by :func:`flask.abort` to raise HTTP errors, and can + be called directly as well. + + By default, this creates an instance of :attr:`aborter_class`, + which defaults to :class:`werkzeug.exceptions.Aborter`. + + .. versionadded:: 2.2 + """ + return self.aborter_class() + + def auto_find_instance_path(self) -> str: + """Tries to locate the instance path if it was not provided to the + constructor of the application class. It will basically calculate + the path to a folder named ``instance`` next to your main file or + the package. + + .. versionadded:: 0.8 + """ + prefix, package_path = find_package(self.import_name) + if prefix is None: + return os.path.join(package_path, "instance") + return os.path.join(prefix, "var", f"{self.name}-instance") + + def create_global_jinja_loader(self) -> DispatchingJinjaLoader: + """Creates the loader for the Jinja environment. Can be used to + override just the loader and keeping the rest unchanged. It's + discouraged to override this function. Instead one should override + the :meth:`jinja_loader` function instead. + + The global loader dispatches between the loaders of the application + and the individual blueprints. + + .. versionadded:: 0.7 + """ + return DispatchingJinjaLoader(self) + + def select_jinja_autoescape(self, filename: str | None) -> bool: + """Returns ``True`` if autoescaping should be active for the given + template name. If no template name is given, returns `True`. + + .. versionchanged:: 2.2 + Autoescaping is now enabled by default for ``.svg`` files. + + .. versionadded:: 0.5 + """ + if filename is None: + return True + return filename.endswith((".html", ".htm", ".xml", ".xhtml", ".svg")) + + @property + def debug(self) -> bool: + """Whether debug mode is enabled. When using ``flask run`` to start the + development server, an interactive debugger will be shown for unhandled + exceptions, and the server will be reloaded when code changes. This maps to the + :data:`DEBUG` config key. It may not behave as expected if set late. + + **Do not enable debug mode when deploying in production.** + + Default: ``False`` + """ + return self.config["DEBUG"] # type: ignore[no-any-return] + + @debug.setter + def debug(self, value: bool) -> None: + self.config["DEBUG"] = value + + if self.config["TEMPLATES_AUTO_RELOAD"] is None: + self.jinja_env.auto_reload = value + + @setupmethod + def register_blueprint(self, blueprint: Blueprint, **options: t.Any) -> None: + """Register a :class:`~flask.Blueprint` on the application. Keyword + arguments passed to this method will override the defaults set on the + blueprint. + + Calls the blueprint's :meth:`~flask.Blueprint.register` method after + recording the blueprint in the application's :attr:`blueprints`. + + :param blueprint: The blueprint to register. + :param url_prefix: Blueprint routes will be prefixed with this. + :param subdomain: Blueprint routes will match on this subdomain. + :param url_defaults: Blueprint routes will use these default values for + view arguments. + :param options: Additional keyword arguments are passed to + :class:`~flask.blueprints.BlueprintSetupState`. They can be + accessed in :meth:`~flask.Blueprint.record` callbacks. + + .. versionchanged:: 2.0.1 + The ``name`` option can be used to change the (pre-dotted) + name the blueprint is registered with. This allows the same + blueprint to be registered multiple times with unique names + for ``url_for``. + + .. versionadded:: 0.7 + """ + blueprint.register(self, options) + + def iter_blueprints(self) -> t.ValuesView[Blueprint]: + """Iterates over all blueprints by the order they were registered. + + .. versionadded:: 0.11 + """ + return self.blueprints.values() + + @setupmethod + def add_url_rule( + self, + rule: str, + endpoint: str | None = None, + view_func: ft.RouteCallable | None = None, + provide_automatic_options: bool | None = None, + **options: t.Any, + ) -> None: + if endpoint is None: + endpoint = _endpoint_from_view_func(view_func) # type: ignore + options["endpoint"] = endpoint + methods = options.pop("methods", None) + + # if the methods are not given and the view_func object knows its + # methods we can use that instead. If neither exists, we go with + # a tuple of only ``GET`` as default. + if methods is None: + methods = getattr(view_func, "methods", None) or ("GET",) + if isinstance(methods, str): + raise TypeError( + "Allowed methods must be a list of strings, for" + ' example: @app.route(..., methods=["POST"])' + ) + methods = {item.upper() for item in methods} + + # Methods that should always be added + required_methods: set[str] = set(getattr(view_func, "required_methods", ())) + + # starting with Flask 0.8 the view_func object can disable and + # force-enable the automatic options handling. + if provide_automatic_options is None: + provide_automatic_options = getattr( + view_func, "provide_automatic_options", None + ) + + if provide_automatic_options is None: + if "OPTIONS" not in methods and self.config["PROVIDE_AUTOMATIC_OPTIONS"]: + provide_automatic_options = True + required_methods.add("OPTIONS") + else: + provide_automatic_options = False + + # Add the required methods now. + methods |= required_methods + + rule_obj = self.url_rule_class(rule, methods=methods, **options) + rule_obj.provide_automatic_options = provide_automatic_options # type: ignore[attr-defined] + + self.url_map.add(rule_obj) + if view_func is not None: + old_func = self.view_functions.get(endpoint) + if old_func is not None and old_func != view_func: + raise AssertionError( + "View function mapping is overwriting an existing" + f" endpoint function: {endpoint}" + ) + self.view_functions[endpoint] = view_func + + @setupmethod + def template_filter( + self, name: str | None = None + ) -> t.Callable[[T_template_filter], T_template_filter]: + """A decorator that is used to register custom template filter. + You can specify a name for the filter, otherwise the function + name will be used. Example:: + + @app.template_filter() + def reverse(s): + return s[::-1] + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + + def decorator(f: T_template_filter) -> T_template_filter: + self.add_template_filter(f, name=name) + return f + + return decorator + + @setupmethod + def add_template_filter( + self, f: ft.TemplateFilterCallable, name: str | None = None + ) -> None: + """Register a custom template filter. Works exactly like the + :meth:`template_filter` decorator. + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + self.jinja_env.filters[name or f.__name__] = f + + @setupmethod + def template_test( + self, name: str | None = None + ) -> t.Callable[[T_template_test], T_template_test]: + """A decorator that is used to register custom template test. + You can specify a name for the test, otherwise the function + name will be used. Example:: + + @app.template_test() + def is_prime(n): + if n == 2: + return True + for i in range(2, int(math.ceil(math.sqrt(n))) + 1): + if n % i == 0: + return False + return True + + .. versionadded:: 0.10 + + :param name: the optional name of the test, otherwise the + function name will be used. + """ + + def decorator(f: T_template_test) -> T_template_test: + self.add_template_test(f, name=name) + return f + + return decorator + + @setupmethod + def add_template_test( + self, f: ft.TemplateTestCallable, name: str | None = None + ) -> None: + """Register a custom template test. Works exactly like the + :meth:`template_test` decorator. + + .. versionadded:: 0.10 + + :param name: the optional name of the test, otherwise the + function name will be used. + """ + self.jinja_env.tests[name or f.__name__] = f + + @setupmethod + def template_global( + self, name: str | None = None + ) -> t.Callable[[T_template_global], T_template_global]: + """A decorator that is used to register a custom template global function. + You can specify a name for the global function, otherwise the function + name will be used. Example:: + + @app.template_global() + def double(n): + return 2 * n + + .. versionadded:: 0.10 + + :param name: the optional name of the global function, otherwise the + function name will be used. + """ + + def decorator(f: T_template_global) -> T_template_global: + self.add_template_global(f, name=name) + return f + + return decorator + + @setupmethod + def add_template_global( + self, f: ft.TemplateGlobalCallable, name: str | None = None + ) -> None: + """Register a custom template global function. Works exactly like the + :meth:`template_global` decorator. + + .. versionadded:: 0.10 + + :param name: the optional name of the global function, otherwise the + function name will be used. + """ + self.jinja_env.globals[name or f.__name__] = f + + @setupmethod + def teardown_appcontext(self, f: T_teardown) -> T_teardown: + """Registers a function to be called when the application + context is popped. The application context is typically popped + after the request context for each request, at the end of CLI + commands, or after a manually pushed context ends. + + .. code-block:: python + + with app.app_context(): + ... + + When the ``with`` block exits (or ``ctx.pop()`` is called), the + teardown functions are called just before the app context is + made inactive. Since a request context typically also manages an + application context it would also be called when you pop a + request context. + + When a teardown function was called because of an unhandled + exception it will be passed an error object. If an + :meth:`errorhandler` is registered, it will handle the exception + and the teardown will not receive it. + + Teardown functions must avoid raising exceptions. If they + execute code that might fail they must surround that code with a + ``try``/``except`` block and log any errors. + + The return values of teardown functions are ignored. + + .. versionadded:: 0.9 + """ + self.teardown_appcontext_funcs.append(f) + return f + + @setupmethod + def shell_context_processor( + self, f: T_shell_context_processor + ) -> T_shell_context_processor: + """Registers a shell context processor function. + + .. versionadded:: 0.11 + """ + self.shell_context_processors.append(f) + return f + + def _find_error_handler( + self, e: Exception, blueprints: list[str] + ) -> ft.ErrorHandlerCallable | None: + """Return a registered error handler for an exception in this order: + blueprint handler for a specific code, app handler for a specific code, + blueprint handler for an exception class, app handler for an exception + class, or ``None`` if a suitable handler is not found. + """ + exc_class, code = self._get_exc_class_and_code(type(e)) + names = (*blueprints, None) + + for c in (code, None) if code is not None else (None,): + for name in names: + handler_map = self.error_handler_spec[name][c] + + if not handler_map: + continue + + for cls in exc_class.__mro__: + handler = handler_map.get(cls) + + if handler is not None: + return handler + return None + + def trap_http_exception(self, e: Exception) -> bool: + """Checks if an HTTP exception should be trapped or not. By default + this will return ``False`` for all exceptions except for a bad request + key error if ``TRAP_BAD_REQUEST_ERRORS`` is set to ``True``. It + also returns ``True`` if ``TRAP_HTTP_EXCEPTIONS`` is set to ``True``. + + This is called for all HTTP exceptions raised by a view function. + If it returns ``True`` for any exception the error handler for this + exception is not called and it shows up as regular exception in the + traceback. This is helpful for debugging implicitly raised HTTP + exceptions. + + .. versionchanged:: 1.0 + Bad request errors are not trapped by default in debug mode. + + .. versionadded:: 0.8 + """ + if self.config["TRAP_HTTP_EXCEPTIONS"]: + return True + + trap_bad_request = self.config["TRAP_BAD_REQUEST_ERRORS"] + + # if unset, trap key errors in debug mode + if ( + trap_bad_request is None + and self.debug + and isinstance(e, BadRequestKeyError) + ): + return True + + if trap_bad_request: + return isinstance(e, BadRequest) + + return False + + def should_ignore_error(self, error: BaseException | None) -> bool: + """This is called to figure out if an error should be ignored + or not as far as the teardown system is concerned. If this + function returns ``True`` then the teardown handlers will not be + passed the error. + + .. versionadded:: 0.10 + """ + return False + + def redirect(self, location: str, code: int = 302) -> BaseResponse: + """Create a redirect response object. + + This is called by :func:`flask.redirect`, and can be called + directly as well. + + :param location: The URL to redirect to. + :param code: The status code for the redirect. + + .. versionadded:: 2.2 + Moved from ``flask.redirect``, which calls this method. + """ + return _wz_redirect( + location, + code=code, + Response=self.response_class, # type: ignore[arg-type] + ) + + def inject_url_defaults(self, endpoint: str, values: dict[str, t.Any]) -> None: + """Injects the URL defaults for the given endpoint directly into + the values dictionary passed. This is used internally and + automatically called on URL building. + + .. versionadded:: 0.7 + """ + names: t.Iterable[str | None] = (None,) + + # url_for may be called outside a request context, parse the + # passed endpoint instead of using request.blueprints. + if "." in endpoint: + names = chain( + names, reversed(_split_blueprint_path(endpoint.rpartition(".")[0])) + ) + + for name in names: + if name in self.url_default_functions: + for func in self.url_default_functions[name]: + func(endpoint, values) + + def handle_url_build_error( + self, error: BuildError, endpoint: str, values: dict[str, t.Any] + ) -> str: + """Called by :meth:`.url_for` if a + :exc:`~werkzeug.routing.BuildError` was raised. If this returns + a value, it will be returned by ``url_for``, otherwise the error + will be re-raised. + + Each function in :attr:`url_build_error_handlers` is called with + ``error``, ``endpoint`` and ``values``. If a function returns + ``None`` or raises a ``BuildError``, it is skipped. Otherwise, + its return value is returned by ``url_for``. + + :param error: The active ``BuildError`` being handled. + :param endpoint: The endpoint being built. + :param values: The keyword arguments passed to ``url_for``. + """ + for handler in self.url_build_error_handlers: + try: + rv = handler(error, endpoint, values) + except BuildError as e: + # make error available outside except block + error = e + else: + if rv is not None: + return rv + + # Re-raise if called with an active exception, otherwise raise + # the passed in exception. + if error is sys.exc_info()[1]: + raise + + raise error diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/blueprints.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/blueprints.py new file mode 100644 index 0000000000..4f912cca05 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/blueprints.py @@ -0,0 +1,632 @@ +from __future__ import annotations + +import os +import typing as t +from collections import defaultdict +from functools import update_wrapper + +from .. import typing as ft +from .scaffold import _endpoint_from_view_func +from .scaffold import _sentinel +from .scaffold import Scaffold +from .scaffold import setupmethod + +if t.TYPE_CHECKING: # pragma: no cover + from .app import App + +DeferredSetupFunction = t.Callable[["BlueprintSetupState"], None] +T_after_request = t.TypeVar("T_after_request", bound=ft.AfterRequestCallable[t.Any]) +T_before_request = t.TypeVar("T_before_request", bound=ft.BeforeRequestCallable) +T_error_handler = t.TypeVar("T_error_handler", bound=ft.ErrorHandlerCallable) +T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable) +T_template_context_processor = t.TypeVar( + "T_template_context_processor", bound=ft.TemplateContextProcessorCallable +) +T_template_filter = t.TypeVar("T_template_filter", bound=ft.TemplateFilterCallable) +T_template_global = t.TypeVar("T_template_global", bound=ft.TemplateGlobalCallable) +T_template_test = t.TypeVar("T_template_test", bound=ft.TemplateTestCallable) +T_url_defaults = t.TypeVar("T_url_defaults", bound=ft.URLDefaultCallable) +T_url_value_preprocessor = t.TypeVar( + "T_url_value_preprocessor", bound=ft.URLValuePreprocessorCallable +) + + +class BlueprintSetupState: + """Temporary holder object for registering a blueprint with the + application. An instance of this class is created by the + :meth:`~flask.Blueprint.make_setup_state` method and later passed + to all register callback functions. + """ + + def __init__( + self, + blueprint: Blueprint, + app: App, + options: t.Any, + first_registration: bool, + ) -> None: + #: a reference to the current application + self.app = app + + #: a reference to the blueprint that created this setup state. + self.blueprint = blueprint + + #: a dictionary with all options that were passed to the + #: :meth:`~flask.Flask.register_blueprint` method. + self.options = options + + #: as blueprints can be registered multiple times with the + #: application and not everything wants to be registered + #: multiple times on it, this attribute can be used to figure + #: out if the blueprint was registered in the past already. + self.first_registration = first_registration + + subdomain = self.options.get("subdomain") + if subdomain is None: + subdomain = self.blueprint.subdomain + + #: The subdomain that the blueprint should be active for, ``None`` + #: otherwise. + self.subdomain = subdomain + + url_prefix = self.options.get("url_prefix") + if url_prefix is None: + url_prefix = self.blueprint.url_prefix + #: The prefix that should be used for all URLs defined on the + #: blueprint. + self.url_prefix = url_prefix + + self.name = self.options.get("name", blueprint.name) + self.name_prefix = self.options.get("name_prefix", "") + + #: A dictionary with URL defaults that is added to each and every + #: URL that was defined with the blueprint. + self.url_defaults = dict(self.blueprint.url_values_defaults) + self.url_defaults.update(self.options.get("url_defaults", ())) + + def add_url_rule( + self, + rule: str, + endpoint: str | None = None, + view_func: ft.RouteCallable | None = None, + **options: t.Any, + ) -> None: + """A helper method to register a rule (and optionally a view function) + to the application. The endpoint is automatically prefixed with the + blueprint's name. + """ + if self.url_prefix is not None: + if rule: + rule = "/".join((self.url_prefix.rstrip("/"), rule.lstrip("/"))) + else: + rule = self.url_prefix + options.setdefault("subdomain", self.subdomain) + if endpoint is None: + endpoint = _endpoint_from_view_func(view_func) # type: ignore + defaults = self.url_defaults + if "defaults" in options: + defaults = dict(defaults, **options.pop("defaults")) + + self.app.add_url_rule( + rule, + f"{self.name_prefix}.{self.name}.{endpoint}".lstrip("."), + view_func, + defaults=defaults, + **options, + ) + + +class Blueprint(Scaffold): + """Represents a blueprint, a collection of routes and other + app-related functions that can be registered on a real application + later. + + A blueprint is an object that allows defining application functions + without requiring an application object ahead of time. It uses the + same decorators as :class:`~flask.Flask`, but defers the need for an + application by recording them for later registration. + + Decorating a function with a blueprint creates a deferred function + that is called with :class:`~flask.blueprints.BlueprintSetupState` + when the blueprint is registered on an application. + + See :doc:`/blueprints` for more information. + + :param name: The name of the blueprint. Will be prepended to each + endpoint name. + :param import_name: The name of the blueprint package, usually + ``__name__``. This helps locate the ``root_path`` for the + blueprint. + :param static_folder: A folder with static files that should be + served by the blueprint's static route. The path is relative to + the blueprint's root path. Blueprint static files are disabled + by default. + :param static_url_path: The url to serve static files from. + Defaults to ``static_folder``. If the blueprint does not have + a ``url_prefix``, the app's static route will take precedence, + and the blueprint's static files won't be accessible. + :param template_folder: A folder with templates that should be added + to the app's template search path. The path is relative to the + blueprint's root path. Blueprint templates are disabled by + default. Blueprint templates have a lower precedence than those + in the app's templates folder. + :param url_prefix: A path to prepend to all of the blueprint's URLs, + to make them distinct from the rest of the app's routes. + :param subdomain: A subdomain that blueprint routes will match on by + default. + :param url_defaults: A dict of default values that blueprint routes + will receive by default. + :param root_path: By default, the blueprint will automatically set + this based on ``import_name``. In certain situations this + automatic detection can fail, so the path can be specified + manually instead. + + .. versionchanged:: 1.1.0 + Blueprints have a ``cli`` group to register nested CLI commands. + The ``cli_group`` parameter controls the name of the group under + the ``flask`` command. + + .. versionadded:: 0.7 + """ + + _got_registered_once = False + + def __init__( + self, + name: str, + import_name: str, + static_folder: str | os.PathLike[str] | None = None, + static_url_path: str | None = None, + template_folder: str | os.PathLike[str] | None = None, + url_prefix: str | None = None, + subdomain: str | None = None, + url_defaults: dict[str, t.Any] | None = None, + root_path: str | None = None, + cli_group: str | None = _sentinel, # type: ignore[assignment] + ): + super().__init__( + import_name=import_name, + static_folder=static_folder, + static_url_path=static_url_path, + template_folder=template_folder, + root_path=root_path, + ) + + if not name: + raise ValueError("'name' may not be empty.") + + if "." in name: + raise ValueError("'name' may not contain a dot '.' character.") + + self.name = name + self.url_prefix = url_prefix + self.subdomain = subdomain + self.deferred_functions: list[DeferredSetupFunction] = [] + + if url_defaults is None: + url_defaults = {} + + self.url_values_defaults = url_defaults + self.cli_group = cli_group + self._blueprints: list[tuple[Blueprint, dict[str, t.Any]]] = [] + + def _check_setup_finished(self, f_name: str) -> None: + if self._got_registered_once: + raise AssertionError( + f"The setup method '{f_name}' can no longer be called on the blueprint" + f" '{self.name}'. It has already been registered at least once, any" + " changes will not be applied consistently.\n" + "Make sure all imports, decorators, functions, etc. needed to set up" + " the blueprint are done before registering it." + ) + + @setupmethod + def record(self, func: DeferredSetupFunction) -> None: + """Registers a function that is called when the blueprint is + registered on the application. This function is called with the + state as argument as returned by the :meth:`make_setup_state` + method. + """ + self.deferred_functions.append(func) + + @setupmethod + def record_once(self, func: DeferredSetupFunction) -> None: + """Works like :meth:`record` but wraps the function in another + function that will ensure the function is only called once. If the + blueprint is registered a second time on the application, the + function passed is not called. + """ + + def wrapper(state: BlueprintSetupState) -> None: + if state.first_registration: + func(state) + + self.record(update_wrapper(wrapper, func)) + + def make_setup_state( + self, app: App, options: dict[str, t.Any], first_registration: bool = False + ) -> BlueprintSetupState: + """Creates an instance of :meth:`~flask.blueprints.BlueprintSetupState` + object that is later passed to the register callback functions. + Subclasses can override this to return a subclass of the setup state. + """ + return BlueprintSetupState(self, app, options, first_registration) + + @setupmethod + def register_blueprint(self, blueprint: Blueprint, **options: t.Any) -> None: + """Register a :class:`~flask.Blueprint` on this blueprint. Keyword + arguments passed to this method will override the defaults set + on the blueprint. + + .. versionchanged:: 2.0.1 + The ``name`` option can be used to change the (pre-dotted) + name the blueprint is registered with. This allows the same + blueprint to be registered multiple times with unique names + for ``url_for``. + + .. versionadded:: 2.0 + """ + if blueprint is self: + raise ValueError("Cannot register a blueprint on itself") + self._blueprints.append((blueprint, options)) + + def register(self, app: App, options: dict[str, t.Any]) -> None: + """Called by :meth:`Flask.register_blueprint` to register all + views and callbacks registered on the blueprint with the + application. Creates a :class:`.BlueprintSetupState` and calls + each :meth:`record` callback with it. + + :param app: The application this blueprint is being registered + with. + :param options: Keyword arguments forwarded from + :meth:`~Flask.register_blueprint`. + + .. versionchanged:: 2.3 + Nested blueprints now correctly apply subdomains. + + .. versionchanged:: 2.1 + Registering the same blueprint with the same name multiple + times is an error. + + .. versionchanged:: 2.0.1 + Nested blueprints are registered with their dotted name. + This allows different blueprints with the same name to be + nested at different locations. + + .. versionchanged:: 2.0.1 + The ``name`` option can be used to change the (pre-dotted) + name the blueprint is registered with. This allows the same + blueprint to be registered multiple times with unique names + for ``url_for``. + """ + name_prefix = options.get("name_prefix", "") + self_name = options.get("name", self.name) + name = f"{name_prefix}.{self_name}".lstrip(".") + + if name in app.blueprints: + bp_desc = "this" if app.blueprints[name] is self else "a different" + existing_at = f" '{name}'" if self_name != name else "" + + raise ValueError( + f"The name '{self_name}' is already registered for" + f" {bp_desc} blueprint{existing_at}. Use 'name=' to" + f" provide a unique name." + ) + + first_bp_registration = not any(bp is self for bp in app.blueprints.values()) + first_name_registration = name not in app.blueprints + + app.blueprints[name] = self + self._got_registered_once = True + state = self.make_setup_state(app, options, first_bp_registration) + + if self.has_static_folder: + state.add_url_rule( + f"{self.static_url_path}/", + view_func=self.send_static_file, # type: ignore[attr-defined] + endpoint="static", + ) + + # Merge blueprint data into parent. + if first_bp_registration or first_name_registration: + self._merge_blueprint_funcs(app, name) + + for deferred in self.deferred_functions: + deferred(state) + + cli_resolved_group = options.get("cli_group", self.cli_group) + + if self.cli.commands: + if cli_resolved_group is None: + app.cli.commands.update(self.cli.commands) + elif cli_resolved_group is _sentinel: + self.cli.name = name + app.cli.add_command(self.cli) + else: + self.cli.name = cli_resolved_group + app.cli.add_command(self.cli) + + for blueprint, bp_options in self._blueprints: + bp_options = bp_options.copy() + bp_url_prefix = bp_options.get("url_prefix") + bp_subdomain = bp_options.get("subdomain") + + if bp_subdomain is None: + bp_subdomain = blueprint.subdomain + + if state.subdomain is not None and bp_subdomain is not None: + bp_options["subdomain"] = bp_subdomain + "." + state.subdomain + elif bp_subdomain is not None: + bp_options["subdomain"] = bp_subdomain + elif state.subdomain is not None: + bp_options["subdomain"] = state.subdomain + + if bp_url_prefix is None: + bp_url_prefix = blueprint.url_prefix + + if state.url_prefix is not None and bp_url_prefix is not None: + bp_options["url_prefix"] = ( + state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/") + ) + elif bp_url_prefix is not None: + bp_options["url_prefix"] = bp_url_prefix + elif state.url_prefix is not None: + bp_options["url_prefix"] = state.url_prefix + + bp_options["name_prefix"] = name + blueprint.register(app, bp_options) + + def _merge_blueprint_funcs(self, app: App, name: str) -> None: + def extend( + bp_dict: dict[ft.AppOrBlueprintKey, list[t.Any]], + parent_dict: dict[ft.AppOrBlueprintKey, list[t.Any]], + ) -> None: + for key, values in bp_dict.items(): + key = name if key is None else f"{name}.{key}" + parent_dict[key].extend(values) + + for key, value in self.error_handler_spec.items(): + key = name if key is None else f"{name}.{key}" + value = defaultdict( + dict, + { + code: {exc_class: func for exc_class, func in code_values.items()} + for code, code_values in value.items() + }, + ) + app.error_handler_spec[key] = value + + for endpoint, func in self.view_functions.items(): + app.view_functions[endpoint] = func + + extend(self.before_request_funcs, app.before_request_funcs) + extend(self.after_request_funcs, app.after_request_funcs) + extend( + self.teardown_request_funcs, + app.teardown_request_funcs, + ) + extend(self.url_default_functions, app.url_default_functions) + extend(self.url_value_preprocessors, app.url_value_preprocessors) + extend(self.template_context_processors, app.template_context_processors) + + @setupmethod + def add_url_rule( + self, + rule: str, + endpoint: str | None = None, + view_func: ft.RouteCallable | None = None, + provide_automatic_options: bool | None = None, + **options: t.Any, + ) -> None: + """Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for + full documentation. + + The URL rule is prefixed with the blueprint's URL prefix. The endpoint name, + used with :func:`url_for`, is prefixed with the blueprint's name. + """ + if endpoint and "." in endpoint: + raise ValueError("'endpoint' may not contain a dot '.' character.") + + if view_func and hasattr(view_func, "__name__") and "." in view_func.__name__: + raise ValueError("'view_func' name may not contain a dot '.' character.") + + self.record( + lambda s: s.add_url_rule( + rule, + endpoint, + view_func, + provide_automatic_options=provide_automatic_options, + **options, + ) + ) + + @setupmethod + def app_template_filter( + self, name: str | None = None + ) -> t.Callable[[T_template_filter], T_template_filter]: + """Register a template filter, available in any template rendered by the + application. Equivalent to :meth:`.Flask.template_filter`. + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + + def decorator(f: T_template_filter) -> T_template_filter: + self.add_app_template_filter(f, name=name) + return f + + return decorator + + @setupmethod + def add_app_template_filter( + self, f: ft.TemplateFilterCallable, name: str | None = None + ) -> None: + """Register a template filter, available in any template rendered by the + application. Works like the :meth:`app_template_filter` decorator. Equivalent to + :meth:`.Flask.add_template_filter`. + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + + def register_template(state: BlueprintSetupState) -> None: + state.app.jinja_env.filters[name or f.__name__] = f + + self.record_once(register_template) + + @setupmethod + def app_template_test( + self, name: str | None = None + ) -> t.Callable[[T_template_test], T_template_test]: + """Register a template test, available in any template rendered by the + application. Equivalent to :meth:`.Flask.template_test`. + + .. versionadded:: 0.10 + + :param name: the optional name of the test, otherwise the + function name will be used. + """ + + def decorator(f: T_template_test) -> T_template_test: + self.add_app_template_test(f, name=name) + return f + + return decorator + + @setupmethod + def add_app_template_test( + self, f: ft.TemplateTestCallable, name: str | None = None + ) -> None: + """Register a template test, available in any template rendered by the + application. Works like the :meth:`app_template_test` decorator. Equivalent to + :meth:`.Flask.add_template_test`. + + .. versionadded:: 0.10 + + :param name: the optional name of the test, otherwise the + function name will be used. + """ + + def register_template(state: BlueprintSetupState) -> None: + state.app.jinja_env.tests[name or f.__name__] = f + + self.record_once(register_template) + + @setupmethod + def app_template_global( + self, name: str | None = None + ) -> t.Callable[[T_template_global], T_template_global]: + """Register a template global, available in any template rendered by the + application. Equivalent to :meth:`.Flask.template_global`. + + .. versionadded:: 0.10 + + :param name: the optional name of the global, otherwise the + function name will be used. + """ + + def decorator(f: T_template_global) -> T_template_global: + self.add_app_template_global(f, name=name) + return f + + return decorator + + @setupmethod + def add_app_template_global( + self, f: ft.TemplateGlobalCallable, name: str | None = None + ) -> None: + """Register a template global, available in any template rendered by the + application. Works like the :meth:`app_template_global` decorator. Equivalent to + :meth:`.Flask.add_template_global`. + + .. versionadded:: 0.10 + + :param name: the optional name of the global, otherwise the + function name will be used. + """ + + def register_template(state: BlueprintSetupState) -> None: + state.app.jinja_env.globals[name or f.__name__] = f + + self.record_once(register_template) + + @setupmethod + def before_app_request(self, f: T_before_request) -> T_before_request: + """Like :meth:`before_request`, but before every request, not only those handled + by the blueprint. Equivalent to :meth:`.Flask.before_request`. + """ + self.record_once( + lambda s: s.app.before_request_funcs.setdefault(None, []).append(f) + ) + return f + + @setupmethod + def after_app_request(self, f: T_after_request) -> T_after_request: + """Like :meth:`after_request`, but after every request, not only those handled + by the blueprint. Equivalent to :meth:`.Flask.after_request`. + """ + self.record_once( + lambda s: s.app.after_request_funcs.setdefault(None, []).append(f) + ) + return f + + @setupmethod + def teardown_app_request(self, f: T_teardown) -> T_teardown: + """Like :meth:`teardown_request`, but after every request, not only those + handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`. + """ + self.record_once( + lambda s: s.app.teardown_request_funcs.setdefault(None, []).append(f) + ) + return f + + @setupmethod + def app_context_processor( + self, f: T_template_context_processor + ) -> T_template_context_processor: + """Like :meth:`context_processor`, but for templates rendered by every view, not + only by the blueprint. Equivalent to :meth:`.Flask.context_processor`. + """ + self.record_once( + lambda s: s.app.template_context_processors.setdefault(None, []).append(f) + ) + return f + + @setupmethod + def app_errorhandler( + self, code: type[Exception] | int + ) -> t.Callable[[T_error_handler], T_error_handler]: + """Like :meth:`errorhandler`, but for every request, not only those handled by + the blueprint. Equivalent to :meth:`.Flask.errorhandler`. + """ + + def decorator(f: T_error_handler) -> T_error_handler: + def from_blueprint(state: BlueprintSetupState) -> None: + state.app.errorhandler(code)(f) + + self.record_once(from_blueprint) + return f + + return decorator + + @setupmethod + def app_url_value_preprocessor( + self, f: T_url_value_preprocessor + ) -> T_url_value_preprocessor: + """Like :meth:`url_value_preprocessor`, but for every request, not only those + handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`. + """ + self.record_once( + lambda s: s.app.url_value_preprocessors.setdefault(None, []).append(f) + ) + return f + + @setupmethod + def app_url_defaults(self, f: T_url_defaults) -> T_url_defaults: + """Like :meth:`url_defaults`, but for every request, not only those handled by + the blueprint. Equivalent to :meth:`.Flask.url_defaults`. + """ + self.record_once( + lambda s: s.app.url_default_functions.setdefault(None, []).append(f) + ) + return f diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/scaffold.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/scaffold.py new file mode 100644 index 0000000000..0e96f15b74 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sansio/scaffold.py @@ -0,0 +1,792 @@ +from __future__ import annotations + +import importlib.util +import os +import pathlib +import sys +import typing as t +from collections import defaultdict +from functools import update_wrapper + +from jinja2 import BaseLoader +from jinja2 import FileSystemLoader +from werkzeug.exceptions import default_exceptions +from werkzeug.exceptions import HTTPException +from werkzeug.utils import cached_property + +from .. import typing as ft +from ..helpers import get_root_path +from ..templating import _default_template_ctx_processor + +if t.TYPE_CHECKING: # pragma: no cover + from click import Group + +# a singleton sentinel value for parameter defaults +_sentinel = object() + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) +T_after_request = t.TypeVar("T_after_request", bound=ft.AfterRequestCallable[t.Any]) +T_before_request = t.TypeVar("T_before_request", bound=ft.BeforeRequestCallable) +T_error_handler = t.TypeVar("T_error_handler", bound=ft.ErrorHandlerCallable) +T_teardown = t.TypeVar("T_teardown", bound=ft.TeardownCallable) +T_template_context_processor = t.TypeVar( + "T_template_context_processor", bound=ft.TemplateContextProcessorCallable +) +T_url_defaults = t.TypeVar("T_url_defaults", bound=ft.URLDefaultCallable) +T_url_value_preprocessor = t.TypeVar( + "T_url_value_preprocessor", bound=ft.URLValuePreprocessorCallable +) +T_route = t.TypeVar("T_route", bound=ft.RouteCallable) + + +def setupmethod(f: F) -> F: + f_name = f.__name__ + + def wrapper_func(self: Scaffold, *args: t.Any, **kwargs: t.Any) -> t.Any: + self._check_setup_finished(f_name) + return f(self, *args, **kwargs) + + return t.cast(F, update_wrapper(wrapper_func, f)) + + +class Scaffold: + """Common behavior shared between :class:`~flask.Flask` and + :class:`~flask.blueprints.Blueprint`. + + :param import_name: The import name of the module where this object + is defined. Usually :attr:`__name__` should be used. + :param static_folder: Path to a folder of static files to serve. + If this is set, a static route will be added. + :param static_url_path: URL prefix for the static route. + :param template_folder: Path to a folder containing template files. + for rendering. If this is set, a Jinja loader will be added. + :param root_path: The path that static, template, and resource files + are relative to. Typically not set, it is discovered based on + the ``import_name``. + + .. versionadded:: 2.0 + """ + + cli: Group + name: str + _static_folder: str | None = None + _static_url_path: str | None = None + + def __init__( + self, + import_name: str, + static_folder: str | os.PathLike[str] | None = None, + static_url_path: str | None = None, + template_folder: str | os.PathLike[str] | None = None, + root_path: str | None = None, + ): + #: The name of the package or module that this object belongs + #: to. Do not change this once it is set by the constructor. + self.import_name = import_name + + self.static_folder = static_folder + self.static_url_path = static_url_path + + #: The path to the templates folder, relative to + #: :attr:`root_path`, to add to the template loader. ``None`` if + #: templates should not be added. + self.template_folder = template_folder + + if root_path is None: + root_path = get_root_path(self.import_name) + + #: Absolute path to the package on the filesystem. Used to look + #: up resources contained in the package. + self.root_path = root_path + + #: A dictionary mapping endpoint names to view functions. + #: + #: To register a view function, use the :meth:`route` decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.view_functions: dict[str, ft.RouteCallable] = {} + + #: A data structure of registered error handlers, in the format + #: ``{scope: {code: {class: handler}}}``. The ``scope`` key is + #: the name of a blueprint the handlers are active for, or + #: ``None`` for all requests. The ``code`` key is the HTTP + #: status code for ``HTTPException``, or ``None`` for + #: other exceptions. The innermost dictionary maps exception + #: classes to handler functions. + #: + #: To register an error handler, use the :meth:`errorhandler` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.error_handler_spec: dict[ + ft.AppOrBlueprintKey, + dict[int | None, dict[type[Exception], ft.ErrorHandlerCallable]], + ] = defaultdict(lambda: defaultdict(dict)) + + #: A data structure of functions to call at the beginning of + #: each request, in the format ``{scope: [functions]}``. The + #: ``scope`` key is the name of a blueprint the functions are + #: active for, or ``None`` for all requests. + #: + #: To register a function, use the :meth:`before_request` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.before_request_funcs: dict[ + ft.AppOrBlueprintKey, list[ft.BeforeRequestCallable] + ] = defaultdict(list) + + #: A data structure of functions to call at the end of each + #: request, in the format ``{scope: [functions]}``. The + #: ``scope`` key is the name of a blueprint the functions are + #: active for, or ``None`` for all requests. + #: + #: To register a function, use the :meth:`after_request` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.after_request_funcs: dict[ + ft.AppOrBlueprintKey, list[ft.AfterRequestCallable[t.Any]] + ] = defaultdict(list) + + #: A data structure of functions to call at the end of each + #: request even if an exception is raised, in the format + #: ``{scope: [functions]}``. The ``scope`` key is the name of a + #: blueprint the functions are active for, or ``None`` for all + #: requests. + #: + #: To register a function, use the :meth:`teardown_request` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.teardown_request_funcs: dict[ + ft.AppOrBlueprintKey, list[ft.TeardownCallable] + ] = defaultdict(list) + + #: A data structure of functions to call to pass extra context + #: values when rendering templates, in the format + #: ``{scope: [functions]}``. The ``scope`` key is the name of a + #: blueprint the functions are active for, or ``None`` for all + #: requests. + #: + #: To register a function, use the :meth:`context_processor` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.template_context_processors: dict[ + ft.AppOrBlueprintKey, list[ft.TemplateContextProcessorCallable] + ] = defaultdict(list, {None: [_default_template_ctx_processor]}) + + #: A data structure of functions to call to modify the keyword + #: arguments passed to the view function, in the format + #: ``{scope: [functions]}``. The ``scope`` key is the name of a + #: blueprint the functions are active for, or ``None`` for all + #: requests. + #: + #: To register a function, use the + #: :meth:`url_value_preprocessor` decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.url_value_preprocessors: dict[ + ft.AppOrBlueprintKey, + list[ft.URLValuePreprocessorCallable], + ] = defaultdict(list) + + #: A data structure of functions to call to modify the keyword + #: arguments when generating URLs, in the format + #: ``{scope: [functions]}``. The ``scope`` key is the name of a + #: blueprint the functions are active for, or ``None`` for all + #: requests. + #: + #: To register a function, use the :meth:`url_defaults` + #: decorator. + #: + #: This data structure is internal. It should not be modified + #: directly and its format may change at any time. + self.url_default_functions: dict[ + ft.AppOrBlueprintKey, list[ft.URLDefaultCallable] + ] = defaultdict(list) + + def __repr__(self) -> str: + return f"<{type(self).__name__} {self.name!r}>" + + def _check_setup_finished(self, f_name: str) -> None: + raise NotImplementedError + + @property + def static_folder(self) -> str | None: + """The absolute path to the configured static folder. ``None`` + if no static folder is set. + """ + if self._static_folder is not None: + return os.path.join(self.root_path, self._static_folder) + else: + return None + + @static_folder.setter + def static_folder(self, value: str | os.PathLike[str] | None) -> None: + if value is not None: + value = os.fspath(value).rstrip(r"\/") + + self._static_folder = value + + @property + def has_static_folder(self) -> bool: + """``True`` if :attr:`static_folder` is set. + + .. versionadded:: 0.5 + """ + return self.static_folder is not None + + @property + def static_url_path(self) -> str | None: + """The URL prefix that the static route will be accessible from. + + If it was not configured during init, it is derived from + :attr:`static_folder`. + """ + if self._static_url_path is not None: + return self._static_url_path + + if self.static_folder is not None: + basename = os.path.basename(self.static_folder) + return f"/{basename}".rstrip("/") + + return None + + @static_url_path.setter + def static_url_path(self, value: str | None) -> None: + if value is not None: + value = value.rstrip("/") + + self._static_url_path = value + + @cached_property + def jinja_loader(self) -> BaseLoader | None: + """The Jinja loader for this object's templates. By default this + is a class :class:`jinja2.loaders.FileSystemLoader` to + :attr:`template_folder` if it is set. + + .. versionadded:: 0.5 + """ + if self.template_folder is not None: + return FileSystemLoader(os.path.join(self.root_path, self.template_folder)) + else: + return None + + def _method_route( + self, + method: str, + rule: str, + options: dict[str, t.Any], + ) -> t.Callable[[T_route], T_route]: + if "methods" in options: + raise TypeError("Use the 'route' decorator to use the 'methods' argument.") + + return self.route(rule, methods=[method], **options) + + @setupmethod + def get(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Shortcut for :meth:`route` with ``methods=["GET"]``. + + .. versionadded:: 2.0 + """ + return self._method_route("GET", rule, options) + + @setupmethod + def post(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Shortcut for :meth:`route` with ``methods=["POST"]``. + + .. versionadded:: 2.0 + """ + return self._method_route("POST", rule, options) + + @setupmethod + def put(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Shortcut for :meth:`route` with ``methods=["PUT"]``. + + .. versionadded:: 2.0 + """ + return self._method_route("PUT", rule, options) + + @setupmethod + def delete(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Shortcut for :meth:`route` with ``methods=["DELETE"]``. + + .. versionadded:: 2.0 + """ + return self._method_route("DELETE", rule, options) + + @setupmethod + def patch(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Shortcut for :meth:`route` with ``methods=["PATCH"]``. + + .. versionadded:: 2.0 + """ + return self._method_route("PATCH", rule, options) + + @setupmethod + def route(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]: + """Decorate a view function to register it with the given URL + rule and options. Calls :meth:`add_url_rule`, which has more + details about the implementation. + + .. code-block:: python + + @app.route("/") + def index(): + return "Hello, World!" + + See :ref:`url-route-registrations`. + + The endpoint name for the route defaults to the name of the view + function if the ``endpoint`` parameter isn't passed. + + The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` and + ``OPTIONS`` are added automatically. + + :param rule: The URL rule string. + :param options: Extra options passed to the + :class:`~werkzeug.routing.Rule` object. + """ + + def decorator(f: T_route) -> T_route: + endpoint = options.pop("endpoint", None) + self.add_url_rule(rule, endpoint, f, **options) + return f + + return decorator + + @setupmethod + def add_url_rule( + self, + rule: str, + endpoint: str | None = None, + view_func: ft.RouteCallable | None = None, + provide_automatic_options: bool | None = None, + **options: t.Any, + ) -> None: + """Register a rule for routing incoming requests and building + URLs. The :meth:`route` decorator is a shortcut to call this + with the ``view_func`` argument. These are equivalent: + + .. code-block:: python + + @app.route("/") + def index(): + ... + + .. code-block:: python + + def index(): + ... + + app.add_url_rule("/", view_func=index) + + See :ref:`url-route-registrations`. + + The endpoint name for the route defaults to the name of the view + function if the ``endpoint`` parameter isn't passed. An error + will be raised if a function has already been registered for the + endpoint. + + The ``methods`` parameter defaults to ``["GET"]``. ``HEAD`` is + always added automatically, and ``OPTIONS`` is added + automatically by default. + + ``view_func`` does not necessarily need to be passed, but if the + rule should participate in routing an endpoint name must be + associated with a view function at some point with the + :meth:`endpoint` decorator. + + .. code-block:: python + + app.add_url_rule("/", endpoint="index") + + @app.endpoint("index") + def index(): + ... + + If ``view_func`` has a ``required_methods`` attribute, those + methods are added to the passed and automatic methods. If it + has a ``provide_automatic_methods`` attribute, it is used as the + default if the parameter is not passed. + + :param rule: The URL rule string. + :param endpoint: The endpoint name to associate with the rule + and view function. Used when routing and building URLs. + Defaults to ``view_func.__name__``. + :param view_func: The view function to associate with the + endpoint name. + :param provide_automatic_options: Add the ``OPTIONS`` method and + respond to ``OPTIONS`` requests automatically. + :param options: Extra options passed to the + :class:`~werkzeug.routing.Rule` object. + """ + raise NotImplementedError + + @setupmethod + def endpoint(self, endpoint: str) -> t.Callable[[F], F]: + """Decorate a view function to register it for the given + endpoint. Used if a rule is added without a ``view_func`` with + :meth:`add_url_rule`. + + .. code-block:: python + + app.add_url_rule("/ex", endpoint="example") + + @app.endpoint("example") + def example(): + ... + + :param endpoint: The endpoint name to associate with the view + function. + """ + + def decorator(f: F) -> F: + self.view_functions[endpoint] = f + return f + + return decorator + + @setupmethod + def before_request(self, f: T_before_request) -> T_before_request: + """Register a function to run before each request. + + For example, this can be used to open a database connection, or + to load the logged in user from the session. + + .. code-block:: python + + @app.before_request + def load_user(): + if "user_id" in session: + g.user = db.session.get(session["user_id"]) + + The function will be called without any arguments. If it returns + a non-``None`` value, the value is handled as if it was the + return value from the view, and further request handling is + stopped. + + This is available on both app and blueprint objects. When used on an app, this + executes before every request. When used on a blueprint, this executes before + every request that the blueprint handles. To register with a blueprint and + execute before every request, use :meth:`.Blueprint.before_app_request`. + """ + self.before_request_funcs.setdefault(None, []).append(f) + return f + + @setupmethod + def after_request(self, f: T_after_request) -> T_after_request: + """Register a function to run after each request to this object. + + The function is called with the response object, and must return + a response object. This allows the functions to modify or + replace the response before it is sent. + + If a function raises an exception, any remaining + ``after_request`` functions will not be called. Therefore, this + should not be used for actions that must execute, such as to + close resources. Use :meth:`teardown_request` for that. + + This is available on both app and blueprint objects. When used on an app, this + executes after every request. When used on a blueprint, this executes after + every request that the blueprint handles. To register with a blueprint and + execute after every request, use :meth:`.Blueprint.after_app_request`. + """ + self.after_request_funcs.setdefault(None, []).append(f) + return f + + @setupmethod + def teardown_request(self, f: T_teardown) -> T_teardown: + """Register a function to be called when the request context is + popped. Typically this happens at the end of each request, but + contexts may be pushed manually as well during testing. + + .. code-block:: python + + with app.test_request_context(): + ... + + When the ``with`` block exits (or ``ctx.pop()`` is called), the + teardown functions are called just before the request context is + made inactive. + + When a teardown function was called because of an unhandled + exception it will be passed an error object. If an + :meth:`errorhandler` is registered, it will handle the exception + and the teardown will not receive it. + + Teardown functions must avoid raising exceptions. If they + execute code that might fail they must surround that code with a + ``try``/``except`` block and log any errors. + + The return values of teardown functions are ignored. + + This is available on both app and blueprint objects. When used on an app, this + executes after every request. When used on a blueprint, this executes after + every request that the blueprint handles. To register with a blueprint and + execute after every request, use :meth:`.Blueprint.teardown_app_request`. + """ + self.teardown_request_funcs.setdefault(None, []).append(f) + return f + + @setupmethod + def context_processor( + self, + f: T_template_context_processor, + ) -> T_template_context_processor: + """Registers a template context processor function. These functions run before + rendering a template. The keys of the returned dict are added as variables + available in the template. + + This is available on both app and blueprint objects. When used on an app, this + is called for every rendered template. When used on a blueprint, this is called + for templates rendered from the blueprint's views. To register with a blueprint + and affect every template, use :meth:`.Blueprint.app_context_processor`. + """ + self.template_context_processors[None].append(f) + return f + + @setupmethod + def url_value_preprocessor( + self, + f: T_url_value_preprocessor, + ) -> T_url_value_preprocessor: + """Register a URL value preprocessor function for all view + functions in the application. These functions will be called before the + :meth:`before_request` functions. + + The function can modify the values captured from the matched url before + they are passed to the view. For example, this can be used to pop a + common language code value and place it in ``g`` rather than pass it to + every view. + + The function is passed the endpoint name and values dict. The return + value is ignored. + + This is available on both app and blueprint objects. When used on an app, this + is called for every request. When used on a blueprint, this is called for + requests that the blueprint handles. To register with a blueprint and affect + every request, use :meth:`.Blueprint.app_url_value_preprocessor`. + """ + self.url_value_preprocessors[None].append(f) + return f + + @setupmethod + def url_defaults(self, f: T_url_defaults) -> T_url_defaults: + """Callback function for URL defaults for all view functions of the + application. It's called with the endpoint and values and should + update the values passed in place. + + This is available on both app and blueprint objects. When used on an app, this + is called for every request. When used on a blueprint, this is called for + requests that the blueprint handles. To register with a blueprint and affect + every request, use :meth:`.Blueprint.app_url_defaults`. + """ + self.url_default_functions[None].append(f) + return f + + @setupmethod + def errorhandler( + self, code_or_exception: type[Exception] | int + ) -> t.Callable[[T_error_handler], T_error_handler]: + """Register a function to handle errors by code or exception class. + + A decorator that is used to register a function given an + error code. Example:: + + @app.errorhandler(404) + def page_not_found(error): + return 'This page does not exist', 404 + + You can also register handlers for arbitrary exceptions:: + + @app.errorhandler(DatabaseError) + def special_exception_handler(error): + return 'Database connection failed', 500 + + This is available on both app and blueprint objects. When used on an app, this + can handle errors from every request. When used on a blueprint, this can handle + errors from requests that the blueprint handles. To register with a blueprint + and affect every request, use :meth:`.Blueprint.app_errorhandler`. + + .. versionadded:: 0.7 + Use :meth:`register_error_handler` instead of modifying + :attr:`error_handler_spec` directly, for application wide error + handlers. + + .. versionadded:: 0.7 + One can now additionally also register custom exception types + that do not necessarily have to be a subclass of the + :class:`~werkzeug.exceptions.HTTPException` class. + + :param code_or_exception: the code as integer for the handler, or + an arbitrary exception + """ + + def decorator(f: T_error_handler) -> T_error_handler: + self.register_error_handler(code_or_exception, f) + return f + + return decorator + + @setupmethod + def register_error_handler( + self, + code_or_exception: type[Exception] | int, + f: ft.ErrorHandlerCallable, + ) -> None: + """Alternative error attach function to the :meth:`errorhandler` + decorator that is more straightforward to use for non decorator + usage. + + .. versionadded:: 0.7 + """ + exc_class, code = self._get_exc_class_and_code(code_or_exception) + self.error_handler_spec[None][code][exc_class] = f + + @staticmethod + def _get_exc_class_and_code( + exc_class_or_code: type[Exception] | int, + ) -> tuple[type[Exception], int | None]: + """Get the exception class being handled. For HTTP status codes + or ``HTTPException`` subclasses, return both the exception and + status code. + + :param exc_class_or_code: Any exception class, or an HTTP status + code as an integer. + """ + exc_class: type[Exception] + + if isinstance(exc_class_or_code, int): + try: + exc_class = default_exceptions[exc_class_or_code] + except KeyError: + raise ValueError( + f"'{exc_class_or_code}' is not a recognized HTTP" + " error code. Use a subclass of HTTPException with" + " that code instead." + ) from None + else: + exc_class = exc_class_or_code + + if isinstance(exc_class, Exception): + raise TypeError( + f"{exc_class!r} is an instance, not a class. Handlers" + " can only be registered for Exception classes or HTTP" + " error codes." + ) + + if not issubclass(exc_class, Exception): + raise ValueError( + f"'{exc_class.__name__}' is not a subclass of Exception." + " Handlers can only be registered for Exception classes" + " or HTTP error codes." + ) + + if issubclass(exc_class, HTTPException): + return exc_class, exc_class.code + else: + return exc_class, None + + +def _endpoint_from_view_func(view_func: ft.RouteCallable) -> str: + """Internal helper that returns the default endpoint for a given + function. This always is the function name. + """ + assert view_func is not None, "expected view func if endpoint is not provided." + return view_func.__name__ + + +def _find_package_path(import_name: str) -> str: + """Find the path that contains the package or module.""" + root_mod_name, _, _ = import_name.partition(".") + + try: + root_spec = importlib.util.find_spec(root_mod_name) + + if root_spec is None: + raise ValueError("not found") + except (ImportError, ValueError): + # ImportError: the machinery told us it does not exist + # ValueError: + # - the module name was invalid + # - the module name is __main__ + # - we raised `ValueError` due to `root_spec` being `None` + return os.getcwd() + + if root_spec.submodule_search_locations: + if root_spec.origin is None or root_spec.origin == "namespace": + # namespace package + package_spec = importlib.util.find_spec(import_name) + + if package_spec is not None and package_spec.submodule_search_locations: + # Pick the path in the namespace that contains the submodule. + package_path = pathlib.Path( + os.path.commonpath(package_spec.submodule_search_locations) + ) + search_location = next( + location + for location in root_spec.submodule_search_locations + if package_path.is_relative_to(location) + ) + else: + # Pick the first path. + search_location = root_spec.submodule_search_locations[0] + + return os.path.dirname(search_location) + else: + # package with __init__.py + return os.path.dirname(os.path.dirname(root_spec.origin)) + else: + # module + return os.path.dirname(root_spec.origin) # type: ignore[type-var, return-value] + + +def find_package(import_name: str) -> tuple[str | None, str]: + """Find the prefix that a package is installed under, and the path + that it would be imported from. + + The prefix is the directory containing the standard directory + hierarchy (lib, bin, etc.). If the package is not installed to the + system (:attr:`sys.prefix`) or a virtualenv (``site-packages``), + ``None`` is returned. + + The path is the entry in :attr:`sys.path` that contains the package + for import. If the package is not installed, it's assumed that the + package was imported from the current working directory. + """ + package_path = _find_package_path(import_name) + py_prefix = os.path.abspath(sys.prefix) + + # installed to the system + if pathlib.PurePath(package_path).is_relative_to(py_prefix): + return py_prefix, package_path + + site_parent, site_folder = os.path.split(package_path) + + # installed to a virtualenv + if site_folder.lower() == "site-packages": + parent, folder = os.path.split(site_parent) + + # Windows (prefix/lib/site-packages) + if folder.lower() == "lib": + return parent, package_path + + # Unix (prefix/lib/pythonX.Y/site-packages) + if os.path.basename(parent).lower() == "lib": + return os.path.dirname(parent), package_path + + # something else (prefix/site-packages) + return site_parent, package_path + + # not installed + return None, package_path diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sessions.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sessions.py new file mode 100644 index 0000000000..ad357706ff --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/sessions.py @@ -0,0 +1,385 @@ +from __future__ import annotations + +import collections.abc as c +import hashlib +import typing as t +from collections.abc import MutableMapping +from datetime import datetime +from datetime import timezone + +from itsdangerous import BadSignature +from itsdangerous import URLSafeTimedSerializer +from werkzeug.datastructures import CallbackDict + +from .json.tag import TaggedJSONSerializer + +if t.TYPE_CHECKING: # pragma: no cover + import typing_extensions as te + + from .app import Flask + from .wrappers import Request + from .wrappers import Response + + +class SessionMixin(MutableMapping[str, t.Any]): + """Expands a basic dictionary with session attributes.""" + + @property + def permanent(self) -> bool: + """This reflects the ``'_permanent'`` key in the dict.""" + return self.get("_permanent", False) # type: ignore[no-any-return] + + @permanent.setter + def permanent(self, value: bool) -> None: + self["_permanent"] = bool(value) + + #: Some implementations can detect whether a session is newly + #: created, but that is not guaranteed. Use with caution. The mixin + # default is hard-coded ``False``. + new = False + + #: Some implementations can detect changes to the session and set + #: this when that happens. The mixin default is hard coded to + #: ``True``. + modified = True + + accessed = False + """Indicates if the session was accessed, even if it was not modified. This + is set when the session object is accessed through the request context, + including the global :data:`.session` proxy. A ``Vary: cookie`` header will + be added if this is ``True``. + + .. versionchanged:: 3.1.3 + This is tracked by the request context. + """ + + +class SecureCookieSession(CallbackDict[str, t.Any], SessionMixin): + """Base class for sessions based on signed cookies. + + This session backend will set the :attr:`modified` and + :attr:`accessed` attributes. It cannot reliably track whether a + session is new (vs. empty), so :attr:`new` remains hard coded to + ``False``. + """ + + #: When data is changed, this is set to ``True``. Only the session + #: dictionary itself is tracked; if the session contains mutable + #: data (for example a nested dict) then this must be set to + #: ``True`` manually when modifying that data. The session cookie + #: will only be written to the response if this is ``True``. + modified = False + + def __init__( + self, + initial: c.Mapping[str, t.Any] | None = None, + ) -> None: + def on_update(self: te.Self) -> None: + self.modified = True + + super().__init__(initial, on_update) + + +class NullSession(SecureCookieSession): + """Class used to generate nicer error messages if sessions are not + available. Will still allow read-only access to the empty session + but fail on setting. + """ + + def _fail(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn: + raise RuntimeError( + "The session is unavailable because no secret " + "key was set. Set the secret_key on the " + "application to something unique and secret." + ) + + __setitem__ = __delitem__ = clear = pop = popitem = update = setdefault = _fail + del _fail + + +class SessionInterface: + """The basic interface you have to implement in order to replace the + default session interface which uses werkzeug's securecookie + implementation. The only methods you have to implement are + :meth:`open_session` and :meth:`save_session`, the others have + useful defaults which you don't need to change. + + The session object returned by the :meth:`open_session` method has to + provide a dictionary like interface plus the properties and methods + from the :class:`SessionMixin`. We recommend just subclassing a dict + and adding that mixin:: + + class Session(dict, SessionMixin): + pass + + If :meth:`open_session` returns ``None`` Flask will call into + :meth:`make_null_session` to create a session that acts as replacement + if the session support cannot work because some requirement is not + fulfilled. The default :class:`NullSession` class that is created + will complain that the secret key was not set. + + To replace the session interface on an application all you have to do + is to assign :attr:`flask.Flask.session_interface`:: + + app = Flask(__name__) + app.session_interface = MySessionInterface() + + Multiple requests with the same session may be sent and handled + concurrently. When implementing a new session interface, consider + whether reads or writes to the backing store must be synchronized. + There is no guarantee on the order in which the session for each + request is opened or saved, it will occur in the order that requests + begin and end processing. + + .. versionadded:: 0.8 + """ + + #: :meth:`make_null_session` will look here for the class that should + #: be created when a null session is requested. Likewise the + #: :meth:`is_null_session` method will perform a typecheck against + #: this type. + null_session_class = NullSession + + #: A flag that indicates if the session interface is pickle based. + #: This can be used by Flask extensions to make a decision in regards + #: to how to deal with the session object. + #: + #: .. versionadded:: 0.10 + pickle_based = False + + def make_null_session(self, app: Flask) -> NullSession: + """Creates a null session which acts as a replacement object if the + real session support could not be loaded due to a configuration + error. This mainly aids the user experience because the job of the + null session is to still support lookup without complaining but + modifications are answered with a helpful error message of what + failed. + + This creates an instance of :attr:`null_session_class` by default. + """ + return self.null_session_class() + + def is_null_session(self, obj: object) -> bool: + """Checks if a given object is a null session. Null sessions are + not asked to be saved. + + This checks if the object is an instance of :attr:`null_session_class` + by default. + """ + return isinstance(obj, self.null_session_class) + + def get_cookie_name(self, app: Flask) -> str: + """The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``.""" + return app.config["SESSION_COOKIE_NAME"] # type: ignore[no-any-return] + + def get_cookie_domain(self, app: Flask) -> str | None: + """The value of the ``Domain`` parameter on the session cookie. If not set, + browsers will only send the cookie to the exact domain it was set from. + Otherwise, they will send it to any subdomain of the given value as well. + + Uses the :data:`SESSION_COOKIE_DOMAIN` config. + + .. versionchanged:: 2.3 + Not set by default, does not fall back to ``SERVER_NAME``. + """ + return app.config["SESSION_COOKIE_DOMAIN"] # type: ignore[no-any-return] + + def get_cookie_path(self, app: Flask) -> str: + """Returns the path for which the cookie should be valid. The + default implementation uses the value from the ``SESSION_COOKIE_PATH`` + config var if it's set, and falls back to ``APPLICATION_ROOT`` or + uses ``/`` if it's ``None``. + """ + return app.config["SESSION_COOKIE_PATH"] or app.config["APPLICATION_ROOT"] # type: ignore[no-any-return] + + def get_cookie_httponly(self, app: Flask) -> bool: + """Returns True if the session cookie should be httponly. This + currently just returns the value of the ``SESSION_COOKIE_HTTPONLY`` + config var. + """ + return app.config["SESSION_COOKIE_HTTPONLY"] # type: ignore[no-any-return] + + def get_cookie_secure(self, app: Flask) -> bool: + """Returns True if the cookie should be secure. This currently + just returns the value of the ``SESSION_COOKIE_SECURE`` setting. + """ + return app.config["SESSION_COOKIE_SECURE"] # type: ignore[no-any-return] + + def get_cookie_samesite(self, app: Flask) -> str | None: + """Return ``'Strict'`` or ``'Lax'`` if the cookie should use the + ``SameSite`` attribute. This currently just returns the value of + the :data:`SESSION_COOKIE_SAMESITE` setting. + """ + return app.config["SESSION_COOKIE_SAMESITE"] # type: ignore[no-any-return] + + def get_cookie_partitioned(self, app: Flask) -> bool: + """Returns True if the cookie should be partitioned. By default, uses + the value of :data:`SESSION_COOKIE_PARTITIONED`. + + .. versionadded:: 3.1 + """ + return app.config["SESSION_COOKIE_PARTITIONED"] # type: ignore[no-any-return] + + def get_expiration_time(self, app: Flask, session: SessionMixin) -> datetime | None: + """A helper method that returns an expiration date for the session + or ``None`` if the session is linked to the browser session. The + default implementation returns now + the permanent session + lifetime configured on the application. + """ + if session.permanent: + return datetime.now(timezone.utc) + app.permanent_session_lifetime + return None + + def should_set_cookie(self, app: Flask, session: SessionMixin) -> bool: + """Used by session backends to determine if a ``Set-Cookie`` header + should be set for this session cookie for this response. If the session + has been modified, the cookie is set. If the session is permanent and + the ``SESSION_REFRESH_EACH_REQUEST`` config is true, the cookie is + always set. + + This check is usually skipped if the session was deleted. + + .. versionadded:: 0.11 + """ + + return session.modified or ( + session.permanent and app.config["SESSION_REFRESH_EACH_REQUEST"] + ) + + def open_session(self, app: Flask, request: Request) -> SessionMixin | None: + """This is called at the beginning of each request, after + pushing the request context, before matching the URL. + + This must return an object which implements a dictionary-like + interface as well as the :class:`SessionMixin` interface. + + This will return ``None`` to indicate that loading failed in + some way that is not immediately an error. The request + context will fall back to using :meth:`make_null_session` + in this case. + """ + raise NotImplementedError() + + def save_session( + self, app: Flask, session: SessionMixin, response: Response + ) -> None: + """This is called at the end of each request, after generating + a response, before removing the request context. It is skipped + if :meth:`is_null_session` returns ``True``. + """ + raise NotImplementedError() + + +session_json_serializer = TaggedJSONSerializer() + + +def _lazy_sha1(string: bytes = b"") -> t.Any: + """Don't access ``hashlib.sha1`` until runtime. FIPS builds may not include + SHA-1, in which case the import and use as a default would fail before the + developer can configure something else. + """ + return hashlib.sha1(string) + + +class SecureCookieSessionInterface(SessionInterface): + """The default session interface that stores sessions in signed cookies + through the :mod:`itsdangerous` module. + """ + + #: the salt that should be applied on top of the secret key for the + #: signing of cookie based sessions. + salt = "cookie-session" + #: the hash function to use for the signature. The default is sha1 + digest_method = staticmethod(_lazy_sha1) + #: the name of the itsdangerous supported key derivation. The default + #: is hmac. + key_derivation = "hmac" + #: A python serializer for the payload. The default is a compact + #: JSON derived serializer with support for some extra Python types + #: such as datetime objects or tuples. + serializer = session_json_serializer + session_class = SecureCookieSession + + def get_signing_serializer(self, app: Flask) -> URLSafeTimedSerializer | None: + if not app.secret_key: + return None + + keys: list[str | bytes] = [] + + if fallbacks := app.config["SECRET_KEY_FALLBACKS"]: + keys.extend(fallbacks) + + keys.append(app.secret_key) # itsdangerous expects current key at top + return URLSafeTimedSerializer( + keys, # type: ignore[arg-type] + salt=self.salt, + serializer=self.serializer, + signer_kwargs={ + "key_derivation": self.key_derivation, + "digest_method": self.digest_method, + }, + ) + + def open_session(self, app: Flask, request: Request) -> SecureCookieSession | None: + s = self.get_signing_serializer(app) + if s is None: + return None + val = request.cookies.get(self.get_cookie_name(app)) + if not val: + return self.session_class() + max_age = int(app.permanent_session_lifetime.total_seconds()) + try: + data = s.loads(val, max_age=max_age) + return self.session_class(data) + except BadSignature: + return self.session_class() + + def save_session( + self, app: Flask, session: SessionMixin, response: Response + ) -> None: + name = self.get_cookie_name(app) + domain = self.get_cookie_domain(app) + path = self.get_cookie_path(app) + secure = self.get_cookie_secure(app) + partitioned = self.get_cookie_partitioned(app) + samesite = self.get_cookie_samesite(app) + httponly = self.get_cookie_httponly(app) + + # Add a "Vary: Cookie" header if the session was accessed at all. + if session.accessed: + response.vary.add("Cookie") + + # If the session is modified to be empty, remove the cookie. + # If the session is empty, return without setting the cookie. + if not session: + if session.modified: + response.delete_cookie( + name, + domain=domain, + path=path, + secure=secure, + partitioned=partitioned, + samesite=samesite, + httponly=httponly, + ) + response.vary.add("Cookie") + + return + + if not self.should_set_cookie(app, session): + return + + expires = self.get_expiration_time(app, session) + val = self.get_signing_serializer(app).dumps(dict(session)) # type: ignore[union-attr] + response.set_cookie( + name, + val, + expires=expires, + httponly=httponly, + domain=domain, + path=path, + secure=secure, + partitioned=partitioned, + samesite=samesite, + ) + response.vary.add("Cookie") diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/signals.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/signals.py new file mode 100644 index 0000000000..444fda9987 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/signals.py @@ -0,0 +1,17 @@ +from __future__ import annotations + +from blinker import Namespace + +# This namespace is only for signals provided by Flask itself. +_signals = Namespace() + +template_rendered = _signals.signal("template-rendered") +before_render_template = _signals.signal("before-render-template") +request_started = _signals.signal("request-started") +request_finished = _signals.signal("request-finished") +request_tearing_down = _signals.signal("request-tearing-down") +got_request_exception = _signals.signal("got-request-exception") +appcontext_tearing_down = _signals.signal("appcontext-tearing-down") +appcontext_pushed = _signals.signal("appcontext-pushed") +appcontext_popped = _signals.signal("appcontext-popped") +message_flashed = _signals.signal("message-flashed") diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/templating.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/templating.py new file mode 100644 index 0000000000..c5fb5b9905 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/templating.py @@ -0,0 +1,220 @@ +from __future__ import annotations + +import typing as t + +from jinja2 import BaseLoader +from jinja2 import Environment as BaseEnvironment +from jinja2 import Template +from jinja2 import TemplateNotFound + +from .globals import _cv_app +from .globals import _cv_request +from .globals import current_app +from .globals import request +from .helpers import stream_with_context +from .signals import before_render_template +from .signals import template_rendered + +if t.TYPE_CHECKING: # pragma: no cover + from .app import Flask + from .sansio.app import App + from .sansio.scaffold import Scaffold + + +def _default_template_ctx_processor() -> dict[str, t.Any]: + """Default template context processor. Replaces the ``request`` and ``g`` + proxies with their concrete objects for faster access. + """ + appctx = _cv_app.get(None) + reqctx = _cv_request.get(None) + rv: dict[str, t.Any] = {} + if appctx is not None: + rv["g"] = appctx.g + if reqctx is not None: + rv["request"] = reqctx.request + # The session proxy cannot be replaced, accessing it gets + # RequestContext.session, which sets session.accessed. + return rv + + +class Environment(BaseEnvironment): + """Works like a regular Jinja environment but has some additional + knowledge of how Flask's blueprint works so that it can prepend the + name of the blueprint to referenced templates if necessary. + """ + + def __init__(self, app: App, **options: t.Any) -> None: + if "loader" not in options: + options["loader"] = app.create_global_jinja_loader() + BaseEnvironment.__init__(self, **options) + self.app = app + + +class DispatchingJinjaLoader(BaseLoader): + """A loader that looks for templates in the application and all + the blueprint folders. + """ + + def __init__(self, app: App) -> None: + self.app = app + + def get_source( + self, environment: BaseEnvironment, template: str + ) -> tuple[str, str | None, t.Callable[[], bool] | None]: + if self.app.config["EXPLAIN_TEMPLATE_LOADING"]: + return self._get_source_explained(environment, template) + return self._get_source_fast(environment, template) + + def _get_source_explained( + self, environment: BaseEnvironment, template: str + ) -> tuple[str, str | None, t.Callable[[], bool] | None]: + attempts = [] + rv: tuple[str, str | None, t.Callable[[], bool] | None] | None + trv: None | (tuple[str, str | None, t.Callable[[], bool] | None]) = None + + for srcobj, loader in self._iter_loaders(template): + try: + rv = loader.get_source(environment, template) + if trv is None: + trv = rv + except TemplateNotFound: + rv = None + attempts.append((loader, srcobj, rv)) + + from .debughelpers import explain_template_loading_attempts + + explain_template_loading_attempts(self.app, template, attempts) + + if trv is not None: + return trv + raise TemplateNotFound(template) + + def _get_source_fast( + self, environment: BaseEnvironment, template: str + ) -> tuple[str, str | None, t.Callable[[], bool] | None]: + for _srcobj, loader in self._iter_loaders(template): + try: + return loader.get_source(environment, template) + except TemplateNotFound: + continue + raise TemplateNotFound(template) + + def _iter_loaders(self, template: str) -> t.Iterator[tuple[Scaffold, BaseLoader]]: + loader = self.app.jinja_loader + if loader is not None: + yield self.app, loader + + for blueprint in self.app.iter_blueprints(): + loader = blueprint.jinja_loader + if loader is not None: + yield blueprint, loader + + def list_templates(self) -> list[str]: + result = set() + loader = self.app.jinja_loader + if loader is not None: + result.update(loader.list_templates()) + + for blueprint in self.app.iter_blueprints(): + loader = blueprint.jinja_loader + if loader is not None: + for template in loader.list_templates(): + result.add(template) + + return list(result) + + +def _render(app: Flask, template: Template, context: dict[str, t.Any]) -> str: + app.update_template_context(context) + before_render_template.send( + app, _async_wrapper=app.ensure_sync, template=template, context=context + ) + rv = template.render(context) + template_rendered.send( + app, _async_wrapper=app.ensure_sync, template=template, context=context + ) + return rv + + +def render_template( + template_name_or_list: str | Template | list[str | Template], + **context: t.Any, +) -> str: + """Render a template by name with the given context. + + :param template_name_or_list: The name of the template to render. If + a list is given, the first name to exist will be rendered. + :param context: The variables to make available in the template. + """ + app = current_app._get_current_object() # type: ignore[attr-defined] + template = app.jinja_env.get_or_select_template(template_name_or_list) + return _render(app, template, context) + + +def render_template_string(source: str, **context: t.Any) -> str: + """Render a template from the given source string with the given + context. + + :param source: The source code of the template to render. + :param context: The variables to make available in the template. + """ + app = current_app._get_current_object() # type: ignore[attr-defined] + template = app.jinja_env.from_string(source) + return _render(app, template, context) + + +def _stream( + app: Flask, template: Template, context: dict[str, t.Any] +) -> t.Iterator[str]: + app.update_template_context(context) + before_render_template.send( + app, _async_wrapper=app.ensure_sync, template=template, context=context + ) + + def generate() -> t.Iterator[str]: + yield from template.generate(context) + template_rendered.send( + app, _async_wrapper=app.ensure_sync, template=template, context=context + ) + + rv = generate() + + # If a request context is active, keep it while generating. + if request: + rv = stream_with_context(rv) + + return rv + + +def stream_template( + template_name_or_list: str | Template | list[str | Template], + **context: t.Any, +) -> t.Iterator[str]: + """Render a template by name with the given context as a stream. + This returns an iterator of strings, which can be used as a + streaming response from a view. + + :param template_name_or_list: The name of the template to render. If + a list is given, the first name to exist will be rendered. + :param context: The variables to make available in the template. + + .. versionadded:: 2.2 + """ + app = current_app._get_current_object() # type: ignore[attr-defined] + template = app.jinja_env.get_or_select_template(template_name_or_list) + return _stream(app, template, context) + + +def stream_template_string(source: str, **context: t.Any) -> t.Iterator[str]: + """Render a template from the given source string with the given + context as a stream. This returns an iterator of strings, which can + be used as a streaming response from a view. + + :param source: The source code of the template to render. + :param context: The variables to make available in the template. + + .. versionadded:: 2.2 + """ + app = current_app._get_current_object() # type: ignore[attr-defined] + template = app.jinja_env.from_string(source) + return _stream(app, template, context) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/testing.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/testing.py new file mode 100644 index 0000000000..55eb12fe75 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/testing.py @@ -0,0 +1,298 @@ +from __future__ import annotations + +import importlib.metadata +import typing as t +from contextlib import contextmanager +from contextlib import ExitStack +from copy import copy +from types import TracebackType +from urllib.parse import urlsplit + +import werkzeug.test +from click.testing import CliRunner +from click.testing import Result +from werkzeug.test import Client +from werkzeug.wrappers import Request as BaseRequest + +from .cli import ScriptInfo +from .sessions import SessionMixin + +if t.TYPE_CHECKING: # pragma: no cover + from _typeshed.wsgi import WSGIEnvironment + from werkzeug.test import TestResponse + + from .app import Flask + + +class EnvironBuilder(werkzeug.test.EnvironBuilder): + """An :class:`~werkzeug.test.EnvironBuilder`, that takes defaults from the + application. + + :param app: The Flask application to configure the environment from. + :param path: URL path being requested. + :param base_url: Base URL where the app is being served, which + ``path`` is relative to. If not given, built from + :data:`PREFERRED_URL_SCHEME`, ``subdomain``, + :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`. + :param subdomain: Subdomain name to append to :data:`SERVER_NAME`. + :param url_scheme: Scheme to use instead of + :data:`PREFERRED_URL_SCHEME`. + :param json: If given, this is serialized as JSON and passed as + ``data``. Also defaults ``content_type`` to + ``application/json``. + :param args: other positional arguments passed to + :class:`~werkzeug.test.EnvironBuilder`. + :param kwargs: other keyword arguments passed to + :class:`~werkzeug.test.EnvironBuilder`. + """ + + def __init__( + self, + app: Flask, + path: str = "/", + base_url: str | None = None, + subdomain: str | None = None, + url_scheme: str | None = None, + *args: t.Any, + **kwargs: t.Any, + ) -> None: + assert not (base_url or subdomain or url_scheme) or ( + base_url is not None + ) != bool(subdomain or url_scheme), ( + 'Cannot pass "subdomain" or "url_scheme" with "base_url".' + ) + + if base_url is None: + http_host = app.config.get("SERVER_NAME") or "localhost" + app_root = app.config["APPLICATION_ROOT"] + + if subdomain: + http_host = f"{subdomain}.{http_host}" + + if url_scheme is None: + url_scheme = app.config["PREFERRED_URL_SCHEME"] + + url = urlsplit(path) + base_url = ( + f"{url.scheme or url_scheme}://{url.netloc or http_host}" + f"/{app_root.lstrip('/')}" + ) + path = url.path + + if url.query: + path = f"{path}?{url.query}" + + self.app = app + super().__init__(path, base_url, *args, **kwargs) + + def json_dumps(self, obj: t.Any, **kwargs: t.Any) -> str: + """Serialize ``obj`` to a JSON-formatted string. + + The serialization will be configured according to the config associated + with this EnvironBuilder's ``app``. + """ + return self.app.json.dumps(obj, **kwargs) + + +_werkzeug_version = "" + + +def _get_werkzeug_version() -> str: + global _werkzeug_version + + if not _werkzeug_version: + _werkzeug_version = importlib.metadata.version("werkzeug") + + return _werkzeug_version + + +class FlaskClient(Client): + """Works like a regular Werkzeug test client but has knowledge about + Flask's contexts to defer the cleanup of the request context until + the end of a ``with`` block. For general information about how to + use this class refer to :class:`werkzeug.test.Client`. + + .. versionchanged:: 0.12 + `app.test_client()` includes preset default environment, which can be + set after instantiation of the `app.test_client()` object in + `client.environ_base`. + + Basic usage is outlined in the :doc:`/testing` chapter. + """ + + application: Flask + + def __init__(self, *args: t.Any, **kwargs: t.Any) -> None: + super().__init__(*args, **kwargs) + self.preserve_context = False + self._new_contexts: list[t.ContextManager[t.Any]] = [] + self._context_stack = ExitStack() + self.environ_base = { + "REMOTE_ADDR": "127.0.0.1", + "HTTP_USER_AGENT": f"Werkzeug/{_get_werkzeug_version()}", + } + + @contextmanager + def session_transaction( + self, *args: t.Any, **kwargs: t.Any + ) -> t.Iterator[SessionMixin]: + """When used in combination with a ``with`` statement this opens a + session transaction. This can be used to modify the session that + the test client uses. Once the ``with`` block is left the session is + stored back. + + :: + + with client.session_transaction() as session: + session['value'] = 42 + + Internally this is implemented by going through a temporary test + request context and since session handling could depend on + request variables this function accepts the same arguments as + :meth:`~flask.Flask.test_request_context` which are directly + passed through. + """ + if self._cookies is None: + raise TypeError( + "Cookies are disabled. Create a client with 'use_cookies=True'." + ) + + app = self.application + ctx = app.test_request_context(*args, **kwargs) + self._add_cookies_to_wsgi(ctx.request.environ) + + with ctx: + sess = app.session_interface.open_session(app, ctx.request) + + if sess is None: + raise RuntimeError("Session backend did not open a session.") + + yield sess + resp = app.response_class() + + if app.session_interface.is_null_session(sess): + return + + with ctx: + app.session_interface.save_session(app, sess, resp) + + self._update_cookies_from_response( + ctx.request.host.partition(":")[0], + ctx.request.path, + resp.headers.getlist("Set-Cookie"), + ) + + def _copy_environ(self, other: WSGIEnvironment) -> WSGIEnvironment: + out = {**self.environ_base, **other} + + if self.preserve_context: + out["werkzeug.debug.preserve_context"] = self._new_contexts.append + + return out + + def _request_from_builder_args( + self, args: tuple[t.Any, ...], kwargs: dict[str, t.Any] + ) -> BaseRequest: + kwargs["environ_base"] = self._copy_environ(kwargs.get("environ_base", {})) + builder = EnvironBuilder(self.application, *args, **kwargs) + + try: + return builder.get_request() + finally: + builder.close() + + def open( + self, + *args: t.Any, + buffered: bool = False, + follow_redirects: bool = False, + **kwargs: t.Any, + ) -> TestResponse: + if args and isinstance( + args[0], (werkzeug.test.EnvironBuilder, dict, BaseRequest) + ): + if isinstance(args[0], werkzeug.test.EnvironBuilder): + builder = copy(args[0]) + builder.environ_base = self._copy_environ(builder.environ_base or {}) # type: ignore[arg-type] + request = builder.get_request() + elif isinstance(args[0], dict): + request = EnvironBuilder.from_environ( + args[0], app=self.application, environ_base=self._copy_environ({}) + ).get_request() + else: + # isinstance(args[0], BaseRequest) + request = copy(args[0]) + request.environ = self._copy_environ(request.environ) + else: + # request is None + request = self._request_from_builder_args(args, kwargs) + + # Pop any previously preserved contexts. This prevents contexts + # from being preserved across redirects or multiple requests + # within a single block. + self._context_stack.close() + + response = super().open( + request, + buffered=buffered, + follow_redirects=follow_redirects, + ) + response.json_module = self.application.json # type: ignore[assignment] + + # Re-push contexts that were preserved during the request. + for cm in self._new_contexts: + self._context_stack.enter_context(cm) + + self._new_contexts.clear() + return response + + def __enter__(self) -> FlaskClient: + if self.preserve_context: + raise RuntimeError("Cannot nest client invocations") + self.preserve_context = True + return self + + def __exit__( + self, + exc_type: type | None, + exc_value: BaseException | None, + tb: TracebackType | None, + ) -> None: + self.preserve_context = False + self._context_stack.close() + + +class FlaskCliRunner(CliRunner): + """A :class:`~click.testing.CliRunner` for testing a Flask app's + CLI commands. Typically created using + :meth:`~flask.Flask.test_cli_runner`. See :ref:`testing-cli`. + """ + + def __init__(self, app: Flask, **kwargs: t.Any) -> None: + self.app = app + super().__init__(**kwargs) + + def invoke( # type: ignore + self, cli: t.Any = None, args: t.Any = None, **kwargs: t.Any + ) -> Result: + """Invokes a CLI command in an isolated environment. See + :meth:`CliRunner.invoke ` for + full method documentation. See :ref:`testing-cli` for examples. + + If the ``obj`` argument is not given, passes an instance of + :class:`~flask.cli.ScriptInfo` that knows how to load the Flask + app being tested. + + :param cli: Command object to invoke. Default is the app's + :attr:`~flask.app.Flask.cli` group. + :param args: List of strings to invoke the command with. + + :return: a :class:`~click.testing.Result` object. + """ + if cli is None: + cli = self.app.cli + + if "obj" not in kwargs: + kwargs["obj"] = ScriptInfo(create_app=lambda: self.app) + + return super().invoke(cli, args, **kwargs) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/typing.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/typing.py new file mode 100644 index 0000000000..6b70c40973 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/typing.py @@ -0,0 +1,93 @@ +from __future__ import annotations + +import collections.abc as cabc +import typing as t + +if t.TYPE_CHECKING: # pragma: no cover + from _typeshed.wsgi import WSGIApplication # noqa: F401 + from werkzeug.datastructures import Headers # noqa: F401 + from werkzeug.sansio.response import Response # noqa: F401 + +# The possible types that are directly convertible or are a Response object. +ResponseValue = t.Union[ + "Response", + str, + bytes, + list[t.Any], + # Only dict is actually accepted, but Mapping allows for TypedDict. + t.Mapping[str, t.Any], + t.Iterator[str], + t.Iterator[bytes], + cabc.AsyncIterable[str], # for Quart, until App is generic. + cabc.AsyncIterable[bytes], +] + +# the possible types for an individual HTTP header +# This should be a Union, but mypy doesn't pass unless it's a TypeVar. +HeaderValue = t.Union[str, list[str], tuple[str, ...]] + +# the possible types for HTTP headers +HeadersValue = t.Union[ + "Headers", + t.Mapping[str, HeaderValue], + t.Sequence[tuple[str, HeaderValue]], +] + +# The possible types returned by a route function. +ResponseReturnValue = t.Union[ + ResponseValue, + tuple[ResponseValue, HeadersValue], + tuple[ResponseValue, int], + tuple[ResponseValue, int, HeadersValue], + "WSGIApplication", +] + +# Allow any subclass of werkzeug.Response, such as the one from Flask, +# as a callback argument. Using werkzeug.Response directly makes a +# callback annotated with flask.Response fail type checking. +ResponseClass = t.TypeVar("ResponseClass", bound="Response") + +AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named +AfterRequestCallable = t.Union[ + t.Callable[[ResponseClass], ResponseClass], + t.Callable[[ResponseClass], t.Awaitable[ResponseClass]], +] +BeforeFirstRequestCallable = t.Union[ + t.Callable[[], None], t.Callable[[], t.Awaitable[None]] +] +BeforeRequestCallable = t.Union[ + t.Callable[[], t.Optional[ResponseReturnValue]], + t.Callable[[], t.Awaitable[t.Optional[ResponseReturnValue]]], +] +ShellContextProcessorCallable = t.Callable[[], dict[str, t.Any]] +TeardownCallable = t.Union[ + t.Callable[[t.Optional[BaseException]], None], + t.Callable[[t.Optional[BaseException]], t.Awaitable[None]], +] +TemplateContextProcessorCallable = t.Union[ + t.Callable[[], dict[str, t.Any]], + t.Callable[[], t.Awaitable[dict[str, t.Any]]], +] +TemplateFilterCallable = t.Callable[..., t.Any] +TemplateGlobalCallable = t.Callable[..., t.Any] +TemplateTestCallable = t.Callable[..., bool] +URLDefaultCallable = t.Callable[[str, dict[str, t.Any]], None] +URLValuePreprocessorCallable = t.Callable[ + [t.Optional[str], t.Optional[dict[str, t.Any]]], None +] + +# This should take Exception, but that either breaks typing the argument +# with a specific exception, or decorating multiple times with different +# exceptions (and using a union type on the argument). +# https://github.com/pallets/flask/issues/4095 +# https://github.com/pallets/flask/issues/4295 +# https://github.com/pallets/flask/issues/4297 +ErrorHandlerCallable = t.Union[ + t.Callable[[t.Any], ResponseReturnValue], + t.Callable[[t.Any], t.Awaitable[ResponseReturnValue]], +] + +RouteCallable = t.Union[ + t.Callable[..., ResponseReturnValue], + t.Callable[..., t.Awaitable[ResponseReturnValue]], +] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/views.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/views.py new file mode 100644 index 0000000000..53fe976dc2 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/views.py @@ -0,0 +1,191 @@ +from __future__ import annotations + +import typing as t + +from . import typing as ft +from .globals import current_app +from .globals import request + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + +http_method_funcs = frozenset( + ["get", "post", "head", "options", "delete", "put", "trace", "patch"] +) + + +class View: + """Subclass this class and override :meth:`dispatch_request` to + create a generic class-based view. Call :meth:`as_view` to create a + view function that creates an instance of the class with the given + arguments and calls its ``dispatch_request`` method with any URL + variables. + + See :doc:`views` for a detailed guide. + + .. code-block:: python + + class Hello(View): + init_every_request = False + + def dispatch_request(self, name): + return f"Hello, {name}!" + + app.add_url_rule( + "/hello/", view_func=Hello.as_view("hello") + ) + + Set :attr:`methods` on the class to change what methods the view + accepts. + + Set :attr:`decorators` on the class to apply a list of decorators to + the generated view function. Decorators applied to the class itself + will not be applied to the generated view function! + + Set :attr:`init_every_request` to ``False`` for efficiency, unless + you need to store request-global data on ``self``. + """ + + #: The methods this view is registered for. Uses the same default + #: (``["GET", "HEAD", "OPTIONS"]``) as ``route`` and + #: ``add_url_rule`` by default. + methods: t.ClassVar[t.Collection[str] | None] = None + + #: Control whether the ``OPTIONS`` method is handled automatically. + #: Uses the same default (``True``) as ``route`` and + #: ``add_url_rule`` by default. + provide_automatic_options: t.ClassVar[bool | None] = None + + #: A list of decorators to apply, in order, to the generated view + #: function. Remember that ``@decorator`` syntax is applied bottom + #: to top, so the first decorator in the list would be the bottom + #: decorator. + #: + #: .. versionadded:: 0.8 + decorators: t.ClassVar[list[t.Callable[..., t.Any]]] = [] + + #: Create a new instance of this view class for every request by + #: default. If a view subclass sets this to ``False``, the same + #: instance is used for every request. + #: + #: A single instance is more efficient, especially if complex setup + #: is done during init. However, storing data on ``self`` is no + #: longer safe across requests, and :data:`~flask.g` should be used + #: instead. + #: + #: .. versionadded:: 2.2 + init_every_request: t.ClassVar[bool] = True + + def dispatch_request(self) -> ft.ResponseReturnValue: + """The actual view function behavior. Subclasses must override + this and return a valid response. Any variables from the URL + rule are passed as keyword arguments. + """ + raise NotImplementedError() + + @classmethod + def as_view( + cls, name: str, *class_args: t.Any, **class_kwargs: t.Any + ) -> ft.RouteCallable: + """Convert the class into a view function that can be registered + for a route. + + By default, the generated view will create a new instance of the + view class for every request and call its + :meth:`dispatch_request` method. If the view class sets + :attr:`init_every_request` to ``False``, the same instance will + be used for every request. + + Except for ``name``, all other arguments passed to this method + are forwarded to the view class ``__init__`` method. + + .. versionchanged:: 2.2 + Added the ``init_every_request`` class attribute. + """ + if cls.init_every_request: + + def view(**kwargs: t.Any) -> ft.ResponseReturnValue: + self = view.view_class( # type: ignore[attr-defined] + *class_args, **class_kwargs + ) + return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] + + else: + self = cls(*class_args, **class_kwargs) # pyright: ignore + + def view(**kwargs: t.Any) -> ft.ResponseReturnValue: + return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] + + if cls.decorators: + view.__name__ = name + view.__module__ = cls.__module__ + for decorator in cls.decorators: + view = decorator(view) + + # We attach the view class to the view function for two reasons: + # first of all it allows us to easily figure out what class-based + # view this thing came from, secondly it's also used for instantiating + # the view class so you can actually replace it with something else + # for testing purposes and debugging. + view.view_class = cls # type: ignore + view.__name__ = name + view.__doc__ = cls.__doc__ + view.__module__ = cls.__module__ + view.methods = cls.methods # type: ignore + view.provide_automatic_options = cls.provide_automatic_options # type: ignore + return view + + +class MethodView(View): + """Dispatches request methods to the corresponding instance methods. + For example, if you implement a ``get`` method, it will be used to + handle ``GET`` requests. + + This can be useful for defining a REST API. + + :attr:`methods` is automatically set based on the methods defined on + the class. + + See :doc:`views` for a detailed guide. + + .. code-block:: python + + class CounterAPI(MethodView): + def get(self): + return str(session.get("counter", 0)) + + def post(self): + session["counter"] = session.get("counter", 0) + 1 + return redirect(url_for("counter")) + + app.add_url_rule( + "/counter", view_func=CounterAPI.as_view("counter") + ) + """ + + def __init_subclass__(cls, **kwargs: t.Any) -> None: + super().__init_subclass__(**kwargs) + + if "methods" not in cls.__dict__: + methods = set() + + for base in cls.__bases__: + if getattr(base, "methods", None): + methods.update(base.methods) # type: ignore[attr-defined] + + for key in http_method_funcs: + if hasattr(cls, key): + methods.add(key.upper()) + + if methods: + cls.methods = methods + + def dispatch_request(self, **kwargs: t.Any) -> ft.ResponseReturnValue: + meth = getattr(self, request.method.lower(), None) + + # If the request method is HEAD and we don't have a handler for it + # retry with GET. + if meth is None and request.method == "HEAD": + meth = getattr(self, "get", None) + + assert meth is not None, f"Unimplemented method {request.method!r}" + return current_app.ensure_sync(meth)(**kwargs) # type: ignore[no-any-return] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/wrappers.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/wrappers.py new file mode 100644 index 0000000000..bab610291c --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/flask/wrappers.py @@ -0,0 +1,257 @@ +from __future__ import annotations + +import typing as t + +from werkzeug.exceptions import BadRequest +from werkzeug.exceptions import HTTPException +from werkzeug.wrappers import Request as RequestBase +from werkzeug.wrappers import Response as ResponseBase + +from . import json +from .globals import current_app +from .helpers import _split_blueprint_path + +if t.TYPE_CHECKING: # pragma: no cover + from werkzeug.routing import Rule + + +class Request(RequestBase): + """The request object used by default in Flask. Remembers the + matched endpoint and view arguments. + + It is what ends up as :class:`~flask.request`. If you want to replace + the request object used you can subclass this and set + :attr:`~flask.Flask.request_class` to your subclass. + + The request object is a :class:`~werkzeug.wrappers.Request` subclass and + provides all of the attributes Werkzeug defines plus a few Flask + specific ones. + """ + + json_module: t.Any = json + + #: The internal URL rule that matched the request. This can be + #: useful to inspect which methods are allowed for the URL from + #: a before/after handler (``request.url_rule.methods``) etc. + #: Though if the request's method was invalid for the URL rule, + #: the valid list is available in ``routing_exception.valid_methods`` + #: instead (an attribute of the Werkzeug exception + #: :exc:`~werkzeug.exceptions.MethodNotAllowed`) + #: because the request was never internally bound. + #: + #: .. versionadded:: 0.6 + url_rule: Rule | None = None + + #: A dict of view arguments that matched the request. If an exception + #: happened when matching, this will be ``None``. + view_args: dict[str, t.Any] | None = None + + #: If matching the URL failed, this is the exception that will be + #: raised / was raised as part of the request handling. This is + #: usually a :exc:`~werkzeug.exceptions.NotFound` exception or + #: something similar. + routing_exception: HTTPException | None = None + + _max_content_length: int | None = None + _max_form_memory_size: int | None = None + _max_form_parts: int | None = None + + @property + def max_content_length(self) -> int | None: + """The maximum number of bytes that will be read during this request. If + this limit is exceeded, a 413 :exc:`~werkzeug.exceptions.RequestEntityTooLarge` + error is raised. If it is set to ``None``, no limit is enforced at the + Flask application level. However, if it is ``None`` and the request has + no ``Content-Length`` header and the WSGI server does not indicate that + it terminates the stream, then no data is read to avoid an infinite + stream. + + Each request defaults to the :data:`MAX_CONTENT_LENGTH` config, which + defaults to ``None``. It can be set on a specific ``request`` to apply + the limit to that specific view. This should be set appropriately based + on an application's or view's specific needs. + + .. versionchanged:: 3.1 + This can be set per-request. + + .. versionchanged:: 0.6 + This is configurable through Flask config. + """ + if self._max_content_length is not None: + return self._max_content_length + + if not current_app: + return super().max_content_length + + return current_app.config["MAX_CONTENT_LENGTH"] # type: ignore[no-any-return] + + @max_content_length.setter + def max_content_length(self, value: int | None) -> None: + self._max_content_length = value + + @property + def max_form_memory_size(self) -> int | None: + """The maximum size in bytes any non-file form field may be in a + ``multipart/form-data`` body. If this limit is exceeded, a 413 + :exc:`~werkzeug.exceptions.RequestEntityTooLarge` error is raised. If it + is set to ``None``, no limit is enforced at the Flask application level. + + Each request defaults to the :data:`MAX_FORM_MEMORY_SIZE` config, which + defaults to ``500_000``. It can be set on a specific ``request`` to + apply the limit to that specific view. This should be set appropriately + based on an application's or view's specific needs. + + .. versionchanged:: 3.1 + This is configurable through Flask config. + """ + if self._max_form_memory_size is not None: + return self._max_form_memory_size + + if not current_app: + return super().max_form_memory_size + + return current_app.config["MAX_FORM_MEMORY_SIZE"] # type: ignore[no-any-return] + + @max_form_memory_size.setter + def max_form_memory_size(self, value: int | None) -> None: + self._max_form_memory_size = value + + @property # type: ignore[override] + def max_form_parts(self) -> int | None: + """The maximum number of fields that may be present in a + ``multipart/form-data`` body. If this limit is exceeded, a 413 + :exc:`~werkzeug.exceptions.RequestEntityTooLarge` error is raised. If it + is set to ``None``, no limit is enforced at the Flask application level. + + Each request defaults to the :data:`MAX_FORM_PARTS` config, which + defaults to ``1_000``. It can be set on a specific ``request`` to apply + the limit to that specific view. This should be set appropriately based + on an application's or view's specific needs. + + .. versionchanged:: 3.1 + This is configurable through Flask config. + """ + if self._max_form_parts is not None: + return self._max_form_parts + + if not current_app: + return super().max_form_parts + + return current_app.config["MAX_FORM_PARTS"] # type: ignore[no-any-return] + + @max_form_parts.setter + def max_form_parts(self, value: int | None) -> None: + self._max_form_parts = value + + @property + def endpoint(self) -> str | None: + """The endpoint that matched the request URL. + + This will be ``None`` if matching failed or has not been + performed yet. + + This in combination with :attr:`view_args` can be used to + reconstruct the same URL or a modified URL. + """ + if self.url_rule is not None: + return self.url_rule.endpoint # type: ignore[no-any-return] + + return None + + @property + def blueprint(self) -> str | None: + """The registered name of the current blueprint. + + This will be ``None`` if the endpoint is not part of a + blueprint, or if URL matching failed or has not been performed + yet. + + This does not necessarily match the name the blueprint was + created with. It may have been nested, or registered with a + different name. + """ + endpoint = self.endpoint + + if endpoint is not None and "." in endpoint: + return endpoint.rpartition(".")[0] + + return None + + @property + def blueprints(self) -> list[str]: + """The registered names of the current blueprint upwards through + parent blueprints. + + This will be an empty list if there is no current blueprint, or + if URL matching failed. + + .. versionadded:: 2.0.1 + """ + name = self.blueprint + + if name is None: + return [] + + return _split_blueprint_path(name) + + def _load_form_data(self) -> None: + super()._load_form_data() + + # In debug mode we're replacing the files multidict with an ad-hoc + # subclass that raises a different error for key errors. + if ( + current_app + and current_app.debug + and self.mimetype != "multipart/form-data" + and not self.files + ): + from .debughelpers import attach_enctype_error_multidict + + attach_enctype_error_multidict(self) + + def on_json_loading_failed(self, e: ValueError | None) -> t.Any: + try: + return super().on_json_loading_failed(e) + except BadRequest as ebr: + if current_app and current_app.debug: + raise + + raise BadRequest() from ebr + + +class Response(ResponseBase): + """The response object that is used by default in Flask. Works like the + response object from Werkzeug but is set to have an HTML mimetype by + default. Quite often you don't have to create this object yourself because + :meth:`~flask.Flask.make_response` will take care of that for you. + + If you want to replace the response object used you can subclass this and + set :attr:`~flask.Flask.response_class` to your subclass. + + .. versionchanged:: 1.0 + JSON support is added to the response, like the request. This is useful + when testing to get the test client response data as JSON. + + .. versionchanged:: 1.0 + + Added :attr:`max_cookie_size`. + """ + + default_mimetype: str | None = "text/html" + + json_module = json + + autocorrect_location_header = False + + @property + def max_cookie_size(self) -> int: # type: ignore + """Read-only view of the :data:`MAX_COOKIE_SIZE` config key. + + See :attr:`~werkzeug.wrappers.Response.max_cookie_size` in + Werkzeug's docs. + """ + if current_app: + return current_app.config["MAX_COOKIE_SIZE"] # type: ignore[no-any-return] + + # return Werkzeug's default when not in an app context + return super().max_cookie_size diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/INSTALLER b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/INSTALLER new file mode 100644 index 0000000000..a1b589e38a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/INSTALLER @@ -0,0 +1 @@ +pip diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/LICENSE.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/LICENSE.txt new file mode 100644 index 0000000000..7b190ca671 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/LICENSE.txt @@ -0,0 +1,28 @@ +Copyright 2011 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/METADATA b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/METADATA new file mode 100644 index 0000000000..ddf5464849 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/METADATA @@ -0,0 +1,60 @@ +Metadata-Version: 2.1 +Name: itsdangerous +Version: 2.2.0 +Summary: Safely pass data to untrusted environments and back. +Maintainer-email: Pallets +Requires-Python: >=3.8 +Description-Content-Type: text/markdown +Classifier: Development Status :: 5 - Production/Stable +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: BSD License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Typing :: Typed +Project-URL: Changes, https://itsdangerous.palletsprojects.com/changes/ +Project-URL: Chat, https://discord.gg/pallets +Project-URL: Documentation, https://itsdangerous.palletsprojects.com/ +Project-URL: Donate, https://palletsprojects.com/donate +Project-URL: Source, https://github.com/pallets/itsdangerous/ + +# ItsDangerous + +... so better sign this + +Various helpers to pass data to untrusted environments and to get it +back safe and sound. Data is cryptographically signed to ensure that a +token has not been tampered with. + +It's possible to customize how data is serialized. Data is compressed as +needed. A timestamp can be added and verified automatically while +loading a token. + + +## A Simple Example + +Here's how you could generate a token for transmitting a user's id and +name between web requests. + +```python +from itsdangerous import URLSafeSerializer +auth_s = URLSafeSerializer("secret key", "auth") +token = auth_s.dumps({"id": 5, "name": "itsdangerous"}) + +print(token) +# eyJpZCI6NSwibmFtZSI6Iml0c2Rhbmdlcm91cyJ9.6YP6T0BaO67XP--9UzTrmurXSmg + +data = auth_s.loads(token) +print(data["name"]) +# itsdangerous +``` + + +## Donate + +The Pallets organization develops and supports ItsDangerous and other +popular packages. In order to grow the community of contributors and +users, and allow the maintainers to devote more time to the projects, +[please donate today][]. + +[please donate today]: https://palletsprojects.com/donate + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/RECORD b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/RECORD new file mode 100644 index 0000000000..333875cf6a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/RECORD @@ -0,0 +1,22 @@ +itsdangerous-2.2.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 +itsdangerous-2.2.0.dist-info/LICENSE.txt,sha256=Y68JiRtr6K0aQlLtQ68PTvun_JSOIoNnvtfzxa4LCdc,1475 +itsdangerous-2.2.0.dist-info/METADATA,sha256=0rk0-1ZwihuU5DnwJVwPWoEI4yWOyCexih3JyZHblhE,1924 +itsdangerous-2.2.0.dist-info/RECORD,, +itsdangerous-2.2.0.dist-info/WHEEL,sha256=EZbGkh7Ie4PoZfRQ8I0ZuP9VklN_TvcZ6DSE5Uar4z4,81 +itsdangerous/__init__.py,sha256=4SK75sCe29xbRgQE1ZQtMHnKUuZYAf3bSpZOrff1IAY,1427 +itsdangerous/__pycache__/__init__.cpython-311.pyc,, +itsdangerous/__pycache__/_json.cpython-311.pyc,, +itsdangerous/__pycache__/encoding.cpython-311.pyc,, +itsdangerous/__pycache__/exc.cpython-311.pyc,, +itsdangerous/__pycache__/serializer.cpython-311.pyc,, +itsdangerous/__pycache__/signer.cpython-311.pyc,, +itsdangerous/__pycache__/timed.cpython-311.pyc,, +itsdangerous/__pycache__/url_safe.cpython-311.pyc,, +itsdangerous/_json.py,sha256=wPQGmge2yZ9328EHKF6gadGeyGYCJQKxtU-iLKE6UnA,473 +itsdangerous/encoding.py,sha256=wwTz5q_3zLcaAdunk6_vSoStwGqYWe307Zl_U87aRFM,1409 +itsdangerous/exc.py,sha256=Rr3exo0MRFEcPZltwecyK16VV1bE2K9_F1-d-ljcUn4,3201 +itsdangerous/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 +itsdangerous/serializer.py,sha256=PmdwADLqkSyQLZ0jOKAgDsAW4k_H0TlA71Ei3z0C5aI,15601 +itsdangerous/signer.py,sha256=YO0CV7NBvHA6j549REHJFUjUojw2pHqwcUpQnU7yNYQ,9647 +itsdangerous/timed.py,sha256=6RvDMqNumGMxf0-HlpaZdN9PUQQmRvrQGplKhxuivUs,8083 +itsdangerous/url_safe.py,sha256=az4e5fXi_vs-YbWj8YZwn4wiVKfeD--GEKRT5Ueu4P4,2505 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/WHEEL b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/WHEEL new file mode 100644 index 0000000000..3b5e64b5e6 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous-2.2.0.dist-info/WHEEL @@ -0,0 +1,4 @@ +Wheel-Version: 1.0 +Generator: flit 3.9.0 +Root-Is-Purelib: true +Tag: py3-none-any diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__init__.py new file mode 100644 index 0000000000..ea55256ebd --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__init__.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import typing as t + +from .encoding import base64_decode as base64_decode +from .encoding import base64_encode as base64_encode +from .encoding import want_bytes as want_bytes +from .exc import BadData as BadData +from .exc import BadHeader as BadHeader +from .exc import BadPayload as BadPayload +from .exc import BadSignature as BadSignature +from .exc import BadTimeSignature as BadTimeSignature +from .exc import SignatureExpired as SignatureExpired +from .serializer import Serializer as Serializer +from .signer import HMACAlgorithm as HMACAlgorithm +from .signer import NoneAlgorithm as NoneAlgorithm +from .signer import Signer as Signer +from .timed import TimedSerializer as TimedSerializer +from .timed import TimestampSigner as TimestampSigner +from .url_safe import URLSafeSerializer as URLSafeSerializer +from .url_safe import URLSafeTimedSerializer as URLSafeTimedSerializer + + +def __getattr__(name: str) -> t.Any: + if name == "__version__": + import importlib.metadata + import warnings + + warnings.warn( + "The '__version__' attribute is deprecated and will be removed in" + " ItsDangerous 2.3. Use feature detection or" + " 'importlib.metadata.version(\"itsdangerous\")' instead.", + DeprecationWarning, + stacklevel=2, + ) + return importlib.metadata.version("itsdangerous") + + raise AttributeError(name) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..b249f9ca20 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/_json.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/_json.cpython-311.pyc new file mode 100644 index 0000000000..ca4a9e4d2c Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/_json.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/encoding.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/encoding.cpython-311.pyc new file mode 100644 index 0000000000..e3ca709e5e Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/encoding.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/exc.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/exc.cpython-311.pyc new file mode 100644 index 0000000000..1a066699e1 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/exc.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/serializer.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/serializer.cpython-311.pyc new file mode 100644 index 0000000000..e1a2f97e6a Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/serializer.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/signer.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/signer.cpython-311.pyc new file mode 100644 index 0000000000..f690f78533 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/signer.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/timed.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/timed.cpython-311.pyc new file mode 100644 index 0000000000..1145153103 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/timed.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/url_safe.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/url_safe.cpython-311.pyc new file mode 100644 index 0000000000..a403e8f666 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/__pycache__/url_safe.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/_json.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/_json.py new file mode 100644 index 0000000000..fc23feaaff --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/_json.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +import json as _json +import typing as t + + +class _CompactJSON: + """Wrapper around json module that strips whitespace.""" + + @staticmethod + def loads(payload: str | bytes) -> t.Any: + return _json.loads(payload) + + @staticmethod + def dumps(obj: t.Any, **kwargs: t.Any) -> str: + kwargs.setdefault("ensure_ascii", False) + kwargs.setdefault("separators", (",", ":")) + return _json.dumps(obj, **kwargs) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/encoding.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/encoding.py new file mode 100644 index 0000000000..f5ca80f905 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/encoding.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +import base64 +import string +import struct +import typing as t + +from .exc import BadData + + +def want_bytes( + s: str | bytes, encoding: str = "utf-8", errors: str = "strict" +) -> bytes: + if isinstance(s, str): + s = s.encode(encoding, errors) + + return s + + +def base64_encode(string: str | bytes) -> bytes: + """Base64 encode a string of bytes or text. The resulting bytes are + safe to use in URLs. + """ + string = want_bytes(string) + return base64.urlsafe_b64encode(string).rstrip(b"=") + + +def base64_decode(string: str | bytes) -> bytes: + """Base64 decode a URL-safe string of bytes or text. The result is + bytes. + """ + string = want_bytes(string, encoding="ascii", errors="ignore") + string += b"=" * (-len(string) % 4) + + try: + return base64.urlsafe_b64decode(string) + except (TypeError, ValueError) as e: + raise BadData("Invalid base64-encoded data") from e + + +# The alphabet used by base64.urlsafe_* +_base64_alphabet = f"{string.ascii_letters}{string.digits}-_=".encode("ascii") + +_int64_struct = struct.Struct(">Q") +_int_to_bytes = _int64_struct.pack +_bytes_to_int = t.cast("t.Callable[[bytes], tuple[int]]", _int64_struct.unpack) + + +def int_to_bytes(num: int) -> bytes: + return _int_to_bytes(num).lstrip(b"\x00") + + +def bytes_to_int(bytestr: bytes) -> int: + return _bytes_to_int(bytestr.rjust(8, b"\x00"))[0] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/exc.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/exc.py new file mode 100644 index 0000000000..a75adcd527 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/exc.py @@ -0,0 +1,106 @@ +from __future__ import annotations + +import typing as t +from datetime import datetime + + +class BadData(Exception): + """Raised if bad data of any sort was encountered. This is the base + for all exceptions that ItsDangerous defines. + + .. versionadded:: 0.15 + """ + + def __init__(self, message: str): + super().__init__(message) + self.message = message + + def __str__(self) -> str: + return self.message + + +class BadSignature(BadData): + """Raised if a signature does not match.""" + + def __init__(self, message: str, payload: t.Any | None = None): + super().__init__(message) + + #: The payload that failed the signature test. In some + #: situations you might still want to inspect this, even if + #: you know it was tampered with. + #: + #: .. versionadded:: 0.14 + self.payload: t.Any | None = payload + + +class BadTimeSignature(BadSignature): + """Raised if a time-based signature is invalid. This is a subclass + of :class:`BadSignature`. + """ + + def __init__( + self, + message: str, + payload: t.Any | None = None, + date_signed: datetime | None = None, + ): + super().__init__(message, payload) + + #: If the signature expired this exposes the date of when the + #: signature was created. This can be helpful in order to + #: tell the user how long a link has been gone stale. + #: + #: .. versionchanged:: 2.0 + #: The datetime value is timezone-aware rather than naive. + #: + #: .. versionadded:: 0.14 + self.date_signed = date_signed + + +class SignatureExpired(BadTimeSignature): + """Raised if a signature timestamp is older than ``max_age``. This + is a subclass of :exc:`BadTimeSignature`. + """ + + +class BadHeader(BadSignature): + """Raised if a signed header is invalid in some form. This only + happens for serializers that have a header that goes with the + signature. + + .. versionadded:: 0.24 + """ + + def __init__( + self, + message: str, + payload: t.Any | None = None, + header: t.Any | None = None, + original_error: Exception | None = None, + ): + super().__init__(message, payload) + + #: If the header is actually available but just malformed it + #: might be stored here. + self.header: t.Any | None = header + + #: If available, the error that indicates why the payload was + #: not valid. This might be ``None``. + self.original_error: Exception | None = original_error + + +class BadPayload(BadData): + """Raised if a payload is invalid. This could happen if the payload + is loaded despite an invalid signature, or if there is a mismatch + between the serializer and deserializer. The original exception + that occurred during loading is stored on as :attr:`original_error`. + + .. versionadded:: 0.15 + """ + + def __init__(self, message: str, original_error: Exception | None = None): + super().__init__(message) + + #: If available, the error that indicates why the payload was + #: not valid. This might be ``None``. + self.original_error: Exception | None = original_error diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/py.typed b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/serializer.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/serializer.py new file mode 100644 index 0000000000..5ddf3871d8 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/serializer.py @@ -0,0 +1,406 @@ +from __future__ import annotations + +import collections.abc as cabc +import json +import typing as t + +from .encoding import want_bytes +from .exc import BadPayload +from .exc import BadSignature +from .signer import _make_keys_list +from .signer import Signer + +if t.TYPE_CHECKING: + import typing_extensions as te + + # This should be either be str or bytes. To avoid having to specify the + # bound type, it falls back to a union if structural matching fails. + _TSerialized = te.TypeVar( + "_TSerialized", bound=t.Union[str, bytes], default=t.Union[str, bytes] + ) +else: + # Still available at runtime on Python < 3.13, but without the default. + _TSerialized = t.TypeVar("_TSerialized", bound=t.Union[str, bytes]) + + +class _PDataSerializer(t.Protocol[_TSerialized]): + def loads(self, payload: _TSerialized, /) -> t.Any: ... + # A signature with additional arguments is not handled correctly by type + # checkers right now, so an overload is used below for serializers that + # don't match this strict protocol. + def dumps(self, obj: t.Any, /) -> _TSerialized: ... + + +# Use TypeIs once it's available in typing_extensions or 3.13. +def is_text_serializer( + serializer: _PDataSerializer[t.Any], +) -> te.TypeGuard[_PDataSerializer[str]]: + """Checks whether a serializer generates text or binary.""" + return isinstance(serializer.dumps({}), str) + + +class Serializer(t.Generic[_TSerialized]): + """A serializer wraps a :class:`~itsdangerous.signer.Signer` to + enable serializing and securely signing data other than bytes. It + can unsign to verify that the data hasn't been changed. + + The serializer provides :meth:`dumps` and :meth:`loads`, similar to + :mod:`json`, and by default uses :mod:`json` internally to serialize + the data to bytes. + + The secret key should be a random string of ``bytes`` and should not + be saved to code or version control. Different salts should be used + to distinguish signing in different contexts. See :doc:`/concepts` + for information about the security of the secret key and salt. + + :param secret_key: The secret key to sign and verify with. Can be a + list of keys, oldest to newest, to support key rotation. + :param salt: Extra key to combine with ``secret_key`` to distinguish + signatures in different contexts. + :param serializer: An object that provides ``dumps`` and ``loads`` + methods for serializing data to a string. Defaults to + :attr:`default_serializer`, which defaults to :mod:`json`. + :param serializer_kwargs: Keyword arguments to pass when calling + ``serializer.dumps``. + :param signer: A ``Signer`` class to instantiate when signing data. + Defaults to :attr:`default_signer`, which defaults to + :class:`~itsdangerous.signer.Signer`. + :param signer_kwargs: Keyword arguments to pass when instantiating + the ``Signer`` class. + :param fallback_signers: List of signer parameters to try when + unsigning with the default signer fails. Each item can be a dict + of ``signer_kwargs``, a ``Signer`` class, or a tuple of + ``(signer, signer_kwargs)``. Defaults to + :attr:`default_fallback_signers`. + + .. versionchanged:: 2.0 + Added support for key rotation by passing a list to + ``secret_key``. + + .. versionchanged:: 2.0 + Removed the default SHA-512 fallback signer from + ``default_fallback_signers``. + + .. versionchanged:: 1.1 + Added support for ``fallback_signers`` and configured a default + SHA-512 fallback. This fallback is for users who used the yanked + 1.0.0 release which defaulted to SHA-512. + + .. versionchanged:: 0.14 + The ``signer`` and ``signer_kwargs`` parameters were added to + the constructor. + """ + + #: The default serialization module to use to serialize data to a + #: string internally. The default is :mod:`json`, but can be changed + #: to any object that provides ``dumps`` and ``loads`` methods. + default_serializer: _PDataSerializer[t.Any] = json + + #: The default ``Signer`` class to instantiate when signing data. + #: The default is :class:`itsdangerous.signer.Signer`. + default_signer: type[Signer] = Signer + + #: The default fallback signers to try when unsigning fails. + default_fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] = [] + + # Serializer[str] if no data serializer is provided, or if it returns str. + @t.overload + def __init__( + self: Serializer[str], + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None = b"itsdangerous", + serializer: None | _PDataSerializer[str] = None, + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): ... + + # Serializer[bytes] with a bytes data serializer positional argument. + @t.overload + def __init__( + self: Serializer[bytes], + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None, + serializer: _PDataSerializer[bytes], + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): ... + + # Serializer[bytes] with a bytes data serializer keyword argument. + @t.overload + def __init__( + self: Serializer[bytes], + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None = b"itsdangerous", + *, + serializer: _PDataSerializer[bytes], + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): ... + + # Fall back with a positional argument. If the strict signature of + # _PDataSerializer doesn't match, fall back to a union, requiring the user + # to specify the type. + @t.overload + def __init__( + self, + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None, + serializer: t.Any, + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): ... + + # Fall back with a keyword argument. + @t.overload + def __init__( + self, + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None = b"itsdangerous", + *, + serializer: t.Any, + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): ... + + def __init__( + self, + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None = b"itsdangerous", + serializer: t.Any | None = None, + serializer_kwargs: dict[str, t.Any] | None = None, + signer: type[Signer] | None = None, + signer_kwargs: dict[str, t.Any] | None = None, + fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] + | None = None, + ): + #: The list of secret keys to try for verifying signatures, from + #: oldest to newest. The newest (last) key is used for signing. + #: + #: This allows a key rotation system to keep a list of allowed + #: keys and remove expired ones. + self.secret_keys: list[bytes] = _make_keys_list(secret_key) + + if salt is not None: + salt = want_bytes(salt) + # if salt is None then the signer's default is used + + self.salt = salt + + if serializer is None: + serializer = self.default_serializer + + self.serializer: _PDataSerializer[_TSerialized] = serializer + self.is_text_serializer: bool = is_text_serializer(serializer) + + if signer is None: + signer = self.default_signer + + self.signer: type[Signer] = signer + self.signer_kwargs: dict[str, t.Any] = signer_kwargs or {} + + if fallback_signers is None: + fallback_signers = list(self.default_fallback_signers) + + self.fallback_signers: list[ + dict[str, t.Any] | tuple[type[Signer], dict[str, t.Any]] | type[Signer] + ] = fallback_signers + self.serializer_kwargs: dict[str, t.Any] = serializer_kwargs or {} + + @property + def secret_key(self) -> bytes: + """The newest (last) entry in the :attr:`secret_keys` list. This + is for compatibility from before key rotation support was added. + """ + return self.secret_keys[-1] + + def load_payload( + self, payload: bytes, serializer: _PDataSerializer[t.Any] | None = None + ) -> t.Any: + """Loads the encoded object. This function raises + :class:`.BadPayload` if the payload is not valid. The + ``serializer`` parameter can be used to override the serializer + stored on the class. The encoded ``payload`` should always be + bytes. + """ + if serializer is None: + use_serializer = self.serializer + is_text = self.is_text_serializer + else: + use_serializer = serializer + is_text = is_text_serializer(serializer) + + try: + if is_text: + return use_serializer.loads(payload.decode("utf-8")) # type: ignore[arg-type] + + return use_serializer.loads(payload) # type: ignore[arg-type] + except Exception as e: + raise BadPayload( + "Could not load the payload because an exception" + " occurred on unserializing the data.", + original_error=e, + ) from e + + def dump_payload(self, obj: t.Any) -> bytes: + """Dumps the encoded object. The return value is always bytes. + If the internal serializer returns text, the value will be + encoded as UTF-8. + """ + return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs)) + + def make_signer(self, salt: str | bytes | None = None) -> Signer: + """Creates a new instance of the signer to be used. The default + implementation uses the :class:`.Signer` base class. + """ + if salt is None: + salt = self.salt + + return self.signer(self.secret_keys, salt=salt, **self.signer_kwargs) + + def iter_unsigners(self, salt: str | bytes | None = None) -> cabc.Iterator[Signer]: + """Iterates over all signers to be tried for unsigning. Starts + with the configured signer, then constructs each signer + specified in ``fallback_signers``. + """ + if salt is None: + salt = self.salt + + yield self.make_signer(salt) + + for fallback in self.fallback_signers: + if isinstance(fallback, dict): + kwargs = fallback + fallback = self.signer + elif isinstance(fallback, tuple): + fallback, kwargs = fallback + else: + kwargs = self.signer_kwargs + + for secret_key in self.secret_keys: + yield fallback(secret_key, salt=salt, **kwargs) + + def dumps(self, obj: t.Any, salt: str | bytes | None = None) -> _TSerialized: + """Returns a signed string serialized with the internal + serializer. The return value can be either a byte or unicode + string depending on the format of the internal serializer. + """ + payload = want_bytes(self.dump_payload(obj)) + rv = self.make_signer(salt).sign(payload) + + if self.is_text_serializer: + return rv.decode("utf-8") # type: ignore[return-value] + + return rv # type: ignore[return-value] + + def dump(self, obj: t.Any, f: t.IO[t.Any], salt: str | bytes | None = None) -> None: + """Like :meth:`dumps` but dumps into a file. The file handle has + to be compatible with what the internal serializer expects. + """ + f.write(self.dumps(obj, salt)) + + def loads( + self, s: str | bytes, salt: str | bytes | None = None, **kwargs: t.Any + ) -> t.Any: + """Reverse of :meth:`dumps`. Raises :exc:`.BadSignature` if the + signature validation fails. + """ + s = want_bytes(s) + last_exception = None + + for signer in self.iter_unsigners(salt): + try: + return self.load_payload(signer.unsign(s)) + except BadSignature as err: + last_exception = err + + raise t.cast(BadSignature, last_exception) + + def load(self, f: t.IO[t.Any], salt: str | bytes | None = None) -> t.Any: + """Like :meth:`loads` but loads from a file.""" + return self.loads(f.read(), salt) + + def loads_unsafe( + self, s: str | bytes, salt: str | bytes | None = None + ) -> tuple[bool, t.Any]: + """Like :meth:`loads` but without verifying the signature. This + is potentially very dangerous to use depending on how your + serializer works. The return value is ``(signature_valid, + payload)`` instead of just the payload. The first item will be a + boolean that indicates if the signature is valid. This function + never fails. + + Use it for debugging only and if you know that your serializer + module is not exploitable (for example, do not use it with a + pickle serializer). + + .. versionadded:: 0.15 + """ + return self._loads_unsafe_impl(s, salt) + + def _loads_unsafe_impl( + self, + s: str | bytes, + salt: str | bytes | None, + load_kwargs: dict[str, t.Any] | None = None, + load_payload_kwargs: dict[str, t.Any] | None = None, + ) -> tuple[bool, t.Any]: + """Low level helper function to implement :meth:`loads_unsafe` + in serializer subclasses. + """ + if load_kwargs is None: + load_kwargs = {} + + try: + return True, self.loads(s, salt=salt, **load_kwargs) + except BadSignature as e: + if e.payload is None: + return False, None + + if load_payload_kwargs is None: + load_payload_kwargs = {} + + try: + return ( + False, + self.load_payload(e.payload, **load_payload_kwargs), + ) + except BadPayload: + return False, None + + def load_unsafe( + self, f: t.IO[t.Any], salt: str | bytes | None = None + ) -> tuple[bool, t.Any]: + """Like :meth:`loads_unsafe` but loads from a file. + + .. versionadded:: 0.15 + """ + return self.loads_unsafe(f.read(), salt=salt) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/signer.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/signer.py new file mode 100644 index 0000000000..e324dc03da --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/signer.py @@ -0,0 +1,266 @@ +from __future__ import annotations + +import collections.abc as cabc +import hashlib +import hmac +import typing as t + +from .encoding import _base64_alphabet +from .encoding import base64_decode +from .encoding import base64_encode +from .encoding import want_bytes +from .exc import BadSignature + + +class SigningAlgorithm: + """Subclasses must implement :meth:`get_signature` to provide + signature generation functionality. + """ + + def get_signature(self, key: bytes, value: bytes) -> bytes: + """Returns the signature for the given key and value.""" + raise NotImplementedError() + + def verify_signature(self, key: bytes, value: bytes, sig: bytes) -> bool: + """Verifies the given signature matches the expected + signature. + """ + return hmac.compare_digest(sig, self.get_signature(key, value)) + + +class NoneAlgorithm(SigningAlgorithm): + """Provides an algorithm that does not perform any signing and + returns an empty signature. + """ + + def get_signature(self, key: bytes, value: bytes) -> bytes: + return b"" + + +def _lazy_sha1(string: bytes = b"") -> t.Any: + """Don't access ``hashlib.sha1`` until runtime. FIPS builds may not include + SHA-1, in which case the import and use as a default would fail before the + developer can configure something else. + """ + return hashlib.sha1(string) + + +class HMACAlgorithm(SigningAlgorithm): + """Provides signature generation using HMACs.""" + + #: The digest method to use with the MAC algorithm. This defaults to + #: SHA1, but can be changed to any other function in the hashlib + #: module. + default_digest_method: t.Any = staticmethod(_lazy_sha1) + + def __init__(self, digest_method: t.Any = None): + if digest_method is None: + digest_method = self.default_digest_method + + self.digest_method: t.Any = digest_method + + def get_signature(self, key: bytes, value: bytes) -> bytes: + mac = hmac.new(key, msg=value, digestmod=self.digest_method) + return mac.digest() + + +def _make_keys_list( + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], +) -> list[bytes]: + if isinstance(secret_key, (str, bytes)): + return [want_bytes(secret_key)] + + return [want_bytes(s) for s in secret_key] # pyright: ignore + + +class Signer: + """A signer securely signs bytes, then unsigns them to verify that + the value hasn't been changed. + + The secret key should be a random string of ``bytes`` and should not + be saved to code or version control. Different salts should be used + to distinguish signing in different contexts. See :doc:`/concepts` + for information about the security of the secret key and salt. + + :param secret_key: The secret key to sign and verify with. Can be a + list of keys, oldest to newest, to support key rotation. + :param salt: Extra key to combine with ``secret_key`` to distinguish + signatures in different contexts. + :param sep: Separator between the signature and value. + :param key_derivation: How to derive the signing key from the secret + key and salt. Possible values are ``concat``, ``django-concat``, + or ``hmac``. Defaults to :attr:`default_key_derivation`, which + defaults to ``django-concat``. + :param digest_method: Hash function to use when generating the HMAC + signature. Defaults to :attr:`default_digest_method`, which + defaults to :func:`hashlib.sha1`. Note that the security of the + hash alone doesn't apply when used intermediately in HMAC. + :param algorithm: A :class:`SigningAlgorithm` instance to use + instead of building a default :class:`HMACAlgorithm` with the + ``digest_method``. + + .. versionchanged:: 2.0 + Added support for key rotation by passing a list to + ``secret_key``. + + .. versionchanged:: 0.18 + ``algorithm`` was added as an argument to the class constructor. + + .. versionchanged:: 0.14 + ``key_derivation`` and ``digest_method`` were added as arguments + to the class constructor. + """ + + #: The default digest method to use for the signer. The default is + #: :func:`hashlib.sha1`, but can be changed to any :mod:`hashlib` or + #: compatible object. Note that the security of the hash alone + #: doesn't apply when used intermediately in HMAC. + #: + #: .. versionadded:: 0.14 + default_digest_method: t.Any = staticmethod(_lazy_sha1) + + #: The default scheme to use to derive the signing key from the + #: secret key and salt. The default is ``django-concat``. Possible + #: values are ``concat``, ``django-concat``, and ``hmac``. + #: + #: .. versionadded:: 0.14 + default_key_derivation: str = "django-concat" + + def __init__( + self, + secret_key: str | bytes | cabc.Iterable[str] | cabc.Iterable[bytes], + salt: str | bytes | None = b"itsdangerous.Signer", + sep: str | bytes = b".", + key_derivation: str | None = None, + digest_method: t.Any | None = None, + algorithm: SigningAlgorithm | None = None, + ): + #: The list of secret keys to try for verifying signatures, from + #: oldest to newest. The newest (last) key is used for signing. + #: + #: This allows a key rotation system to keep a list of allowed + #: keys and remove expired ones. + self.secret_keys: list[bytes] = _make_keys_list(secret_key) + self.sep: bytes = want_bytes(sep) + + if self.sep in _base64_alphabet: + raise ValueError( + "The given separator cannot be used because it may be" + " contained in the signature itself. ASCII letters," + " digits, and '-_=' must not be used." + ) + + if salt is not None: + salt = want_bytes(salt) + else: + salt = b"itsdangerous.Signer" + + self.salt = salt + + if key_derivation is None: + key_derivation = self.default_key_derivation + + self.key_derivation: str = key_derivation + + if digest_method is None: + digest_method = self.default_digest_method + + self.digest_method: t.Any = digest_method + + if algorithm is None: + algorithm = HMACAlgorithm(self.digest_method) + + self.algorithm: SigningAlgorithm = algorithm + + @property + def secret_key(self) -> bytes: + """The newest (last) entry in the :attr:`secret_keys` list. This + is for compatibility from before key rotation support was added. + """ + return self.secret_keys[-1] + + def derive_key(self, secret_key: str | bytes | None = None) -> bytes: + """This method is called to derive the key. The default key + derivation choices can be overridden here. Key derivation is not + intended to be used as a security method to make a complex key + out of a short password. Instead you should use large random + secret keys. + + :param secret_key: A specific secret key to derive from. + Defaults to the last item in :attr:`secret_keys`. + + .. versionchanged:: 2.0 + Added the ``secret_key`` parameter. + """ + if secret_key is None: + secret_key = self.secret_keys[-1] + else: + secret_key = want_bytes(secret_key) + + if self.key_derivation == "concat": + return t.cast(bytes, self.digest_method(self.salt + secret_key).digest()) + elif self.key_derivation == "django-concat": + return t.cast( + bytes, self.digest_method(self.salt + b"signer" + secret_key).digest() + ) + elif self.key_derivation == "hmac": + mac = hmac.new(secret_key, digestmod=self.digest_method) + mac.update(self.salt) + return mac.digest() + elif self.key_derivation == "none": + return secret_key + else: + raise TypeError("Unknown key derivation method") + + def get_signature(self, value: str | bytes) -> bytes: + """Returns the signature for the given value.""" + value = want_bytes(value) + key = self.derive_key() + sig = self.algorithm.get_signature(key, value) + return base64_encode(sig) + + def sign(self, value: str | bytes) -> bytes: + """Signs the given string.""" + value = want_bytes(value) + return value + self.sep + self.get_signature(value) + + def verify_signature(self, value: str | bytes, sig: str | bytes) -> bool: + """Verifies the signature for the given value.""" + try: + sig = base64_decode(sig) + except Exception: + return False + + value = want_bytes(value) + + for secret_key in reversed(self.secret_keys): + key = self.derive_key(secret_key) + + if self.algorithm.verify_signature(key, value, sig): + return True + + return False + + def unsign(self, signed_value: str | bytes) -> bytes: + """Unsigns the given string.""" + signed_value = want_bytes(signed_value) + + if self.sep not in signed_value: + raise BadSignature(f"No {self.sep!r} found in value") + + value, sig = signed_value.rsplit(self.sep, 1) + + if self.verify_signature(value, sig): + return value + + raise BadSignature(f"Signature {sig!r} does not match", payload=value) + + def validate(self, signed_value: str | bytes) -> bool: + """Only validates the given signed value. Returns ``True`` if + the signature exists and is valid. + """ + try: + self.unsign(signed_value) + return True + except BadSignature: + return False diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/timed.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/timed.py new file mode 100644 index 0000000000..73843755d9 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/timed.py @@ -0,0 +1,228 @@ +from __future__ import annotations + +import collections.abc as cabc +import time +import typing as t +from datetime import datetime +from datetime import timezone + +from .encoding import base64_decode +from .encoding import base64_encode +from .encoding import bytes_to_int +from .encoding import int_to_bytes +from .encoding import want_bytes +from .exc import BadSignature +from .exc import BadTimeSignature +from .exc import SignatureExpired +from .serializer import _TSerialized +from .serializer import Serializer +from .signer import Signer + + +class TimestampSigner(Signer): + """Works like the regular :class:`.Signer` but also records the time + of the signing and can be used to expire signatures. The + :meth:`unsign` method can raise :exc:`.SignatureExpired` if the + unsigning failed because the signature is expired. + """ + + def get_timestamp(self) -> int: + """Returns the current timestamp. The function must return an + integer. + """ + return int(time.time()) + + def timestamp_to_datetime(self, ts: int) -> datetime: + """Convert the timestamp from :meth:`get_timestamp` into an + aware :class`datetime.datetime` in UTC. + + .. versionchanged:: 2.0 + The timestamp is returned as a timezone-aware ``datetime`` + in UTC rather than a naive ``datetime`` assumed to be UTC. + """ + return datetime.fromtimestamp(ts, tz=timezone.utc) + + def sign(self, value: str | bytes) -> bytes: + """Signs the given string and also attaches time information.""" + value = want_bytes(value) + timestamp = base64_encode(int_to_bytes(self.get_timestamp())) + sep = want_bytes(self.sep) + value = value + sep + timestamp + return value + sep + self.get_signature(value) + + # Ignore overlapping signatures check, return_timestamp is the only + # parameter that affects the return type. + + @t.overload + def unsign( # type: ignore[overload-overlap] + self, + signed_value: str | bytes, + max_age: int | None = None, + return_timestamp: t.Literal[False] = False, + ) -> bytes: ... + + @t.overload + def unsign( + self, + signed_value: str | bytes, + max_age: int | None = None, + return_timestamp: t.Literal[True] = True, + ) -> tuple[bytes, datetime]: ... + + def unsign( + self, + signed_value: str | bytes, + max_age: int | None = None, + return_timestamp: bool = False, + ) -> tuple[bytes, datetime] | bytes: + """Works like the regular :meth:`.Signer.unsign` but can also + validate the time. See the base docstring of the class for + the general behavior. If ``return_timestamp`` is ``True`` the + timestamp of the signature will be returned as an aware + :class:`datetime.datetime` object in UTC. + + .. versionchanged:: 2.0 + The timestamp is returned as a timezone-aware ``datetime`` + in UTC rather than a naive ``datetime`` assumed to be UTC. + """ + try: + result = super().unsign(signed_value) + sig_error = None + except BadSignature as e: + sig_error = e + result = e.payload or b"" + + sep = want_bytes(self.sep) + + # If there is no timestamp in the result there is something + # seriously wrong. In case there was a signature error, we raise + # that one directly, otherwise we have a weird situation in + # which we shouldn't have come except someone uses a time-based + # serializer on non-timestamp data, so catch that. + if sep not in result: + if sig_error: + raise sig_error + + raise BadTimeSignature("timestamp missing", payload=result) + + value, ts_bytes = result.rsplit(sep, 1) + ts_int: int | None = None + ts_dt: datetime | None = None + + try: + ts_int = bytes_to_int(base64_decode(ts_bytes)) + except Exception: + pass + + # Signature is *not* okay. Raise a proper error now that we have + # split the value and the timestamp. + if sig_error is not None: + if ts_int is not None: + try: + ts_dt = self.timestamp_to_datetime(ts_int) + except (ValueError, OSError, OverflowError) as exc: + # Windows raises OSError + # 32-bit raises OverflowError + raise BadTimeSignature( + "Malformed timestamp", payload=value + ) from exc + + raise BadTimeSignature(str(sig_error), payload=value, date_signed=ts_dt) + + # Signature was okay but the timestamp is actually not there or + # malformed. Should not happen, but we handle it anyway. + if ts_int is None: + raise BadTimeSignature("Malformed timestamp", payload=value) + + # Check timestamp is not older than max_age + if max_age is not None: + age = self.get_timestamp() - ts_int + + if age > max_age: + raise SignatureExpired( + f"Signature age {age} > {max_age} seconds", + payload=value, + date_signed=self.timestamp_to_datetime(ts_int), + ) + + if age < 0: + raise SignatureExpired( + f"Signature age {age} < 0 seconds", + payload=value, + date_signed=self.timestamp_to_datetime(ts_int), + ) + + if return_timestamp: + return value, self.timestamp_to_datetime(ts_int) + + return value + + def validate(self, signed_value: str | bytes, max_age: int | None = None) -> bool: + """Only validates the given signed value. Returns ``True`` if + the signature exists and is valid.""" + try: + self.unsign(signed_value, max_age=max_age) + return True + except BadSignature: + return False + + +class TimedSerializer(Serializer[_TSerialized]): + """Uses :class:`TimestampSigner` instead of the default + :class:`.Signer`. + """ + + default_signer: type[TimestampSigner] = TimestampSigner + + def iter_unsigners( + self, salt: str | bytes | None = None + ) -> cabc.Iterator[TimestampSigner]: + return t.cast("cabc.Iterator[TimestampSigner]", super().iter_unsigners(salt)) + + # TODO: Signature is incompatible because parameters were added + # before salt. + + def loads( # type: ignore[override] + self, + s: str | bytes, + max_age: int | None = None, + return_timestamp: bool = False, + salt: str | bytes | None = None, + ) -> t.Any: + """Reverse of :meth:`dumps`, raises :exc:`.BadSignature` if the + signature validation fails. If a ``max_age`` is provided it will + ensure the signature is not older than that time in seconds. In + case the signature is outdated, :exc:`.SignatureExpired` is + raised. All arguments are forwarded to the signer's + :meth:`~TimestampSigner.unsign` method. + """ + s = want_bytes(s) + last_exception = None + + for signer in self.iter_unsigners(salt): + try: + base64d, timestamp = signer.unsign( + s, max_age=max_age, return_timestamp=True + ) + payload = self.load_payload(base64d) + + if return_timestamp: + return payload, timestamp + + return payload + except SignatureExpired: + # The signature was unsigned successfully but was + # expired. Do not try the next signer. + raise + except BadSignature as err: + last_exception = err + + raise t.cast(BadSignature, last_exception) + + def loads_unsafe( # type: ignore[override] + self, + s: str | bytes, + max_age: int | None = None, + salt: str | bytes | None = None, + ) -> tuple[bool, t.Any]: + return self._loads_unsafe_impl(s, salt, load_kwargs={"max_age": max_age}) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/url_safe.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/url_safe.py new file mode 100644 index 0000000000..56a0793315 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/itsdangerous/url_safe.py @@ -0,0 +1,83 @@ +from __future__ import annotations + +import typing as t +import zlib + +from ._json import _CompactJSON +from .encoding import base64_decode +from .encoding import base64_encode +from .exc import BadPayload +from .serializer import _PDataSerializer +from .serializer import Serializer +from .timed import TimedSerializer + + +class URLSafeSerializerMixin(Serializer[str]): + """Mixed in with a regular serializer it will attempt to zlib + compress the string to make it shorter if necessary. It will also + base64 encode the string so that it can safely be placed in a URL. + """ + + default_serializer: _PDataSerializer[str] = _CompactJSON + + def load_payload( + self, + payload: bytes, + *args: t.Any, + serializer: t.Any | None = None, + **kwargs: t.Any, + ) -> t.Any: + decompress = False + + if payload.startswith(b"."): + payload = payload[1:] + decompress = True + + try: + json = base64_decode(payload) + except Exception as e: + raise BadPayload( + "Could not base64 decode the payload because of an exception", + original_error=e, + ) from e + + if decompress: + try: + json = zlib.decompress(json) + except Exception as e: + raise BadPayload( + "Could not zlib decompress the payload before decoding the payload", + original_error=e, + ) from e + + return super().load_payload(json, *args, **kwargs) + + def dump_payload(self, obj: t.Any) -> bytes: + json = super().dump_payload(obj) + is_compressed = False + compressed = zlib.compress(json) + + if len(compressed) < (len(json) - 1): + json = compressed + is_compressed = True + + base64d = base64_encode(json) + + if is_compressed: + base64d = b"." + base64d + + return base64d + + +class URLSafeSerializer(URLSafeSerializerMixin, Serializer[str]): + """Works like :class:`.Serializer` but dumps and loads into a URL + safe string consisting of the upper and lowercase character of the + alphabet as well as ``'_'``, ``'-'`` and ``'.'``. + """ + + +class URLSafeTimedSerializer(URLSafeSerializerMixin, TimedSerializer[str]): + """Works like :class:`.TimedSerializer` but dumps and loads into a + URL safe string consisting of the upper and lowercase character of + the alphabet as well as ``'_'``, ``'-'`` and ``'.'``. + """ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/INSTALLER b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/INSTALLER new file mode 100644 index 0000000000..a1b589e38a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/INSTALLER @@ -0,0 +1 @@ +pip diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/METADATA b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/METADATA new file mode 100644 index 0000000000..ffef2ff3bf --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/METADATA @@ -0,0 +1,84 @@ +Metadata-Version: 2.4 +Name: Jinja2 +Version: 3.1.6 +Summary: A very fast and expressive template engine. +Maintainer-email: Pallets +Requires-Python: >=3.7 +Description-Content-Type: text/markdown +Classifier: Development Status :: 5 - Production/Stable +Classifier: Environment :: Web Environment +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: BSD License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content +Classifier: Topic :: Text Processing :: Markup :: HTML +Classifier: Typing :: Typed +License-File: LICENSE.txt +Requires-Dist: MarkupSafe>=2.0 +Requires-Dist: Babel>=2.7 ; extra == "i18n" +Project-URL: Changes, https://jinja.palletsprojects.com/changes/ +Project-URL: Chat, https://discord.gg/pallets +Project-URL: Documentation, https://jinja.palletsprojects.com/ +Project-URL: Donate, https://palletsprojects.com/donate +Project-URL: Source, https://github.com/pallets/jinja/ +Provides-Extra: i18n + +# Jinja + +Jinja is a fast, expressive, extensible templating engine. Special +placeholders in the template allow writing code similar to Python +syntax. Then the template is passed data to render the final document. + +It includes: + +- Template inheritance and inclusion. +- Define and import macros within templates. +- HTML templates can use autoescaping to prevent XSS from untrusted + user input. +- A sandboxed environment can safely render untrusted templates. +- AsyncIO support for generating templates and calling async + functions. +- I18N support with Babel. +- Templates are compiled to optimized Python code just-in-time and + cached, or can be compiled ahead-of-time. +- Exceptions point to the correct line in templates to make debugging + easier. +- Extensible filters, tests, functions, and even syntax. + +Jinja's philosophy is that while application logic belongs in Python if +possible, it shouldn't make the template designer's job difficult by +restricting functionality too much. + + +## In A Nutshell + +```jinja +{% extends "base.html" %} +{% block title %}Members{% endblock %} +{% block content %} + +{% endblock %} +``` + +## Donate + +The Pallets organization develops and supports Jinja and other popular +packages. In order to grow the community of contributors and users, and +allow the maintainers to devote more time to the projects, [please +donate today][]. + +[please donate today]: https://palletsprojects.com/donate + +## Contributing + +See our [detailed contributing documentation][contrib] for many ways to +contribute, including reporting issues, requesting features, asking or answering +questions, and making PRs. + +[contrib]: https://palletsprojects.com/contributing/ + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/RECORD b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/RECORD new file mode 100644 index 0000000000..4b48b91fa4 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/RECORD @@ -0,0 +1,57 @@ +jinja2-3.1.6.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 +jinja2-3.1.6.dist-info/METADATA,sha256=aMVUj7Z8QTKhOJjZsx7FDGvqKr3ZFdkh8hQ1XDpkmcg,2871 +jinja2-3.1.6.dist-info/RECORD,, +jinja2-3.1.6.dist-info/WHEEL,sha256=_2ozNFCLWc93bK4WKHCO-eDUENDlo-dgc9cU3qokYO4,82 +jinja2-3.1.6.dist-info/entry_points.txt,sha256=OL85gYU1eD8cuPlikifFngXpeBjaxl6rIJ8KkC_3r-I,58 +jinja2-3.1.6.dist-info/licenses/LICENSE.txt,sha256=O0nc7kEF6ze6wQ-vG-JgQI_oXSUrjp3y4JefweCUQ3s,1475 +jinja2/__init__.py,sha256=xxepO9i7DHsqkQrgBEduLtfoz2QCuT6_gbL4XSN1hbU,1928 +jinja2/__pycache__/__init__.cpython-311.pyc,, +jinja2/__pycache__/_identifier.cpython-311.pyc,, +jinja2/__pycache__/async_utils.cpython-311.pyc,, +jinja2/__pycache__/bccache.cpython-311.pyc,, +jinja2/__pycache__/compiler.cpython-311.pyc,, +jinja2/__pycache__/constants.cpython-311.pyc,, +jinja2/__pycache__/debug.cpython-311.pyc,, +jinja2/__pycache__/defaults.cpython-311.pyc,, +jinja2/__pycache__/environment.cpython-311.pyc,, +jinja2/__pycache__/exceptions.cpython-311.pyc,, +jinja2/__pycache__/ext.cpython-311.pyc,, +jinja2/__pycache__/filters.cpython-311.pyc,, +jinja2/__pycache__/idtracking.cpython-311.pyc,, +jinja2/__pycache__/lexer.cpython-311.pyc,, +jinja2/__pycache__/loaders.cpython-311.pyc,, +jinja2/__pycache__/meta.cpython-311.pyc,, +jinja2/__pycache__/nativetypes.cpython-311.pyc,, +jinja2/__pycache__/nodes.cpython-311.pyc,, +jinja2/__pycache__/optimizer.cpython-311.pyc,, +jinja2/__pycache__/parser.cpython-311.pyc,, +jinja2/__pycache__/runtime.cpython-311.pyc,, +jinja2/__pycache__/sandbox.cpython-311.pyc,, +jinja2/__pycache__/tests.cpython-311.pyc,, +jinja2/__pycache__/utils.cpython-311.pyc,, +jinja2/__pycache__/visitor.cpython-311.pyc,, +jinja2/_identifier.py,sha256=_zYctNKzRqlk_murTNlzrju1FFJL7Va_Ijqqd7ii2lU,1958 +jinja2/async_utils.py,sha256=vK-PdsuorOMnWSnEkT3iUJRIkTnYgO2T6MnGxDgHI5o,2834 +jinja2/bccache.py,sha256=gh0qs9rulnXo0PhX5jTJy2UHzI8wFnQ63o_vw7nhzRg,14061 +jinja2/compiler.py,sha256=9RpCQl5X88BHllJiPsHPh295Hh0uApvwFJNQuutULeM,74131 +jinja2/constants.py,sha256=GMoFydBF_kdpaRKPoM5cl5MviquVRLVyZtfp5-16jg0,1433 +jinja2/debug.py,sha256=CnHqCDHd-BVGvti_8ZsTolnXNhA3ECsY-6n_2pwU8Hw,6297 +jinja2/defaults.py,sha256=boBcSw78h-lp20YbaXSJsqkAI2uN_mD_TtCydpeq5wU,1267 +jinja2/environment.py,sha256=9nhrP7Ch-NbGX00wvyr4yy-uhNHq2OCc60ggGrni_fk,61513 +jinja2/exceptions.py,sha256=ioHeHrWwCWNaXX1inHmHVblvc4haO7AXsjCp3GfWvx0,5071 +jinja2/ext.py,sha256=5PF5eHfh8mXAIxXHHRB2xXbXohi8pE3nHSOxa66uS7E,31875 +jinja2/filters.py,sha256=PQ_Egd9n9jSgtnGQYyF4K5j2nYwhUIulhPnyimkdr-k,55212 +jinja2/idtracking.py,sha256=-ll5lIp73pML3ErUYiIJj7tdmWxcH_IlDv3yA_hiZYo,10555 +jinja2/lexer.py,sha256=LYiYio6br-Tep9nPcupWXsPEtjluw3p1mU-lNBVRUfk,29786 +jinja2/loaders.py,sha256=wIrnxjvcbqh5VwW28NSkfotiDq8qNCxIOSFbGUiSLB4,24055 +jinja2/meta.py,sha256=OTDPkaFvU2Hgvx-6akz7154F8BIWaRmvJcBFvwopHww,4397 +jinja2/nativetypes.py,sha256=7GIGALVJgdyL80oZJdQUaUfwSt5q2lSSZbXt0dNf_M4,4210 +jinja2/nodes.py,sha256=m1Duzcr6qhZI8JQ6VyJgUNinjAf5bQzijSmDnMsvUx8,34579 +jinja2/optimizer.py,sha256=rJnCRlQ7pZsEEmMhsQDgC_pKyDHxP5TPS6zVPGsgcu8,1651 +jinja2/parser.py,sha256=lLOFy3sEmHc5IaEHRiH1sQVnId2moUQzhyeJZTtdY30,40383 +jinja2/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 +jinja2/runtime.py,sha256=gDk-GvdriJXqgsGbHgrcKTP0Yp6zPXzhzrIpCFH3jAU,34249 +jinja2/sandbox.py,sha256=Mw2aitlY2I8la7FYhcX2YG9BtUYcLnD0Gh3d29cDWrY,15009 +jinja2/tests.py,sha256=VLsBhVFnWg-PxSBz1MhRnNWgP1ovXk3neO1FLQMeC9Q,5926 +jinja2/utils.py,sha256=rRp3o9e7ZKS4fyrWRbELyLcpuGVTFcnooaOa1qx_FIk,24129 +jinja2/visitor.py,sha256=EcnL1PIwf_4RVCOMxsRNuR8AXHbS1qfAdMOE2ngKJz4,3557 diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/WHEEL b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/WHEEL new file mode 100644 index 0000000000..23d2d7e9a5 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/WHEEL @@ -0,0 +1,4 @@ +Wheel-Version: 1.0 +Generator: flit 3.11.0 +Root-Is-Purelib: true +Tag: py3-none-any diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/entry_points.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/entry_points.txt new file mode 100644 index 0000000000..abc3eae3b3 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/entry_points.txt @@ -0,0 +1,3 @@ +[babel.extractors] +jinja2=jinja2.ext:babel_extract[i18n] + diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/licenses/LICENSE.txt b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/licenses/LICENSE.txt new file mode 100644 index 0000000000..c37cae49ec --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2-3.1.6.dist-info/licenses/LICENSE.txt @@ -0,0 +1,28 @@ +Copyright 2007 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__init__.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__init__.py new file mode 100644 index 0000000000..1a423a3eac --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__init__.py @@ -0,0 +1,38 @@ +"""Jinja is a template engine written in pure Python. It provides a +non-XML syntax that supports inline expressions and an optional +sandboxed environment. +""" + +from .bccache import BytecodeCache as BytecodeCache +from .bccache import FileSystemBytecodeCache as FileSystemBytecodeCache +from .bccache import MemcachedBytecodeCache as MemcachedBytecodeCache +from .environment import Environment as Environment +from .environment import Template as Template +from .exceptions import TemplateAssertionError as TemplateAssertionError +from .exceptions import TemplateError as TemplateError +from .exceptions import TemplateNotFound as TemplateNotFound +from .exceptions import TemplateRuntimeError as TemplateRuntimeError +from .exceptions import TemplatesNotFound as TemplatesNotFound +from .exceptions import TemplateSyntaxError as TemplateSyntaxError +from .exceptions import UndefinedError as UndefinedError +from .loaders import BaseLoader as BaseLoader +from .loaders import ChoiceLoader as ChoiceLoader +from .loaders import DictLoader as DictLoader +from .loaders import FileSystemLoader as FileSystemLoader +from .loaders import FunctionLoader as FunctionLoader +from .loaders import ModuleLoader as ModuleLoader +from .loaders import PackageLoader as PackageLoader +from .loaders import PrefixLoader as PrefixLoader +from .runtime import ChainableUndefined as ChainableUndefined +from .runtime import DebugUndefined as DebugUndefined +from .runtime import make_logging_undefined as make_logging_undefined +from .runtime import StrictUndefined as StrictUndefined +from .runtime import Undefined as Undefined +from .utils import clear_caches as clear_caches +from .utils import is_undefined as is_undefined +from .utils import pass_context as pass_context +from .utils import pass_environment as pass_environment +from .utils import pass_eval_context as pass_eval_context +from .utils import select_autoescape as select_autoescape + +__version__ = "3.1.6" diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/__init__.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000..46e5f3b8e2 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/__init__.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/_identifier.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/_identifier.cpython-311.pyc new file mode 100644 index 0000000000..8ffe8539c8 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/_identifier.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/async_utils.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/async_utils.cpython-311.pyc new file mode 100644 index 0000000000..ae456c6a98 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/async_utils.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/bccache.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/bccache.cpython-311.pyc new file mode 100644 index 0000000000..ccb0e54132 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/bccache.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/compiler.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/compiler.cpython-311.pyc new file mode 100644 index 0000000000..11e395f0bc Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/compiler.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/constants.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/constants.cpython-311.pyc new file mode 100644 index 0000000000..ac8329befc Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/constants.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/debug.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/debug.cpython-311.pyc new file mode 100644 index 0000000000..eba721bdc9 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/debug.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/defaults.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/defaults.cpython-311.pyc new file mode 100644 index 0000000000..1a040aef67 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/defaults.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/environment.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/environment.cpython-311.pyc new file mode 100644 index 0000000000..80beba3d3c Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/environment.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/exceptions.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/exceptions.cpython-311.pyc new file mode 100644 index 0000000000..eb3f443617 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/exceptions.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/ext.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/ext.cpython-311.pyc new file mode 100644 index 0000000000..611c7ea3da Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/ext.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/filters.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/filters.cpython-311.pyc new file mode 100644 index 0000000000..2894d5c8ba Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/filters.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/idtracking.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/idtracking.cpython-311.pyc new file mode 100644 index 0000000000..eeecc9d849 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/idtracking.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/lexer.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/lexer.cpython-311.pyc new file mode 100644 index 0000000000..77fc03ada9 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/lexer.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/loaders.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/loaders.cpython-311.pyc new file mode 100644 index 0000000000..931f5dc1af Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/loaders.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/meta.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/meta.cpython-311.pyc new file mode 100644 index 0000000000..322c22ba9e Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/meta.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nativetypes.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nativetypes.cpython-311.pyc new file mode 100644 index 0000000000..f2cf769d5e Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nativetypes.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nodes.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nodes.cpython-311.pyc new file mode 100644 index 0000000000..d3d406b339 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/nodes.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/optimizer.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/optimizer.cpython-311.pyc new file mode 100644 index 0000000000..2a4c9187cd Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/optimizer.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/parser.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/parser.cpython-311.pyc new file mode 100644 index 0000000000..b906d22c23 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/parser.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/runtime.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/runtime.cpython-311.pyc new file mode 100644 index 0000000000..b0e1c44dcd Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/runtime.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/sandbox.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/sandbox.cpython-311.pyc new file mode 100644 index 0000000000..d091ccdfb3 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/sandbox.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/tests.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/tests.cpython-311.pyc new file mode 100644 index 0000000000..00db3cc6d9 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/tests.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/utils.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/utils.cpython-311.pyc new file mode 100644 index 0000000000..119b06d7f9 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/utils.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/visitor.cpython-311.pyc b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/visitor.cpython-311.pyc new file mode 100644 index 0000000000..f031717ee5 Binary files /dev/null and b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/__pycache__/visitor.cpython-311.pyc differ diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/_identifier.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/_identifier.py new file mode 100644 index 0000000000..928c1503c7 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/_identifier.py @@ -0,0 +1,6 @@ +import re + +# generated by scripts/generate_identifier_pattern.py +pattern = re.compile( + r"[\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߽߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛࣓-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣ৾ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣૺ-૿ଁ-ଃ଼ା-ୄେୈୋ-୍ୖୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఄా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഀ-ഃ഻഼ാ-ൄെ-ൈൊ-്ൗൢൣංඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ູົຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭ᳲ-᳴᳷-᳹᷀-᷹᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꣿꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𐴤-𐽆𐴧-𐽐𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑄴𑅅𑅆𑅳𑆀-𑆂𑆳-𑇀𑇉-𑇌𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌻𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑑞𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑠬-𑠺𑨁-𑨊𑨳-𑨹𑨻-𑨾𑩇𑩑-𑩛𑪊-𑪙𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𑴱-𑴶𑴺𑴼𑴽𑴿-𑵅𑵇𑶊-𑶎𑶐𑶑𑶓-𑶗𑻳-𑻶𖫰-𖫴𖬰-𖬶𖽑-𖽾𖾏-𖾒𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞣐𞀪-𞣖𞥄-𞥊󠄀-󠇯]+" # noqa: B950 +) diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/async_utils.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/async_utils.py new file mode 100644 index 0000000000..f0c140205c --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/async_utils.py @@ -0,0 +1,99 @@ +import inspect +import typing as t +from functools import WRAPPER_ASSIGNMENTS +from functools import wraps + +from .utils import _PassArg +from .utils import pass_eval_context + +if t.TYPE_CHECKING: + import typing_extensions as te + +V = t.TypeVar("V") + + +def async_variant(normal_func): # type: ignore + def decorator(async_func): # type: ignore + pass_arg = _PassArg.from_obj(normal_func) + need_eval_context = pass_arg is None + + if pass_arg is _PassArg.environment: + + def is_async(args: t.Any) -> bool: + return t.cast(bool, args[0].is_async) + + else: + + def is_async(args: t.Any) -> bool: + return t.cast(bool, args[0].environment.is_async) + + # Take the doc and annotations from the sync function, but the + # name from the async function. Pallets-Sphinx-Themes + # build_function_directive expects __wrapped__ to point to the + # sync function. + async_func_attrs = ("__module__", "__name__", "__qualname__") + normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs)) + + @wraps(normal_func, assigned=normal_func_attrs) + @wraps(async_func, assigned=async_func_attrs, updated=()) + def wrapper(*args, **kwargs): # type: ignore + b = is_async(args) + + if need_eval_context: + args = args[1:] + + if b: + return async_func(*args, **kwargs) + + return normal_func(*args, **kwargs) + + if need_eval_context: + wrapper = pass_eval_context(wrapper) + + wrapper.jinja_async_variant = True # type: ignore[attr-defined] + return wrapper + + return decorator + + +_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)} + + +async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V": + # Avoid a costly call to isawaitable + if type(value) in _common_primitives: + return t.cast("V", value) + + if inspect.isawaitable(value): + return await t.cast("t.Awaitable[V]", value) + + return value + + +class _IteratorToAsyncIterator(t.Generic[V]): + def __init__(self, iterator: "t.Iterator[V]"): + self._iterator = iterator + + def __aiter__(self) -> "te.Self": + return self + + async def __anext__(self) -> V: + try: + return next(self._iterator) + except StopIteration as e: + raise StopAsyncIteration(e.value) from e + + +def auto_aiter( + iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]", +) -> "t.AsyncIterator[V]": + if hasattr(iterable, "__aiter__"): + return iterable.__aiter__() + else: + return _IteratorToAsyncIterator(iter(iterable)) + + +async def auto_to_list( + value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]", +) -> t.List["V"]: + return [x async for x in auto_aiter(value)] diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/bccache.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/bccache.py new file mode 100644 index 0000000000..ada8b099ff --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/bccache.py @@ -0,0 +1,408 @@ +"""The optional bytecode cache system. This is useful if you have very +complex template situations and the compilation of all those templates +slows down your application too much. + +Situations where this is useful are often forking web applications that +are initialized on the first request. +""" + +import errno +import fnmatch +import marshal +import os +import pickle +import stat +import sys +import tempfile +import typing as t +from hashlib import sha1 +from io import BytesIO +from types import CodeType + +if t.TYPE_CHECKING: + import typing_extensions as te + + from .environment import Environment + + class _MemcachedClient(te.Protocol): + def get(self, key: str) -> bytes: ... + + def set( + self, key: str, value: bytes, timeout: t.Optional[int] = None + ) -> None: ... + + +bc_version = 5 +# Magic bytes to identify Jinja bytecode cache files. Contains the +# Python major and minor version to avoid loading incompatible bytecode +# if a project upgrades its Python version. +bc_magic = ( + b"j2" + + pickle.dumps(bc_version, 2) + + pickle.dumps((sys.version_info[0] << 24) | sys.version_info[1], 2) +) + + +class Bucket: + """Buckets are used to store the bytecode for one template. It's created + and initialized by the bytecode cache and passed to the loading functions. + + The buckets get an internal checksum from the cache assigned and use this + to automatically reject outdated cache material. Individual bytecode + cache subclasses don't have to care about cache invalidation. + """ + + def __init__(self, environment: "Environment", key: str, checksum: str) -> None: + self.environment = environment + self.key = key + self.checksum = checksum + self.reset() + + def reset(self) -> None: + """Resets the bucket (unloads the bytecode).""" + self.code: t.Optional[CodeType] = None + + def load_bytecode(self, f: t.BinaryIO) -> None: + """Loads bytecode from a file or file like object.""" + # make sure the magic header is correct + magic = f.read(len(bc_magic)) + if magic != bc_magic: + self.reset() + return + # the source code of the file changed, we need to reload + checksum = pickle.load(f) + if self.checksum != checksum: + self.reset() + return + # if marshal_load fails then we need to reload + try: + self.code = marshal.load(f) + except (EOFError, ValueError, TypeError): + self.reset() + return + + def write_bytecode(self, f: t.IO[bytes]) -> None: + """Dump the bytecode into the file or file like object passed.""" + if self.code is None: + raise TypeError("can't write empty bucket") + f.write(bc_magic) + pickle.dump(self.checksum, f, 2) + marshal.dump(self.code, f) + + def bytecode_from_string(self, string: bytes) -> None: + """Load bytecode from bytes.""" + self.load_bytecode(BytesIO(string)) + + def bytecode_to_string(self) -> bytes: + """Return the bytecode as bytes.""" + out = BytesIO() + self.write_bytecode(out) + return out.getvalue() + + +class BytecodeCache: + """To implement your own bytecode cache you have to subclass this class + and override :meth:`load_bytecode` and :meth:`dump_bytecode`. Both of + these methods are passed a :class:`~jinja2.bccache.Bucket`. + + A very basic bytecode cache that saves the bytecode on the file system:: + + from os import path + + class MyCache(BytecodeCache): + + def __init__(self, directory): + self.directory = directory + + def load_bytecode(self, bucket): + filename = path.join(self.directory, bucket.key) + if path.exists(filename): + with open(filename, 'rb') as f: + bucket.load_bytecode(f) + + def dump_bytecode(self, bucket): + filename = path.join(self.directory, bucket.key) + with open(filename, 'wb') as f: + bucket.write_bytecode(f) + + A more advanced version of a filesystem based bytecode cache is part of + Jinja. + """ + + def load_bytecode(self, bucket: Bucket) -> None: + """Subclasses have to override this method to load bytecode into a + bucket. If they are not able to find code in the cache for the + bucket, it must not do anything. + """ + raise NotImplementedError() + + def dump_bytecode(self, bucket: Bucket) -> None: + """Subclasses have to override this method to write the bytecode + from a bucket back to the cache. If it unable to do so it must not + fail silently but raise an exception. + """ + raise NotImplementedError() + + def clear(self) -> None: + """Clears the cache. This method is not used by Jinja but should be + implemented to allow applications to clear the bytecode cache used + by a particular environment. + """ + + def get_cache_key( + self, name: str, filename: t.Optional[t.Union[str]] = None + ) -> str: + """Returns the unique hash key for this template name.""" + hash = sha1(name.encode("utf-8")) + + if filename is not None: + hash.update(f"|{filename}".encode()) + + return hash.hexdigest() + + def get_source_checksum(self, source: str) -> str: + """Returns a checksum for the source.""" + return sha1(source.encode("utf-8")).hexdigest() + + def get_bucket( + self, + environment: "Environment", + name: str, + filename: t.Optional[str], + source: str, + ) -> Bucket: + """Return a cache bucket for the given template. All arguments are + mandatory but filename may be `None`. + """ + key = self.get_cache_key(name, filename) + checksum = self.get_source_checksum(source) + bucket = Bucket(environment, key, checksum) + self.load_bytecode(bucket) + return bucket + + def set_bucket(self, bucket: Bucket) -> None: + """Put the bucket into the cache.""" + self.dump_bytecode(bucket) + + +class FileSystemBytecodeCache(BytecodeCache): + """A bytecode cache that stores bytecode on the filesystem. It accepts + two arguments: The directory where the cache items are stored and a + pattern string that is used to build the filename. + + If no directory is specified a default cache directory is selected. On + Windows the user's temp directory is used, on UNIX systems a directory + is created for the user in the system temp directory. + + The pattern can be used to have multiple separate caches operate on the + same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s`` + is replaced with the cache key. + + >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache') + + This bytecode cache supports clearing of the cache using the clear method. + """ + + def __init__( + self, directory: t.Optional[str] = None, pattern: str = "__jinja2_%s.cache" + ) -> None: + if directory is None: + directory = self._get_default_cache_dir() + self.directory = directory + self.pattern = pattern + + def _get_default_cache_dir(self) -> str: + def _unsafe_dir() -> "te.NoReturn": + raise RuntimeError( + "Cannot determine safe temp directory. You " + "need to explicitly provide one." + ) + + tmpdir = tempfile.gettempdir() + + # On windows the temporary directory is used specific unless + # explicitly forced otherwise. We can just use that. + if os.name == "nt": + return tmpdir + if not hasattr(os, "getuid"): + _unsafe_dir() + + dirname = f"_jinja2-cache-{os.getuid()}" + actual_dir = os.path.join(tmpdir, dirname) + + try: + os.mkdir(actual_dir, stat.S_IRWXU) + except OSError as e: + if e.errno != errno.EEXIST: + raise + try: + os.chmod(actual_dir, stat.S_IRWXU) + actual_dir_stat = os.lstat(actual_dir) + if ( + actual_dir_stat.st_uid != os.getuid() + or not stat.S_ISDIR(actual_dir_stat.st_mode) + or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU + ): + _unsafe_dir() + except OSError as e: + if e.errno != errno.EEXIST: + raise + + actual_dir_stat = os.lstat(actual_dir) + if ( + actual_dir_stat.st_uid != os.getuid() + or not stat.S_ISDIR(actual_dir_stat.st_mode) + or stat.S_IMODE(actual_dir_stat.st_mode) != stat.S_IRWXU + ): + _unsafe_dir() + + return actual_dir + + def _get_cache_filename(self, bucket: Bucket) -> str: + return os.path.join(self.directory, self.pattern % (bucket.key,)) + + def load_bytecode(self, bucket: Bucket) -> None: + filename = self._get_cache_filename(bucket) + + # Don't test for existence before opening the file, since the + # file could disappear after the test before the open. + try: + f = open(filename, "rb") + except (FileNotFoundError, IsADirectoryError, PermissionError): + # PermissionError can occur on Windows when an operation is + # in progress, such as calling clear(). + return + + with f: + bucket.load_bytecode(f) + + def dump_bytecode(self, bucket: Bucket) -> None: + # Write to a temporary file, then rename to the real name after + # writing. This avoids another process reading the file before + # it is fully written. + name = self._get_cache_filename(bucket) + f = tempfile.NamedTemporaryFile( + mode="wb", + dir=os.path.dirname(name), + prefix=os.path.basename(name), + suffix=".tmp", + delete=False, + ) + + def remove_silent() -> None: + try: + os.remove(f.name) + except OSError: + # Another process may have called clear(). On Windows, + # another program may be holding the file open. + pass + + try: + with f: + bucket.write_bytecode(f) + except BaseException: + remove_silent() + raise + + try: + os.replace(f.name, name) + except OSError: + # Another process may have called clear(). On Windows, + # another program may be holding the file open. + remove_silent() + except BaseException: + remove_silent() + raise + + def clear(self) -> None: + # imported lazily here because google app-engine doesn't support + # write access on the file system and the function does not exist + # normally. + from os import remove + + files = fnmatch.filter(os.listdir(self.directory), self.pattern % ("*",)) + for filename in files: + try: + remove(os.path.join(self.directory, filename)) + except OSError: + pass + + +class MemcachedBytecodeCache(BytecodeCache): + """This class implements a bytecode cache that uses a memcache cache for + storing the information. It does not enforce a specific memcache library + (tummy's memcache or cmemcache) but will accept any class that provides + the minimal interface required. + + Libraries compatible with this class: + + - `cachelib `_ + - `python-memcached `_ + + (Unfortunately the django cache interface is not compatible because it + does not support storing binary data, only text. You can however pass + the underlying cache client to the bytecode cache which is available + as `django.core.cache.cache._client`.) + + The minimal interface for the client passed to the constructor is this: + + .. class:: MinimalClientInterface + + .. method:: set(key, value[, timeout]) + + Stores the bytecode in the cache. `value` is a string and + `timeout` the timeout of the key. If timeout is not provided + a default timeout or no timeout should be assumed, if it's + provided it's an integer with the number of seconds the cache + item should exist. + + .. method:: get(key) + + Returns the value for the cache key. If the item does not + exist in the cache the return value must be `None`. + + The other arguments to the constructor are the prefix for all keys that + is added before the actual cache key and the timeout for the bytecode in + the cache system. We recommend a high (or no) timeout. + + This bytecode cache does not support clearing of used items in the cache. + The clear method is a no-operation function. + + .. versionadded:: 2.7 + Added support for ignoring memcache errors through the + `ignore_memcache_errors` parameter. + """ + + def __init__( + self, + client: "_MemcachedClient", + prefix: str = "jinja2/bytecode/", + timeout: t.Optional[int] = None, + ignore_memcache_errors: bool = True, + ): + self.client = client + self.prefix = prefix + self.timeout = timeout + self.ignore_memcache_errors = ignore_memcache_errors + + def load_bytecode(self, bucket: Bucket) -> None: + try: + code = self.client.get(self.prefix + bucket.key) + except Exception: + if not self.ignore_memcache_errors: + raise + else: + bucket.bytecode_from_string(code) + + def dump_bytecode(self, bucket: Bucket) -> None: + key = self.prefix + bucket.key + value = bucket.bytecode_to_string() + + try: + if self.timeout is not None: + self.client.set(key, value, self.timeout) + else: + self.client.set(key, value) + except Exception: + if not self.ignore_memcache_errors: + raise diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/compiler.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/compiler.py new file mode 100644 index 0000000000..a4ff6a1b11 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/compiler.py @@ -0,0 +1,1998 @@ +"""Compiles nodes from the parser into Python code.""" + +import typing as t +from contextlib import contextmanager +from functools import update_wrapper +from io import StringIO +from itertools import chain +from keyword import iskeyword as is_python_keyword + +from markupsafe import escape +from markupsafe import Markup + +from . import nodes +from .exceptions import TemplateAssertionError +from .idtracking import Symbols +from .idtracking import VAR_LOAD_ALIAS +from .idtracking import VAR_LOAD_PARAMETER +from .idtracking import VAR_LOAD_RESOLVE +from .idtracking import VAR_LOAD_UNDEFINED +from .nodes import EvalContext +from .optimizer import Optimizer +from .utils import _PassArg +from .utils import concat +from .visitor import NodeVisitor + +if t.TYPE_CHECKING: + import typing_extensions as te + + from .environment import Environment + +F = t.TypeVar("F", bound=t.Callable[..., t.Any]) + +operators = { + "eq": "==", + "ne": "!=", + "gt": ">", + "gteq": ">=", + "lt": "<", + "lteq": "<=", + "in": "in", + "notin": "not in", +} + + +def optimizeconst(f: F) -> F: + def new_func( + self: "CodeGenerator", node: nodes.Expr, frame: "Frame", **kwargs: t.Any + ) -> t.Any: + # Only optimize if the frame is not volatile + if self.optimizer is not None and not frame.eval_ctx.volatile: + new_node = self.optimizer.visit(node, frame.eval_ctx) + + if new_node != node: + return self.visit(new_node, frame) + + return f(self, node, frame, **kwargs) + + return update_wrapper(new_func, f) # type: ignore[return-value] + + +def _make_binop(op: str) -> t.Callable[["CodeGenerator", nodes.BinExpr, "Frame"], None]: + @optimizeconst + def visitor(self: "CodeGenerator", node: nodes.BinExpr, frame: Frame) -> None: + if ( + self.environment.sandboxed and op in self.environment.intercepted_binops # type: ignore + ): + self.write(f"environment.call_binop(context, {op!r}, ") + self.visit(node.left, frame) + self.write(", ") + self.visit(node.right, frame) + else: + self.write("(") + self.visit(node.left, frame) + self.write(f" {op} ") + self.visit(node.right, frame) + + self.write(")") + + return visitor + + +def _make_unop( + op: str, +) -> t.Callable[["CodeGenerator", nodes.UnaryExpr, "Frame"], None]: + @optimizeconst + def visitor(self: "CodeGenerator", node: nodes.UnaryExpr, frame: Frame) -> None: + if ( + self.environment.sandboxed and op in self.environment.intercepted_unops # type: ignore + ): + self.write(f"environment.call_unop(context, {op!r}, ") + self.visit(node.node, frame) + else: + self.write("(" + op) + self.visit(node.node, frame) + + self.write(")") + + return visitor + + +def generate( + node: nodes.Template, + environment: "Environment", + name: t.Optional[str], + filename: t.Optional[str], + stream: t.Optional[t.TextIO] = None, + defer_init: bool = False, + optimized: bool = True, +) -> t.Optional[str]: + """Generate the python source for a node tree.""" + if not isinstance(node, nodes.Template): + raise TypeError("Can't compile non template nodes") + + generator = environment.code_generator_class( + environment, name, filename, stream, defer_init, optimized + ) + generator.visit(node) + + if stream is None: + return generator.stream.getvalue() # type: ignore + + return None + + +def has_safe_repr(value: t.Any) -> bool: + """Does the node have a safe representation?""" + if value is None or value is NotImplemented or value is Ellipsis: + return True + + if type(value) in {bool, int, float, complex, range, str, Markup}: + return True + + if type(value) in {tuple, list, set, frozenset}: + return all(has_safe_repr(v) for v in value) + + if type(value) is dict: # noqa E721 + return all(has_safe_repr(k) and has_safe_repr(v) for k, v in value.items()) + + return False + + +def find_undeclared( + nodes: t.Iterable[nodes.Node], names: t.Iterable[str] +) -> t.Set[str]: + """Check if the names passed are accessed undeclared. The return value + is a set of all the undeclared names from the sequence of names found. + """ + visitor = UndeclaredNameVisitor(names) + try: + for node in nodes: + visitor.visit(node) + except VisitorExit: + pass + return visitor.undeclared + + +class MacroRef: + def __init__(self, node: t.Union[nodes.Macro, nodes.CallBlock]) -> None: + self.node = node + self.accesses_caller = False + self.accesses_kwargs = False + self.accesses_varargs = False + + +class Frame: + """Holds compile time information for us.""" + + def __init__( + self, + eval_ctx: EvalContext, + parent: t.Optional["Frame"] = None, + level: t.Optional[int] = None, + ) -> None: + self.eval_ctx = eval_ctx + + # the parent of this frame + self.parent = parent + + if parent is None: + self.symbols = Symbols(level=level) + + # in some dynamic inheritance situations the compiler needs to add + # write tests around output statements. + self.require_output_check = False + + # inside some tags we are using a buffer rather than yield statements. + # this for example affects {% filter %} or {% macro %}. If a frame + # is buffered this variable points to the name of the list used as + # buffer. + self.buffer: t.Optional[str] = None + + # the name of the block we're in, otherwise None. + self.block: t.Optional[str] = None + + else: + self.symbols = Symbols(parent.symbols, level=level) + self.require_output_check = parent.require_output_check + self.buffer = parent.buffer + self.block = parent.block + + # a toplevel frame is the root + soft frames such as if conditions. + self.toplevel = False + + # the root frame is basically just the outermost frame, so no if + # conditions. This information is used to optimize inheritance + # situations. + self.rootlevel = False + + # variables set inside of loops and blocks should not affect outer frames, + # but they still needs to be kept track of as part of the active context. + self.loop_frame = False + self.block_frame = False + + # track whether the frame is being used in an if-statement or conditional + # expression as it determines which errors should be raised during runtime + # or compile time. + self.soft_frame = False + + def copy(self) -> "te.Self": + """Create a copy of the current one.""" + rv = object.__new__(self.__class__) + rv.__dict__.update(self.__dict__) + rv.symbols = self.symbols.copy() + return rv + + def inner(self, isolated: bool = False) -> "Frame": + """Return an inner frame.""" + if isolated: + return Frame(self.eval_ctx, level=self.symbols.level + 1) + return Frame(self.eval_ctx, self) + + def soft(self) -> "te.Self": + """Return a soft frame. A soft frame may not be modified as + standalone thing as it shares the resources with the frame it + was created of, but it's not a rootlevel frame any longer. + + This is only used to implement if-statements and conditional + expressions. + """ + rv = self.copy() + rv.rootlevel = False + rv.soft_frame = True + return rv + + __copy__ = copy + + +class VisitorExit(RuntimeError): + """Exception used by the `UndeclaredNameVisitor` to signal a stop.""" + + +class DependencyFinderVisitor(NodeVisitor): + """A visitor that collects filter and test calls.""" + + def __init__(self) -> None: + self.filters: t.Set[str] = set() + self.tests: t.Set[str] = set() + + def visit_Filter(self, node: nodes.Filter) -> None: + self.generic_visit(node) + self.filters.add(node.name) + + def visit_Test(self, node: nodes.Test) -> None: + self.generic_visit(node) + self.tests.add(node.name) + + def visit_Block(self, node: nodes.Block) -> None: + """Stop visiting at blocks.""" + + +class UndeclaredNameVisitor(NodeVisitor): + """A visitor that checks if a name is accessed without being + declared. This is different from the frame visitor as it will + not stop at closure frames. + """ + + def __init__(self, names: t.Iterable[str]) -> None: + self.names = set(names) + self.undeclared: t.Set[str] = set() + + def visit_Name(self, node: nodes.Name) -> None: + if node.ctx == "load" and node.name in self.names: + self.undeclared.add(node.name) + if self.undeclared == self.names: + raise VisitorExit() + else: + self.names.discard(node.name) + + def visit_Block(self, node: nodes.Block) -> None: + """Stop visiting a blocks.""" + + +class CompilerExit(Exception): + """Raised if the compiler encountered a situation where it just + doesn't make sense to further process the code. Any block that + raises such an exception is not further processed. + """ + + +class CodeGenerator(NodeVisitor): + def __init__( + self, + environment: "Environment", + name: t.Optional[str], + filename: t.Optional[str], + stream: t.Optional[t.TextIO] = None, + defer_init: bool = False, + optimized: bool = True, + ) -> None: + if stream is None: + stream = StringIO() + self.environment = environment + self.name = name + self.filename = filename + self.stream = stream + self.created_block_context = False + self.defer_init = defer_init + self.optimizer: t.Optional[Optimizer] = None + + if optimized: + self.optimizer = Optimizer(environment) + + # aliases for imports + self.import_aliases: t.Dict[str, str] = {} + + # a registry for all blocks. Because blocks are moved out + # into the global python scope they are registered here + self.blocks: t.Dict[str, nodes.Block] = {} + + # the number of extends statements so far + self.extends_so_far = 0 + + # some templates have a rootlevel extends. In this case we + # can safely assume that we're a child template and do some + # more optimizations. + self.has_known_extends = False + + # the current line number + self.code_lineno = 1 + + # registry of all filters and tests (global, not block local) + self.tests: t.Dict[str, str] = {} + self.filters: t.Dict[str, str] = {} + + # the debug information + self.debug_info: t.List[t.Tuple[int, int]] = [] + self._write_debug_info: t.Optional[int] = None + + # the number of new lines before the next write() + self._new_lines = 0 + + # the line number of the last written statement + self._last_line = 0 + + # true if nothing was written so far. + self._first_write = True + + # used by the `temporary_identifier` method to get new + # unique, temporary identifier + self._last_identifier = 0 + + # the current indentation + self._indentation = 0 + + # Tracks toplevel assignments + self._assign_stack: t.List[t.Set[str]] = [] + + # Tracks parameter definition blocks + self._param_def_block: t.List[t.Set[str]] = [] + + # Tracks the current context. + self._context_reference_stack = ["context"] + + @property + def optimized(self) -> bool: + return self.optimizer is not None + + # -- Various compilation helpers + + def fail(self, msg: str, lineno: int) -> "te.NoReturn": + """Fail with a :exc:`TemplateAssertionError`.""" + raise TemplateAssertionError(msg, lineno, self.name, self.filename) + + def temporary_identifier(self) -> str: + """Get a new unique identifier.""" + self._last_identifier += 1 + return f"t_{self._last_identifier}" + + def buffer(self, frame: Frame) -> None: + """Enable buffering for the frame from that point onwards.""" + frame.buffer = self.temporary_identifier() + self.writeline(f"{frame.buffer} = []") + + def return_buffer_contents( + self, frame: Frame, force_unescaped: bool = False + ) -> None: + """Return the buffer contents of the frame.""" + if not force_unescaped: + if frame.eval_ctx.volatile: + self.writeline("if context.eval_ctx.autoescape:") + self.indent() + self.writeline(f"return Markup(concat({frame.buffer}))") + self.outdent() + self.writeline("else:") + self.indent() + self.writeline(f"return concat({frame.buffer})") + self.outdent() + return + elif frame.eval_ctx.autoescape: + self.writeline(f"return Markup(concat({frame.buffer}))") + return + self.writeline(f"return concat({frame.buffer})") + + def indent(self) -> None: + """Indent by one.""" + self._indentation += 1 + + def outdent(self, step: int = 1) -> None: + """Outdent by step.""" + self._indentation -= step + + def start_write(self, frame: Frame, node: t.Optional[nodes.Node] = None) -> None: + """Yield or write into the frame buffer.""" + if frame.buffer is None: + self.writeline("yield ", node) + else: + self.writeline(f"{frame.buffer}.append(", node) + + def end_write(self, frame: Frame) -> None: + """End the writing process started by `start_write`.""" + if frame.buffer is not None: + self.write(")") + + def simple_write( + self, s: str, frame: Frame, node: t.Optional[nodes.Node] = None + ) -> None: + """Simple shortcut for start_write + write + end_write.""" + self.start_write(frame, node) + self.write(s) + self.end_write(frame) + + def blockvisit(self, nodes: t.Iterable[nodes.Node], frame: Frame) -> None: + """Visit a list of nodes as block in a frame. If the current frame + is no buffer a dummy ``if 0: yield None`` is written automatically. + """ + try: + self.writeline("pass") + for node in nodes: + self.visit(node, frame) + except CompilerExit: + pass + + def write(self, x: str) -> None: + """Write a string into the output stream.""" + if self._new_lines: + if not self._first_write: + self.stream.write("\n" * self._new_lines) + self.code_lineno += self._new_lines + if self._write_debug_info is not None: + self.debug_info.append((self._write_debug_info, self.code_lineno)) + self._write_debug_info = None + self._first_write = False + self.stream.write(" " * self._indentation) + self._new_lines = 0 + self.stream.write(x) + + def writeline( + self, x: str, node: t.Optional[nodes.Node] = None, extra: int = 0 + ) -> None: + """Combination of newline and write.""" + self.newline(node, extra) + self.write(x) + + def newline(self, node: t.Optional[nodes.Node] = None, extra: int = 0) -> None: + """Add one or more newlines before the next write.""" + self._new_lines = max(self._new_lines, 1 + extra) + if node is not None and node.lineno != self._last_line: + self._write_debug_info = node.lineno + self._last_line = node.lineno + + def signature( + self, + node: t.Union[nodes.Call, nodes.Filter, nodes.Test], + frame: Frame, + extra_kwargs: t.Optional[t.Mapping[str, t.Any]] = None, + ) -> None: + """Writes a function call to the stream for the current node. + A leading comma is added automatically. The extra keyword + arguments may not include python keywords otherwise a syntax + error could occur. The extra keyword arguments should be given + as python dict. + """ + # if any of the given keyword arguments is a python keyword + # we have to make sure that no invalid call is created. + kwarg_workaround = any( + is_python_keyword(t.cast(str, k)) + for k in chain((x.key for x in node.kwargs), extra_kwargs or ()) + ) + + for arg in node.args: + self.write(", ") + self.visit(arg, frame) + + if not kwarg_workaround: + for kwarg in node.kwargs: + self.write(", ") + self.visit(kwarg, frame) + if extra_kwargs is not None: + for key, value in extra_kwargs.items(): + self.write(f", {key}={value}") + if node.dyn_args: + self.write(", *") + self.visit(node.dyn_args, frame) + + if kwarg_workaround: + if node.dyn_kwargs is not None: + self.write(", **dict({") + else: + self.write(", **{") + for kwarg in node.kwargs: + self.write(f"{kwarg.key!r}: ") + self.visit(kwarg.value, frame) + self.write(", ") + if extra_kwargs is not None: + for key, value in extra_kwargs.items(): + self.write(f"{key!r}: {value}, ") + if node.dyn_kwargs is not None: + self.write("}, **") + self.visit(node.dyn_kwargs, frame) + self.write(")") + else: + self.write("}") + + elif node.dyn_kwargs is not None: + self.write(", **") + self.visit(node.dyn_kwargs, frame) + + def pull_dependencies(self, nodes: t.Iterable[nodes.Node]) -> None: + """Find all filter and test names used in the template and + assign them to variables in the compiled namespace. Checking + that the names are registered with the environment is done when + compiling the Filter and Test nodes. If the node is in an If or + CondExpr node, the check is done at runtime instead. + + .. versionchanged:: 3.0 + Filters and tests in If and CondExpr nodes are checked at + runtime instead of compile time. + """ + visitor = DependencyFinderVisitor() + + for node in nodes: + visitor.visit(node) + + for id_map, names, dependency in ( + (self.filters, visitor.filters, "filters"), + ( + self.tests, + visitor.tests, + "tests", + ), + ): + for name in sorted(names): + if name not in id_map: + id_map[name] = self.temporary_identifier() + + # add check during runtime that dependencies used inside of executed + # blocks are defined, as this step may be skipped during compile time + self.writeline("try:") + self.indent() + self.writeline(f"{id_map[name]} = environment.{dependency}[{name!r}]") + self.outdent() + self.writeline("except KeyError:") + self.indent() + self.writeline("@internalcode") + self.writeline(f"def {id_map[name]}(*unused):") + self.indent() + self.writeline( + f'raise TemplateRuntimeError("No {dependency[:-1]}' + f' named {name!r} found.")' + ) + self.outdent() + self.outdent() + + def enter_frame(self, frame: Frame) -> None: + undefs = [] + for target, (action, param) in frame.symbols.loads.items(): + if action == VAR_LOAD_PARAMETER: + pass + elif action == VAR_LOAD_RESOLVE: + self.writeline(f"{target} = {self.get_resolve_func()}({param!r})") + elif action == VAR_LOAD_ALIAS: + self.writeline(f"{target} = {param}") + elif action == VAR_LOAD_UNDEFINED: + undefs.append(target) + else: + raise NotImplementedError("unknown load instruction") + if undefs: + self.writeline(f"{' = '.join(undefs)} = missing") + + def leave_frame(self, frame: Frame, with_python_scope: bool = False) -> None: + if not with_python_scope: + undefs = [] + for target in frame.symbols.loads: + undefs.append(target) + if undefs: + self.writeline(f"{' = '.join(undefs)} = missing") + + def choose_async(self, async_value: str = "async ", sync_value: str = "") -> str: + return async_value if self.environment.is_async else sync_value + + def func(self, name: str) -> str: + return f"{self.choose_async()}def {name}" + + def macro_body( + self, node: t.Union[nodes.Macro, nodes.CallBlock], frame: Frame + ) -> t.Tuple[Frame, MacroRef]: + """Dump the function def of a macro or call block.""" + frame = frame.inner() + frame.symbols.analyze_node(node) + macro_ref = MacroRef(node) + + explicit_caller = None + skip_special_params = set() + args = [] + + for idx, arg in enumerate(node.args): + if arg.name == "caller": + explicit_caller = idx + if arg.name in ("kwargs", "varargs"): + skip_special_params.add(arg.name) + args.append(frame.symbols.ref(arg.name)) + + undeclared = find_undeclared(node.body, ("caller", "kwargs", "varargs")) + + if "caller" in undeclared: + # In older Jinja versions there was a bug that allowed caller + # to retain the special behavior even if it was mentioned in + # the argument list. However thankfully this was only really + # working if it was the last argument. So we are explicitly + # checking this now and error out if it is anywhere else in + # the argument list. + if explicit_caller is not None: + try: + node.defaults[explicit_caller - len(node.args)] + except IndexError: + self.fail( + "When defining macros or call blocks the " + 'special "caller" argument must be omitted ' + "or be given a default.", + node.lineno, + ) + else: + args.append(frame.symbols.declare_parameter("caller")) + macro_ref.accesses_caller = True + if "kwargs" in undeclared and "kwargs" not in skip_special_params: + args.append(frame.symbols.declare_parameter("kwargs")) + macro_ref.accesses_kwargs = True + if "varargs" in undeclared and "varargs" not in skip_special_params: + args.append(frame.symbols.declare_parameter("varargs")) + macro_ref.accesses_varargs = True + + # macros are delayed, they never require output checks + frame.require_output_check = False + frame.symbols.analyze_node(node) + self.writeline(f"{self.func('macro')}({', '.join(args)}):", node) + self.indent() + + self.buffer(frame) + self.enter_frame(frame) + + self.push_parameter_definitions(frame) + for idx, arg in enumerate(node.args): + ref = frame.symbols.ref(arg.name) + self.writeline(f"if {ref} is missing:") + self.indent() + try: + default = node.defaults[idx - len(node.args)] + except IndexError: + self.writeline( + f'{ref} = undefined("parameter {arg.name!r} was not provided",' + f" name={arg.name!r})" + ) + else: + self.writeline(f"{ref} = ") + self.visit(default, frame) + self.mark_parameter_stored(ref) + self.outdent() + self.pop_parameter_definitions() + + self.blockvisit(node.body, frame) + self.return_buffer_contents(frame, force_unescaped=True) + self.leave_frame(frame, with_python_scope=True) + self.outdent() + + return frame, macro_ref + + def macro_def(self, macro_ref: MacroRef, frame: Frame) -> None: + """Dump the macro definition for the def created by macro_body.""" + arg_tuple = ", ".join(repr(x.name) for x in macro_ref.node.args) + name = getattr(macro_ref.node, "name", None) + if len(macro_ref.node.args) == 1: + arg_tuple += "," + self.write( + f"Macro(environment, macro, {name!r}, ({arg_tuple})," + f" {macro_ref.accesses_kwargs!r}, {macro_ref.accesses_varargs!r}," + f" {macro_ref.accesses_caller!r}, context.eval_ctx.autoescape)" + ) + + def position(self, node: nodes.Node) -> str: + """Return a human readable position for the node.""" + rv = f"line {node.lineno}" + if self.name is not None: + rv = f"{rv} in {self.name!r}" + return rv + + def dump_local_context(self, frame: Frame) -> str: + items_kv = ", ".join( + f"{name!r}: {target}" + for name, target in frame.symbols.dump_stores().items() + ) + return f"{{{items_kv}}}" + + def write_commons(self) -> None: + """Writes a common preamble that is used by root and block functions. + Primarily this sets up common local helpers and enforces a generator + through a dead branch. + """ + self.writeline("resolve = context.resolve_or_missing") + self.writeline("undefined = environment.undefined") + self.writeline("concat = environment.concat") + # always use the standard Undefined class for the implicit else of + # conditional expressions + self.writeline("cond_expr_undefined = Undefined") + self.writeline("if 0: yield None") + + def push_parameter_definitions(self, frame: Frame) -> None: + """Pushes all parameter targets from the given frame into a local + stack that permits tracking of yet to be assigned parameters. In + particular this enables the optimization from `visit_Name` to skip + undefined expressions for parameters in macros as macros can reference + otherwise unbound parameters. + """ + self._param_def_block.append(frame.symbols.dump_param_targets()) + + def pop_parameter_definitions(self) -> None: + """Pops the current parameter definitions set.""" + self._param_def_block.pop() + + def mark_parameter_stored(self, target: str) -> None: + """Marks a parameter in the current parameter definitions as stored. + This will skip the enforced undefined checks. + """ + if self._param_def_block: + self._param_def_block[-1].discard(target) + + def push_context_reference(self, target: str) -> None: + self._context_reference_stack.append(target) + + def pop_context_reference(self) -> None: + self._context_reference_stack.pop() + + def get_context_ref(self) -> str: + return self._context_reference_stack[-1] + + def get_resolve_func(self) -> str: + target = self._context_reference_stack[-1] + if target == "context": + return "resolve" + return f"{target}.resolve" + + def derive_context(self, frame: Frame) -> str: + return f"{self.get_context_ref()}.derived({self.dump_local_context(frame)})" + + def parameter_is_undeclared(self, target: str) -> bool: + """Checks if a given target is an undeclared parameter.""" + if not self._param_def_block: + return False + return target in self._param_def_block[-1] + + def push_assign_tracking(self) -> None: + """Pushes a new layer for assignment tracking.""" + self._assign_stack.append(set()) + + def pop_assign_tracking(self, frame: Frame) -> None: + """Pops the topmost level for assignment tracking and updates the + context variables if necessary. + """ + vars = self._assign_stack.pop() + if ( + not frame.block_frame + and not frame.loop_frame + and not frame.toplevel + or not vars + ): + return + public_names = [x for x in vars if x[:1] != "_"] + if len(vars) == 1: + name = next(iter(vars)) + ref = frame.symbols.ref(name) + if frame.loop_frame: + self.writeline(f"_loop_vars[{name!r}] = {ref}") + return + if frame.block_frame: + self.writeline(f"_block_vars[{name!r}] = {ref}") + return + self.writeline(f"context.vars[{name!r}] = {ref}") + else: + if frame.loop_frame: + self.writeline("_loop_vars.update({") + elif frame.block_frame: + self.writeline("_block_vars.update({") + else: + self.writeline("context.vars.update({") + for idx, name in enumerate(sorted(vars)): + if idx: + self.write(", ") + ref = frame.symbols.ref(name) + self.write(f"{name!r}: {ref}") + self.write("})") + if not frame.block_frame and not frame.loop_frame and public_names: + if len(public_names) == 1: + self.writeline(f"context.exported_vars.add({public_names[0]!r})") + else: + names_str = ", ".join(map(repr, sorted(public_names))) + self.writeline(f"context.exported_vars.update(({names_str}))") + + # -- Statement Visitors + + def visit_Template( + self, node: nodes.Template, frame: t.Optional[Frame] = None + ) -> None: + assert frame is None, "no root frame allowed" + eval_ctx = EvalContext(self.environment, self.name) + + from .runtime import async_exported + from .runtime import exported + + if self.environment.is_async: + exported_names = sorted(exported + async_exported) + else: + exported_names = sorted(exported) + + self.writeline("from jinja2.runtime import " + ", ".join(exported_names)) + + # if we want a deferred initialization we cannot move the + # environment into a local name + envenv = "" if self.defer_init else ", environment=environment" + + # do we have an extends tag at all? If not, we can save some + # overhead by just not processing any inheritance code. + have_extends = node.find(nodes.Extends) is not None + + # find all blocks + for block in node.find_all(nodes.Block): + if block.name in self.blocks: + self.fail(f"block {block.name!r} defined twice", block.lineno) + self.blocks[block.name] = block + + # find all imports and import them + for import_ in node.find_all(nodes.ImportedName): + if import_.importname not in self.import_aliases: + imp = import_.importname + self.import_aliases[imp] = alias = self.temporary_identifier() + if "." in imp: + module, obj = imp.rsplit(".", 1) + self.writeline(f"from {module} import {obj} as {alias}") + else: + self.writeline(f"import {imp} as {alias}") + + # add the load name + self.writeline(f"name = {self.name!r}") + + # generate the root render function. + self.writeline( + f"{self.func('root')}(context, missing=missing{envenv}):", extra=1 + ) + self.indent() + self.write_commons() + + # process the root + frame = Frame(eval_ctx) + if "self" in find_undeclared(node.body, ("self",)): + ref = frame.symbols.declare_parameter("self") + self.writeline(f"{ref} = TemplateReference(context)") + frame.symbols.analyze_node(node) + frame.toplevel = frame.rootlevel = True + frame.require_output_check = have_extends and not self.has_known_extends + if have_extends: + self.writeline("parent_template = None") + self.enter_frame(frame) + self.pull_dependencies(node.body) + self.blockvisit(node.body, frame) + self.leave_frame(frame, with_python_scope=True) + self.outdent() + + # make sure that the parent root is called. + if have_extends: + if not self.has_known_extends: + self.indent() + self.writeline("if parent_template is not None:") + self.indent() + if not self.environment.is_async: + self.writeline("yield from parent_template.root_render_func(context)") + else: + self.writeline("agen = parent_template.root_render_func(context)") + self.writeline("try:") + self.indent() + self.writeline("async for event in agen:") + self.indent() + self.writeline("yield event") + self.outdent() + self.outdent() + self.writeline("finally: await agen.aclose()") + self.outdent(1 + (not self.has_known_extends)) + + # at this point we now have the blocks collected and can visit them too. + for name, block in self.blocks.items(): + self.writeline( + f"{self.func('block_' + name)}(context, missing=missing{envenv}):", + block, + 1, + ) + self.indent() + self.write_commons() + # It's important that we do not make this frame a child of the + # toplevel template. This would cause a variety of + # interesting issues with identifier tracking. + block_frame = Frame(eval_ctx) + block_frame.block_frame = True + undeclared = find_undeclared(block.body, ("self", "super")) + if "self" in undeclared: + ref = block_frame.symbols.declare_parameter("self") + self.writeline(f"{ref} = TemplateReference(context)") + if "super" in undeclared: + ref = block_frame.symbols.declare_parameter("super") + self.writeline(f"{ref} = context.super({name!r}, block_{name})") + block_frame.symbols.analyze_node(block) + block_frame.block = name + self.writeline("_block_vars = {}") + self.enter_frame(block_frame) + self.pull_dependencies(block.body) + self.blockvisit(block.body, block_frame) + self.leave_frame(block_frame, with_python_scope=True) + self.outdent() + + blocks_kv_str = ", ".join(f"{x!r}: block_{x}" for x in self.blocks) + self.writeline(f"blocks = {{{blocks_kv_str}}}", extra=1) + debug_kv_str = "&".join(f"{k}={v}" for k, v in self.debug_info) + self.writeline(f"debug_info = {debug_kv_str!r}") + + def visit_Block(self, node: nodes.Block, frame: Frame) -> None: + """Call a block and register it for the template.""" + level = 0 + if frame.toplevel: + # if we know that we are a child template, there is no need to + # check if we are one + if self.has_known_extends: + return + if self.extends_so_far > 0: + self.writeline("if parent_template is None:") + self.indent() + level += 1 + + if node.scoped: + context = self.derive_context(frame) + else: + context = self.get_context_ref() + + if node.required: + self.writeline(f"if len(context.blocks[{node.name!r}]) <= 1:", node) + self.indent() + self.writeline( + f'raise TemplateRuntimeError("Required block {node.name!r} not found")', + node, + ) + self.outdent() + + if not self.environment.is_async and frame.buffer is None: + self.writeline( + f"yield from context.blocks[{node.name!r}][0]({context})", node + ) + else: + self.writeline(f"gen = context.blocks[{node.name!r}][0]({context})") + self.writeline("try:") + self.indent() + self.writeline( + f"{self.choose_async()}for event in gen:", + node, + ) + self.indent() + self.simple_write("event", frame) + self.outdent() + self.outdent() + self.writeline( + f"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}" + ) + + self.outdent(level) + + def visit_Extends(self, node: nodes.Extends, frame: Frame) -> None: + """Calls the extender.""" + if not frame.toplevel: + self.fail("cannot use extend from a non top-level scope", node.lineno) + + # if the number of extends statements in general is zero so + # far, we don't have to add a check if something extended + # the template before this one. + if self.extends_so_far > 0: + # if we have a known extends we just add a template runtime + # error into the generated code. We could catch that at compile + # time too, but i welcome it not to confuse users by throwing the + # same error at different times just "because we can". + if not self.has_known_extends: + self.writeline("if parent_template is not None:") + self.indent() + self.writeline('raise TemplateRuntimeError("extended multiple times")') + + # if we have a known extends already we don't need that code here + # as we know that the template execution will end here. + if self.has_known_extends: + raise CompilerExit() + else: + self.outdent() + + self.writeline("parent_template = environment.get_template(", node) + self.visit(node.template, frame) + self.write(f", {self.name!r})") + self.writeline("for name, parent_block in parent_template.blocks.items():") + self.indent() + self.writeline("context.blocks.setdefault(name, []).append(parent_block)") + self.outdent() + + # if this extends statement was in the root level we can take + # advantage of that information and simplify the generated code + # in the top level from this point onwards + if frame.rootlevel: + self.has_known_extends = True + + # and now we have one more + self.extends_so_far += 1 + + def visit_Include(self, node: nodes.Include, frame: Frame) -> None: + """Handles includes.""" + if node.ignore_missing: + self.writeline("try:") + self.indent() + + func_name = "get_or_select_template" + if isinstance(node.template, nodes.Const): + if isinstance(node.template.value, str): + func_name = "get_template" + elif isinstance(node.template.value, (tuple, list)): + func_name = "select_template" + elif isinstance(node.template, (nodes.Tuple, nodes.List)): + func_name = "select_template" + + self.writeline(f"template = environment.{func_name}(", node) + self.visit(node.template, frame) + self.write(f", {self.name!r})") + if node.ignore_missing: + self.outdent() + self.writeline("except TemplateNotFound:") + self.indent() + self.writeline("pass") + self.outdent() + self.writeline("else:") + self.indent() + + def loop_body() -> None: + self.indent() + self.simple_write("event", frame) + self.outdent() + + if node.with_context: + self.writeline( + f"gen = template.root_render_func(" + "template.new_context(context.get_all(), True," + f" {self.dump_local_context(frame)}))" + ) + self.writeline("try:") + self.indent() + self.writeline(f"{self.choose_async()}for event in gen:") + loop_body() + self.outdent() + self.writeline( + f"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}" + ) + elif self.environment.is_async: + self.writeline( + "for event in (await template._get_default_module_async())" + "._body_stream:" + ) + loop_body() + else: + self.writeline("yield from template._get_default_module()._body_stream") + + if node.ignore_missing: + self.outdent() + + def _import_common( + self, node: t.Union[nodes.Import, nodes.FromImport], frame: Frame + ) -> None: + self.write(f"{self.choose_async('await ')}environment.get_template(") + self.visit(node.template, frame) + self.write(f", {self.name!r}).") + + if node.with_context: + f_name = f"make_module{self.choose_async('_async')}" + self.write( + f"{f_name}(context.get_all(), True, {self.dump_local_context(frame)})" + ) + else: + self.write(f"_get_default_module{self.choose_async('_async')}(context)") + + def visit_Import(self, node: nodes.Import, frame: Frame) -> None: + """Visit regular imports.""" + self.writeline(f"{frame.symbols.ref(node.target)} = ", node) + if frame.toplevel: + self.write(f"context.vars[{node.target!r}] = ") + + self._import_common(node, frame) + + if frame.toplevel and not node.target.startswith("_"): + self.writeline(f"context.exported_vars.discard({node.target!r})") + + def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None: + """Visit named imports.""" + self.newline(node) + self.write("included_template = ") + self._import_common(node, frame) + var_names = [] + discarded_names = [] + for name in node.names: + if isinstance(name, tuple): + name, alias = name + else: + alias = name + self.writeline( + f"{frame.symbols.ref(alias)} =" + f" getattr(included_template, {name!r}, missing)" + ) + self.writeline(f"if {frame.symbols.ref(alias)} is missing:") + self.indent() + # The position will contain the template name, and will be formatted + # into a string that will be compiled into an f-string. Curly braces + # in the name must be replaced with escapes so that they will not be + # executed as part of the f-string. + position = self.position(node).replace("{", "{{").replace("}", "}}") + message = ( + "the template {included_template.__name__!r}" + f" (imported on {position})" + f" does not export the requested name {name!r}" + ) + self.writeline( + f"{frame.symbols.ref(alias)} = undefined(f{message!r}, name={name!r})" + ) + self.outdent() + if frame.toplevel: + var_names.append(alias) + if not alias.startswith("_"): + discarded_names.append(alias) + + if var_names: + if len(var_names) == 1: + name = var_names[0] + self.writeline(f"context.vars[{name!r}] = {frame.symbols.ref(name)}") + else: + names_kv = ", ".join( + f"{name!r}: {frame.symbols.ref(name)}" for name in var_names + ) + self.writeline(f"context.vars.update({{{names_kv}}})") + if discarded_names: + if len(discarded_names) == 1: + self.writeline(f"context.exported_vars.discard({discarded_names[0]!r})") + else: + names_str = ", ".join(map(repr, discarded_names)) + self.writeline( + f"context.exported_vars.difference_update(({names_str}))" + ) + + def visit_For(self, node: nodes.For, frame: Frame) -> None: + loop_frame = frame.inner() + loop_frame.loop_frame = True + test_frame = frame.inner() + else_frame = frame.inner() + + # try to figure out if we have an extended loop. An extended loop + # is necessary if the loop is in recursive mode if the special loop + # variable is accessed in the body if the body is a scoped block. + extended_loop = ( + node.recursive + or "loop" + in find_undeclared(node.iter_child_nodes(only=("body",)), ("loop",)) + or any(block.scoped for block in node.find_all(nodes.Block)) + ) + + loop_ref = None + if extended_loop: + loop_ref = loop_frame.symbols.declare_parameter("loop") + + loop_frame.symbols.analyze_node(node, for_branch="body") + if node.else_: + else_frame.symbols.analyze_node(node, for_branch="else") + + if node.test: + loop_filter_func = self.temporary_identifier() + test_frame.symbols.analyze_node(node, for_branch="test") + self.writeline(f"{self.func(loop_filter_func)}(fiter):", node.test) + self.indent() + self.enter_frame(test_frame) + self.writeline(self.choose_async("async for ", "for ")) + self.visit(node.target, loop_frame) + self.write(" in ") + self.write(self.choose_async("auto_aiter(fiter)", "fiter")) + self.write(":") + self.indent() + self.writeline("if ", node.test) + self.visit(node.test, test_frame) + self.write(":") + self.indent() + self.writeline("yield ") + self.visit(node.target, loop_frame) + self.outdent(3) + self.leave_frame(test_frame, with_python_scope=True) + + # if we don't have an recursive loop we have to find the shadowed + # variables at that point. Because loops can be nested but the loop + # variable is a special one we have to enforce aliasing for it. + if node.recursive: + self.writeline( + f"{self.func('loop')}(reciter, loop_render_func, depth=0):", node + ) + self.indent() + self.buffer(loop_frame) + + # Use the same buffer for the else frame + else_frame.buffer = loop_frame.buffer + + # make sure the loop variable is a special one and raise a template + # assertion error if a loop tries to write to loop + if extended_loop: + self.writeline(f"{loop_ref} = missing") + + for name in node.find_all(nodes.Name): + if name.ctx == "store" and name.name == "loop": + self.fail( + "Can't assign to special loop variable in for-loop target", + name.lineno, + ) + + if node.else_: + iteration_indicator = self.temporary_identifier() + self.writeline(f"{iteration_indicator} = 1") + + self.writeline(self.choose_async("async for ", "for "), node) + self.visit(node.target, loop_frame) + if extended_loop: + self.write(f", {loop_ref} in {self.choose_async('Async')}LoopContext(") + else: + self.write(" in ") + + if node.test: + self.write(f"{loop_filter_func}(") + if node.recursive: + self.write("reciter") + else: + if self.environment.is_async and not extended_loop: + self.write("auto_aiter(") + self.visit(node.iter, frame) + if self.environment.is_async and not extended_loop: + self.write(")") + if node.test: + self.write(")") + + if node.recursive: + self.write(", undefined, loop_render_func, depth):") + else: + self.write(", undefined):" if extended_loop else ":") + + self.indent() + self.enter_frame(loop_frame) + + self.writeline("_loop_vars = {}") + self.blockvisit(node.body, loop_frame) + if node.else_: + self.writeline(f"{iteration_indicator} = 0") + self.outdent() + self.leave_frame( + loop_frame, with_python_scope=node.recursive and not node.else_ + ) + + if node.else_: + self.writeline(f"if {iteration_indicator}:") + self.indent() + self.enter_frame(else_frame) + self.blockvisit(node.else_, else_frame) + self.leave_frame(else_frame) + self.outdent() + + # if the node was recursive we have to return the buffer contents + # and start the iteration code + if node.recursive: + self.return_buffer_contents(loop_frame) + self.outdent() + self.start_write(frame, node) + self.write(f"{self.choose_async('await ')}loop(") + if self.environment.is_async: + self.write("auto_aiter(") + self.visit(node.iter, frame) + if self.environment.is_async: + self.write(")") + self.write(", loop)") + self.end_write(frame) + + # at the end of the iteration, clear any assignments made in the + # loop from the top level + if self._assign_stack: + self._assign_stack[-1].difference_update(loop_frame.symbols.stores) + + def visit_If(self, node: nodes.If, frame: Frame) -> None: + if_frame = frame.soft() + self.writeline("if ", node) + self.visit(node.test, if_frame) + self.write(":") + self.indent() + self.blockvisit(node.body, if_frame) + self.outdent() + for elif_ in node.elif_: + self.writeline("elif ", elif_) + self.visit(elif_.test, if_frame) + self.write(":") + self.indent() + self.blockvisit(elif_.body, if_frame) + self.outdent() + if node.else_: + self.writeline("else:") + self.indent() + self.blockvisit(node.else_, if_frame) + self.outdent() + + def visit_Macro(self, node: nodes.Macro, frame: Frame) -> None: + macro_frame, macro_ref = self.macro_body(node, frame) + self.newline() + if frame.toplevel: + if not node.name.startswith("_"): + self.write(f"context.exported_vars.add({node.name!r})") + self.writeline(f"context.vars[{node.name!r}] = ") + self.write(f"{frame.symbols.ref(node.name)} = ") + self.macro_def(macro_ref, macro_frame) + + def visit_CallBlock(self, node: nodes.CallBlock, frame: Frame) -> None: + call_frame, macro_ref = self.macro_body(node, frame) + self.writeline("caller = ") + self.macro_def(macro_ref, call_frame) + self.start_write(frame, node) + self.visit_Call(node.call, frame, forward_caller=True) + self.end_write(frame) + + def visit_FilterBlock(self, node: nodes.FilterBlock, frame: Frame) -> None: + filter_frame = frame.inner() + filter_frame.symbols.analyze_node(node) + self.enter_frame(filter_frame) + self.buffer(filter_frame) + self.blockvisit(node.body, filter_frame) + self.start_write(frame, node) + self.visit_Filter(node.filter, filter_frame) + self.end_write(frame) + self.leave_frame(filter_frame) + + def visit_With(self, node: nodes.With, frame: Frame) -> None: + with_frame = frame.inner() + with_frame.symbols.analyze_node(node) + self.enter_frame(with_frame) + for target, expr in zip(node.targets, node.values): + self.newline() + self.visit(target, with_frame) + self.write(" = ") + self.visit(expr, frame) + self.blockvisit(node.body, with_frame) + self.leave_frame(with_frame) + + def visit_ExprStmt(self, node: nodes.ExprStmt, frame: Frame) -> None: + self.newline(node) + self.visit(node.node, frame) + + class _FinalizeInfo(t.NamedTuple): + const: t.Optional[t.Callable[..., str]] + src: t.Optional[str] + + @staticmethod + def _default_finalize(value: t.Any) -> t.Any: + """The default finalize function if the environment isn't + configured with one. Or, if the environment has one, this is + called on that function's output for constants. + """ + return str(value) + + _finalize: t.Optional[_FinalizeInfo] = None + + def _make_finalize(self) -> _FinalizeInfo: + """Build the finalize function to be used on constants and at + runtime. Cached so it's only created once for all output nodes. + + Returns a ``namedtuple`` with the following attributes: + + ``const`` + A function to finalize constant data at compile time. + + ``src`` + Source code to output around nodes to be evaluated at + runtime. + """ + if self._finalize is not None: + return self._finalize + + finalize: t.Optional[t.Callable[..., t.Any]] + finalize = default = self._default_finalize + src = None + + if self.environment.finalize: + src = "environment.finalize(" + env_finalize = self.environment.finalize + pass_arg = { + _PassArg.context: "context", + _PassArg.eval_context: "context.eval_ctx", + _PassArg.environment: "environment", + }.get( + _PassArg.from_obj(env_finalize) # type: ignore + ) + finalize = None + + if pass_arg is None: + + def finalize(value: t.Any) -> t.Any: # noqa: F811 + return default(env_finalize(value)) + + else: + src = f"{src}{pass_arg}, " + + if pass_arg == "environment": + + def finalize(value: t.Any) -> t.Any: # noqa: F811 + return default(env_finalize(self.environment, value)) + + self._finalize = self._FinalizeInfo(finalize, src) + return self._finalize + + def _output_const_repr(self, group: t.Iterable[t.Any]) -> str: + """Given a group of constant values converted from ``Output`` + child nodes, produce a string to write to the template module + source. + """ + return repr(concat(group)) + + def _output_child_to_const( + self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo + ) -> str: + """Try to optimize a child of an ``Output`` node by trying to + convert it to constant, finalized data at compile time. + + If :exc:`Impossible` is raised, the node is not constant and + will be evaluated at runtime. Any other exception will also be + evaluated at runtime for easier debugging. + """ + const = node.as_const(frame.eval_ctx) + + if frame.eval_ctx.autoescape: + const = escape(const) + + # Template data doesn't go through finalize. + if isinstance(node, nodes.TemplateData): + return str(const) + + return finalize.const(const) # type: ignore + + def _output_child_pre( + self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo + ) -> None: + """Output extra source code before visiting a child of an + ``Output`` node. + """ + if frame.eval_ctx.volatile: + self.write("(escape if context.eval_ctx.autoescape else str)(") + elif frame.eval_ctx.autoescape: + self.write("escape(") + else: + self.write("str(") + + if finalize.src is not None: + self.write(finalize.src) + + def _output_child_post( + self, node: nodes.Expr, frame: Frame, finalize: _FinalizeInfo + ) -> None: + """Output extra source code after visiting a child of an + ``Output`` node. + """ + self.write(")") + + if finalize.src is not None: + self.write(")") + + def visit_Output(self, node: nodes.Output, frame: Frame) -> None: + # If an extends is active, don't render outside a block. + if frame.require_output_check: + # A top-level extends is known to exist at compile time. + if self.has_known_extends: + return + + self.writeline("if parent_template is None:") + self.indent() + + finalize = self._make_finalize() + body: t.List[t.Union[t.List[t.Any], nodes.Expr]] = [] + + # Evaluate constants at compile time if possible. Each item in + # body will be either a list of static data or a node to be + # evaluated at runtime. + for child in node.nodes: + try: + if not ( + # If the finalize function requires runtime context, + # constants can't be evaluated at compile time. + finalize.const + # Unless it's basic template data that won't be + # finalized anyway. + or isinstance(child, nodes.TemplateData) + ): + raise nodes.Impossible() + + const = self._output_child_to_const(child, frame, finalize) + except (nodes.Impossible, Exception): + # The node was not constant and needs to be evaluated at + # runtime. Or another error was raised, which is easier + # to debug at runtime. + body.append(child) + continue + + if body and isinstance(body[-1], list): + body[-1].append(const) + else: + body.append([const]) + + if frame.buffer is not None: + if len(body) == 1: + self.writeline(f"{frame.buffer}.append(") + else: + self.writeline(f"{frame.buffer}.extend((") + + self.indent() + + for item in body: + if isinstance(item, list): + # A group of constant data to join and output. + val = self._output_const_repr(item) + + if frame.buffer is None: + self.writeline("yield " + val) + else: + self.writeline(val + ",") + else: + if frame.buffer is None: + self.writeline("yield ", item) + else: + self.newline(item) + + # A node to be evaluated at runtime. + self._output_child_pre(item, frame, finalize) + self.visit(item, frame) + self._output_child_post(item, frame, finalize) + + if frame.buffer is not None: + self.write(",") + + if frame.buffer is not None: + self.outdent() + self.writeline(")" if len(body) == 1 else "))") + + if frame.require_output_check: + self.outdent() + + def visit_Assign(self, node: nodes.Assign, frame: Frame) -> None: + self.push_assign_tracking() + + # ``a.b`` is allowed for assignment, and is parsed as an NSRef. However, + # it is only valid if it references a Namespace object. Emit a check for + # that for each ref here, before assignment code is emitted. This can't + # be done in visit_NSRef as the ref could be in the middle of a tuple. + seen_refs: t.Set[str] = set() + + for nsref in node.find_all(nodes.NSRef): + if nsref.name in seen_refs: + # Only emit the check for each reference once, in case the same + # ref is used multiple times in a tuple, `ns.a, ns.b = c, d`. + continue + + seen_refs.add(nsref.name) + ref = frame.symbols.ref(nsref.name) + self.writeline(f"if not isinstance({ref}, Namespace):") + self.indent() + self.writeline( + "raise TemplateRuntimeError" + '("cannot assign attribute on non-namespace object")' + ) + self.outdent() + + self.newline(node) + self.visit(node.target, frame) + self.write(" = ") + self.visit(node.node, frame) + self.pop_assign_tracking(frame) + + def visit_AssignBlock(self, node: nodes.AssignBlock, frame: Frame) -> None: + self.push_assign_tracking() + block_frame = frame.inner() + # This is a special case. Since a set block always captures we + # will disable output checks. This way one can use set blocks + # toplevel even in extended templates. + block_frame.require_output_check = False + block_frame.symbols.analyze_node(node) + self.enter_frame(block_frame) + self.buffer(block_frame) + self.blockvisit(node.body, block_frame) + self.newline(node) + self.visit(node.target, frame) + self.write(" = (Markup if context.eval_ctx.autoescape else identity)(") + if node.filter is not None: + self.visit_Filter(node.filter, block_frame) + else: + self.write(f"concat({block_frame.buffer})") + self.write(")") + self.pop_assign_tracking(frame) + self.leave_frame(block_frame) + + # -- Expression Visitors + + def visit_Name(self, node: nodes.Name, frame: Frame) -> None: + if node.ctx == "store" and ( + frame.toplevel or frame.loop_frame or frame.block_frame + ): + if self._assign_stack: + self._assign_stack[-1].add(node.name) + ref = frame.symbols.ref(node.name) + + # If we are looking up a variable we might have to deal with the + # case where it's undefined. We can skip that case if the load + # instruction indicates a parameter which are always defined. + if node.ctx == "load": + load = frame.symbols.find_load(ref) + if not ( + load is not None + and load[0] == VAR_LOAD_PARAMETER + and not self.parameter_is_undeclared(ref) + ): + self.write( + f"(undefined(name={node.name!r}) if {ref} is missing else {ref})" + ) + return + + self.write(ref) + + def visit_NSRef(self, node: nodes.NSRef, frame: Frame) -> None: + # NSRef is a dotted assignment target a.b=c, but uses a[b]=c internally. + # visit_Assign emits code to validate that each ref is to a Namespace + # object only. That can't be emitted here as the ref could be in the + # middle of a tuple assignment. + ref = frame.symbols.ref(node.name) + self.writeline(f"{ref}[{node.attr!r}]") + + def visit_Const(self, node: nodes.Const, frame: Frame) -> None: + val = node.as_const(frame.eval_ctx) + if isinstance(val, float): + self.write(str(val)) + else: + self.write(repr(val)) + + def visit_TemplateData(self, node: nodes.TemplateData, frame: Frame) -> None: + try: + self.write(repr(node.as_const(frame.eval_ctx))) + except nodes.Impossible: + self.write( + f"(Markup if context.eval_ctx.autoescape else identity)({node.data!r})" + ) + + def visit_Tuple(self, node: nodes.Tuple, frame: Frame) -> None: + self.write("(") + idx = -1 + for idx, item in enumerate(node.items): + if idx: + self.write(", ") + self.visit(item, frame) + self.write(",)" if idx == 0 else ")") + + def visit_List(self, node: nodes.List, frame: Frame) -> None: + self.write("[") + for idx, item in enumerate(node.items): + if idx: + self.write(", ") + self.visit(item, frame) + self.write("]") + + def visit_Dict(self, node: nodes.Dict, frame: Frame) -> None: + self.write("{") + for idx, item in enumerate(node.items): + if idx: + self.write(", ") + self.visit(item.key, frame) + self.write(": ") + self.visit(item.value, frame) + self.write("}") + + visit_Add = _make_binop("+") + visit_Sub = _make_binop("-") + visit_Mul = _make_binop("*") + visit_Div = _make_binop("/") + visit_FloorDiv = _make_binop("//") + visit_Pow = _make_binop("**") + visit_Mod = _make_binop("%") + visit_And = _make_binop("and") + visit_Or = _make_binop("or") + visit_Pos = _make_unop("+") + visit_Neg = _make_unop("-") + visit_Not = _make_unop("not ") + + @optimizeconst + def visit_Concat(self, node: nodes.Concat, frame: Frame) -> None: + if frame.eval_ctx.volatile: + func_name = "(markup_join if context.eval_ctx.volatile else str_join)" + elif frame.eval_ctx.autoescape: + func_name = "markup_join" + else: + func_name = "str_join" + self.write(f"{func_name}((") + for arg in node.nodes: + self.visit(arg, frame) + self.write(", ") + self.write("))") + + @optimizeconst + def visit_Compare(self, node: nodes.Compare, frame: Frame) -> None: + self.write("(") + self.visit(node.expr, frame) + for op in node.ops: + self.visit(op, frame) + self.write(")") + + def visit_Operand(self, node: nodes.Operand, frame: Frame) -> None: + self.write(f" {operators[node.op]} ") + self.visit(node.expr, frame) + + @optimizeconst + def visit_Getattr(self, node: nodes.Getattr, frame: Frame) -> None: + if self.environment.is_async: + self.write("(await auto_await(") + + self.write("environment.getattr(") + self.visit(node.node, frame) + self.write(f", {node.attr!r})") + + if self.environment.is_async: + self.write("))") + + @optimizeconst + def visit_Getitem(self, node: nodes.Getitem, frame: Frame) -> None: + # slices bypass the environment getitem method. + if isinstance(node.arg, nodes.Slice): + self.visit(node.node, frame) + self.write("[") + self.visit(node.arg, frame) + self.write("]") + else: + if self.environment.is_async: + self.write("(await auto_await(") + + self.write("environment.getitem(") + self.visit(node.node, frame) + self.write(", ") + self.visit(node.arg, frame) + self.write(")") + + if self.environment.is_async: + self.write("))") + + def visit_Slice(self, node: nodes.Slice, frame: Frame) -> None: + if node.start is not None: + self.visit(node.start, frame) + self.write(":") + if node.stop is not None: + self.visit(node.stop, frame) + if node.step is not None: + self.write(":") + self.visit(node.step, frame) + + @contextmanager + def _filter_test_common( + self, node: t.Union[nodes.Filter, nodes.Test], frame: Frame, is_filter: bool + ) -> t.Iterator[None]: + if self.environment.is_async: + self.write("(await auto_await(") + + if is_filter: + self.write(f"{self.filters[node.name]}(") + func = self.environment.filters.get(node.name) + else: + self.write(f"{self.tests[node.name]}(") + func = self.environment.tests.get(node.name) + + # When inside an If or CondExpr frame, allow the filter to be + # undefined at compile time and only raise an error if it's + # actually called at runtime. See pull_dependencies. + if func is None and not frame.soft_frame: + type_name = "filter" if is_filter else "test" + self.fail(f"No {type_name} named {node.name!r}.", node.lineno) + + pass_arg = { + _PassArg.context: "context", + _PassArg.eval_context: "context.eval_ctx", + _PassArg.environment: "environment", + }.get( + _PassArg.from_obj(func) # type: ignore + ) + + if pass_arg is not None: + self.write(f"{pass_arg}, ") + + # Back to the visitor function to handle visiting the target of + # the filter or test. + yield + + self.signature(node, frame) + self.write(")") + + if self.environment.is_async: + self.write("))") + + @optimizeconst + def visit_Filter(self, node: nodes.Filter, frame: Frame) -> None: + with self._filter_test_common(node, frame, True): + # if the filter node is None we are inside a filter block + # and want to write to the current buffer + if node.node is not None: + self.visit(node.node, frame) + elif frame.eval_ctx.volatile: + self.write( + f"(Markup(concat({frame.buffer}))" + f" if context.eval_ctx.autoescape else concat({frame.buffer}))" + ) + elif frame.eval_ctx.autoescape: + self.write(f"Markup(concat({frame.buffer}))") + else: + self.write(f"concat({frame.buffer})") + + @optimizeconst + def visit_Test(self, node: nodes.Test, frame: Frame) -> None: + with self._filter_test_common(node, frame, False): + self.visit(node.node, frame) + + @optimizeconst + def visit_CondExpr(self, node: nodes.CondExpr, frame: Frame) -> None: + frame = frame.soft() + + def write_expr2() -> None: + if node.expr2 is not None: + self.visit(node.expr2, frame) + return + + self.write( + f'cond_expr_undefined("the inline if-expression on' + f" {self.position(node)} evaluated to false and no else" + f' section was defined.")' + ) + + self.write("(") + self.visit(node.expr1, frame) + self.write(" if ") + self.visit(node.test, frame) + self.write(" else ") + write_expr2() + self.write(")") + + @optimizeconst + def visit_Call( + self, node: nodes.Call, frame: Frame, forward_caller: bool = False + ) -> None: + if self.environment.is_async: + self.write("(await auto_await(") + if self.environment.sandboxed: + self.write("environment.call(context, ") + else: + self.write("context.call(") + self.visit(node.node, frame) + extra_kwargs = {"caller": "caller"} if forward_caller else None + loop_kwargs = {"_loop_vars": "_loop_vars"} if frame.loop_frame else {} + block_kwargs = {"_block_vars": "_block_vars"} if frame.block_frame else {} + if extra_kwargs: + extra_kwargs.update(loop_kwargs, **block_kwargs) + elif loop_kwargs or block_kwargs: + extra_kwargs = dict(loop_kwargs, **block_kwargs) + self.signature(node, frame, extra_kwargs) + self.write(")") + if self.environment.is_async: + self.write("))") + + def visit_Keyword(self, node: nodes.Keyword, frame: Frame) -> None: + self.write(node.key + "=") + self.visit(node.value, frame) + + # -- Unused nodes for extensions + + def visit_MarkSafe(self, node: nodes.MarkSafe, frame: Frame) -> None: + self.write("Markup(") + self.visit(node.expr, frame) + self.write(")") + + def visit_MarkSafeIfAutoescape( + self, node: nodes.MarkSafeIfAutoescape, frame: Frame + ) -> None: + self.write("(Markup if context.eval_ctx.autoescape else identity)(") + self.visit(node.expr, frame) + self.write(")") + + def visit_EnvironmentAttribute( + self, node: nodes.EnvironmentAttribute, frame: Frame + ) -> None: + self.write("environment." + node.name) + + def visit_ExtensionAttribute( + self, node: nodes.ExtensionAttribute, frame: Frame + ) -> None: + self.write(f"environment.extensions[{node.identifier!r}].{node.name}") + + def visit_ImportedName(self, node: nodes.ImportedName, frame: Frame) -> None: + self.write(self.import_aliases[node.importname]) + + def visit_InternalName(self, node: nodes.InternalName, frame: Frame) -> None: + self.write(node.name) + + def visit_ContextReference( + self, node: nodes.ContextReference, frame: Frame + ) -> None: + self.write("context") + + def visit_DerivedContextReference( + self, node: nodes.DerivedContextReference, frame: Frame + ) -> None: + self.write(self.derive_context(frame)) + + def visit_Continue(self, node: nodes.Continue, frame: Frame) -> None: + self.writeline("continue", node) + + def visit_Break(self, node: nodes.Break, frame: Frame) -> None: + self.writeline("break", node) + + def visit_Scope(self, node: nodes.Scope, frame: Frame) -> None: + scope_frame = frame.inner() + scope_frame.symbols.analyze_node(node) + self.enter_frame(scope_frame) + self.blockvisit(node.body, scope_frame) + self.leave_frame(scope_frame) + + def visit_OverlayScope(self, node: nodes.OverlayScope, frame: Frame) -> None: + ctx = self.temporary_identifier() + self.writeline(f"{ctx} = {self.derive_context(frame)}") + self.writeline(f"{ctx}.vars = ") + self.visit(node.context, frame) + self.push_context_reference(ctx) + + scope_frame = frame.inner(isolated=True) + scope_frame.symbols.analyze_node(node) + self.enter_frame(scope_frame) + self.blockvisit(node.body, scope_frame) + self.leave_frame(scope_frame) + self.pop_context_reference() + + def visit_EvalContextModifier( + self, node: nodes.EvalContextModifier, frame: Frame + ) -> None: + for keyword in node.options: + self.writeline(f"context.eval_ctx.{keyword.key} = ") + self.visit(keyword.value, frame) + try: + val = keyword.value.as_const(frame.eval_ctx) + except nodes.Impossible: + frame.eval_ctx.volatile = True + else: + setattr(frame.eval_ctx, keyword.key, val) + + def visit_ScopedEvalContextModifier( + self, node: nodes.ScopedEvalContextModifier, frame: Frame + ) -> None: + old_ctx_name = self.temporary_identifier() + saved_ctx = frame.eval_ctx.save() + self.writeline(f"{old_ctx_name} = context.eval_ctx.save()") + self.visit_EvalContextModifier(node, frame) + for child in node.body: + self.visit(child, frame) + frame.eval_ctx.revert(saved_ctx) + self.writeline(f"context.eval_ctx.revert({old_ctx_name})") diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/constants.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/constants.py new file mode 100644 index 0000000000..41a1c23b0a --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/constants.py @@ -0,0 +1,20 @@ +#: list of lorem ipsum words used by the lipsum() helper function +LOREM_IPSUM_WORDS = """\ +a ac accumsan ad adipiscing aenean aliquam aliquet amet ante aptent arcu at +auctor augue bibendum blandit class commodo condimentum congue consectetuer +consequat conubia convallis cras cubilia cum curabitur curae cursus dapibus +diam dictum dictumst dignissim dis dolor donec dui duis egestas eget eleifend +elementum elit enim erat eros est et etiam eu euismod facilisi facilisis fames +faucibus felis fermentum feugiat fringilla fusce gravida habitant habitasse hac +hendrerit hymenaeos iaculis id imperdiet in inceptos integer interdum ipsum +justo lacinia lacus laoreet lectus leo libero ligula litora lobortis lorem +luctus maecenas magna magnis malesuada massa mattis mauris metus mi molestie +mollis montes morbi mus nam nascetur natoque nec neque netus nibh nisi nisl non +nonummy nostra nulla nullam nunc odio orci ornare parturient pede pellentesque +penatibus per pharetra phasellus placerat platea porta porttitor posuere +potenti praesent pretium primis proin pulvinar purus quam quis quisque rhoncus +ridiculus risus rutrum sagittis sapien scelerisque sed sem semper senectus sit +sociis sociosqu sodales sollicitudin suscipit suspendisse taciti tellus tempor +tempus tincidunt torquent tortor tristique turpis ullamcorper ultrices +ultricies urna ut varius vehicula vel velit venenatis vestibulum vitae vivamus +viverra volutpat vulputate""" diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/debug.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/debug.py new file mode 100644 index 0000000000..eeeeee78b6 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/debug.py @@ -0,0 +1,191 @@ +import sys +import typing as t +from types import CodeType +from types import TracebackType + +from .exceptions import TemplateSyntaxError +from .utils import internal_code +from .utils import missing + +if t.TYPE_CHECKING: + from .runtime import Context + + +def rewrite_traceback_stack(source: t.Optional[str] = None) -> BaseException: + """Rewrite the current exception to replace any tracebacks from + within compiled template code with tracebacks that look like they + came from the template source. + + This must be called within an ``except`` block. + + :param source: For ``TemplateSyntaxError``, the original source if + known. + :return: The original exception with the rewritten traceback. + """ + _, exc_value, tb = sys.exc_info() + exc_value = t.cast(BaseException, exc_value) + tb = t.cast(TracebackType, tb) + + if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated: + exc_value.translated = True + exc_value.source = source + # Remove the old traceback, otherwise the frames from the + # compiler still show up. + exc_value.with_traceback(None) + # Outside of runtime, so the frame isn't executing template + # code, but it still needs to point at the template. + tb = fake_traceback( + exc_value, None, exc_value.filename or "", exc_value.lineno + ) + else: + # Skip the frame for the render function. + tb = tb.tb_next + + stack = [] + + # Build the stack of traceback object, replacing any in template + # code with the source file and line information. + while tb is not None: + # Skip frames decorated with @internalcode. These are internal + # calls that aren't useful in template debugging output. + if tb.tb_frame.f_code in internal_code: + tb = tb.tb_next + continue + + template = tb.tb_frame.f_globals.get("__jinja_template__") + + if template is not None: + lineno = template.get_corresponding_lineno(tb.tb_lineno) + fake_tb = fake_traceback(exc_value, tb, template.filename, lineno) + stack.append(fake_tb) + else: + stack.append(tb) + + tb = tb.tb_next + + tb_next = None + + # Assign tb_next in reverse to avoid circular references. + for tb in reversed(stack): + tb.tb_next = tb_next + tb_next = tb + + return exc_value.with_traceback(tb_next) + + +def fake_traceback( # type: ignore + exc_value: BaseException, tb: t.Optional[TracebackType], filename: str, lineno: int +) -> TracebackType: + """Produce a new traceback object that looks like it came from the + template source instead of the compiled code. The filename, line + number, and location name will point to the template, and the local + variables will be the current template context. + + :param exc_value: The original exception to be re-raised to create + the new traceback. + :param tb: The original traceback to get the local variables and + code info from. + :param filename: The template filename. + :param lineno: The line number in the template source. + """ + if tb is not None: + # Replace the real locals with the context that would be + # available at that point in the template. + locals = get_template_locals(tb.tb_frame.f_locals) + locals.pop("__jinja_exception__", None) + else: + locals = {} + + globals = { + "__name__": filename, + "__file__": filename, + "__jinja_exception__": exc_value, + } + # Raise an exception at the correct line number. + code: CodeType = compile( + "\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec" + ) + + # Build a new code object that points to the template file and + # replaces the location with a block name. + location = "template" + + if tb is not None: + function = tb.tb_frame.f_code.co_name + + if function == "root": + location = "top-level template code" + elif function.startswith("block_"): + location = f"block {function[6:]!r}" + + if sys.version_info >= (3, 8): + code = code.replace(co_name=location) + else: + code = CodeType( + code.co_argcount, + code.co_kwonlyargcount, + code.co_nlocals, + code.co_stacksize, + code.co_flags, + code.co_code, + code.co_consts, + code.co_names, + code.co_varnames, + code.co_filename, + location, + code.co_firstlineno, + code.co_lnotab, + code.co_freevars, + code.co_cellvars, + ) + + # Execute the new code, which is guaranteed to raise, and return + # the new traceback without this frame. + try: + exec(code, globals, locals) + except BaseException: + return sys.exc_info()[2].tb_next # type: ignore + + +def get_template_locals(real_locals: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]: + """Based on the runtime locals, get the context that would be + available at that point in the template. + """ + # Start with the current template context. + ctx: t.Optional[Context] = real_locals.get("context") + + if ctx is not None: + data: t.Dict[str, t.Any] = ctx.get_all().copy() + else: + data = {} + + # Might be in a derived context that only sets local variables + # rather than pushing a context. Local variables follow the scheme + # l_depth_name. Find the highest-depth local that has a value for + # each name. + local_overrides: t.Dict[str, t.Tuple[int, t.Any]] = {} + + for name, value in real_locals.items(): + if not name.startswith("l_") or value is missing: + # Not a template variable, or no longer relevant. + continue + + try: + _, depth_str, name = name.split("_", 2) + depth = int(depth_str) + except ValueError: + continue + + cur_depth = local_overrides.get(name, (-1,))[0] + + if cur_depth < depth: + local_overrides[name] = (depth, value) + + # Modify the context with any derived context. + for name, (_, value) in local_overrides.items(): + if value is missing: + data.pop(name, None) + else: + data[name] = value + + return data diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/defaults.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/defaults.py new file mode 100644 index 0000000000..638cad3d2d --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/defaults.py @@ -0,0 +1,48 @@ +import typing as t + +from .filters import FILTERS as DEFAULT_FILTERS # noqa: F401 +from .tests import TESTS as DEFAULT_TESTS # noqa: F401 +from .utils import Cycler +from .utils import generate_lorem_ipsum +from .utils import Joiner +from .utils import Namespace + +if t.TYPE_CHECKING: + import typing_extensions as te + +# defaults for the parser / lexer +BLOCK_START_STRING = "{%" +BLOCK_END_STRING = "%}" +VARIABLE_START_STRING = "{{" +VARIABLE_END_STRING = "}}" +COMMENT_START_STRING = "{#" +COMMENT_END_STRING = "#}" +LINE_STATEMENT_PREFIX: t.Optional[str] = None +LINE_COMMENT_PREFIX: t.Optional[str] = None +TRIM_BLOCKS = False +LSTRIP_BLOCKS = False +NEWLINE_SEQUENCE: "te.Literal['\\n', '\\r\\n', '\\r']" = "\n" +KEEP_TRAILING_NEWLINE = False + +# default filters, tests and namespace + +DEFAULT_NAMESPACE = { + "range": range, + "dict": dict, + "lipsum": generate_lorem_ipsum, + "cycler": Cycler, + "joiner": Joiner, + "namespace": Namespace, +} + +# default policies +DEFAULT_POLICIES: t.Dict[str, t.Any] = { + "compiler.ascii_str": True, + "urlize.rel": "noopener", + "urlize.target": None, + "urlize.extra_schemes": None, + "truncate.leeway": 5, + "json.dumps_function": None, + "json.dumps_kwargs": {"sort_keys": True}, + "ext.i18n.trimmed": False, +} diff --git a/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/environment.py b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/environment.py new file mode 100644 index 0000000000..0fc6e5be87 --- /dev/null +++ b/labs/lab18/app_python/venv1/lib/python3.11/site-packages/jinja2/environment.py @@ -0,0 +1,1672 @@ +"""Classes for managing templates and their runtime and compile time +options. +""" + +import os +import typing +import typing as t +import weakref +from collections import ChainMap +from functools import lru_cache +from functools import partial +from functools import reduce +from types import CodeType + +from markupsafe import Markup + +from . import nodes +from .compiler import CodeGenerator +from .compiler import generate +from .defaults import BLOCK_END_STRING +from .defaults import BLOCK_START_STRING +from .defaults import COMMENT_END_STRING +from .defaults import COMMENT_START_STRING +from .defaults import DEFAULT_FILTERS # type: ignore[attr-defined] +from .defaults import DEFAULT_NAMESPACE +from .defaults import DEFAULT_POLICIES +from .defaults import DEFAULT_TESTS # type: ignore[attr-defined] +from .defaults import KEEP_TRAILING_NEWLINE +from .defaults import LINE_COMMENT_PREFIX +from .defaults import LINE_STATEMENT_PREFIX +from .defaults import LSTRIP_BLOCKS +from .defaults import NEWLINE_SEQUENCE +from .defaults import TRIM_BLOCKS +from .defaults import VARIABLE_END_STRING +from .defaults import VARIABLE_START_STRING +from .exceptions import TemplateNotFound +from .exceptions import TemplateRuntimeError +from .exceptions import TemplatesNotFound +from .exceptions import TemplateSyntaxError +from .exceptions import UndefinedError +from .lexer import get_lexer +from .lexer import Lexer +from .lexer import TokenStream +from .nodes import EvalContext +from .parser import Parser +from .runtime import Context +from .runtime import new_context +from .runtime import Undefined +from .utils import _PassArg +from .utils import concat +from .utils import consume +from .utils import import_string +from .utils import internalcode +from .utils import LRUCache +from .utils import missing + +if t.TYPE_CHECKING: + import typing_extensions as te + + from .bccache import BytecodeCache + from .ext import Extension + from .loaders import BaseLoader + +_env_bound = t.TypeVar("_env_bound", bound="Environment") + + +# for direct template usage we have up to ten living environments +@lru_cache(maxsize=10) +def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_bound: + """Return a new spontaneous environment. A spontaneous environment + is used for templates created directly rather than through an + existing environment. + + :param cls: Environment class to create. + :param args: Positional arguments passed to environment. + """ + env = cls(*args) + env.shared = True + return env + + +def create_cache( + size: int, +) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]: + """Return the cache class for the given size.""" + if size == 0: + return None + + if size < 0: + return {} + + return LRUCache(size) # type: ignore + + +def copy_cache( + cache: t.Optional[t.MutableMapping[t.Any, t.Any]], +) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]: + """Create an empty copy of the given cache.""" + if cache is None: + return None + + if type(cache) is dict: # noqa E721 + return {} + + return LRUCache(cache.capacity) # type: ignore + + +def load_extensions( + environment: "Environment", + extensions: t.Sequence[t.Union[str, t.Type["Extension"]]], +) -> t.Dict[str, "Extension"]: + """Load the extensions from the list and bind it to the environment. + Returns a dict of instantiated extensions. + """ + result = {} + + for extension in extensions: + if isinstance(extension, str): + extension = t.cast(t.Type["Extension"], import_string(extension)) + + result[extension.identifier] = extension(environment) + + return result + + +def _environment_config_check(environment: _env_bound) -> _env_bound: + """Perform a sanity check on the environment.""" + assert issubclass( + environment.undefined, Undefined + ), "'undefined' must be a subclass of 'jinja2.Undefined'." + assert ( + environment.block_start_string + != environment.variable_start_string + != environment.comment_start_string + ), "block, variable and comment start strings must be different." + assert environment.newline_sequence in { + "\r", + "\r\n", + "\n", + }, "'newline_sequence' must be one of '\\n', '\\r\\n', or '\\r'." + return environment + + +class Environment: + r"""The core component of Jinja is the `Environment`. It contains + important shared variables like configuration, filters, tests, + globals and others. Instances of this class may be modified if + they are not shared and if no template was loaded so far. + Modifications on environments after the first template was loaded + will lead to surprising effects and undefined behavior. + + Here are the possible initialization parameters: + + `block_start_string` + The string marking the beginning of a block. Defaults to ``'{%'``. + + `block_end_string` + The string marking the end of a block. Defaults to ``'%}'``. + + `variable_start_string` + The string marking the beginning of a print statement. + Defaults to ``'{{'``. + + `variable_end_string` + The string marking the end of a print statement. Defaults to + ``'}}'``. + + `comment_start_string` + The string marking the beginning of a comment. Defaults to ``'{#'``. + + `comment_end_string` + The string marking the end of a comment. Defaults to ``'#}'``. + + `line_statement_prefix` + If given and a string, this will be used as prefix for line based + statements. See also :ref:`line-statements`. + + `line_comment_prefix` + If given and a string, this will be used as prefix for line based + comments. See also :ref:`line-statements`. + + .. versionadded:: 2.2 + + `trim_blocks` + If this is set to ``True`` the first newline after a block is + removed (block, not variable tag!). Defaults to `False`. + + `lstrip_blocks` + If this is set to ``True`` leading spaces and tabs are stripped + from the start of a line to a block. Defaults to `False`. + + `newline_sequence` + The sequence that starts a newline. Must be one of ``'\r'``, + ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a + useful default for Linux and OS X systems as well as web + applications. + + `keep_trailing_newline` + Preserve the trailing newline when rendering templates. + The default is ``False``, which causes a single newline, + if present, to be stripped from the end of the template. + + .. versionadded:: 2.7 + + `extensions` + List of Jinja extensions to use. This can either be import paths + as strings or extension classes. For more information have a + look at :ref:`the extensions documentation `. + + `optimized` + should the optimizer be enabled? Default is ``True``. + + `undefined` + :class:`Undefined` or a subclass of it that is used to represent + undefined values in the template. + + `finalize` + A callable that can be used to process the result of a variable + expression before it is output. For example one can convert + ``None`` implicitly into an empty string here. + + `autoescape` + If set to ``True`` the XML/HTML autoescaping feature is enabled by + default. For more details about autoescaping see + :class:`~markupsafe.Markup`. As of Jinja 2.4 this can also + be a callable that is passed the template name and has to + return ``True`` or ``False`` depending on autoescape should be + enabled by default. + + .. versionchanged:: 2.4 + `autoescape` can now be a function + + `loader` + The template loader for this environment. + + `cache_size` + The size of the cache. Per default this is ``400`` which means + that if more than 400 templates are loaded the loader will clean + out the least recently used template. If the cache size is set to + ``0`` templates are recompiled all the time, if the cache size is + ``-1`` the cache will not be cleaned. + + .. versionchanged:: 2.8 + The cache size was increased to 400 from a low 50. + + `auto_reload` + Some loaders load templates from locations where the template + sources may change (ie: file system or database). If + ``auto_reload`` is set to ``True`` (default) every time a template is + requested the loader checks if the source changed and if yes, it + will reload the template. For higher performance it's possible to + disable that. + + `bytecode_cache` + If set to a bytecode cache object, this object will provide a + cache for the internal Jinja bytecode so that templates don't + have to be parsed if they were not changed. + + See :ref:`bytecode-cache` for more information. + + `enable_async` + If set to true this enables async template execution which + allows using async functions and generators. + """ + + #: if this environment is sandboxed. Modifying this variable won't make + #: the environment sandboxed though. For a real sandboxed environment + #: have a look at jinja2.sandbox. This flag alone controls the code + #: generation by the compiler. + sandboxed = False + + #: True if the environment is just an overlay + overlayed = False + + #: the environment this environment is linked to if it is an overlay + linked_to: t.Optional["Environment"] = None + + #: shared environments have this set to `True`. A shared environment + #: must not be modified + shared = False + + #: the class that is used for code generation. See + #: :class:`~jinja2.compiler.CodeGenerator` for more information. + code_generator_class: t.Type["CodeGenerator"] = CodeGenerator + + concat = "".join + + #: the context class that is used for templates. See + #: :class:`~jinja2.runtime.Context` for more information. + context_class: t.Type[Context] = Context + + template_class: t.Type["Template"] + + def __init__( + self, + block_start_string: str = BLOCK_START_STRING, + block_end_string: str = BLOCK_END_STRING, + variable_start_string: str = VARIABLE_START_STRING, + variable_end_string: str = VARIABLE_END_STRING, + comment_start_string: str = COMMENT_START_STRING, + comment_end_string: str = COMMENT_END_STRING, + line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX, + line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX, + trim_blocks: bool = TRIM_BLOCKS, + lstrip_blocks: bool = LSTRIP_BLOCKS, + newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE, + keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE, + extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (), + optimized: bool = True, + undefined: t.Type[Undefined] = Undefined, + finalize: t.Optional[t.Callable[..., t.Any]] = None, + autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False, + loader: t.Optional["BaseLoader"] = None, + cache_size: int = 400, + auto_reload: bool = True, + bytecode_cache: t.Optional["BytecodeCache"] = None, + enable_async: bool = False, + ): + # !!Important notice!! + # The constructor accepts quite a few arguments that should be + # passed by keyword rather than position. However it's important to + # not change the order of arguments because it's used at least + # internally in those cases: + # - spontaneous environments (i18n extension and Template) + # - unittests + # If parameter changes are required only add parameters at the end + # and don't change the arguments (or the defaults!) of the arguments + # existing already. + + # lexer / parser information + self.block_start_string = block_start_string + self.block_end_string = block_end_string + self.variable_start_string = variable_start_string + self.variable_end_string = variable_end_string + self.comment_start_string = comment_start_string + self.comment_end_string = comment_end_string + self.line_statement_prefix = line_statement_prefix + self.line_comment_prefix = line_comment_prefix + self.trim_blocks = trim_blocks + self.lstrip_blocks = lstrip_blocks + self.newline_sequence = newline_sequence + self.keep_trailing_newline = keep_trailing_newline + + # runtime information + self.undefined: t.Type[Undefined] = undefined + self.optimized = optimized + self.finalize = finalize + self.autoescape = autoescape + + # defaults + self.filters = DEFAULT_FILTERS.copy() + self.tests = DEFAULT_TESTS.copy() + self.globals = DEFAULT_NAMESPACE.copy() + + # set the loader provided + self.loader = loader + self.cache = create_cache(cache_size) + self.bytecode_cache = bytecode_cache + self.auto_reload = auto_reload + + # configurable policies + self.policies = DEFAULT_POLICIES.copy() + + # load extensions + self.extensions = load_extensions(self, extensions) + + self.is_async = enable_async + _environment_config_check(self) + + def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None: + """Adds an extension after the environment was created. + + .. versionadded:: 2.5 + """ + self.extensions.update(load_extensions(self, [extension])) + + def extend(self, **attributes: t.Any) -> None: + """Add the items to the instance of the environment if they do not exist + yet. This is used by :ref:`extensions ` to register + callbacks and configuration values without breaking inheritance. + """ + for key, value in attributes.items(): + if not hasattr(self, key): + setattr(self, key, value) + + def overlay( + self, + block_start_string: str = missing, + block_end_string: str = missing, + variable_start_string: str = missing, + variable_end_string: str = missing, + comment_start_string: str = missing, + comment_end_string: str = missing, + line_statement_prefix: t.Optional[str] = missing, + line_comment_prefix: t.Optional[str] = missing, + trim_blocks: bool = missing, + lstrip_blocks: bool = missing, + newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing, + keep_trailing_newline: bool = missing, + extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing, + optimized: bool = missing, + undefined: t.Type[Undefined] = missing, + finalize: t.Optional[t.Callable[..., t.Any]] = missing, + autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing, + loader: t.Optional["BaseLoader"] = missing, + cache_size: int = missing, + auto_reload: bool = missing, + bytecode_cache: t.Optional["BytecodeCache"] = missing, + enable_async: bool = missing, + ) -> "te.Self": + """Create a new overlay environment that shares all the data with the + current environment except for cache and the overridden attributes. + Extensions cannot be removed for an overlayed environment. An overlayed + environment automatically gets all the extensions of the environment it + is linked to plus optional extra extensions. + + Creating overlays should happen after the initial environment was set + up completely. Not all attributes are truly linked, some are just + copied over so modifications on the original environment may not shine + through. + + .. versionchanged:: 3.1.5 + ``enable_async`` is applied correctly. + + .. versionchanged:: 3.1.2 + Added the ``newline_sequence``, ``keep_trailing_newline``, + and ``enable_async`` parameters to match ``__init__``. + """ + args = dict(locals()) + del args["self"], args["cache_size"], args["extensions"], args["enable_async"] + + rv = object.__new__(self.__class__) + rv.__dict__.update(self.__dict__) + rv.overlayed = True + rv.linked_to = self + + for key, value in args.items(): + if value is not missing: + setattr(rv, key, value) + + if cache_size is not missing: + rv.cache = create_cache(cache_size) + else: + rv.cache = copy_cache(self.cache) + + rv.extensions = {} + for key, value in self.extensions.items(): + rv.extensions[key] = value.bind(rv) + if extensions is not missing: + rv.extensions.update(load_extensions(rv, extensions)) + + if enable_async is not missing: + rv.is_async = enable_async + + return _environment_config_check(rv) + + @property + def lexer(self) -> Lexer: + """The lexer for this environment.""" + return get_lexer(self) + + def iter_extensions(self) -> t.Iterator["Extension"]: + """Iterates over the extensions by priority.""" + return iter(sorted(self.extensions.values(), key=lambda x: x.priority)) + + def getitem( + self, obj: t.Any, argument: t.Union[str, t.Any] + ) -> t.Union[t.Any, Undefined]: + """Get an item or attribute of an object but prefer the item.""" + try: + return obj[argument] + except (AttributeError, TypeError, LookupError): + if isinstance(argument, str): + try: + attr = str(argument) + except Exception: + pass + else: + try: + return getattr(obj, attr) + except AttributeError: + pass + return self.undefined(obj=obj, name=argument) + + def getattr(self, obj: t.Any, attribute: str) -> t.Any: + """Get an item or attribute of an object but prefer the attribute. + Unlike :meth:`getitem` the attribute *must* be a string. + """ + try: + return getattr(obj, attribute) + except AttributeError: + pass + try: + return obj[attribute] + except (TypeError, LookupError, AttributeError): + return self.undefined(obj=obj, name=attribute) + + def _filter_test_common( + self, + name: t.Union[str, Undefined], + value: t.Any, + args: t.Optional[t.Sequence[t.Any]], + kwargs: t.Optional[t.Mapping[str, t.Any]], + context: t.Optional[Context], + eval_ctx: t.Optional[EvalContext], + is_filter: bool, + ) -> t.Any: + if is_filter: + env_map = self.filters + type_name = "filter" + else: + env_map = self.tests + type_name = "test" + + func = env_map.get(name) # type: ignore + + if func is None: + msg = f"No {type_name} named {name!r}." + + if isinstance(name, Undefined): + try: + name._fail_with_undefined_error() + except Exception as e: + msg = f"{msg} ({e}; did you forget to quote the callable name?)" + + raise TemplateRuntimeError(msg) + + args = [value, *(args if args is not None else ())] + kwargs = kwargs if kwargs is not None else {} + pass_arg = _PassArg.from_obj(func) + + if pass_arg is _PassArg.context: + if context is None: + raise TemplateRuntimeError( + f"Attempted to invoke a context {type_name} without context." + ) + + args.insert(0, context) + elif pass_arg is _PassArg.eval_context: + if eval_ctx is None: + if context is not None: + eval_ctx = context.eval_ctx + else: + eval_ctx = EvalContext(self) + + args.insert(0, eval_ctx) + elif pass_arg is _PassArg.environment: + args.insert(0, self) + + return func(*args, **kwargs) + + def call_filter( + self, + name: str, + value: t.Any, + args: t.Optional[t.Sequence[t.Any]] = None, + kwargs: t.Optional[t.Mapping[str, t.Any]] = None, + context: t.Optional[Context] = None, + eval_ctx: t.Optional[EvalContext] = None, + ) -> t.Any: + """Invoke a filter on a value the same way the compiler does. + + This might return a coroutine if the filter is running from an + environment in async mode and the filter supports async + execution. It's your responsibility to await this if needed. + + .. versionadded:: 2.7 + """ + return self._filter_test_common( + name, value, args, kwargs, context, eval_ctx, True + ) + + def call_test( + self, + name: str, + value: t.Any, + args: t.Optional[t.Sequence[t.Any]] = None, + kwargs: t.Optional[t.Mapping[str, t.Any]] = None, + context: t.Optional[Context] = None, + eval_ctx: t.Optional[EvalContext] = None, + ) -> t.Any: + """Invoke a test on a value the same way the compiler does. + + This might return a coroutine if the test is running from an + environment in async mode and the test supports async execution. + It's your responsibility to await this if needed. + + .. versionchanged:: 3.0 + Tests support ``@pass_context``, etc. decorators. Added + the ``context`` and ``eval_ctx`` parameters. + + .. versionadded:: 2.7 + """ + return self._filter_test_common( + name, value, args, kwargs, context, eval_ctx, False + ) + + @internalcode + def parse( + self, + source: str, + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + ) -> nodes.Template: + """Parse the sourcecode and return the abstract syntax tree. This + tree of nodes is used by the compiler to convert the template into + executable source- or bytecode. This is useful for debugging or to + extract information from templates. + + If you are :ref:`developing Jinja extensions ` + this gives you a good overview of the node tree generated. + """ + try: + return self._parse(source, name, filename) + except TemplateSyntaxError: + self.handle_exception(source=source) + + def _parse( + self, source: str, name: t.Optional[str], filename: t.Optional[str] + ) -> nodes.Template: + """Internal parsing function used by `parse` and `compile`.""" + return Parser(self, source, name, filename).parse() + + def lex( + self, + source: str, + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + ) -> t.Iterator[t.Tuple[int, str, str]]: + """Lex the given sourcecode and return a generator that yields + tokens as tuples in the form ``(lineno, token_type, value)``. + This can be useful for :ref:`extension development ` + and debugging templates. + + This does not perform preprocessing. If you want the preprocessing + of the extensions to be applied you have to filter source through + the :meth:`preprocess` method. + """ + source = str(source) + try: + return self.lexer.tokeniter(source, name, filename) + except TemplateSyntaxError: + self.handle_exception(source=source) + + def preprocess( + self, + source: str, + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + ) -> str: + """Preprocesses the source with all extensions. This is automatically + called for all parsing and compiling methods but *not* for :meth:`lex` + because there you usually only want the actual source tokenized. + """ + return reduce( + lambda s, e: e.preprocess(s, name, filename), + self.iter_extensions(), + str(source), + ) + + def _tokenize( + self, + source: str, + name: t.Optional[str], + filename: t.Optional[str] = None, + state: t.Optional[str] = None, + ) -> TokenStream: + """Called by the parser to do the preprocessing and filtering + for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`. + """ + source = self.preprocess(source, name, filename) + stream = self.lexer.tokenize(source, name, filename, state) + + for ext in self.iter_extensions(): + stream = ext.filter_stream(stream) # type: ignore + + if not isinstance(stream, TokenStream): + stream = TokenStream(stream, name, filename) + + return stream + + def _generate( + self, + source: nodes.Template, + name: t.Optional[str], + filename: t.Optional[str], + defer_init: bool = False, + ) -> str: + """Internal hook that can be overridden to hook a different generate + method in. + + .. versionadded:: 2.5 + """ + return generate( # type: ignore + source, + self, + name, + filename, + defer_init=defer_init, + optimized=self.optimized, + ) + + def _compile(self, source: str, filename: str) -> CodeType: + """Internal hook that can be overridden to hook a different compile + method in. + + .. versionadded:: 2.5 + """ + return compile(source, filename, "exec") + + @typing.overload + def compile( + self, + source: t.Union[str, nodes.Template], + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + raw: "te.Literal[False]" = False, + defer_init: bool = False, + ) -> CodeType: ... + + @typing.overload + def compile( + self, + source: t.Union[str, nodes.Template], + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + raw: "te.Literal[True]" = ..., + defer_init: bool = False, + ) -> str: ... + + @internalcode + def compile( + self, + source: t.Union[str, nodes.Template], + name: t.Optional[str] = None, + filename: t.Optional[str] = None, + raw: bool = False, + defer_init: bool = False, + ) -> t.Union[str, CodeType]: + """Compile a node or template source code. The `name` parameter is + the load name of the template after it was joined using + :meth:`join_path` if necessary, not the filename on the file system. + the `filename` parameter is the estimated filename of the template on + the file system. If the template came from a database or memory this + can be omitted. + + The return value of this method is a python code object. If the `raw` + parameter is `True` the return value will be a string with python + code equivalent to the bytecode returned otherwise. This method is + mainly used internally. + + `defer_init` is use internally to aid the module code generator. This + causes the generated code to be able to import without the global + environment variable to be set. + + .. versionadded:: 2.4 + `defer_init` parameter added. + """ + source_hint = None + try: + if isinstance(source, str): + source_hint = source + source = self._parse(source, name, filename) + source = self._generate(source, name, filename, defer_init=defer_init) + if raw: + return source + if filename is None: + filename = "