Skip to content

Commit

Permalink
code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Nov 23, 2022
1 parent 614cb56 commit a1bfd4a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
52 changes: 33 additions & 19 deletions plugins/module_utils/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,26 @@ def _run_from_pod(self, cmd):
stdout=True,
tty=False,
_preload_content=False,
**self.container_arg
**self.container_arg,
)

stderr, stdout = [], []
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
stdout.extend(resp.read_stdout().rstrip("\n").split("\n"))
if resp.peek_stderr():
stderr.extend(resp.read_stderr().rstrip("\n").split("\n"))
error = resp.read_channel(ERROR_CHANNEL)
resp.close()
error = yaml.safe_load(error)
return error, stdout, stderr
except Exception as e:
self.module.fail_json(
msg="Failed to execute command [{0}] on pod {1}/{2} due to : {3}".format(
cmd, self.namespace, self.name, to_native(e)
msg="Error while running/parsing from pod {1}/{2} command='{0}' : {3}".format(
self.namespace, self.name, cmd, to_native(e)
)
)
stderr, stdout = [], []
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
stdout.extend(resp.read_stdout().rstrip("\n").split("\n"))
if resp.peek_stderr():
stderr.extend(resp.read_stderr().rstrip("\n").split("\n"))
error = resp.read_channel(ERROR_CHANNEL)
resp.close()
error = yaml.safe_load(error)
return error, stdout, stderr

def is_directory_path_from_pod(self, file_path, failed_if_not_exists=True):
# check if file exists
Expand Down Expand Up @@ -136,7 +137,7 @@ def pod_shell(self):
return self._shellname

def listfiles_with_find(self, path):
find_cmd = ["find", path, "-type", "f", "-name", "*"]
find_cmd = ["find", path, "-type", "f"]
error, out, err = self._run_from_pod(cmd=find_cmd)
if error.get("status") != "Success":
return [], error.get("message")
Expand All @@ -147,9 +148,21 @@ def listfile_with_echo(self, path):
error, out, err = self._run_from_pod(cmd=echo_cmd)
if error.get("status") != "Success":
return [], error.get("message")

files = []
for f in out:
files.extend(f.rstrip("\n").split(" "))
# construct file list
for raw in out:
raw_list = raw.split(f"{path}/")
for i, v in enumerate(raw_list):
if v == "":
continue
if i == (len(raw_list) + 1):
# the last element does not contain additional space
files.append(os.path.join(path, v))
else:
# remove last space character
files.append(os.path.join(path, v[:-1]))

result = []
for file in files:
if os.path.basename(file) not in (".", ".."):
Expand Down Expand Up @@ -189,6 +202,7 @@ def list_remote_files(self):
self.files_to_copy, error = executables.get(item)(self.remote_path)
if error:
self.module.fail_json(msg=error)
break

def read(self):
self.stdout = None
Expand Down Expand Up @@ -245,7 +259,7 @@ def copy(self):
stdout=True,
tty=False,
_preload_content=False,
**self.container_arg
**self.container_arg,
)
errors = []
with open(dest_file, "wb") as fh:
Expand Down Expand Up @@ -347,7 +361,7 @@ def run(self):
stdout=True,
tty=False,
_preload_content=False,
**self.container_arg
**self.container_arg,
)
with TemporaryFile() as tar_buffer:
with tarfile.open(fileobj=tar_buffer, mode="w") as tar:
Expand Down
77 changes: 39 additions & 38 deletions tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,49 @@
ignore_errors: true
register: _result

- block:
- name: Copy files into container
k8s_cp:
namespace: "{{ copy_namespace }}"
pod: '{{ pod_without_executable_find.name }}'
remote_path: '{{ item.path }}'
content: '{{ item.content }}'
state: to_pod
with_items:
- path: /ansible/root.txt
content: this file is located at the root directory
- path: /ansible/.hidden_root.txt
content: this hidden file is located at the root directory
- path: /ansible/.sudir/root.txt
content: this file is located at the root of the sub directory
- path: /ansible/.sudir/.hidden_root.txt
content: this hidden file is located at the root of the sub directory
- name: Validate that 'find' executable is missing from Pod
assert:
that:
- _result is failed
fail_msg: "Pod contains 'find' executable, therefore we cannot run the next tasks."

- name: Delete existing directory
file:
path: /tmp/openjdk-files
state: absent
ignore_errors: true
- name: Copy files into container
k8s_cp:
namespace: "{{ copy_namespace }}"
pod: '{{ pod_without_executable_find.name }}'
remote_path: '{{ item.path }}'
content: '{{ item.content }}'
state: to_pod
with_items:
- path: /ansible/root.txt
content: this file is located at the root directory
- path: /ansible/.hidden_root.txt
content: this hidden file is located at the root directory
- path: /ansible/.sudir/root.txt
content: this file is located at the root of the sub directory
- path: /ansible/.sudir/.hidden_root.txt
content: this hidden file is located at the root of the sub directory

- name: copy directory from Pod into local filesystem (new directory to create)
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files
state: from_pod
- name: Delete existing directory
file:
path: /tmp/openjdk-files
state: absent
ignore_errors: true

- name: Compare directories
kubectl_file_compare:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files
- name: copy directory from Pod into local filesystem (new directory to create)
k8s_cp:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files
state: from_pod

when:
- _result is failed
- '"error executing command in container: failed to exec in container" in _result.module_stderr'
- name: Compare directories
kubectl_file_compare:
namespace: '{{ copy_namespace }}'
pod: '{{ pod_without_executable_find.name }}'
remote_path: /ansible
local_path: /tmp/openjdk-files

always:
- name: Remove directories created into remote Pod
Expand Down

0 comments on commit a1bfd4a

Please sign in to comment.