func validateDeploy(auth *ManagerAuthArg, manifest *Manifest, sha, env string, t *Task) (deps map[string]DepsType, err error) {
	t.LogStatus("Validate Deploy")
	// authorize that we're allowed to use the app
	if err = AuthorizeApp(auth, manifest.Name); err != nil {
		return nil, errors.New("Permission Denied: " + err.Error())
	}
	// fetch the environment
	t.LogStatus("Fetching Environment")
	zkEnv, err := datamodel.GetEnv(env)
	if err != nil {
		return nil, errors.New("Environment Error: " + err.Error())
	}
	// lock the deploy
	dl := datamodel.NewDeployLock(t.ID, manifest.Name, sha, env)
	if err := dl.Lock(); err != nil {
		return nil, err
	}
	defer dl.Unlock()
	if manifest.Instances <= 0 {
		return nil, errors.New(fmt.Sprintf("Invalid Number of Instances: %d", manifest.Instances))
	}
	if manifest.CPUShares < 0 ||
		(manifest.CPUShares > 0 && manifest.CPUShares != 1 && manifest.CPUShares%CPUSharesIncrement != 0) {
		return nil, errors.New(fmt.Sprintf("CPU Shares should be 1 or a multiple of %d", CPUSharesIncrement))
	}
	if manifest.MemoryLimit < 0 ||
		(manifest.MemoryLimit > 0 && manifest.MemoryLimit%MemoryLimitIncrement != 0) {
		return nil, errors.New(fmt.Sprintf("Memory Limit should be a multiple of %d", MemoryLimitIncrement))
	}
	t.LogStatus("Resolving Dependencies")
	return ResolveDepValues(manifest.Name, zkEnv, manifest.DepNames(), true, t)
}
Beispiel #2
0
func (e *ResolveDepsExecutor) Execute(t *Task) error {
	zkEnv, err := datamodel.GetEnv(e.arg.Env)
	if err != nil {
		return errors.New("Environment Error: " + err.Error())
	}
	e.reply.Deps, err = ResolveDepValues(e.arg.App, zkEnv, e.arg.DepNames, false, t)
	if err != nil {
		return err
	}
	e.reply.Status = StatusOk
	return nil
}
Beispiel #3
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
}