func AddHosts(poolName string, hosts map[string]cfg.Host) error { if poolName == "" { return errors.New("Please specify a pool name to add the hosts to") } else if len(hosts) == 0 { return errors.New("Please specify at least one host to add to the pool") } return routerzk.AddHosts(zkConn.Conn, poolName, hosts) }
func (e *UpdatePoolExecutor) Execute(t *Task) error { if e.arg.Pool.Name == "" { return errors.New("Please specify a name") } else if e.arg.Pool.Config.HealthzEvery == "" { return errors.New("Please specify a healthz check frequency") } else if e.arg.Pool.Config.HealthzTimeout == "" { return errors.New("Please specify a healthz timeout") } else if e.arg.Pool.Config.RequestTimeout == "" { return errors.New("Please specify a request timeout") } // no need to check hosts. an empty pool is still a valid pool helper.SetRouterRoot(e.arg.Pool.Internal) err := routerzk.SetPool(datamodel.Zk.Conn, e.arg.Pool) if err != nil { e.reply.Status = StatusError return err } // diff pools and update existingHosts, err := routerzk.GetHosts(datamodel.Zk.Conn, e.arg.Pool.Name) if err != nil { e.reply.Status = StatusError return err } delHosts := []string{} for name, _ := range existingHosts { if _, ok := e.arg.Pool.Hosts[name]; !ok { delHosts = append(delHosts, name) } } newHosts := map[string]routercfg.Host{} for name, newHost := range e.arg.Pool.Hosts { if _, ok := existingHosts[name]; !ok { newHosts[name] = newHost } } err = routerzk.AddHosts(datamodel.Zk.Conn, e.arg.Pool.Name, newHosts) if err != nil { e.reply.Status = StatusError return err } err = routerzk.DelHosts(datamodel.Zk.Conn, e.arg.Pool.Name, delHosts) if err != nil { e.reply.Status = StatusError return err } e.reply.Status = StatusOk return nil }
func AddToPool(containers []string) error { pools := map[bool]map[string][]*ZkInstance{} pools[true] = map[string][]*ZkInstance{} pools[false] = map[string][]*ZkInstance{} for _, cont := range containers { inst, err := GetInstance(cont) if err != nil { // instance doesn't exist continue } name := helper.CreatePoolName(inst.App, inst.Sha, inst.Env) zkApp, err := GetApp(inst.App) if err != nil { return err } currInsts := pools[zkApp.Internal][name] if currInsts == nil { currInsts = []*ZkInstance{} } pools[zkApp.Internal][name] = append(currInsts, inst) } for internal, allPools := range pools { helper.SetRouterRoot(internal) for name, insts := range allPools { // create pool if we need to if exists, err := routerzk.PoolExists(Zk.Conn, name); !exists || err != nil { if err = routerzk.SetPool(Zk.Conn, defaultPool(name, internal)); err != nil { return err } } // add hosts hosts := map[string]routercfg.Host{} for _, inst := range insts { address := fmt.Sprintf("%s:%d", inst.Host, inst.Port) hosts[address] = routercfg.Host{Address: address} } if err := routerzk.AddHosts(Zk.Conn, name, hosts); err != nil { return err } } } return nil }