This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from juju/juju
/
publish.go
76 lines (64 loc) · 1.66 KB
/
publish.go
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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package peergrouper
import (
"sync"
"github.com/juju/errors"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
)
type apiHostPortsSetter interface {
SetAPIHostPorts([][]network.HostPort) error
}
type publisher struct {
st apiHostPortsSetter
preferIPv6 bool
mu sync.Mutex
lastAPIServers [][]network.HostPort
}
func newPublisher(st apiHostPortsSetter, preferIPv6 bool) *publisher {
return &publisher{
st: st,
preferIPv6: preferIPv6,
}
}
func (pub *publisher) publishAPIServers(apiServers [][]network.HostPort, instanceIds []instance.Id) error {
if len(apiServers) == 0 {
return errors.Errorf("no api servers specified")
}
pub.mu.Lock()
defer pub.mu.Unlock()
sortedAPIServers := make([][]network.HostPort, len(apiServers))
for i, hostPorts := range apiServers {
sortedAPIServers[i] = append([]network.HostPort{}, hostPorts...)
network.SortHostPorts(sortedAPIServers[i], pub.preferIPv6)
}
if apiServersEqual(sortedAPIServers, pub.lastAPIServers) {
logger.Debugf("API host ports have not changed")
return nil
}
// TODO(rog) publish instanceIds in environment storage.
err := pub.st.SetAPIHostPorts(sortedAPIServers)
if err != nil {
return err
}
pub.lastAPIServers = sortedAPIServers
return nil
}
func apiServersEqual(a, b [][]network.HostPort) bool {
if len(a) != len(b) {
return false
}
for i, hostPortsA := range a {
hostPortsB := b[i]
if len(hostPortsA) != len(hostPortsB) {
return false
}
for j := range hostPortsA {
if hostPortsA[j] != hostPortsB[j] {
return false
}
}
}
return true
}