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