Esempio n. 1
0
func checkRole(role string, rType string) error {
	log.Printf("[CheckRole] checking myself (%s:%s) for %s:%s", Region, Host, rType, role)
	zkManager, err := datamodel.GetManager(Region, Host)
	if err != nil {
		return err
	}
	log.Printf("[CheckRole] roles: %v", zkManager.Roles)
	if !zkManager.HasRole(role, rType) {
		log.Printf("[CheckRole] role check fail.")
		managersWithRole := ""
		managers, err := datamodel.ListManagers()
		if err != nil {
			return err
		}
		for region, rManagers := range managers {
			for _, manager := range rManagers {
				zm, err := datamodel.GetManager(region, manager)
				if err != nil {
					continue
				}
				if zm.HasRole(role, rType) {
					managersWithRole = managersWithRole + zm.ManagerCName + "\n"
				}
			}
		}
		return errors.New(fmt.Sprintf("This manager does not have the ability to %s %s. "+
			"Please try one of these:\n%s", rType, role, managersWithRole))
	}
	log.Printf("[CheckRole] role check success.")
	return nil
}
Esempio n. 2
0
func Unregister(region, value string) error {
	zkManager, err := datamodel.GetManager(region, value)
	if err != nil {
		return err
	}
	if dns.Provider == nil {
		// if we have no dns provider then just save here
		return zkManager.Delete()
	}
	records := []string{}
	if zkManager.ManagerRecordID != "" {
		records = append(records, zkManager.ManagerRecordID)
	}
	if zkManager.RegistryRecordID != "" {
		records = append(records, zkManager.RegistryRecordID)
	}
	if len(records) > 0 {
		err, errChan := dns.Provider.DeleteRecords(region, "DELETE_MANAGER "+value+" in "+region, records...)
		if err != nil {
			return err
		}
		err = <-errChan // wait for it to propagate
		if err != nil {
			return err
		}
	}
	return zkManager.Delete()
}
Esempio n. 3
0
func (e *RemoveRoleExecutor) Execute(t *Task) error {
	if e.arg.Host == "" {
		return errors.New("Please specify a host")
	}
	if e.arg.Region == "" {
		return errors.New("Please specify a region")
	}
	if e.arg.Role == "" {
		return errors.New("Please specify a role")
	}
	zkManager, err := datamodel.GetManager(e.arg.Region, e.arg.Host)
	if err != nil {
		e.reply.Status = StatusError
		return err
	}
	err = zkManager.RemoveRole(e.arg.Role, e.arg.Type)
	if err != nil {
		e.reply.Status = StatusError
		return err
	}
	e.reply.Status = StatusOk
	castedManager := Manager(*zkManager)
	e.reply.Manager = &castedManager
	return err
}
Esempio n. 4
0
func (e *GetSelfExecutor) Execute(t *Task) error {
	zkManager, err := datamodel.GetManager(Host, Region)
	castedManager := Manager(*zkManager)
	e.reply.Manager = &castedManager
	if err != nil {
		e.reply.Status = StatusError
	}
	e.reply.Status = StatusOk
	return err
}
Esempio n. 5
0
func selfRegister() {
	log.Println("[SelfRegister] Registering Self.")
	zkManager, err := datamodel.GetManager(Region, Host)
	if err == nil && zkManager != nil {
		// i'm already registered
		log.Println("[SelfRegister] Already Registered")
		return
	}
	mgr, err := manager.Register(Region, Host, "", "")
	if err != nil {
		log.Fatalln("[SelfRegister] Failure: ", err)
	}
	log.Printf("[SelfRegister] Success: %s", mgr.ManagerCName)
}
Esempio n. 6
0
func (e *RequestAppDependencyExecutor) Execute(t *Task) error {
	if e.arg.App == "" {
		e.reply.Status = StatusError
		return errors.New("Please specify an app")
	}
	if e.arg.Dependency == "" {
		e.reply.Status = StatusError
		return errors.New("Please specify an app to depend on")
	}
	if len(e.arg.Envs) == 0 {
		e.reply.Status = StatusError
		return errors.New("Please specify the envs your app needs the dependency in")
	}
	for _, env := range e.arg.Envs {
		if _, err := datamodel.GetEnv(env); err != nil {
			e.reply.Status = StatusError
			return errors.New("The env " + env + " does not exist")
		}
	}
	// fetch apps
	zkApp, err := datamodel.GetApp(e.arg.App)
	if err != nil {
		e.reply.Status = StatusError
		return err
	}
	zkDep, err := datamodel.GetApp(e.arg.Dependency)
	if err != nil {
		e.reply.Status = StatusError
		return err
	}
	// check depender envs
	missingEnvs := []string{}
	if dad := zkDep.GetDependerAppData(e.arg.App, false); dad != nil {
		for _, env := range e.arg.Envs {
			if dad.DependerEnvData[env] == nil {
				missingEnvs = append(missingEnvs, env)
			}
		}
	} else {
		missingEnvs = e.arg.Envs
	}
	if len(missingEnvs) == 0 {
		return errors.New(fmt.Sprintf("Your app already has access to the dependency %s in envs %v",
			e.arg.Dependency, e.arg.Envs))
	}

	// load template, format body, and set up subject
	subject := fmt.Sprintf("[Atlantis] '%s' is requesting '%s' as a dependency in envs %s", e.arg.App,
		e.arg.Dependency, strings.Join(e.arg.Envs, ","))

	tmpl := template.Must(template.New("request_dependency").Parse(`
{{.DepTeam}},

{{.User}} has requested that you add '{{.App}}' as a depender of your app '{{.Dependency}}' in the the environment(s) '{{.Envs}}'.

Please visit this page to do so: https://{{.ManagerCName}}/static/dashboard/#addAppDepender/{{.Dependency}}/{{.App}}/{{.Envs}}

If you wish to discuss this dependency, {{.AppTeam}} is included in this thread for your convenience.
`))
	myself, err := datamodel.GetManager(Region, Host)
	if err != nil {
		return err
	}
	buf := bytes.NewBuffer([]byte{})
	tmpl.Execute(buf, RequestAppDependencyTemplate{
		App:          e.arg.App,
		AppTeam:      strings.SplitN(zkApp.Email, "@", 2)[0],
		Dependency:   e.arg.Dependency,
		DepTeam:      strings.SplitN(zkDep.Email, "@", 2)[0],
		Envs:         strings.Join(e.arg.Envs, ","),
		ManagerCName: myself.ManagerCName,
		User:         e.arg.ManagerAuthArg.User,
	})
	// send email requesting dependency
	if err := smtp.SendMail([]string{zkDep.Email, zkApp.Email}, subject, buf.String()); err != nil {
		e.reply.Status = StatusError
		return err
	}
	e.reply.Status = StatusOk
	return nil
}
Esempio n. 7
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()
}