Created
October 31, 2018 04:59
-
-
Save jordic/cd4790ec9ab33b752fefad62ee6d6e2a to your computer and use it in GitHub Desktop.
Apache LB controller python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import requests | |
_nonce = r'&nonce=([a-f0-9\-]+)' | |
class Balancer(object): | |
def __init__(self, url, balancer="myzopelb"): | |
self.url = url | |
self.nonce = None | |
self.balancer = balancer | |
def get_nonce(self): | |
if self.nonce == None: | |
res = requests.get(self.url) | |
import pdb; pdb.set_trace() | |
self.nonce = re.findall(_nonce, res.text)[0] | |
return self.nonce | |
def set_status(self, instance, action): | |
query = ( | |
"{url}?b={balancer}&w=http://127.0.0.1:80" | |
"{ins}&dw={action}&nonce={nonce}".format( | |
url=self.url, | |
balancer=self.balancer, | |
ins=instance, | |
action=action, | |
nonce=self.get_nonce() | |
)) | |
return requests.get(query) | |
def enable(self, instance): | |
self.set_status(instance, "Enable") | |
def disable(self, instance): | |
self.set_status(instance, "Disable") | |
if __name__ == "__main__": | |
b = Balancer("http://localhost:9981/balancer-manager") | |
b.enable(80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment