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