From 99bdd241af3b129a963055030cdaf98669f51aac Mon Sep 17 00:00:00 2001 From: Douglas Schilling Landgraf Date: Wed, 1 Jan 2025 04:06:22 -0500 Subject: [PATCH] initial code for QM python api When integration happens, it's important to have API. Example: creating and managing containers via python. Signed-off-by: Douglas Schilling Landgraf --- api/python/qm/README.md | 69 ++++++++++++++++ api/python/qm/__init__.py | 94 ++++++++++++++++++++++ api/python/qm/examples/reading_information | 75 +++++++++++++++++ 3 files changed, 238 insertions(+) create mode 100644 api/python/qm/README.md create mode 100644 api/python/qm/__init__.py create mode 100755 api/python/qm/examples/reading_information diff --git a/api/python/qm/README.md b/api/python/qm/README.md new file mode 100644 index 00000000..ebc6ff08 --- /dev/null +++ b/api/python/qm/README.md @@ -0,0 +1,69 @@ +1. Create a dir in the python site-packages for QM + +```console +sudo mkdir -p /usr/lib/python3.13/site-packages/qm/ +``` + +1.1 Copy library to it site-library for the specific user, in the case + bellow will be available only for root user. + +```console +sudo cp __init__.py /usr/lib/python3.13/site-packages/qm/ +``` + +1.2 Test it + +Source code: + +```python +from qm import QM + +def main(): + # Initialize the QM class + qm_manager = QM() + + # Example: Start a container named 'qm' + container_name = "qm" + if qm_manager.start_container(container_name): + print(f"Container '{container_name}' started successfully.") + else: + print(f"Failed to start container '{container_name}'.") + + # Example: Check the status of the container + status = qm_manager.container_status(container_name) + #print(f"Status of container '{container_name}': {status}") + print(f"Status of container {status}") + + # Example: Count the number of running containers + running_count = qm_manager.count_running_containers() + print(f"Number of running containers: {running_count}") + + # Example: Stop the container + if qm_manager.stop_container(container_name): + print(f"Container '{container_name}' stopped successfully.") + else: + print(f"Failed to stop container '{container_name}'.") + + # Example: Check if the QM service is installed + if qm_manager.is_qm_service_installed(): + print("QM service is installed.") + else: + print("QM service is not installed.") + + # Example: Check if the QM service is running + if qm_manager.is_qm_service_running(): + print("QM service is running.") + else: + print("QM service is not running.") + +if __name__ == "__main__": + main() +``` + +Testing: + +```console +$ sudo ./reading_information +QM service is installed. +QM service is running. +``` diff --git a/api/python/qm/__init__.py b/api/python/qm/__init__.py new file mode 100644 index 00000000..0049fea5 --- /dev/null +++ b/api/python/qm/__init__.py @@ -0,0 +1,94 @@ +# QM Python API Setup Guide + +This guide provides instructions to set up and test the QM Python library. + +## Steps + +1. **Create a directory for QM in Python site-packages** + + Ensure the directory exists in the Python site-packages path: + + ```bash + sudo mkdir -p /usr/lib/python3.13/site-packages/qm/ + ``` + +2. **Copy the library to the site-packages directory** + + Copy the `__init__.py` file containing the QM library code: + + ```bash + sudo cp __init__.py /usr/lib/python3.13/site-packages/qm/ + ``` + +3. **Test the setup** + + Save the following Python script to a file named `reading_information.py`: + + ```python + from qm import QM + + def main(): + # Initialize the QM class + qm_manager = QM() + + # Example: Start a container named 'qm' + container_name = "qm" + if qm_manager.start_container(container_name): + print(f"Container '{container_name}' started successfully.") + else: + print(f"Failed to start container '{container_name}'.") + + # Example: Check the status of the container + status = qm_manager.container_status(container_name) + print(f"Status of container: {status}") + + # Example: Count the number of running containers + running_count = qm_manager.count_running_containers() + print(f"Number of running containers: {running_count}") + + # Example: Stop the container + if qm_manager.stop_container(container_name): + print(f"Container '{container_name}' stopped successfully.") + else: + print(f"Failed to stop container '{container_name}'.") + + # Example: Check if the QM service is installed + if qm_manager.is_qm_service_installed(): + print("QM service is installed.") + else: + print("QM service is not installed.") + + # Example: Check if the QM service is running + if qm_manager.is_qm_service_running(): + print("QM service is running.") + else: + print("QM service is not running.") + + if __name__ == "__main__": + main() + ``` + + Run the script: + + ```bash + sudo python3 reading_information.py + ``` + +## Expected Output + +When you run the script, you should see output similar to this: + +```text +Starting container: qm +Container 'qm' started successfully. +Checking status for container: qm +Status of container: Running +Counting running containers +Number of running containers: 1 +Stopping container: qm +Container 'qm' stopped successfully. +Checking if QM service is installed +QM service is installed. +Checking if QM service is running +QM service is running. +``` diff --git a/api/python/qm/examples/reading_information b/api/python/qm/examples/reading_information new file mode 100755 index 00000000..596ec4fe --- /dev/null +++ b/api/python/qm/examples/reading_information @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Module for managing QM containers and checking service status. + +This script demonstrates the usage of the QM class to start, stop, +and check the status of Podman containers, as well as verify the +installation and running state of the QM service. +""" + +from qm import QM + + +def main(): + """ + Demonstrate QM container and service management operations. + + The main function initializes a QM instance and performs the following: + - Starts a container named 'qm'. + - Checks the container's status. + - Counts the number of running containers. + - Stops the container. + - Verifies if the QM service is installed. + - Verifies if the QM service is running. + """ + # Initialize the QM class + qm_manager = QM() + + # Example: Start a container named 'qm' + container_name = "qm" + if qm_manager.start_container(container_name): + print(f"Container '{container_name}' started successfully.") + else: + print(f"Failed to start container '{container_name}'.") + + # Example: Check the status of the container + status = qm_manager.container_status(container_name) + print(f"Status of container {status}") + + # Example: Count the number of running containers + running_count = qm_manager.count_running_containers() + print(f"Number of running containers: {running_count}") + + # Example: Stop the container + if qm_manager.stop_container(container_name): + print(f"Container '{container_name}' stopped successfully.") + else: + print(f"Failed to stop container '{container_name}'.") + + # Example: Check if the QM service is installed + if qm_manager.is_qm_service_installed(): + print("QM service is installed.") + else: + print("QM service is not installed.") + + # Example: Check if the QM service is running + if qm_manager.is_qm_service_running(): + print("QM service is running.") + else: + print("QM service is not running.") + + +if __name__ == "__main__": + main()