virt.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import time
  2. import uuid
  3. import attr
  4. from saltfactories.factories.daemons.container import SaltMinionContainerFactory
  5. from saltfactories.utils import ports
  6. from tests.support.runtests import RUNTIME_VARS
  7. @attr.s(kw_only=True, slots=True)
  8. class SaltVirtMinionContainerFactory(SaltMinionContainerFactory):
  9. host_uuid = attr.ib(default=attr.Factory(uuid.uuid4))
  10. ssh_port = attr.ib(
  11. default=attr.Factory(ports.get_unused_localhost_port), repr=False
  12. )
  13. sshd_port = attr.ib(default=attr.Factory(ports.get_unused_localhost_port))
  14. libvirt_tcp_port = attr.ib(
  15. default=attr.Factory(ports.get_unused_localhost_port), repr=False
  16. )
  17. libvirt_tls_port = attr.ib(
  18. default=attr.Factory(ports.get_unused_localhost_port), repr=False
  19. )
  20. uri = attr.ib(init=False)
  21. ssh_uri = attr.ib(init=False)
  22. tcp_uri = attr.ib(init=False)
  23. tls_uri = attr.ib(init=False)
  24. def __attrs_post_init__(self):
  25. self.uri = "localhost:{}".format(self.sshd_port)
  26. self.ssh_uri = "qemu+ssh://{}/system".format(self.uri)
  27. self.tcp_uri = "qemu+tcp://localhost:{}/system".format(self.libvirt_tcp_port)
  28. self.tls_uri = "qemu+tls://localhost:{}/system".format(self.libvirt_tls_port)
  29. if self.check_ports is None:
  30. self.check_ports = []
  31. self.check_ports.extend(
  32. [self.sshd_port, self.libvirt_tcp_port, self.libvirt_tls_port]
  33. )
  34. if "environment" not in self.container_run_kwargs:
  35. self.container_run_kwargs["environment"] = {}
  36. self.container_run_kwargs["environment"].update(
  37. {
  38. "SSH_PORT": str(self.ssh_port),
  39. "SSHD_PORT": str(self.sshd_port),
  40. "LIBVIRT_TCP_PORT": str(self.libvirt_tcp_port),
  41. "LIBVIRT_TLS_PORT": str(self.libvirt_tls_port),
  42. "NO_START_MINION": "1",
  43. "HOST_UUID": self.host_uuid,
  44. "PYTHONDONTWRITEBYTECODE": "1",
  45. }
  46. )
  47. if "ports" not in self.container_run_kwargs:
  48. self.container_run_kwargs["ports"] = {}
  49. self.container_run_kwargs["ports"].update(
  50. {
  51. "{}/tcp".format(self.ssh_port): self.ssh_port,
  52. "{}/tcp".format(self.sshd_port): self.sshd_port,
  53. "{}/tcp".format(self.libvirt_tcp_port): self.libvirt_tcp_port,
  54. "{}/tcp".format(self.libvirt_tls_port): self.libvirt_tls_port,
  55. }
  56. )
  57. if "volumes" not in self.container_run_kwargs:
  58. self.container_run_kwargs["volumes"] = {}
  59. self.container_run_kwargs["volumes"].update(
  60. {
  61. RUNTIME_VARS.CODE_DIR: {"bind": "/salt", "mode": "z"},
  62. RUNTIME_VARS.CODE_DIR: {"bind": RUNTIME_VARS.CODE_DIR, "mode": "z"},
  63. }
  64. )
  65. self.container_run_kwargs["working_dir"] = RUNTIME_VARS.CODE_DIR
  66. self.container_run_kwargs["network_mode"] = "host"
  67. self.container_run_kwargs["cap_add"] = ["ALL"]
  68. self.container_run_kwargs["privileged"] = True
  69. super().__attrs_post_init__()
  70. self.python_executable = "python3"
  71. def _container_start_checks(self):
  72. # Once we're able to ls the salt-minion script it means the container
  73. # has salt installed
  74. ret = self.run("ls", "-lah", self.get_script_path())
  75. if ret.exitcode == 0:
  76. return True
  77. time.sleep(1)
  78. return False