Skip to content

Commit

Permalink
initial code for QM python api
Browse files Browse the repository at this point in the history
When integration happens, it's important to have API.
Example: creating and managing containers via python.

Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Jan 1, 2025
1 parent ef5463d commit 99bdd24
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 0 deletions.
69 changes: 69 additions & 0 deletions api/python/qm/README.md
Original file line number Diff line number Diff line change
@@ -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.
```
94 changes: 94 additions & 0 deletions api/python/qm/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
```
75 changes: 75 additions & 0 deletions api/python/qm/examples/reading_information
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 99bdd24

Please sign in to comment.