Skip to content

Commit

Permalink
working to provide plan by process
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Dec 14, 2023
1 parent 65cd8f7 commit fae23ca
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tsuru/client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,21 @@ func (a *app) String(simplified bool) string {
buf.WriteString("\n")
buf.WriteString("App Plan:\n")
buf.WriteString(renderPlans([]apptypes.Plan{a.Plan}, renderPlansOpts{}))

overrides := map[string]string{}
for _, p := range a.Processes {
if p.Plan != "" {
overrides[p.Name] = p.Plan
}
}

if len(overrides) > 0 {
buf.WriteString("\n")
buf.WriteString("Override plan per process:\n")
buf.WriteString(renderPlanOverride(overrides))
}
}

if !simplified && internalAddressesTable.Rows() > 0 {
buf.WriteString("\n")
buf.WriteString("Cluster internal addresses:\n")
Expand Down Expand Up @@ -1642,3 +1656,71 @@ func addCName(cnames []string, g cmd.AppNameMixIn, client *cmd.Client) error {
_, err = client.Do(request)
return err
}

type AppProcessUpdate struct {
plan string
resetDefaultPlan bool
noRestart bool
fs *gnuflag.FlagSet
}

func (c *AppProcessUpdate) Info() *cmd.Info {
return &cmd.Info{
Name: "app-process-update",
Usage: "app process update [app] [process] [--plan/-p plan name] [--default-plan]",
Desc: `Updates a plan of app process`,
MinArgs: 2,
}
}

func (c *AppProcessUpdate) Flags() *gnuflag.FlagSet {
if c.fs == nil {
flagSet := gnuflag.NewFlagSet("", gnuflag.ExitOnError)
planMessage := "Changes plan for the app"
planReset := "Reset process to default of app"
noRestartMessage := "Prevent tsuru from restarting the application"
flagSet.StringVar(&c.plan, "plan", "", planMessage)
flagSet.StringVar(&c.plan, "p", "", planMessage)
flagSet.BoolVar(&c.resetDefaultPlan, "default-plan", false, planReset)
flagSet.BoolVar(&c.noRestart, "no-restart", false, noRestartMessage)
c.fs = flagSet
}
return c.fs
}

func (c *AppProcessUpdate) Run(ctx *cmd.Context, cli *cmd.Client) error {
ctx.RawOutput()

if c.resetDefaultPlan {
c.plan = "$default"
}

a := tsuru.UpdateApp{
NoRestart: c.noRestart,
Processes: []tsuru.AppProcess{
{
Name: ctx.Args[1],
Plan: c.plan,
},
},
}

apiClient, err := client.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: cli.HTTPClient,
})
if err != nil {
return err
}

resp, err := apiClient.AppApi.AppUpdate(context.Background(), ctx.Args[0], a)
if err != nil {
return err
}
err = cmd.StreamJSONResponse(ctx.Stdout, resp)
if err != nil {
return err
}
fmt.Fprintf(ctx.Stdout, "Process %q of app %q has been updated!\n", ctx.Args[1], ctx.Args[0])

return nil
}
22 changes: 22 additions & 0 deletions tsuru/client/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"

"github.com/tsuru/gnuflag"
Expand Down Expand Up @@ -125,6 +126,27 @@ func renderPlans(plans []apptypes.Plan, opts renderPlansOpts) string {
return table.String()
}

func renderPlanOverride(overrides map[string]string) string {
table := tablecli.NewTable()
table.Headers = []string{"Process", "Plan"}

processes := []string{}
for process := range overrides {
processes = append(processes, process)
}

sort.Strings(processes)

for _, process := range processes {
row := []string{
process,
overrides[process],
}
table.AddRow(row)
}
return table.String()
}

func renderPlansK8SFriendly(plans []apptypes.Plan, showMaxBurstAllowed bool) string {
table := tablecli.NewTable()
table.Headers = []string{"Name"}
Expand Down
1 change: 1 addition & 0 deletions tsuru/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func buildManager(name string) *cmd.Manager {
m.Register(&client.AppCreate{})
m.Register(&client.AppRemove{})
m.Register(&client.AppUpdate{})
m.Register(&client.AppProcessUpdate{})
m.Register(&client.UnitAdd{})
m.Register(&client.UnitRemove{})
m.Register(&client.UnitKill{})
Expand Down

0 comments on commit fae23ca

Please sign in to comment.