1 | import fs from 'fs'
|
2 | import yaml from 'js-yaml'
|
3 | import scheduler from '@zombiec0rn/zombie-scheduler'
|
4 | import zdiff from '@zombiec0rn/zombie-service-diff'
|
5 | import assign from 'object.assign'
|
6 | import uniq from 'lodash.uniq'
|
7 | import find from 'lodash.find'
|
8 | import randomString from 'random-string'
|
9 | import * as utils from './utils'
|
10 |
|
11 | function scrambleFingerprint(service) {
|
12 | service.previousFingerprint = service.fingerprint
|
13 | service.fingerprint = randomString()
|
14 | }
|
15 |
|
16 | function getCurrent(nodes) {
|
17 | let services = nodes.reduce((services, node) => {
|
18 | let nodeServices = utils.extractServices(node)
|
19 | delete node.services
|
20 | return services.concat(nodeServices)
|
21 | }, [])
|
22 |
|
23 |
|
24 |
|
25 | let duplicates = utils.detectDuplicateFingerprints(services)
|
26 | services.forEach(s => {
|
27 | if (duplicates.indexOf(s.fingerprint) >= 0) {
|
28 | scrambleFingerprint(s)
|
29 | }
|
30 | })
|
31 |
|
32 | return services
|
33 | }
|
34 |
|
35 | export default function makePlan(nodes, _wanted) {
|
36 |
|
37 |
|
38 |
|
39 | let current = getCurrent(nodes)
|
40 | let wanted = _wanted.map(s => {
|
41 | s.fingerprint = zdiff.fingerprint(s)
|
42 | return s
|
43 | })
|
44 |
|
45 |
|
46 | let tags = uniq(nodes.reduce((t, n) => {
|
47 | return t.concat(n.tags || [])
|
48 | },[]).concat(wanted.reduce((t,s) => {
|
49 | return t.concat(s.placement || [])
|
50 | },[])))
|
51 |
|
52 | let tagmap = tags.reduce((m, t) => {
|
53 | m[t] = {}
|
54 | m[t].nodes = nodes.filter(n => (n.tags || []).indexOf(t) >= 0)
|
55 | m[t].wanted = wanted.filter(s => (s.placement || []).indexOf(t) >= 0)
|
56 | return m
|
57 | },{})
|
58 |
|
59 | let wantedmap = wanted.reduce((m, s) => {
|
60 | m[s.id] = s
|
61 | return m
|
62 | },{})
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | var tagadds = []
|
71 | tags.forEach(t => {
|
72 | let tagplan = scheduler.spread(tagmap[t].nodes, tagmap[t].wanted)
|
73 | tagplan.add.forEach(s => {
|
74 | assign(s, wantedmap[s.id])
|
75 | })
|
76 | tagadds = tagadds.concat(tagplan.add)
|
77 | })
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | current.forEach(s => {
|
84 | let tagadd = find(tagadds, { id: s.id })
|
85 | if (!tagadd) return
|
86 | if (tagadd.host.hostname != s.host.hostname) {
|
87 |
|
88 | scrambleFingerprint(s)
|
89 | }
|
90 | else if (tagadd.fingerprint != s.fingerprint) {
|
91 |
|
92 |
|
93 | scrambleFingerprint(s)
|
94 | }
|
95 | else {
|
96 |
|
97 | tagadds = tagadds.filter(ta => ta.id != s.id)
|
98 | }
|
99 | })
|
100 |
|
101 | let plan = scheduler.spread(nodes, wanted, current.concat(tagadds))
|
102 | let tagaddids = tagadds.map(s => s.id)
|
103 | plan.keep = plan.keep.filter(s => {
|
104 | let istagadd = tagaddids.indexOf(s.id) >= 0
|
105 | if (istagadd) plan.add.push(s)
|
106 | return !istagadd
|
107 | })
|
108 |
|
109 |
|
110 |
|
111 | plan.add.forEach(s => {
|
112 | if (!s.env) s.env = []
|
113 | s.env.push(`ZOMBIE_SWARM_FINGERPRINT=${s.fingerprint}`)
|
114 | delete s.fingerprint
|
115 | })
|
116 |
|
117 |
|
118 | return plan
|
119 | }
|