You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

botserver.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import docker
  2. import os
  3. import getpass
  4. import logging
  5. _image = "dokoliho/botserver"
  6. _uid = os.getuid()
  7. _username = getpass.getuser()
  8. _containername = "botserver_" + _username
  9. _port = 62187 + _uid
  10. _logger = logging.getLogger(__name__)
  11. _from_env=False
  12. _verbose=False
  13. def _client():
  14. if _from_env:
  15. client = docker.from_env()
  16. else:
  17. client = docker.DockerClient(base_url='tcp://172.17.0.1:2375')
  18. return client
  19. def _get_running_container():
  20. client = _client()
  21. try:
  22. return client.containers.get(_containername)
  23. except:
  24. msg = f"Container {_containername} not found."
  25. if _verbose: print(msg)
  26. _logger.info(msg)
  27. return None
  28. def connect_remote_daemon(flag):
  29. global _from_env
  30. _from_env = not flag
  31. def stop_container():
  32. container = _get_running_container()
  33. if container is not None:
  34. container.stop()
  35. container.remove()
  36. msg = f"Container {_containername} stopped."
  37. if _verbose: print(msg)
  38. _logger.info(msg)
  39. def start_container(verbose=False):
  40. global _verbose
  41. _verbose = verbose
  42. stop_container()
  43. client = _client()
  44. try:
  45. client.containers.run(_image,
  46. command=["sleep", "inf"],
  47. detach=True,
  48. name=_containername,
  49. ports={63187: _port})
  50. msg = f"Container {_containername} started with port {_port}"
  51. if _verbose: print(msg)
  52. _logger.info(msg)
  53. return _port
  54. except Exception as e:
  55. if _verbose: print(str(e))
  56. _logger.warning(str(e))
  57. return None
  58. def exec_in_container(command):
  59. container = _get_running_container()
  60. if container == None:
  61. msg = f"Container {_containername} not running."
  62. _logger.warning(msg)
  63. raise Exception(msg)
  64. try:
  65. msg = f"Executing {command}"
  66. if _verbose: print(msg)
  67. _logger.info(msg)
  68. result = container.exec_run(command, stream=True)
  69. except Exception as e:
  70. print(str(e))
  71. _logger.warning(str(e))
  72. return
  73. for line in result.output:
  74. print(line.decode("utf-8"))
  75. msg = f"Execution of {command} finished."
  76. if _verbose: print(msg)
  77. _logger.info(msg)
  78. def exec_synced_in_container(command):
  79. container = _get_running_container()
  80. if container == None:
  81. msg = f"Container {_containername} not running."
  82. _logger.warning(msg)
  83. raise Exception(msg)
  84. try:
  85. msg = f"Executing (sync) {command}"
  86. if _verbose: print(msg)
  87. _logger.info(msg)
  88. exit_code, output = container.exec_run(command)
  89. except Exception as e:
  90. print(str(e))
  91. return
  92. print(output.decode("utf-8") )
  93. msg = f"Execution of {command} finished."
  94. if _verbose: print(msg)
  95. _logger.info(msg)
  96. def print_logs():
  97. container = _get_running_container()
  98. if container == None:
  99. msg = f"Container {_containername} not running."
  100. _logger.warning(msg)
  101. raise Exception(msg)
  102. print(container.logs().decode("utf-8"))
  103. if __name__ == "__main__":
  104. connect_remote_daemon(False)
  105. start_container(verbose=True)
  106. # exec_in_container("echo 'hello world'")
  107. exec_in_container("/bots/server/bots training")
  108. stop_container()