UNPKG

5.59 kBPlain TextView Raw
1'use strict'
2
3import Modem = require('docker-modem')
4
5/**
6 * Class representing a service
7 */
8export class Service {
9 modem: Modem
10 id: String
11 data: Object = {}
12
13 /**
14 * Create a service
15 * @param {Modem} modem Modem to connect to the remote service
16 * @param {string} id Id of the service (optional)
17 */
18 constructor (modem: Modem, id: String) {
19 this.modem = modem
20 this.id = id
21 }
22
23 /**
24 * Update a service
25 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/update-a-service
26 * @param {Object} opts Query params in the request (optional)
27 * @return {Promise} Promise return the new service
28 */
29 update (opts?: Object): Promise<Service> {
30 const call = {
31 path: `/services/${this.id}/update?`,
32 method: 'POST',
33 options: opts,
34 statusCodes: {
35 200: true,
36 404: 'no such service',
37 500: 'server error'
38 }
39 }
40
41 return new Promise((resolve, reject) => {
42 this.modem.dial(call, (err, conf) => {
43 if (err) return reject(err)
44 const service = new Service(this.modem, this.id)
45 service.data = conf
46 resolve(service)
47 })
48 })
49 }
50
51 /**
52 * Get low-level information on a service
53 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-one-or-more-services
54 * The reason why this module isn't called inspect is because that interferes with the inspect utility of service.
55 * @param {Object} opts Query params in the request (optional)
56 * @return {Promise} Promise return the service
57 */
58 status (opts?: Object): Promise<Service> {
59 const call = {
60 path: `/services/${this.id}?`,
61 method: 'GET',
62 options: opts,
63 statusCodes: {
64 200: true,
65 404: 'no such service',
66 500: 'server error'
67 }
68 }
69
70 return new Promise((resolve, reject) => {
71 this.modem.dial(call, (err, conf) => {
72 if (err) return reject(err)
73 const service = new Service(this.modem, this.id)
74 service.data = conf
75 resolve(service)
76 })
77 })
78 }
79
80 /**
81 * Remove a service
82 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/remove-a-service
83 * @param {Object} opts Query params in the request (optional)
84 * @return {Promise} Promise return the result
85 */
86 remove (opts?: Object): Promise<String> {
87 const call = {
88 path: `/services/${this.id}?`,
89 method: 'DELETE',
90 options: opts,
91 statusCodes: {
92 200: true,
93 404: 'no such service',
94 500: 'server error'
95 }
96 }
97
98 return new Promise((resolve, reject) => {
99 this.modem.dial(call, (err, res: String) => {
100 if (err) return reject(err)
101 resolve(res)
102 })
103 })
104 }
105
106 /**
107 * Logs of a service
108 * https://docs.docker.com/engine/api/v1.27/#operation/ServiceLogs
109 * @param {Object} opts Query params in the request (optional)
110 * @return {Promise} Promise return the result
111 */
112 logs (opts?: Object): Promise<String> {
113 const call = {
114 path: `/services/${this.id}/logs?`,
115 method: 'GET',
116 options: opts,
117 statusCodes: {
118 101: true,
119 200: true,
120 404: 'no such service',
121 500: 'server error',
122 501: 'use --experimental to see this',
123 503: 'node is not part of a swarm'
124 }
125 }
126
127 return new Promise((resolve, reject) => {
128 this.modem.dial(call, (err, res: String) => {
129 if (err) return reject(err)
130 resolve(res)
131 })
132 })
133 }
134}
135
136export default class {
137 modem: Modem
138
139 /**
140 * Create a service
141 * @param {Modem} modem Modem to connect to the remote service
142 */
143 constructor (modem: Modem) {
144 this.modem = modem
145 }
146
147 /**
148 * Get a Service object
149 * @param {id} string ID of the secret
150 * @return {Network}
151 */
152 get (id: String): Service {
153 return new Service(this.modem, id)
154 }
155
156 /**
157 * Create a service
158 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-a-service
159 * @param {Object} opts Query params in the request (optional)
160 * @return {Promise} Promise return the new service
161 */
162 create (opts?: Object): Promise<Service> {
163 const call = {
164 path: '/services/create?',
165 method: 'POST',
166 options: opts,
167 statusCodes: {
168 201: true,
169 406: 'node is not part of a swarm',
170 409: 'name conflicts'
171 }
172 }
173
174 return new Promise((resolve, reject) => {
175 this.modem.dial(call, (err, conf) => {
176 if (err) return reject(err)
177 const service = new Service(this.modem, conf.ID)
178 service.data = conf
179 resolve(service)
180 })
181 })
182 }
183
184 /**
185 * Get the list of services
186 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-services
187 * @param {Object} opts Query params in the request (optional)
188 * @return {Promise} Promise returning the result as a list of services
189 */
190 list (opts?: Object): Promise<Array<Service>> {
191 const call = {
192 path: '/services?',
193 method: 'GET',
194 options: opts,
195 statusCodes: {
196 200: true,
197 500: 'server error'
198 }
199 }
200
201 return new Promise((resolve, reject) => {
202 this.modem.dial(call, (err, result) => {
203 if (err) return reject(err)
204 resolve(result.map((conf) => {
205 const service = new Service(this.modem, conf.ID)
206 service.data = conf
207 return service
208 }))
209 })
210 })
211 }
212}