nacos-config-interactive.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # @Author : wangyeuwen
  4. import http.client
  5. import urllib.parse
  6. import re
  7. def get_params() -> dict:
  8. params = {
  9. 'host': '127.0.0.1',
  10. 'port': '8848',
  11. 'tenant': '',
  12. 'group': 'SEATA_GROUP',
  13. 'username': '',
  14. 'password': ''
  15. }
  16. host = input("Please enter the host of nacos.\n请输入nacos的host [localhost]\n>>> ")
  17. port = input("Please enter the port of nacos.\n请输入nacos的port [8848]\n>>> ")
  18. group = input("Please enter the group of nacos.\n请输入nacos的group [SEATA_GROUP]\n>>> ")
  19. tenant = input("Please enter the tenant of nacos.\n请输入nacos的tenant\n>>> ")
  20. username = input("Please enter the username of nacos.\n请输入nacos的username\n>>> ")
  21. password = input("Please enter the password of nacos.\n请输入nacos的password\n>>> ")
  22. confirm = input("Are you sure to continue? [y/n]")
  23. if confirm[0] == 'y' or confirm[0] == 'Y':
  24. if len(host) != 0: params['host'] = host
  25. if len(port) != 0: params['port'] = port
  26. if len(group) != 0: params['group'] = group
  27. if len(tenant) != 0: params['tenant'] = tenant
  28. if len(username) != 0: params['username'] = username
  29. if len(password) != 0: params['password'] = password
  30. elif confirm[0] == 'n' or confirm[0] == 'N':
  31. exit()
  32. else:
  33. print("Just enter y or n, please.")
  34. exit()
  35. return params
  36. def error_exit():
  37. print(' init nacos config fail.')
  38. exit()
  39. def get_pair(line: str) -> tuple:
  40. res = re.match(r"([\.\w]+)=(.*)", line)
  41. return res.groups() if res is not None else ['', '']
  42. headers = {
  43. 'content-type': "application/x-www-form-urlencoded"
  44. }
  45. hasError = False
  46. params = get_params()
  47. url_prefix = f"{params['host']}:{params['port']}"
  48. tenant = params['tenant']
  49. username = params['username']
  50. password = params['password']
  51. group = params['group']
  52. url_postfix_base = f'/nacos/v1/cs/configs?group={group}&tenant={tenant}'
  53. if username != '' and password != '':
  54. url_postfix_base += f'&username={username}&password={password}'
  55. if url_prefix == ':':
  56. error_exit()
  57. for line in open('../config.txt'):
  58. pair = get_pair(line.rstrip("\n"))
  59. if len(pair) < 2 or pair[0] == '' or pair[0].startswith("#") or pair[1] == '':
  60. continue
  61. url_postfix = url_postfix_base + f'&dataId={urllib.parse.quote(str(pair[0]))}&content={urllib.parse.quote(str(pair[1])).strip()}'
  62. conn = http.client.HTTPConnection(url_prefix)
  63. conn.request("POST", url_postfix, headers=headers)
  64. res = conn.getresponse()
  65. data = res.read().decode("utf-8")
  66. if data != "true":
  67. hasError = True
  68. print(f"{pair[0]}={pair[1]} {data if hasError else 'success'}")
  69. if hasError:
  70. print("init nacos config fail.")
  71. else:
  72. print("init nacos config finished, please start seata-server.")