1 | 'use strict'
|
2 |
|
3 | import Modem = require('docker-modem')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export class Service {
|
9 | modem: Modem
|
10 | id: String
|
11 | data: Object = {}
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | constructor (modem: Modem, id: String) {
|
19 | this.modem = modem
|
20 | this.id = id
|
21 | }
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
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 |
|
82 |
|
83 |
|
84 |
|
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 |
|
108 |
|
109 |
|
110 |
|
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 |
|
136 | export default class {
|
137 | modem: Modem
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 | constructor (modem: Modem) {
|
144 | this.modem = modem
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 | get (id: String): Service {
|
153 | return new Service(this.modem, id)
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
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 |
|
186 |
|
187 |
|
188 |
|
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 | }
|