- Add complete pytest testing framework with conftest.py and test files - Add performance monitoring and benchmarking capabilities - Add plugin system with ccache plugin example - Add comprehensive documentation (API, deployment, testing, etc.) - Add Docker API wrapper for service deployment - Add advanced configuration examples - Remove old wget package file - Update core modules with enhanced functionality
9.4 KiB
Deb-Mock Plugin System
Overview
The deb-mock plugin system provides a powerful and extensible way to customize build behavior, add new features, and integrate with external tools. It's based on Fedora Mock's proven plugin architecture, adapted specifically for Debian-based build environments.
Features
- Hook-based architecture - Plugins can hook into various stages of the build process
- Dynamic loading - Plugins are loaded at runtime based on configuration
- API versioning - Ensures compatibility between deb-mock versions and plugins
- Configuration-driven - Rich configuration options for each plugin
- Error handling - Robust error handling with required vs. optional plugins
- Base classes - Helper classes for easier plugin development
Architecture
Core Components
- PluginManager - Main plugin orchestration class
- HookStages - Standard hook stages for plugins
- BasePlugin - Base class for plugin development
- Plugin Configuration - YAML-based plugin configuration
Hook Stages
The plugin system provides hooks at various stages of the build process:
Chroot Lifecycle
prechroot_init- Before chroot initializationpostchroot_init- After chroot initializationprechroot_clean- Before chroot cleanuppostchroot_clean- After chroot cleanup
Build Lifecycle
prebuild- Before build startspostbuild- After build completesbuild_start- When build beginsbuild_end- When build ends
Package Management
pre_install_deps- Before installing dependenciespost_install_deps- After installing dependenciespre_install_package- Before installing packagespost_install_package- After installing packages
Mount Management
pre_mount- Before mounting filesystemspost_mount- After mounting filesystemspre_unmount- Before unmounting filesystemspost_unmount- After unmounting filesystems
Cache Management
pre_cache_create- Before creating cachespost_cache_create- After creating cachespre_cache_restore- Before restoring cachespost_cache_restore- After restoring caches
Parallel Build Hooks
pre_parallel_build- Before parallel buildspost_parallel_build- After parallel buildsparallel_build_start- When parallel build startsparallel_build_end- When parallel build ends
Error Handling
on_error- When errors occuron_warning- When warnings occur
Configuration
Basic Plugin Configuration
# Enable plugins
plugins: ["ccache_plugin", "build_monitor"]
# Plugin directory (optional)
plugin_dir: "./plugins"
# Plugin-specific configuration
plugin_conf:
# CCache plugin
ccache_enable: true
ccache_required: false
ccache_opts:
dir: "/var/cache/deb-mock/ccache"
max_cache_size: "4G"
show_stats: true
compress: true
hashdir: true
debug: false
# Build monitor plugin
build_monitor_enable: true
build_monitor_required: false
build_monitor_opts:
log_file: "/var/log/deb-mock/builds.log"
notify_on_completion: true
track_build_time: true
Plugin Configuration Options
{plugin}_enable- Enable/disable plugin (default: true){plugin}_required- Make plugin required (default: false){plugin}_opts- Plugin-specific configuration options
Plugin Development
Basic Plugin Structure
#!/usr/bin/env python3
"""
Example plugin for deb-mock
"""
requires_api_version = "1.0"
run_in_bootstrap = False
def init(plugin_manager, conf, deb_mock):
"""Plugin entry point"""
ExamplePlugin(plugin_manager, conf, deb_mock)
class ExamplePlugin:
"""Example plugin implementation"""
def __init__(self, plugin_manager, conf, deb_mock):
self.plugin_manager = plugin_manager
self.conf = conf
self.deb_mock = deb_mock
# Register hooks
self._register_hooks()
def _register_hooks(self):
"""Register plugin hooks"""
self.plugin_manager.add_hook("prebuild", self._prebuild_hook)
self.plugin_manager.add_hook("postbuild", self._postbuild_hook)
def _prebuild_hook(self, source_package, **kwargs):
"""Hook called before build starts"""
print(f"Example plugin: Pre-build hook for {source_package}")
def _postbuild_hook(self, build_result, source_package, **kwargs):
"""Hook called after build completes"""
print(f"Example plugin: Post-build hook for {source_package}")
Using BasePlugin Class
from deb_mock.plugin import BasePlugin, HookStages
class MyPlugin(BasePlugin):
"""Plugin using the base class"""
def _register_hooks(self):
"""Override to register hooks"""
self.plugin_manager.add_hook(HookStages.PREBUILD, self._my_hook)
def _my_hook(self, source_package, **kwargs):
"""My custom hook"""
self.log_info(f"Processing {source_package}")
# Plugin logic here
Plugin API Requirements
Every plugin must define:
requires_api_version- API version compatibilityrun_in_bootstrap- Whether to run in bootstrap chrootsinit()function - Plugin entry point
Available Hooks
Plugins can register hooks for any of the standard stages defined in HookStages, or create custom stages.
Built-in Plugins
CCache Plugin
The CCache plugin provides compiler caching for faster rebuilds:
plugin_conf:
ccache_enable: true
ccache_opts:
dir: "/var/cache/deb-mock/ccache"
max_cache_size: "4G"
show_stats: true
compress: true
hashdir: true
debug: false
Features:
- Automatic ccache setup in chroots
- Configurable cache size and options
- Build statistics reporting
- Environment variable management
Build Monitor Plugin
The Build Monitor plugin tracks build performance and provides notifications:
plugin_conf:
build_monitor_enable: true
build_monitor_opts:
log_file: "/var/log/deb-mock/builds.log"
notify_on_completion: true
track_build_time: true
performance_metrics: true
Features:
- Build time tracking
- Performance metrics collection
- Completion notifications
- Detailed logging
CLI Commands
Plugin Management
# Show plugin information
deb-mock plugin-info
# List available hook stages
deb-mock list-stages
# List hooks for a specific stage
deb-mock list-hooks prebuild
Plugin Configuration
Plugins are configured through the main configuration file or command-line options. The plugin system automatically loads enabled plugins and initializes them with the deb-mock instance.
Best Practices
Plugin Development
- Use descriptive names - Choose clear, descriptive plugin names
- Handle errors gracefully - Don't let plugin failures break builds
- Use logging - Use the provided logging methods for debugging
- Validate configuration - Check configuration values and provide defaults
- Document hooks - Clearly document what each hook does
Configuration
- Enable only needed plugins - Don't enable plugins you don't use
- Use required sparingly - Only mark plugins as required if builds fail without them
- Provide defaults - Always provide sensible default values
- Test configurations - Test plugin configurations before production use
Performance
- Minimize hook overhead - Keep hooks lightweight
- Use async when possible - Consider async operations for I/O heavy tasks
- Cache results - Cache expensive operations when appropriate
- Profile plugins - Monitor plugin performance impact
Troubleshooting
Common Issues
- Plugin not loading - Check plugin directory and file permissions
- API version mismatch - Ensure plugin API version matches deb-mock
- Hook not firing - Verify hook stage names and registration
- Configuration errors - Check YAML syntax and plugin configuration
Debugging
- Enable debug logging - Use
--debugflag for verbose output - Check plugin info - Use
plugin-infocommand to verify plugin loading - Verify hooks - Use
list-hooksto check hook registration - Test individually - Test plugins in isolation before integration
Examples
Complete Plugin Example
See examples/plugins/ccache_plugin.py for a complete working plugin.
Configuration Example
See examples/plugin-config.yaml for a complete plugin-enabled configuration.
API Reference
PluginManager Methods
init_plugins(deb_mock)- Initialize all enabled pluginscall_hooks(stage, *args, **kwargs)- Call hooks for a stageadd_hook(stage, function)- Register a hook functionremove_hook(stage, function)- Remove a hook functionget_hooks(stage)- Get hooks for a stagelist_stages()- List available hook stagesget_plugin_info()- Get plugin system information
BasePlugin Methods
get_config(key, default)- Get plugin configurationset_config(key, value)- Set plugin configurationlog_info(message)- Log info messagelog_warning(message)- Log warning messagelog_error(message)- Log error messagelog_debug(message)- Log debug message
Future Enhancements
- Plugin repositories - Centralized plugin distribution
- Plugin dependencies - Plugin-to-plugin dependencies
- Plugin validation - Automated plugin testing and validation
- Plugin metrics - Performance and usage metrics
- Plugin hot-reload - Runtime plugin updates