| 1 | package cs |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | type Container struct { |
| 12 | Name string `json:"name"` |
| 13 | Node string `json:"node"` |
| 14 | VMID string `json:"vm_id"` |
| 15 | IP string `json:"ip"` |
| 16 | Running bool `json:"running"` |
| 17 | Status string `json:"status"` |
| 18 | Health string `json:"health"` |
| 19 | StatusExt string `json:"status_ext"` |
| 20 | FailCount int `json:"fail_count"` |
| 21 | Ports map[string][]Port `json:"ports"` |
| 22 | } |
| 23 | |
| 24 | type Service struct { |
| 25 | ID string `json:"id"` |
| 26 | Name string `json:"name"` |
| 27 | Project string `json:"project"` |
| 28 | ComposeVersion string `json:"compose_version"` |
| 29 | Containers map[string]Container `json:"containers"` |
| 30 | Created time.Time `json:"created"` |
| 31 | CurrentState string `json:"current_state"` |
| 32 | Definition Definition `json:"definition"` |
| 33 | DesiredState string `json:"desired_state"` |
| 34 | Extensions map[string]interface{} `json:"extensions"` |
| 35 | Hash string `json:"hash"` |
| 36 | Updated time.Time `json:"updated"` |
| 37 | Version string `json:"version"` |
| 38 | } |
| 39 | |
| 40 | type ScaleType string |
| 41 | |
| 42 | const ( |
| 43 | ScaleTo ScaleType = "scale_to" |
| 44 | ) |
| 45 | |
| 46 | type ScaleServiceArgs struct { |
| 47 | ServiceId string `json:"-"` |
| 48 | Type ScaleType `json:"type"` |
| 49 | Value int `json:"value"` |
| 50 | } |
| 51 | |
| 52 | type GetServiceResponse Service |
| 53 | type GetServicesResponse []Service |
| 54 | |
| 55 | func (client *ProjectClient) GetServices(q string, containers bool) (services GetServicesResponse, err error) { |
| 56 | query := make(url.Values) |
| 57 | |
| 58 | if len(q) != 0 { |
| 59 | query.Add("q", q) |
| 60 | } |
| 61 | |
| 62 | query.Add("containers", strconv.FormatBool(containers)) |
| 63 | |
| 64 | err = client.Invoke(http.MethodGet, "/services/", query, nil, &services) |
| 65 | |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | func (client *ProjectClient) GetService(serviceId string) (service GetServiceResponse, err error) { |
| 70 | |
| 71 | if len(serviceId) == 0 { |
| 72 | err = errors.New("service id is empty") |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | err = client.Invoke(http.MethodGet, "/services/"+serviceId, nil, nil, &service) |
| 77 | |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | func (client *ProjectClient) StartService(serviceId string) (err error) { |
| 82 | |
| 83 | if len(serviceId) == 0 { |
| 84 | err = errors.New("service id is empty") |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | err = client.Invoke(http.MethodPost, "/services/"+serviceId+"/start", nil, nil, nil) |
| 89 | |
| 90 | return |
| 91 | } |
| 92 | |
| 93 | func (client *ProjectClient) StopService(serviceId string, timeout ...time.Duration) (err error) { |
| 94 | |
| 95 | if len(serviceId) == 0 { |
| 96 | err = errors.New("service id is empty") |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | query := make(url.Values) |
| 101 | |
| 102 | if len(timeout) > 0 { |
| 103 | if timeout[0] > 0 { |
| 104 | query.Add("t", strconv.Itoa(int(timeout[0].Seconds()))) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | err = client.Invoke(http.MethodPost, "/services/"+serviceId+"/stop", query, nil, nil) |
| 109 | |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | func (client *ProjectClient) KillService(serviceId string, signal ...string) (err error) { |
| 114 | |
| 115 | if len(serviceId) == 0 { |
| 116 | err = errors.New("service id is empty") |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | query := make(url.Values) |
| 121 | |
| 122 | if len(signal) > 0 { |
| 123 | if len(signal[0]) > 0 { |
| 124 | query.Add("signal", signal[0]) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | err = client.Invoke(http.MethodPost, "/services/"+serviceId+"/kill", query, nil, nil) |
| 129 | |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | func (client *ProjectClient) ScaleService(args *ScaleServiceArgs) (err error) { |
| 134 | |
| 135 | if len(args.ServiceId) == 0 { |
| 136 | err = errors.New("service id is empty") |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | err = client.Invoke(http.MethodPost, "/services/"+args.ServiceId+"/scale", nil, args, nil) |
| 141 | |
| 142 | return |
| 143 | } |