From fa53f05254ca359a5eed7ade936462e9880c87c0 Mon Sep 17 00:00:00 2001 From: theko2fi Date: Fri, 5 Jan 2024 21:39:45 +0100 Subject: [PATCH 1/3] remove ansible.errors from `multipass_vm` and `multipass_vm_transfer_into` modules --- plugins/module_utils/errors.py | 14 ++++++++++ plugins/module_utils/multipass.py | 4 +-- plugins/modules/multipass_vm.py | 3 --- plugins/modules/multipass_vm_transfer_into.py | 26 +++++++++++-------- 4 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 plugins/module_utils/errors.py diff --git a/plugins/module_utils/errors.py b/plugins/module_utils/errors.py new file mode 100644 index 0000000..6a7edf6 --- /dev/null +++ b/plugins/module_utils/errors.py @@ -0,0 +1,14 @@ +#!/usr/bin/python +# +# Copyright (c) 2022, Kenneth KOFFI +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +class MultipassFileTransferError(Exception): + pass + +class MultipassContentTransferError(Exception): + pass + +class SocketError(Exception): + pass \ No newline at end of file diff --git a/plugins/module_utils/multipass.py b/plugins/module_utils/multipass.py index 89fc4f1..c35b200 100644 --- a/plugins/module_utils/multipass.py +++ b/plugins/module_utils/multipass.py @@ -5,9 +5,7 @@ import json import time from shlex import split as shlexsplit - -class SocketError(Exception): - pass +from errors import SocketError # Added decorator to automatically retry on unpredictable module failures diff --git a/plugins/modules/multipass_vm.py b/plugins/modules/multipass_vm.py index bf0aa79..3ac2aef 100644 --- a/plugins/modules/multipass_vm.py +++ b/plugins/modules/multipass_vm.py @@ -5,7 +5,6 @@ # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) from ansible.module_utils.basic import AnsibleModule -from ansible.errors import AnsibleError from ansible_collections.theko2fi.multipass.plugins.module_utils.multipass import MultipassClient @@ -18,8 +17,6 @@ def is_vm_exists(vm_name): return True except NameError: return False - except Exception as e: - raise AnsibleError(str(e)) def get_vm_state(vm_name: str): if is_vm_exists(vm_name=vm_name): diff --git a/plugins/modules/multipass_vm_transfer_into.py b/plugins/modules/multipass_vm_transfer_into.py index 06e9844..c001bff 100644 --- a/plugins/modules/multipass_vm_transfer_into.py +++ b/plugins/modules/multipass_vm_transfer_into.py @@ -10,18 +10,22 @@ import os import base64 from io import BytesIO, open as iopen -from ansible.module_utils._text import to_bytes, to_native, to_text -from ansible.errors import AnsibleError, AnsibleFileNotFound +from ansible.module_utils._text import to_bytes, to_native import subprocess from ansible.module_utils.basic import AnsibleModule -from ansible_collections.theko2fi.multipass.plugins.module_utils.multipass import retry_on_failure, SocketError +from ansible_collections.theko2fi.multipass.plugins.module_utils.multipass import retry_on_failure +from ansible_collections.theko2fi.multipass.plugins.module_utils.errors import ( + SocketError, + MultipassFileTransferError, + MultipassContentTransferError + ) def put_file(remote_addr, in_path, out_path): ''' transfer a file from local to the multipass VM ''' if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): - raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path))) + raise FileNotFoundError("file or module does not exist: {0}".format(to_native(in_path))) @retry_on_failure(ExceptionsToCheck=SocketError) #retry on SocketError def low_level_code_put_file(in_path, remote_addr, out_path): @@ -38,14 +42,14 @@ def low_level_code_put_file(in_path, remote_addr, out_path): if "Socket error" in stderr.decode(encoding="utf-8"): raise SocketError(stderr.decode(encoding="utf-8").strip()) else: - raise AnsibleError(stderr.decode(encoding="utf-8").strip()) + raise MultipassFileTransferError(stderr.decode(encoding="utf-8").strip()) return exitcode try: # This function call actually put file inside the target VM - exitcode = low_level_code_put_file(in_path, remote_addr, out_path) + low_level_code_put_file(in_path, remote_addr, out_path) except Exception as e: - raise AnsibleError("failed to transfer file {0} to {1}:{2}\nError: {3}".format( + raise MultipassFileTransferError("failed to transfer file {0} to {1}:{2}\nError: {3}".format( to_native(in_path), to_native(remote_addr), to_native(out_path), to_native(e) ) ) @@ -63,19 +67,19 @@ def _low_level_code_put_content(remote_addr, out_path, in_content): stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - stdout, stderr = transfer_process.communicate(input=in_content) + _, stderr = transfer_process.communicate(input=in_content) exitcode = transfer_process.wait() if(exitcode != 0): if "Socket error" in stderr.decode(encoding="utf-8"): raise SocketError(stderr.decode(encoding="utf-8").strip()) else: - raise AnsibleError(stderr.decode(encoding="utf-8").strip()) + raise MultipassContentTransferError(stderr.decode(encoding="utf-8").strip()) return exitcode try: - exitcode = _low_level_code_put_content(remote_addr=remote_addr, out_path=out_path, in_content=in_content) + _low_level_code_put_content(remote_addr=remote_addr, out_path=out_path, in_content=in_content) except Exception as e: - raise AnsibleError("failed to transfer content {0} to {1}:{2}\nError: {3}".format( + raise MultipassFileTransferError("failed to transfer content {0} to {1}:{2}\nError: {3}".format( to_native(in_content), to_native(remote_addr), to_native(out_path), to_native(e) ) ) From be82ce99e6cd95de44d0e759d5304d73df9193b1 Mon Sep 17 00:00:00 2001 From: theko2fi Date: Fri, 5 Jan 2024 21:56:14 +0100 Subject: [PATCH 2/3] set release version to 0.2.3 --- CHANGELOG.rst | 16 ++++++++++++++++ changelogs/changelog.yaml | 15 +++++++++++++++ changelogs/fragments/v0.2.3.yaml | 7 +++++++ galaxy.yml | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/v0.2.3.yaml diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 08cbb71..fe7f908 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,22 @@ Theko2Fi.Multipass Release Notes .. contents:: Topics +v0.2.3 +====== + +Release Summary +--------------- + +Release Date: 2024-01-05 + +This release contains a bugfix + + +Bugfixes +-------- + +- Fix - Collection not working inside virtual environment https://github.com/theko2fi/ansible-multipass-collection/issues/9 + v0.2.2 ====== diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index 75ad79e..2ce5dbe 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -55,3 +55,18 @@ releases: fragments: - v0.2.2.yaml release_date: '2023-10-21' + 0.2.3: + changes: + bugfixes: + - 'Fix - Collection not working inside virtual environment https://github.com/theko2fi/ansible-multipass-collection/issues/9 + + ' + release_summary: 'Release Date: 2024-01-05 + + + This release contains a bugfix + + ' + fragments: + - v0.2.3.yaml + release_date: '2024-01-05' diff --git a/changelogs/fragments/v0.2.3.yaml b/changelogs/fragments/v0.2.3.yaml new file mode 100644 index 0000000..e3da0fe --- /dev/null +++ b/changelogs/fragments/v0.2.3.yaml @@ -0,0 +1,7 @@ +release_summary: | + Release Date: 2024-01-05 + + This release contains a bugfix +bugfixes: + - | + Fix - Collection not working inside virtual environment https://github.com/theko2fi/ansible-multipass-collection/issues/9 diff --git a/galaxy.yml b/galaxy.yml index ec016de..8870de6 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -8,7 +8,7 @@ namespace: theko2fi name: multipass # The version of the collection. Must be compatible with semantic versioning -version: 0.2.2 +version: 0.2.3 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection readme: README.md From 807d457a4fc3f75408b84e524ee51cf17c518dfc Mon Sep 17 00:00:00 2001 From: theko2fi Date: Fri, 5 Jan 2024 22:34:16 +0100 Subject: [PATCH 3/3] fix No module named 'errors' --- plugins/module_utils/multipass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/multipass.py b/plugins/module_utils/multipass.py index c35b200..961202e 100644 --- a/plugins/module_utils/multipass.py +++ b/plugins/module_utils/multipass.py @@ -5,7 +5,7 @@ import json import time from shlex import split as shlexsplit -from errors import SocketError +from .errors import SocketError # Added decorator to automatically retry on unpredictable module failures