File: services.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (143 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download | duplicates (3)
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
package cs

import (
	"errors"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

type Container struct {
	Name      string            `json:"name"`
	Node      string            `json:"node"`
	VMID      string            `json:"vm_id"`
	IP        string            `json:"ip"`
	Running   bool              `json:"running"`
	Status    string            `json:"status"`
	Health    string            `json:"health"`
	StatusExt string            `json:"status_ext"`
	FailCount int               `json:"fail_count"`
	Ports     map[string][]Port `json:"ports"`
}

type Service struct {
	ID             string                 `json:"id"`
	Name           string                 `json:"name"`
	Project        string                 `json:"project"`
	ComposeVersion string                 `json:"compose_version"`
	Containers     map[string]Container   `json:"containers"`
	Created        time.Time              `json:"created"`
	CurrentState   string                 `json:"current_state"`
	Definition     Definition             `json:"definition"`
	DesiredState   string                 `json:"desired_state"`
	Extensions     map[string]interface{} `json:"extensions"`
	Hash           string                 `json:"hash"`
	Updated        time.Time              `json:"updated"`
	Version        string                 `json:"version"`
}

type ScaleType string

const (
	ScaleTo ScaleType = "scale_to"
)

type ScaleServiceArgs struct {
	ServiceId string    `json:"-"`
	Type      ScaleType `json:"type"`
	Value     int       `json:"value"`
}

type GetServiceResponse Service
type GetServicesResponse []Service

func (client *ProjectClient) GetServices(q string, containers bool) (services GetServicesResponse, err error) {
	query := make(url.Values)

	if len(q) != 0 {
		query.Add("q", q)
	}

	query.Add("containers", strconv.FormatBool(containers))

	err = client.Invoke(http.MethodGet, "/services/", query, nil, &services)

	return
}

func (client *ProjectClient) GetService(serviceId string) (service GetServiceResponse, err error) {

	if len(serviceId) == 0 {
		err = errors.New("service id is empty")
		return
	}

	err = client.Invoke(http.MethodGet, "/services/"+serviceId, nil, nil, &service)

	return
}

func (client *ProjectClient) StartService(serviceId string) (err error) {

	if len(serviceId) == 0 {
		err = errors.New("service id is empty")
		return
	}

	err = client.Invoke(http.MethodPost, "/services/"+serviceId+"/start", nil, nil, nil)

	return
}

func (client *ProjectClient) StopService(serviceId string, timeout ...time.Duration) (err error) {

	if len(serviceId) == 0 {
		err = errors.New("service id 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, "/services/"+serviceId+"/stop", query, nil, nil)

	return
}

func (client *ProjectClient) KillService(serviceId string, signal ...string) (err error) {

	if len(serviceId) == 0 {
		err = errors.New("service id 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, "/services/"+serviceId+"/kill", query, nil, nil)

	return
}

func (client *ProjectClient) ScaleService(args *ScaleServiceArgs) (err error) {

	if len(args.ServiceId) == 0 {
		err = errors.New("service id is empty")
		return
	}

	err = client.Invoke(http.MethodPost, "/services/"+args.ServiceId+"/scale", nil, args, nil)

	return
}