]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/behave_tests/features/kcli_handler.py
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / behave_tests / features / kcli_handler.py
CommitLineData
20effc67
TL
1import subprocess
2import time
3import os
4
5
6kcli_exec = r"""
7podman run --net host -it --rm --security-opt label=disable
8 -v $HOME/.ssh:/root/.ssh -v $HOME/.kcli:/root/.kcli
9 -v /var/lib/libvirt/images:/var/lib/libvirt/images
10 -v /var/run/libvirt:/var/run/libvirt -v $PWD:/workdir
11 -v /var/tmp:/ignitiondir jolmomar/kcli
12"""
13
14
15def _create_kcli_cmd(command):
16 cmd = kcli_exec.replace("$HOME", os.getenv("HOME"))
17 cmd = cmd.replace("$PWD", os.getenv("PWD"))
18 kcli = cmd.replace("\n", "").split(" ")
19 return kcli + command.split(" ")
20
21
22def is_bootstrap_script_complete():
23 """
24 Checks for status of bootstrap script executions.
25 """
26 timeout = 0
27 command = " ".join(
28 [
29 f'"{cmd}"' for cmd in
30 "journalctl --no-tail --no-pager -t cloud-init".split(" ")
31 ]
32 )
33 cmd = _create_kcli_cmd(
34 f'ssh ceph-node-00 {command} | grep "Bootstrap complete."'
35 )
36 while timeout < 10: # Totally waits for 5 mins before giving up
37 proc = subprocess.run(cmd, capture_output=True, text=True)
38 if "Bootstrap complete." in proc.stdout:
39 print("Bootstrap script completed successfully")
40 return True
41 timeout += 1
42 print("Waiting for bootstrap_cluster script...")
43 print(proc.stdout[len(proc.stdout) - 240:])
44 time.sleep(30)
45 print(
46 f"Timeout reached {30*timeout}. Giving up for boostrap to complete"
47 )
48 return False
49
50
51def execute_kcli_cmd(command):
52 """
53 Executes the kcli command by combining the provided command
54 with kcli executable command.
55 """
56 cmd = _create_kcli_cmd(command)
57 print(f"Executing kcli command : {command}")
58 try:
59 proc = subprocess.run(
60 cmd,
61 capture_output=True,
62 text=True,
63 # env=dict(STORAGE_OPTS=''),
64 )
65 except Exception as ex:
66 print(f"Error executing kcli command\n{ex}")
67
68 op = proc.stderr if proc.stderr else proc.stdout
69 return (op, proc.returncode)
70
71
72def execute_ssh_cmd(vm_name, shell, command):
73 """
74 Executes the provided ssh command on the provided vm machine
75 """
76 if shell == "cephadm_shell":
77 command = f"cephadm shell {command}"
78 sudo_cmd = f"sudo -i {command}".split(" ")
79 sudo_cmd = " ".join([f'"{cmd}"' for cmd in sudo_cmd])
80 cmd = _create_kcli_cmd(f"ssh {vm_name} {sudo_cmd}")
81 print(f"Executing ssh command : {cmd}")
82 try:
83 proc = subprocess.run(cmd, capture_output=True, text=True)
84 except Exception as ex:
85 print(f"Error executing ssh command: {ex}")
86
87 op = proc.stderr if proc.stderr else proc.stdout
88 return (op, proc.returncode)