-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (68 loc) · 1.82 KB
/
Copy pathindex.js
File metadata and controls
80 lines (68 loc) · 1.82 KB
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
import minimist from 'minimist'
import {
log,
cloud_detector,
join_docker_swarm,
register_with_agent,
find_zerotier_address,
load_certificates_maybe,
find_not_zerotier_address
} from './utils.js'
const args = minimist(process.argv.slice(2), {
default: {
type: 'worker',
retries: 5,
secretpath: '/run/secrets',
docker_port: '4243',
docker_host: '127.0.0.1',
detectInterval: 10000,
register_api_host: '127.0.0.1',
register_api_port: '3210',
swarm_listen_address: '2377',
use_zt_network: false
}
})
console.log(args)
const state = {
tls: false,
cert: {
tlsCert: "",
tlsKey: "",
tlsCA: ""
}
}
let retries = 0
load_certificates_maybe(args, state)
async function detectAndUpdate() {
log('Finding ZT interface')
let address = find_zerotier_address(args)
log(`Found ZT interface: ${address}`)
log('Detecting cloud environment...')
let cloud = { cloud: 'unknown', zone: 'unknown', labels: {} }
try { cloud = await cloud_detector() } catch(e) { console.error('ERROR: Unable to detect cloud environment, using default') }
log(`Found cloud environment: ${cloud?.cloud}`)
log('Registering with agent')
const agent_response = await register_with_agent(args, cloud, address)
log('Successfully registered with agent.')
log('Joining swarm')
if (args.use_zt_network) {
await join_docker_swarm(args, state, address, agent_response?.token)
}
else {
await join_docker_swarm(args, state, find_not_zerotier_address(), agent_response?.token)
}
log('Swarm joined. Exiting...')
process.exit(0)
}
process.on('uncaughtException', log)
async function loop() {
try {
await detectAndUpdate()
} catch(e) {
console.error(e)
retries++
if (retries >= args.retries) process.exit(1)
setTimeout(loop, args?.detectInterval)
}
}
loop()