github.com/naphatkrit/deis@v1.12.3/client/cmd/limits.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 8 "github.com/deis/deis/pkg/prettyprint" 9 10 "github.com/deis/deis/client/controller/api" 11 "github.com/deis/deis/client/controller/models/config" 12 ) 13 14 // LimitsList lists an app's limits. 15 func LimitsList(appID string) error { 16 c, appID, err := load(appID) 17 18 if err != nil { 19 return err 20 } 21 22 config, err := config.List(c, appID) 23 24 fmt.Printf("=== %s Limits\n\n", appID) 25 26 fmt.Println("--- Memory") 27 if len(config.Memory) == 0 { 28 fmt.Println("Unlimited") 29 } else { 30 memoryMap := make(map[string]string) 31 32 for key, value := range config.Memory { 33 memoryMap[key] = fmt.Sprintf("%v", value) 34 } 35 36 fmt.Print(prettyprint.PrettyTabs(memoryMap, 5)) 37 } 38 39 fmt.Println("\n--- CPU") 40 if len(config.CPU) == 0 { 41 fmt.Println("Unlimited") 42 } else { 43 cpuMap := make(map[string]string) 44 45 for key, value := range config.CPU { 46 cpuMap[key] = strconv.Itoa(int(value.(float64))) 47 } 48 49 fmt.Print(prettyprint.PrettyTabs(cpuMap, 5)) 50 } 51 52 return nil 53 } 54 55 // LimitsSet sets an app's limits. 56 func LimitsSet(appID string, limits []string, limitType string) error { 57 c, appID, err := load(appID) 58 59 if err != nil { 60 return err 61 } 62 63 limitsMap := parseLimits(limits) 64 65 fmt.Print("Applying limits... ") 66 67 quit := progress() 68 configObj := api.Config{} 69 70 if limitType == "cpu" { 71 configObj.CPU = limitsMap 72 } else { 73 configObj.Memory = limitsMap 74 } 75 76 _, err = config.Set(c, appID, configObj) 77 78 quit <- true 79 <-quit 80 81 if err != nil { 82 return err 83 } 84 85 fmt.Print("done\n\n") 86 87 return LimitsList(appID) 88 } 89 90 // LimitsUnset removes an app's limits. 91 func LimitsUnset(appID string, limits []string, limitType string) error { 92 c, appID, err := load(appID) 93 94 if err != nil { 95 return err 96 } 97 98 fmt.Print("Applying limits... ") 99 100 quit := progress() 101 102 configObj := api.Config{} 103 104 valuesMap := make(map[string]interface{}) 105 106 for _, limit := range limits { 107 valuesMap[limit] = nil 108 } 109 110 if limitType == "cpu" { 111 configObj.CPU = valuesMap 112 } else { 113 configObj.Memory = valuesMap 114 } 115 116 _, err = config.Set(c, appID, configObj) 117 118 quit <- true 119 <-quit 120 121 if err != nil { 122 return err 123 } 124 125 fmt.Print("done\n\n") 126 127 return LimitsList(appID) 128 } 129 130 func parseLimits(limits []string) map[string]interface{} { 131 limitsMap := make(map[string]interface{}) 132 133 for _, limit := range limits { 134 key, value, err := parseLimit(limit) 135 136 if err != nil { 137 fmt.Println(err) 138 continue 139 } 140 141 limitsMap[key] = value 142 } 143 144 return limitsMap 145 } 146 147 func parseLimit(limit string) (string, string, error) { 148 regex := regexp.MustCompile("^([A-z]+)=([0-9]+[bkmgBKMG]{1,2}|[0-9]{1,4})$") 149 150 if !regex.MatchString(limit) { 151 return "", "", fmt.Errorf(`%s doesn't fit format type=#unit or type=# 152 Examples: web=2G worker=500M web=300`, limit) 153 } 154 155 capture := regex.FindStringSubmatch(limit) 156 157 return capture[1], capture[2], nil 158 }