podman

Форк
0
218 строк · 5.9 Кб
1
// Copyright 2016 go-dockerclient authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
package docker
6

7
import (
8
	"context"
9
	"encoding/json"
10
	"errors"
11
	"io"
12
	"net/http"
13
	"time"
14

15
	"github.com/docker/docker/api/types/swarm"
16
)
17

18
// NoSuchService is the error returned when a given service does not exist.
19
type NoSuchService struct {
20
	ID  string
21
	Err error
22
}
23

24
func (err *NoSuchService) Error() string {
25
	if err.Err != nil {
26
		return err.Err.Error()
27
	}
28
	return "No such service: " + err.ID
29
}
30

31
// CreateServiceOptions specify parameters to the CreateService function.
32
//
33
// See https://goo.gl/KrVjHz for more details.
34
type CreateServiceOptions struct {
35
	Auth AuthConfiguration `qs:"-"`
36
	swarm.ServiceSpec
37
	Context context.Context
38
}
39

40
// CreateService creates a new service, returning the service instance
41
// or an error in case of failure.
42
//
43
// See https://goo.gl/KrVjHz for more details.
44
func (c *Client) CreateService(opts CreateServiceOptions) (*swarm.Service, error) {
45
	headers, err := headersWithAuth(opts.Auth)
46
	if err != nil {
47
		return nil, err
48
	}
49
	path := "/services/create?" + queryString(opts)
50
	resp, err := c.do(http.MethodPost, path, doOptions{
51
		headers:   headers,
52
		data:      opts.ServiceSpec,
53
		forceJSON: true,
54
		context:   opts.Context,
55
	})
56
	if err != nil {
57
		return nil, err
58
	}
59
	defer resp.Body.Close()
60
	var service swarm.Service
61
	if err := json.NewDecoder(resp.Body).Decode(&service); err != nil {
62
		return nil, err
63
	}
64
	return &service, nil
65
}
66

67
// RemoveServiceOptions encapsulates options to remove a service.
68
//
69
// See https://goo.gl/Tqrtya for more details.
70
type RemoveServiceOptions struct {
71
	ID      string `qs:"-"`
72
	Context context.Context
73
}
74

75
// RemoveService removes a service, returning an error in case of failure.
76
//
77
// See https://goo.gl/Tqrtya for more details.
78
func (c *Client) RemoveService(opts RemoveServiceOptions) error {
79
	path := "/services/" + opts.ID
80
	resp, err := c.do(http.MethodDelete, path, doOptions{context: opts.Context})
81
	if err != nil {
82
		var e *Error
83
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
84
			return &NoSuchService{ID: opts.ID}
85
		}
86
		return err
87
	}
88
	resp.Body.Close()
89
	return nil
90
}
91

92
// UpdateServiceOptions specify parameters to the UpdateService function.
93
//
94
// See https://goo.gl/wu3MmS for more details.
95
type UpdateServiceOptions struct {
96
	Auth              AuthConfiguration `qs:"-"`
97
	swarm.ServiceSpec `qs:"-"`
98
	Context           context.Context
99
	Version           uint64
100
	Rollback          string
101
}
102

103
// UpdateService updates the service at ID with the options
104
//
105
// See https://goo.gl/wu3MmS for more details.
106
func (c *Client) UpdateService(id string, opts UpdateServiceOptions) error {
107
	headers, err := headersWithAuth(opts.Auth)
108
	if err != nil {
109
		return err
110
	}
111
	resp, err := c.do(http.MethodPost, "/services/"+id+"/update?"+queryString(opts), doOptions{
112
		headers:   headers,
113
		data:      opts.ServiceSpec,
114
		forceJSON: true,
115
		context:   opts.Context,
116
	})
117
	if err != nil {
118
		var e *Error
119
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
120
			return &NoSuchService{ID: id}
121
		}
122
		return err
123
	}
124
	defer resp.Body.Close()
125
	return nil
126
}
127

128
// InspectService returns information about a service by its ID.
129
//
130
// See https://goo.gl/dHmr75 for more details.
131
func (c *Client) InspectService(id string) (*swarm.Service, error) {
132
	path := "/services/" + id
133
	resp, err := c.do(http.MethodGet, path, doOptions{})
134
	if err != nil {
135
		var e *Error
136
		if errors.As(err, &e) && e.Status == http.StatusNotFound {
137
			return nil, &NoSuchService{ID: id}
138
		}
139
		return nil, err
140
	}
141
	defer resp.Body.Close()
142
	var service swarm.Service
143
	if err := json.NewDecoder(resp.Body).Decode(&service); err != nil {
144
		return nil, err
145
	}
146
	return &service, nil
147
}
148

149
// ListServicesOptions specify parameters to the ListServices function.
150
//
151
// See https://goo.gl/DwvNMd for more details.
152
type ListServicesOptions struct {
153
	Filters map[string][]string
154
	Status  bool
155
	Context context.Context
156
}
157

158
// ListServices returns a slice of services matching the given criteria.
159
//
160
// See https://goo.gl/DwvNMd for more details.
161
func (c *Client) ListServices(opts ListServicesOptions) ([]swarm.Service, error) {
162
	path := "/services?" + queryString(opts)
163
	resp, err := c.do(http.MethodGet, path, doOptions{context: opts.Context})
164
	if err != nil {
165
		return nil, err
166
	}
167
	defer resp.Body.Close()
168
	var services []swarm.Service
169
	if err := json.NewDecoder(resp.Body).Decode(&services); err != nil {
170
		return nil, err
171
	}
172
	return services, nil
173
}
174

175
// LogsServiceOptions represents the set of options used when getting logs from a
176
// service.
177
type LogsServiceOptions struct {
178
	Context           context.Context
179
	Service           string        `qs:"-"`
180
	OutputStream      io.Writer     `qs:"-"`
181
	ErrorStream       io.Writer     `qs:"-"`
182
	InactivityTimeout time.Duration `qs:"-"`
183
	Tail              string
184
	Since             int64
185

186
	// Use raw terminal? Usually true when the container contains a TTY.
187
	RawTerminal bool `qs:"-"`
188
	Follow      bool
189
	Stdout      bool
190
	Stderr      bool
191
	Timestamps  bool
192
	Details     bool
193
}
194

195
// GetServiceLogs gets stdout and stderr logs from the specified service.
196
//
197
// When LogsServiceOptions.RawTerminal is set to false, go-dockerclient will multiplex
198
// the streams and send the containers stdout to LogsServiceOptions.OutputStream, and
199
// stderr to LogsServiceOptions.ErrorStream.
200
//
201
// When LogsServiceOptions.RawTerminal is true, callers will get the raw stream on
202
// LogsServiceOptions.OutputStream.
203
func (c *Client) GetServiceLogs(opts LogsServiceOptions) error {
204
	if opts.Service == "" {
205
		return &NoSuchService{ID: opts.Service}
206
	}
207
	if opts.Tail == "" {
208
		opts.Tail = "all"
209
	}
210
	path := "/services/" + opts.Service + "/logs?" + queryString(opts)
211
	return c.stream(http.MethodGet, path, streamOptions{
212
		setRawTerminal:    opts.RawTerminal,
213
		stdout:            opts.OutputStream,
214
		stderr:            opts.ErrorStream,
215
		inactivityTimeout: opts.InactivityTimeout,
216
		context:           opts.Context,
217
	})
218
}
219

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.