forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_drive_metadata_service.go
164 lines (126 loc) · 4.51 KB
/
config_drive_metadata_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
package infrastructure
import (
"encoding/json"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type configDriveMetadataService struct {
resolver DNSResolver
platform boshplatform.Platform
diskPaths []string
metaDataFilePath string
userDataFilePath string
// Loaded state
metaDataContents MetadataContentsType
userDataContents UserDataContentsType
logger boshlog.Logger
logTag string
}
func NewConfigDriveMetadataService(
resolver DNSResolver,
platform boshplatform.Platform,
diskPaths []string,
metaDataFilePath string,
userDataFilePath string,
logger boshlog.Logger,
) MetadataService {
return &configDriveMetadataService{
resolver: resolver,
platform: platform,
diskPaths: diskPaths,
metaDataFilePath: metaDataFilePath,
userDataFilePath: userDataFilePath,
logTag: "ConfigDriveMetadataService",
logger: logger,
}
}
func (ms *configDriveMetadataService) GetPublicKey() (string, error) {
if firstPublicKey, ok := ms.metaDataContents.PublicKeys["0"]; ok {
if openSSHKey, ok := firstPublicKey["openssh-key"]; ok {
return openSSHKey, nil
}
}
return "", bosherr.Error("Failed to load openssh-key from config drive metadata service")
}
func (ms *configDriveMetadataService) GetInstanceID() (string, error) {
if ms.metaDataContents.InstanceID == "" {
return "", bosherr.Error("Failed to load instance-id from config drive metadata service")
}
ms.logger.Debug(ms.logTag, "Getting instance id: %s", ms.metaDataContents.InstanceID)
return ms.metaDataContents.InstanceID, nil
}
func (ms *configDriveMetadataService) GetServerName() (string, error) {
if ms.userDataContents.Server.Name == "" {
return "", bosherr.Error("Failed to load server name from config drive metadata service")
}
ms.logger.Debug(ms.logTag, "Getting server name: %s", ms.userDataContents.Server.Name)
return ms.userDataContents.Server.Name, nil
}
func (ms *configDriveMetadataService) GetRegistryEndpoint() (string, error) {
if ms.userDataContents.Registry.Endpoint == "" {
return "", bosherr.Error("Failed to load registry endpoint from config drive metadata service")
}
endpoint := ms.userDataContents.Registry.Endpoint
nameServers := ms.userDataContents.DNS.Nameserver
if len(nameServers) == 0 {
ms.logger.Debug(ms.logTag, "Getting registry endpoint %s", endpoint)
return endpoint, nil
}
resolvedEndpoint, err := ms.resolver.LookupHost(nameServers, endpoint)
if err != nil {
return "", bosherr.WrapError(err, "Resolving registry endpoint")
}
ms.logger.Debug(ms.logTag, "Registry endpoint %s was resolved to %s", endpoint, resolvedEndpoint)
return resolvedEndpoint, nil
}
func (ms *configDriveMetadataService) GetNetworks() (boshsettings.Networks, error) {
return ms.userDataContents.Networks, nil
}
func (ms *configDriveMetadataService) GetSettings() (boshsettings.Settings, error) {
if ms.userDataContents.Settings.AgentID == "" {
return boshsettings.Settings{}, bosherr.Error("Metadata does not provide settings")
}
return ms.userDataContents.Settings, nil
}
func (ms *configDriveMetadataService) IsAvailable() bool {
if len(ms.diskPaths) == 0 {
ms.logger.Warn(ms.logTag, "Disk paths are not given")
return false
}
return ms.load() == nil
}
func (ms *configDriveMetadataService) load() error {
ms.logger.Debug(ms.logTag, "Loading config drive metadata service")
var err error
for _, diskPath := range ms.diskPaths {
err = ms.loadFromDiskPath(diskPath)
if err == nil {
ms.logger.Debug(ms.logTag, "Successfully loaded config from %s", diskPath)
return nil
}
ms.logger.Warn(ms.logTag, "Failed to load config from %s - %s", diskPath, err.Error())
}
return err
}
func (ms *configDriveMetadataService) loadFromDiskPath(diskPath string) error {
contentPaths := []string{ms.metaDataFilePath, ms.userDataFilePath}
contents, err := ms.platform.GetFilesContentsFromDisk(diskPath, contentPaths)
if err != nil {
return bosherr.WrapError(err, "Reading files on config drive")
}
var metadata MetadataContentsType
err = json.Unmarshal(contents[0], &metadata)
if err != nil {
return bosherr.WrapError(err, "Parsing config drive metadata from meta_data.json")
}
ms.metaDataContents = metadata
var userdata UserDataContentsType
err = json.Unmarshal(contents[1], &userdata)
if err != nil {
return bosherr.WrapError(err, "Parsing config drive metadata from user_data")
}
ms.userDataContents = userdata
return nil
}