示例#1
0
func (e *UpdateRuleExecutor) Execute(t *Task) (err error) {
	if e.arg.Rule.Name == "" {
		return errors.New("Please specify a name")
	} else if e.arg.Rule.Type == "" {
		return errors.New("Please specify a type")
	} else if e.arg.Rule.Value == "" {
		return errors.New("Please specify a value")
	} else if e.arg.Rule.Next == "" && e.arg.Rule.Pool == "" {
		return errors.New("Please specify either a next trie or a pool")
	}
	// fill in current cname suffixes in multi-host rules
	if e.arg.Rule.Type == "multi-host" && dns.Provider != nil {
		suffix, err := dns.Provider.Suffix(Region)
		if err != nil {
			return err
		}
		list := strings.Join(helper.GetAppCNameSuffixes(suffix), ",")
		e.arg.Rule.Value = fmt.Sprintf("%s:%s", e.arg.Rule.Value, list)
	}

	helper.SetRouterRoot(e.arg.Rule.Internal)
	err = routerzk.SetRule(datamodel.Zk.Conn, e.arg.Rule)
	if err != nil {
		e.reply.Status = StatusError
	} else {
		e.reply.Status = StatusOk
	}
	return err
}
示例#2
0
func SetRule(rule cfg.Rule) error {

	if rule.Name == "" {
		return errors.New("Please specify a name")
	} else if rule.Type == "" {
		return errors.New("Please specify a type")
	} else if rule.Value == "" {
		return errors.New("Please specify a value")
	} else if rule.Next == "" {
		return errors.New("Please specify a next value")
	} else if rule.Pool == "" {
		return errors.New("Please specify a pool")
	}

	return routerzk.SetRule(zkConn.Conn, rule)
}
示例#3
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
}