|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Collabora Limited |
| 5 | +# Author: Denys Fedoryshchenko <[email protected]> |
| 6 | + |
| 7 | +"""Example usage of KernelCI Context module with CLI parsing""" |
| 8 | + |
| 9 | +from kernelci.context import KContext |
| 10 | + |
| 11 | + |
| 12 | +def main(): |
| 13 | + # Example 1: Direct CLI parsing |
| 14 | + # Run this script with: |
| 15 | + # python example_context_usage.py --settings kernelci.toml --name scheduler_k8s --runtimes k8s-gke-eu-west4 k8s-all loop |
| 16 | + |
| 17 | + print("=== Example: Direct CLI Parsing ===") |
| 18 | + context = KContext(parse_cli=True) |
| 19 | + |
| 20 | + print(f"Program name: {context.program_name}") |
| 21 | + print(f"Runtimes: {context.get_runtimes()}") |
| 22 | + print(f"Settings path: {context.secrets_path}") |
| 23 | + print(f"Config paths: {context.config_paths}") |
| 24 | + |
| 25 | + # Access configuration and secrets |
| 26 | + if context.program_name: |
| 27 | + print(f"\nProcessing as {context.program_name}") |
| 28 | + |
| 29 | + # Get API configuration if available |
| 30 | + api_config = context.get_api_config("production") |
| 31 | + if api_config: |
| 32 | + print(f"API URL: {api_config.get('url', 'Not configured')}") |
| 33 | + if "token" in api_config: |
| 34 | + print("API token found (hidden)") |
| 35 | + |
| 36 | + # Process runtimes |
| 37 | + for runtime in context.get_runtimes(): |
| 38 | + print(f"\nProcessing runtime: {runtime}") |
| 39 | + runtime_config = context.get_runtime_config(runtime) |
| 40 | + if runtime_config: |
| 41 | + print(f" Runtime config: {runtime_config}") |
| 42 | + else: |
| 43 | + print(f" No configuration found for runtime: {runtime}") |
| 44 | + |
| 45 | + # Example: Initialize storage |
| 46 | + print("\n=== Storage Initialization Example ===") |
| 47 | + storage_names = context.get_storage_names() |
| 48 | + print(f"Available storage configs: {storage_names}") |
| 49 | + |
| 50 | + if storage_names: |
| 51 | + storage_name = storage_names[0] # Use first available storage |
| 52 | + print(f"\nInitializing storage: {storage_name}") |
| 53 | + storage = context.init_storage(storage_name) |
| 54 | + if storage: |
| 55 | + print(f" Storage '{storage_name}' initialized successfully") |
| 56 | + print(f" Storage type: {storage.config.storage_type}") |
| 57 | + print(f" Base URL: {storage.config.base_url}") |
| 58 | + else: |
| 59 | + print(f" Failed to initialize storage '{storage_name}'") |
| 60 | + |
| 61 | + # Example: Initialize API |
| 62 | + print("\n=== API Initialization Example ===") |
| 63 | + api_names = context.get_api_names() |
| 64 | + print(f"Available API configs: {api_names}") |
| 65 | + |
| 66 | + if api_names: |
| 67 | + api_name = api_names[0] # Use first available API |
| 68 | + print(f"\nInitializing API: {api_name}") |
| 69 | + api = context.init_api(api_name) |
| 70 | + if api: |
| 71 | + print(f" API '{api_name}' initialized successfully") |
| 72 | + print(f" URL: {api.get('url', 'Not configured')}") |
| 73 | + if "token" in api: |
| 74 | + print(" Token: [HIDDEN]") |
| 75 | + else: |
| 76 | + print(f"✗ Failed to initialize API '{api_name}'") |
| 77 | + |
| 78 | + print("\n=== Example completed successfully ===") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments