No project description provided
Project description
keepalived-config
Python API for configuration files for linux keepalived package.
This project is forked from https://github.com/Slinred/keepalived-config. Special thanks to the original author for their work.
License
This project is licensed under the GPL-3 License - see the LICENSE file for details.
Features
- Parse a keepalived configuration from file or string
- Modify the config object and any parameter inside
- Save back the (modified) config to another (or the same) file
- Comments in the config file are supported and can also be added via the python API
- empty lines in the config file, can be kept and are represented as empty config parameters
Main Classes and Methods
Core Classes
KeepAlivedConfig- Main configuration class representing a keepalived configurationKeepAlivedConfigManager- Unified entry point for managing all keepalived configurationsKeepAlivedConfigVRRP- VRRP instance managementKeepAlivedConfigVirtualServer- Virtual server managementKeepAlivedConfigTemplates- Template system for creating configurationsOperationResult- Result wrapper for operations
Configuration Objects
VRRPConfig- Configuration object for VRRP instancesVirtualServerConfig- Configuration object for virtual servers
Main Methods
KeepAlivedConfigManager (Recommended Entry Point)
load_config(config_file)- Load configuration from filesave_config(file_path)- Save configuration to filevalidate()- Validate configuration integrityvrrp- Access to VRRP management functionsvirtual_server- Access to virtual server management functions
KeepAlivedConfigVRRP
create_vrrp_instance()- Create VRRP instanceupdate_vrrp_instance()- Update VRRP instanceremove_vrrp_instance()- Remove VRRP instanceget_vrrp_instance()- Get VRRP instancelist_vrrp_instances()- List all VRRP instancescreate_from_template()- Create VRRP instance from template
KeepAlivedConfigVirtualServer
create_virtual_server()- Create virtual serverupdate_virtual_server()- Update virtual serverremove_virtual_server()- Remove virtual serverget_virtual_server()- Get virtual serverlist_virtual_servers()- List all virtual serversadd_real_server()- Add real server to virtual serverupdate_real_server()- Update real serverremove_real_server()- Remove real servercreate_from_template()- Create virtual server from template
KeepAlivedConfigTemplates
from_template()- Create configuration from templateregister_template()- Register custom templateunregister_template()- Unregister templatelist_templates()- List all available templates
Basic Usage Examples
Simple Configuration Creation
from keepalived_config import KeepAlivedConfigManager, VRRPConfig, VirtualServerConfig
# Create configuration manager (recommended entry point)
manager = KeepAlivedConfigManager()
# Create VRRP instance using configuration object
vrrp_config = VRRPConfig(
state="MASTER",
interface="eth0",
virtual_router_id=51,
priority=100
)
result = manager.vrrp.create_vrrp_instance(
instance_name="VI_1",
config=vrrp_config,
virtual_ipaddresses=["192.168.1.100/24"]
)
if result:
print("VRRP instance created successfully")
else:
print(f"Failed to create VRRP instance: {result.message}")
# Create virtual server
vs_config = VirtualServerConfig(
delay_loop=6,
lb_algo="rr",
lb_kind="DR",
protocol="TCP"
)
result = manager.virtual_server.create_virtual_server(
virtual_server_ip="192.168.1.100",
virtual_server_port=80,
config=vs_config
)
if result:
print("Virtual server created successfully")
else:
print(f"Failed to create virtual server: {result.message}")
# Add real server to virtual server
result = manager.virtual_server.add_real_server(
virtual_server_ip="192.168.1.100",
virtual_server_port=80,
real_server_ip="192.168.1.10",
real_server_port=80,
weight=1,
health_check="TCP_CHECK",
health_check_params={
"connect_timeout": 3,
"delay_before_retry": 3
}
)
if result:
print("Real server added successfully")
else:
print(f"Failed to add real server: {result.message}")
# Save configuration to file
result = manager.save_config("keepalived.conf")
if result:
print("Configuration saved successfully")
else:
print(f"Failed to save configuration: {result.message}")
Template Usage
from keepalived_config import KeepAlivedConfigTemplates, KeepAlivedConfigManager
# Create configuration from template
config = KeepAlivedConfigTemplates.from_template(
"basic_vrrp",
"VI_1",
state="MASTER",
interface="eth0",
virtual_router_id=51,
priority=100,
virtual_ipaddress="192.168.1.100/24"
)
print(config.params[0].to_str())
# Using templates with manager
manager = KeepAlivedConfigManager()
result = manager.vrrp.create_from_template(
"complete_vrrp_backup",
"VI_2",
interface="eth1",
virtual_router_id=52,
priority=90,
advert_int=2,
auth_type="PASS",
auth_pass="backup_password",
virtual_ipaddress="192.168.2.100/24"
)
if result:
print("VRRP instance created from template successfully")
else:
print(f"Failed to create VRRP instance from template: {result.message}")
Custom Template Registration
from keepalived_config import KeepAlivedConfigTemplates
# Define custom template
custom_template = {
"type": "vrrp_instance",
"params": {
"state": "{state}",
"interface": "{interface}",
"virtual_router_id": "{virtual_router_id}",
"priority": "{priority}",
"virtual_ipaddress": ["{virtual_ipaddress}"],
"nopreempt": "",
"preempt_delay": "{preempt_delay}"
}
}
# Register template
KeepAlivedConfigTemplates.register_template("my_vrrp_template", custom_template)
# Use custom template
config = KeepAlivedConfigTemplates.from_template(
"my_vrrp_template",
"VI_CUSTOM",
state="BACKUP",
interface="eth1",
virtual_router_id=52,
priority=90,
virtual_ipaddress="192.168.2.100/24",
preempt_delay=5
)
print(config.params[0].to_str())
Configuration Loading and Validation
from keepalived_config import KeepAlivedConfigManager
# Load existing configuration
manager = KeepAlivedConfigManager()
result = manager.load_config("existing_keepalived.conf")
if result:
print("Configuration loaded successfully")
# List existing VRRP instances
print("VRRP instances:")
for instance in manager.vrrp_instances:
print(f" - {instance}")
# List existing virtual servers
print("Virtual servers:")
for vs in manager.virtual_servers:
print(f" - {vs}")
# Validate configuration
result = manager.validate()
if result:
print("Configuration is valid")
else:
print("Configuration validation issues:")
for issue in result.data:
print(f" - {issue}")
else:
print(f"Failed to load configuration: {result.message}")
Extended Configuration Parameters
from keepalived_config import KeepAlivedConfigManager, VRRPConfig, VirtualServerConfig
manager = KeepAlivedConfigManager()
# Using extended VRRP parameters
vrrp_config = VRRPConfig(
state="MASTER",
interface="eth0",
virtual_router_id=51,
priority=100,
# Extended parameters
unicast_src_ip="192.168.1.1",
unicast_peer=["192.168.1.2", "192.168.1.3"],
smtp_alert=True,
notify_master="/etc/keepalived/scripts/notify_master.sh",
notify_backup="/etc/keepalived/scripts/notify_backup.sh"
)
result = manager.vrrp.create_vrrp_instance(
instance_name="VI_EXTENDED",
config=vrrp_config,
virtual_ipaddresses=["192.168.1.100/24"]
)
# Using extended virtual server parameters
vs_config = VirtualServerConfig(
delay_loop=10,
lb_algo="wrr",
lb_kind="DR",
protocol="TCP",
# Extended parameters
ha_suspend=True,
alpha=True,
omega=True,
quorum=2,
quorum_up="/etc/keepalived/scripts/quorum_up.sh",
quorum_down="/etc/keepalived/scripts/quorum_down.sh",
hysteresis=3,
retry=5
)
result = manager.virtual_server.create_virtual_server(
virtual_server_ip="192.168.1.100",
virtual_server_port=80,
config=vs_config
)
TODO
- Support for included config files
Development
Setup
To setup your dev environment, you have 2 options:
- local: execute the command
main.sh setup. This will install a virtual python environment and install the required packages. - container: Use the provided devcontainer, where everything is already installed (no need to run the setup command)
Tests
Units tests are to be developed for all public modules and methods and placed inside the tests directory.
They can be executed via the command main.sh test
Packaging
The source build and wheel distrubtions can be generated via the command main.sh build.
The package can then be uploaded to PyPi via the command main.sh upload.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keepalived_api-0.0.2.tar.gz.
File metadata
- Download URL: keepalived_api-0.0.2.tar.gz
- Upload date:
- Size: 78.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66bca1630557ede6d3cce155e15beeed413e8cfcf37c84e0d7c58d265c9ed10a
|
|
| MD5 |
db99ac373681d432397164c2344234ec
|
|
| BLAKE2b-256 |
d82886912290ea2e88ed8f7918c0af2f5c414f7ddd0125e96ea03e548c9b085f
|
File details
Details for the file keepalived_api-0.0.2-py3-none-any.whl.
File metadata
- Download URL: keepalived_api-0.0.2-py3-none-any.whl
- Upload date:
- Size: 58.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
013e11625ebed0d70195b279bfb9ebfb6002b7f45f84efff2cf9ec3681099c9a
|
|
| MD5 |
b70f839d54a097eb2ea01edb0e588f38
|
|
| BLAKE2b-256 |
5f6f0ebef8ee4f47a564989954d6cfdfccc5ba4f32efeca67032f4ffb1236de5
|