dot_pythonrc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- mode: Python;-*-
  2. from pprint import pprint as pp
  3. import ipaddress
  4. import json
  5. import os
  6. class MongoHms:
  7. """Connect to Mongo database and provide methods to control HMS."""
  8. import pymongo
  9. def __init__(self):
  10. self.myclient = self.pymongo.MongoClient(
  11. "mongodb://"
  12. + "admin"
  13. + ":" + os.getenv("HMS_MONGO_PASSWORD")
  14. + "@" + ",".join(["hms01-mr.intr:27017",
  15. "hms02-mr.intr:27017",
  16. "hms03-mr.intr:27017"])
  17. + "/admin?replicaSet=hms-rs0"
  18. )
  19. self.service = self.myclient["staffResourceController"]["service"]
  20. self.socket = self.myclient["staffResourceController"]["socket"]
  21. self.service_socket = self.myclient["staffResourceController"]["serviceSocket"]
  22. self.installer = self.myclient["appscat"]["installer"]
  23. def ip_address_to_int(self, ip_address):
  24. """Convert IP address to integer."""
  25. return int(ipaddress.ip_address(ip_address))
  26. def web_find_socket(self, socket_name, server):
  27. """Return socket by SOCKET_NAME on SERVER."""
  28. return [socket for socket in self.socket.find({"name": socket_name + "@" + server})]
  29. def web_socket_id(self, socket_name, server):
  30. """Return socket_id by SOCKET_NAME on SERVER."""
  31. return str(self.web_find_socket(socket_name, server)[0]["_id"])
  32. def web_create_socket(self, socket_name, ip_address, protocol, server, port):
  33. """Create socket with specified SOCKET_NAME, IP_ADDRESS, PROTOCOL on SERVER PORT."""
  34. if not self.web_find_socket(socket_name, server):
  35. self.socket.insert_one({"protocol": protocol,
  36. "address": self.ip_address_to_int(ip_address),
  37. "port": port,
  38. "name": socket_name + "@" + server,
  39. "switchedOn": True,
  40. "_class" : "NetworkSocket"}).inserted_id
  41. return self.web_socket_id(socket_name, server)
  42. def web_service_name_to_server_id(self, service_name, server):
  43. """Return server id from SERVICE_NAME on SERVER."""
  44. return self.service.find_one({"name": service_name + "@" + server})['serverId']
  45. def web_find_service(self, service_name, server):
  46. """Return list of services matching SERVICE_NAME on SERVER."""
  47. return [service for service in self.service.find({"name": service_name + "@" + server})]
  48. def web_service_id(self, service_name, server):
  49. """Return service id from SERVICE_NAME on SERVER."""
  50. return str(self.web_find_service(service_name, server)[0]["_id"])
  51. def web_create_service(self, service_name, template_id, server, server_id, socket):
  52. """Create service with specified SERVICE_NAME, TEMPLATE_ID on SERVER with SERVER_ID on SOCKET."""
  53. if not self.web_find_service(service_name, server):
  54. self.service.insert_one({
  55. "serverId": server_id,
  56. "templateId": template_id,
  57. "socketIds": [socket],
  58. "instanceProps": {
  59. "security_level": "default"
  60. },
  61. "name": service_name + "@" + server,
  62. "switchedOn": True,
  63. "_class": "ru.majordomo.hms.rc.staff.resources.Service"
  64. })
  65. return self.web_service_id(service_name, server)
  66. def web_create_ssh_service(self, server, ip_address):
  67. """Create ssh service on SERVER with IP_ADDRESS."""
  68. return self.web_create_service(
  69. "ssh-guest-room",
  70. "5d6693a6f7619300012fbbd5",
  71. "5d8a2a2e708301660013776d",
  72. server,
  73. self.web_service_name_to_server_id('nginx', server),
  74. self.web_create_socket("ssh-guest-room-ssh", ip_address, "ssh", server, "1022")
  75. )
  76. def web_create_php_service(self, template_id, version, server, ip_address, port):
  77. """Create php service with specified TEMPLATE_ID with php VERSION on SERVER with IP_ADDRESS on PORT."""
  78. service = "apache2-php" + version + "-default"
  79. return self.web_create_service(
  80. service,
  81. template_id,
  82. server,
  83. self.web_service_name_to_server_id("nginx", server),
  84. self.web_create_socket(service + "-http", ip_address, "http", server, port),
  85. )
  86. def task_set_error(self, account_id):
  87. """Set all running task on ACCOUNT_ID (e.g. "208112") as processed."""
  88. return self.myclient["personmgr"]["processingBusinessOperation"].update_many({
  89. "personalAccountId":account_id,
  90. "state":"PROCESSING"
  91. }, {"$set": {"state":"ERROR"}})
  92. def task_set_processed(self, account_id):
  93. """Set all running task on ACCOUNT_ID (e.g. "208112") as processed."""
  94. return self.myclient["personmgr"]["processingBusinessOperation"].update_many({
  95. "personalAccountId":account_id,
  96. "state":"PROCESSING"
  97. }, {"$set": {"state":"PROCESSED"}})
  98. def wordpress(self, version):
  99. from datetime import datetime
  100. current_time = datetime.utcnow()
  101. self.installer.insert_one({
  102. "name" : f"WordPress {version} Installer",
  103. "dockerImage" : f"docker-registry.intr/apps/wordpress:{version}",
  104. "requiredParams" : [
  105. "DB_HOST",
  106. "APP_PATH",
  107. "APP_TITLE",
  108. "DB_NAME",
  109. "ADMIN_USERNAME",
  110. "DB_PASSWORD",
  111. "DB_USER",
  112. "ADMIN_EMAIL",
  113. "ADMIN_PASSWORD",
  114. "DOMAIN_NAME"
  115. ],
  116. "created" : current_time,
  117. "updated" : current_time,
  118. "_class" : "ru.majordomo.hms.appscat.model.Installer"
  119. })