Esempio n. 1
0
// BindApp makes the bind between the service instance and an app.
func (si *ServiceInstance) BindApp(app bind.App) error {
	err := si.AddApp(app.GetName())
	if err != nil {
		return &errors.Http{Code: http.StatusConflict, Message: "This app is already bound to this service instance."}
	}
	err = si.update()
	if err != nil {
		return err
	}
	if len(app.GetUnits()) == 0 {
		return &errors.Http{Code: http.StatusPreconditionFailed, Message: "This app does not have an IP yet."}
	}
	envs := make(chan map[string]string, len(app.GetUnits())+1)
	for _, unit := range app.GetUnits() {
		go func(unit bind.Unit) {
			vars, _ := si.BindUnit(app, unit)
			envs <- vars
		}(unit)
	}
	var envVars []bind.EnvVar
	for k, v := range <-envs {
		envVars = append(envVars, bind.EnvVar{
			Name:         k,
			Value:        v,
			Public:       false,
			InstanceName: si.Name,
		})
	}
	return app.SetEnvs(envVars, false)
}
Esempio n. 2
0
func (si *ServiceInstance) Bind(app bind.App) error {
	err := si.AddApp(app.GetName())
	if err != nil {
		return &errors.Http{Code: http.StatusConflict, Message: "This app is already binded to this service instance."}
	}
	var envVars []bind.EnvVar
	var setEnv = func(env map[string]string) {
		for k, v := range env {
			envVars = append(envVars, bind.EnvVar{
				Name:         k,
				Value:        v,
				Public:       false,
				InstanceName: si.Name,
			})
		}
	}
	cli := si.Service().ProductionEndpoint()
	if len(app.GetUnits()) == 0 {
		return &errors.Http{Code: http.StatusPreconditionFailed, Message: "This app does not have an IP yet."}
	}
	env, err := cli.Bind(si, app)
	if err != nil {
		return err
	}
	setEnv(env)
	err = si.update()
	if err != nil {
		cli.Unbind(si, app)
		return err
	}
	return app.SetEnvs(envVars, false)
}