podman
64 строки · 2.2 Кб
1package docker
2
3import (
4"errors"
5"fmt"
6"net/http"
7)
8
9// RestartPolicy represents the policy for automatically restarting a container.
10//
11// Possible values are:
12//
13// - always: the docker daemon will always restart the container
14// - on-failure: the docker daemon will restart the container on failures, at
15// most MaximumRetryCount times
16// - unless-stopped: the docker daemon will always restart the container except
17// when user has manually stopped the container
18// - no: the docker daemon will not restart the container automatically
19type RestartPolicy struct {
20Name string `json:"Name,omitempty" yaml:"Name,omitempty" toml:"Name,omitempty"`
21MaximumRetryCount int `json:"MaximumRetryCount,omitempty" yaml:"MaximumRetryCount,omitempty" toml:"MaximumRetryCount,omitempty"`
22}
23
24// AlwaysRestart returns a restart policy that tells the Docker daemon to
25// always restart the container.
26func AlwaysRestart() RestartPolicy {
27return RestartPolicy{Name: "always"}
28}
29
30// RestartOnFailure returns a restart policy that tells the Docker daemon to
31// restart the container on failures, trying at most maxRetry times.
32func RestartOnFailure(maxRetry int) RestartPolicy {
33return RestartPolicy{Name: "on-failure", MaximumRetryCount: maxRetry}
34}
35
36// RestartUnlessStopped returns a restart policy that tells the Docker daemon to
37// always restart the container except when user has manually stopped the container.
38func RestartUnlessStopped() RestartPolicy {
39return RestartPolicy{Name: "unless-stopped"}
40}
41
42// NeverRestart returns a restart policy that tells the Docker daemon to never
43// restart the container on failures.
44func NeverRestart() RestartPolicy {
45return RestartPolicy{Name: "no"}
46}
47
48// RestartContainer stops a container, killing it after the given timeout (in
49// seconds), during the stop process.
50//
51// See https://goo.gl/MrAKQ5 for more details.
52func (c *Client) RestartContainer(id string, timeout uint) error {
53path := fmt.Sprintf("/containers/%s/restart?t=%d", id, timeout)
54resp, err := c.do(http.MethodPost, path, doOptions{})
55if err != nil {
56var e *Error
57if errors.As(err, &e) && e.Status == http.StatusNotFound {
58return &NoSuchContainer{ID: id}
59}
60return err
61}
62resp.Body.Close()
63return nil
64}
65