podman

Форк
0
156 строк · 3.8 Кб
1
package containers
2

3
import (
4
	"bytes"
5
	"context"
6
	"errors"
7
	"fmt"
8
	"net/http"
9
	"strings"
10

11
	"github.com/containers/podman/v5/libpod/define"
12
	"github.com/containers/podman/v5/pkg/api/handlers"
13
	"github.com/containers/podman/v5/pkg/bindings"
14
	dockerAPI "github.com/docker/docker/api/types"
15
	jsoniter "github.com/json-iterator/go"
16
	"github.com/sirupsen/logrus"
17
)
18

19
var json = jsoniter.ConfigCompatibleWithStandardLibrary
20

21
// ExecCreate creates a new exec session in an existing container.
22
// The exec session will not be started; that is done with ExecStart.
23
// Returns ID of new exec session, or an error if one occurred.
24
func ExecCreate(ctx context.Context, nameOrID string, config *handlers.ExecCreateConfig) (string, error) {
25
	conn, err := bindings.GetClient(ctx)
26
	if err != nil {
27
		return "", err
28
	}
29

30
	if config == nil {
31
		return "", errors.New("must provide a configuration for exec session")
32
	}
33

34
	requestJSON, err := json.Marshal(config)
35
	if err != nil {
36
		return "", fmt.Errorf("marshalling exec config to JSON: %w", err)
37
	}
38
	jsonReader := strings.NewReader(string(requestJSON))
39

40
	resp, err := conn.DoRequest(ctx, jsonReader, http.MethodPost, "/containers/%s/exec", nil, nil, nameOrID)
41
	if err != nil {
42
		return "", err
43
	}
44
	defer resp.Body.Close()
45

46
	respStruct := new(dockerAPI.IDResponse)
47
	if err := resp.Process(respStruct); err != nil {
48
		return "", err
49
	}
50

51
	return respStruct.ID, nil
52
}
53

54
// ExecInspect inspects an existing exec session, returning detailed information
55
// about it.
56
func ExecInspect(ctx context.Context, sessionID string, options *ExecInspectOptions) (*define.InspectExecSession, error) {
57
	if options == nil {
58
		options = new(ExecInspectOptions)
59
	}
60
	_ = options
61
	conn, err := bindings.GetClient(ctx)
62
	if err != nil {
63
		return nil, err
64
	}
65

66
	logrus.Debugf("Inspecting session ID %q", sessionID)
67

68
	resp, err := conn.DoRequest(ctx, nil, http.MethodGet, "/exec/%s/json", nil, nil, sessionID)
69
	if err != nil {
70
		return nil, err
71
	}
72
	defer resp.Body.Close()
73

74
	respStruct := new(define.InspectExecSession)
75
	if err := resp.Process(respStruct); err != nil {
76
		return nil, err
77
	}
78

79
	return respStruct, nil
80
}
81

82
// ExecStart starts (but does not attach to) a given exec session.
83
func ExecStart(ctx context.Context, sessionID string, options *ExecStartOptions) error {
84
	if options == nil {
85
		options = new(ExecStartOptions)
86
	}
87
	_ = options
88
	conn, err := bindings.GetClient(ctx)
89
	if err != nil {
90
		return err
91
	}
92

93
	logrus.Debugf("Starting exec session ID %q", sessionID)
94

95
	// We force Detach to true
96
	body := struct {
97
		Detach bool `json:"Detach"`
98
	}{
99
		Detach: true,
100
	}
101
	bodyJSON, err := json.Marshal(body)
102
	if err != nil {
103
		return err
104
	}
105

106
	resp, err := conn.DoRequest(ctx, bytes.NewReader(bodyJSON), http.MethodPost, "/exec/%s/start", nil, nil, sessionID)
107
	if err != nil {
108
		return err
109
	}
110
	defer resp.Body.Close()
111

112
	return resp.Process(nil)
113
}
114

115
// ExecRemove removes a given exec session.
116
func ExecRemove(ctx context.Context, sessionID string, options *ExecRemoveOptions) error {
117
	v := bindings.ServiceVersion(ctx)
118
	// The exec remove endpoint was added in 4.8.
119
	if v.Major < 4 || (v.Major == 4 && v.Minor < 8) {
120
		// Do no call this endpoint as it will not be supported on the server and throw an "NOT FOUND" error.
121
		return bindings.NewAPIVersionError("/exec/{id}/remove", v, "4.8.0")
122
	}
123
	if options == nil {
124
		options = new(ExecRemoveOptions)
125
	}
126
	conn, err := bindings.GetClient(ctx)
127
	if err != nil {
128
		return err
129
	}
130

131
	logrus.Debugf("Removing exec session ID %q", sessionID)
132

133
	// We force Detach to true
134
	body := struct {
135
		Force bool `json:"Force"`
136
	}{
137
		Force: false,
138
	}
139

140
	if options.Force != nil {
141
		body.Force = *options.Force
142
	}
143

144
	bodyJSON, err := json.Marshal(body)
145
	if err != nil {
146
		return err
147
	}
148

149
	resp, err := conn.DoRequest(ctx, bytes.NewReader(bodyJSON), http.MethodPost, "/exec/%s/remove", nil, nil, sessionID)
150
	if err != nil {
151
		return err
152
	}
153
	defer resp.Body.Close()
154

155
	return resp.Process(nil)
156
}
157

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

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

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

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