nacos-config-interactive.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one or more
  5. # contributor license agreements. See the NOTICE file distributed with
  6. # this work for additional information regarding copyright ownership.
  7. # The ASF licenses this file to You under the Apache License, Version 2.0
  8. # (the "License"); you may not use this file except in compliance with
  9. # the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. import http.client
  20. import urllib.parse
  21. import re
  22. def get_params() -> dict:
  23. params = {
  24. 'host': '127.0.0.1',
  25. 'port': '8848',
  26. 'tenant': '',
  27. 'group': 'SEATA_GROUP',
  28. 'username': '',
  29. 'password': ''
  30. }
  31. host = input("Please enter the host of nacos.\n请输入nacos的host [localhost]\n>>> ")
  32. port = input("Please enter the port of nacos.\n请输入nacos的port [8848]\n>>> ")
  33. group = input("Please enter the group of nacos.\n请输入nacos的group [SEATA_GROUP]\n>>> ")
  34. tenant = input("Please enter the tenant of nacos.\n请输入nacos的tenant\n>>> ")
  35. username = input("Please enter the username of nacos.\n请输入nacos的username\n>>> ")
  36. password = input("Please enter the password of nacos.\n请输入nacos的password\n>>> ")
  37. confirm = input("Are you sure to continue? [y/n]")
  38. if confirm[0] == 'y' or confirm[0] == 'Y':
  39. if len(host) != 0: params['host'] = host
  40. if len(port) != 0: params['port'] = port
  41. if len(group) != 0: params['group'] = group
  42. if len(tenant) != 0: params['tenant'] = tenant
  43. if len(username) != 0: params['username'] = username
  44. if len(password) != 0: params['password'] = password
  45. elif confirm[0] == 'n' or confirm[0] == 'N':
  46. exit()
  47. else:
  48. print("Just enter y or n, please.")
  49. exit()
  50. return params
  51. def error_exit():
  52. print(' init nacos config fail.')
  53. exit()
  54. def get_pair(line: str) -> tuple:
  55. res = re.match(r"([\.\w]+)=(.*)", line)
  56. return res.groups() if res is not None else ['', '']
  57. headers = {
  58. 'content-type': "application/x-www-form-urlencoded"
  59. }
  60. hasError = False
  61. params = get_params()
  62. url_prefix = f"{params['host']}:{params['port']}"
  63. tenant = params['tenant']
  64. username = params['username']
  65. password = params['password']
  66. group = params['group']
  67. url_postfix_base = f'/nacos/v1/cs/configs?group={group}&tenant={tenant}'
  68. if username != '' and password != '':
  69. url_postfix_base += f'&username={username}&password={password}'
  70. if url_prefix == ':':
  71. error_exit()
  72. for line in open('../config.txt'):
  73. pair = get_pair(line.rstrip("\n"))
  74. if len(pair) < 2 or pair[0] == '' or pair[0].startswith("#") or pair[1] == '':
  75. continue
  76. url_postfix = url_postfix_base + f'&dataId={urllib.parse.quote(str(pair[0]))}&content={urllib.parse.quote(str(pair[1])).strip()}'
  77. conn = http.client.HTTPConnection(url_prefix)
  78. conn.request("POST", url_postfix, headers=headers)
  79. res = conn.getresponse()
  80. data = res.read().decode("utf-8")
  81. if data != "true":
  82. hasError = True
  83. print(f"{pair[0]}={pair[1]} {data if hasError else 'success'}")
  84. if hasError:
  85. print("init nacos config fail.")
  86. else:
  87. print("init nacos config finished, please start seata-server.")