github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/healthchecks.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "sort" 6 7 "github.com/drycc/controller-sdk-go/api" 8 "github.com/drycc/controller-sdk-go/config" 9 ) 10 11 func getHealthcheckString(procType, probeType string, healthcheck *api.Healthcheck) string { 12 params := fmt.Sprintf( 13 "delay=%ds timeout=%ds period=%ds #success=%d #failure=%d", 14 healthcheck.InitialDelaySeconds, 15 healthcheck.TimeoutSeconds, 16 healthcheck.PeriodSeconds, 17 healthcheck.SuccessThreshold, 18 healthcheck.FailureThreshold, 19 ) 20 21 if healthcheck.Exec != nil { 22 return fmt.Sprintf("%s %s exec %v %s", probeType, procType, healthcheck.Exec.Command, params) 23 } else if healthcheck.TCPSocket != nil { 24 return fmt.Sprintf("%s %s tcp-socket port=%v %s", probeType, procType, healthcheck.TCPSocket.Port, params) 25 } else if healthcheck.HTTPGet != nil { 26 return fmt.Sprintf( 27 "%s %s http-get headers=%v path=%s port=%d %s", 28 probeType, 29 procType, 30 healthcheck.HTTPGet.HTTPHeaders, 31 healthcheck.HTTPGet.Path, 32 healthcheck.HTTPGet.Port, 33 params, 34 ) 35 } 36 return "" 37 } 38 39 func getHealthchecksStrings(procType string, healthchecks *api.Healthchecks) []string { 40 var probes []string 41 for key := range *healthchecks { 42 probes = append(probes, getHealthcheckString(procType, key, (*healthchecks)[key])) 43 } 44 return probes 45 } 46 47 // HealthchecksList lists an app's healthchecks. 48 func (d *DryccCmd) HealthchecksList(appID, procType string) error { 49 s, appID, err := load(d.ConfigFile, appID) 50 if err != nil { 51 return err 52 } 53 54 config, err := config.List(s.Client, appID) 55 56 if err != nil { 57 return err 58 } 59 60 if procType == "" { 61 if len(config.Healthcheck) == 0 { 62 d.Println("No health checks configured.") 63 } else { 64 var keys []string 65 for k := range config.Healthcheck { 66 keys = append(keys, k) 67 } 68 sort.Strings(keys) 69 table := d.getDefaultFormatTable([]string{}) 70 table.Append([]string{"App:", config.App}) 71 table.Append([]string{"UUID:", config.UUID}) 72 table.Append([]string{"Owner:", config.Owner}) 73 table.Append([]string{"Created:", config.Created}) 74 table.Append([]string{"Updated:", config.Updated}) 75 table.Append([]string{"Healthchecks:"}) 76 for _, key := range keys { 77 for _, probe := range getHealthchecksStrings(key, config.Healthcheck[key]) { 78 if probe != "" { 79 table.Append([]string{"", probe}) 80 } 81 } 82 } 83 table.Render() 84 } 85 } else { 86 if healthcheck, found := config.Healthcheck[procType]; found { 87 table := d.getDefaultFormatTable([]string{}) 88 table.Append([]string{"App:", config.App}) 89 table.Append([]string{"UUID:", config.UUID}) 90 table.Append([]string{"Owner:", config.Owner}) 91 table.Append([]string{"Created:", config.Created}) 92 table.Append([]string{"Updated:", config.Updated}) 93 table.Append([]string{"Healthchecks:"}) 94 for _, probe := range getHealthchecksStrings(procType, healthcheck) { 95 if probe != "" { 96 table.Append([]string{"", probe}) 97 } 98 } 99 table.Render() 100 } else { 101 d.Println("No health checks configured.") 102 } 103 } 104 return nil 105 } 106 107 // HealthchecksSet sets an app's healthchecks. 108 func (d *DryccCmd) HealthchecksSet(appID, healthcheckType, procType string, probe *api.Healthcheck) error { 109 s, appID, err := load(d.ConfigFile, appID) 110 111 if err != nil { 112 return err 113 } 114 115 d.Printf("Applying %s healthcheck... ", healthcheckType) 116 117 quit := progress(d.WOut) 118 119 healthcheckMap := make(api.Healthchecks) 120 healthcheckMap[healthcheckType] = probe 121 configObj := api.Config{Healthcheck: make(map[string]*api.Healthchecks)} 122 configObj.Healthcheck[procType] = &healthcheckMap 123 124 _, err = config.Set(s.Client, appID, configObj) 125 126 quit <- true 127 <-quit 128 129 if err != nil { 130 return err 131 } 132 133 d.Print("done\n\n") 134 135 return d.HealthchecksList(appID, procType) 136 } 137 138 // HealthchecksUnset removes an app's healthchecks. 139 func (d *DryccCmd) HealthchecksUnset(appID, procType string, healthchecks []string) error { 140 s, appID, err := load(d.ConfigFile, appID) 141 if err != nil { 142 return err 143 } 144 145 d.Print("Removing healthchecks... ") 146 147 quit := progress(d.WOut) 148 149 configObj := api.Config{} 150 151 healthchecksMap := make(map[string]*api.Healthchecks) 152 healthcheckMap := make(api.Healthchecks) 153 154 for _, healthcheck := range healthchecks { 155 healthcheckMap[healthcheck] = nil 156 } 157 healthchecksMap[procType] = &healthcheckMap 158 159 configObj.Healthcheck = healthchecksMap 160 161 _, err = config.Set(s.Client, appID, configObj) 162 163 quit <- true 164 <-quit 165 166 if err != nil { 167 return err 168 } 169 170 d.Print("done\n\n") 171 172 return d.HealthchecksList(appID, procType) 173 }