func nodeHealingUpdate(w http.ResponseWriter, r *http.Request, t auth.Token) error { err := r.ParseForm() if err != nil { return err } poolName := r.FormValue("pool") if poolName == "" { if !permission.Check(t, permission.PermHealingUpdate) { return permission.ErrUnauthorized } } else { if !permission.Check(t, permission.PermHealingUpdate, permission.Context(permission.CtxPool, poolName)) { return permission.ErrUnauthorized } } var config healer.NodeHealerConfig delete(r.Form, "pool") dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err = dec.DecodeValues(&config, r.Form) if err != nil { return err } return healer.UpdateConfig(poolName, config) }
// title: set envs // path: /apps/{app}/env // method: POST // consume: application/x-www-form-urlencoded // produce: application/x-json-stream // responses: // 200: Envs updated // 400: Invalid data // 401: Unauthorized // 404: App not found func setEnv(w http.ResponseWriter, r *http.Request, t auth.Token) error { err := r.ParseForm() if err != nil { return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()} } var e Envs dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err = dec.DecodeValues(&e, r.Form) if err != nil { return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()} } if len(e.Envs) == 0 { msg := "You must provide the list of environment variables" return &errors.HTTP{Code: http.StatusBadRequest, Message: msg} } u, err := t.User() if err != nil { return err } extra := fmt.Sprintf("private=%t", e.Private) appName := r.URL.Query().Get(":app") a, err := getAppFromContext(appName, r) if err != nil { return err } allowed := permission.Check(t, permission.PermAppUpdateEnvSet, append(permission.Contexts(permission.CtxTeam, a.Teams), permission.Context(permission.CtxApp, a.Name), permission.Context(permission.CtxPool, a.Pool), )..., ) if !allowed { return permission.ErrUnauthorized } envs := map[string]string{} variables := []bind.EnvVar{} for _, v := range e.Envs { envs[v.Name] = v.Value variables = append(variables, bind.EnvVar{Name: v.Name, Value: v.Value, Public: !e.Private}) } rec.Log(u.Email, "set-env", "app="+appName, envs, extra) w.Header().Set("Content-Type", "application/x-json-stream") keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "") defer keepAliveWriter.Stop() writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)} err = a.SetEnvs( bind.SetEnvApp{ Envs: variables, PublicOnly: true, ShouldRestart: !e.NoRestart, }, writer) if err != nil { writer.Encode(tsuruIo.SimpleJsonMessage{Error: err.Error()}) return nil } return nil }
func logsConfigSetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error { err := r.ParseForm() if err != nil { return &errors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse form values: %s", err), } } pool := r.Form.Get("pool") restart, _ := strconv.ParseBool(r.Form.Get("restart")) delete(r.Form, "pool") delete(r.Form, "restart") var conf container.DockerLogConfig dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) err = dec.DecodeValues(&conf, r.Form) if err != nil { return &errors.HTTP{ Code: http.StatusBadRequest, Message: fmt.Sprintf("unable to parse fields in docker log config: %s", err), } } if pool == "" && !permission.Check(t, permission.PermPoolUpdateLogs) { return permission.ErrUnauthorized } hasPermission := permission.Check(t, permission.PermPoolUpdateLogs, permission.Context(permission.CtxPool, pool)) if !hasPermission { return permission.ErrUnauthorized } err = conf.Save(pool) if err != nil { return err } keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 15*time.Second, "") defer keepAliveWriter.Stop() writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)} fmt.Fprintln(writer, "Log config successfully updated.") if restart { filter := &app.Filter{} if pool != "" { filter.Pools = []string{pool} } tryRestartAppsByFilter(filter, writer) } return nil }
func nodeContainerUpdate(w http.ResponseWriter, r *http.Request, t auth.Token) error { err := r.ParseForm() if err != nil { return err } poolName := r.FormValue("pool") if poolName == "" { if !permission.Check(t, permission.PermNodecontainerUpdate) { return permission.ErrUnauthorized } } else { if !permission.Check(t, permission.PermNodecontainerUpdate, permission.Context(permission.CtxPool, poolName)) { return permission.ErrUnauthorized } } dec := form.NewDecoder(nil) dec.IgnoreUnknownKeys(true) dec.IgnoreCase(true) var config nodecontainer.NodeContainerConfig err = dec.DecodeValues(&config, r.Form) if err != nil { return err } err = nodecontainer.UpdateContainer(poolName, &config) if err != nil { if _, ok := err.(nodecontainer.ValidationErr); ok { return &errors.HTTP{ Code: http.StatusBadRequest, Message: err.Error(), } } return err } return nil }