-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker-compose.yml
267 lines (235 loc) · 8.36 KB
/
docker-compose.yml
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
version: '3.9'
services:
syncthing:
image: syncthing/syncthing:1.23
hostname: '{{.Node.Hostname}}'
volumes:
- /var/syncthing:/var/syncthing
networks:
- syncthing
environment:
- STGUIAPIKEY=A1B2C3
#- STNODEFAULTFOLDER=1
#ports:
# - target: 8384
# published: 48384
# protocol: tcp
# mode: host
deploy:
mode: global
#placement:
# constraints:
# - node.labels.type==app
syncthing-controller:
image: node:lts-alpine
hostname: '{{.Node.Hostname}}'
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- syncthing
environment:
- STGUIAPIKEY=A1B2C3
deploy:
replicas: 1
placement:
constraints:
- node.role==manager
entrypoint: ["/bin/sh", "-c"]
command:
- |
/bin/sleep 10
cd /home/node
/usr/local/bin/npm install axios
cat << 'EOF' > controller.js
import axios from 'axios'
// for Docker: handle SIGINT and SIGTERM
process.on('SIGINT', function () { process.exit() })
process.on('SIGTERM', function () { process.exit() })
const debug = false
const KEY = process.env.STGUIAPIKEY || ''
const SECS = process.env.SYNCTHING_SLEEP || '60'
const PORT = process.env.SYNCTHING_PORT || '8384'
function arraysEqual(a1, a2) {
return JSON.stringify(a1.sort()) == JSON.stringify(a2.sort());
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// get data from Docker via docker.sock
async function getDocker(url) {
const options = {
socketPath: '/var/run/docker.sock',
headers: {
'Content-Type': 'application/json',
}
}
const response = await axios.get(url, options);
return response.data
}
// get IPs of all running tasks of a Docker service
async function getDockerServiceIPs(name) {
const filters = {
'service': [name],
'desired-state': ['running'],
}
const tasks = await getDocker('/tasks?filters=' + JSON.stringify(filters))
let ips = []
for (let t of tasks) {
for (let n of t.NetworksAttachments) {
for (let i of n.Addresses) {
let ip = String(i).substring(0, i.indexOf('/'))
ips.push(ip)
}
}
}
return ips
}
// get data from Syncthing
async function getSyncthing(url) {
const options = {
headers: {
'Content-Type': 'application/json',
'X-API-Key': KEY,
}
}
const response = await axios.get(url, options);
return response.data
}
// post data to Syncthing
async function postSyncthing(url, post) {
const options = {
headers: {
'Content-Type': 'application/json',
'X-API-Key': KEY,
}
}
const response = await axios.post(url, post, options);
return response.data
}
// patch data to Syncthing
async function patchSyncthing(url, patch) {
const options = {
headers: {
'Content-Type': 'application/json',
'X-API-Key': KEY,
}
}
const response = await axios.patch(url, patch, options);
return response.data
}
// get myID of a Syncthing instance
async function getSyncthingID(ip) {
const url = 'http://' + ip + ':' + PORT + '/rest/system/status'
const data = await getSyncthing(url)
return data.myID
}
// get all myIDs of all Syncthing instances
async function getAllSyncthingIDs(ips) {
let ids = []
for (let ip of ips) {
let id = await getSyncthingID(ip)
ids.push(id)
}
return ids
}
// get devices of a Syncthing instance
async function getSyncthingDevices(ip) {
const url = 'http://' + ip + ':' + PORT + '/rest/config/devices'
const data = await getSyncthing(url)
return data.map(d => d.deviceID)
}
// add a device to a Syncthing instance
async function addSyncthingDevice(ip, device) {
const url = 'http://' + ip + ':' + PORT + '/rest/config/devices'
const post = {
deviceID: device,
autoAcceptFolders: true
}
const data = await postSyncthing(url, post)
return data
}
// add missing devices to every Syncthing instance
async function addMissingSyncthingDevices(ips, ids) {
for (let ip of ips) {
let devices = await getSyncthingDevices(ip)
for (let id of ids) {
if (!devices.includes(id)) {
console.log('add device to Syncthing', ip, id)
await addSyncthingDevice(ip, id)
}
}
}
}
// get folders of a Syncthing instance
async function getSyncthingFolders(ip) {
const url = 'http://' + ip + ':' + PORT + '/rest/config/folders'
const data = await getSyncthing(url)
return data.map(f => f.path)
}
// add a folder to a Syncthing instance
async function addSyncthingFolder(ip, folder) {
const url = 'http://' + ip + ':' + PORT + '/rest/config/folders'
let post = {
id: folder,
label: folder,
path: folder,
"type": "sendreceive",
"devices": []
}
for (let id of await getSyncthingDevices(ip)) {
post.devices.push({ "deviceID": id })
}
const data = await postSyncthing(url, post)
return data
}
// add devices to folder of every Syncthing instance
async function addDevicesToFolder(ips, ids, name) {
for (let ip of ips) {
const url = 'http://' + ip + ':' + PORT + '/rest/config/folders/' + name
let folder = await getSyncthing(url)
let devices = folder.devices.map(d => d.deviceID)
if (!arraysEqual(ids, devices)) {
console.log('patch folder devices at Syncthing', ip, folder.id, ids)
const patch = { 'devices': [] }
for (let i of ids) {
patch.devices.push({ 'deviceID': i })
}
await patchSyncthing(url, patch)
} else {
console.log('folder already shared at Syncthing', ip, folder.id, ids)
}
}
}
// add missing folder to a single Syncthing instance
async function addMissingSyncthingFolder(ip, folder) {
let folders = await getSyncthingFolders(ip)
if (!folders.includes(folder)) {
console.log('add folder to Syncthing', ip, folder)
await addSyncthingFolder(ip, folder)
}
}
async function run() {
const ips = await getDockerServiceIPs('syncthing_syncthing')
console.log('Got IPs from Docker Swarm', ips)
const ids = await getAllSyncthingIDs(ips)
console.log('Got IDs from Syncthing', ids)
const updatedDevices = await addMissingSyncthingDevices(ips, ids)
const updatedFolders = await addDevicesToFolder(ips, ids, 'default')
}
while (true) {
try {
run()
} catch (e) {
console.error(e.message)
}
console.log('----- going to sleep for ' + SECS + ' seconds -----')
await sleep(SECS * 1000)
}
EOF
echo '{ "type": "module" }' > package.json
/usr/local/bin/node controller.js
networks:
syncthing:
name: syncthing
driver: overlay
attachable: true