Docker.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import docker
  2. from docker.errors import APIError
  3. class Docker:
  4. def __init__(self):
  5. self.client = docker.from_env()
  6. def exec_command(self, container_name, cmd, user=None):
  7. """
  8. Execute a command in a container by its container name.
  9. :param container_name: The name of the container.
  10. :param cmd: The command to execute as a list (e.g., ["ls", "-la"]).
  11. :param user: The user to execute the command as (optional).
  12. :return: A standardized response with status, output, and exit_code.
  13. """
  14. filters = {"name": container_name}
  15. try:
  16. for container in self.client.containers.list(filters=filters):
  17. exec_result = container.exec_run(cmd, user=user)
  18. return {
  19. "status": "success",
  20. "exit_code": exec_result.exit_code,
  21. "output": exec_result.output.decode("utf-8")
  22. }
  23. except APIError as e:
  24. return {
  25. "status": "error",
  26. "exit_code": "APIError",
  27. "output": str(e)
  28. }
  29. except Exception as e:
  30. return {
  31. "status": "error",
  32. "exit_code": "Exception",
  33. "output": str(e)
  34. }
  35. def start_container(self, container_name):
  36. """
  37. Start a container by its container name.
  38. :param container_name: The name of the container.
  39. :return: A standardized response with status, output, and exit_code.
  40. """
  41. filters = {"name": container_name}
  42. try:
  43. for container in self.client.containers.list(filters=filters):
  44. container.start()
  45. return {
  46. "status": "success",
  47. "exit_code": "0",
  48. "output": f"Container '{container_name}' started successfully."
  49. }
  50. except APIError as e:
  51. return {
  52. "status": "error",
  53. "exit_code": "APIError",
  54. "output": str(e)
  55. }
  56. except Exception as e:
  57. return {
  58. "status": "error",
  59. "error_type": "Exception",
  60. "output": str(e)
  61. }
  62. def stop_container(self, container_name):
  63. """
  64. Stop a container by its container name.
  65. :param container_name: The name of the container.
  66. :return: A standardized response with status, output, and exit_code.
  67. """
  68. filters = {"name": container_name}
  69. try:
  70. for container in self.client.containers.list(filters=filters):
  71. container.stop()
  72. return {
  73. "status": "success",
  74. "exit_code": "0",
  75. "output": f"Container '{container_name}' stopped successfully."
  76. }
  77. except APIError as e:
  78. return {
  79. "status": "error",
  80. "exit_code": "APIError",
  81. "output": str(e)
  82. }
  83. except Exception as e:
  84. return {
  85. "status": "error",
  86. "exit_code": "Exception",
  87. "output": str(e)
  88. }
  89. def restart_container(self, container_name):
  90. """
  91. Restart a container by its container name.
  92. :param container_name: The name of the container.
  93. :return: A standardized response with status, output, and exit_code.
  94. """
  95. filters = {"name": container_name}
  96. try:
  97. for container in self.client.containers.list(filters=filters):
  98. container.restart()
  99. return {
  100. "status": "success",
  101. "exit_code": "0",
  102. "output": f"Container '{container_name}' restarted successfully."
  103. }
  104. except APIError as e:
  105. return {
  106. "status": "error",
  107. "exit_code": "APIError",
  108. "output": str(e)
  109. }
  110. except Exception as e:
  111. return {
  112. "status": "error",
  113. "exit_code": "Exception",
  114. "output": str(e)
  115. }