Beispiel #1
0
func DeleteHosts(poolName string, hosts []string) error {

	if poolName == "" {
		return errors.New("Please specify a pool name to delete hosts from")
	} else if len(hosts) == 0 {
		return errors.New("Please specifiy at least one host to delete from the pool")
	}

	return routerzk.DelHosts(zkConn.Conn, poolName, hosts)
}
Beispiel #2
0
func DeleteFromPool(containers []string) error {
	pools := map[bool]map[string]*poolDefinition{}
	pools[true] = map[string]*poolDefinition{}
	pools[false] = map[string]*poolDefinition{}
	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
		}
		poolDef := pools[zkApp.Internal][name]
		if poolDef == nil {
			poolDef = &poolDefinition{
				app:   inst.App,
				sha:   inst.Sha,
				env:   inst.Env,
				insts: []*ZkInstance{},
			}
			pools[zkApp.Internal][name] = poolDef
		}
		pools[zkApp.Internal][name].insts = append(poolDef.insts, inst)
	}
	for internal, allPools := range pools {
		helper.SetRouterRoot(internal)
		for name, poolDef := range allPools {
			// remove hosts
			hosts := []string{}
			for _, inst := range poolDef.insts {
				hosts = append(hosts, fmt.Sprintf("%s:%d", inst.Host, inst.Port))
			}
			routerzk.DelHosts(Zk.Conn, name, hosts)
			// delete pool if no hosts exist
			getHosts, err := routerzk.GetHosts(Zk.Conn, name)
			if err != nil || len(getHosts) == 0 {
				err = routerzk.DelPool(Zk.Conn, name)
				if err != nil {
					log.Println("Error trying to clean up pool:", err)
				}
				err = CleanupCreatedPoolRefs(internal, poolDef.app, poolDef.sha, poolDef.env)
				if err != nil {
					log.Println("Error trying to clean up pool:", err)
				}
			}
		}
	}
	return nil
}
Beispiel #3
0
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
}