| 1 | package cs |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | type Project struct { |
| 13 | Name string `json:"name"` |
| 14 | Description string `json:"description"` |
| 15 | Template string `json:"template"` |
| 16 | Version string `json:"version"` |
| 17 | Created string `json:"created"` |
| 18 | Updated string `json:"updated"` |
| 19 | DesiredState string `json:"desired_state"` |
| 20 | CurrentState string `json:"current_state"` |
| 21 | Environment map[string]string `json:"environment"` |
| 22 | Services []Service `json:"services"` |
| 23 | } |
| 24 | |
| 25 | type GetProjectsResponse []Project |
| 26 | |
| 27 | type GetProjectResponse Project |
| 28 | |
| 29 | type Port struct { |
| 30 | HostIP string `json:"host_ip"` |
| 31 | HostPort string `json:"host_port"` |
| 32 | } |
| 33 | |
| 34 | type Labels map[string]string |
| 35 | |
| 36 | type Definition struct { |
| 37 | Environment []string `json:"environment"` |
| 38 | Image string `json:"image"` |
| 39 | KernelMemory int `json:"kernel_memory"` |
| 40 | Labels Labels `json:"labels"` |
| 41 | MemLimit int `json:"mem_limit"` |
| 42 | MemswapLimit int `json:"memswap_limit"` |
| 43 | MemswapReservation int `json:"memswap_reservation"` |
| 44 | OomKillDisable bool `json:"oom_kill_disable"` |
| 45 | Restart string `json:"restart"` |
| 46 | ShmSize int `json:"shm_size"` |
| 47 | Volumes []string `json:"volumes"` |
| 48 | } |
| 49 | |
| 50 | type ProjectCreationArgs struct { |
| 51 | Name string `json:"name"` |
| 52 | Description string `json:"description"` |
| 53 | Template string `json:"template"` |
| 54 | Version string `json:"version"` |
| 55 | Environment map[string]string `json:"environment"` |
| 56 | LatestImage bool `json:"latest_image"` |
| 57 | } |
| 58 | |
| 59 | type ProjectUpdationArgs struct { |
| 60 | Name string `json:"-"` |
| 61 | Description string `json:"description"` |
| 62 | Template string `json:"template"` |
| 63 | Version string `json:"version"` |
| 64 | Environment map[string]string `json:"environment"` |
| 65 | LatestImage bool `json:"latest_image"` |
| 66 | UpdateMethod string `json:"update_method"` |
| 67 | } |
| 68 | |
| 69 | func (client *ProjectClient) GetProjects(q string, services, containers bool) (projects GetProjectsResponse, err error) { |
| 70 | query := make(url.Values) |
| 71 | |
| 72 | if len(q) != 0 { |
| 73 | query.Add("q", q) |
| 74 | } |
| 75 | |
| 76 | query.Add("services", strconv.FormatBool(services)) |
| 77 | query.Add("containers", strconv.FormatBool(containers)) |
| 78 | |
| 79 | err = client.Invoke(http.MethodGet, "/projects/", query, nil, &projects) |
| 80 | |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | func (client *ProjectClient) GetProject(name string) (project GetProjectResponse, err error) { |
| 85 | |
| 86 | if len(name) == 0 { |
| 87 | err = errors.New("project name is empty") |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | err = client.Invoke(http.MethodGet, "/projects/"+name, nil, nil, &project) |
| 92 | |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | func (client *ProjectClient) StartProject(name string) (err error) { |
| 97 | |
| 98 | if len(name) == 0 { |
| 99 | err = errors.New("project name is empty") |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | err = client.Invoke(http.MethodPost, "/projects/"+name+"/start", nil, nil, nil) |
| 104 | |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | func (client *ProjectClient) StopProject(name string, timeout ...time.Duration) (err error) { |
| 109 | |
| 110 | if len(name) == 0 { |
| 111 | err = errors.New("project name is empty") |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | query := make(url.Values) |
| 116 | |
| 117 | if len(timeout) > 0 { |
| 118 | if timeout[0] > 0 { |
| 119 | query.Add("t", strconv.Itoa(int(timeout[0].Seconds()))) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | err = client.Invoke(http.MethodPost, "/projects/"+name+"/stop", query, nil, nil) |
| 124 | |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | func (client *ProjectClient) KillProject(name string, signal ...string) (err error) { |
| 129 | |
| 130 | if len(name) == 0 { |
| 131 | err = errors.New("project name is empty") |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | query := make(url.Values) |
| 136 | |
| 137 | if len(signal) > 0 { |
| 138 | if len(signal[0]) > 0 { |
| 139 | query.Add("signal", signal[0]) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | err = client.Invoke(http.MethodPost, "/projects/"+name+"/kill", query, nil, nil) |
| 144 | |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | func (client *ProjectClient) CreateProject(args *ProjectCreationArgs) (err error) { |
| 149 | |
| 150 | args.Template = strings.TrimSpace(args.Template) |
| 151 | |
| 152 | err = client.Invoke(http.MethodPost, "/projects/", nil, args, nil) |
| 153 | |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | func (client *ProjectClient) UpdateProject(args *ProjectUpdationArgs) (err error) { |
| 158 | |
| 159 | if len(args.Name) == 0 { |
| 160 | err = errors.New("project name is empty") |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | args.Template = strings.TrimSpace(args.Template) |
| 165 | |
| 166 | err = client.Invoke(http.MethodPost, "/projects/"+args.Name+"/update", nil, args, nil) |
| 167 | |
| 168 | return |
| 169 | } |
| 170 | |
| 171 | func (client *ProjectClient) DeleteProject(name string, forceDelete, deleteVolume bool) (err error) { |
| 172 | |
| 173 | if len(name) == 0 { |
| 174 | err = errors.New("project name is empty") |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | query := make(url.Values) |
| 179 | |
| 180 | query.Add("name", name) |
| 181 | query.Add("force", strconv.FormatBool(forceDelete)) |
| 182 | query.Add("volume", strconv.FormatBool(deleteVolume)) |
| 183 | |
| 184 | err = client.Invoke(http.MethodDelete, "/projects/"+name, query, nil, nil) |
| 185 | |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | func (client *ProjectClient) ConfirmBlueGreenProject(name string, force bool) (err error) { |
| 190 | |
| 191 | if len(name) == 0 { |
| 192 | err = errors.New("project name is empty") |
| 193 | return |
| 194 | } |
| 195 | |
| 196 | err = client.Invoke(http.MethodPost, "/projects/"+name+"/confirm-update?force="+strconv.FormatBool(force), nil, nil, nil) |
| 197 | |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | func (client *ProjectClient) RollBackBlueGreenProject(name string, force bool) (err error) { |
| 202 | |
| 203 | if len(name) == 0 { |
| 204 | err = errors.New("project name is empty") |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | err = client.Invoke(http.MethodPost, "/projects/"+name+"/rollback-update?force="+strconv.FormatBool(force), nil, nil, nil) |
| 209 | |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | type SwarmNodeType struct { |
| 214 | IP string |
| 215 | Healthy bool |
| 216 | InstanceId string |
| 217 | ZoneId string |
| 218 | RegionId string |
| 219 | Status string |
| 220 | CreationTime string `json:"created"` |
| 221 | UpdatedTime string `json:"updated"` |
| 222 | Containers int |
| 223 | ContainersRunning int |
| 224 | ContainersPaused int |
| 225 | ContainersStopped int |
| 226 | AgentVersion string |
| 227 | Name string |
| 228 | } |
| 229 | type GetSwarmClusterNodesResponse []SwarmNodeType |
| 230 | |
| 231 | func (client *ProjectClient) GetSwarmClusterNodes() (project GetSwarmClusterNodesResponse, err error) { |
| 232 | err = client.Invoke(http.MethodGet, "/hosts/", nil, nil, &project) |
| 233 | return |
| 234 | } |