Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial code for QM python api #691

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
```
92 changes: 92 additions & 0 deletions api/python/qm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 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.

"""
QM library for managing containers and checking QM services.

This package provides functionality to start, stop,
check the status of containers,
and verify if the QM service is installed and running.
"""


class QM:
"""Class for managing QM containers and services."""

def start_container(self, container_name):
"""
Start a container with the given name.

Args:
container_name (str): Name of the container to start.

Returns:
bool: True if the container started successfully, False otherwise.
"""
print(f"Starting container: {container_name}")
return True

def stop_container(self, container_name):
"""
Stop a container with the given name.

Args:
container_name (str): Name of the container to stop.

Returns:
bool: True if the container stopped successfully, False otherwise.
"""
print(f"Stopping container: {container_name}")
return True

def container_status(self, container_name):
"""
Get the status of a container.

Args:
container_name (str): Name of the container to check.

Returns:
str: Status of the container (e.g., "Running", "Stopped").
"""
print(f"Checking status for container: {container_name}")
return "Running"

def count_running_containers(self):
"""
Count the number of running containers.

Returns:
int: Number of running containers.
"""
print("Counting running containers")
return 1

def is_qm_service_installed(self):
"""
Check if the QM service is installed.

Returns:
bool: True if the QM service is installed, False otherwise.
"""
print("Checking if QM service is installed")
return True

def is_qm_service_running(self):
"""
Check if the QM service is running.

Returns:
bool: True if the QM service is running, False otherwise.
"""
print("Checking if QM service is running")
return True
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()
Loading