aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/porto/libporto.hpp
blob: e30f22a41e7fbc1d8254509df80eb4a90418b4b3 (plain) (blame)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#pragma once

#include <atomic>
#include <thread>

#include <util/string/cast.h>
#include <util/generic/hash.h>
#include <util/generic/map.h>
#include <util/generic/vector.h>

#include <library/cpp/porto/proto/rpc.pb.h>

namespace Porto {

constexpr int INFINITE_TIMEOUT = -1;
constexpr int DEFAULT_TIMEOUT = 300;        // 5min
constexpr int DEFAULT_DISK_TIMEOUT = 900;   // 15min

constexpr char SOCKET_PATH[] = "/run/portod.socket";

typedef std::function<void(const TWaitResponse &event)> TWaitCallback;

enum {
    GET_NONBLOCK = 1,   // try lock container state
    GET_SYNC = 2,       // refresh cached values, cache ttl 5s
    GET_REAL = 4,       // no faked or inherited values
};

struct DockerImage {
    std::string Id;
    std::vector<std::string> Tags;
    std::vector<std::string> Digests;
    std::vector<std::string> Layers;
    uint64_t Size;
    struct Config {
        std::vector<std::string> Cmd;
        std::vector<std::string> Env;
    } Config;

    DockerImage() = default;
    DockerImage(const TDockerImage &i);

    DockerImage(const DockerImage &i) = default;
    DockerImage(DockerImage &&i) = default;

    DockerImage& operator=(const DockerImage &i) = default;
    DockerImage& operator=(DockerImage &&i) = default;
};

class TPortoApi {
#ifdef __linux__
    friend class TAsyncWaiter;
#endif
private:
    int Fd = -1;
    int Timeout = DEFAULT_TIMEOUT;
    int DiskTimeout = DEFAULT_DISK_TIMEOUT;
    bool AutoReconnect = true;

    EError LastError = EError::Success;
    TString LastErrorMsg;

    /*
     * These keep last request and response. Method might return
     * pointers to Rsp innards -> pointers valid until next call.
     */
    TPortoRequest Req;
    TPortoResponse Rsp;

    std::vector<TString> AsyncWaitNames;
    std::vector<TString> AsyncWaitLabels;
    int AsyncWaitTimeout = INFINITE_TIMEOUT;
    TWaitCallback AsyncWaitCallback;
    bool AsyncWaitOneShot = false;

    EError SetError(const TString &prefix, int _errno) Y_WARN_UNUSED_RESULT;

    EError SetSocketTimeout(int direction, int timeout) Y_WARN_UNUSED_RESULT;

    EError Send(const TPortoRequest &req) Y_WARN_UNUSED_RESULT;

    EError Recv(TPortoResponse &rsp) Y_WARN_UNUSED_RESULT;

    EError Call(int extra_timeout = 0) Y_WARN_UNUSED_RESULT;

    EError CallWait(TString &result_state, int wait_timeout) Y_WARN_UNUSED_RESULT;

public:
    TPortoApi() { }
    ~TPortoApi();

    int GetFd() const {
        return Fd;
    }

    bool Connected() const {
        return Fd >= 0;
    }

    EError Connect(const char *socket_path = SOCKET_PATH) Y_WARN_UNUSED_RESULT;
    void Disconnect();

    /* Requires signal(SIGPIPE, SIG_IGN) */
    void SetAutoReconnect(bool auto_reconnect) {
        AutoReconnect = auto_reconnect;
    }

    /* Request and response timeout in seconds */
    int GetTimeout() const {
        return Timeout;
    }
    EError SetTimeout(int timeout);

    /* Extra timeout for disk operations in seconds */
    int GetDiskTimeout() const {
        return DiskTimeout;
    }
    EError SetDiskTimeout(int timeout);

    EError Error() const Y_WARN_UNUSED_RESULT {
        return LastError;
    }

    EError GetLastError(TString &msg) const Y_WARN_UNUSED_RESULT {
        msg = LastErrorMsg;
        return LastError;
    }

    /* Returns "LastError:(LastErrorMsg)" */
    TString GetLastError() const Y_WARN_UNUSED_RESULT;

    /* Returns text protobuf */
    TString GetLastRequest() const {
        return Req.DebugString();
    }
    TString GetLastResponse() const {
        return Rsp.DebugString();
    }

    /* To be used for next changed_since */
    uint64_t ResponseTimestamp() const Y_WARN_UNUSED_RESULT {
        return Rsp.timestamp();
    }

    // extra_timeout: 0 - none, -1 - infinite
    EError Call(const TPortoRequest &req,
                TPortoResponse &rsp,
                int extra_timeout = 0) Y_WARN_UNUSED_RESULT;

    EError Call(const TString &req,
                TString &rsp,
                int extra_timeout = 0) Y_WARN_UNUSED_RESULT;

    /* System */

    EError GetVersion(TString &tag, TString &revision) Y_WARN_UNUSED_RESULT;

    const TGetSystemResponse *GetSystem();

    EError SetSystem(const TString &key, const TString &val) Y_WARN_UNUSED_RESULT;

    /* Container */

    const TListPropertiesResponse *ListProperties();

    EError ListProperties(TVector<TString> &properties) Y_WARN_UNUSED_RESULT;

    const TListResponse *List(const TString &mask = "");

    EError List(TVector<TString> &names, const TString &mask = "") Y_WARN_UNUSED_RESULT;

    EError Create(const TString &name) Y_WARN_UNUSED_RESULT;

    EError CreateWeakContainer(const TString &name) Y_WARN_UNUSED_RESULT;

    EError Destroy(const TString &name) Y_WARN_UNUSED_RESULT;

    EError Start(const TString &name)Y_WARN_UNUSED_RESULT;

    // stop_timeout: time between SIGTERM and SIGKILL, -1 - default
    EError Stop(const TString &name, int stop_timeout = -1) Y_WARN_UNUSED_RESULT;

    EError Kill(const TString &name, int sig = 9) Y_WARN_UNUSED_RESULT;

    EError Pause(const TString &name) Y_WARN_UNUSED_RESULT;

    EError Resume(const TString &name) Y_WARN_UNUSED_RESULT;

    EError Respawn(const TString &name) Y_WARN_UNUSED_RESULT;

    // wait_timeout: 0 - nonblock, -1 - infinite
    EError WaitContainer(const TString &name,
                         TString &result_state,
                         int wait_timeout = INFINITE_TIMEOUT) Y_WARN_UNUSED_RESULT;

    EError WaitContainers(const TVector<TString> &names,
                          TString &result_name,
                          TString &result_state,
                          int wait_timeout = INFINITE_TIMEOUT) Y_WARN_UNUSED_RESULT;

    const TWaitResponse *Wait(const TVector<TString> &names,
                              const TVector<TString> &labels,
                              int wait_timeout = INFINITE_TIMEOUT) Y_WARN_UNUSED_RESULT;

    EError AsyncWait(const TVector<TString> &names,
                     const TVector<TString> &labels,
                     TWaitCallback callbacks,
                     int wait_timeout = INFINITE_TIMEOUT,
                     const TString &targetState = "") Y_WARN_UNUSED_RESULT;

    EError StopAsyncWait(const TVector<TString> &names,
                         const TVector<TString> &labels,
                         const TString &targetState = "") Y_WARN_UNUSED_RESULT;

    const TGetResponse *Get(const TVector<TString> &names,
                            const TVector<TString> &properties,
                            int flags = 0) Y_WARN_UNUSED_RESULT;

    /* Porto v5 api */
    EError GetContainerSpec(const TString &name, TContainer &container) Y_WARN_UNUSED_RESULT ;
    EError ListContainersBy(const TListContainersRequest &listContainersRequest, TVector<TContainer> &containers) Y_WARN_UNUSED_RESULT;
    EError CreateFromSpec(const TContainerSpec &container, TVector<TVolumeSpec> volumes, bool start = false) Y_WARN_UNUSED_RESULT;
    EError UpdateFromSpec(const TContainerSpec &container) Y_WARN_UNUSED_RESULT;

    EError GetProperty(const TString &name,
                       const TString &property,
                       TString &value,
                       int flags = 0) Y_WARN_UNUSED_RESULT;

    EError GetProperty(const TString &name,
                       const TString &property,
                       const TString &index,
                       TString &value,
                       int flags = 0) Y_WARN_UNUSED_RESULT {
        return GetProperty(name, property + "[" + index + "]", value, flags);
    }

    EError SetProperty(const TString &name,
                       const TString &property,
                       const TString &value) Y_WARN_UNUSED_RESULT;

    EError SetProperty(const TString &name,
                       const TString &property,
                       const TString &index,
                       const TString &value) Y_WARN_UNUSED_RESULT {
        return SetProperty(name, property + "[" + index + "]", value);
    }

    EError GetInt(const TString &name,
                  const TString &property,
                  const TString &index,
                  uint64_t &value) Y_WARN_UNUSED_RESULT;

    EError GetInt(const TString &name,
                       const TString &property,
                       uint64_t &value) Y_WARN_UNUSED_RESULT {
        return GetInt(name, property, "", value);
    }

    EError SetInt(const TString &name,
                  const TString &property,
                  const TString &index,
                  uint64_t value) Y_WARN_UNUSED_RESULT;

    EError SetInt(const TString &name,
                  const TString &property,
                  uint64_t value) Y_WARN_UNUSED_RESULT {
        return SetInt(name, property, "", value);
    }

    EError GetProcMetric(const TVector<TString> &names,
                         const TString &metric,
                         TMap<TString, uint64_t> &values);

    EError GetLabel(const TString &name,
                    const TString &label,
                    TString &value) Y_WARN_UNUSED_RESULT {
        return GetProperty(name, "labels", label, value);
    }

    EError SetLabel(const TString &name,
                    const TString &label,
                    const TString &value,
                    const TString &prev_value = " ") Y_WARN_UNUSED_RESULT;

    EError IncLabel(const TString &name,
                    const TString &label,
                    int64_t add,
                    int64_t &result) Y_WARN_UNUSED_RESULT;

    EError IncLabel(const TString &name,
                    const TString &label,
                    int64_t add = 1) Y_WARN_UNUSED_RESULT {
        int64_t result;
        return IncLabel(name, label, add, result);
    }

    EError ConvertPath(const TString &path,
                       const TString &src_name,
                       const TString &dst_name,
                       TString &result_path) Y_WARN_UNUSED_RESULT;

    EError AttachProcess(const TString &name, int pid,
                         const TString &comm = "") Y_WARN_UNUSED_RESULT;

    EError AttachThread(const TString &name, int pid,
                        const TString &comm = "") Y_WARN_UNUSED_RESULT;

    EError LocateProcess(int pid,
                         const TString &comm /* = "" */,
                         TString &name) Y_WARN_UNUSED_RESULT;

    /* Volume */

    const TListVolumePropertiesResponse *ListVolumeProperties();

    EError ListVolumeProperties(TVector<TString> &properties) Y_WARN_UNUSED_RESULT;

    const TListVolumesResponse *ListVolumes(const TString &path = "",
                                            const TString &container = "");

    EError ListVolumes(TVector<TString> &paths) Y_WARN_UNUSED_RESULT;

    const TVolumeDescription *GetVolumeDesc(const TString &path);

    /* Porto v5 api */
    EError ListVolumesBy(const TGetVolumeRequest &getVolumeRequest, TVector<TVolumeSpec> &volumes) Y_WARN_UNUSED_RESULT;
    EError CreateVolumeFromSpec(const TVolumeSpec &volume, TVolumeSpec &resultSpec) Y_WARN_UNUSED_RESULT;

    const TVolumeSpec *GetVolume(const TString &path);

    const TGetVolumeResponse *GetVolumes(uint64_t changed_since = 0);

    EError CreateVolume(TString &path,
                        const TMap<TString, TString> &config) Y_WARN_UNUSED_RESULT;

    EError LinkVolume(const TString &path,
                      const TString &container = "",
                      const TString &target = "",
                      bool read_only = false,
                      bool required = false) Y_WARN_UNUSED_RESULT;

    EError UnlinkVolume(const TString &path,
                        const TString &container = "",
                        const TString &target = "***",
                        bool strict = false) Y_WARN_UNUSED_RESULT;

    EError TuneVolume(const TString &path,
                      const TMap<TString, TString> &config) Y_WARN_UNUSED_RESULT;

    /* Layer */

    const TListLayersResponse *ListLayers(const TString &place = "",
                                          const TString &mask = "");

    EError ListLayers(TVector<TString> &layers,
                      const TString &place = "",
                      const TString &mask = "") Y_WARN_UNUSED_RESULT;

    EError ImportLayer(const TString &layer,
                       const TString &tarball,
                       bool merge = false,
                       const TString &place = "",
                       const TString &private_value = "",
                       bool verboseError = false) Y_WARN_UNUSED_RESULT;

    EError ExportLayer(const TString &volume,
                       const TString &tarball,
                       const TString &compress = "") Y_WARN_UNUSED_RESULT;

    EError ReExportLayer(const TString &layer,
                         const TString &tarball,
                         const TString &compress = "") Y_WARN_UNUSED_RESULT;

    EError RemoveLayer(const TString &layer,
                       const TString &place = "",
                       bool async = false) Y_WARN_UNUSED_RESULT;

    EError GetLayerPrivate(TString &private_value,
                           const TString &layer,
                           const TString &place = "") Y_WARN_UNUSED_RESULT;

    EError SetLayerPrivate(const TString &private_value,
                           const TString &layer,
                           const TString &place = "") Y_WARN_UNUSED_RESULT;

    /* Docker images */

    EError DockerImageStatus(DockerImage &image,
                             const TString &name,
                             const TString &place = "") Y_WARN_UNUSED_RESULT;

    EError ListDockerImages(std::vector<DockerImage> &images,
                            const TString &place = "",
                            const TString &mask = "") Y_WARN_UNUSED_RESULT;

    EError PullDockerImage(DockerImage &image,
                           const TString &name,
                           const TString &place = "",
                           const TString &auth_token = "",
                           const TString &auth_host = "",
                           const TString &auth_service = "") Y_WARN_UNUSED_RESULT;

    EError RemoveDockerImage(const TString &name,
                             const TString &place = "");

    /* Storage */

    const TListStoragesResponse *ListStorages(const TString &place = "",
                                              const TString &mask = "");

    EError ListStorages(TVector<TString> &storages,
                        const TString &place = "",
                        const TString &mask = "") Y_WARN_UNUSED_RESULT;

    EError RemoveStorage(const TString &storage,
                         const TString &place = "") Y_WARN_UNUSED_RESULT;

    EError ImportStorage(const TString &storage,
                         const TString &archive,
                         const TString &place = "",
                         const TString &compression = "",
                         const TString &private_value = "") Y_WARN_UNUSED_RESULT;

    EError ExportStorage(const TString &storage,
                         const TString &archive,
                         const TString &place = "",
                         const TString &compression = "") Y_WARN_UNUSED_RESULT;
};

#ifdef __linux__
class TAsyncWaiter {
    struct TCallbackData {
        const TWaitCallback Callback;
        const TString State;
    };

    enum class ERequestType {
        None,
        Add,
        Del,
        Stop,
    };

    THashMap<TString, TCallbackData> AsyncCallbacks;
    std::unique_ptr<std::thread> WatchDogThread;
    std::atomic<uint64_t> CallbacksCount;
    int EpollFd = -1;
    TPortoApi Api;

    int Sock, MasterSock;
    TString ReqCt;
    TString ReqState;
    TWaitCallback ReqCallback;

    std::function<void(const TString &error, int ret)> FatalCallback;
    bool FatalError = false;

    void MainCallback(const TWaitResponse &event);
    inline TWaitCallback GetMainCallback() {
        return [this](const TWaitResponse &event) {
            MainCallback(event);
        };
    }

    int Repair();
    void WatchDog();

    void SendInt(int fd, int value);
    int RecvInt(int fd);

    void HandleAddRequest();
    void HandleDelRequest();

    void Fatal(const TString &error, int ret) {
        FatalError = true;
        FatalCallback(error, ret);
    }

    public:
    TAsyncWaiter(std::function<void(const TString &error, int ret)> fatalCallback);
    ~TAsyncWaiter();

    int Add(const TString &ct, const TString &state, TWaitCallback callback);
    int Remove(const TString &ct);
    uint64_t InvocationCount() const {
        return CallbacksCount;
    }
};
#endif

} /* namespace Porto */