def test_balancer_detach_member(self): balancer = self.driver.get_balancer(balancer_id="lba-1235f") member = Member("i-64bd081c", None, None) self.assertTrue(balancer.detach_member(member))
def _to_member(self, obj, port, balancer): return Member( id=obj["id"], ip=obj["nic"][0]["ipaddress"], port=port, balancer=balancer )
def create_lb(kwargs=None, call=None): r''' Create a load-balancer configuration. CLI Example: .. code-block:: bash salt-cloud -f create_lb dimensiondata \ name=dev-lb port=80 protocol=http \ members=w1,w2,w3 algorithm=ROUND_ROBIN ''' conn = get_conn() if call != 'function': raise SaltCloudSystemExit( 'The create_lb function must be called with -f or --function.' ) if not kwargs or 'name' not in kwargs: log.error( 'A name must be specified when creating a health check.' ) return False if 'port' not in kwargs: log.error( 'A port or port-range must be specified for the load-balancer.' ) return False if 'networkdomain' not in kwargs: log.error( 'A network domain must be specified for the load-balancer.' ) return False if 'members' in kwargs: members = [] ip = "" membersList = kwargs.get('members').split(',') log.debug('MemberList: %s', membersList) for member in membersList: try: log.debug('Member: %s', member) node = get_node(conn, member) log.debug('Node: %s', node) ip = node.private_ips[0] except Exception as err: log.error( 'Failed to get node ip: %s', err, # Show the traceback if the debug logging level is enabled exc_info_on_loglevel=logging.DEBUG ) members.append(Member(ip, ip, kwargs['port'])) else: members = None log.debug('Members: %s', members) networkdomain = kwargs['networkdomain'] name = kwargs['name'] port = kwargs['port'] protocol = kwargs.get('protocol', None) algorithm = kwargs.get('algorithm', None) lb_conn = get_lb_conn(conn) network_domains = conn.ex_list_network_domains() network_domain = [y for y in network_domains if y.name == networkdomain][0] log.debug('Network Domain: %s', network_domain.id) lb_conn.ex_set_current_network_domain(network_domain.id) __utils__['cloud.fire_event']( 'event', 'create load_balancer', 'salt/cloud/loadbalancer/creating', args=kwargs, sock_dir=__opts__['sock_dir'], transport=__opts__['transport'] ) lb = lb_conn.create_balancer( name, port, protocol, algorithm, members ) __utils__['cloud.fire_event']( 'event', 'created load_balancer', 'salt/cloud/loadbalancer/created', args=kwargs, sock_dir=__opts__['sock_dir'], transport=__opts__['transport'] ) return _expand_balancer(lb)
def _to_member(self, el, balancer=None): member = Member(id=el["ip"]["id"], ip=el["ip"]["ip"], port=el["port"], balancer=balancer) return member
def _to_member(self, el): lbmember = Member(id=el["id"], ip=el["address"], port=el["port"]) return lbmember
from libcloud.loadbalancer.base import Member, Algorithm from libcloud.loadbalancer.types import Provider from libcloud.loadbalancer.providers import get_driver ACCESS_ID = "your access id" SECRET_KEY = "your secret key" cls = get_driver(Provider.ELB) driver = cls(key=ACCESS_ID, secret=SECRET_KEY) print(driver.list_balancers()) # members associated with the load balancer members = (Member(None, "192.168.88.1", 8000), Member(None, "192.168.88.2", 8080)) new_balancer = driver.create_balancer( name="MyLB", algorithm=Algorithm.ROUND_ROBIN, port=80, protocol="http", members=members, ) print(new_balancer)
def _node_to_member(self, data): return Member(data['id'], None, None)
def test_balancer_attach_member(self): balancer = self.driver.get_balancer(balancer_id="lba-1235f") member = balancer.attach_member(Member("srv-kg983", ip=None, port=None)) self.assertEqual(member.id, "srv-kg983")
def run_until_success(func, lb, member, max_retry=10): success = False attempt = 0 while not success: try: func(lb, member) except Exception: print "sleeping...", Exception time.sleep(5) max_retry -= 1 if max_retry <= 0: print 'hit max retrys, bailing...' raise Exception continue success = True # members that need to be added m_add = etcd_state - lb_state for m in m_add: new_member = Member(id=None, ip=m[0], port=m[1]) print "adding:", new_member run_until_success(driver.balancer_attach_member, lb, new_member) # members that need to be deleted m_delete = lb_state - etcd_state for m in m_delete: member = member_lookup[m] print "removing:", member run_until_success(driver.balancer_detach_member, lb, member)
def test_balancer_detach_member(self): balancer = self.driver.get_balancer(balancer_id="76265") member = Member("226227", None, None) self.assertTrue(balancer.detach_member(member))
ecs = ECSDriver(your_access_key_id, your_access_key_secret, region=region) protos = slb.list_protocols() print("Found %d protocols: %s" % (len(protos), protos)) balancers = slb.list_balancers() print("Found %d load balancers" % len(balancers)) print(balancers) if len(balancers) > 0: b1 = balancers[0] print("Delete %s" % b1) slb.destroy_balancer(b1) else: extra = { "AddressType": "internet", "Bandwidth": 1, "StickySession": "off", "HealthCheck": "off", } nodes = ecs.list_nodes() print("Found %d nodes" % len(nodes)) members = [ Member(node.id, node.public_ips[0], 80, extra={"Weight": 50 * (i + 1)}) for i, node in enumerate(nodes) ] new_b = slb.create_balancer( "test-balancer", 80, "http", Algorithm.WEIGHTED_ROUND_ROBIN, members, **extra ) print("Created balancer %s" % new_b)
from libcloud.loadbalancer.base import DEFAULT_ALGORITHM, Member from libcloud.loadbalancer.types import Provider from libcloud.loadbalancer.providers import get_driver USER_NAME = "your user name" SECRET_KEY = "your secret key" cls = get_driver(Provider.SOFTLAYER) driver = cls(key=USER_NAME, secret=SECRET_KEY) balancer = driver.list_balancers()[0] if balancer.port < 0: # no front-end port defined, configure it with such one driver.ex_configure_load_balancer(balancer, port=80, protocol="http", algorithm=DEFAULT_ALGORITHM) member1 = balancer.attach_member(Member(None, "192.168.88.1", 8000)) member2 = balancer.attach_member(Member(None, "192.168.88.2", 8080)) print(balancer.list_members()) balancer.detach_member(member1) print(balancer.list_members()) balancer.detach_member(member2) print(balancer.list_members())
def test_balancer_detach_member(self): balancer = self.driver.get_balancer(balancer_id='76265') member = Member('226227', None, None) self.assertTrue(balancer.detach_member(member))
def _node_to_member(self, data, balancer): return Member(id=data['id'], ip=None, port=None, balancer=balancer)
def test_create_balancer(self): members = [Member(1, '1.1.1.1', 80), Member(2, '1.1.1.2', 80)] balancer = self.driver.create_balancer('fake', members) self.assertTrue(isinstance(balancer, LoadBalancer))
def _to_member(self, obj, port): return Member(id=obj['id'], ip=obj['nic'][0]['ipaddress'], port=port)
import time from pprint import pprint from libcloud.loadbalancer.base import Member, Algorithm from libcloud.loadbalancer.types import State, Provider from libcloud.loadbalancer.providers import get_driver driver = get_driver(Provider.RACKSPACE_US)("username", "api key") name = "test-lb" members = (Member(None, "192.168.86.1", 8080), Member(None, "192.168.86.2", 8080)) print("Creating load balancer") new_balancer = driver.create_balancer( name=name, algorithm=Algorithm.ROUND_ROBIN, port=80, protocol="http", members=members, ) print("Waiting for load balancer to become ready...") while True: balancer = driver.get_balancer(balancer_id=new_balancer.id) if balancer.state == State.RUNNING: break print("Load balancer not ready yet, sleeping 20 seconds...") time.sleep(20)
def _to_member(self, el): member = Member(id=el["ip"]["id"], ip=el["ip"]["ip"], port=el["port"]) return member
def test_create_balancer(self): members = [Member(1, '1.1.1.1', 80), Member(2, '1.1.1.2', 80)] balancer = self.driver.create_balancer(name='test', algorithm=Algorithm.ROUND_ROBIN, members=members) self.assertTrue(isinstance(balancer, LoadBalancer))
def create_lb(kwargs=None, call=None): r""" Create a load-balancer configuration. CLI Example: .. code-block:: bash salt-cloud -f create_lb dimensiondata \ name=dev-lb port=80 protocol=http \ members=w1,w2,w3 algorithm=ROUND_ROBIN """ conn = get_conn() if call != "function": raise SaltCloudSystemExit( "The create_lb function must be called with -f or --function.") if not kwargs or "name" not in kwargs: log.error("A name must be specified when creating a health check.") return False if "port" not in kwargs: log.error( "A port or port-range must be specified for the load-balancer.") return False if "networkdomain" not in kwargs: log.error("A network domain must be specified for the load-balancer.") return False if "members" in kwargs: members = [] ip = "" membersList = kwargs.get("members").split(",") log.debug("MemberList: %s", membersList) for member in membersList: try: log.debug("Member: %s", member) node = get_node(conn, member) # pylint: disable=not-callable log.debug("Node: %s", node) ip = node.private_ips[0] except Exception as err: # pylint: disable=broad-except log.error( "Failed to get node ip: %s", err, # Show the traceback if the debug logging level is enabled exc_info_on_loglevel=logging.DEBUG, ) members.append(Member(ip, ip, kwargs["port"])) else: members = None log.debug("Members: %s", members) networkdomain = kwargs["networkdomain"] name = kwargs["name"] port = kwargs["port"] protocol = kwargs.get("protocol", None) algorithm = kwargs.get("algorithm", None) lb_conn = get_lb_conn(conn) network_domains = conn.ex_list_network_domains() network_domain = [y for y in network_domains if y.name == networkdomain][0] log.debug("Network Domain: %s", network_domain.id) lb_conn.ex_set_current_network_domain(network_domain.id) event_data = _to_event_data(kwargs) __utils__["cloud.fire_event"]( "event", "create load_balancer", "salt/cloud/loadbalancer/creating", args=event_data, sock_dir=__opts__["sock_dir"], transport=__opts__["transport"], ) lb = lb_conn.create_balancer(name, port, protocol, algorithm, members) event_data = _to_event_data(kwargs) __utils__["cloud.fire_event"]( "event", "created load_balancer", "salt/cloud/loadbalancer/created", args=event_data, sock_dir=__opts__["sock_dir"], transport=__opts__["transport"], ) return _expand_balancer(lb)
def test_balancer_attach_member(self): balancer = self.driver.list_balancers()[0] member = Member(id=1234, ip='1.1.1.1', port=80) balancer.attach_member(member)
def test_create_balancer_multiple_member_ports_exception(self): members = [Member('m1', '1.2.3.4', 80), Member('m2', '1.2.3.5', 81)] self.assertRaises(AttributeError, self.driver.create_balancer, None, 80, 'http', Algorithm.WEIGHTED_ROUND_ROBIN, members)
def test_balancer_attach_member(self): balancer = self.driver.get_balancer(balancer_id='8290') member = balancer.attach_member(Member(None, ip='10.1.0.12', port='80')) self.assertEquals(member.ip, '10.1.0.12') self.assertEquals(member.port, 80)
import time from pprint import pprint from libcloud.loadbalancer.base import Member, Algorithm from libcloud.loadbalancer.types import State, Provider from libcloud.loadbalancer.providers import get_driver driver = get_driver(Provider.RACKSPACE_US)('username', 'api key') name = 'test-lb' members = (Member(None, '192.168.86.1', 8080), Member(None, '192.168.86.2', 8080)) print('Creating load balancer') new_balancer = driver.create_balancer(name=name, algorithm=Algorithm.ROUND_ROBIN, port=80, protocol='http', members=members) print('Waiting for load balancer to become ready...') while True: balancer = driver.get_balancer(balancer_id=new_balancer.id) if balancer.state == State.RUNNING: break print('Load balancer not ready yet, sleeping 20 seconds...') time.sleep(20) print('Load balancer is ready')
def test_balancer_attach_member(self): balancer = self.driver.get_balancer(balancer_id='lba-1235f') member = balancer.attach_member(Member('srv-kg983', ip=None, port=None)) self.assertEquals(member.id, 'srv-kg983')
from libcloud.loadbalancer.base import Member, Algorithm from libcloud.loadbalancer.types import Provider from libcloud.loadbalancer.providers import get_driver ACCESS_ID = 'your access id' SECRET_KEY = 'your secret key' cls = get_driver(Provider.ELB) driver = cls(key=ACCESS_ID, secret=SECRET_KEY) print(driver.list_balancers()) # members associated with the load balancer members = (Member(None, '192.168.88.1', 8000), Member(None, '192.168.88.2', 8080)) new_balancer = driver.create_balancer(name='MyLB', algorithm=Algorithm.ROUND_ROBIN, port=80, protocol='http', members=members) print(new_balancer)
def test_balancer_detach_member(self): balancer = self.driver.get_balancer(balancer_id='lba-1235f') member = Member('srv-lv426', None, None) self.assertTrue(balancer.detach_member(member))
def test_balancer_detach_member(self): self.balancer = self.driver.get_balancer(balancer_id="tests") self.member = Member("i-23tshnsdq", None, None) self.assertTrue(self.balancer.detach_member(self.member))
from libcloud.loadbalancer.base import DEFAULT_ALGORITHM, Member from libcloud.loadbalancer.types import Provider from libcloud.loadbalancer.providers import get_driver USER_NAME = 'your user name' SECRET_KEY = 'your secret key' cls = get_driver(Provider.SOFTLAYER) driver = cls(key=USER_NAME, secret=SECRET_KEY) balancer = driver.list_balancers()[0] if balancer.port < 0: # no front-end port defined, configure it with such one driver.ex_configure_load_balancer(balancer, port=80, protocol='http', algorithm=DEFAULT_ALGORITHM) member1 = balancer.attach_member(Member(None, '192.168.88.1', 8000)) member2 = balancer.attach_member(Member(None, '192.168.88.2', 8080)) print(balancer.list_members()) balancer.detach_member(member1) print(balancer.list_members()) balancer.detach_member(member2) print(balancer.list_members())