Skip to content

Commit

Permalink
Merge pull request #10 from theko2fi/dev/0.2.3
Browse files Browse the repository at this point in the history
Dev/0.2.3
  • Loading branch information
theko2fi authored Jan 5, 2024
2 parents be60a3e + 807d457 commit f0acf51
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 18 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
======

Expand Down
15 changes: 15 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 7 additions & 0 deletions changelogs/fragments/v0.2.3.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions plugins/module_utils/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python
#
# Copyright (c) 2022, Kenneth KOFFI <https://www.linkedin.com/in/kenneth-koffi-6b1218178/>
# 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
4 changes: 1 addition & 3 deletions plugins/module_utils/multipass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions plugins/modules/multipass_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down
26 changes: 15 additions & 11 deletions plugins/modules/multipass_vm_transfer_into.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
)
)
Expand All @@ -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)
)
)
Expand Down

0 comments on commit f0acf51

Please sign in to comment.