Skip to content

Instantly share code, notes, and snippets.

@harryge00
Last active May 13, 2020 02:09
Show Gist options
  • Save harryge00/bc06bd5dded1158422571aaf17f1ed4f to your computer and use it in GitHub Desktop.
Save harryge00/bc06bd5dded1158422571aaf17f1ed4f to your computer and use it in GitHub Desktop.
Add backend/frontend pair for DC/OS edge-lb pool
#!/usr/bin/env python
import json
import os
import toml
from os.path import expanduser
import requests
import argparse
import sys
import re
from urlparse import urljoin
# initiate the parser
parser = argparse.ArgumentParser()
# add long and short argument
parser.add_argument("--edgelb", "-e", help="edgelb service name", default="edgelb")
parser.add_argument("--pool", "-po", help="edgelb pool name", required=True)
parser.add_argument("--protocol", "-pr", help="protocol", default="TCP")
parser.add_argument("--framework", "-fw", help="edgelb framework name")
parser.add_argument("--taskNamePattern", "-t", help="taskNamePattern")
parser.add_argument("--serviceID", "-s", help="marathon service id")
parser.add_argument("--portName", "-pn", help="portName of the backend")
parser.add_argument("--backendPort", "-b", help="port of the backend", type=int)
parser.add_argument("--frontPort", "-fp", help="frontend port", type=int, required=True)
# read arguments from the command line
args = parser.parse_args()
if not (args.framework or args.serviceID):
parser.error('One of --framework or --serviceID must be given')
if not (args.portName or args.backendPort):
parser.error('One of --portName or --backendPort must be given')
if args.framework and not args.taskNamePattern:
parser.error('Both of --framework and --taskNamePattern must be given')
home = expanduser("~")
dcosdir = os.path.join(home, ".dcos")
for root, dirs, files in os.walk(dcosdir, topdown=False):
for name in files:
if name == "attached":
tomlPath = os.path.join(root, "dcos.toml")
print("Current cluster config toml:", tomlPath)
dcosConfig = toml.load(tomlPath)
if not dcosConfig['core']:
print("Invalid config")
sys.exit(1)
dcosToken = dcosConfig['core']['dcos_acs_token']
dcosUrl = dcosConfig['core']['dcos_url']
edgelbUrl = urljoin(dcosUrl, "service/%s/v2/pools/%s" % (args.edgelb, args.pool) )
header = {
"Content-Type": "application/json",
'Authorization': 'token=' + dcosToken
}
print(edgelbUrl)
# sending get request and saving the response as response object
r = requests.get(url = edgelbUrl, headers = header)
pool = r.json()
print("current pool:", pool)
frontends = pool['haproxy']['frontends']
backends = pool['haproxy']['backends']
def convertBackendName(backendName):
if backendName[0] == "/":
backendName = backendName[1:]
return backendName.replace("/", "-")
mesos = {}
marathon = {}
if args.framework and args.taskNamePattern:
backendName = "%s-%s" % (convertBackendName(args.framework), re.sub('[^a-z0-9-]+', '', args.taskNamePattern))
mesos = {
"frameworkName" : args.framework,
"taskNamePattern" : args.taskNamePattern
}
if args.backendPort:
backendName = "%s-%d" % (backendName, args.backendPort)
endpoint = {
"port": args.backendPort
}
else:
backendName = "%s-%s" % (backendName, args.portName)
endpoint = {
"portName": args.portName
}
else:
marathon = {
"serviceID": args.serviceID
}
if args.backendPort:
backendName = "%s-%d" % (convertBackendName(args.serviceID), args.backendPort)
endpoint = {
"port": args.backendPort
}
else:
backendName = "%s-%s" % (convertBackendName(args.serviceID), args.portName)
endpoint = {
"portName": args.portName
}
endpoint["type"] = "AUTO_IP"
backend = {
"name": backendName,
"protocol": args.protocol,
"services": [
{
"endpoint": endpoint,
"marathon": marathon,
"mesos": mesos
}
]
}
frontend = {
"bindPort": args.frontPort,
"protocol": args.protocol,
"linkBackend": {
"defaultBackend": backendName
}
}
print(backend)
print(frontend)
backends.append(backend)
frontends.append(frontend)
putBody = json.dumps(pool)
print(putBody)
r = requests.put(edgelbUrl, headers=header, data=putBody)
print(r.status_code, r.content)
@harryge00
Copy link
Author

harryge00 commented Dec 19, 2019

  • Expose mysql (framework: my-13309, portName: mysqlproxy, taskNamePattern: haproxy ) on port 13309
python add-backend-frontend.py --pool mysql-lb --framework  my-13309 --portName mysqlproxy  --taskNamePattern haproxy -frontPort 13309 
  • Expose the marathon service /unicom/redis-16379/slave, whose port is 6379 on haproxy's 16380
add-backend-frontend.py --pool general-lb --serviceID /unicom/redis-16379/slave --backendPort 6379  --frontPort 16380

@harryge00
Copy link
Author

TODO

目前只提供了加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment