Line data Source code
1 43 : export const VERSION = "/v1.12/";
2 :
3 43 : const podmanCall = (con, name, method, args, body) => con.call({
4 43 : method,
5 43 : path: VERSION + name,
6 43 : body: body || "",
7 43 : params: args,
8 43 : });
9 :
10 43 : const podmanJson = (con, name, method, args, body) => podmanCall(con, name, method, args, body)
11 43 : .then(reply => JSON.parse(reply));
12 :
13 43 : export const streamEvents = (con, callback) => con.monitor(VERSION + "libpod/events", callback);
14 :
15 43 : export function getInfo(con) {
16 43 : return new Promise((resolve, reject) => {
17 0 : const timeout = setTimeout(() => reject(new Error("timeout")), 5000);
18 43 : podmanJson(con, "libpod/info", "GET", {})
19 43 : .then(reply => resolve(reply))
20 43 : .catch(reject)
21 43 : .finally(() => clearTimeout(timeout));
22 43 : });
23 43 : }
24 :
25 43 : export const getContainers = con => podmanJson(con, "libpod/containers/json", "GET", { all: true });
26 :
27 43 : export const streamContainerStats = (con, callback) => con.monitor(VERSION + "libpod/containers/stats", callback);
28 :
29 41 : export function inspectContainer(con, id) {
30 41 : const options = {
31 41 : size: false // set true to display filesystem usage
32 41 : };
33 41 : return podmanJson(con, "libpod/containers/" + id + "/json", "GET", options);
34 41 : }
35 :
36 4 : export const delContainer = (con, id, force) => podmanCall(con, "libpod/containers/" + id, "DELETE", { force });
37 :
38 2 : export const renameContainer = (con, id, config) => podmanCall(con, "libpod/containers/" + id + "/rename", "POST", config);
39 :
40 14 : export const createContainer = (con, config) => podmanJson(con, "libpod/containers/create", "POST", {}, JSON.stringify(config));
41 :
42 2 : export const commitContainer = (con, commitData) => podmanCall(con, "libpod/commit", "POST", commitData);
43 :
44 19 : export const postContainer = (con, action, id, args) => podmanCall(con, "libpod/containers/" + id + "/" + action, "POST", args);
45 :
46 2 : export const runHealthcheck = (con, id) => podmanCall(con, "libpod/containers/" + id + "/healthcheck", "GET", {});
47 :
48 1 : export const postPod = (con, action, id, args) => podmanCall(con, "libpod/pods/" + id + "/" + action, "POST", args);
49 :
50 1 : export const delPod = (con, id, force) => podmanCall(con, "libpod/pods/" + id, "DELETE", { force });
51 :
52 2 : export const createPod = (con, config) => podmanCall(con, "libpod/pods/create", "POST", {}, JSON.stringify(config));
53 :
54 4 : export function execContainer(con, id) {
55 4 : const args = {
56 4 : AttachStderr: true,
57 4 : AttachStdout: true,
58 4 : AttachStdin: true,
59 4 : Tty: true,
60 4 : Cmd: ["/bin/sh"],
61 4 : };
62 :
63 4 : return podmanJson(con, "libpod/containers/" + id + "/exec", "POST", {}, JSON.stringify(args));
64 4 : }
65 :
66 4 : export function resizeContainersTTY(con, id, exec, width, height) {
67 4 : const args = {
68 4 : h: height,
69 4 : w: width,
70 4 : };
71 :
72 4 : let point = "containers/";
73 4 : if (!exec)
74 4 : point = "exec/";
75 :
76 4 : return podmanCall(con, "libpod/" + point + id + "/resize", "POST", args);
77 4 : }
78 :
79 43 : function parseImageInfo(info) {
80 43 : const image = {};
81 :
82 43 : if (info.Config) {
83 43 : image.Entrypoint = info.Config.Entrypoint;
84 43 : image.Command = info.Config.Cmd;
85 42 : image.Ports = Object.keys(info.Config.ExposedPorts || {});
86 0 : image.Env = info.Config.Env || [];
87 43 : }
88 43 : image.Author = info.Author;
89 :
90 43 : return image;
91 43 : }
92 :
93 43 : export function getImages(con, id) {
94 43 : const options = {};
95 43 : if (id)
96 4 : options.filters = JSON.stringify({ id: [id] });
97 43 : return podmanJson(con, "libpod/images/json", "GET", options)
98 43 : .then(reply => {
99 43 : const images = {};
100 43 : const promises = [];
101 :
102 43 : for (const image of reply) {
103 43 : images[image.Id] = image;
104 43 : promises.push(podmanJson(con, "libpod/images/" + image.Id + "/json", "GET", {}));
105 43 : }
106 :
107 43 : return Promise.all(promises)
108 43 : .then(replies => {
109 43 : for (const info of replies)
110 43 : images[info.Id] = { uid: con.uid, ...images[info.Id], ...parseImageInfo(info) };
111 43 : return images;
112 43 : });
113 43 : });
114 43 : }
115 :
116 43 : export function getPods(con, id) {
117 43 : const options = {};
118 43 : if (id)
119 8 : options.filters = JSON.stringify({ id: [id] });
120 43 : return podmanJson(con, "libpod/pods/json", "GET", options);
121 43 : }
122 :
123 2 : export const delImage = (con, id, force) => podmanJson(con, "libpod/images/" + id, "DELETE", { force });
124 :
125 1 : export const untagImage = (con, id, repo, tag) => podmanCall(con, "libpod/images/" + id + "/untag", "POST", { repo, tag });
126 :
127 5 : export function pullImage(con, reference) {
128 5 : return new Promise((resolve, reject) => {
129 5 : podmanCall(con, "libpod/images/pull", "POST", { reference })
130 5 : .then(r => {
131 : // Need to check the last response if it contains error
132 5 : const responses = r.trim().split("\n");
133 5 : const response = JSON.parse(responses[responses.length - 1]);
134 1 : if (response.error) {
135 1 : response.message = response.error;
136 1 : reject(response);
137 1 : } else if (response.cause) // present for 400 and 500 errors
138 0 : reject(response);
139 : else
140 5 : resolve();
141 5 : })
142 5 : .catch(reject);
143 5 : });
144 5 : }
145 :
146 4 : export const pruneUnusedImages = con => podmanJson(con, "libpod/images/prune?all=true", "POST", {});
147 :
148 1 : export const imageHistory = (con, id) => podmanJson(con, `libpod/images/${id}/history`, "GET", {});
149 :
150 14 : export const imageExists = (con, id) => podmanCall(con, "libpod/images/" + id + "/exists", "GET", {});
151 :
152 14 : export const containerExists = (con, id) => podmanCall(con, "libpod/containers/" + id + "/exists", "GET", {});
|