-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.js
126 lines (123 loc) · 4.25 KB
/
controller.js
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
const Docker = require('dockerode');
const docker = new Docker();
module.exports = {
listAll: async () => {
return new Promise((resolve, reject) => {
docker.listContainers({ all: true }, function (err, containers) {
if (err) return reject(err)
resolve(containers)
});
});
},
getInfo: async (id) => {
return new Promise(async (resolve, reject) => {
const container = await docker.getContainer(id);
container.inspect(function (err, data) {
if (err) return reject(err)
if (data.Id != id) reject("notvalid")
resolve(data)
});
});
},
getContainer: async (id) => {
return new Promise(async (resolve, reject) => {
const container = await docker.getContainer(id);
container.inspect(function (err, data) {
if (err) return reject(err)
if (data.Id != id) reject("notvalid")
});
resolve(container)
});
},
startContainer: async (id) => {
return new Promise(async (resolve, reject) => {
const container = await docker.getContainer(id);
container.start()
resolve(true)
})
},
stopContainer: async (id) => {
return new Promise(async (resolve, reject) => {
const container = await docker.getContainer(id);
container.stop().catch(err => {
reject(err.statusCode)
})
resolve(true)
})
},
killContainer: async (id) => {
return new Promise(async (resolve, reject) => {
const container = await docker.getContainer(id);
container.kill().catch(err => {
reject(err.statusCode)
})
resolve(true)
})
},
startAllContainers: async () => {
return new Promise(async (resolve, reject) => {
docker.listContainers({ all: true }, function (err, containers) {
if (err) reject(false);
containers.forEach(function (containerInfo) {
const container = docker.getContainer(containerInfo.Id);
container.start().catch(error => {
if (error.statusCode === 304) return
});
});
});
resolve(true);
})
},
stopAllContainers: async () => {
return new Promise(async (resolve, reject) => {
docker.listContainers({ all: true }, function (err, containers) {
if (err) reject(false);
containers.forEach(function (containerInfo) {
docker.getContainer(containerInfo.Id).stop().catch(error => {
if (error.statusCode === 304) return
});
});
});
resolve(true);
})
},
killAllContainers: async () => {
return new Promise(async (resolve, reject) => {
docker.listContainers({ all: true }, function (err, containers) {
if (err) reject(false);
containers.forEach(function (containerInfo) {
docker.getContainer(containerInfo.Id).kill().catch(error => {
if (error.statusCode === 409) return
});
});
});
resolve(true);
})
},
// The two below are not used yet.
removeContainer: async (id) => {
return new Promise(async (resolve, reject) => {
const container = docker.getContainer(id);
if (!container.Name) return reject(false)
container.remove()
resolve(true)
})
},
createContainer: async (name, image, ports) => {
docker.createContainer({
Image: image,
name: name,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
HostConfig: {
PortBindings: {
'80/tcp': [{ HostPort: '80' }], '443/tcp': [{ HostPort: '443' }]
},
},
}).then(function (container) {
container.start();
})
}
}