예제 #1
0
func createRecordSets(internal bool, zone, value string, zkRouter *datamodel.ZkRouter) ([]dns.Record, error) {
	var suffix string
	var err error
	if suffix, err = preCreateRecordSets(internal, zone, value, zkRouter); err != nil {
		return nil, err
	}

	records := make([]dns.Record, 3)

	// WEIGHT=1 router.<suffix>
	records[0] = dns.NewRecord(helper.GetRegionRouterCName(internal, suffix), value, 1)

	// WEIGHT=1 routerX.<zone>.<suffix>
	records[1] = dns.NewRecord(zkRouter.CName, value, 1)

	// WEIGHT=1 router.<zone>.<suffix>
	records[2] = dns.NewRecord(helper.GetZoneRouterCName(internal, zkRouter.Zone, suffix), value, 1)

	/*// WEIGHT=0 router.<zone>.<suffix> -> will be activated when needed
	for _, azone := range AvailableZones {
		if azone == zone {
			continue
		}
		record := dns.NewRecord(helper.GetZoneRouterCName(internal, azone, suffix), value, 0)
		records = append(records, record)
	}*/ // we don't need this yet
	return records, nil
}
예제 #2
0
func Register(region, value, registryCName, managerCName string) (*datamodel.ZkManager, error) {
	if tmpManager, err := datamodel.GetManager(region, value); err == nil {
		return tmpManager, errors.New("Already registered.")
	}

	// NOTE[jigish]: health check removed because we can't actually do it security-group wise.

	// set up datamodel
	zkManager := datamodel.Manager(region, value)
	err := zkManager.Save()
	if err != nil {
		return zkManager, err
	}

	// pass through specified cnames
	if managerCName != "" {
		zkManager.ManagerCName = managerCName
	}
	if registryCName != "" {
		zkManager.RegistryCName = registryCName
	}
	err = zkManager.Save()
	if err != nil {
		return zkManager, err
	}

	if dns.Provider == nil || (zkManager.ManagerCName != "" && zkManager.RegistryCName != "") {
		return zkManager, nil
	}

	suffix, err := dns.Provider.Suffix(region)
	if err != nil {
		return nil, err
	}

	// set up unspecified cnames
	// first delete all entries we may already have for this Value in DNS
	err = dns.DeleteRecordsForValue(region, value)
	if err != nil {
		return zkManager, err
	}
	// choose cnames
	managers, err := datamodel.ListManagersInRegion(region)
	if err != nil {
		return zkManager, err
	}
	managerMap := map[string]bool{}
	registryMap := map[string]bool{}
	for _, manager := range managers {
		tmpManager, err := datamodel.GetManager(region, manager)
		if err != nil {
			return zkManager, err
		}
		managerMap[tmpManager.ManagerCName] = true
		registryMap[tmpManager.RegistryCName] = true
	}

	cnames := []dns.Record{}
	if zkManager.ManagerCName == "" {
		managerNum := 1
		zkManager.ManagerCName = helper.GetManagerCName(managerNum, suffix)
		for ; managerMap[zkManager.ManagerCName]; managerNum++ {
			zkManager.ManagerCName = helper.GetManagerCName(managerNum, suffix)
		}
		// managerX.<region>.<suffix>
		cname := dns.NewRecord(zkManager.ManagerCName, zkManager.Host, 0)
		zkManager.ManagerRecordID = cname.ID()
		cnames = append(cnames, cname)
	}
	if zkManager.RegistryCName == "" {
		registryNum := 1
		zkManager.RegistryCName = helper.GetRegistryCName(registryNum, suffix)
		for ; registryMap[zkManager.RegistryCName]; registryNum++ {
			zkManager.RegistryCName = helper.GetRegistryCName(registryNum, suffix)
		}
		// registryX.<region>.<suffix>
		cname := dns.NewRecord(zkManager.RegistryCName, zkManager.Host, 0)
		zkManager.RegistryRecordID = cname.ID()
		cnames = append(cnames, cname)
	}

	if len(cnames) == 0 {
		return zkManager, nil
	}
	err = dns.Provider.CreateRecords(region, "CREATE_MANAGER "+zkManager.Host+" in "+region, cnames)
	if err != nil {
		return zkManager, err
	}
	return zkManager, zkManager.Save()
}