forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
166 lines (131 loc) · 4.58 KB
/
service.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
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
package settings
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"sync"
)
type Service interface {
LoadSettings() error
// GetSettings does not return error because without settings Agent cannot start.
GetSettings() Settings
PublicSSHKeyForUsername(string) (string, error)
InvalidateSettings() error
}
const settingsServiceLogTag = "settingsService"
type settingsService struct {
fs boshsys.FileSystem
settingsPath string
settings Settings
settingsMutex sync.Mutex
settingsSource Source
defaultNetworkResolver DefaultNetworkResolver
logger boshlog.Logger
}
type DefaultNetworkResolver interface {
// Ideally we would find a network based on a MAC address
// but current CPI implementations do not include it
GetDefaultNetwork() (Network, error)
}
func NewService(
fs boshsys.FileSystem,
settingsPath string,
settingsSource Source,
defaultNetworkResolver DefaultNetworkResolver,
logger boshlog.Logger,
) (service Service) {
return &settingsService{
fs: fs,
settingsPath: settingsPath,
settings: Settings{},
settingsSource: settingsSource,
defaultNetworkResolver: defaultNetworkResolver,
logger: logger,
}
}
func (s *settingsService) PublicSSHKeyForUsername(username string) (string, error) {
return s.settingsSource.PublicSSHKeyForUsername(username)
}
func (s *settingsService) LoadSettings() error {
s.logger.Debug(settingsServiceLogTag, "Loading settings from fetcher")
newSettings, fetchErr := s.settingsSource.Settings()
if fetchErr != nil {
s.logger.Error(settingsServiceLogTag, "Failed loading settings via fetcher: %v", fetchErr)
existingSettingsJSON, readError := s.fs.ReadFile(s.settingsPath)
if readError != nil {
s.logger.Error(settingsServiceLogTag, "Failed reading settings from file %s", readError.Error())
return bosherr.WrapError(fetchErr, "Invoking settings fetcher")
}
s.logger.Debug(settingsServiceLogTag, "Successfully read settings from file")
cachedSettings := Settings{}
err := json.Unmarshal(existingSettingsJSON, &cachedSettings)
if err != nil {
s.logger.Error(settingsServiceLogTag, "Failed unmarshalling settings from file %s", err.Error())
return bosherr.WrapError(fetchErr, "Invoking settings fetcher")
}
s.settingsMutex.Lock()
s.settings = cachedSettings
s.settingsMutex.Unlock()
return nil
}
s.logger.Debug(settingsServiceLogTag, "Successfully received settings from fetcher")
s.settingsMutex.Lock()
s.settings = newSettings
s.settingsMutex.Unlock()
newSettingsJSON, err := json.Marshal(newSettings)
if err != nil {
return bosherr.WrapError(err, "Marshalling settings json")
}
err = s.fs.WriteFile(s.settingsPath, newSettingsJSON)
if err != nil {
return bosherr.WrapError(err, "Writing setting json")
}
return nil
}
// GetSettings returns setting even if it fails to resolve IPs for dynamic networks.
func (s *settingsService) GetSettings() Settings {
s.settingsMutex.Lock()
settingsCopy := s.settings
if s.settings.Networks != nil {
settingsCopy.Networks = make(map[string]Network)
}
for networkName, network := range s.settings.Networks {
settingsCopy.Networks[networkName] = network
}
s.settingsMutex.Unlock()
for networkName, network := range settingsCopy.Networks {
if !network.IsDHCP() {
continue
}
resolvedNetwork, err := s.resolveNetwork(network)
if err != nil {
break
}
settingsCopy.Networks[networkName] = resolvedNetwork
}
return settingsCopy
}
func (s *settingsService) InvalidateSettings() error {
err := s.fs.RemoveAll(s.settingsPath)
if err != nil {
return bosherr.WrapError(err, "Removing settings file")
}
return nil
}
func (s *settingsService) resolveNetwork(network Network) (Network, error) {
// Ideally this would be GetNetworkByMACAddress(mac string)
// Currently, we are relying that if the default network does not contain
// the MAC adddress the InterfaceConfigurationCreator will fail.
resolvedNetwork, err := s.defaultNetworkResolver.GetDefaultNetwork()
if err != nil {
s.logger.Error(settingsServiceLogTag, "Failed retrieving default network %s", err.Error())
return Network{}, bosherr.WrapError(err, "Failed retrieving default network")
}
// resolvedNetwork does not have all information for a network
network.IP = resolvedNetwork.IP
network.Netmask = resolvedNetwork.Netmask
network.Gateway = resolvedNetwork.Gateway
network.Resolved = true
return network, nil
}