|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + |
| 7 | + "github.com/deis/pkg/prettyprint" |
| 8 | + |
| 9 | + "github.com/notmaxx/controller-sdk-go/api" |
| 10 | + "github.com/notmaxx/controller-sdk-go/config" |
| 11 | +) |
| 12 | + |
| 13 | +// LimitsList lists an app's timeouts. |
| 14 | +func (d *DeisCmd) TimeoutsList(appID string) error { |
| 15 | + s, appID, err := load(d.ConfigFile, appID) |
| 16 | + |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + |
| 21 | + config, err := config.List(s.Client, appID) |
| 22 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + d.Printf("=== %s Timeouts (sec)\n\n", appID) |
| 27 | + |
| 28 | + if len(config.Timeout) == 0 { |
| 29 | + d.Println("default (30 sec) or controlled by env KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS") |
| 30 | + } else { |
| 31 | + timeoutsMap := make(map[string]string) |
| 32 | + |
| 33 | + for key, value := range config.Timeout { |
| 34 | + timeoutsMap[key] = fmt.Sprintf("%v", value) |
| 35 | + } |
| 36 | + |
| 37 | + d.Print(prettyprint.PrettyTabs(timeoutsMap, 5)) |
| 38 | + } |
| 39 | + |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// LimitsSet sets an app's timeouts. |
| 44 | +func (d *DeisCmd) TimeoutsSet(appID string, timeouts []string) error { |
| 45 | + s, appID, err := load(d.ConfigFile, appID) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + timeoutsMap, err := parseTimeouts(timeouts) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + d.Print("Applying timeouts... ") |
| 57 | + |
| 58 | + quit := progress(d.WOut) |
| 59 | + configObj := api.Config{} |
| 60 | + |
| 61 | + configObj.Timeout = timeoutsMap |
| 62 | + |
| 63 | + _, err = config.Set(s.Client, appID, configObj) |
| 64 | + quit <- true |
| 65 | + <-quit |
| 66 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + d.Print("done\n\n") |
| 71 | + |
| 72 | + return d.TimeoutsList(appID) |
| 73 | +} |
| 74 | + |
| 75 | +// LimitsUnset removes an app's timeouts. |
| 76 | +func (d *DeisCmd) TimeoutsUnset(appID string, timeouts []string) error { |
| 77 | + s, appID, err := load(d.ConfigFile, appID) |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + d.Print("Applying timeouts... ") |
| 84 | + |
| 85 | + quit := progress(d.WOut) |
| 86 | + |
| 87 | + configObj := api.Config{} |
| 88 | + |
| 89 | + valuesMap := make(map[string]interface{}) |
| 90 | + |
| 91 | + for _, timeout := range timeouts { |
| 92 | + valuesMap[timeout] = nil |
| 93 | + } |
| 94 | + |
| 95 | + configObj.Timeout = valuesMap |
| 96 | + |
| 97 | + _, err = config.Set(s.Client, appID, configObj) |
| 98 | + quit <- true |
| 99 | + <-quit |
| 100 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + d.Print("done\n\n") |
| 105 | + |
| 106 | + return d.TimeoutsList(appID) |
| 107 | +} |
| 108 | + |
| 109 | +func parseTimeouts(timeouts []string) (map[string]interface{}, error) { |
| 110 | + timeoutsMap := make(map[string]interface{}) |
| 111 | + |
| 112 | + for _, timeout := range timeouts { |
| 113 | + key, value, err := parseTimeout(timeout) |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + timeoutsMap[key] = value |
| 120 | + } |
| 121 | + |
| 122 | + return timeoutsMap, nil |
| 123 | +} |
| 124 | + |
| 125 | +func parseTimeout(timeout string) (string, string, error) { |
| 126 | + regex := regexp.MustCompile("^[0-9]*$") |
| 127 | + |
| 128 | + if !regex.MatchString(timeout) { |
| 129 | + return "", "", fmt.Errorf(`%s doesn't fit format type=# |
| 130 | +Examples: web=30 worker=300`, timeout) |
| 131 | + } |
| 132 | + |
| 133 | + capture := regex.FindStringSubmatch(timeout) |
| 134 | + |
| 135 | + return capture[1], capture[2], nil |
| 136 | +} |
0 commit comments