Skip to content

Instantly share code, notes, and snippets.

Last active February 15, 2016 17:12
Show Gist options
  • Save andaag/4b340ac88922f4b7517a to your computer and use it in GitHub Desktop.
Save andaag/4b340ac88922f4b7517a to your computer and use it in GitHub Desktop.
A script to copy some eureka instances from a remote eureka to a local one. I use it to pull some dev eurekas into a docker-compose setup.
import json
import urllib
import urllib.request
import gzip
import os
### Need to setup some env's first
PROXY = os.environ["EUREKA_PROXY"]
proxy_list = PROXY.strip().split(",")
def grab_file(url):
req = urllib.request.Request(url, headers={'Accept': 'application/json', "Accept-Encoding": "gzip",})
response = urllib.request.urlopen(req)
data = urllib.request.urlopen(req).read()
if'Content-Encoding') == 'gzip':
data = gzip.decompress(data)
return json.loads(data.decode("utf-8"))
def register_instance(url, payload):
payload = json.dumps(payload).encode('utf8')
req = urllib.request.Request(url, data=payload,
headers={'Content-Type': 'application/json', "Accept-Encoding": "gzip",}
response = urllib.request.urlopen(req)
return response
source_eureka = grab_file(EUREKA_SOURCE + "v2/apps")
target_eureka = grab_file(EUREKA_TARGET + "v2/apps")
print("Running apps on source", EUREKA_SOURCE,":", ", ".join([v["name"] for v in source_eureka["applications"]["application"]]))
print("Looking for:", proxy_list)
apps = source_eureka["applications"]["application"]
def copyfields(instance):
r = {}
fields = ["hostName","app","ipAddr","vipAddress","secureVipAddress","status","port","securePort","homePageUrl","statusPageUrl","healthCheckUrl"]
for field in fields:
if field in instance:
r[field] = instance[field]
return {"instance":r}
for app in apps:
if app["name"] in proxy_list:
print("Found app", app["name"])
for instance in app["instance"]:
ip = instance["ipAddr"]
status = instance["status"]
instance = copyfields(instance)
status = register_instance(EUREKA_TARGET + "v2/apps/" + app["name"], instance).status
print("\t\tRegistered with status:", status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment