48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
import sys
|
||
|
|
import argparse
|
||
|
|
import urllib.request
|
||
|
|
import urllib.error
|
||
|
|
|
||
|
|
def fetch_config(ip, data_id, group, tenant):
|
||
|
|
url = f"http://{ip}:8848/nacos/v1/cs/configs?dataId={data_id}&group={group}&tenant={tenant}"
|
||
|
|
print(f"Trying to fetch config from {url}...")
|
||
|
|
try:
|
||
|
|
with urllib.request.urlopen(url, timeout=5) as response:
|
||
|
|
if response.status == 200:
|
||
|
|
return response.read().decode('utf-8')
|
||
|
|
except urllib.error.URLError as e:
|
||
|
|
print(f"Failed to connect to {ip}: {e}")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error fetching config from {ip}: {e}")
|
||
|
|
return None
|
||
|
|
|
||
|
|
def main():
|
||
|
|
parser = argparse.ArgumentParser(description="Fetch configuration from Nacos via HTTP.")
|
||
|
|
parser.add_argument("--hosts", nargs='+', default=["192.168.3.200", "192.168.3.230"], help="List of Nacos server IPs")
|
||
|
|
parser.add_argument("--data-id", required=True, help="Nacos Data ID (e.g., redis.yml)")
|
||
|
|
parser.add_argument("--group", default="DEFAULT_GROUP", help="Nacos Group (default: DEFAULT_GROUP)")
|
||
|
|
parser.add_argument("--tenant", default="dev", help="Nacos Tenant (default: dev)")
|
||
|
|
|
||
|
|
args = parser.parse_args()
|
||
|
|
|
||
|
|
config_content = None
|
||
|
|
for ip in args.hosts:
|
||
|
|
config_content = fetch_config(ip, args.data_id, args.group, args.tenant)
|
||
|
|
if config_content:
|
||
|
|
print(f"Successfully fetched config from {ip}")
|
||
|
|
break
|
||
|
|
|
||
|
|
if config_content:
|
||
|
|
print("-" * 40)
|
||
|
|
print(f"CONTENT OF {args.data_id}:")
|
||
|
|
print("-" * 40)
|
||
|
|
print(config_content)
|
||
|
|
print("-" * 40)
|
||
|
|
else:
|
||
|
|
print(f"Failed to fetch config '{args.data_id}' from all provided hosts.")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|