nacos-config.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 sys
  21. import getopt as opts
  22. import urllib.parse
  23. import re
  24. def get_params() -> dict:
  25. params = {
  26. '-h': '127.0.0.1',
  27. '-p': '8848',
  28. '-t': '',
  29. '-g': 'SEATA_GROUP',
  30. '-u': '',
  31. '-w': ''
  32. }
  33. inputs, args = opts.getopt(sys.argv[1:], shortopts='h:p:t:g:u:w:')
  34. for k, v in inputs:
  35. params[k] = v
  36. print(params)
  37. return params
  38. def error_exit():
  39. print('python nacos-config.py [-h host] [-p port] [-t tenant] [-g group] [-u username] [-w password]')
  40. exit()
  41. def get_pair(line: str) -> tuple:
  42. res = re.match(r"([\.\w]+)=(.*)",line)
  43. return res.groups() if res is not None else ['','']
  44. headers = {
  45. 'content-type': "application/x-www-form-urlencoded"
  46. }
  47. hasError = False
  48. params = get_params()
  49. url_prefix = f"{params['-h']}:{params['-p']}"
  50. tenant = params['-t']
  51. username = params['-u']
  52. password = params['-w']
  53. group = params['-g']
  54. url_postfix_base = f'/nacos/v1/cs/configs?group={group}&tenant={tenant}'
  55. if username != '' and password != '':
  56. url_postfix_base += f'&username={username}&password={password}'
  57. if url_prefix == ':':
  58. error_exit()
  59. for line in open('../config.txt'):
  60. pair = get_pair(line.rstrip("\n"))
  61. if len(pair) < 2 or pair[0] == '' or pair[0].startswith("#") or pair[1] == '':
  62. continue
  63. url_postfix = url_postfix_base + f'&dataId={urllib.parse.quote(str(pair[0]))}&content={urllib.parse.quote(str(pair[1])).strip()}'
  64. conn = http.client.HTTPConnection(url_prefix)
  65. conn.request("POST", url_postfix, headers=headers)
  66. res = conn.getresponse()
  67. data = res.read().decode("utf-8")
  68. if data != "true":
  69. hasError = True
  70. print(f"{pair[0]}={pair[1]} {data if hasError else 'success'}")
  71. if hasError:
  72. print("init nacos config fail.")
  73. else:
  74. print("init nacos config finished, please start seata-server.")