Example #1
0
func CleanupCreatedPoolRefs(internal bool, app, sha, env string) error {
	helper.SetRouterRoot(internal)
	// remove static rule, cleanup rule from trie if needed
	ruleName := helper.GetAppShaEnvStaticRuleName(app, sha, env)
	trieName := helper.GetAppEnvTrieName(app, env)
	// remove static rule from trie
	trie, err := routerzk.GetTrie(Zk.Conn, trieName)
	if err != nil {
		return err
	}
	newRules := []string{}
	for _, rule := range trie.Rules {
		if rule != ruleName {
			newRules = append(newRules, rule)
		}
	}
	if len(trie.Rules) != len(newRules) {
		trie.Rules = newRules
		err = routerzk.SetTrie(Zk.Conn, trie)
		if err != nil {
			return err
		}
	}
	// delete static rule
	err = routerzk.DelRule(Zk.Conn, ruleName)
	if err != nil {
		return err
	}
	return nil
}
Example #2
0
func UpdateAppEnvTrie(internal bool, app, sha, env string) (string, error) {
	helper.SetRouterRoot(internal)
	// create trie (if it doesn't exist)
	trieName := helper.GetAppEnvTrieName(app, env)
	if exists, err := routerzk.TrieExists(Zk.Conn, trieName); !exists || err != nil {
		err = routerzk.SetTrie(Zk.Conn, routercfg.Trie{
			Name:     trieName,
			Rules:    []string{},
			Internal: internal,
		})
		if err != nil {
			return trieName, err
		}
	}
	// if sha != "" attach pool as static rule (if trie is empty)
	if sha != "" {
		// if static rule does not exist, create it
		ruleName := helper.GetAppShaEnvStaticRuleName(app, sha, env)
		poolName := helper.CreatePoolName(app, sha, env)
		if exists, err := routerzk.RuleExists(Zk.Conn, ruleName); !exists || err != nil {
			err = routerzk.SetRule(Zk.Conn, routercfg.Rule{
				Name:     ruleName,
				Type:     "static",
				Value:    "true",
				Pool:     poolName,
				Internal: internal,
			})
			if err != nil {
				return trieName, err
			}
		}
		trie, err := routerzk.GetTrie(Zk.Conn, trieName)
		if err != nil {
			return trieName, err
		}
		if len(trie.Rules) == 0 {
			trie.Rules = []string{ruleName}
		} else {
			trie.Rules = append(trie.Rules, ruleName)
		}
		if err = routerzk.SetTrie(Zk.Conn, trie); err != nil {
			return trieName, err
		}
	}
	return trieName, nil
}
Example #3
0
func SetTrie(trie cfg.Trie) error {

	if trie.Name == "" {
		return errors.New("Please specify a name")
	} else if len(trie.Rules) <= 0 {
		return errors.New("Please specify a rule")
	}

	return routerzk.SetTrie(zkConn.Conn, trie)
}
Example #4
0
func (e *UpdateTrieExecutor) Execute(t *Task) (err error) {
	if e.arg.Trie.Name == "" {
		return errors.New("Please specify a name")
	}
	helper.SetRouterRoot(e.arg.Trie.Internal)
	err = routerzk.SetTrie(datamodel.Zk.Conn, e.arg.Trie)
	if err != nil {
		e.reply.Status = StatusError
	} else {
		e.reply.Status = StatusOk
	}
	return err
}