def configuredProxyManagerFactory(configuration): """ A factory for generating a ProxyManager, complete with pre-created services, groups, and data from a configuration file. Most of the work here is mapping configuration to models. The collection of models is what is passed to the proxyManagerFactory. """ services = [] # build services from configuration for serviceName, serviceConf in configuration.services.items(): addresses = [util.splitHostPort(x) for x in serviceConf.listen] pservice = model.ProxyService(serviceName, addresses=addresses) # build groups for groupName, groupConf in serviceConf.groups.items(): # build hosts pgroup = model.ProxyGroup(groupName, groupConf.scheduler, groupConf.isEnabled()) for hostName, hostConf in groupConf.hosts.items(): host, port = util.splitHostPort(hostConf.ip) phost = model.ProxyHost(hostName, host, port, hostConf.weight) # add the host to the group pgroup.addHost(phost) # add the group to the service pservice.addGroup(pgroup) # add the service to the service collection services.append(pservice) # call the proxyManagerFactory and return it return manager.proxyManagerFactory(services)
def configuredProxyManagerFactory(configuration): """ A factory for generating a ProxyManager, complete with pre-created services, groups, and data from a configuration file. Most of the work here is mapping configuration to models. The collection of models is what is passed to the proxyManagerFactory. """ services = [] # build services from configuration for serviceName, serviceConf in configuration.services.items(): addresses = [util.splitHostPort(x) for x in serviceConf.listen] pservice = model.ProxyService(serviceName, addresses=addresses) # build groups for groupName, groupConf in serviceConf.groups.items(): # build hosts pgroup = model.ProxyGroup( groupName, groupConf.scheduler, groupConf.isEnabled) for hostName, hostConf in groupConf.hosts.items(): host, port = util.splitHostPort(hostConf.ip) phost = model.ProxyHost(hostName, host, port, hostConf.weight) # add the host to the group pgroup.addHost(phost) # add the group to the service pservice.addGroup(pgroup) # add the service to the service collection services.append(pservice) # call the proxyManagerFactory and return it return manager.proxyManagerFactory(services)
def initApplication(): global overlay application = service.Application('Demo LB Service') # This is because you have to add a first host. It will not be used for # anything. proxyServices = [HostMapper(proxy='0.0.0.0:8080', lbType=leastc, host='host0', address='127.0.0.1:10000'),] overlay = Overlay() overlay.init(12345, 12346) pm = manager.proxyManagerFactory(proxyServices) tr = pm.getTracker("proxy1", "group1") def start_workers(_): d = Deferred() # load number of workers f = open("config.yaml", "r") config = yaml.load(f) f.close() N = config["workers"] print "Start %i number of workers!" % N #N = 2 def _start_worker(_): deferred = deferToThread(overlay.aws.start_worker) def _addHost(w): send_log("INFO", "Started new host %s:10000" % str(w.instances[0].private_ip_address)) tr.newHost((w.instances[0].private_ip_address, 3000), "host" + str(id)) return w deferred.addCallback(_addHost) return deferred for id in range(1,N+1): d.addCallback(_start_worker) d.callback(0) return d def add_workers(_): for w in overlay.aws.workers: send_log("INFO", "Added host " + str(w.instances[0].private_ip_address)) tr.newHost((w.instances[0].private_ip_address, 3000), "host" + str(id)) id += 1 overlay.d.addCallbacks(add_workers, start_workers) def remove_default_worker(_): tr.delHost('127.0.0.1:10000') overlay.d.addBoth(remove_default_worker) for s in pm.services: print s lbs = LoadBalancedService(pm) #configuration = config.Config("config.xml") #print pm.trackers os = LoadBalanceService(pm.getTracker('proxy1', 'group1'), pm) os.setServiceParent(application) lbs.setServiceParent(application) atexit.register(before_exit) return application