diff --git a/.changelogs/1.0.7/135_fix_systemd_service_install_target.yml b/.changelogs/1.0.7/135_fix_systemd_service_install_target.yml deleted file mode 100644 index 5a4fcfc..0000000 --- a/.changelogs/1.0.7/135_fix_systemd_service_install_target.yml +++ /dev/null @@ -1,2 +0,0 @@ -fixed: - - Fix systemd service file missing install target and network requirements (by @thomasfinstad). [#135] diff --git a/.changelogs/1.1.0/114_refactor_code_base.yml b/.changelogs/1.1.0/114_refactor_code_base.yml new file mode 100644 index 0000000..9c9be62 --- /dev/null +++ b/.changelogs/1.1.0/114_refactor_code_base.yml @@ -0,0 +1,12 @@ +fixed: + - Refactored code base for ProxLB [#114] + - Renamed package from `proxlb` to `python3-proxlb` to align with Debian packaging guidelines [#114] + - Switched to `pycodestyle` for linting [#114] + - Package building will be done within GitHub actions pipeline [#114] + - ProxLB now only returns a warning when no guests for further balancing are not present (instead of quitting) [132#] + - All nodes (according to the free resources) will be used now [#130] + - Fixed logging outputs where highest/lowest were mixed-up [#129] + - Stop balancing when movement would get worste (new force param to enfoce for affinity rules) [#128] + - Added requested documentation regarding Proxmox HA groups [#127] + - Rewrite of the whole affinity/anti-affinity rules evaluation and placement [#123] + - Fixed the `ignore` parameter for nodes where the node and guests on the node will be untouched [#102] \ No newline at end of file diff --git a/.changelogs/1.1.0/release_meta.yml b/.changelogs/1.1.0/release_meta.yml new file mode 100644 index 0000000..c19765d --- /dev/null +++ b/.changelogs/1.1.0/release_meta.yml @@ -0,0 +1 @@ +date: TBD diff --git a/.flake8 b/.flake8 deleted file mode 100644 index ad137c2..0000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -per-file-ignores = - proxlb: E501,E221,E266,E231,E127,E222,E128 diff --git a/.github/workflows/02-create-package.yml b/.github/workflows/02-create-package.yml deleted file mode 100644 index c2e6bb8..0000000 --- a/.github/workflows/02-create-package.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Run basic pipeline on push -on: [push] -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.8"] - steps: - - uses: actions/checkout@v3 - - name: Set up Python for ProxLB - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies for ProxLB - run: | - python -m pip install --upgrade pip - pip install pytest proxmoxer flake8 - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Run Python linting - run: | - python3 -m flake8 proxlb - - name: Create distro packages - run: | - cd packaging - ./01_package.sh diff --git a/.github/workflows/10-code-liniting.yml b/.github/workflows/10-code-liniting.yml new file mode 100644 index 0000000..2a6cf8c --- /dev/null +++ b/.github/workflows/10-code-liniting.yml @@ -0,0 +1,21 @@ +name: Code linting +on: [push] +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8"] + steps: + - uses: actions/checkout@v3 + - name: Setup dependencies for code linting + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install additional dependencies for code linting + run: | + sudo apt-get update + sudo apt-get -y install python3-pycodestyle pycodestyle + - name: Run code linting on ProxLB Python code + run: | + pycodestyle proxlb/* diff --git a/.github/workflows/20-pipeline-build-deb-package.yml b/.github/workflows/20-pipeline-build-deb-package.yml new file mode 100644 index 0000000..4e65a6e --- /dev/null +++ b/.github/workflows/20-pipeline-build-deb-package.yml @@ -0,0 +1,73 @@ +name: "Build package: .deb" +on: [push] +jobs: + lint-code-proxlb: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8"] + steps: + - uses: actions/checkout@v3 + - name: Setup dependencies for code linting + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install additional dependencies for code linting + run: | + sudo apt-get update + sudo apt-get -y install python3-pycodestyle pycodestyle + - name: Run code linting on ProxLB Python code + run: | + pycodestyle proxlb/* && \ + echo "OK: Code linting successfully performed on ProxLB code." + + build-package-debian: + needs: lint-code-proxlb + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v3 + with: + ref: 'development' + + - name: Set up Docker with Debian image + run: | + docker pull debian:latest + + - name: Build DEB package in Docker container + run: | + docker run --rm -v $(pwd):/workspace -w /workspace debian:latest bash -c " + # Install dependencies + apt-get update && \ + apt-get install -y python3 python3-setuptools debhelper dh-python python3-pip python3-stdeb python3-proxmoxer python3-requests python3-urllib3 && \ + # Build package + python3 setup.py --command-packages=stdeb.command bdist_deb && \ + echo 'OK: Debian package successfully created.' + " + + - name: Upload Debian package python3-proxlb as artifact + uses: actions/upload-artifact@v4 + with: + name: debian-package + path: deb_dist/*.deb + + integration-test-debian: + needs: build-package-debian + runs-on: ubuntu-latest + steps: + - name: Download Debian package artifact + uses: actions/download-artifact@v4 + with: + name: debian-package + path: deb_dist/ + + - name: Set up Docker with Debian image + run: docker pull debian:latest + + - name: Install and test Debian package in Docker container + run: | + docker run --rm -v $(pwd)/deb_dist:/deb_dist -w /deb_dist debian:latest bash -c " + apt-get update && \ + apt-get install -y ./python3-proxlb*.deb && \ + python3 -c 'import proxlb; print(\"OK: Debian package successfully installed.\")' + " \ No newline at end of file diff --git a/.github/workflows/20-pipeline-build-rpm-package.yml b/.github/workflows/20-pipeline-build-rpm-package.yml new file mode 100644 index 0000000..8149964 --- /dev/null +++ b/.github/workflows/20-pipeline-build-rpm-package.yml @@ -0,0 +1,96 @@ +name: "Build package: .rpm" +on: [push] +jobs: + lint-code-proxlb: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8"] + steps: + - uses: actions/checkout@v3 + - name: Setup dependencies for code linting + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install additional dependencies for code linting + run: | + sudo apt-get update + sudo apt-get -y install python3-pycodestyle pycodestyle + - name: Run code linting on ProxLB Python code + run: | + pycodestyle proxlb/* && \ + echo "OK: Code linting successfully performed on ProxLB code." + + build-package-rpm: + needs: lint-code-proxlb + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v3 + with: + ref: 'development' + + - name: Set up Docker with Debian image + run: | + docker pull debian:latest + + - name: Build DEB package in Docker container + run: | + docker run --rm -v $(pwd):/workspace -w /workspace debian:latest bash -c " + # Install dependencies + apt-get update && \ + apt-get install -y python3 python3-setuptools rpm debhelper dh-python python3-pip python3-stdeb python3-proxmoxer python3-requests python3-urllib3 && \ + # Build package + python3 setup.py --command-packages=stdeb.command bdist_rpm && \ + echo 'OK: RPM package successfully created.' + " + + - name: Upload RPM package python3-proxlb as artifact + uses: actions/upload-artifact@v4 + with: + name: rpm-package + path: dist/*.rpm + + # integration-test-rpm-rockylinux-9: + # needs: build-package-rpm + # runs-on: ubuntu-latest + # steps: + # - name: Download RPM package artifact + # uses: actions/download-artifact@v4 + # with: + # name: rpm-package + # path: dist/ + + # - name: Set up Docker with RockyLinux 9 image + # run: docker pull rockylinux:9 + + # - name: Install and test RPM package in Rocky Linux Docker container + # run: | + # docker run --rm -v $(pwd)/dist:/dist -w /dist rockylinux:9 bash -c " + # # DNF does not handle wildcards well + # rpm_file=\$(ls proxlb*.noarch.rpm) && \ + # dnf install -y \$rpm_file && \ + # python3 -c 'import proxlb; print(\"OK: RPM package successfully installed.\")' + # " + + # integration-test-rpm-rockylinux-8: + # needs: build-package-rpm + # runs-on: ubuntu-latest + # steps: + # - name: Download RPM package artifact + # uses: actions/download-artifact@v4 + # with: + # name: rpm-package + # path: dist/ + + # - name: Set up Docker with RockyLinux 8 image + # run: docker pull rockylinux:8 + + # - name: Install and test RPM package in Rocky Linux Docker container + # run: | + # docker run --rm -v $(pwd)/dist:/dist -w /dist rockylinux:8 bash -c " + # # DNF does not handle wildcards well + # rpm_file=\$(ls proxlb*.noarch.rpm) && \ + # dnf install -y \$rpm_file && \ + # python3 -c 'import proxlb; print(\"OK: RPM package successfully installed.\")' + # " diff --git a/.gitignore b/.gitignore index 53621a6..85fbc8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -packaging/changelog-fragments-creator/ -dev/ \ No newline at end of file +__pycache__ +*.pyc +.DS_Store +build/ +dist/ +*.egg-info/ +proxlb_dev.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index cd4ebcb..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,122 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## [1.0.6] - 2024-12-24 - -### Fixed - -- Fix maintenance mode when using cli arg and config mode by using the merged list (by @CartCaved). [#119] -- Fix that a scheduler time definition of 1 (int) gets wrongly interpreted as a bool (by @gyptazy). [#115] - -## [1.0.5] - 2024-10-30 - -### Changed - -- Change docs to make bool usage in configs more clear. [#104] - -### Fixed - -- Fix migration from local disks (by @greenlogles). [#113] -- Fix allowed values (add DEBUG, WARNING) for log verbosity. [#98] -- Fix node (and its objects) evaluation when not reachable (e.g., maintenance). [#107] -- Fix evaluation of maintenance mode where comparing list & string resulted in a crash (by @glitchvern). [#106] - - -## [1.0.4] - 2024-10-11 - -### Added - -- Add feature to make API timeout configureable. [#91] -- Add maintenance mode to evacuate a node and move workloads for other nodes in the cluster. [#58] -- Add version output cli arg. [#89] - -### Changed - -- Run storage balancing only on supported shared storages. [#79] -- Run storage balancing only when needed to save time. [#79] - -### Fixed - -- Fix CPU balancing where calculations are done in float instead of int. (by @glitchvern) [#75] -- Fix documentation for the underlying infrastructure. [#81] - - -## [1.0.3] - 2024-09-12 - -### Added - -- Add storage balancing function. [#51] -- Add a convert function to cast all bool alike options from configparser to bools. [#53] -- Add a config parser options for future features. [#53] -- Add a config versio schema that must be supported by ProxLB. [#53] -- Add doc how to add dedicated user for authentication. (by @Dulux-Oz) -- Add feature to allow the API hosts being provided as a comma separated list. [#60] -- Add cli arg `-b` to return the next best node for next VM/CT placement. [#8] - -### Changed - -- Improve the underlying code base for future implementations. [#53] -- Provide a more reasonable output when HA services are not active in a Proxmox cluster. [#68] - -### Fixed - -- Fixed `master_only` function by inverting the condition. -- Improved the overall validation and error handling. [#64] -- Fix bug in the `proxlb.conf` in the vm_balancing section. -- Fix anti-affinity rules not evaluating a new and different node. [#67] -- Fix documentation for the master_only parameter placed in the wrong config section. [#74] -- Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error. [#71] - - -## [1.0.2] - 2024-08-13 - -### Added - -- Add option to run ProxLB only on the Proxmox's master node in the cluster (reg. HA feature). [#40] -- Add option to run migrations in parallel or sequentially. [#41] - -### Changed - -- Fix daemon timer to use hours instead of minutes. [#45] - -### Fixed - -- Fix CMake packaging for Debian package to avoid overwriting the config file. [#49] - - -## [1.0.0] - 2024-08-01 - -### Added - -- Add option_mode to rebalance by node's free resources in percent (instead of bytes). [#29] -- Add LXC/Container integration. [#27] -- Add exclude grouping feature to rebalance VMs from being located together to new nodes. [#4] -- Add dry-run support to see what kind of rebalancing would be done. [#6] -- Add Docker/Podman support. [#10 by @daanbosch] -- Add feature to prevent VMs from being relocated by defining a wildcard pattern. [#7] -- Add feature to prevent VMs from being relocated by defining the 'plb_ignore_vm' tag. [#7] -- Add include grouping feature to rebalance VMs bundled to new nodes. [#3] -- Add option to rebalance by assigned VM resources to avoid overprovisioning. [#16] -- Add feature to make log verbosity configurable [#17]. - -### Changed - -- Adjusted general logging and log more details. - - -## [0.9.9] - 2024-07-06 - -### Added - -- Initial public development release of ProxLB. - - -## [0.9.0] - 2024-02-01 - -### Added - -- Development release of ProxLB. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 79ba0f3..00eb94b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -116,6 +116,6 @@ By participating in this project, you agree to abide by our [Code of Conduct](CO ## Getting Help -If you need help or have any questions, feel free to reach out by creating an issue or by joining our [discussion forum](https://github.com/gyptazy/proxlb/discussions). You can also refer to our [documentation](https://github.com/gyptazy/ProxLB/tree/main/docs) for more information about the project or join our [chat room](https://matrix.to/#/#proxlb:gyptazy.ch) in Matrix. +If you need help or have any questions, feel free to reach out by creating an issue or by joining our [discussion forum](https://github.com/gyptazy/proxlb/discussions). You can also refer to our [documentation](https://github.com/gyptazy/ProxLB/tree/main/docs) for more information about the project or join our [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix. -Thank you for contributing to ProxLB! Together, we can enhance the efficiency and performance of Proxmox clusters. +Thank you for contributing to ProxLB! Together, we can enhance the efficiency and performance of Proxmox clusters. \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index e8bf3fb..0000000 --- a/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -# Use the official Python 3.12 image -FROM python:3.12 - -# Labels -LABEL maintainer="gyptazy@gyptazy.ch" -LABEL org.label-schema.schema-version="0.9" -LABEL org.label-schema.description="ProxLB - Rebalance VM workloads across nodes in a Proxmox cluster." -LABEL org.label-schema.url="https://github.com/gyptazy/ProxLB" - -# Create a directory for the app -WORKDIR /app - -# Copy the python program from the current directory to /app -COPY proxlb /app/proxlb - -# Copy requirements to the container -COPY requirements.txt /app/requirements.txt - -RUN pip install -r /app/requirements.txt - -# Set the entry point to use the virtual environment's python -ENTRYPOINT ["python3", "/app/proxlb"] diff --git a/LICENSE b/LICENSE index f288702..e72bfdd 100644 --- a/LICENSE +++ b/LICENSE @@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read -. +. \ No newline at end of file diff --git a/README.md b/README.md index d7a9625..53070c3 100644 --- a/README.md +++ b/README.md @@ -5,189 +5,195 @@

+# :warning: Important: ProxLB 1.1.x is coming +This repository is currently under heavy work and changes. During that time it might come to issues, non working pipelines or wrong documentation. Please select a stable release tag for a suitable version during this time! + ## Table of Contents -- [ProxLB - (Re)Balance VM Workloads in Proxmox Clusters](#proxlb---rebalance-vm-workloads-in-proxmox-clusters) - - [Table of Contents](#table-of-contents) - - [Introduction](#introduction) - - [Video of Migration](#video-of-migration) - - [Features](#features) - - [How does it work?](#how-does-it-work) - - [Usage](#usage) - - [Dependencies](#dependencies) - - [Options](#options) - - [Notes](#notes) - - [Parameters](#parameters) - - [Balancing](#balancing) - - [General](#general) - - [By Used Memory of VMs/CTs](#by-used-memory-of-vmscts) - - [By Assigned Memory of VMs/CTs](#by-assigned-memory-of-vmscts) - - [Storage Balancing](#storage-balancing) - - [Affinity Rules / Grouping Relationships](#affinity-rules--grouping-relationships) - - [Affinity (Stay Together)](#affinity-stay-together) - - [Anti-Affinity (Keep Apart)](#anti-affinity-keep-apart) - - [Ignore VMs (Tag Style)](#ignore-vms-tag-style) - - [Systemd](#systemd) - - [Manual](#manual) - - [Proxmox GUI Integration](#proxmox-gui-integration) - - [Quick Start](#quick-start) - - [Container Quick Start (Docker/Podman)](#container-quick-start-dockerpodman) - - [Logging](#logging) - - [Motivation](#motivation) - - [References](#references) - - [Downloads](#downloads) - - [Packages](#packages) - - [Repository](#repository) - - [Stable Releases](#stable-releases) - - [Beta/Testing Releases](#betatesting-releases) - - [Container Images (Docker/Podman)](#container-images-dockerpodman) - - [Misc](#misc) - - [Bugs](#bugs) - - [Contributing](#contributing) - - [Documentation](#documentation) - - [Support](#support) - - [Author(s)](#authors) +1. [Introduction](#introduction) +2. [Features](#features) +3. [How does it work?](#how-does-it-work) +4. [Installation](#installation) + 1. [Requirements / Dependencies](#requirements--dependencies) + 2. [Debian Package](#debian-package) + 3. [RedHat Package](#redhat-package) + 4. [Container / Docker](#container--docker) + 5. [Source](#source) +5. [Upgrading](#upgrading) + 1. [Upgrading from < 1.1.0](#upgrading-from--110) + 2. [Upgrading from >= 1.1.0](#upgrading-from--110) +6. [Usage / Configuration](#usage--configuration) + 1. [GUI Integration](#gui-integration) + 2. [Proxmox HA Integration](#proxmox-ha-integration) + 3. [Options](#options) +7. [Affinity & Anti-Affinity Rules](#affinity--anti-affinity-rules) + 1. [Affinity Rules](#affinity-rules) + 2. [Anti-Affinity Rules](#anti-affinity-rules) +8. [Maintenance](#maintenance) +9. [Misc](#misc) + 1. [Bugs](#bugs) + 2. [Contributing](#contributing) + 3. [Documentation](#documentation) + 4. [Support](#support) +10. [Author(s)](#authors) + ## Introduction -`ProxLB` (PLB) is an advanced tool designed to enhance the efficiency and performance of Proxmox clusters by optimizing the distribution of virtual machines (VMs) or Containers (CTs) across the cluster nodes by using the Proxmox API. ProxLB meticulously gathers and analyzes a comprehensive set of resource metrics from both the cluster nodes and the running VMs. These metrics include CPU usage, memory consumption, and disk utilization, specifically focusing on local disk resources. +ProxLB is an advanced load balancing solution specifically designed for Proxmox clusters, addressing the absence of a Dynamic Resource Scheduler (DRS) that is familiar to VMware users. As a third-party solution, ProxLB enhances the management and efficiency of Proxmox clusters by intelligently distributing workloads across available nodes. Workloads can be balanced by different times like the guest's memory, CPU or disk usage or their assignment to avoid overprovisioning and ensuring resources. -PLB collects resource usage data from each node in the Proxmox cluster, including CPU, (local) disk and memory utilization. Additionally, it gathers resource usage statistics from all running VMs, ensuring a granular understanding of the cluster's workload distribution. +One of the key advantages of ProxLB is that it is fully open-source and free, making it accessible for anyone to use, modify, and contribute to. This ensures transparency and fosters community-driven improvements. ProxLB supports filtering and ignoring specific nodes and guests through configuration files and API calls, providing administrators with the flexibility to tailor the load balancing behavior to their specific needs. -Intelligent rebalancing is a key feature of ProxLB where it re-balances VMs based on their memory, disk or CPU usage, ensuring that no node is overburdened while others remain underutilized. The rebalancing capabilities of PLB significantly enhance cluster performance and reliability. By ensuring that resources are evenly distributed, PLB helps prevent any single node from becoming a performance bottleneck, improving the reliability and stability of the cluster. Efficient rebalancing leads to better utilization of available resources, potentially reducing the need for additional hardware investments and lowering operational costs. +A standout feature of ProxLB is its maintenance mode. When enabled, all guest workloads are automatically moved to other nodes within the cluster, ensuring that a node can be safely updated, rebooted, or undergo hardware maintenance without disrupting the overall cluster operation. Additionally, ProxLB supports both affinity and anti-affinity rules, allowing operators to group multiple guests to run together on the same node or ensure that certain guests do not run on the same node, depending on the cluster's node count. This feature is crucial for optimizing performance and maintaining high availability. -Automated rebalancing reduces the need for manual actions, allowing operators to focus on other critical tasks, thereby increasing operational efficiency. +ProxLB can also return the best next node for guest placement, which can be integrated into CI/CD pipelines using tools like Ansible or Terraform. This capability streamlines the deployment process and ensures efficient resource utilization. Furthermore, ProxLB leverages the Proxmox API, including the entire ACL (Access Control List) system, for secure and efficient operation. Unlike some solutions, it does not require SSH access, enhancing security and simplifying configuration. + +Overall, ProxLB significantly enhances resource management by intelligently distributing workloads, reducing downtime through its maintenance mode, and providing improved flexibility with affinity and anti-affinity rules. Its seamless integration with CI/CD tools and reliance on the Proxmox API make it a robust and secure solution for optimizing Proxmox cluster performance. ### Video of Migration ## Features +ProxLB's key features are by enabling automatic rebalancing of VMs and CTs across a Proxmox cluster based on memory, CPU, and local disk usage while identifying optimal nodes for automation. It supports maintenance mode, affinity rules, and seamless Proxmox API integration with ACL support, offering flexible usage as a one-time operation, a daemon, or through the Proxmox Web GUI. + +**Features** * Rebalance VMs/CTs in the cluster by: * Memory * Disk (only local storage) * CPU -* Rebalance Storage in the cluster - * Rebalance VMs/CTs disks to other storage pools - * Rebalance by used storage -* Get best Node for new VM/CT placement in cluster -* Performing - * Periodically - * One-shot solution -* Types - * Rebalance only VMs - * Rebalance only CTs - * Rebalance all (VMs and CTs) - * Rebalance VM/CT disks (Storage) -* Filter - * Exclude nodes - * Exclude virtual machines -* Grouping - * Include groups (VMs that are rebalanced to nodes together) - * Exclude groups (VMs that must run on different nodes) - * Ignore groups (VMs that should be untouched) -* Dry-run support - * Human readable output in CLI - * JSON output for further parsing -* Migrate VM workloads away (e.g. maintenance preparation) +* Get best nodes for further automation +* Supported Guest Types + * VMs + * CTs +* Maintenance Mode + * Set node(s) into maintenance + * Move all workloads to different nodes +* Affinity / Anti-Affinity Rules * Fully based on Proxmox API + * Fully integrated into the Proxmox ACL + * No SSH required * Usage - * One-Shot (one-shot) - * Periodically (daemon) - * Proxmox Web GUI Integration (optional) + * One-Time + * Daemon + * Proxmox Web GUI Integration ## How does it work? ProxLB is a load-balancing system designed to optimize the distribution of virtual machines (VMs) and containers (CTs) across a cluster. It works by first gathering resource usage metrics from all nodes in the cluster through the Proxmox API. This includes detailed resource metrics for each VM and CT on every node. ProxLB then evaluates the difference between the maximum and minimum resource usage of the nodes, referred to as "Balanciness." If this difference exceeds a predefined threshold (which is configurable), the system initiates the rebalancing process. Before starting any migrations, ProxLB validates that rebalancing actions are necessary and beneficial. Depending on the selected balancing mode — such as CPU, memory, or disk — it creates a balancing matrix. This matrix sorts the VMs by their maximum used or assigned resources, identifying the VM with the highest usage. ProxLB then places this VM on the node with the most free resources in the selected balancing type. This process runs recursively until the operator-defined Balanciness is achieved. Balancing can be defined for the used or max. assigned resources of VMs/CTs. -## Usage -Running PLB is easy and it runs almost everywhere since it just depends on `Python3` and the `proxmoxer` library. Therefore, it can directly run on a Proxmox node, dedicated systems like Debian, RedHat, or even FreeBSD, as long as the API is reachable by the client running PLB. +## Installation -### Dependencies -* Python3 -* proxmoxer (Python module) +### Requirements / Dependencies +* Python3.x +* proxmoxer +* requests +* urllib3 +* pyyaml + +The dependencies can simply be installed with `pip` by running the following command: +``` +pip install -r requirements.txt +``` + +Distribution packages, such like the provided `.deb` package will automatically resolve and install all required dependencies by using already packaged version from the distribution's repository. + +### Debian Package + +### RedHat Package + +### Container / Docker + +### Source + +## Upgrading + +### Upgrading from < 1.1.0 +Upgrading ProxLB is not supported due to a fundamental redesign introduced in version 1.1.x. With this update, ProxLB transitioned from a monolithic application to a pure Python-style project, embracing a more modular and flexible architecture. This shift aimed to improve maintainability and extensibility while keeping up with modern development practices. Additionally, ProxLB moved away from traditional ini-style configuration files and adopted YAML for configuration management. This change simplifies configuration handling, reduces the need for extensive validation, and ensures better type casting, ultimately providing a more streamlined and user-friendly experience. + +### Upgrading from >= 1.1.0 +Uprading within the current stable versions, starting from 1.1.0, will be possible in all supported ways. + +## Usage / Configuration +Running ProxLB is straightforward and versatile, as it only requires `Python3` and the `proxmoxer` library. This means ProxLB can be executed directly on a Proxmox node or on dedicated systems such as Debian, RedHat, or even FreeBSD, provided that the Proxmox API is accessible from the client running ProxLB. ProxLB can also run inside a Container - Docker or LXC - and is simply up to you. + +### GUI Integration + ProxLB can also be accessed through the Proxmox Web UI by installing the optional `pve-proxmoxlb-service-ui` package, which depends on the proxlb package. For full Web UI integration, this package must be installed on all nodes within the cluster. Once installed, a new menu item - `Rebalancing`, appears in the cluster level under the HA section. Once installed, it offers two key functionalities: +* Rebalancing VM workloads +* Migrate VM workloads away from a defined node (e.g. maintenance preparation) + +**Note:** This package is currently discontinued and will be readded at a later time. See also: [#44: How to install pve-proxmoxlb-service-ui package](https://github.com/gyptazy/ProxLB/issues/44). + +### Proxmox HA Integration +Proxmox HA (High Availability) groups are designed to ensure that virtual machines (VMs) remain running within a Proxmox cluster. HA groups define specific rules for where VMs should be started or migrated in case of node failures, ensuring minimal downtime and automatic recovery. + +However, when used in conjunction with ProxLB, the built-in load balancer for Proxmox, conflicts can arise. ProxLB operates with its own logic for workload distribution, taking into account affinity and anti-affinity rules. While it effectively balances guest workloads, it may re-shift and redistribute VMs in a way that does not align with HA group constraints, potentially leading to unsuitable placements. + +Due to these conflicts, it is currently not recommended to use both HA groups and ProxLB simultaneously. The interaction between the two mechanisms can lead to unexpected behavior, where VMs might not adhere to HA group rules after being moved by ProxLB. + +A solution to improve compatibility between HA groups and ProxLB is under evaluation, aiming to ensure that both features can work together without disrupting VM placement strategies. + +See also: [#65: Host groups: Honour HA groups](https://github.com/gyptazy/ProxLB/issues/65). ### Options -The following options can be set in the `proxlb.conf` file: +The following options can be set in the configuration file `proxlb.yaml`: -| Section | Option | Example | Description | -|------|:------:|:------:|:------:| -| `proxmox` | api_host | hypervisor01.gyptazy.com | Host or IP address (or comma separated list) of the remote Proxmox API. | -| | api_user | root@pam | Username for the API. | -| | api_pass | FooBar | Password for the API. | -| | verify_ssl | 1 | Validate SSL certificates (1) or ignore (0). (default: 1, type: bool) | -| | timeout | 10 | Timeout for the Proxmox API in sec. (default: 10) | -| `vm_balancing` | enable | 1 | Enables VM/CT balancing. | -| | method | memory | Defines the balancing method (default: memory) where you can use `memory`, `disk` or `cpu`. | -| | mode | used | Rebalance by `used` resources (efficiency) or `assigned` (avoid overprovisioning) resources. (default: used)| -| | mode_option | byte | Rebalance by node's resources in `bytes` or `percent`. (default: bytes) | -| | type | vm | Rebalance only `vm` (virtual machines), `ct` (containers) or `all` (virtual machines & containers). (default: vm)| -| | balanciness | 10 | Value of the percentage of lowest and highest resource consumption on nodes may differ before rebalancing. (default: 10) | -| | parallel_migrations | 1 | Defines if migrations should be done parallely or sequentially. (default: 1, type: bool) | -| | maintenance_nodes | dummynode03,dummynode04 | Defines a comma separated list of nodes to set them into maintenance mode and move VMs/CTs to other nodes. | -| | ignore_nodes | dummynode01,dummynode02,test* | Defines a comma separated list of nodes to exclude. | -| | ignore_vms | testvm01,testvm02 | Defines a comma separated list of VMs to exclude. (`*` as suffix wildcard or tags are also supported) | -| `storage_balancing` | enable | 0 | Enables storage balancing. | -| | balanciness | 10 | Value of the percentage of lowest and highest storage consumption may differ before rebalancing. (default: 10) | -| | parallel_migrations | 1 | Defines if migrations should be done parallely or sequentially. (default: 1, type: bool) | -| `update_service` | enable | 0 | Enables the automated update service (rolling updates). (default: 0, type: bool) | -| `api` | enable | 0 | Enables the ProxLB API. | -| `service`| daemon | 1 | Run as a daemon (1) or one-shot (0). (default: 1, type: bool) | -| | schedule | 24 | Hours to rebalance in hours. (default: 24) | -| | master_only | 0 | Defines is this should only be performed (1) on the cluster master node or not (0). (default: 0, type: bool) | -| | log_verbosity | INFO | Defines the log level (default: CRITICAL) where you can use `DEBUG`, `INFO`, `WARNING` or `CRITICAL` | -| | config_version | 3 | Defines the current config version schema for ProxLB | +| Section | Option | Example | Type | Description | +|------|:------:|:------:|:------:|:------:| +| `proxmox_api` | | | | | +| | hosts | ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe'] | `List` | List of Proxmox nodes. Can be IPv4, IPv6 or mixed. | +| | user | root@pam | `Str` | Username for the API. | +| | pass | FooBar | `Str` | Password for the API. | +| | ssl_verification | True | `Bool` | Validate SSL certificates (1) or ignore (0). (default: 1, type: bool) | +| | timeout | 10 | `Int` | Timeout for the Proxmox API in sec. (default: 10) | +| `proxmox_cluster` | | | | | +| | maintenance_nodes | ['virt66.example.com'] | `List` | A list of Proxmox nodes that are defined to be in a maintenance. (default: []) | +| | ignore_nodes | [] | `List` | A list of Proxmox nodes that are defined to be ignored. (default: []) | +| | overprovisioning | False | `Bool` | Avoids balancing when nodes would become overprovisioned. | +| `balancing` | | | | | +| | enable | True | `Bool` | Enables the guest balancing. (default: True)| +| | force | True | `Bool` | Enforcing affinity/anti-affinity rules but balancing might become worse. (default: False) | +| | parallel | False | `Bool` | If guests should be moved in parallel or sequentially. (default: False)| +| | live | True | `Bool` | If guests should be moved live or shutdown. (default: True)| +| | with_local_disks | True | `Bool` | If balancing of guests should include local disks (default: True)| +| | balance_types | ['vm', 'ct'] | `List` | Defined the types of guests that should be honored. (default: ['vm', 'ct']) | +| | max_job_validation | 1800 | `Int` | How long a job validation may take in seconds. (default: 1800) | +| | balanciness | 10 | `Int` | The maximum delta of resource usage between node with highest and lowest usage. (default: 10) | +| | method | memory | `Str` | The balancing method that should be used. (default: memory | choices: memory, cpu, disk)| +| | mode | used | `Str` | The balancing mode that should be used. (default: used | choices: used, assigned)| +| `service` | | | | | +| | daemon | False | `Bool` | If daemon mode should be activated (default: False)| +| | schedule | 12 | `Int` | How often rebalancing should occur in hours in daemon mode (default: 12)| +| | log_level | INFO | `Str` | Defines the default log level that should be logged. (default: INFO) | An example of the configuration file looks like: ``` -[proxmox] -api_host: hypervisor01.gyptazy.com -api_user: root@pam -api_pass: FooBar -verify_ssl: 1 -timeout: 10 -[vm_balancing] -enable: 1 -method: memory -mode: used -type: vm -# Balanciness defines how much difference may be -# between the lowest & highest resource consumption -# of nodes before rebalancing will be done. -# Examples: -# Rebalancing: node01: 41% memory consumption :: node02: 52% consumption -# No rebalancing: node01: 43% memory consumption :: node02: 50% consumption -balanciness: 10 -# Enable parallel migrations. If set to 0 it will wait for completed migrations -# before starting next migration. -parallel_migrations: 1 -maintenance_nodes: dummynode03,dummynode04 -ignore_nodes: dummynode01,dummynode02 -ignore_vms: testvm01,testvm02 -[storage_balancing] -enable: 0 -[update_service] -enable: 0 -[api] -enable: 0 -[service] -# The master_only option might be useful if running ProxLB on all nodes in a cluster -# but only a single one should do the balancing. The master node is obtained from the Proxmox -# HA status. -master_only: 0 -daemon: 1 -config_version: 3 -``` +proxmox_api: + hosts: ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe'] + user: root@pam + pass: crazyPassw0rd! + ssl_verification: False + timeout: 10 -#### Notes -* If running ProxLB on more than one Proxmox node you can set `api_host` to a comma-separated list of each node's IP address or hostname. (Example: `api_host: node01.gyptazy.com,node02.gyptazy.com,node03.gyptazy.com`) -* The `verify_ssl` parameter can switch between the mode to verify trusted remote certificates. Keep in mind, that even local ones are **not** trusted by default and need to be imported to the truststore. -* Even when using only the `vm_balancing` mode, ensure to have the other sections listed in your config: -``` -[storage_balancing] -enable: 0 -[update_service] -enable: 0 -[api] -enable: 0 +proxmox_cluster: + maintenance_nodes: ['virt66.example.com'] + ignore_nodes: [] + overprovisioning: True + +balancing: + enable: True + force: False + parallel: False + live: True + with_local_disks: True + balance_types: ['vm', 'ct'] + max_job_validation: 1800 + balanciness: 5 + method: memory + mode: assigned + +service: + daemon: False + schedule: 12 + log_level: DEBUG ``` ### Parameters @@ -196,204 +202,70 @@ The following options and parameters are currently supported: | Option | Long Option | Description | Default | |------|:------:|------:|------:| | -c | --config | Path to a config file. | /etc/proxlb/proxlb.conf (default) | -| -d | --dry-run | Performs a dry-run without doing any actions. | Unset | -| -j | --json | Returns a JSON of the VM movement. | Unset | -| -b | --best-node | Returns the best next node for a VM/CT placement (useful for further usage with Terraform/Ansible). | Unset | -| -m | --maintenance | Sets node(s) to maintenance mode & moves workloads away. | Unset | -| -v | --version | Returns the ProxLB version on stdout. | Unset | +| -d | --dry-run | Performs a dry-run without doing any actions. | False | +| -j | --json | Returns a JSON of the VM movement. | False | +| -b | --best-node | Returns the best next node for a VM/CT placement (useful for further usage with Terraform/Ansible). | False | +| -v | --version | Returns the ProxLB version on stdout. | False | -### Balancing -#### General -In general, virtual machines (VMs), containers (CTs) can be rebalanced and moved around nodes or shared storage (storage balancing) in the cluster. Often, this also works without downtime without any further downtimes. However, this does **not** work with containers. LXC based containers will be shutdown, copied and started on the new node. Also to note, live migrations can work fluently without any issues but there are still several things to be considered. This is out of scope for ProxLB and applies in general to Proxmox and your cluster setup. You can find more details about this here: https://pve.proxmox.com/wiki/Migrate_to_Proxmox_VE. +## Affinity & Anti-Affinity Rules +ProxLB provides an advanced mechanism to define affinity and anti-affinity rules, enabling precise control over virtual machine (VM) placement. These rules help manage resource distribution, improve high availability configurations, and optimize performance within a Proxmox Virtual Environment (PVE) cluster. By leveraging Proxmox’s integrated access management, ProxLB ensures that users can only define and manage rules for guests they have permission to access. -#### By Used Memory of VMs/CTs -By continuously monitoring the current resource usage of VMs, ProxLB intelligently reallocates workloads to prevent any single node from becoming overloaded. This approach ensures that resources are balanced efficiently, providing consistent and optimal performance across the entire cluster at all times. To activate this balancing mode, simply activate the following option in your ProxLB configuration: +ProxLB implements affinity and anti-affinity rules through a tag-based system within the Proxmox web interface. Each guest (virtual machine or container) can be assigned specific tags, which then dictate its placement behavior. This method maintains a streamlined and secure approach to managing VM relationships while preserving Proxmox’s inherent permission model. + +### Affinity Rules + Affinity rules are used to group certain VMs together, ensuring that they run on the same host whenever possible. This can be beneficial for workloads requiring low-latency communication, such as clustered databases or application servers that frequently exchange data. + +To define an affinity rule which keeps all guests assigned to this tag together on a node, users assign a tag with the prefix `plb_affinity_$TAG`: + +#### Example for Screenshot ``` -mode: used +plb_affinity_talos ``` -Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode. +As a result, ProxLB will attempt to place all VMs with the `plb_affinity_web` tag on the same host (see also the attached screenshot with the same node). -#### By Assigned Memory of VMs/CTs -By ensuring that resources are always available for each VM, ProxLB prevents over-provisioning and maintains a balanced load across all nodes. This guarantees that users have consistent access to the resources they need. However, if the total assigned resources exceed the combined capacity of the cluster, ProxLB will issue a warning, indicating potential over-provisioning despite its best efforts to balance the load. To activate this balancing mode, simply activate the following option in your ProxLB configuration: +### Anti-Affinity Rules + Conversely, anti-affinity rules ensure that designated VMs do not run on the same physical host. This is particularly useful for high-availability setups, where redundancy is crucial. Ensuring that critical services are distributed across multiple hosts reduces the risk of a single point of failure. + +To define an anti-affinity rule that ensures to not move systems within this group to the same node, users assign a tag with the prefix: + +#### Example for Screenshot ``` -mode: assigned +plb_anti_affinity_ntp ``` -Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode. +As a result, ProxLB will try to place the VMs with the `plb_anti_affinity_ntp` tag on different hosts (see also the attached screenshot with the different nodes). -#### Storage Balancing -Starting with ProxLB 1.0.3, ProxLB also supports the balancing of underlying shared storage. In this case, all attached disks (`rootfs` in a context of a CT) of a VM or CT are being fetched and evaluated. If a VM has multiple disks attached, the disks can also be distributed over different storages. As a result, only shared storage is supported. Non shared storage would require to move the whole VM including all attached disks to the parent's node local storage. +**Note:** While this ensures that ProxLB tries distribute these VMs across different physical hosts within the Proxmox cluster this may not always work. If you have more guests attached to the group than nodes in the cluster, we still need to run them anywhere. If this case occurs, the next one with the most free resources will be selected. -Limitations: -* Only shared storage -* Only supported for the following VM disk types: - * ide (only disks, not CD) - * nvme - * scsi - * virtio - * sata - * rootfs (Container) +## Maintenance + -*Note: Storage balancing is currently in beta and should be used carefully.* - -### Affinity Rules / Grouping Relationships -#### Affinity (Stay Together) - Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_include_ followed by your unique identifier, for example, plb_include_group1. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be included in the group. - -#### Anti-Affinity (Keep Apart) - Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_exclude_ followed by your unique identifier, for example, plb_exclude_critical. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be excluded from being on the same node. - -#### Ignore VMs (Tag Style) - In Proxmox, you can ensure that certain VMs are ignored during the rebalancing process by setting a specific tag within the Proxmox Web UI, rather than solely relying on configurations in the ProxLB config file. This can be achieved by adding the tag 'plb_ignore_vm' to the VM. Once this tag is applied, the VM will be excluded from any further rebalancing operations, simplifying the management process. - -### Systemd -When installing a Linux distribution (such as .deb or .rpm) file, this will be shipped with a systemd unit file. The default configuration file will be sourced from `/etc/proxlb/proxlb.conf`. - -| Unit Name | Options | -|------|:------:| -| proxlb | start, stop, status, restart | - -### Manual -A manual installation is possible and also supports BSD based systems. Proxmox Rebalancing Service relies on mainly two important files: -* proxlb (Python Executable) -* proxlb.conf (Config file) - -The executable must be able to read the config file, if no dedicated config file is given by the `-c` argument, PLB tries to read it from `/etc/proxlb/proxlb.conf`. - -### Proxmox GUI Integration - PLB can also be directly be used from the Proxmox Web UI by installing the optional package `pve-proxmoxlb-service-ui` package which has a dependency on the `proxlb` package. For the Web UI integration, it requires to be installed (in addition) on the nodes on the cluster. Afterwards, a new menu item is present in the HA chapter called `Rebalancing`. This chapter provides two possibilities: -* Rebalancing VM workloads -* Migrate VM workloads away from a defined node (e.g. maintenance preparation) - -### Quick Start -The easiest way to get started is by using the ready-to-use packages that I provide on my CDN and to run it on a Linux Debian based system. This can also be one of the Proxmox nodes itself. - -``` -wget https://cdn.gyptazy.com/files/os/debian/proxlb/proxlb_1.0.6_amd64.deb -dpkg -i proxlb_1.0.6_amd64.deb -# Adjust your config -vi /etc/proxlb/proxlb.conf -systemctl restart proxlb -systemctl status proxlb -``` - -### Container Quick Start (Docker/Podman) -Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in a container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment. - -```bash -git clone https://github.com/gyptazy/ProxLB.git -cd ProxLB -docker build -t proxlb . -``` - -Afterwards simply adjust the config file to your needs: -``` -vi /etc/proxlb/proxlb.conf -``` - -Finally, start the created container. -```bash -docker run -it --rm -v $(pwd)/proxlb.conf:/etc/proxlb/proxlb.conf proxlb -``` - -### Logging -ProxLB uses the `SystemdHandler` for logging. You can find all your logs in your systemd unit log or in the `journalctl`. In default, ProxLB only logs critical events. However, for further understanding of the balancing it might be useful to change this to `INFO` or `DEBUG` which can simply be done in the [proxlb.conf](https://github.com/gyptazy/ProxLB/blob/main/proxlb.conf#L14) file by changing the `log_verbosity` parameter. - -Available logging values: -| Verbosity | Description | -|------|:------:| -| DEBUG | This option logs everything and is needed for debugging the code. | -| INFO | This option provides insides behind the scenes. What/why has been something done and with which values. | -| WARNING | This option provides only warning messages, which might be a problem in general but not for the application itself. | -| CRITICAL | This option logs all critical events that will avoid running ProxLB. | - -## Motivation -As a developer managing a cluster of virtual machines for my projects, I often encountered the challenge of resource imbalance. Nodes within the cluster would become unevenly loaded, with some nodes being overburdened while others remained underutilized. This imbalance led to inefficiencies, performance bottlenecks, and increased operational costs. Frustrated by the lack of an adequate solution to address this issue, I decided to develop the ProxLB (PLB) to ensure better resource distribution across my clusters. - -My primary motivation for creating PLB stemmed from my work on my BoxyBSD project, where I consistently faced the difficulty of maintaining balanced nodes while running various VM workloads but also on my personal clusters. The absence of an efficient rebalancing mechanism made it challenging to achieve optimal performance and stability. Recognizing the necessity for a tool that could gather and analyze resource metrics from both the cluster nodes and the running VMs, I embarked on developing ProxLB. - -PLB meticulously collects detailed resource usage data from each node in a Proxmox cluster, including CPU load, memory usage, and local disk space utilization. It also gathers comprehensive statistics from all running VMs, providing a granular understanding of the workload distribution. With this data, PLB intelligently redistributes VMs based on memory usage, local disk usage, and CPU usage. This ensures that no single node is overburdened, storage resources are evenly distributed, and the computational load is balanced, enhancing overall cluster performance. - -As an advocate of the open-source philosophy, I believe in the power of community and collaboration. By sharing solutions like PLB, I aim to contribute to the collective knowledge and tools available to developers facing similar challenges. Open source fosters innovation, transparency, and mutual support, enabling developers to build on each other's work and create better solutions together. - -Developing PLB was driven by a desire to solve a real problem I faced in my projects. However, the spirit behind this effort was to provide a valuable resource to the community. By open-sourcing PLB, I hope to help other developers manage their clusters more efficiently, optimize their resource usage, and reduce operational costs. Sharing this solution aligns with the core principles of open source, where the goal is not only to solve individual problems but also to contribute to the broader ecosystem. - -## References -Here you can find some overviews of references for and about the ProxLB (PLB): - -| Description | Link | -|------|:------:| -| General introduction into ProxLB | https://gyptazy.com/blog/proxlb-rebalancing-vm-workloads-across-nodes-in-proxmox-clusters/ | -| Howto install and use ProxLB on Debian to rebalance vm workloads in a Proxmox cluster | https://gyptazy.com/howtos/howto-install-and-use-proxlb-to-rebalance-vm-workloads-across-nodes-in-proxmox-clusters/ | - -## Downloads -ProxLB can be obtained in man different ways, depending on which use case you prefer. You can use simply copy the code from GitHub, use created packages for Debian or RedHat based systems, use a Repository to keep ProxLB always up to date or simply use a Container image for Docker/Podman. - -### Packages -Ready to use packages can be found at: -* https://cdn.gyptazy.com/files/os/debian/proxlb/ -* https://cdn.gyptazy.com/files/os/ubuntu/proxlb/ -* https://cdn.gyptazy.com/files/os/redhat/proxlb/ - -### Repository -Debian based systems can also use the repository by adding the following line to their apt sources: - -#### Stable Releases -``` -deb https://repo.gyptazy.com/stable / -``` - -#### Beta/Testing Releases -``` -deb https://repo.gyptazy.com/testing / -``` - -The Repository's GPG key can be found at: `https://repo.gyptazy.com/repository.gpg` - -You can also simply import it by running: - -``` -# KeyID: 17169F23F9F71A14AD49EDADDB51D3EB01824F4C -# UID: gyptazy Solutions Repository -# SHA256: 52c267e6f4ec799d40cdbdb29fa518533ac7942dab557fa4c217a76f90d6b0f3 repository.gpg - -wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gpg -``` - -*Note: The defined repositories `repo.gyptazy.com` and `repo.proxlb.de` are the same!* - -### Container Images (Docker/Podman) -Container Images for Podman, Docker etc., can be found at: -| Version | Image | -|------|:------:| -| latest | cr.gyptazy.com/proxlb/proxlb:latest | -| v1.0.6 | cr.gyptazy.com/proxlb/proxlb:v1.0.6 | -| v1.0.5 | cr.gyptazy.com/proxlb/proxlb:v1.0.5 | -| v1.0.4 | cr.gyptazy.com/proxlb/proxlb:v1.0.4 | -| v1.0.3 | cr.gyptazy.com/proxlb/proxlb:v1.0.3 | -| v1.0.2 | cr.gyptazy.com/proxlb/proxlb:v1.0.2 | -| v1.0.0 | cr.gyptazy.com/proxlb/proxlb:v1.0.0 | -| v0.9.9 | cr.gyptazy.com/proxlb/proxlb:v0.9.9 | +The `maintenance_nodes` option allows operators to designate one or more Proxmox nodes for maintenance mode. When a node is set to maintenance, no new guest workloads will be assigned to it, and all existing workloads will be migrated to other available nodes within the cluster. This process ensures that (anti)-affinity rules and resource availability are respected, preventing disruptions while maintaining optimal performance across the infrastructure. ## Misc ### Bugs Bugs can be reported via the GitHub issue tracker [here](https://github.com/gyptazy/ProxLB/issues). You may also report bugs via email or deliver PRs to fix them on your own. Therefore, you might also see the contributing chapter. ### Contributing -Feel free to add further documentation, to adjust already existing one or to contribute with code. Please take care about the style guide and naming conventions. You can find more in our [CONTRIBUTING.md](https://github.com/gyptazy/ProxLB/blob/main/CONTRIBUTING.md) file. +Feel free to add further documentation, to adjust already existing one or to contribute with code. Please take care about the style guide and naming conventions. You can find more in our [CONTRIBUTING.md](https://github.com/gyptazy/ProxLB/blob/development/CONTRIBUTING.md) file. ### Documentation -You can also find additional and more detailed documentation within the [docs/](https://github.com/gyptazy/ProxLB/tree/main/docs) directory. +You can also find additional and more detailed documentation within the [docs/](https://github.com/gyptazy/ProxLB/tree/development/docs) directory. ### Support -If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix and on Reddit. Join our community for real-time help, advice, and discussions. Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub. We are here to help and ensure you have the best experience possible. +If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix or [Discord](https://discord.gg/JemGu7WbfQ). Join our community for real-time help, advice, and discussions. The Matrix and Discord room are bridged to ensure that the communication is not splitted - so simply feel free to join which fits most to you! + +Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub. | Support Channel | Link | |------|:------:| | Matrix | [#proxlb:gyptazy.com](https://matrix.to/#/#proxlb:gyptazy.com) | +| Discord | [Discord](https://discord.gg/JemGu7WbfQ) | | GitHub Community | [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/) | GitHub | [ProxLB GitHub](https://github.com/gyptazy/ProxLB/issues) | +**Note:** Please always keep in mind that this is a one-man show project without any further help. This includes coding, testing, packaging and all the infrastructure around it to keep this project up and running. + ### Author(s) - * Florian Paul Azim Hoberg @gyptazy (https://gyptazy.com) + * Florian Paul Azim Hoberg @gyptazy (https://gyptazy.com) \ No newline at end of file diff --git a/config/proxlb_example.yaml b/config/proxlb_example.yaml new file mode 100644 index 0000000..1c63640 --- /dev/null +++ b/config/proxlb_example.yaml @@ -0,0 +1,28 @@ +proxmox_api: + hosts: ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe'] + user: root@pam + pass: crazyPassw0rd! + ssl_verification: False + timeout: 10 + +proxmox_cluster: + maintenance_nodes: ['virt66.example.com'] + ignore_nodes: [] + overprovisioning: True + +balancing: + enable: True + force: False + parallel: False + live: True + with_local_disks: True + balance_types: ['vm', 'ct'] + max_job_validation: 1800 + balanciness: 5 + method: memory + mode: assigned + +service: + daemon: False + schedule: 12 + log_level: DEBUG \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 5cb70e0..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,7 +0,0 @@ -services: - proxlb: - build: . - volumes: - - ./proxlb.conf:/etc/proxlb/proxlb.conf - restart: unless-stopped - container_name: proxlb diff --git a/docs/01_Installation.md b/docs/01_Installation.md deleted file mode 100644 index 19c6ddf..0000000 --- a/docs/01_Installation.md +++ /dev/null @@ -1,33 +0,0 @@ -# Installation - -## Packages -The easiest way to get started is by using the ready-to-use packages that I provide on my CDN and to run it on a Linux Debian based system. This can also be one of the Proxmox nodes itself. - -``` -wget https://cdn.gyptazy.ch/files/amd64/debian/proxlb/proxlb_0.9.9_amd64.deb -dpkg -i proxlb_0.9.9_amd64.deb -# Adjust your config -vi /etc/proxlb/proxlb.conf -# Enable and start the service -systemctl enable --now proxlb -systemctl status proxlb -``` - -## Container (Docker/Podman) -Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in a container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment. - -```bash -git clone https://github.com/gyptazy/ProxLB.git -cd ProxLB -build -t proxlb . -``` - -Afterwards simply adjust the config file to your needs: -``` -vi /etc/proxlb/proxlb.conf -``` - -Finally, start the created container. -```bash -docker run -it --rm -v $(pwd)/proxlb.conf:/etc/proxlb/proxlb.conf proxlb -``` \ No newline at end of file diff --git a/docs/02_Configuration.md b/docs/02_Configuration.md deleted file mode 100644 index 66645a9..0000000 --- a/docs/02_Configuration.md +++ /dev/null @@ -1,48 +0,0 @@ -# Configuration - -## Balancing -### By Used Memmory of VMs -By continuously monitoring the current resource usage of VMs, ProxLB intelligently reallocates workloads to prevent any single node from becoming overloaded. This approach ensures that resources are balanced efficiently, providing consistent and optimal performance across the entire cluster at all times. To activate this balancing mode, simply activate the following option in your ProxLB configuration: -``` -mode: used -``` -Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode. - -### By Assigned Memory of VMs -By ensuring that resources are always available for each VM, ProxLB prevents over-provisioning and maintains a balanced load across all nodes. This guarantees that users have consistent access to the resources they need. However, if the total assigned resources exceed the combined capacity of the cluster, ProxLB will issue a warning, indicating potential over-provisioning despite its best efforts to balance the load. To activate this balancing mode, simply activate the following option in your ProxLB configuration: -``` -mode: assigned -``` -Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode. - -## Grouping -### Include (Stay Together) - Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_include_ followed by your unique identifier, for example, plb_include_group1. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be included in the group. - -### Exclude (Stay Separate) - Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_exclude_ followed by your unique identifier, for example, plb_exclude_critical. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be excluded from being on the same node. - -### Ignore VMs (tag style) - In Proxmox, you can ensure that certain VMs are ignored during the rebalancing process by setting a specific tag within the Proxmox Web UI, rather than solely relying on configurations in the ProxLB config file. This can be achieved by adding the tag 'plb_ignore_vm' to the VM. Once this tag is applied, the VM will be excluded from any further rebalancing operations, simplifying the management process. - -## Authentication / User Account / User / Permissions -### Authentication -ProxLB also supports different accounts in ProxLB. Therefore, you can simply create a new user and group and add the required roles permissions. - -### Creating Dedicated User for Balanciung -It is recommended to not use the `root@pam` user for balancing. Therefore, creating a new user might be suitable and is very easy to create. -A new user can be created by the gui, api and cli. The required roles are stated in the next chapter, but you can also use the following lines -to create a user on the cli with the required roles to balance VMs & CTs. - -``` -pveum role add ProxLBAdmin --privs Datastore.Audit,Sys.Audit,VM.Audit,VM.Migrate -pveum user add proxlb_admin@pve --password -pveum acl modify / --roles ProxLBAdmin --users proxlb_admin@pve -``` - -### Required Roles -When using ProxLB with a dedicated account, you might also keep the assigned roles low. Therefore, you need to ensure that the newly created user is at least assigned to the following roles: -* Datastore.Audit (Required for storage evaluation) -* Sys.Audit (Required to get resource metrics of the nodes) -* VM.Audit (Requited to get resource metrics of VMs/CTs) -* VM.Migrate (Required for migration of VMs/CTs) \ No newline at end of file diff --git a/docs/03_FAQ.md b/docs/03_FAQ.md deleted file mode 100644 index 3257cc8..0000000 --- a/docs/03_FAQ.md +++ /dev/null @@ -1,87 +0,0 @@ -## FAQ - -### Could not import all dependencies -ProxLB requires the Python library `proxmoxer`. This can simply be installed by the most -system repositories. If you encounter this error message you simply need to install it. - - -``` -# systemctl status proxlb -x proxlb.service - Proxmox Rebalancing Service - Loaded: loaded (/etc/systemd/system/proxlb.service; static) - Active: failed (Result: exit-code) since Sat 2024-07-06 10:25:16 UTC; 1s ago - Duration: 239ms - Process: 7285 ExecStart=/usr/bin/proxlb -c /etc/proxlb/proxlb.conf (code=exited, status=2) - Main PID: 7285 (code=exited, status=2) - CPU: 129ms - -Jul 06 10:25:16 build01 systemd[1]: Started proxlb.service - ProxLB. -Jul 06 10:25:16 build01 proxlb[7285]: proxlb: Error: [python-imports]: Could not import all dependencies. Please install "proxmoxer". -``` - -Debian/Ubuntu: apt-get install python3-proxmoxer -If the package is not provided by your systems repository, you can also install it by running `pip3 install proxmoxer`. - -### How does it work? -ProxLB is a load-balancing system designed to optimize the distribution of virtual machines (VMs) and containers (CTs) across a cluster. It works by first gathering resource usage metrics from all nodes in the cluster through the Proxmox API. This includes detailed resource metrics for each VM and CT on every node. ProxLB then evaluates the difference between the maximum and minimum resource usage of the nodes, referred to as "Balanciness." If this difference exceeds a predefined threshold (which is configurable), the system initiates the rebalancing process. - -Before starting any migrations, ProxLB validates that rebalancing actions are necessary and beneficial. Depending on the selected balancing mode — such as CPU, memory, or disk — it creates a balancing matrix. This matrix sorts the VMs by their maximum used or assigned resources, identifying the VM with the highest usage. ProxLB then places this VM on the node with the most free resources in the selected balancing type. This process runs recursively until the operator-defined Balanciness is achieved. Balancing can be defined for the used or max. assigned resources of VMs/CTs. - -### ProxLB config version is too low -ProxLB may run into an error when the used config schema version is too low. This might happen after major changes that require new config options. Please make sure, to use a supported config version in addition to your running ProxLB config. - -Example Error: -``` -Error: [config-version-validator]: ProxLB config version 2 is too low. Required: 3. -``` - -The easiest way to solve this, is by taking the minimum required config schema version from a git tag, representing the ProxLB version. - -### Logging -ProxLB uses the `SystemdHandler` for logging. You can find all your logs in your systemd unit log or in the `journalctl`. In default, ProxLB only logs critical events. However, for further understanding of the balancing it might be useful to change this to `INFO` or `DEBUG` which can simply be done in the [proxlb.conf](https://github.com/gyptazy/ProxLB/blob/main/proxlb.conf#L14) file by changing the `log_verbosity` parameter. - -Available logging values: -| Verbosity | Description | -|------|:------:| -| DEBUG | This option logs everything and is needed for debugging the code. | -| INFO | This option provides insides behind the scenes. What/why has been something done and with which values. | -| WARNING | This option provides only warning messages, which might be a problem in general but not for the application itself. | -| CRITICAL | This option logs all critical events that will avoid running ProxLB. | - -### Motivation -As a developer managing a cluster of virtual machines for my projects, I often encountered the challenge of resource imbalance. Nodes within the cluster would become unevenly loaded, with some nodes being overburdened while others remained underutilized. This imbalance led to inefficiencies, performance bottlenecks, and increased operational costs. Frustrated by the lack of an adequate solution to address this issue, I decided to develop the ProxLB (PLB) to ensure better resource distribution across my clusters. - -My primary motivation for creating PLB stemmed from my work on my BoxyBSD project, where I consistently faced the difficulty of maintaining balanced nodes while running various VM workloads but also on my personal clusters. The absence of an efficient rebalancing mechanism made it challenging to achieve optimal performance and stability. Recognizing the necessity for a tool that could gather and analyze resource metrics from both the cluster nodes and the running VMs, I embarked on developing ProxLB. - -PLB meticulously collects detailed resource usage data from each node in a Proxmox cluster, including CPU load, memory usage, and local disk space utilization. It also gathers comprehensive statistics from all running VMs, providing a granular understanding of the workload distribution. With this data, PLB intelligently redistributes VMs based on memory usage, local disk usage, and CPU usage. This ensures that no single node is overburdened, storage resources are evenly distributed, and the computational load is balanced, enhancing overall cluster performance. - -As an advocate of the open-source philosophy, I believe in the power of community and collaboration. By sharing solutions like PLB, I aim to contribute to the collective knowledge and tools available to developers facing similar challenges. Open source fosters innovation, transparency, and mutual support, enabling developers to build on each other's work and create better solutions together. - -Developing PLB was driven by a desire to solve a real problem I faced in my projects. However, the spirit behind this effort was to provide a valuable resource to the community. By open-sourcing PLB, I hope to help other developers manage their clusters more efficiently, optimize their resource usage, and reduce operational costs. Sharing this solution aligns with the core principles of open source, where the goal is not only to solve individual problems but also to contribute to the broader ecosystem. - -### Packages / Container Images -Ready to use packages can be found at: -* https://cdn.gyptazy.ch/files/amd64/debian/proxlb/ -* https://cdn.gyptazy.ch/files/amd64/ubuntu/proxlb/ -* https://cdn.gyptazy.ch/files/amd64/redhat/proxlb/ -* https://cdn.gyptazy.ch/files/amd64/freebsd/proxlb/ - -Container Images for Podman, Docker etc., can be found at: -| Version | Image | -|------|:------:| -| latest | cr.gyptazy.ch/proxlb/proxlb:latest | - -### Bugs -Bugs can be reported via the GitHub issue tracker [here](https://github.com/gyptazy/ProxLB/issues). You may also report bugs via email or deliver PRs to fix them on your own. Therefore, you might also see the contributing chapter. - -### Contributing -Feel free to add further documentation, to adjust already existing one or to contribute with code. Please take care about the style guide and naming conventions. You can find more in our [CONTRIBUTING.md](https://github.com/gyptazy/ProxLB/blob/main/CONTRIBUTING.md) file. - -### Support -If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.ch) in Matrix and on Reddit. Join our community for real-time help, advice, and discussions. Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [Reddit community](https://www.reddit.com/r/Proxmox/comments/1e78ap3/introducing_proxlb_rebalance_your_vm_workloads/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub. We are here to help and ensure you have the best experience possible. - -| Support Channel | Link | -|------|:------:| -| Matrix | [#proxlb:gyptazy.ch](https://matrix.to/#/#proxlb:gyptazy.ch) | -| Reddit | [Reddit community](https://www.reddit.com/r/Proxmox/comments/1e78ap3/introducing_proxlb_rebalance_your_vm_workloads/) | -| GitHub | [ProxLB GitHub](https://github.com/gyptazy/ProxLB/issues) | diff --git a/misc/01-replace-version.sh b/misc/01-replace-version.sh new file mode 100644 index 0000000..3d79ded --- /dev/null +++ b/misc/01-replace-version.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +VERSION="1.1.0-alpha" + +sed -i "s/^__version__ = .*/__version__ = \"$VERSION\"/" "proxlb/utils/version.py" +sed -i "s/version=\"[0-9]*\.[0-9]*\.[0-9]*\"/version=\"$VERSION\"/" setup.py +echo "OK: Versions have been sucessfully set to $VERSION" \ No newline at end of file diff --git a/misc/02-create-changelog.sh b/misc/02-create-changelog.sh new file mode 100644 index 0000000..334ce40 --- /dev/null +++ b/misc/02-create-changelog.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +git clone https://github.com/gyptazy/changelog-fragments-creator.git +./changelog-fragments-creator/changelog-creator -f .changelogs/ -o CHANGELOG.md +echo "Created changelog file" \ No newline at end of file diff --git a/packaging/01_package.sh b/packaging/01_package.sh deleted file mode 100755 index 67a088e..0000000 --- a/packaging/01_package.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -sudo apt-get install rpm cmake git make python3-yaml - -git clone https://github.com/gyptazy/changelog-fragments-creator.git -./changelog-fragments-creator/changelog-creator -f ../.changelogs/ -o ../CHANGELOG.md -mkdir packages -mkdir build -cd build -cmake .. -cpack -G DEB . -cpack -G RPM . -cp *.deb ../packages -cp *.rpm ../packages -cd .. -rm -rf build -echo "Packages created. Packages can be found in directory: packages" diff --git a/packaging/02_changelog_only.sh b/packaging/02_changelog_only.sh deleted file mode 100755 index 336b634..0000000 --- a/packaging/02_changelog_only.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -git clone https://github.com/gyptazy/changelog-fragments-creator.git -./changelog-fragments-creator/changelog-creator -f ../.changelogs/ -o ../CHANGELOG.md -echo "Created changelog file" diff --git a/packaging/CMakeLists.txt b/packaging/CMakeLists.txt deleted file mode 100644 index 5c9b553..0000000 --- a/packaging/CMakeLists.txt +++ /dev/null @@ -1,40 +0,0 @@ -cmake_minimum_required(VERSION 3.16) -project(proxmox-rebalancing-service VERSION 1.0.6) - -install(PROGRAMS ../proxlb DESTINATION /bin) -install(FILES ../proxlb.conf DESTINATION /etc/proxlb) -install(FILES proxlb.service DESTINATION /etc/systemd/system) - -# General -set(CPACK_PACKAGE_NAME "proxlb") -set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/../LICENSE") -set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/../README.md") -set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Florian Paul Azim Hoberg ") -set(CPACK_PACKAGE_CONTACT "Florian Paul Azim Hoberg ") -set(CPACK_PACKAGE_VENDOR "gyptazy") - -# RPM packaging -set(CPACK_PACKAGE_VERSION ${CMAKE_PROJECT_VERSION}) -set(CPACK_GENERATOR "RPM") -set(CPACK_RPM_PACKAGE_ARCHITECTURE "amd64") -set(CPACK_RPM_PACKAGE_SUMMARY "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.") -set(CPACK_RPM_PACKAGE_DESCRIPTION "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.") -set(CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/changelog_redhat") -set(CPACK_PACKAGE_RELEASE 1) -set(CPACK_RPM_PACKAGE_LICENSE "GPL 3.0") -set(CPACK_RPM_PACKAGE_REQUIRES "python >= 3.2.0") - -# DEB packaging -set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) -set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64") -set(CPACK_DEBIAN_PACKAGE_SUMMARY "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.") -set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.") -set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/changelog_debian") -set(CPACK_DEBIAN_PACKAGE_DEPENDS "python3, python3-proxmoxer") -set(CPACK_DEBIAN_PACKAGE_LICENSE "GPL 3.0") - -# Install -set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) -set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinst;${CMAKE_CURRENT_SOURCE_DIR}/conffiles") -set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinst") -include(CPack) diff --git a/packaging/README.md b/packaging/README.md deleted file mode 100644 index c2cb0b5..0000000 --- a/packaging/README.md +++ /dev/null @@ -1,14 +0,0 @@ -## Build packages -Building the packages requires cmake, deb and rpm. -For building packages, simly run the following commands: - -``` -mkdir build -cd build -cmake .. -cpack -G RPM . -cpack -G DEB . -``` - -When running on Debian/Ubuntu you can directly call `01_package.sh` -to create your own packages. diff --git a/packaging/changelog_debian b/packaging/changelog_debian deleted file mode 100644 index e02c9af..0000000 --- a/packaging/changelog_debian +++ /dev/null @@ -1,63 +0,0 @@ -proxlb (1.0.5) unstable; urgency=low - - * Fix migration from local disks. - * Fix allowed values (add DEBUG, WARNING) for log verbosity. - * Fix node (and its objects) evaluation when not reachable (e.g., maintenance). - * Fix evaluation of maintenance mode where comparing list & string resulted in a crash. - * Change docs to make bool usage in configs more clear. - - -- Florian Paul Azim Hoberg Wed, 30 Oct 2024 17:02:31 +0100 - -proxlb (1.0.4) unstable; urgency=low - - * Add feature to make API timeout configureable. - * Add maintenance mode to evacuate a node and move workloads for other nodes in the cluster. - * Add version output cli arg. - * Run storage balancing only on supported shared storages. - * Run storage balancing only when needed to save time. - * Fix CPU balancing where calculations are done in float instead of int. (by @glitchvern) - * Fix documentation for the underlying infrastructure. - - -- Florian Paul Azim Hoberg Fri, 11 Oct 2024 06:14:13 +0200 - -proxlb (1.0.3) unstable; urgency=low - - * Add a convert function to cast all bool alike options from configparser to bools. - * Add a config parser options for future features. - * Add a config versio schema that must be supported by ProxLB. - * Add feature to allow the API hosts being provided as a comma separated list. - * Add storage balancing function. - * Add doc how to add dedicated user for authentication. (by @Dulux-Oz) - * Add cli arg `-b` to return the next best node for next VM/CT placement.Fix some wonkey code styles. - * Provide a more reasonable output when HA services are not active in a Proxmox cluster. - * Improve the underlying code base for future implementations. - * Fix documentation for the master_only parameter placed in the wrong config section. - * Fixed `master_only` function by inverting the condition. - * Improved the overall validation and error handling. - * Fix bug in the `proxlb.conf` in the vm_balancing section. - * Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error. - * Fix anti-affinity rules not evaluating a new and different node. - - -- Florian Paul Azim Hoberg Wed, 11 Sep 2024 17:31:03 +0200 - -proxlb (1.0.2) unstable; urgency=low - - * Add option to run migration in parallel or sequentially. - * Add option to run ProxLB only on a Proxmox cluster master (req. HA feature). - * Fix daemon timer to use hours instead of minutes. - * Fix CMake packaging for Debian package to avoid overwriting the config file. - * Fix some wonkey code styles. - - -- Florian Paul Azim Hoberg Tue, 13 Aug 2024 17:28:14 +0200 - -proxlb (1.0.0) unstable; urgency=low - - * Initial release of ProxLB. - - -- Florian Paul Azim Hoberg Thu, 01 Aug 2024 17:04:12 +0200 - -proxlb (0.9.0) unstable; urgency=low - - * Initial development release of ProxLB as a tech preview. - - -- Florian Paul Azim Hoberg Sun, 07 Jul 2024 05:38:41 +0200 diff --git a/packaging/changelog_redhat b/packaging/changelog_redhat deleted file mode 100644 index b93b801..0000000 --- a/packaging/changelog_redhat +++ /dev/null @@ -1,44 +0,0 @@ -* Wed Oct 30 2024 Florian Paul Azim Hoberg -- Fix migration from local disks. -- Fix allowed values (add DEBUG, WARNING) for log verbosity. -- Fix node (and its objects) evaluation when not reachable (e.g., maintenance). -- Fix evaluation of maintenance mode where comparing list & string resulted in a crash. -- Change docs to make bool usage in configs more clear. - -* Fri Oct 11 2024 Florian Paul Azim Hoberg -- Add feature to make API timeout configureable. -- Add maintenance mode to evacuate a node and move workloads for other nodes in the cluster. -- Add version output cli arg. -- Run storage balancing only on supported shared storages. -- Run storage balancing only when needed to save time. -- Fix CPU balancing where calculations are done in float instead of int. (by @glitchvern) -- Fix documentation for the underlying infrastructure. - -* Wed Sep 12 2024 Florian Paul Azim Hoberg -- Add a convert function to cast all bool alike options from configparser to bools. -- Add a config parser options for future features. -- Add a config versio schema that must be supported by ProxLB. -- Add feature to allow the API hosts being provided as a comma separated list. -- Add storage balancing function. -- Add doc how to add dedicated user for authentication. (by @Dulux-Oz) -- Add cli arg `-b` to return the next best node for next VM/CT placement.Fix some wonkey code styles. -- Provide a more reasonable output when HA services are not active in a Proxmox cluster. -- Improve the underlying code base for future implementations. -- Fix documentation for the master_only parameter placed in the wrong config section. -- Fixed `master_only` function by inverting the condition. -- Improved the overall validation and error handling. -- Fix bug in the `proxlb.conf` in the vm_balancing section. -- Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error. -- Fix anti-affinity rules not evaluating a new and different node. - -* Tue Aug 13 2024 Florian Paul Azim Hoberg -- Add option to run migration in parallel or sequentially. -- Add option to run ProxLB only on a Proxmox cluster master (req. HA feature). -- Fixed daemon timer to use hours instead of minutes. -- Fixed some wonkey code styles. - -* Thu Aug 01 2024 Florian Paul Azim Hoberg -- Initial release of ProxLB. - -* Sun Jul 07 2024 Florian Paul Azim Hoberg -- Initial development release of ProxLB as a tech preview. diff --git a/packaging/conffiles b/packaging/conffiles deleted file mode 100644 index 800e6dc..0000000 --- a/packaging/conffiles +++ /dev/null @@ -1 +0,0 @@ -/etc/proxlb/proxlb.conf diff --git a/packaging/postinst b/packaging/postinst deleted file mode 100644 index e270d02..0000000 --- a/packaging/postinst +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -useradd -m plb -chown plb:plb /etc/proxlb/proxlb.conf -chmod 600 /etc/proxlb/proxlb.conf -systemctl daemon-reload diff --git a/packaging/proxlb.service b/packaging/proxlb.service deleted file mode 100644 index 4e98581..0000000 --- a/packaging/proxlb.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=ProxLB - Rebalance VM workloads -After=network-online.target -Wants=network-online.target - -[Service] -ExecStart=/usr/bin/proxlb -c /etc/proxlb/proxlb.conf -User=plb - -[Install] -WantedBy=multi-user.target diff --git a/proxlb b/proxlb deleted file mode 100755 index c888161..0000000 --- a/proxlb +++ /dev/null @@ -1,1579 +0,0 @@ -#!/usr/bin/env python3 - -# ProxLB -# ProxLB (re)balances VM workloads across nodes in Proxmox clusters. -# ProxLB obtains current metrics from all nodes within the cluster for -# further auto balancing by memory, disk or cpu and rebalances the VMs -# over all available nodes in a cluster by having an equal resource usage. -# Copyright (C) 2024 Florian Paul Azim Hoberg @gyptazy -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import argparse -import configparser -import copy -import json -import logging -import os -try: - import proxmoxer - _imports = True -except ImportError: - _imports = False -import random -import re -import requests -import socket -import sys -import time -import urllib3 - - -# Constants -__appname__ = "ProxLB" -__version__ = "1.0.5" -__config_version__ = 3 -__author__ = "Florian Paul Azim Hoberg @gyptazy" -__errors__ = False - - -# Classes -## Logging class -class SystemdHandler(logging.Handler): - """ Class to handle logging options. """ - PREFIX = { - logging.CRITICAL: "<2> " + __appname__ + ": ", - logging.ERROR: "<3> " + __appname__ + ": ", - logging.WARNING: "<4> " + __appname__ + ": ", - logging.INFO: "<6> " + __appname__ + ": ", - logging.DEBUG: "<7> " + __appname__ + ": ", - logging.NOTSET: "<7 " + __appname__ + ": ", - } - - def __init__(self, stream=sys.stdout): - self.stream = stream - logging.Handler.__init__(self) - - def emit(self, record): - try: - msg = self.PREFIX[record.levelno] + self.format(record) + "\n" - self.stream.write(msg) - self.stream.flush() - except Exception: - self.handleError(record) - - -# Functions -def initialize_logger(log_level, update_log_verbosity=False): - """ Initialize ProxLB logging handler. """ - info_prefix = 'Info: [logger]:' - - root_logger = logging.getLogger() - root_logger.setLevel(log_level) - - if not update_log_verbosity: - root_logger.addHandler(SystemdHandler()) - logging.info(f'{info_prefix} Logger got initialized.') - else: - logging.info(f'{info_prefix} Logger verbosity got updated to: {log_level}.') - - -def pre_validations(config_path, proxlb_config=False): - """ Run pre-validations as sanity checks. """ - info_prefix = 'Info: [pre-validations]:' - - if proxlb_config: - logging.info(f'{info_prefix} Validating ProxLB config file content.') - __validate_config_content(proxlb_config) - logging.info(f'{info_prefix} ProxLB config file content validation done.') - else: - logging.info(f'{info_prefix} Validating basic configuration.') - __validate_imports() - __validate_config_file(config_path) - logging.info(f'{info_prefix} All pre-validations done.') - - -def post_validations(): - """ Run post-validations as sanity checks. """ - error_prefix = 'Error: [post-validations]:' - info_prefix = 'Info: [post-validations]:' - - if __errors__: - logging.critical(f'{error_prefix} Not all post-validations succeeded. Please validate!') - else: - logging.info(f'{info_prefix} All post-validations succeeded.') - - -def validate_daemon(daemon, schedule): - """ Validate if ProxLB runs as a daemon. """ - info_prefix = 'Info: [daemon]:' - - if bool(int(daemon)): - logging.info(f'{info_prefix} Running in daemon mode. Next run in {schedule} hours.') - time.sleep(int(schedule) * 60 * 60) - else: - logging.info(f'{info_prefix} Not running in daemon mode. Quitting.') - sys.exit(0) - - -def __validate_imports(): - """ Validate if all Python imports succeeded. """ - error_prefix = 'Error: [python-imports]:' - info_prefix = 'Info: [python-imports]:' - - if not _imports: - logging.critical(f'{error_prefix} Could not import all dependencies. Please install "proxmoxer".') - sys.exit(2) - else: - logging.info(f'{info_prefix} All required dependencies were imported.') - - -def __validate_config_file(config_path): - """ Validate if all Python imports succeeded. """ - error_prefix = 'Error: [config]:' - info_prefix = 'Info: [config]:' - - if not os.path.isfile(config_path): - logging.critical(f'{error_prefix} Could not find config file in: {config_path}.') - sys.exit(2) - else: - logging.info(f'{info_prefix} Configuration file loaded from: {config_path}.') - - -def __validate_config_content(proxlb_config): - """ Validate the user's config options. """ - error_prefix = 'Error: [config]:' - info_prefix = 'Info: [config]:' - - validate_bool_options = [ - 'proxmox_api_ssl_v', - 'vm_balancing_enable', - 'vm_parallel_migrations', - 'storage_balancing_enable', - 'storage_parallel_migrations', - 'update_service', - 'api', - 'master_only', - 'daemon' - ] - - for bool_val in validate_bool_options: - if type(proxlb_config.get(bool_val, None)) == bool: - logging.info(f'{info_prefix} Config option {bool_val} is in a correct format.') - else: - logging.critical(f'{error_prefix} Config option {bool_val} is incorrect: {proxlb_config.get(bool_val, None)}') - sys.exit(2) - - validate_string_options = [ - 'vm_balancing_method', - 'vm_balancing_mode', - 'vm_balancing_mode_option', - 'vm_balancing_type', - 'storage_balancing_method', - 'log_verbosity' - ] - - whitelist_string_options = { - 'vm_balancing_method': ['memory', 'disk', 'cpu'], - 'vm_balancing_mode': ['used', 'assigned'], - 'vm_balancing_mode_option': ['bytes', 'percent'], - 'vm_balancing_type': ['vm', 'ct', 'all'], - 'storage_balancing_method': ['disk_space'], - 'log_verbosity': ['DEBUG', 'INFO', 'WARNING', 'CRITICAL'] - } - - for string_val in validate_string_options: - if proxlb_config[string_val] in whitelist_string_options[string_val]: - logging.info(f'{info_prefix} Config option {string_val} is in a correct format.') - else: - logging.critical(f'{error_prefix} Config option {string_val} is incorrect: {proxlb_config.get(string_val, None)}') - sys.exit(2) - - -def initialize_args(): - """ Initialize given arguments for ProxLB. """ - argparser = argparse.ArgumentParser(description='ProxLB') - argparser.add_argument('-c', '--config', help='Path to config file', type=str, required=False) - argparser.add_argument('-d', '--dry-run', help='Perform a dry-run without doing any actions.', action='store_true', required=False) - argparser.add_argument('-j', '--json', help='Return a JSON of the VM movement.', action='store_true', required=False) - argparser.add_argument('-b', '--best-node', help='Returns the best next node.', action='store_true', required=False) - argparser.add_argument('-m', '--maintenance', help='Sets node to maintenance mode & moves workloads away.', type=str, required=False) - argparser.add_argument('-v', '--version', help='Returns the current ProxLB version.', action='store_true', required=False) - return argparser.parse_args() - - -def proxlb_output_version(): - """ Print ProxLB version information on CLI. """ - print(f'{__appname__} version {__version__}\nRequired config version: >= {__config_version__}') - print('ProxLB support: https://github.com/gyptazy/ProxLB\nDeveloper: gyptazy.com') - sys.exit(0) - - -def initialize_config_path(app_args): - """ Initialize path to ProxLB config file. """ - info_prefix = 'Info: [config]:' - - config_path = app_args.config - if app_args.config is None: - config_path = '/etc/proxlb/proxlb.conf' - logging.info(f'{info_prefix} No config file provided. Falling back to: {config_path}.') - else: - logging.info(f'{info_prefix} Using config file: {config_path}.') - return config_path - - -def initialize_config_options(config_path): - """ Read configuration from given config file for ProxLB. """ - error_prefix = 'Error: [config]:' - info_prefix = 'Info: [config]:' - proxlb_config = {} - - try: - config = configparser.ConfigParser() - config.read(config_path) - # Proxmox config - proxlb_config['proxmox_api_host'] = config['proxmox']['api_host'] - proxlb_config['proxmox_api_user'] = config['proxmox']['api_user'] - proxlb_config['proxmox_api_pass'] = config['proxmox']['api_pass'] - proxlb_config['proxmox_api_ssl_v'] = config['proxmox']['verify_ssl'] - proxlb_config['proxmox_api_timeout'] = config['proxmox'].get('timeout', 10) - # VM Balancing - proxlb_config['vm_balancing_enable'] = config['vm_balancing'].get('enable', 1) - proxlb_config['vm_balancing_method'] = config['vm_balancing'].get('method', 'memory') - proxlb_config['vm_balancing_mode'] = config['vm_balancing'].get('mode', 'used') - proxlb_config['vm_balancing_mode_option'] = config['vm_balancing'].get('mode_option', 'bytes') - proxlb_config['vm_balancing_type'] = config['vm_balancing'].get('type', 'vm') - proxlb_config['vm_balanciness'] = config['vm_balancing'].get('balanciness', 10) - proxlb_config['vm_parallel_migrations'] = config['vm_balancing'].get('parallel_migrations', 1) - proxlb_config['vm_maintenance_nodes'] = config['vm_balancing'].get('maintenance_nodes', '') - proxlb_config['vm_ignore_nodes'] = config['vm_balancing'].get('ignore_nodes', '') - proxlb_config['vm_ignore_vms'] = config['vm_balancing'].get('ignore_vms', '') - proxlb_config['vm_enforce_affinity_groups'] = config['vm_balancing'].get('enforce_affinity_groups', 1) - # Storage Balancing - proxlb_config['storage_balancing_enable'] = config['storage_balancing'].get('enable', 0) - proxlb_config['storage_balancing_method'] = config['storage_balancing'].get('method', 'disk_space') - proxlb_config['storage_balanciness'] = config['storage_balancing'].get('balanciness', 10) - proxlb_config['storage_parallel_migrations'] = config['storage_balancing'].get('parallel_migrations', 1) - # Update Support - proxlb_config['update_service'] = config['update_service'].get('enable', 0) - # API - proxlb_config['api'] = config['update_service'].get('enable', 0) - # Service - proxlb_config['master_only'] = config['service'].get('master_only', 0) - proxlb_config['daemon'] = config['service'].get('daemon', 1) - proxlb_config['schedule'] = config['service'].get('schedule', 24) - proxlb_config['log_verbosity'] = config['service'].get('log_verbosity', 'CRITICAL') - proxlb_config['config_version'] = config['service'].get('config_version', 2) - except configparser.NoSectionError: - logging.critical(f'{error_prefix} Could not find the required section.') - sys.exit(2) - except configparser.ParsingError: - logging.critical(f'{error_prefix} Unable to parse the config file.') - sys.exit(2) - except KeyError: - logging.critical(f'{error_prefix} Could not find the required options in config file.') - sys.exit(2) - - # Normalize and update bools. Afterwards, validate minimum required config version. - proxlb_config = __update_config_parser_bools(proxlb_config) - validate_config_minimum_version(proxlb_config) - logging.info(f'{info_prefix} Configuration file loaded.') - - return proxlb_config - - -def __update_config_parser_bools(proxlb_config): - """ Update bools in config from configparser to real bools """ - info_prefix = 'Info: [config-bool-converter]:' - ignore_sections = ['schedule'] - - # Normalize and update config parser values to bools. - for section, option_value in proxlb_config.items(): - - if option_value in [1, '1', 'yes', 'Yes', 'true', 'True', 'enable']: - if section not in ignore_sections: - logging.info(f'{info_prefix} Converting {section} to bool: True.') - proxlb_config[section] = True - - if option_value in [0, '0', 'no', 'No', 'false', 'False', 'disable']: - if section not in ignore_sections: - logging.info(f'{info_prefix} Converting {section} to bool: False.') - proxlb_config[section] = False - - return proxlb_config - - -def validate_config_minimum_version(proxlb_config): - """ Validate the minimum required config file for ProxLB """ - info_prefix = 'Info: [config-version-validator]:' - error_prefix = 'Error: [config-version-validator]:' - - if int(proxlb_config['config_version']) < __config_version__: - logging.error(f'{error_prefix} ProxLB config version {proxlb_config["config_version"]} is too low. Required: {__config_version__}.') - print(f'{error_prefix} ProxLB config version {proxlb_config["config_version"]} is too low. Required: {__config_version__}.') - sys.exit(1) - else: - logging.info(f'{info_prefix} ProxLB config version {proxlb_config["config_version"]} is fine. Required: {__config_version__}.') - - -def api_connect(proxmox_api_host, proxmox_api_user, proxmox_api_pass, proxmox_api_ssl_v, proxmox_api_timeout): - """ Connect and authenticate to the Proxmox remote API. """ - error_prefix = 'Error: [api-connection]:' - warn_prefix = 'Warning: [api-connection]:' - info_prefix = 'Info: [api-connection]:' - proxmox_api_ssl_v = bool(int(proxmox_api_ssl_v)) - - if not proxmox_api_ssl_v: - requests.packages.urllib3.disable_warnings() - logging.warning(f'{warn_prefix} API connection does not verify SSL certificate.') - - proxmox_api_host = __api_connect_get_host(proxmox_api_host) - - try: - api_object = proxmoxer.ProxmoxAPI(proxmox_api_host, user=proxmox_api_user, password=proxmox_api_pass, verify_ssl=proxmox_api_ssl_v, timeout=int(proxmox_api_timeout)) - except proxmoxer.backends.https.AuthenticationError as proxmox_api_error: - logging.critical(f'{error_prefix} Provided credentials do not work: {proxmox_api_error}') - sys.exit(2) - except urllib3.exceptions.NameResolutionError: - logging.critical(f'{error_prefix} Could not resolve the given host: {proxmox_api_host}.') - sys.exit(2) - except requests.exceptions.ConnectTimeout: - logging.critical(f'{error_prefix} Connection time out to host: {proxmox_api_host}.') - sys.exit(2) - except requests.exceptions.SSLError: - logging.critical(f'{error_prefix} SSL certificate verification failed for host: {proxmox_api_host}.') - sys.exit(2) - - logging.info(f'{info_prefix} API connection succeeded to host: {proxmox_api_host}.') - return api_object - - -def __api_connect_get_host(proxmox_api_host): - """ Validate if a list of API hosts got provided and pre-validate the hosts. """ - info_prefix = 'Info: [api-connect-get-host]:' - proxmox_port = 8006 - - if ',' in proxmox_api_host: - logging.info(f'{info_prefix} Multiple hosts for API connection are given. Testing hosts for further usage.') - proxmox_api_host = proxmox_api_host.split(',') - - # Validate all given hosts and check for responsive on Proxmox web port. - for host in proxmox_api_host: - logging.info(f'{info_prefix} Testing host {host} on port tcp/{proxmox_port}.') - reachable = __api_connect_test_ipv4_host(host, proxmox_port) - if reachable: - return host - else: - logging.info(f'{info_prefix} Using host {proxmox_api_host} on port tcp/{proxmox_port}.') - return proxmox_api_host - - -def __api_connect_test_ipv4_host(proxmox_api_host, port): - """ Validate if a given host on the IPv4 management address is reachable. """ - error_prefix = 'Error: [api-connect-test-host]:' - info_prefix = 'Info: [api-connect-test-host]:' - proxmox_connection_timeout = 2 - - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(proxmox_connection_timeout) - logging.info(f'{info_prefix} Timeout for host {proxmox_api_host} is set to {proxmox_connection_timeout} seconds.') - result = sock.connect_ex((proxmox_api_host,port)) - - if result == 0: - sock.close() - logging.info(f'{info_prefix} Host {proxmox_api_host} is reachable on port tcp/{port}.') - return True - else: - sock.close() - logging.critical(f'{error_prefix} Host {proxmox_api_host} is unreachable on port tcp/{port}.') - return False - - -def __api_connect_test_ipv6_host(proxmox_api_host, port): - """ Validate if a given host on the IPv6 management address is reachable. """ - error_prefix = 'Error: [api-connect-test-host]:' - info_prefix = 'Info: [api-connect-test-host]:' - proxmox_connection_timeout = 2 - - sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - sock.settimeout(proxmox_connection_timeout) - logging.info(f'{info_prefix} Timeout for host {proxmox_api_host} is set to {proxmox_connection_timeout}.') - result = sock.connect_ex((proxmox_api_host,port)) - - if result == 0: - sock.close() - logging.info(f'{info_prefix} Host {proxmox_api_host} is reachable on port tcp/{port}.') - return True - else: - sock.close() - logging.critical(f'{error_prefix} Host {proxmox_api_host} is unreachable on port tcp/{port}.') - return False - - -def execute_rebalancing_only_by_master(api_object, master_only): - """ Validate if balancing should only be done by the cluster master. Afterwards, validate if this node is the cluster master. """ - info_prefix = 'Info: [only-on-master-executor]:' - master_only = bool(int(master_only)) - - if bool(int(master_only)): - logging.info(f'{info_prefix} Master only rebalancing is defined. Starting validation.') - cluster_master_node = get_cluster_master(api_object) - cluster_master = validate_cluster_master(cluster_master_node) - return cluster_master, master_only - else: - logging.info(f'{info_prefix} No master only rebalancing is defined. Skipping validation.') - return False, master_only - - -def get_cluster_master(api_object): - """ Get the current master of the Proxmox cluster. """ - error_prefix = 'Error: [cluster-master-getter]:' - info_prefix = 'Info: [cluster-master-getter]:' - - try: - ha_status_object = api_object.cluster().ha().status().manager_status().get() - logging.info(f'{info_prefix} Master node: {ha_status_object.get("manager_status", None).get("master_node", None)}') - except urllib3.exceptions.NameResolutionError: - logging.critical(f'{error_prefix} Could not resolve the API.') - sys.exit(2) - except requests.exceptions.ConnectTimeout: - logging.critical(f'{error_prefix} Connection time out to API.') - sys.exit(2) - except requests.exceptions.SSLError: - logging.critical(f'{error_prefix} SSL certificate verification failed for API.') - sys.exit(2) - - cluster_master = ha_status_object.get("manager_status", None).get("master_node", None) - - if cluster_master: - return cluster_master - else: - logging.critical(f'{error_prefix} Could not obtain cluster master. Please check your configuration and ensure HA services in Proxmox are enabled. Stopping.') - sys.exit(2) - - -def validate_cluster_master(cluster_master): - """ Validate if the current execution node is the cluster master. """ - info_prefix = 'Info: [cluster-master-validator]:' - - node_executor_hostname = socket.gethostname() - logging.info(f'{info_prefix} Node executor hostname is: {node_executor_hostname}') - - if node_executor_hostname != cluster_master: - logging.info(f'{info_prefix} {node_executor_hostname} is not the cluster master ({cluster_master}).') - return False - else: - return True - - -def get_node_statistics(api_object, ignore_nodes, maintenance_nodes): - """ Get statistics of cpu, memory and disk for each node in the cluster. """ - info_prefix = 'Info: [node-statistics]:' - node_statistics = {} - ignore_nodes_list = ignore_nodes.split(',') - maintenance_nodes_list = maintenance_nodes.split(',') - - for node in api_object.nodes.get(): - if node['status'] == 'online': - node_statistics[node['node']] = {} - node_statistics[node['node']]['maintenance'] = False - node_statistics[node['node']]['ignore'] = False - node_statistics[node['node']]['cpu_total'] = node['maxcpu'] - node_statistics[node['node']]['cpu_assigned'] = 0 - node_statistics[node['node']]['cpu_assigned_percent'] = int((node_statistics[node['node']]['cpu_assigned']) / int(node_statistics[node['node']]['cpu_total']) * 100) - node_statistics[node['node']]['cpu_assigned_percent_last_run'] = 0 - node_statistics[node['node']]['cpu_used'] = node['cpu'] - node_statistics[node['node']]['cpu_free'] = (node['maxcpu']) - (node['cpu'] * node['maxcpu']) - node_statistics[node['node']]['cpu_free_percent'] = int((node_statistics[node['node']]['cpu_free']) / int(node['maxcpu']) * 100) - node_statistics[node['node']]['cpu_free_percent_last_run'] = 0 - node_statistics[node['node']]['memory_total'] = node['maxmem'] - node_statistics[node['node']]['memory_assigned'] = 0 - node_statistics[node['node']]['memory_assigned_percent'] = int((node_statistics[node['node']]['memory_assigned']) / int(node_statistics[node['node']]['memory_total']) * 100) - node_statistics[node['node']]['memory_assigned_percent_last_run'] = 0 - node_statistics[node['node']]['memory_used'] = node['mem'] - node_statistics[node['node']]['memory_free'] = int(node['maxmem']) - int(node['mem']) - node_statistics[node['node']]['memory_free_percent'] = int((node_statistics[node['node']]['memory_free']) / int(node['maxmem']) * 100) - node_statistics[node['node']]['memory_free_percent_last_run'] = 0 - node_statistics[node['node']]['disk_total'] = node['maxdisk'] - node_statistics[node['node']]['disk_assigned'] = 0 - node_statistics[node['node']]['disk_assigned_percent'] = int((node_statistics[node['node']]['disk_assigned']) / int(node_statistics[node['node']]['disk_total']) * 100) - node_statistics[node['node']]['disk_assigned_percent_last_run'] = 0 - node_statistics[node['node']]['disk_used'] = node['disk'] - node_statistics[node['node']]['disk_free'] = int(node['maxdisk']) - int(node['disk']) - node_statistics[node['node']]['disk_free_percent'] = int((node_statistics[node['node']]['disk_free']) / int(node['maxdisk']) * 100) - node_statistics[node['node']]['disk_free_percent_last_run'] = 0 - logging.info(f'{info_prefix} Added node {node["node"]}.') - - # Update node specific vars - if node['node'] in maintenance_nodes_list: - node_statistics[node['node']]['maintenance'] = True - logging.info(f'{info_prefix} Maintenance mode: {node["node"]} is set to maintenance mode.') - - if node['node'] in ignore_nodes_list: - node_statistics[node['node']]['ignore'] = True - logging.info(f'{info_prefix} Ignore Node: {node["node"]} is set to be ignored.') - - logging.info(f'{info_prefix} Created node statistics.') - return node_statistics - - -def get_vm_statistics(api_object, ignore_vms, balancing_type): - """ Get statistics of cpu, memory and disk for each vm in the cluster. """ - info_prefix = 'Info: [vm-statistics]:' - warn_prefix = 'Warn: [vm-statistics]:' - vm_statistics = {} - ignore_vms_list = ignore_vms.split(',') - group_include = None - group_exclude = None - vm_ignore = None - vm_ignore_wildcard = False - _vm_details_storage_allowed = ['ide', 'nvme', 'scsi', 'virtio', 'sata', 'rootfs'] - - # Wildcard support: Initially validate if we need to honour - # any wildcards within the vm_ignore list. - vm_ignore_wildcard = __validate_ignore_vm_wildcard(ignore_vms) - - for node in api_object.nodes.get(): - - # Get VM/CT objects only when the node is online and reachable. - if node['status'] == 'online': - - # Add all virtual machines if type is vm or all. - if balancing_type == 'vm' or balancing_type == 'all': - for vm in api_object.nodes(node['node']).qemu.get(): - - # Get the VM tags from API. - vm_tags = __get_vm_tags(api_object, node, vm['vmid'], 'vm') - if vm_tags is not None: - group_include, group_exclude, vm_ignore = __get_proxlb_groups(vm_tags) - - # Get wildcard match for VMs to ignore if a wildcard pattern was - # previously found. Wildcards may slow down the task when using - # many patterns in the ignore list. Therefore, run this only if - # a wildcard pattern was found. We also do not need to validate - # this if the VM is already being ignored by a defined tag. - if vm_ignore_wildcard and not vm_ignore: - vm_ignore = __check_vm_name_wildcard_pattern(vm['name'], ignore_vms_list) - - if vm['status'] == 'running' and vm['name'] not in ignore_vms_list and not vm_ignore: - vm_statistics[vm['name']] = {} - vm_statistics[vm['name']]['group_include'] = group_include - vm_statistics[vm['name']]['group_exclude'] = group_exclude - vm_statistics[vm['name']]['cpu_total'] = vm['cpus'] - vm_statistics[vm['name']]['cpu_used'] = vm['cpu'] - vm_statistics[vm['name']]['memory_total'] = vm['maxmem'] - vm_statistics[vm['name']]['memory_used'] = vm['mem'] - vm_statistics[vm['name']]['disk_total'] = vm['maxdisk'] - vm_statistics[vm['name']]['disk_used'] = vm['disk'] - vm_statistics[vm['name']]['vmid'] = vm['vmid'] - vm_statistics[vm['name']]['node_parent'] = node['node'] - vm_statistics[vm['name']]['node_rebalance'] = node['node'] - vm_statistics[vm['name']]['storage'] = {} - vm_statistics[vm['name']]['type'] = 'vm' - - # Get disk details of the related object. - _vm_details = api_object.nodes(node['node']).qemu(vm['vmid']).config.get() - logging.info(f'{info_prefix} Getting disk information for vm {vm["name"]}.') - - for vm_detail_key, vm_detail_value in _vm_details.items(): - # vm_detail_key_validator = re.sub('\d+$', '', vm_detail_key) - vm_detail_key_validator = re.sub(r'\d+$', '', vm_detail_key) - - if vm_detail_key_validator in _vm_details_storage_allowed: - vm_statistics[vm['name']]['storage'][vm_detail_key] = {} - match = re.match(r'([^:]+):[^/]+/(.+),iothread=\d+,size=(\d+G)', _vm_details[vm_detail_key]) - - # Create an efficient match group and split the strings to assign them to the storage information. - if match: - _volume = match.group(1) - _disk_name = match.group(2) - _disk_size = match.group(3) - - vm_statistics[vm['name']]['storage'][vm_detail_key]['name'] = _disk_name - vm_statistics[vm['name']]['storage'][vm_detail_key]['device_name'] = vm_detail_key - vm_statistics[vm['name']]['storage'][vm_detail_key]['volume'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_parent'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_rebalance'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['size'] = _disk_size[:-1] - logging.info(f'{info_prefix} Added disk for {vm["name"]}: Name {_disk_name} on volume {_volume} with size {_disk_size}.') - else: - logging.info(f'{info_prefix} No (or unsupported) disk(s) for {vm["name"]} found.') - - logging.info(f'{info_prefix} Added vm {vm["name"]}.') - - # Add all containers if type is ct or all. - if balancing_type == 'ct' or balancing_type == 'all': - for vm in api_object.nodes(node['node']).lxc.get(): - - logging.warning(f'{warn_prefix} Rebalancing on LXC containers (CT) always requires them to shut down.') - logging.warning(f'{warn_prefix} {vm["name"]} is from type CT and cannot be live migrated!') - # Get the VM tags from API. - vm_tags = __get_vm_tags(api_object, node, vm['vmid'], 'ct') - if vm_tags is not None: - group_include, group_exclude, vm_ignore = __get_proxlb_groups(vm_tags) - - # Get wildcard match for VMs to ignore if a wildcard pattern was - # previously found. Wildcards may slow down the task when using - # many patterns in the ignore list. Therefore, run this only if - # a wildcard pattern was found. We also do not need to validate - # this if the VM is already being ignored by a defined tag. - if vm_ignore_wildcard and not vm_ignore: - vm_ignore = __check_vm_name_wildcard_pattern(vm['name'], ignore_vms_list) - - if vm['status'] == 'running' and vm['name'] not in ignore_vms_list and not vm_ignore: - vm_statistics[vm['name']] = {} - vm_statistics[vm['name']]['group_include'] = group_include - vm_statistics[vm['name']]['group_exclude'] = group_exclude - vm_statistics[vm['name']]['cpu_total'] = vm['cpus'] - vm_statistics[vm['name']]['cpu_used'] = vm['cpu'] - vm_statistics[vm['name']]['memory_total'] = vm['maxmem'] - vm_statistics[vm['name']]['memory_used'] = vm['mem'] - vm_statistics[vm['name']]['disk_total'] = vm['maxdisk'] - vm_statistics[vm['name']]['disk_used'] = vm['disk'] - vm_statistics[vm['name']]['vmid'] = vm['vmid'] - vm_statistics[vm['name']]['node_parent'] = node['node'] - vm_statistics[vm['name']]['node_rebalance'] = node['node'] - vm_statistics[vm['name']]['storage'] = {} - vm_statistics[vm['name']]['type'] = 'ct' - - # Get disk details of the related object. - _vm_details = api_object.nodes(node['node']).lxc(vm['vmid']).config.get() - logging.info(f'{info_prefix} Getting disk information for vm {vm["name"]}.') - - for vm_detail_key, vm_detail_value in _vm_details.items(): - # vm_detail_key_validator = re.sub('\d+$', '', vm_detail_key) - vm_detail_key_validator = re.sub(r'\d+$', '', vm_detail_key) - - if vm_detail_key_validator in _vm_details_storage_allowed: - vm_statistics[vm['name']]['storage'][vm_detail_key] = {} - match = re.match(r'(?P[^:]+):(?P[^,]+),size=(?P\S+)', _vm_details[vm_detail_key]) - - # Create an efficient match group and split the strings to assign them to the storage information. - if match: - _volume = match.group(1) - _disk_name = match.group(2) - _disk_size = match.group(3) - - vm_statistics[vm['name']]['storage'][vm_detail_key]['name'] = _disk_name - vm_statistics[vm['name']]['storage'][vm_detail_key]['device_name'] = vm_detail_key - vm_statistics[vm['name']]['storage'][vm_detail_key]['volume'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_parent'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['storage_rebalance'] = _volume - vm_statistics[vm['name']]['storage'][vm_detail_key]['size'] = _disk_size[:-1] - logging.info(f'{info_prefix} Added disk for {vm["name"]}: Name {_disk_name} on volume {_volume} with size {_disk_size}.') - else: - logging.info(f'{info_prefix} No disks for {vm["name"]} found.') - - logging.info(f'{info_prefix} Added vm {vm["name"]}.') - - logging.info(f'{info_prefix} Created VM statistics.') - return vm_statistics - - -def update_node_statistics(node_statistics, vm_statistics): - """ Update node statistics by VMs statistics. """ - info_prefix = 'Info: [node-update-statistics]:' - warn_prefix = 'Warning: [node-update-statistics]:' - - for vm, vm_value in vm_statistics.items(): - node_statistics[vm_value['node_parent']]['cpu_assigned'] = node_statistics[vm_value['node_parent']]['cpu_assigned'] + int(vm_value['cpu_total']) - node_statistics[vm_value['node_parent']]['cpu_assigned_percent'] = (node_statistics[vm_value['node_parent']]['cpu_assigned'] / node_statistics[vm_value['node_parent']]['cpu_total']) * 100 - node_statistics[vm_value['node_parent']]['memory_assigned'] = node_statistics[vm_value['node_parent']]['memory_assigned'] + int(vm_value['memory_total']) - node_statistics[vm_value['node_parent']]['memory_assigned_percent'] = (node_statistics[vm_value['node_parent']]['memory_assigned'] / node_statistics[vm_value['node_parent']]['memory_total']) * 100 - node_statistics[vm_value['node_parent']]['disk_assigned'] = node_statistics[vm_value['node_parent']]['disk_assigned'] + int(vm_value['disk_total']) - node_statistics[vm_value['node_parent']]['disk_assigned_percent'] = (node_statistics[vm_value['node_parent']]['disk_assigned'] / node_statistics[vm_value['node_parent']]['disk_total']) * 100 - - if node_statistics[vm_value['node_parent']]['cpu_assigned_percent'] > 99: - logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for CPU by {int(node_statistics[vm_value["node_parent"]]["cpu_assigned_percent"])}%.') - - if node_statistics[vm_value['node_parent']]['memory_assigned_percent'] > 99: - logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for memory by {int(node_statistics[vm_value["node_parent"]]["memory_assigned_percent"])}%.') - - if node_statistics[vm_value['node_parent']]['disk_assigned_percent'] > 99: - logging.warning(f'{warn_prefix} Node {vm_value["node_parent"]} is overprovisioned for disk by {int(node_statistics[vm_value["node_parent"]]["disk_assigned_percent"])}%.') - - logging.info(f'{info_prefix} Updated node resource assignments by all VMs.') - logging.debug('node_statistics') - return node_statistics - - -def get_storage_statistics(api_object): - """ Get statistics of all storage in the cluster. """ - info_prefix = 'Info: [storage-statistics]:' - storage_whitelist = ['nfs'] - storage_statistics = {} - - for node in api_object.nodes.get(): - - for storage in api_object.nodes(node['node']).storage.get(): - - # Only add enabled and active storage repositories that might be suitable for further - # storage balancing. - if storage['enabled'] and storage['active'] and storage['shared'] and storage['type'] in storage_whitelist: - storage_statistics[storage['storage']] = {} - storage_statistics[storage['storage']]['name'] = storage['storage'] - storage_statistics[storage['storage']]['total'] = storage['total'] - storage_statistics[storage['storage']]['used'] = storage['used'] - storage_statistics[storage['storage']]['used_percent'] = storage['used'] / storage['total'] * 100 - storage_statistics[storage['storage']]['used_percent_last_run'] = 0 - storage_statistics[storage['storage']]['free'] = storage['total'] - storage['used'] - storage_statistics[storage['storage']]['free_percent'] = storage_statistics[storage['storage']]['free'] / storage['total'] * 100 - storage_statistics[storage['storage']]['used_fraction'] = storage['used_fraction'] - storage_statistics[storage['storage']]['type'] = storage['type'] - storage_statistics[storage['storage']]['content'] = storage['content'] - storage_statistics[storage['storage']]['usage_type'] = '' - - # Split the Proxmox returned values to a list and validate the supported - # types of the underlying storage for further migrations. - storage_content_list = storage['content'].split(',') - usage_ct = False - usage_vm = False - - if 'rootdir' in storage_content_list: - usage_ct = True - storage_statistics[storage['storage']]['usage_type'] = 'ct' - logging.info(f'{info_prefix} Storage {storage["storage"]} support CTs.') - - if 'images' in storage_content_list: - usage_vm = True - storage_statistics[storage['storage']]['usage_type'] = 'vm' - logging.info(f'{info_prefix} Storage {storage["storage"]} support VMs.') - - if usage_ct and usage_vm: - storage_statistics[storage['storage']]['usage_type'] = 'all' - logging.info(f'{info_prefix} Updateing storage {storage["storage"]} support to CTs and VMs.') - - logging.info(f'{info_prefix} Added storage {storage["storage"]}.') - - logging.info(f'{info_prefix} Created storage statistics.') - return storage_statistics - - -def __validate_ignore_vm_wildcard(ignore_vms): - """ Validate if a wildcard is used for ignored VMs. """ - if '*' in ignore_vms: - return True - - -def __check_vm_name_wildcard_pattern(vm_name, ignore_vms_list): - """ Validate if the VM name is in the ignore list pattern included. """ - for ignore_vm in ignore_vms_list: - if '*' in ignore_vm: - if ignore_vm[:-1] in vm_name: - return True - - -def __get_vm_tags(api_object, node, vmid, balancing_type): - """ Get tags for a VM/CT for a given VMID. """ - info_prefix = 'Info: [api-get-vm-tags]:' - - if balancing_type == 'vm': - vm_config = api_object.nodes(node['node']).qemu(vmid).config.get() - - if balancing_type == 'ct': - vm_config = api_object.nodes(node['node']).lxc(vmid).config.get() - - if vm_config.get("tags", None) is None: - logging.info(f'{info_prefix} Got no VM/CT tag for VM {vm_config.get("name", None)} from API.') - else: - logging.info(f'{info_prefix} Got VM/CT tag {vm_config.get("tags", None)} for VM {vm_config.get("name", None)} from API.') - return vm_config.get('tags', None) - - -def __get_proxlb_groups(vm_tags): - """ Get ProxLB related include and exclude groups. """ - info_prefix = 'Info: [api-get-vm-include-exclude-tags]:' - group_include = None - group_exclude = None - vm_ignore = None - - group_list = re.split(";", vm_tags) - for group in group_list: - - if group.startswith('plb_include_'): - logging.info(f'{info_prefix} Got PLB include group.') - group_include = group - - if group.startswith('plb_affinity_'): - logging.info(f'{info_prefix} Got PLB include group.') - group_include = group - - if group.startswith('plb_exclude_'): - logging.info(f'{info_prefix} Got PLB exclude group.') - group_exclude = group - - if group.startswith('plb_antiaffinity_'): - logging.info(f'{info_prefix} Got PLB exclude group.') - group_exclude = group - - if group.startswith('plb_ignore_vm'): - logging.info(f'{info_prefix} Got PLB ignore group.') - vm_ignore = True - - return group_include, group_exclude, vm_ignore - - -def balancing_vm_calculations(balancing_method, balancing_mode, balancing_mode_option, node_statistics, vm_statistics, balanciness, app_args, rebalance, processed_vms): - """ Calculate re-balancing of VMs on present nodes across the cluster. """ - info_prefix = 'Info: [rebalancing-vm-calculator]:' - - # Validate for a supported balancing method, mode and if rebalancing is required. - __validate_balancing_method(balancing_method) - __validate_balancing_mode(balancing_mode) - __validate_vm_statistics(vm_statistics) - rebalance = __validate_balanciness(balanciness, balancing_method, balancing_mode, node_statistics) - - # Run rebalancing calculations. - if rebalance: - # Get most used/assigned resources of the VM and the most free or less allocated node. - resources_vm_most_used, processed_vms = __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms) - resources_node_most_free = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics) - - # If most used vm is on most free node then skip it and get another one. - while resources_vm_most_used[1]['node_parent'] == resources_node_most_free[0] and len(processed_vms) < len(vm_statistics): - resources_vm_most_used, processed_vms = __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms) - logging.debug(f'{info_prefix} processed {len(processed_vms)} out of {len(vm_statistics)} vms.') - - # Update resource statistics for VMs and nodes. - node_statistics, vm_statistics = __update_vm_resource_statistics(resources_vm_most_used, resources_node_most_free, - vm_statistics, node_statistics, balancing_method, balancing_mode) - - # Start recursion until we do not have any needs to rebalance anymore. - balancing_vm_calculations(balancing_method, balancing_mode, balancing_mode_option, node_statistics, vm_statistics, balanciness, app_args, rebalance, processed_vms) - - # If only best node argument set we simply return the next best node for VM - # and CT placement on the CLI and stop ProxLB. - if app_args.best_node: - logging.info(f'{info_prefix} Only best next node for new VM & CT placement requsted.') - best_next_node = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics) - print(best_next_node[0]) - logging.info(f'{info_prefix} Best next node for VM & CT placement: {best_next_node[0]}') - sys.exit(0) - - logging.info(f'{info_prefix} Balancing calculations done.') - return node_statistics, vm_statistics - - -def balancing_vm_maintenance(proxlb_config, app_args, node_statistics, vm_statistics): - """ Calculate re-balancing of VMs that need to be moved away from maintenance nodes. """ - info_prefix = 'Info: [rebalancing-maintenance-vm-calculator]:' - maintenance_nodes_list = proxlb_config['vm_maintenance_nodes'].split(',') - nodes_present = list(node_statistics.keys()) - balancing_method = proxlb_config['vm_balancing_method'] - balancing_mode = proxlb_config['vm_balancing_mode'] - balancing_mode_option = proxlb_config['vm_balancing_mode_option'] - - # Merge maintenance nodes from config and cli args. - if app_args.maintenance is not None: - logging.info(f'{info_prefix} Maintenance nodes from CLI arg and config will be merged.') - maintenance_nodes_list = maintenance_nodes_list + app_args.maintenance.split(',') - - # Ensure that only existing nodes in the cluster will be used. - if len(maintenance_nodes_list) > 1: - maintenance_nodes_list = set(maintenance_nodes_list) & set(nodes_present) - logging.info(f'{info_prefix} Maintenance mode for the following hosts defined: {maintenance_nodes_list}') - else: - logging.info(f'{info_prefix} No nodes for maintenance mode defined.') - return node_statistics, vm_statistics - - for node_name in maintenance_nodes_list: - node_vms = list(filter(lambda item: item[0] if item[1]['node_parent'] == node_name else [], vm_statistics.items())) - # Update resource statistics for VMs and nodes. - for vm in node_vms: - resources_node_most_free = __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics) - node_statistics, vm_statistics = __update_vm_resource_statistics(vm, resources_node_most_free, vm_statistics, node_statistics, balancing_method, balancing_mode) - - return node_statistics, vm_statistics - - -def __validate_balancing_method(balancing_method): - """ Validate for valid and supported balancing method. """ - error_prefix = 'Error: [balancing-method-validation]:' - info_prefix = 'Info: [balancing-method-validation]:' - - if balancing_method not in ['memory', 'disk', 'cpu']: - logging.error(f'{error_prefix} Invalid balancing method: {balancing_method}') - sys.exit(2) - else: - logging.info(f'{info_prefix} Valid balancing method: {balancing_method}') - - -def __validate_balancing_mode(balancing_mode): - """ Validate for valid and supported balancing mode. """ - error_prefix = 'Error: [balancing-mode-validation]:' - info_prefix = 'Info: [balancing-mode-validation]:' - - if balancing_mode not in ['used', 'assigned']: - logging.error(f'{error_prefix} Invalid balancing method: {balancing_mode}') - sys.exit(2) - else: - logging.info(f'{info_prefix} Valid balancing method: {balancing_mode}') - - -def __validate_vm_statistics(vm_statistics): - """ Validate for at least a single object of type CT/VM to rebalance. """ - error_prefix = 'Error: [balancing-vm-stats-validation]:' - - if len(vm_statistics) == 0: - logging.error(f'{error_prefix} Not a single CT/VM found in cluster.') - sys.exit(1) - - -def __validate_balanciness(balanciness, balancing_method, balancing_mode, node_statistics): - """ Validate for balanciness to ensure further rebalancing is needed. """ - info_prefix = 'Info: [balanciness-validation]:' - node_resource_percent_list = [] - node_assigned_percent_match = [] - - # Remap balancing mode to get the related values from nodes dict. - if balancing_mode == 'used': - node_resource_selector = 'free' - if balancing_mode == 'assigned': - node_resource_selector = 'assigned' - - for node_name, node_info in node_statistics.items(): - - # Save information of nodes from current run to compare them in the next recursion. - if node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_last_run'] == node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent']: - node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_match'] = True - else: - node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_match'] = False - # Update value to the current value of the recursion run. - node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent_last_run'] = node_statistics[node_name][f'{balancing_method}_{node_resource_selector}_percent'] - - # If all node resources are unchanged, the recursion can be left. - for key, value in node_statistics.items(): - node_assigned_percent_match.append(value.get(f'{balancing_method}_{node_resource_selector}_percent_match', False)) - - if False not in node_assigned_percent_match: - return False - - # Add node information to resource list. - if not node_statistics[node_name]['maintenance']: - node_resource_percent_list.append(int(node_info[f'{balancing_method}_{node_resource_selector}_percent'])) - logging.debug(f'{info_prefix} Node: {node_name} with values: {node_info}') - - # Create a sorted list of the delta + balanciness between the node resources. - node_resource_percent_list_sorted = sorted(node_resource_percent_list) - node_lowest_percent = node_resource_percent_list_sorted[0] - node_highest_percent = node_resource_percent_list_sorted[-1] - - # Validate if the recursion should be proceeded for further rebalancing. - if (int(node_lowest_percent) + int(balanciness)) < int(node_highest_percent): - logging.info(f'{info_prefix} Rebalancing for {balancing_method} is needed. Highest usage: {int(node_highest_percent)}% | Lowest usage: {int(node_lowest_percent)}%.') - return True - else: - logging.info(f'{info_prefix} Rebalancing for {balancing_method} is not needed. Highest usage: {int(node_highest_percent)}% | Lowest usage: {int(node_lowest_percent)}%.') - return False - - -def __get_most_used_resources_vm(balancing_method, balancing_mode, vm_statistics, processed_vms): - """ Get and return the most used resources of a VM by the defined balancing method. """ - info_prefix = 'Info: [get-most-used-resources-vm]:' - - # Remap balancing mode to get the related values from nodes dict. - if balancing_mode == 'used': - vm_resource_selector = 'used' - if balancing_mode == 'assigned': - vm_resource_selector = 'total' - - vm = max(vm_statistics.items(), key=lambda item: item[1][f'{balancing_method}_{vm_resource_selector}'] if item[0] not in processed_vms else -float('inf')) - processed_vms.append(vm[0]) - - logging.info(f'{info_prefix} {vm}') - return vm, processed_vms - - -def __get_most_free_resources_node(balancing_method, balancing_mode, balancing_mode_option, node_statistics): - """ Get and return the most free resources of a node by the defined balancing method. """ - info_prefix = 'Info: [get-most-free-resources-nodes]:' - - # Return the node information based on the balancing mode. - if balancing_mode == 'used' and balancing_mode_option == 'bytes': - node = max(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_free'] if not item[1]['maintenance'] else -float('inf')) - if balancing_mode == 'used' and balancing_mode_option == 'percent': - node = max(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_free_percent'] if not item[1]['maintenance'] else -float('inf')) - if balancing_mode == 'assigned': - node = min(node_statistics.items(), key=lambda item: item[1][f'{balancing_method}_assigned'] if not item[1]['maintenance'] and (item[1][f'{balancing_method}_assigned_percent'] > 0 or item[1][f'{balancing_method}_assigned_percent'] < 100) else -float('inf')) - - logging.info(f'{info_prefix} {node}') - return node - - -def __update_vm_resource_statistics(resource_highest_used_resources_vm, resource_highest_free_resources_node, vm_statistics, node_statistics, balancing_method, balancing_mode): - """ Update VM and node resource statistics. """ - info_prefix = 'Info: [rebalancing-resource-statistics-update]:' - - if resource_highest_used_resources_vm[1]['node_parent'] != resource_highest_free_resources_node[0]: - vm_name = resource_highest_used_resources_vm[0] - vm_node_parent = resource_highest_used_resources_vm[1]['node_parent'] - vm_node_rebalance = resource_highest_free_resources_node[0] - vm_resource_used = vm_statistics[resource_highest_used_resources_vm[0]][f'{balancing_method}_used'] - vm_resource_total = vm_statistics[resource_highest_used_resources_vm[0]][f'{balancing_method}_total'] - - # Update dictionaries for new values - # Assign new rebalance node to vm - vm_statistics[vm_name]['node_rebalance'] = vm_node_rebalance - - logging.info(f'{info_prefix} Moving {vm_name} from {vm_node_parent} to {vm_node_rebalance}') - - # Recalculate values for nodes - ## Add freed resources to old parent node - node_statistics[vm_node_parent][f'{balancing_method}_used'] = int(node_statistics[vm_node_parent][f'{balancing_method}_used']) - int(vm_resource_used) - node_statistics[vm_node_parent][f'{balancing_method}_free'] = int(node_statistics[vm_node_parent][f'{balancing_method}_free']) + int(vm_resource_used) - node_statistics[vm_node_parent][f'{balancing_method}_free_percent'] = int(int(node_statistics[vm_node_parent][f'{balancing_method}_free']) / int(node_statistics[vm_node_parent][f'{balancing_method}_total']) * 100) - node_statistics[vm_node_parent][f'{balancing_method}_assigned'] = int(node_statistics[vm_node_parent][f'{balancing_method}_assigned']) - int(vm_resource_total) - node_statistics[vm_node_parent][f'{balancing_method}_assigned_percent'] = int(int(node_statistics[vm_node_parent][f'{balancing_method}_assigned']) / int(node_statistics[vm_node_parent][f'{balancing_method}_total']) * 100) - - ## Removed newly allocated resources to new rebalanced node - node_statistics[vm_node_rebalance][f'{balancing_method}_used'] = int(node_statistics[vm_node_rebalance][f'{balancing_method}_used']) + int(vm_resource_used) - node_statistics[vm_node_rebalance][f'{balancing_method}_free'] = int(node_statistics[vm_node_rebalance][f'{balancing_method}_free']) - int(vm_resource_used) - node_statistics[vm_node_rebalance][f'{balancing_method}_free_percent'] = int(int(node_statistics[vm_node_rebalance][f'{balancing_method}_free']) / int(node_statistics[vm_node_rebalance][f'{balancing_method}_total']) * 100) - node_statistics[vm_node_rebalance][f'{balancing_method}_assigned'] = int(node_statistics[vm_node_rebalance][f'{balancing_method}_assigned']) + int(vm_resource_total) - node_statistics[vm_node_rebalance][f'{balancing_method}_assigned_percent'] = int(int(node_statistics[vm_node_rebalance][f'{balancing_method}_assigned']) / int(node_statistics[vm_node_rebalance][f'{balancing_method}_total']) * 100) - - logging.info(f'{info_prefix} Updated VM and node statistics.') - return node_statistics, vm_statistics - - -def __get_vm_tags_include_groups(vm_statistics, node_statistics, balancing_method, balancing_mode): - """ Get VMs tags for include groups. """ - info_prefix = 'Info: [rebalancing-tags-group-include]:' - tags_include_vms = {} - processed_vm = [] - - # Create groups of tags with belongings hosts. - for vm_name, vm_values in vm_statistics.items(): - if vm_values.get('group_include', None): - if not tags_include_vms.get(vm_values['group_include'], None): - tags_include_vms[vm_values['group_include']] = [vm_name] - else: - tags_include_vms[vm_values['group_include']] = tags_include_vms[vm_values['group_include']] + [vm_name] - - # Update the VMs to the corresponding node to their group assignments. - for group, vm_names in tags_include_vms.items(): - # Do not take care of tags that have only a single host included. - if len(vm_names) < 2: - logging.info(f'{info_prefix} Only one host in group assignment.') - return node_statistics, vm_statistics - - vm_node_rebalance = False - logging.info(f'{info_prefix} Create include groups of VM hosts.') - for vm_name in vm_names: - if vm_name not in processed_vm: - if not vm_node_rebalance: - vm_node_rebalance = vm_statistics[vm_name]['node_rebalance'] - else: - _mocked_vm_object = (vm_name, vm_statistics[vm_name]) - node_statistics, vm_statistics = __update_vm_resource_statistics(_mocked_vm_object, [vm_node_rebalance], vm_statistics, node_statistics, balancing_method, balancing_mode) - processed_vm.append(vm_name) - - return node_statistics, vm_statistics - - -def __get_vm_tags_exclude_groups(vm_statistics, node_statistics, balancing_method, balancing_mode): - """ Get VMs tags for exclude groups. """ - info_prefix = 'Info: [rebalancing-tags-group-exclude]:' - tags_exclude_vms = {} - - # Create groups of tags with belongings hosts. - for vm_name, vm_values in vm_statistics.items(): - if vm_values.get('group_exclude', None): - if not tags_exclude_vms.get(vm_values['group_exclude'], None): - tags_exclude_vms[vm_values['group_exclude']] = {} - tags_exclude_vms[vm_values['group_exclude']]['nodes_used'] = [] - tags_exclude_vms[vm_values['group_exclude']]['nodes_used'].append(vm_statistics[vm_name]['node_rebalance']) - tags_exclude_vms[vm_values['group_exclude']]['vms'] = [vm_name] - else: - tags_exclude_vms[vm_values['group_exclude']]['vms'] = tags_exclude_vms[vm_values['group_exclude']]['vms'] + [vm_name] - tags_exclude_vms[vm_values['group_exclude']]['nodes_used'].append(vm_statistics[vm_name]['node_rebalance']) - - # Evaluate all VMs assigned for each exclude groups and validate that they will be moved to another random node. - # However, if there are still more VMs than nodes we need to deal with it. - for exclude_group, group_values in tags_exclude_vms.items(): - - group_values['nodes_used'] = [] - for vm in group_values['vms']: - - proceed = True - counter = 0 - - while proceed: - - if vm_statistics[vm]['node_rebalance'] in group_values['nodes_used']: - # Find another possible new target node if possible by randomly get any node from - # the cluster and validating if this is already used for this anti-affinity group. - logging.info(f'{info_prefix} Rebalancing of VM {vm} is needed due to anti-affinity group policy.') - random_node, counter, proceed = __get_random_node(counter, node_statistics, vm) - - if random_node not in group_values['nodes_used']: - logging.info(f'{info_prefix} New random node {random_node} has not yet been used for the anti-affinity group {exclude_group}.') - group_values['nodes_used'].append(random_node) - logging.info(f'{info_prefix} New random node {random_node} has been added as an already used node to the anti-affinity group {exclude_group}.') - logging.info(f'{info_prefix} VM {vm} switched node from {vm_statistics[vm]["node_rebalance"]} to {random_node} due to the anti-affinity group {exclude_group}.') - vm_statistics[vm]['node_rebalance'] = random_node - - else: - # Add the used node to the list for the anti-affinity group to ensure no - # other VM with the same anti-affinity group will use it (if possible). - logging.info(f'{info_prefix} Node {vm_statistics[vm]["node_rebalance"]} has been added as an already used node to the anti-affinity group {exclude_group}.') - logging.info(f'{info_prefix} No rebalancing for VM {vm} needed due to any anti-affinity group policies.') - group_values['nodes_used'].append(vm_statistics[vm]['node_rebalance']) - proceed = False - - return node_statistics, vm_statistics - - -def __get_random_node(counter, node_statistics, vm): - """ Get a random node within the Proxmox cluster. """ - warning_prefix = 'Warning: [random-node-getter]:' - info_prefix = 'Info: [random-node-getter]:' - - counter = counter + 1 - random_node = None - if counter < 30: - random_node = random.choice(list(node_statistics.keys())) - logging.info(f'{info_prefix} New random node {random_node} evaluated for vm {vm} in run {counter}.') - return random_node, counter, False - else: - logging.warning(f'{warning_prefix} Reached limit for random node evaluation for vm {vm}. Unable to find a suitable new node.') - return random_node, counter, False - - -def __wait_job_finalized(api_object, node_name, job_id, counter): - """ Wait for a job to be finalized. """ - error_prefix = 'Error: [job-status-getter]:' - info_prefix = 'Info: [job-status-getter]:' - - logging.info(f'{info_prefix} Getting job status for job {job_id}.') - task = api_object.nodes(node_name).tasks(job_id).status().get() - logging.info(f'{info_prefix} {task}') - - if task['status'] == 'running': - logging.info(f'{info_prefix} Validating job {job_id} for the {counter} run.') - - # Do not run for infinity this recursion and fail when reaching the limit. - if counter == 300: - logging.critical(f'{error_prefix} The job {job_id} on node {node_name} did not finished in time for migration.') - - time.sleep(5) - counter = counter + 1 - logging.info(f'{info_prefix} Revalidating job {job_id} in a next run.') - __wait_job_finalized(api_object, node_name, job_id, counter) - - logging.info(f'{info_prefix} Job {job_id} for migration from {node_name} terminiated succesfully.') - - -def __run_vm_rebalancing(api_object, _vm_vm_statistics, app_args, parallel_migrations): - """ Run & execute the VM rebalancing via API. """ - error_prefix = 'Error: [vm-rebalancing-executor]:' - info_prefix = 'Info: [vm-rebalancing-executor]:' - - # Remove VMs/CTs that do not have a new node location. - vms_to_remove = [vm_name for vm_name, vm_info in _vm_vm_statistics.items() if 'node_rebalance' in vm_info and vm_info['node_rebalance'] == vm_info.get('node_parent')] - for vm_name in vms_to_remove: - del _vm_vm_statistics[vm_name] - - if len(_vm_vm_statistics) > 0 and not app_args.dry_run: - for vm, value in _vm_vm_statistics.items(): - - try: - # Migrate type VM (live migration). - if value['type'] == 'vm': - logging.info(f'{info_prefix} Rebalancing VM {vm} from node {value["node_parent"]} to node {value["node_rebalance"]}.') - options = {'target': value['node_rebalance'], 'online': 1, 'with-local-disks': 1} - job_id = api_object.nodes(value['node_parent']).qemu(value['vmid']).migrate().post(**options) - - # Migrate type CT (requires restart of container). - if value['type'] == 'ct': - logging.info(f'{info_prefix} Rebalancing CT {vm} from node {value["node_parent"]} to node {value["node_rebalance"]}.') - job_id = api_object.nodes(value['node_parent']).lxc(value['vmid']).migrate().post(target=value['node_rebalance'],restart=1) - - except proxmoxer.core.ResourceException as error_resource: - logging.critical(f'{error_prefix} {error_resource}') - - # Wait for migration to be finished unless running parallel migrations. - if not bool(int(parallel_migrations)): - logging.info(f'{info_prefix} Rebalancing will be performed sequentially.') - __wait_job_finalized(api_object, value['node_parent'], job_id, counter=1) - else: - logging.info(f'{info_prefix} Rebalancing will be performed parallely.') - - else: - if app_args.dry_run: - logging.info(f'{info_prefix} Running in dry run mode. Not executing any balancing.') - else: - logging.info(f'{info_prefix} No rebalancing needed.') - - return _vm_vm_statistics - - -def __run_storage_rebalancing(api_object, _storage_vm_statistics, app_args, parallel_migrations): - """ Run & execute the storage rebalancing via API. """ - error_prefix = 'Error: [storage-rebalancing-executor]:' - info_prefix = 'Info: [storage-rebalancing-executor]:' - - # Remove VMs/CTs that do not have a new storage location. - vms_to_remove = [vm_name for vm_name, vm_info in _storage_vm_statistics.items() if all(storage.get('storage_rebalance') == storage.get('storage_parent') for storage in vm_info.get('storage', {}).values())] - for vm_name in vms_to_remove: - del _storage_vm_statistics[vm_name] - - if len(_storage_vm_statistics) > 0 and not app_args.dry_run: - for vm, value in _storage_vm_statistics.items(): - for disk, disk_info in value['storage'].items(): - - if disk_info.get('storage_rebalance', None) is not None: - try: - # Migrate type VM (live migration). - logging.info(f'{info_prefix} Rebalancing storage of VM {vm} from node.') - job_id = api_object.nodes(value['node_parent']).qemu(value['vmid']).move_disk().post(disk=disk,storage=disk_info.get('storage_rebalance', None), delete=1) - - except proxmoxer.core.ResourceException as error_resource: - logging.critical(f'{error_prefix} {error_resource}') - - # Wait for migration to be finished unless running parallel migrations. - if not bool(int(parallel_migrations)): - logging.info(f'{info_prefix} Rebalancing will be performed sequentially.') - __wait_job_finalized(api_object, value['node_parent'], job_id, counter=1) - else: - logging.info(f'{info_prefix} Rebalancing will be performed parallely.') - - else: - logging.info(f'{info_prefix} No rebalancing needed.') - - return _storage_vm_statistics - - -def __create_json_output(vm_statistics, app_args): - """ Create a machine parsable json output of VM rebalance statitics. """ - info_prefix = 'Info: [json-output-generator]:' - - if app_args.json: - logging.info(f'{info_prefix} Printing json output of VM statistics.') - print(json.dumps(vm_statistics)) - - -def __create_cli_output(vm_statistics, app_args): - """ Create output for CLI when running in dry-run mode. """ - info_prefix_dry_run = 'Info: [cli-output-generator-dry-run]:' - info_prefix_run = 'Info: [cli-output-generator]:' - vm_to_node_list = [] - - if app_args.dry_run: - info_prefix = info_prefix_dry_run - logging.info(f'{info_prefix} Starting dry-run to rebalance vms to their new nodes.') - else: - info_prefix = info_prefix_run - logging.info(f'{info_prefix} Start rebalancing vms to their new nodes.') - - vm_to_node_list.append(['VM', 'Current Node', 'Rebalanced Node', 'Current Storage', 'Rebalanced Storage', 'VM Type']) - for vm_name, vm_values in vm_statistics.items(): - for disk, disk_values in vm_values['storage'].items(): - vm_to_node_list.append([vm_name, vm_values['node_parent'], vm_values['node_rebalance'], f'{disk_values.get("storage_parent", "N/A")} ({disk_values.get("device_name", "N/A")})', f'{disk_values.get("storage_rebalance", "N/A")} ({disk_values.get("device_name", "N/A")})', vm_values['type']]) - - if len(vm_statistics) > 0: - logging.info(f'{info_prefix} Printing cli output of VM rebalancing.') - __print_table_cli(vm_to_node_list, app_args.dry_run) - else: - logging.info(f'{info_prefix} No rebalancing needed.') - - -def __print_table_cli(table, dry_run=False): - """ Pretty print a given table to the cli. """ - info_prefix_dry_run = 'Info: [cli-output-generator-table-dryn-run]:' - info_prefix_run = 'Info: [cli-output-generator-table]:' - info_prefix = info_prefix_run - - longest_cols = [ - (max([len(str(row[i])) for row in table]) + 3) - for i in range(len(table[0])) - ] - - row_format = "".join(["{:>" + str(longest_col) + "}" for longest_col in longest_cols]) - - for row in table: - # Print CLI output when running in dry-run mode to make the user's life easier. - if dry_run: - info_prefix = info_prefix_dry_run - print(row_format.format(*row)) - - # Log all items in info mode. - logging.info(f'{info_prefix} {row_format.format(*row)}') - - -def run_rebalancing(api_object, vm_statistics, app_args, parallel_migrations, balancing_type): - """ Run rebalancing of vms to new nodes in cluster. """ - _vm_vm_statistics = {} - _storage_vm_statistics = {} - - if balancing_type == 'vm': - _vm_vm_statistics = copy.deepcopy(vm_statistics) - _vm_vm_statistics = __run_vm_rebalancing(api_object, _vm_vm_statistics, app_args, parallel_migrations) - return _vm_vm_statistics - - if balancing_type == 'storage': - _storage_vm_statistics = copy.deepcopy(vm_statistics) - _storage_vm_statistics = __run_storage_rebalancing(api_object, _storage_vm_statistics, app_args, parallel_migrations) - return _storage_vm_statistics - - -def run_output_rebalancing(app_args, vm_output_statistics, storage_output_statistics): - """ Generate output of rebalanced resources. """ - output_statistics = {**vm_output_statistics, **storage_output_statistics} - __create_json_output(output_statistics, app_args) - __create_cli_output(output_statistics, app_args) - - -def balancing_storage_calculations(storage_balancing_method, storage_statistics, vm_statistics, balanciness, rebalance, processed_vms): - """ Calculate re-balancing of storage on present datastores across the cluster. """ - info_prefix = 'Info: [storage-rebalancing-calculator]:' - - # Validate for a supported balancing method, mode and if rebalancing is required. - __validate_vm_statistics(vm_statistics) - rebalance = __validate_storage_balanciness(balanciness, storage_balancing_method, storage_statistics) - - if rebalance: - vm_name, vm_disk_device = __get_most_used_resources_vm_storage(vm_statistics) - - if vm_name not in processed_vms: - processed_vms.append(vm_name) - resources_storage_most_free = __get_most_free_storage(storage_balancing_method, storage_statistics) - - # Update resource statistics for VMs and storage. - storage_statistics, vm_statistic = __update_resource_storage_statistics(storage_statistics, resources_storage_most_free, vm_statistics, vm_name, vm_disk_device) - - # Start recursion until we do not have any needs to rebalance anymore. - balancing_storage_calculations(storage_balancing_method, storage_statistics, vm_statistics, balanciness, rebalance, processed_vms) - - logging.info(f'{info_prefix} Balancing calculations done.') - return storage_statistics, vm_statistics - - -def __validate_storage_balanciness(balanciness, storage_balancing_method, storage_statistics): - """ Validate for balanciness of storage to ensure further rebalancing is needed. """ - info_prefix = 'Info: [storage-balanciness-validation]:' - error_prefix = 'Error: [storage-balanciness-validation]:' - storage_resource_percent_list = [] - storage_assigned_percent_match = [] - - # Validate for an allowed balancing method and define the storage resource selector. - if storage_balancing_method == 'disk_space': - logging.info(f'{info_prefix} Getting most free storage volume by disk size.') - storage_resource_selector = 'used' - elif storage_balancing_method == 'disk_io': - logging.error(f'{error_prefix} Getting most free storage volume by disk IO is not yet supported.') - sys.exit(2) - else: - logging.error(f'{error_prefix} Getting most free storage volume by disk IO is not yet supported.') - sys.exit(2) - - # Obtain the metrics - for storage_name, storage_info in storage_statistics.items(): - - logging.info(f'{info_prefix} Validating storage: {storage_name} for balanciness for usage with: {storage_balancing_method}.') - # Save information of nodes from current run to compare them in the next recursion. - if storage_statistics[storage_name][f'{storage_resource_selector}_percent_last_run'] == storage_statistics[storage_name][f'{storage_resource_selector}_percent']: - storage_statistics[storage_name][f'{storage_resource_selector}_percent_match'] = True - else: - storage_statistics[storage_name][f'{storage_resource_selector}_percent_match'] = False - - # Update value to the current value of the recursion run. - storage_statistics[storage_name][f'{storage_resource_selector}_percent_last_run'] = storage_statistics[storage_name][f'{storage_resource_selector}_percent'] - - # If all node resources are unchanged, the recursion can be left. - for key, value in storage_statistics.items(): - storage_assigned_percent_match.append(value.get(f'{storage_resource_selector}_percent_match', False)) - - if False not in storage_assigned_percent_match: - return False - - # Add node information to resource list. - storage_resource_percent_list.append(int(storage_info[f'{storage_resource_selector}_percent'])) - logging.info(f'{info_prefix} Storage: {storage_name} with values: {storage_info}') - - # Create a sorted list of the delta + balanciness between the node resources. - storage_resource_percent_list_sorted = sorted(storage_resource_percent_list) - storage_lowest_percent = storage_resource_percent_list_sorted[0] - storage_highest_percent = storage_resource_percent_list_sorted[-1] - - # Validate if the recursion should be proceeded for further rebalancing. - if (int(storage_lowest_percent) + int(balanciness)) < int(storage_highest_percent): - logging.info(f'{info_prefix} Rebalancing for type "{storage_resource_selector}" of storage is needed. Highest usage: {int(storage_highest_percent)}% | Lowest usage: {int(storage_lowest_percent)}%.') - return True - else: - logging.info(f'{info_prefix} Rebalancing for type "{storage_resource_selector}" of storage is not needed. Highest usage: {int(storage_highest_percent)}% | Lowest usage: {int(storage_lowest_percent)}%.') - return False - - -def __get_most_used_resources_vm_storage(vm_statistics): - """ Get and return the most used disk of a VM by storage. """ - info_prefix = 'Info: [get-most-used-disks-resources-vm]:' - - # Get the biggest storage of a VM/CT. A VM/CT can hold multiple disks. Therefore, we need to iterate - # over all assigned disks to get the biggest one. - vm_object = sorted( - vm_statistics.items(), - key=lambda x: max( - (size_in_bytes(storage['size']) for storage in x[1].get('storage', {}).values() if 'size' in storage), - default=0 - ), - reverse=True - ) - - vm_object = vm_object[0] - vm_name = vm_object[0] - vm_disk_device = max(vm_object[1]['storage'], key=lambda x: int(vm_object[1]['storage'][x]['size'])) - logging.info(f'{info_prefix} Got most used VM: {vm_name} with storage device: {vm_disk_device}.') - - return vm_name, vm_disk_device - - -def __get_most_free_storage(storage_balancing_method, storage_statistics): - """ Get the storage with the most free space or IO, depending on the balancing mode. """ - info_prefix = 'Info: [get-most-free-storage]:' - error_prefix = 'Error: [get-most-free-storage]:' - storage_volume = None - logging.info(f'{info_prefix} Starting to evaluate the most free storage volume.') - - if storage_balancing_method == 'disk_space': - logging.info(f'{info_prefix} Getting most free storage volume by disk space.') - storage_volume = max(storage_statistics, key=lambda x: storage_statistics[x]['free_percent']) - - if storage_balancing_method == 'disk_io': - logging.info(f'{info_prefix} Getting most free storage volume by disk IO.') - logging.error(f'{error_prefix} Getting most free storage volume by disk IO is not yet supported.') - sys.exit(2) - - return storage_volume - - -def __update_resource_storage_statistics(storage_statistics, resources_storage_most_free, vm_statistics, vm_name, vm_disk_device): - """ Update VM and storage resource statistics. """ - info_prefix = 'Info: [rebalancing-storage-resource-statistics-update]:' - current_storage = vm_statistics[vm_name]['storage'][vm_disk_device]['storage_parent'] - current_storage_size = storage_statistics[current_storage]['free'] / (1024 ** 3) - rebalance_storage = resources_storage_most_free - rebalance_storage_size = storage_statistics[rebalance_storage]['free'] / (1024 ** 3) - vm_storage_size = vm_statistics[vm_name]['storage'][vm_disk_device]['size'] - vm_storage_size_bytes = int(vm_storage_size) * 1024**3 - - # Assign new storage device to vm - logging.info(f'{info_prefix} Validating VM {vm_name} for potential storage rebalancing.') - if vm_statistics[vm_name]['storage'][vm_disk_device]['storage_rebalance'] == vm_statistics[vm_name]['storage'][vm_disk_device]['storage_parent']: - logging.info(f'{info_prefix} Setting VM {vm_name} from {current_storage} to {rebalance_storage} storage.') - vm_statistics[vm_name]['storage'][vm_disk_device]['storage_rebalance'] = resources_storage_most_free - else: - logging.info(f'{info_prefix} Setting VM {vm_name} from {current_storage} to {rebalance_storage} storage.') - - # Recalculate values for storage - ## Add freed resources to old parent storage device - storage_statistics[current_storage]['used'] = storage_statistics[current_storage]['used'] - vm_storage_size_bytes - storage_statistics[current_storage]['free'] = storage_statistics[current_storage]['free'] + vm_storage_size_bytes - storage_statistics[current_storage]['free_percent'] = (storage_statistics[current_storage]['free'] / storage_statistics[current_storage]['total']) * 100 - storage_statistics[current_storage]['used_percent'] = (storage_statistics[current_storage]['used'] / storage_statistics[current_storage]['total']) * 100 - logging.info(f'{info_prefix} Adding free space of {vm_storage_size}G to old storage with {current_storage_size}G. [free: {int(current_storage_size) + int(vm_storage_size)}G | {storage_statistics[current_storage]["free_percent"]}%]') - - ## Removed newly allocated resources to new rebalanced storage device - storage_statistics[rebalance_storage]['used'] = storage_statistics[rebalance_storage]['used'] + vm_storage_size_bytes - storage_statistics[rebalance_storage]['free'] = storage_statistics[rebalance_storage]['free'] - vm_storage_size_bytes - storage_statistics[rebalance_storage]['free_percent'] = (storage_statistics[rebalance_storage]['free'] / storage_statistics[rebalance_storage]['total']) * 100 - storage_statistics[rebalance_storage]['used_percent'] = (storage_statistics[rebalance_storage]['used'] / storage_statistics[rebalance_storage]['total']) * 100 - logging.info(f'{info_prefix} Adding used space of {vm_storage_size}G to new storage with {rebalance_storage_size}G. [free: {int(rebalance_storage_size) - int(vm_storage_size)}G | {storage_statistics[rebalance_storage]["free_percent"]}%]') - - logging.info(f'{info_prefix} Updated VM and storage statistics.') - return storage_statistics, vm_statistics - - -def size_in_bytes(size_str): - size_unit = size_str[-1].upper() - size_value = float(size_str) - size_multipliers = {'K': 1024, 'M': 1024**2, 'G': 1024**3, 'T': 1024**4} - return size_value * size_multipliers.get(size_unit, 1) - - -def balancing_vm_affinity_groups(node_statistics, vm_statistics, balancing_method, balancing_mode): - """ Enforce (anti-)affinity groups for further VM movement across the cluster. """ - node_statistics, vm_statistics = __get_vm_tags_include_groups(vm_statistics, node_statistics, balancing_method, balancing_mode) - node_statistics, vm_statistics = __get_vm_tags_exclude_groups(vm_statistics, node_statistics, balancing_method, balancing_mode) - return node_statistics, vm_statistics - - -def main(): - """ Run ProxLB for balancing VM workloads across a Proxmox cluster. """ - vm_output_statistics = {} - storage_output_statistics = {} - - # Initialize PAS. - initialize_logger('CRITICAL') - app_args = initialize_args() - if app_args.version: - proxlb_output_version() - config_path = initialize_config_path(app_args) - pre_validations(config_path) - - # Parse global config. - proxlb_config = initialize_config_options(config_path) - pre_validations(config_path, proxlb_config) - - # Overwrite logging handler with user defined log verbosity. - initialize_logger(proxlb_config['log_verbosity'], update_log_verbosity=True) - - while True: - # API Authentication. - api_object = api_connect(proxlb_config['proxmox_api_host'], proxlb_config['proxmox_api_user'], proxlb_config['proxmox_api_pass'], proxlb_config['proxmox_api_ssl_v'], proxlb_config['proxmox_api_timeout']) - - # Get master node of cluster and ensure that ProxLB is only performed on the - # cluster master node to avoid ongoing rebalancing. - cluster_master, master_only = execute_rebalancing_only_by_master(api_object, proxlb_config['master_only']) - - # Validate daemon service and skip following tasks when not being the cluster master. - if not cluster_master and master_only: - validate_daemon(proxlb_config['daemon'], proxlb_config['schedule']) - continue - - # Get metrics & statistics for vms and nodes. - if proxlb_config['vm_balancing_enable'] or proxlb_config['storage_balancing_enable'] or app_args.best_node: - node_statistics = get_node_statistics(api_object, proxlb_config['vm_ignore_nodes'], proxlb_config['vm_maintenance_nodes']) - vm_statistics = get_vm_statistics(api_object, proxlb_config['vm_ignore_vms'], proxlb_config['vm_balancing_type']) - node_statistics = update_node_statistics(node_statistics, vm_statistics) - # Obtaining metrics for the storage may take longer times and is not needed for VM/CT balancing. - # We can save time by skipping this when not really needed. - if proxlb_config['storage_balancing_enable']: - storage_statistics = get_storage_statistics(api_object) - - # Execute VM/CT balancing sub-routines. - if proxlb_config['vm_balancing_enable'] or app_args.best_node: - node_statistics, vm_statistics = balancing_vm_calculations(proxlb_config['vm_balancing_method'], proxlb_config['vm_balancing_mode'], proxlb_config['vm_balancing_mode_option'], node_statistics, vm_statistics, proxlb_config['vm_balanciness'], app_args, rebalance=False, processed_vms=[]) - node_statistics, vm_statistics = balancing_vm_maintenance(proxlb_config, app_args, node_statistics, vm_statistics) - node_statistics, vm_statistics = balancing_vm_affinity_groups(node_statistics, vm_statistics, proxlb_config['vm_balancing_method'], proxlb_config['vm_balancing_mode'],) - vm_output_statistics = run_rebalancing(api_object, vm_statistics, app_args, proxlb_config['vm_parallel_migrations'], 'vm') - - # Execute storage balancing sub-routines. - if proxlb_config['storage_balancing_enable']: - storage_statistics, vm_statistics = balancing_storage_calculations(proxlb_config['storage_balancing_method'], storage_statistics, vm_statistics, proxlb_config['storage_balanciness'], rebalance=False, processed_vms=[]) - storage_output_statistics = run_rebalancing(api_object, vm_statistics, app_args, proxlb_config['storage_parallel_migrations'], 'storage') - - # Generate balancing output - if proxlb_config['vm_balancing_enable'] or proxlb_config['storage_balancing_enable']: - run_output_rebalancing(app_args, vm_output_statistics, storage_output_statistics) - - # Validate for any errors. - post_validations() - - # Validate daemon service. - validate_daemon(proxlb_config['daemon'], proxlb_config['schedule']) - - -if __name__ == '__main__': - main() diff --git a/proxlb.conf b/proxlb.conf deleted file mode 100644 index 573be45..0000000 --- a/proxlb.conf +++ /dev/null @@ -1,23 +0,0 @@ -[proxmox] -api_host: hypervisor01.gyptazy.ch -api_user: root@pam -api_pass: FooBar -verify_ssl: 1 -[vm_balancing] -enable: 1 -method: memory -mode: used -maintenance_nodes: dummynode03,dummynode04 -ignore_nodes: dummynode01,dummynode02 -ignore_vms: testvm01,testvm02 -[storage_balancing] -enable: 0 -[update_service] -enable: 0 -[api] -enable: 0 -[service] -daemon: 1 -schedule: 24 -log_verbosity: CRITICAL -config_version: 3 diff --git a/proxlb/main.py b/proxlb/main.py new file mode 100644 index 0000000..b46801e --- /dev/null +++ b/proxlb/main.py @@ -0,0 +1,79 @@ +""" +ProxLB is a load balancing tool for Proxmox Virtual Environment (PVE) clusters. +It connects to the Proxmox API, retrieves information about nodes, guests, and groups, +and performs calculations to determine the optimal distribution of resources across the +cluster. The tool supports daemon mode for continuous operation and can log metrics and +perform balancing actions based on the configuration provided. It also includes a CLI +parser for handling command-line arguments and a custom logger for systemd integration. +""" + +import logging +from utils.logger import SystemdLogger +from utils.cli_parser import CliParser +from utils.config_parser import ConfigParser +from utils.proxmox_api import ProxmoxApi +from models.nodes import Nodes +from models.guests import Guests +from models.groups import Groups +from models.calculations import Calculations +from models.balancing import Balancing +from utils.helper import Helper + + +def main(): + """ + ProxLB main function + """ + # Initialize logging handler + logger = SystemdLogger(level=logging.INFO) + + # Parses arguments passed from the CLI + cli_parser = CliParser() + cli_args = cli_parser.parse_args() + Helper.get_version(cli_args.version) + + # Parse ProxLB config file + config_parser = ConfigParser(cli_args.config) + proxlb_config = config_parser.get_config() + + # Update log level from config and fallback to INFO if not defined + logger.set_log_level(proxlb_config.get('service', {}).get('log_level', 'INFO')) + + # Connect to Proxmox API & create API object + proxmox_api = ProxmoxApi(proxlb_config) + + # Overwrite password after creating the API object + proxlb_config["proxmox_api"]["pass"] = "********" + + while True: + # Get all required objects from the Proxmox cluster + meta = {"meta": proxlb_config} + nodes = Nodes.get_nodes(proxmox_api, proxlb_config) + guests = Guests.get_guests(proxmox_api, nodes) + groups = Groups.get_groups(guests, nodes) + + # Merge obtained objects from the Proxmox cluster for further usage + proxlb_data = {**meta, **nodes, **guests, **groups} + Helper.log_node_metrics(proxlb_data) + + # Update the initial node resource assignments + # by the previously created groups. + Calculations.set_node_assignments(proxlb_data) + Calculations.get_most_free_node(proxlb_data, cli_args.best_node) + Calculations.relocate_guests_on_maintenance_nodes(proxlb_data) + Calculations.get_balanciness(proxlb_data) + Calculations.relocate_guests(proxlb_data) + Helper.log_node_metrics(proxlb_data, init=False) + + # Perform balancing actions via Proxmox API + if not cli_args.dry_run: + Balancing(proxmox_api, proxlb_data) + + # Validate daemon mode + Helper.get_daemon_mode(proxlb_config) + + logger.debug(f"Finished: __main__") + + +if __name__ == "__main__": + main() diff --git a/proxlb/models/__init__.py b/proxlb/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/proxlb/models/balancing.py b/proxlb/models/balancing.py new file mode 100644 index 0000000..b3a9feb --- /dev/null +++ b/proxlb/models/balancing.py @@ -0,0 +1,171 @@ +""" +The balancing class is responsible for processing workloads on Proxmox clusters. +The previously generated data (hold in proxlb_data) will processed and guests and +other supported types will be moved across Proxmox clusters based on the defined +values by an operator. +""" + +import proxmoxer +import time +from utils.logger import SystemdLogger +from typing import Dict, Any + +logger = SystemdLogger() + + +class Balancing: + """ + The balancing class is responsible for processing workloads on Proxmox clusters. + The previously generated data (hold in proxlb_data) will processed and guests and + other supported types will be moved across Proxmox clusters based on the defined + values by an operator. + """ + + def __init__(self, proxmox_api: any, proxlb_data: Dict[str, Any]): + """ + Initializes the Balancing class with the provided ProxLB data. + + Args: + proxlb_data (dict): The data required for balancing VMs and CTs. + """ + for guest_name, guest_meta in proxlb_data["guests"].items(): + + if guest_meta["node_current"] != guest_meta["node_target"]: + guest_id = guest_meta["id"] + guest_node_current = guest_meta["node_current"] + guest_node_target = guest_meta["node_target"] + + # VM Balancing + if guest_meta["type"] == "vm": + self.exec_rebalancing_vm(proxmox_api, proxlb_data, guest_name) + + # CT Balancing + elif guest_meta["type"] == "ct": + self.exec_rebalancing_ct(proxmox_api, proxlb_data, guest_name) + + # Hopefully never reaching, but should be catched + else: + logger.critical(f"Balancing: Got unexpected guest type: {guest_meta['type']}. Cannot proceed guest: {guest_meta['name']}.") + + def exec_rebalancing_vm(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None: + """ + Executes the rebalancing of a virtual machine (VM) to a new node within the cluster. + This function initiates the migration of a specified VM to a target node as part of the + load balancing process. It logs the migration process and handles any exceptions that + may occur during the migration. + Args: + proxmox_api (object): The Proxmox API client instance used to interact with the Proxmox cluster. + proxlb_data (dict): A dictionary containing data related to the ProxLB load balancing configuration. + guest_name (str): The name of the guest VM to be migrated. + Raises: + proxmox_api.core.ResourceException: If an error occurs during the migration process. + Returns: + None + """ + logger.debug("Starting: exec_rebalancing_vm.") + guest_id = proxlb_data["guests"][guest_name]["id"] + guest_node_current = proxlb_data["guests"][guest_name]["node_current"] + guest_node_target = proxlb_data["guests"][guest_name]["node_target"] + + if proxlb_data["meta"]["balancing"].get("live", True): + online_migration = 1 + else: + online_migration = 0 + + if proxlb_data["meta"]["balancing"].get("with_local_disks", True): + with_local_disks = 1 + else: + with_local_disks = 0 + + migration_options = { + 'target': {guest_node_target}, + 'online': online_migration, + 'with-local-disks': with_local_disks + } + + try: + logger.debug(f"Balancing: Starting to migrate guest {guest_name} of type VM.") + job_id = proxmox_api.nodes(guest_node_current).qemu(guest_id).migrate().post(**migration_options) + job = self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_node_current, job_id) + except proxmoxer.core.ResourceException as proxmox_api_error: + logger.critical(f"Balancing: Failed to migrate guest {guest_name} of type CT due to some Proxmox errors. Please check if resource is locked or similar.") + + logger.debug("Finished: exec_rebalancing_vm.") + + def exec_rebalancing_ct(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None: + """ + Executes the rebalancing of a container (CT) to a new node within the cluster. + This function initiates the migration of a specified CT to a target node as part of the + load balancing process. It logs the migration process and handles any exceptions that + may occur during the migration. + Args: + proxmox_api (object): The Proxmox API client instance used to interact with the Proxmox cluster. + proxlb_data (dict): A dictionary containing data related to the ProxLB load balancing configuration. + guest_name (str): The name of the guest CT to be migrated. + Raises: + proxmox_api.core.ResourceException: If an error occurs during the migration process. + Returns: + None + """ + logger.debug("Starting: exec_rebalancing_ct.") + guest_id = proxlb_data["guests"][guest_name]["id"] + guest_node_current = proxlb_data["guests"][guest_name]["node_current"] + guest_node_target = proxlb_data["guests"][guest_name]["node_target"] + + try: + logger.debug(f"Balancing: Starting to migrate guest {guest_name} of type CT.") + job_id = proxmox_api.nodes(guest_node_current).lxc(guest_id).migrate().post(target=guest_node_target, restart=1) + job = self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_node_current, job_id) + except proxmoxer.core.ResourceException as proxmox_api_error: + logger.critical(f"Balancing: Failed to migrate guest {guest_name} of type CT due to some Proxmox errors. Please check if resource is locked or similar.") + + logger.debug("Finished: exec_rebalancing_ct.") + + def get_rebalancing_job_status(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str, guest_current_node: str, job_id: int, retry_counter: int = 1) -> bool: + """ + Monitors the status of a rebalancing job on a Proxmox node until it completes or a timeout is reached. + + Args: + proxmox_api (object): The Proxmox API client instance. + proxlb_data (dict): The ProxLB configuration data. + guest_name (str): The name of the guest (virtual machine) being rebalanced. + guest_current_node (str): The current node where the guest is running. + job_id (str): The ID of the rebalancing job to monitor. + retry_counter (int, optional): The current retry count. Defaults to 1. + + Returns: + bool: True if the job completed successfully, False otherwise. + """ + logger.debug("Starting: get_rebalancing_job_status.") + # Parallel migrations can take a huge time and create a higher load, if not defined by an + # operator we will use a sequential mode by default + if not proxlb_data["meta"]["balancing"].get("parallel", False): + job = proxmox_api.nodes(guest_current_node).tasks(job_id).status().get() + + # Watch job id until it finalizes + if job["status"] == "running": + # Do not hammer the API while + # watching the job status + time.sleep(10) + retry_counter += 1 + + # Run recursion until we hit the soft-limit of maximum migration time for a guest + if retry_counter < proxlb_data["meta"]["balancing"].get("max_job_validation", 1800): + logger.debug(f"Balancing: Job ID {job_id} (guest: {guest_name}) for migration is still running... (Run: {retry_counter})") + self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_current_node, job_id, retry_counter) + else: + logger.warning(f"Balancing: Job ID {job_id} (guest: {guest_name}) for migration took too long. Please check manually.") + logger.debug("Finished: get_rebalancing_job_status.") + return False + + # Validate job output for errors when finished + if job["status"] == "stopped": + + if job["exitstatus"] == "OK": + logger.debug(f"Balancing: Job ID {job_id} (guest: {guest_name}) was sucessfully.") + logger.debug("Finished: get_rebalancing_job_status.") + return True + else: + logger.critical(f"Balancing: Job ID {job_id} (guest: {guest_name}) went into an error! Please check manually.") + logger.debug("Finished: get_rebalancing_job_status.") + return False diff --git a/proxlb/models/calculations.py b/proxlb/models/calculations.py new file mode 100644 index 0000000..10765ae --- /dev/null +++ b/proxlb/models/calculations.py @@ -0,0 +1,308 @@ +""" +The calculation class is responsible for handling the balancing of virtual machines (VMs) +and containers (CTs) across all available nodes in a Proxmox cluster. It provides methods +to calculate the optimal distribution of VMs and CTs based on the provided data. +""" + +import sys +from typing import Dict, Any +from utils.logger import SystemdLogger + +logger = SystemdLogger() + + +class Calculations: + """ + The calculation class is responsible for handling the balancing of virtual machines (VMs) + and containers (CTs) across all available nodes in a Proxmox cluster. It provides methods + to calculate the optimal distribution of VMs and CTs based on the provided data. + """ + + def __init__(self, proxlb_data: Dict[str, Any]): + """ + Initializes the Calculation class with the provided ProxLB data. + + Args: + proxlb_data (Dict[str, Any]): The data required for balancing VMs and CTs. + """ + + @staticmethod + def set_node_assignments(proxlb_data: Dict[str, Any]) -> Dict[str, Any]: + """ + Set the assigned ressources of the nodes based on the current assigned + guest resources by their created groups as an initial base. + + Args: + proxlb_data (Dict[str, Any]): The data holding all current statistics. + + Returns: + Dict[str, Any]: Updated ProxLB data of nodes section with updated node assigned values. + """ + logger.debug("Starting: set_node_assignments.") + for group_name, group_meta in proxlb_data["groups"]["affinity"].items(): + + for guest_name in group_meta["guests"]: + guest_node_current = proxlb_data["guests"][guest_name]["node_current"] + # Update Hardware assignments + # Update assigned values for the current node + proxlb_data["nodes"][guest_node_current]["cpu_assigned"] += proxlb_data["guests"][guest_name]["cpu_total"] + proxlb_data["nodes"][guest_node_current]["memory_assigned"] += proxlb_data["guests"][guest_name]["memory_total"] + proxlb_data["nodes"][guest_node_current]["disk_assigned"] += proxlb_data["guests"][guest_name]["disk_total"] + # Update assigned percentage values for the current node + proxlb_data["nodes"][guest_node_current]["cpu_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["cpu_assigned"] / proxlb_data["nodes"][guest_node_current]["cpu_total"] * 100 + proxlb_data["nodes"][guest_node_current]["memory_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["memory_assigned"] / proxlb_data["nodes"][guest_node_current]["memory_total"] * 100 + proxlb_data["nodes"][guest_node_current]["disk_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["disk_assigned"] / proxlb_data["nodes"][guest_node_current]["disk_total"] * 100 + + logger.debug("Finished: set_node_assignments.") + + @staticmethod + def get_balanciness(proxlb_data: Dict[str, Any]) -> Dict[str, Any]: + """ + Get the blanaciness for further actions where the highest and lowest + usage or assignments of Proxmox nodes are compared. Based on the users + provided balanciness delta the balancing will be performed. + + Args: + proxlb_data (Dict[str, Any]): The data holding all content of all objects. + Returns: + Dict[str, Any]: Updated meta data section of the balanciness action defined + as a bool. + """ + logger.debug("Starting: get_balanciness.") + proxlb_data["meta"]["balancing"]["balance"] = False + + if len(proxlb_data["groups"]) > 0: + method = proxlb_data["meta"]["balancing"].get("method", "memory") + mode = proxlb_data["meta"]["balancing"].get("mode", "used") + balanciness = proxlb_data["meta"]["balancing"].get("balanciness", 10) + method_value = [node_meta[f"{method}_{mode}_percent"] for node_meta in proxlb_data["nodes"].values()] + method_value_highest = max(method_value) + method_value_lowest = min(method_value) + + if method_value_highest - method_value_lowest > balanciness: + proxlb_data["meta"]["balancing"]["balance"] = True + logger.debug(f"Guest balancing is required. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.") + logger.critical(f"Guest balancing is required. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.") + else: + logger.debug(f"Guest balancing is ok. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.") + logger.critical(f"Guest balancing is ok. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.") + + else: + logger.warning("No guests for balancing found.") + + logger.debug("Finished: get_balanciness.") + + @staticmethod + def get_most_free_node(proxlb_data: Dict[str, Any], return_node: bool = False) -> Dict[str, Any]: + """ + Get the name of the Proxmox node in the cluster with the most free resources based on + the user defined method (e.g.: memory) and mode (e.g.: used). + + Args: + proxlb_data (Dict[str, Any]): The data holding all content of all objects. + return_node (bool): The indicator to simply return the best node for further + assignments. + + Returns: + Dict[str, Any]: Updated meta data section of the node with the most free resources that should + be used for the next balancing action. + """ + logger.debug("Starting: get_most_free_node.") + proxlb_data["meta"]["balancing"]["balance_next_node"] = "" + + # Do not include nodes that are marked in 'maintenance' + filtered_nodes = [node for node in proxlb_data["nodes"].values() if not node["maintenance"]] + lowest_usage_node = min(filtered_nodes, key=lambda x: x["memory_used_percent"]) + proxlb_data["meta"]["balancing"]["balance_reason"] = 'resources' + proxlb_data["meta"]["balancing"]["balance_next_node"] = lowest_usage_node["name"] + + # If executed to simply get the best node for further usage, we return + # the best node on stdout and gracefully exit here + if return_node: + print(lowest_usage_node["name"]) + sys.exit(0) + + logger.debug("Finished: get_most_free_node.") + + @staticmethod + def relocate_guests_on_maintenance_nodes(proxlb_data: Dict[str, Any]): + """ + Relocates guests that are currently on nodes marked for maintenance to + nodes with the most available resources. + + This function iterates over all guests on maintenance nodes and attempts + to relocate them to nodes with the most free resources that are not in + maintenance mode. It updates the node resources accordingly and logs + warnings if the balancing may not be perfect due to the maintenance + status of the original node. + + Args: + proxlb_data (Dict[str, Any]): The data holding all content of all objects. + Returns: + None + """ + logger.debug("Starting: get_most_free_node.") + proxlb_data["meta"]["balancing"]["balance_next_guest"] = "" + + for guest_name in proxlb_data["groups"]["maintenance"]: + # Update the node with the most free nodes which is + # not in a maintenance + proxlb_data["meta"]["balancing"]["balance_next_guest"] = guest_name + Calculations.get_most_free_node(proxlb_data) + Calculations.update_node_resources(proxlb_data) + logger.warning(f"Warning: Balancing may not be perfect because guest {guest_name} was located on a node which is in maintenance mode.") + + logger.debug("Finished: get_most_free_node.") + + @staticmethod + def relocate_guests(proxlb_data: Dict[str, Any]): + """ + Relocates guests within the provided data structure to ensure affinity groups are + placed on nodes with the most free resources. + + This function iterates over each affinity group in the provided data, identifies + the node with the most free resources, and migrates all guests within the group + to that node. It updates the node resources accordingly. + + Args: + proxlb_data (Dict[str, Any]): The data holding all content of all objects. + Returns: + None + """ + logger.debug("Starting: relocate_guests.") + if proxlb_data["meta"]["balancing"]["balance"] or proxlb_data["meta"]["balancing"]["force"]: + + if proxlb_data["meta"]["balancing"].get("balance", False): + logger.debug("Balancing of guests will be performt. Reason: balanciness") + + if proxlb_data["meta"]["balancing"].get("force", False): + logger.debug("Balancing of guests will be performt. Reason: force balancing") + + for group_name in proxlb_data["groups"]["affinity"]: + + # We get initially the node with the most free resources and then + # migrate all guests within the group to that node to ensure the + # affinity. + Calculations.get_most_free_node(proxlb_data) + + for guest_name in proxlb_data["groups"]["affinity"][group_name]["guests"]: + proxlb_data["meta"]["balancing"]["balance_next_guest"] = guest_name + Calculations.val_anti_affinity(proxlb_data, guest_name) + Calculations.update_node_resources(proxlb_data) + + logger.debug("Finished: relocate_guests.") + + @staticmethod + def val_anti_affinity(proxlb_data: Dict[str, Any], guest_name: str): + """ + Validates and assigns nodes to guests based on anti-affinity rules. + + This function iterates over all defined anti-affinity groups in the provided + `proxlb_data` and checks if the specified `guest_name` is included in any of + these groups. If the guest is included and has not been processed yet, it + attempts to assign an unused and non-maintenance node to the guest, ensuring + that the anti-affinity rules are respected. + + Parameters: + proxlb_data (Dict[str, Any]): The data holding all content of all objects. + guest_name (str): The name of the guest to be validated and assigned a node. + + Returns: + None + """ + logger.debug("Starting: val_anti_affinity.") + # Start by interating over all defined anti-affinity groups + for group_name in proxlb_data["groups"]["anti_affinity"].keys(): + + # Validate if the provided guest ist included in the anti-affinity group + if guest_name in proxlb_data["groups"]["anti_affinity"][group_name]['guests'] and not proxlb_data["guests"][guest_name]["processed"]: + logger.debug(f"Anti-Affinity: Guest: {guest_name} is included in anti-affinity group: {group_name}.") + + # Iterate over all available nodes + for node_name in proxlb_data["nodes"].keys(): + + # Only select node if it was not used before and is not in a + # maintenance mode. Afterwards, add it to the list of already + # used nodes for the current anti-affinity group + if node_name not in proxlb_data["groups"]["anti_affinity"][group_name]["used_nodes"]: + + if not proxlb_data["nodes"][node_name]["maintenance"]: + # If the node has not been used yet, we assign this node to the guest + proxlb_data["meta"]["balancing"]["balance_next_node"] = node_name + proxlb_data["groups"]["anti_affinity"][group_name]["used_nodes"].append(node_name) + logger.debug(f"Node: {node_name} marked as used for anti-affinity group: {group_name} with guest {guest_name}") + break + + else: + logger.critical(f"Node: {node_name} already got used for anti-affinity group:: {group_name}. (Tried for guest: {guest_name})") + + else: + logger.debug(f"Guest: {guest_name} is not included in anti-affinity group: {group_name}. Skipping.") + + logger.debug("Finished: val_anti_affinity.") + + @staticmethod + def update_node_resources(proxlb_data): + """ + Updates the resource allocation and usage statistics for nodes when a guest + is moved from one node to another. + + Parameters: + proxlb_data (dict): A dictionary containing information about the nodes and + guests, including their resource allocations and usage. + + The function performs the following steps: + 1. Retrieves the guest name, current node, and target node from the provided data. + 2. Updates the resource allocations and usage statistics for the target node by + adding the resources of the moved guest. + 3. Updates the resource allocations and usage statistics for the current node by + subtracting the resources of the moved guest. + 4. Logs the start and end of the resource update process, as well as the movement + of the guest from the current node to the target node. + """ + logger.debug("Starting: update_node_resources.") + guest_name = proxlb_data["meta"]["balancing"]["balance_next_guest"] + node_current = proxlb_data["guests"][guest_name]["node_current"] + node_target = proxlb_data["meta"]["balancing"]["balance_next_node"] + + # Update resources for the target node by the moved guest resources + # Add assigned resources to the target node + proxlb_data["nodes"][node_target]["cpu_assigned"] += proxlb_data["guests"][guest_name]["cpu_total"] + proxlb_data["nodes"][node_target]["memory_assigned"] += proxlb_data["guests"][guest_name]["memory_total"] + proxlb_data["nodes"][node_target]["disk_assigned"] += proxlb_data["guests"][guest_name]["disk_total"] + # Update the assigned percentages of assigned resources for the target node + proxlb_data["nodes"][node_target]["cpu_assigned_percent"] = proxlb_data["nodes"][node_target]["cpu_assigned"] / proxlb_data["nodes"][node_target]["cpu_total"] * 100 + proxlb_data["nodes"][node_target]["memory_assigned_percent"] = proxlb_data["nodes"][node_target]["memory_assigned"] / proxlb_data["nodes"][node_target]["memory_total"] * 100 + proxlb_data["nodes"][node_target]["disk_assigned_percent"] = proxlb_data["nodes"][node_target]["disk_assigned"] / proxlb_data["nodes"][node_target]["disk_total"] * 100 + # Add used resources to the target node + proxlb_data["nodes"][node_target]["cpu_used"] += proxlb_data["guests"][guest_name]["cpu_used"] + proxlb_data["nodes"][node_target]["memory_used"] += proxlb_data["guests"][guest_name]["memory_used"] + proxlb_data["nodes"][node_target]["disk_used"] += proxlb_data["guests"][guest_name]["disk_used"] + # Update the used percentages of usage resources for the target node + proxlb_data["nodes"][node_target]["cpu_used_percent"] = proxlb_data["nodes"][node_target]["cpu_used"] / proxlb_data["nodes"][node_target]["cpu_total"] * 100 + proxlb_data["nodes"][node_target]["memory_used_percent"] = proxlb_data["nodes"][node_target]["memory_used"] / proxlb_data["nodes"][node_target]["memory_total"] * 100 + proxlb_data["nodes"][node_target]["disk_used_percent"] = proxlb_data["nodes"][node_target]["disk_used"] / proxlb_data["nodes"][node_target]["disk_total"] * 100 + + # Update resources for the current node by the moved guest resources + # Add assigned resources to the target node + proxlb_data["nodes"][node_current]["cpu_assigned"] -= proxlb_data["guests"][guest_name]["cpu_total"] + proxlb_data["nodes"][node_current]["memory_assigned"] -= proxlb_data["guests"][guest_name]["memory_total"] + proxlb_data["nodes"][node_current]["disk_assigned"] -= proxlb_data["guests"][guest_name]["disk_total"] + # Update the assigned percentages of assigned resources for the target node + proxlb_data["nodes"][node_current]["cpu_assigned_percent"] = proxlb_data["nodes"][node_current]["cpu_assigned"] / proxlb_data["nodes"][node_current]["cpu_total"] * 100 + proxlb_data["nodes"][node_current]["memory_assigned_percent"] = proxlb_data["nodes"][node_current]["memory_assigned"] / proxlb_data["nodes"][node_current]["memory_total"] * 100 + proxlb_data["nodes"][node_current]["disk_assigned_percent"] = proxlb_data["nodes"][node_current]["disk_assigned"] / proxlb_data["nodes"][node_current]["disk_total"] * 100 + # Add used resources to the target node + proxlb_data["nodes"][node_current]["cpu_used"] -= proxlb_data["guests"][guest_name]["cpu_used"] + proxlb_data["nodes"][node_current]["memory_used"] -= proxlb_data["guests"][guest_name]["memory_used"] + proxlb_data["nodes"][node_current]["disk_used"] -= proxlb_data["guests"][guest_name]["disk_used"] + # Update the used percentages of usage resources for the target node + proxlb_data["nodes"][node_current]["cpu_used_percent"] = proxlb_data["nodes"][node_current]["cpu_used"] / proxlb_data["nodes"][node_current]["cpu_total"] * 100 + proxlb_data["nodes"][node_current]["memory_used_percent"] = proxlb_data["nodes"][node_current]["memory_used"] / proxlb_data["nodes"][node_current]["memory_total"] * 100 + proxlb_data["nodes"][node_current]["disk_used_percent"] = proxlb_data["nodes"][node_current]["disk_used"] / proxlb_data["nodes"][node_current]["disk_total"] * 100 + + # Assign guest to the new target node + proxlb_data["guests"][guest_name]["node_target"] = node_target + logger.debug(f"Set guest {guest_name} from node {node_current} to node {node_target}.") + + logger.debug("Finished: update_node_resources.") diff --git a/proxlb/models/groups.py b/proxlb/models/groups.py new file mode 100644 index 0000000..1dc12de --- /dev/null +++ b/proxlb/models/groups.py @@ -0,0 +1,110 @@ +""" +The groups class is responsible for handling the correlations between the guests +and their groups like affinity and anti-affinity groups. To ensure a proper balancing +guests will ge grouped and then evaluated for further balancing. +""" + +from typing import Dict, Any +from utils.logger import SystemdLogger +from utils.helper import Helper + +logger = SystemdLogger() + + +class Groups: + """ + The groups class is responsible for handling the correlations between the guests + and their groups like affinity and anti-affinity groups. To ensure a proper balancing + guests will ge grouped and then evaluated for further balancing. + """ + + def __init__(self, proxlb_data: Dict[str, Any]): + """ + Initializes the Groups class with the provided ProxLB data. + + Args: + proxlb_data (Dict[str, Any]): The data required for balancing VMs and CTs. + """ + + @staticmethod + def get_groups(guests: Dict[str, Any], nodes: Dict[str, Any]) -> Dict[str, Any]: + """ + Generates and returns a dictionary of affinity and anti-affinity groups based on the provided data. + + Args: + guests (Dict[str, Any]): A dictionary containing the guest data. + nodes (Dict[str, Any]): A dictionary containing the nodes data. + + Returns: + Dict[str, Any]: A dictionary containing the created groups that includes: + * Affinity groups (or a randon and uniq group) + * Anti-affinity groups + * A list of guests that are currently placed on a node which + is defined to be in maintenance. + """ + logger.debug("Starting: get_groups.") + groups = {'groups': {'affinity': {}, 'anti_affinity': {}, 'maintenance': []}} + + for guest_name, guest_meta in guests["guests"].items(): + # Create affinity grouping + # Use an affinity group if available for the guest + if len(guest_meta["affinity_groups"]) > 0: + for affinity_group in guest_meta["affinity_groups"]: + group_name = affinity_group + logger.debug(f'Affinity group {affinity_group} for {guest_name} will be used.') + else: + # Generate a random uniq group name for the guest if + # the guest does not belong to any affinity group + random_group = Helper.get_uuid_string() + group_name = random_group + logger.debug(f'Random uniq group {random_group} for {guest_name} will be used.') + + if not groups["groups"]["affinity"].get(group_name, False): + # Create group template with initial guest meta information + groups["groups"]["affinity"][group_name] = {} + groups["groups"]["affinity"][group_name]["guests"] = [] + groups["groups"]["affinity"][group_name]["guests"].append(guest_name) + groups["groups"]["affinity"][group_name]["counter"] = 1 + # Create groups resource template by the guests resources + groups["groups"]["affinity"][group_name]["cpu_total"] = guest_meta["cpu_total"] + groups["groups"]["affinity"][group_name]["cpu_used"] = guest_meta["cpu_used"] + groups["groups"]["affinity"][group_name]["memory_total"] = guest_meta["memory_total"] + groups["groups"]["affinity"][group_name]["memory_used"] = guest_meta["cpu_used"] + groups["groups"]["affinity"][group_name]["disk_total"] = guest_meta["disk_total"] + groups["groups"]["affinity"][group_name]["disk_used"] = guest_meta["cpu_used"] + else: + # Update group templates by guest meta information + groups["groups"]["affinity"][group_name]["guests"].append(guest_name) + groups["groups"]["affinity"][group_name]["counter"] += 1 + # Update group resources by guest resources + groups["groups"]["affinity"][group_name]["cpu_total"] += guest_meta["cpu_total"] + groups["groups"]["affinity"][group_name]["cpu_used"] += guest_meta["cpu_used"] + groups["groups"]["affinity"][group_name]["memory_total"] += guest_meta["memory_total"] + groups["groups"]["affinity"][group_name]["memory_used"] += guest_meta["cpu_used"] + groups["groups"]["affinity"][group_name]["disk_total"] += guest_meta["disk_total"] + groups["groups"]["affinity"][group_name]["disk_used"] += guest_meta["cpu_used"] + + # Create anti-affinity grouping + if len(guest_meta["anti_affinity_groups"]) > 0: + for anti_affinity_group in guest_meta["anti_affinity_groups"]: + anti_affinity_group_name = anti_affinity_group + logger.debug(f'Anti-affinity group {anti_affinity_group_name} for {guest_name} will be used.') + + if not groups["groups"]["anti_affinity"].get(anti_affinity_group_name, False): + groups["groups"]["anti_affinity"][anti_affinity_group_name] = {} + groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"] = [] + groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"].append(guest_name) + groups["groups"]["anti_affinity"][anti_affinity_group_name]["counter"] = 1 + groups["groups"]["anti_affinity"][anti_affinity_group_name]["used_nodes"] = [] + else: + groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"].append(guest_name) + groups["groups"]["anti_affinity"][anti_affinity_group_name]["counter"] += 1 + + # Create grouping of guests that are currently located on nodes that are + # marked as in maintenance and must be migrated + if nodes["nodes"][guest_meta["node_current"]]["maintenance"]: + logger.debug(f'{guest_name} will be migrated to another node because the underlying node {guest_meta["node_current"]} is defined to be in maintenance.') + groups["groups"]["maintenance"].append(guest_name) + + logger.debug("Finished: get_groups.") + return groups diff --git a/proxlb/models/guests.py b/proxlb/models/guests.py new file mode 100644 index 0000000..b015e45 --- /dev/null +++ b/proxlb/models/guests.py @@ -0,0 +1,97 @@ +""" +The Guests class retrieves all running guests on the Proxmox cluster across all available nodes. +It handles both VM and CT guest types, collecting their resource metrics. +""" + +from typing import Dict, Any +from utils.logger import SystemdLogger +from models.tags import Tags + +logger = SystemdLogger() + + +class Guests: + """ + The Guests class retrieves all running guests on the Proxmox cluster across all available nodes. + It handles both VM and CT guest types, collecting their resource metrics. + """ + def __init__(self): + """ + Initializes the Guests class with the provided ProxLB data. + """ + + @staticmethod + def get_guests(proxmox_api: any, nodes: Dict[str, Any]) -> Dict[str, Any]: + """ + Get metrics of all guests in a Proxmox cluster. + + This method retrieves metrics for all running guests (both VMs and CTs) across all nodes in the Proxmox cluster. + It iterates over each node and collects resource metrics for each running guest, including CPU, memory, and disk usage. + Additionally, it retrieves tags and affinity/anti-affinity groups for each guest. + + Args: + proxmox_api (any): The Proxmox API client instance. + nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster. + + Returns: + Dict[str, Any]: A dictionary containing metrics and information for all running guests. + """ + logger.debug("Starting: get_guests.") + guests = {"guests": {}} + + # Guest objects are always only in the scope of a node. + # Therefore, we need to iterate over all nodes to get all guests. + for node in nodes['nodes'].keys(): + + # VM objects: Iterate over all VMs on the current node by the qemu API object. + # Unlike the nodes we need to keep them even when being ignored to create proper + # resource metrics for rebalancing to ensure that we do not overprovisiong the node. + for guest in proxmox_api.nodes(node).qemu.get(): + if guest['status'] == 'running': + guests['guests'][guest['name']] = {} + guests['guests'][guest['name']]['name'] = guest['name'] + guests['guests'][guest['name']]['cpu_total'] = guest['cpus'] + guests['guests'][guest['name']]['cpu_used'] = guest['cpu'] + guests['guests'][guest['name']]['memory_total'] = guest['maxmem'] + guests['guests'][guest['name']]['memory_used'] = guest['mem'] + guests['guests'][guest['name']]['disk_total'] = guest['maxdisk'] + guests['guests'][guest['name']]['disk_used'] = guest['disk'] + guests['guests'][guest['name']]['id'] = guest['vmid'] + guests['guests'][guest['name']]['node_current'] = node + guests['guests'][guest['name']]['node_target'] = node + guests['guests'][guest['name']]['processed'] = False + guests['guests'][guest['name']]['tags'] = Tags.get_tags_from_guests(proxmox_api, node, guest['vmid'], 'vm') + guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['type'] = 'vm' + else: + logger.debug(f'Metric for VM {guest["name"]} ignored because VM is not running.') + + # CT objects: Iterate over all VMs on the current node by the lxc API object. + # Unlike the nodes we need to keep them even when being ignored to create proper + # resource metrics for rebalancing to ensure that we do not overprovisiong the node. + for guest in proxmox_api.nodes(node).lxc.get(): + if guest['status'] == 'running': + guests['guests'][guest['name']] = {} + guests['guests'][guest['name']]['name'] = guest['name'] + guests['guests'][guest['name']]['cpu_total'] = guest['cpus'] + guests['guests'][guest['name']]['cpu_used'] = guest['cpu'] + guests['guests'][guest['name']]['memory_total'] = guest['maxmem'] + guests['guests'][guest['name']]['memory_used'] = guest['mem'] + guests['guests'][guest['name']]['disk_total'] = guest['maxdisk'] + guests['guests'][guest['name']]['disk_used'] = guest['disk'] + guests['guests'][guest['name']]['id'] = guest['vmid'] + guests['guests'][guest['name']]['node_current'] = node + guests['guests'][guest['name']]['node_target'] = node + guests['guests'][guest['name']]['processed'] = False + guests['guests'][guest['name']]['tags'] = Tags.get_tags_from_guests(proxmox_api, node, guest['vmid'], 'ct') + guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags']) + guests['guests'][guest['name']]['type'] = 'ct' + else: + logger.debug(f'Metric for CT {guest["name"]} ignored because CT is not running.') + + logger.debug("Finished: get_guests.") + return guests diff --git a/proxlb/models/nodes.py b/proxlb/models/nodes.py new file mode 100644 index 0000000..e68b1e0 --- /dev/null +++ b/proxlb/models/nodes.py @@ -0,0 +1,123 @@ +""" +The Nodes class retrieves all running nodes in a Proxmox cluster +and collects their resource metrics. +""" + +from typing import Dict, Any +from utils.logger import SystemdLogger + +logger = SystemdLogger() + + +class Nodes: + """ + The Nodes class retrieves all running nodes in a Proxmox cluster + and collects their resource metrics. + """ + def __init__(self): + """ + Initializes the Nodes class with the provided ProxLB data. + """ + + @staticmethod + def get_nodes(proxmox_api: any, proxlb_config: Dict[str, Any]) -> Dict[str, Any]: + """ + Get metrics of all nodes in a Proxmox cluster. + + This method retrieves metrics for all available nodes in the Proxmox cluster. + It iterates over each node and collects resource metrics including CPU, memory, and disk usage. + + Args: + proxmox_api (any): The Proxmox API client instance. + nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster. + + Returns: + Dict[str, Any]: A dictionary containing metrics and information for all running nodes. + """ + logger.debug("Starting: get_nodes.") + nodes = {"nodes": {}} + + for node in proxmox_api.nodes.get(): + # Ignoring a node results into ignoring all placed guests on the ignored node! + if node["status"] == "online" and not Nodes.set_node_ignore(proxlb_config, node["node"]): + nodes["nodes"][node["node"]] = {} + nodes["nodes"][node["node"]]["name"] = node["node"] + nodes["nodes"][node["node"]]["maintenance"] = False + nodes["nodes"][node["node"]]["cpu_total"] = node["maxcpu"] + nodes["nodes"][node["node"]]["cpu_assigned"] = 0 + nodes["nodes"][node["node"]]["cpu_used"] = node["cpu"] + nodes["nodes"][node["node"]]["cpu_free"] = (node["maxcpu"]) - (node["cpu"] * node["maxcpu"]) + nodes["nodes"][node["node"]]["cpu_assigned_percent"] = nodes["nodes"][node["node"]]["cpu_assigned"] / nodes["nodes"][node["node"]]["cpu_total"] * 100 + nodes["nodes"][node["node"]]["cpu_free_percent"] = nodes["nodes"][node["node"]]["cpu_free"] / node["maxcpu"] * 100 + nodes["nodes"][node["node"]]["cpu_used_percent"] = nodes["nodes"][node["node"]]["cpu_used"] / node["maxcpu"] * 100 + nodes["nodes"][node["node"]]["memory_total"] = node["maxmem"] + nodes["nodes"][node["node"]]["memory_assigned"] = 0 + nodes["nodes"][node["node"]]["memory_used"] = node["mem"] + nodes["nodes"][node["node"]]["memory_free"] = node["maxmem"] - node["mem"] + nodes["nodes"][node["node"]]["memory_assigned_percent"] = nodes["nodes"][node["node"]]["memory_assigned"] / nodes["nodes"][node["node"]]["memory_total"] * 100 + nodes["nodes"][node["node"]]["memory_free_percent"] = nodes["nodes"][node["node"]]["memory_free"] / node["maxmem"] * 100 + nodes["nodes"][node["node"]]["memory_used_percent"] = nodes["nodes"][node["node"]]["memory_used"] / node["maxmem"] * 100 + nodes["nodes"][node["node"]]["disk_total"] = node["maxdisk"] + nodes["nodes"][node["node"]]["disk_assigned"] = 0 + nodes["nodes"][node["node"]]["disk_used"] = node["disk"] + nodes["nodes"][node["node"]]["disk_free"] = node["maxdisk"] - node["disk"] + nodes["nodes"][node["node"]]["disk_assigned_percent"] = nodes["nodes"][node["node"]]["disk_assigned"] / nodes["nodes"][node["node"]]["disk_total"] * 100 + nodes["nodes"][node["node"]]["disk_free_percent"] = nodes["nodes"][node["node"]]["disk_free"] / node["maxdisk"] * 100 + nodes["nodes"][node["node"]]["disk_used_percent"] = nodes["nodes"][node["node"]]["disk_used"] / node["maxdisk"] * 100 + + # Evaluate if node should be set to maintenance mode + if Nodes.set_node_maintenance(proxlb_config, node["node"]): + nodes["nodes"][node["node"]]["maintenance"] = True + + logger.debug("Finished: get_nodes.") + return nodes + + @staticmethod + def set_node_maintenance(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]: + """ + Set nodes to maintenance mode based on the provided configuration. + + This method updates the nodes dictionary to mark certain nodes as being in maintenance mode + based on the configuration provided in proxlb_config. + + Args: + proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration, including maintenance nodes. + node_name: (str): The current node name within the outer iteration. + + Returns: + Bool: Returns a bool if the provided node name is present in the maintenance section of the config file. + """ + logger.debug("Starting: set_node_maintenance.") + + if proxlb_config.get("proxmox_cluster", None).get("maintenance_nodes", None) is not None: + if len(proxlb_config.get("proxmox_cluster", {}).get("maintenance_nodes", [])) > 0: + if node_name in proxlb_config.get("proxmox_cluster", {}).get("maintenance_nodes", []): + logger.warning(f"Node: {node_name} has been set to maintenance mode.") + return True + + logger.debug("Finished: set_node_maintenance.") + + @staticmethod + def set_node_ignore(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]: + """ + Set nodes to be ignored based on the provided configuration. + + This method updates the nodes dictionary to mark certain nodes as being ignored + based on the configuration provided in proxlb_config. + + Args: + proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration, including maintenance nodes. + node_name: (str): The current node name within the outer iteration. + + Returns: + Bool: Returns a bool if the provided node name is present in the ignore section of the config file. + """ + logger.debug("Starting: set_node_ignore.") + + if proxlb_config.get("proxmox_cluster", None).get("ignore_nodes", None) is not None: + if len(proxlb_config.get("proxmox_cluster", {}).get("ignore_nodes", [])) > 0: + if node_name in proxlb_config.get("proxmox_cluster", {}).get("ignore_nodes", []): + logger.warning(f"Node: {node_name} has been set to be ignored. Not adding node!") + return True + + logger.debug("Finished: set_node_ignore.") diff --git a/proxlb/models/tags.py b/proxlb/models/tags.py new file mode 100644 index 0000000..f69ae4a --- /dev/null +++ b/proxlb/models/tags.py @@ -0,0 +1,130 @@ +""" +The Tags class retrieves all tags from guests of the type VM or CT running +in a Proxmox cluster and validates for affinity, anti-affinity and ignore +tags set for the guest in the Proxmox API. +""" + +import time +from typing import List +from utils.logger import SystemdLogger + +logger = SystemdLogger() + + +class Tags: + """ + The Tags class retrieves all tags from guests of the type VM or CT running + in a Proxmox cluster and validates for affinity, anti-affinity and ignore + tags set for the guest in the Proxmox API. + """ + def __init__(self): + """ + Initializes the Tags class. + """ + + @staticmethod + def get_tags_from_guests(proxmox_api: any, node: str, guest_id: int, guest_type: str) -> List[str]: + """ + Get tags for a guest from the Proxmox cluster by the API. + + This method retrieves all tags for a given guest from the Proxmox API which + is held in the guest_config. + + Args: + proxmox_api (any): The Proxmox API client instance. + node (str): The node name where the given guest is located. + guest_id (int): The internal Proxmox ID of the guest. + guest_type (str): The type (vm or ct) of the guest. + + Returns: + List: A list of all tags assoiciated with the given guest. + """ + logger.debug("Starting: get_tags_from_guests.") + time.sleep(0.1) + if guest_type == 'vm': + guest_config = proxmox_api.nodes(node).qemu(guest_id).config.get() + tags = guest_config.get("tags", []) + if guest_type == 'ct': + guest_config = proxmox_api.nodes(node).lxc(guest_id).config.get() + tags = guest_config.get("tags", []) + + if isinstance(tags, str): + tags = tags.split(";") + + logger.debug("Finished: get_tags_from_guests.") + return tags + + @staticmethod + def get_affinity_groups(tags: List[str]) -> List[str]: + """ + Get affinity tags for a guest from the Proxmox cluster by the API. + + This method retrieves all tags for a given guest and evaluates the + affinity tags which are required during the balancing calculations. + + Args: + tags (List): A list holding all defined tags for a given guest. + + Returns: + List: A list including all affinity tags for the given guest. + """ + logger.debug("Starting: get_affinity_groups.") + affinity_tags = [] + + if len(tags) > 0: + for tag in tags: + if tag.startswith("plb_affinity"): + affinity_tags.append(tag) + + logger.debug("Finished: get_affinity_groups.") + return affinity_tags + + @staticmethod + def get_anti_affinity_groups(tags: List[str]) -> List[str]: + """ + Get anti-affinity tags for a guest from the Proxmox cluster by the API. + + This method retrieves all tags for a given guest and evaluates the + anti-affinity tags which are required during the balancing calculations. + + Args: + tags (List): A list holding all defined tags for a given guest. + + Returns: + List: A list including all anti-affinity tags for the given guest.. + """ + logger.debug("Starting: get_anti_affinity_groups.") + anti_affinity_tags = [] + + if len(tags) > 0: + for tag in tags: + if tag.startswith("plb_anti_affinity"): + anti_affinity_tags.append(tag) + + logger.debug("Finished: get_anti_affinity_groups.") + return anti_affinity_tags + + @staticmethod + def get_ignore(tags: List[str]) -> bool: + """ + Validate for ignore tags of a guest from the Proxmox cluster by the API. + + This method retrieves all tags for a given guest and evaluates the + ignore tag which are required during the balancing calculations. + + Args: + tags (List): A list holding all defined tags for a given guest. + + Returns: + Bool: Returns a bool that indicates wether to ignore a guest or not. + """ + logger.debug("Starting: get_ignore.") + ignore_tag = False + + if len(tags) > 0: + for tag in tags: + if tag.startswith("plb_ignore"): + ignore_tag = True + + logger.debug("Finished: get_ignore.") + return ignore_tag diff --git a/proxlb/utils/__init__.py b/proxlb/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/proxlb/utils/cli_parser.py b/proxlb/utils/cli_parser.py new file mode 100644 index 0000000..d5e6b52 --- /dev/null +++ b/proxlb/utils/cli_parser.py @@ -0,0 +1,85 @@ +""" +The CliParser class handles the parsing of command-line interface (CLI) arguments. +""" + +import argparse +import utils.version +from utils.logger import SystemdLogger + +logger = SystemdLogger() + + +class CliParser: + """ + The CliParser class handles the parsing of command-line interface (CLI) arguments. + """ + def __init__(self): + """ + Initializes the CliParser class. + + This method sets up an argument parser for the command-line interface (CLI) with various options: + - `-c` or `--config`: Specifies the path to the configuration file. + - `-d` or `--dry-run`: Performs a dry-run without executing any actions. + - `-j` or `--json`: Returns a JSON of the VM movement. + - `-b` or `--best-node`: Returns the best next node. + - `-v` or `--version`: Returns the current ProxLB version. + + Logs the start and end of the initialization process. + """ + logger.debug("Starting: CliParser.") + + self.parser = argparse.ArgumentParser( + description=( + f"{utils.version.__app_name__} ({utils.version.__version__}): " + f"{utils.version.__app_desc__}" + ) + ) + + self.parser.add_argument( + "-c", "--config", + help="Path to the configuration file", + type=str, + required=False + ) + self.parser.add_argument( + "-d", "--dry-run", + help="Perform a dry-run without executing any actions", + action="store_true", + required=False + ) + self.parser.add_argument( + "-j", "--json", + help="Return a JSON of the VM movement", + action="store_true", + required=False + ) + self.parser.add_argument( + "-b", "--best-node", + help="Returns the best next node", + action="store_true", + required=False + ) + self.parser.add_argument( + "-v", "--version", + help="Returns the current ProxLB version", + action="store_true", + required=False + ) + logger.debug("Finished: CliParser.") + + def parse_args(self) -> argparse.Namespace: + """ + Parses and returns the parsed command-line interface (CLI) arguments. + + This method uses the argparse library to parse the arguments provided + via the command line. It logs the start and end of the parsing process, + as well as the parsed arguments for debugging purposes. + + Returns: + argparse.Namespace: An object containing the parsed CLI arguments. + """ + logger.debug("Starting: parse_args.") + logger.debug(self.parser.parse_args()) + + logger.debug("Finished: parse_args.") + return self.parser.parse_args() diff --git a/proxlb/utils/config_parser.py b/proxlb/utils/config_parser.py new file mode 100644 index 0000000..1af66ee --- /dev/null +++ b/proxlb/utils/config_parser.py @@ -0,0 +1,83 @@ +""" +The ConfigParser class handles the parsing of configuration file +from a given YAML file from any location. +""" + +import os +import sys +try: + import yaml + PYYAML_PRESENT = True +except ImportError: + PYYAML_PRESENT = False +from typing import Dict, Any +from utils.logger import SystemdLogger + + +if not PYYAML_PRESENT: + print("Error: The required library 'pyyaml' is not installed.") + sys.exit(1) + + +logger = SystemdLogger() + + +class ConfigParser: + """ + The ConfigParser class handles the parsing of configuration file + from a given YAML file from any location. + """ + def __init__(self, config_path: str): + """ + Initializes the configuration file parser and validates the config file. + """ + logger.debug("Starting: ConfigParser.") + self.config_path = self.test_config_path(config_path) + logger.debug("Finished: ConfigParser.") + + def test_config_path(self, config_path: str) -> None: + """ + Checks if configuration file is present at given config path. + """ + logger.debug("Starting: test_config_path.") + # Test for config file at given location + if config_path is not None: + + if os.path.exists(config_path): + logger.debug(f"The file {config_path} exists.") + else: + logger.error(f"The file {config_path} does not exist.") + sys.exit(1) + + # Test for config file at default location as a fallback + if config_path is None: + default_config_path = "/etc/proxlb/proxlb.yaml" + + if os.path.exists(default_config_path): + logger.debug(f"The file {default_config_path} exists.") + config_path = default_config_path + else: + print(f"The config file {default_config_path} does not exist.") + logger.critical(f"The config file {default_config_path} does not exist.") + sys.exit(1) + + logger.debug("Finished: test_config_path.") + return config_path + + def get_config(self) -> Dict[str, Any]: + """ + Parses and returns CLI arguments. + """ + logger.debug("Starting: get_config.") + logger.info(f"Using config path: {self.config_path}") + + try: + with open(self.config_path, "r", encoding="utf-8") as config_file: + config_data = yaml.load(config_file, Loader=yaml.FullLoader) + return config_data + except yaml.YAMLError as exception_error: + print(f"Error loading YAML file: {exception_error}") + logger.critical(f"Error loading YAML file: {exception_error}") + sys.exit(1) + + logger.debug("Finished: get_config.") diff --git a/proxlb/utils/helper.py b/proxlb/utils/helper.py new file mode 100644 index 0000000..1d6df6c --- /dev/null +++ b/proxlb/utils/helper.py @@ -0,0 +1,105 @@ +""" +The Helper class provides some basic helper functions to not mess up the code in other +classes. +""" + +import uuid +import sys +import time +import utils.version +from utils.logger import SystemdLogger +from typing import Dict, Any + +logger = SystemdLogger() + + +class Helper: + """ + The Helper class provides some basic helper functions to not mess up the code in other + classes. + """ + def __init__(self): + """ + Initializes the general Helper clas. + """ + + @staticmethod + def get_uuid_string() -> str: + """ + Generates a random uuid and returns it as a string. + + Args: + None + + Returns: + Str: Returns a random uuid as a string. + """ + logger.debug("Starting: get_uuid_string.") + generated_uuid = uuid.uuid4() + logger.debug("Finished: get_uuid_string.") + return str(generated_uuid) + + @staticmethod + def log_node_metrics(proxlb_data: Dict[str, Any], init: bool = True) -> None: + """ + Logs the memory, CPU, and disk usage metrics of nodes in the provided proxlb_data dictionary. + + This method processes the usage metrics of nodes and logs them. It also updates the + 'statistics' field in the 'meta' section of the proxlb_data dictionary with the + memory, CPU, and disk usage metrics before and after a certain operation. + + proxlb_data (Dict[str, Any]): A dictionary containing node metrics and metadata. + init (bool): A flag indicating whether to initialize the 'before' statistics + (True) or update the 'after' statistics (False). Default is True. + """ + logger.debug("Starting: log_node_metrics.") + nodes_usage_memory = " | ".join([f"{key}: {value['memory_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()]) + nodes_usage_cpu = " | ".join([f"{key}: {value['cpu_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()]) + nodes_usage_disk = " | ".join([f"{key}: {value['disk_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()]) + + if init: + proxlb_data["meta"]["statistics"] = {"before": {"memory": nodes_usage_memory, "cpu": nodes_usage_cpu, "disk": nodes_usage_disk}, "after": {"memory": "", "cpu": "", "disk": ""}} + else: + proxlb_data["meta"]["statistics"]["after"] = {"memory": nodes_usage_memory, "cpu": nodes_usage_cpu, "disk": nodes_usage_disk} + + logger.debug(f"Nodes usage memory: {nodes_usage_memory}") + logger.debug(f"Nodes usage cpu: {nodes_usage_cpu}") + logger.debug(f"Nodes usage disk: {nodes_usage_disk}") + logger.debug("Finished: log_node_metrics.") + + @staticmethod + def get_version(print_version: bool = False) -> None: + """ + Returns the current version of ProxLB and optionally prints it to stdout. + + Parameters: + print_version (bool): If True, prints the version information to stdout and exits the program. + + Returns: + None + """ + if print_version: + print(f"{utils.version.__app_name__} version: {utils.version.__version__}\n(C) 2025 by {utils.version.__author__}\n{utils.version.__url__}") + sys.exit(0) + + @staticmethod + def get_daemon_mode(proxlb_config: Dict[str, Any]) -> None: + """ + Checks if the daemon mode is active and handles the scheduling accordingly. + + Parameters: + proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration. + + Returns: + None + """ + logger.debug("Starting: get_daemon_mode.") + if proxlb_config.get("service", {}).get("daemon", False): + sleep_seconds = proxlb_config.get("service", {}).get("schedule", 12) * 3600 + logger.info(f"Daemon mode active: Next run in: {proxlb_config.get('service', {}).get('schedule', 12)} hours.") + time.sleep(sleep_seconds) + else: + logger.debug("Daemon mode is not active.") + sys.exit(0) + + logger.debug("Finished: get_daemon_mode.") diff --git a/proxlb/utils/logger.py b/proxlb/utils/logger.py new file mode 100644 index 0000000..3ecf259 --- /dev/null +++ b/proxlb/utils/logger.py @@ -0,0 +1,109 @@ +""" +The SystemdLogger class provides the root logger support. It dynamically +evaluates the further usage and imports of journald and adjusts +the logger to the systems functionality where it gets executed +""" + +import logging +try: + from systemd.journal import JournalHandler + SYSTEMD_PRESENT = True +except ImportError: + SYSTEMD_PRESENT = False + + +class SystemdLogger: + """ + The SystemdLogger class provides the root logger support. It dynamically + evaluates the further usage and imports of journald and adjusts + the logger to the systems functionality where it gets executed. + """ + # Create a singleton instance variable + instance = None + + def __new__(cls, name: str = "ProxLB", level: str = logging.INFO) -> 'SystemdLogger': + """ + Creating a new systemd logger class based on a given logging name + and its logging level/verbosity. + + Args: + name (str): The application name that is being used for the logger. + level (str): The log level defined as a string (e.g.: INFO). + + Returns: + SystemdLogger: The systemd logger object. + """ + # Check if instance already exists, otherwise create a new one + if cls.instance is None: + cls.instance = super(SystemdLogger, cls).__new__(cls) + cls.instance.initialize_logger(name, level) + return cls.instance + + def initialize_logger(self, name: str, level: str) -> None: + """ + Initializing the systemd logger class based on a given logging name + and its logging level/verbosity. + + Args: + name (str): The application name that is being used for the logger. + level (str): The log level defined as a string (e.g.: INFO). + """ + self.logger = logging.getLogger(name) + self.logger.setLevel(level) + + # Create a JournalHandler for systemd integration if this + # is supported on the underlying OS. + if SYSTEMD_PRESENT: + # Add a JournalHandler for systemd integration + journal_handler = JournalHandler() + journal_handler.setLevel(level) + # Set a formatter to include the logger's name and log message + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + journal_handler.setFormatter(formatter) + # Add handler to logger + self.logger.addHandler(journal_handler) + + def set_log_level(self, level: str) -> None: + """ + Modifies and sets the log level on the given log level. + + Args: + level (str): The log level defined as a string (e.g.: INFO). + """ + self.logger.setLevel(level) + + for handler in self.logger.handlers: + handler.setLevel(level) + + self.logger.debug("Set to debug level") + + # Handle systemd log levels + def debug(self, msg: str) -> str: + """ + Logger out for messages of type: DEBUG + """ + self.logger.debug(msg) + + def info(self, msg: str) -> str: + """ + Logger out for messages of type: INFO + """ + self.logger.info(msg) + + def warning(self, msg: str) -> str: + """ + Logger out for messages of type: WARNING + """ + self.logger.warning(msg) + + def error(self, msg: str) -> str: + """ + Logger out for messages of type: ERROR + """ + self.logger.error(msg) + + def critical(self, msg: str) -> str: + """ + Logger out for messages of type: CRITICAL + """ + self.logger.critical(msg) diff --git a/proxlb/utils/proxmox_api.py b/proxlb/utils/proxmox_api.py new file mode 100644 index 0000000..41b9379 --- /dev/null +++ b/proxlb/utils/proxmox_api.py @@ -0,0 +1,292 @@ +""" +Module providing a function printing python version. +""" + +try: + import proxmoxer + PROXMOXER_PRESENT = True +except ImportError: + PROXMOXER_PRESENT = False +import random +import socket +import sys +try: + import requests + REQUESTS_PRESENT = True +except ImportError: + REQUESTS_PRESENT = False +try: + import urllib3 + URLLIB3_PRESENT = True +except ImportError: + URLLIB3_PRESENT = False +from typing import Dict, Any +from utils.logger import SystemdLogger + + +if not PROXMOXER_PRESENT: + print("Error: The required library 'proxmoxer' is not installed.") + sys.exit(1) + +if not URLLIB3_PRESENT: + print("Error: The required library 'urllib3' is not installed.") + sys.exit(1) + +if not REQUESTS_PRESENT: + print("Error: The required library 'requests' is not installed.") + sys.exit(1) + + +logger = SystemdLogger() + + +class ProxmoxApi: + """ + Handles command-line argument parsing for ProxLB. + """ + def __init__(self, proxlb_config: Dict[str, Any]) -> None: + """ + Initialize the ProxmoxApi instance. + + This method sets up the ProxmoxApi instance by testing the required module dependencies + and establishing a connection to the Proxmox API using the provided configuration. + + Args: + proxlb_config (Dict[str, Any]): Configuration dictionary containing Proxmox API connection details. + + Returns: + None + """ + logger.debug("Starting: ProxmoxApi initialization.") + self.proxmox_api = self.api_connect(proxlb_config) + logger.debug("Finished: ProxmoxApi initialization.") + + def __getattr__(self, name): + """ + Delegate attribute access to proxmox_api. + """ + return getattr(self.proxmox_api, name) + + def api_connect_get_hosts(self, proxmox_api_endpoints: list) -> str: + """ + Perform a connectivity test to determine a working host for the Proxmox API. + + This method takes a list of Proxmox API endpoints and validates their connectivity. + It returns a working host from the list. If only one endpoint is provided, it is + returned immediately. If multiple endpoints are provided, each one is tested for + connectivity. If a valid host is found, it is returned. If multiple valid hosts + are found, one is chosen at random to distribute the load across the cluster. + + Args: + proxmox_api_endpoints (list): A list of Proxmox API endpoints to test. + + Returns: + str: A working Proxmox API host. + + Raises: + SystemExit: If the provided endpoints are not a list, if the list is empty, + or if no valid hosts are found. + """ + logger.debug("Starting: api_connect_get_hosts.") + # Pre-validate the given API endpoints + if not isinstance(proxmox_api_endpoints, list): + logger.critical(f"The proxmox_api hosts are not defined as a list type.") + sys.exit(1) + + if not proxmox_api_endpoints: + logger.critical(f"No proxmox_api hosts are defined.") + sys.exit(1) + + if len(proxmox_api_endpoints) == 0: + logger.critical(f"No proxmox_api hosts are defined.") + sys.exit(1) + + # Get a suitable Proxmox API endpoint. Therefore, we check if we only have + # a single Proxmox API endpoint or multiple ones. If only one, we can return + # this one immediately. If this one does not work, the urllib will raise an + # exception during the connection attempt. + if len(proxmox_api_endpoints) == 1: + return proxmox_api_endpoints[0] + + # If we have multiple Proxmox API endpoints, we need to check each one by + # doing a connection attempt for IPv4 and IPv6. If we find a working one, + # we return that one. This allows us to define multiple endpoints in a cluster. + validated_api_hosts = [] + for host in proxmox_api_endpoints: + validated = self.test_api_proxmox_host(host) + if validated: + validated_api_hosts.append(validated) + + if len(validated_api_hosts) > 0: + # Choose a random host to distribute the load across the cluster + # as a simple load balancing mechanism. + return random.choice(validated_api_hosts) + + logger.critical("No valid Proxmox API hosts found.") + print("No valid Proxmox API hosts found.") + + logger.debug("Finished: api_connect_get_hosts.") + sys.exit(1) + + def test_api_proxmox_host(self, host: str) -> str: + """ + Tests the connectivity to a Proxmox host by resolving its IP address and + checking both IPv4 and IPv6 addresses. + + This function attempts to resolve the given hostname to its IP addresses + (both IPv4 and IPv6). It then tests the connectivity to the Proxmox API + using the resolved IP addresses. If the host is reachable via either + IPv4 or IPv6, the function returns the hostname. If the host is not + reachable, the function returns False. + + Args: + host (str): The hostname of the Proxmox server to test. + + Returns: + str: The hostname if the Proxmox server is reachable. + bool: False if the Proxmox server is not reachable. + """ + logger.debug("Starting: test_api_proxmox_host.") + ip = socket.getaddrinfo(host, None, socket.AF_UNSPEC) + for address_type in ip: + if address_type[0] == socket.AF_INET: + logger.debug(f"{host} is type ipv4.") + if self.test_api_proxmox_host_ipv4(host): + return host + elif address_type[0] == socket.AF_INET6: + logger.debug(f"{host} is type ipv6.") + if self.test_api_proxmox_host_ipv6(host): + return host + else: + return False + + logger.debug("Finished: test_api_proxmox_host.") + + def test_api_proxmox_host_ipv4(self, host: str, port: int = 8006, timeout: int = 1) -> bool: + """ + Test the reachability of a Proxmox host on its IPv4 management address. + + This method attempts to establish a TCP connection to the specified host and port + within a given timeout period. It logs the process and results, indicating whether + the host is reachable or not. + + Args: + host (str): The IPv4 address or hostname of the Proxmox host to test. + port (int, optional): The TCP port to connect to on the host. Defaults to 8006. + timeout (int, optional): The timeout duration in seconds for the connection attempt. Defaults to 1. + + Returns: + bool: True if the host is reachable on the specified port, False otherwise. + """ + logger.debug("Starting: test_api_proxmox_host_ipv4.") + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(timeout) + logger.warning(f"Warning: Host {host} ran into a timout when connectoing on IPv4 for tcp/{port}.") + result = sock.connect_ex((host, port)) + + if result == 0: + sock.close() + logger.debug(f"Host {host} is reachable on IPv4 for tcp/{port}.") + return True + + sock.close() + logger.warning(f"Host {host} is unreachable on IPv4 for tcp/{port}.") + + logger.debug("Finished: test_api_proxmox_host_ipv4.") + return False + + def test_api_proxmox_host_ipv6(self, host: str, port: int = 8006, timeout: int = 1) -> bool: + """ + Test the reachability of a Proxmox host on its IPv6 management address. + + This method attempts to establish a TCP connection to the specified host and port + within a given timeout period. It logs the process and results, indicating whether + the host is reachable or not. + + Args: + host (str): The IPv6 address or hostname of the Proxmox host to test. + port (int, optional): The TCP port to connect to on the host. Defaults to 8006. + timeout (int, optional): The timeout duration in seconds for the connection attempt. Defaults to 1. + + Returns: + bool: True if the host is reachable on the specified port, False otherwise. + """ + logger.debug("Starting: test_api_proxmox_host_ipv6.") + sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + sock.settimeout(timeout) + logger.warning(f"Host {host} ran into a timout when connectoing on IPv6 for tcp/{port}.") + result = sock.connect_ex((host, port)) + + if result == 0: + sock.close() + logger.debug(f"Host {host} is reachable on IPv6 for tcp/{port}.") + return True + + sock.close() + logger.warning(f"Host {host} is unreachable on IPv6 for tcp/{port}.") + + logger.debug("Finished: test_api_proxmox_host_ipv4.") + return False + + def api_connect(self, proxlb_config: Dict[str, Any]) -> proxmoxer.ProxmoxAPI: + """ + Establishes a connection to the Proxmox API using the provided configuration. + + This function retrieves the Proxmox API endpoint from the configuration, optionally disables SSL certificate + validation warnings, and attempts to authenticate and create a ProxmoxAPI object. It handles various exceptions + related to authentication, connection timeouts, SSL errors, and connection refusals, logging appropriate error + messages and exiting the program if necessary. + + Args: + proxlb_config (Dict[str, Any]): A dictionary containing the Proxmox API configuration. Expected keys include: + - "proxmox_api": A dictionary with the following keys: + - "hosts" (List[str]): A list of Proxmox API host addresses. + - "user" (str): The username for Proxmox API authentication. + - "pass" (str): The password for Proxmox API authentication. + - "ssl_verification" (bool): Whether to verify SSL certificates (default is True). + - "timeout" (int): The timeout duration for API requests. + + Returns: + proxmoxer.ProxmoxAPI: An authenticated ProxmoxAPI object. + + Raises: + proxmoxer.backends.https.AuthenticationError: If authentication fails. + requests.exceptions.ConnectTimeout: If the connection to the Proxmox API times out. + requests.exceptions.SSLError: If SSL certificate validation fails. + requests.exceptions.ConnectionError: If the connection to the Proxmox API is refused. + """ + logger.debug("Starting: api_connect.") + # Get a valid Proxmox API endpoint + proxmox_api_endpoint = self.api_connect_get_hosts(proxlb_config.get("proxmox_api", {}).get("hosts", [])) + + # Disable warnings for SSL certificate validation + if not proxlb_config.get("proxmox_api").get("ssl_verification", True): + logger.warning(f"SSL certificate validation to host {proxmox_api_endpoint} is deactivated.") + urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning) + requests.packages.urllib3.disable_warnings() + + # Login into Proxmox API and create API object + try: + proxmox_api = proxmoxer.ProxmoxAPI( + proxmox_api_endpoint, + user=proxlb_config.get("proxmox_api").get("user", True), + password=proxlb_config.get("proxmox_api").get("pass", True), + verify_ssl=proxlb_config.get("proxmox_api").get("ssl_verification", True), + timeout=proxlb_config.get("proxmox_api").get("timeout", True)) + except proxmoxer.backends.https.AuthenticationError as proxmox_api_error: + logger.critical(f"Authentication failed. Please check the defined credentials: {proxmox_api_error}") + sys.exit(2) + except requests.exceptions.ConnectTimeout: + logger.critical(f"Connection timeout to host {proxmox_api_endpoint}") + sys.exit(2) + except requests.exceptions.SSLError as proxmox_api_error: + logger.critical(f"SSL certificate validation failed: {proxmox_api_error}") + sys.exit(2) + except requests.exceptions.ConnectionError: + logger.critical(f"Connection refused by host {proxmox_api_endpoint}") + sys.exit(2) + + logger.info(f"API connection to host {proxmox_api_endpoint} succeeded.") + + logger.debug("Finished: api_connect.") + return proxmox_api diff --git a/proxlb/utils/version.py b/proxlb/utils/version.py new file mode 100644 index 0000000..e94d60c --- /dev/null +++ b/proxlb/utils/version.py @@ -0,0 +1,5 @@ +__app_name__ = "ProxLB" +__app_desc__ = "A DRS alike loadbalancer for Proxmox clusters." +__author__ = "Florian Paul Azim Hoberg " +__version__ = "1.1.0-alpha" +__url__ = "https://github.com/gyptazy/ProxLB" diff --git a/requirements.txt b/requirements.txt index 4adb81f..4cfddb2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -argparse -configparser proxmoxer requests -urllib3 \ No newline at end of file +urllib3 +PyYAML \ No newline at end of file diff --git a/service/proxlb.service b/service/proxlb.service new file mode 100644 index 0000000..9d44bd1 --- /dev/null +++ b/service/proxlb.service @@ -0,0 +1,11 @@ +[Unit] +Description=ProxLB - A loadbalancer for Proxmox clusters +After=network-online.target +Wants=network-online.target + +[Service] +ExecStart=python3 /usr/lib/python3/dist-packages/proxlb/main.py -c /etc/proxlb/proxlb.yaml +User=plb + +[Install] +WantedBy=multi-user.target diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..612901e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[pycodestyle] +ignore = E501, W503 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..2e09083 --- /dev/null +++ b/setup.py @@ -0,0 +1,21 @@ +from setuptools import setup + +setup( + name="proxlb", + version="1.1.0-alpha", + description="A DRS alike loadbalancer for Proxmox clusters.", + long_description="An advanced DRS alike loadbalancer for Proxmox clusters that also supports maintenance modes and affinity/anti-affinity rules.", + author="Florian Paul Azim Hoberg", + author_email="gyptazy@gyptazy.com", + maintainer="Florian Paul Azim Hoberg", + maintainer_email="gyptazy@gyptazy.com", + url="https://github.com/gyptazy/ProxLB", + packages=["proxlb", "proxlb.utils", "proxlb.models"], + install_requires=[ + "requests", + "urllib3", + "proxmoxer", + "pyyaml", + ], + data_files=[('/etc/systemd/system', ['service/proxlb.service']), ('/etc/proxlb/', ['config/proxlb_example.yaml'])], +) diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 0ec08e8..0000000 --- a/tests/README.md +++ /dev/null @@ -1 +0,0 @@ -## Unit Tests diff --git a/tests/tests.py b/tests/tests.py deleted file mode 100644 index 0088fea..0000000 --- a/tests/tests.py +++ /dev/null @@ -1,175 +0,0 @@ -import unittest -from unittest.mock import patch, MagicMock -import logging -import sys -import os -import configparser - -from proxlb import ( - initialize_logger, - pre_validations, - post_validations, - validate_daemon, - __validate_imports, - __validate_config_file, - initialize_args, - initialize_config_path, - initialize_config_options, - api_connect, - get_node_statistics, - get_vm_statistics, - balancing_calculations, - __get_node_most_free_values, - run_vm_rebalancing, - SystemdHandler, - __errors__ -) - -class TestProxLB(unittest.TestCase): - - def test_initialize_logger(self): - with patch('logging.getLogger') as mock_get_logger, patch('logging.Handler'): - mock_logger = MagicMock() - mock_get_logger.return_value = mock_logger - initialize_logger(logging.DEBUG, SystemdHandler()) - mock_logger.setLevel.assert_called_with(logging.DEBUG) - self.assertTrue(mock_logger.addHandler.called) - - def test_pre_validations(self): - with patch('proxlb.__validate_imports') as mock_validate_imports, patch('proxlb.__validate_config_file') as mock_validate_config_file: - pre_validations('/path/to/config') - self.assertTrue(mock_validate_imports.called) - mock_validate_config_file.assert_called_with('/path/to/config') - - def test_post_validations(self): - global __errors__ - __errors__ = False - with patch('logging.critical') as mock_critical, patch('logging.info') as mock_info: - post_validations() - self.assertTrue(mock_info.called) - self.assertFalse(mock_critical.called) - - __errors__ = True - with patch('logging.critical') as mock_critical, patch('logging.info'): - post_validations() - self.assertTrue(mock_critical.called) - - def test_validate_daemon(self): - with patch('logging.info') as mock_info, patch('time.sleep') as mock_sleep, patch('sys.exit') as mock_exit: - validate_daemon(1, 1) - self.assertTrue(mock_info.called) - self.assertTrue(mock_sleep.called) - - validate_daemon(0, 1) - self.assertTrue(mock_exit.called) - - def test_validate_imports(self): - global _imports - _imports = True - with patch('logging.critical') as mock_critical, patch('logging.info') as mock_info, patch('sys.exit') as mock_exit: - __validate_imports() - self.assertTrue(mock_info.called) - self.assertFalse(mock_exit.called) - self.assertFalse(mock_critical.called) - - _imports = False - with patch('logging.critical') as mock_critical, patch('logging.info'), patch('sys.exit') as mock_exit: - __validate_imports() - self.assertTrue(mock_critical.called) - self.assertTrue(mock_exit.called) - - def test_validate_config_file(self): - with patch('os.path.isfile', return_value=True), patch('logging.critical') as mock_critical, patch('logging.info') as mock_info, patch('sys.exit') as mock_exit: - __validate_config_file('/path/to/config') - self.assertTrue(mock_info.called) - self.assertFalse(mock_exit.called) - self.assertFalse(mock_critical.called) - - with patch('os.path.isfile', return_value=False), patch('logging.critical') as mock_critical, patch('logging.info'), patch('sys.exit') as mock_exit: - __validate_config_file('/path/to/config') - self.assertTrue(mock_critical.called) - self.assertTrue(mock_exit.called) - - @patch('argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(config='/path/to/config')) - def test_initialize_args(self, mock_parse_args): - args = initialize_args() - self.assertEqual(args.config, '/path/to/config') - - def test_initialize_config_path(self): - app_args = MagicMock(config='/path/to/config') - with patch('logging.info') as mock_info: - config_path = initialize_config_path(app_args) - self.assertEqual(config_path, '/path/to/config') - self.assertTrue(mock_info.called) - - app_args.config = None - with patch('logging.info') as mock_info: - config_path = initialize_config_path(app_args) - self.assertEqual(config_path, '/etc/proxlb/proxlb.conf') - self.assertTrue(mock_info.called) - - @patch('configparser.ConfigParser.read', side_effect=lambda x: setattr(configparser.ConfigParser(), 'proxmox', {'api_host': 'host', 'api_user': 'user', 'api_pass': 'pass', 'verify_ssl': '0'})) - def test_initialize_config_options(self, mock_read): - with patch('logging.info') as mock_info, patch('sys.exit') as mock_exit: - config_path = '/path/to/config' - proxmox_api_host, proxmox_api_user, proxmox_api_pass, proxmox_api_ssl_v, balancing_method, ignore_nodes, ignore_vms, daemon, schedule = initialize_config_options(config_path) - self.assertEqual(proxmox_api_host, 'host') - self.assertEqual(proxmox_api_user, 'user') - self.assertEqual(proxmox_api_pass, 'pass') - self.assertEqual(proxmox_api_ssl_v, '0') - self.assertTrue(mock_info.called) - self.assertFalse(mock_exit.called) - - @patch('proxmoxer.ProxmoxAPI') - def test_api_connect(self, mock_proxmox_api): - with patch('requests.packages.urllib3.disable_warnings') as mock_disable_warnings, patch('logging.warning') as mock_warning, patch('logging.info') as mock_info: - proxmox_api_ssl_v = 0 - api_object = api_connect('host', 'user', 'pass', proxmox_api_ssl_v) - self.assertTrue(mock_disable_warnings.called) - self.assertTrue(mock_warning.called) - self.assertTrue(mock_info.called) - self.assertTrue(mock_proxmox_api.called) - - def test_get_node_statistics(self): - mock_api_object = MagicMock() - mock_api_object.nodes.get.return_value = [{'node': 'node1', 'status': 'online', 'maxcpu': 100, 'cpu': 50, 'maxmem': 1000, 'mem': 500, 'maxdisk': 10000, 'disk': 5000}] - node_statistics = get_node_statistics(mock_api_object, '') - self.assertIn('node1', node_statistics) - self.assertEqual(node_statistics['node1']['cpu_total'], 100) - self.assertEqual(node_statistics['node1']['cpu_used'], 50) - self.assertEqual(node_statistics['node1']['memory_total'], 1000) - self.assertEqual(node_statistics['node1']['memory_used'], 500) - self.assertEqual(node_statistics['node1']['disk_total'], 10000) - self.assertEqual(node_statistics['node1']['disk_used'], 5000) - - def test_get_vm_statistics(self): - mock_api_object = MagicMock() - mock_api_object.nodes.get.return_value = [{'node': 'node1', 'status': 'online'}] - mock_api_object.nodes().qemu.get.return_value = [{'name': 'vm1', 'status': 'running', 'cpus': 4, 'cpu': 2, 'maxmem': 8000, 'mem': 4000, 'maxdisk': 20000, 'disk': 10000, 'vmid': 101}] - vm_statistics = get_vm_statistics(mock_api_object, '') - self.assertIn('vm1', vm_statistics) - self.assertEqual(vm_statistics['vm1']['cpu_total'], 4) - self.assertEqual(vm_statistics['vm1']['cpu_used'], 2) - self.assertEqual(vm_statistics['vm1']['memory_total'], 8000) - self.assertEqual(vm_statistics['vm1']['memory_used'], 4000) - self.assertEqual(vm_statistics['vm1']['disk_total'], 20000) - self.assertEqual(vm_statistics['vm1']['disk_used'], 10000) - self.assertEqual(vm_statistics['vm1']['vmid'], 101) - self.assertEqual(vm_statistics['vm1']['node_parent'], 'node1') - - def test_balancing_calculations(self): - node_statistics = { - 'node1': {'cpu_free': 80, 'memory_free': 8000, 'disk_free': 80000}, - 'node2': {'cpu_free': 70, 'memory_free': 7000, 'disk_free': 70000} - } - vm_statistics = { - 'vm1': {'cpu_used': 20, 'memory_used': 2000, 'disk_used': 20000, 'node_parent': 'node1'}, - 'vm2': {'cpu_used': 30, 'memory_used': 3000, 'disk_used': 30000, 'node_parent': 'node1'} - } - with patch('logging.info') as mock_info, patch('logging.error') as mock_error: - node_statistics_rebalanced, vm_statistics_rebalanced = balancing_calculations('memory', node_statistics, vm_statistics) - self.assertTrue(mock_info.called) - self.assertFalse(mock_error.called) - self.assertEqual(vm_statistics_rebalanced['vm1']['node_rebalance'], 'node2') - self.assertEqual(vm_statistics_rebalanced['vm2']['node -