cloudtest_actions.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from textwrap import dedent
  10. from typing import Any
  11. from materialize.checks.actions import Action
  12. from materialize.checks.executors import Executor
  13. from materialize.cloudtest.app.materialize_application import MaterializeApplication
  14. from materialize.cloudtest.k8s.environmentd import EnvironmentdStatefulSet
  15. from materialize.mz_version import MzVersion
  16. class ReplaceEnvironmentdStatefulSet(Action):
  17. """Change the image tag of the environmentd stateful set, re-create the definition and replace the existing one."""
  18. new_tag: str | None
  19. def __init__(self, new_tag: str | None = None) -> None:
  20. self.new_tag = new_tag
  21. def execute(self, e: Executor) -> None:
  22. new_version = (
  23. MzVersion.parse_mz(self.new_tag)
  24. if self.new_tag
  25. else MzVersion.parse_cargo()
  26. )
  27. print(
  28. f"Replacing environmentd stateful set from version {e.current_mz_version} to version {new_version}"
  29. )
  30. mz = e.cloudtest_application()
  31. stateful_set = [
  32. resource
  33. for resource in mz.resources
  34. if type(resource) == EnvironmentdStatefulSet
  35. ]
  36. assert len(stateful_set) == 1
  37. stateful_set = stateful_set[0]
  38. stateful_set.tag = self.new_tag
  39. stateful_set.replace()
  40. e.current_mz_version = new_version
  41. def join(self, e: Executor) -> None:
  42. # execute is blocking already
  43. pass
  44. class SetupSshTunnels(Action):
  45. """Prepare the SSH tunnels."""
  46. def __init__(self, mz: MaterializeApplication) -> None:
  47. self.handle: Any | None = None
  48. self.mz = mz
  49. def execute(self, e: Executor) -> None:
  50. connection_count = 4
  51. self.handle = e.testdrive(
  52. "\n".join(
  53. [
  54. dedent(
  55. f"""
  56. > CREATE CONNECTION IF NOT EXISTS ssh_tunnel_{i} TO SSH TUNNEL (
  57. HOST 'ssh-bastion-host',
  58. USER 'mz',
  59. PORT 22
  60. );
  61. """
  62. )
  63. for i in range(connection_count)
  64. ]
  65. )
  66. )
  67. for i in range(connection_count):
  68. public_key = self.mz.environmentd.sql_query(
  69. "SELECT public_key_1 FROM mz_ssh_tunnel_connections ssh"
  70. " JOIN mz_connections c ON c.id = ssh.id"
  71. f" WHERE c.name = 'ssh_tunnel_{i}';"
  72. )[0][0]
  73. # Add public key to SSH bastion host
  74. self.mz.kubectl(
  75. "exec",
  76. "svc/ssh-bastion-host",
  77. "--",
  78. "bash",
  79. "-c",
  80. f"echo '{public_key}' >> /etc/authorized_keys/mz",
  81. )
  82. def join(self, e: Executor) -> None:
  83. e.join(self.handle)