feat(metadata): Distinguish between userdata and metadata for datasources

This commit is contained in:
Alex Crawford 2014-06-18 11:58:18 -07:00
parent 29ed6b38bd
commit 840c208b60
9 changed files with 55 additions and 11 deletions

View File

@ -79,7 +79,7 @@ func main() {
} }
fmt.Printf("Fetching user-data from datasource of type %q\n", ds.Type()) fmt.Printf("Fetching user-data from datasource of type %q\n", ds.Type())
userdataBytes, err := ds.Fetch() userdataBytes, err := ds.FetchUserdata()
if err != nil { if err != nil {
fmt.Printf("Failed fetching user-data from datasource: %v\n", err) fmt.Printf("Failed fetching user-data from datasource: %v\n", err)
if ignoreFailure { if ignoreFailure {

View File

@ -18,7 +18,11 @@ func (self *configDrive) ConfigRoot() string {
return self.root return self.root
} }
func (self *configDrive) Fetch() ([]byte, error) { func (self *configDrive) FetchMetadata() ([]byte, error) {
return self.readFile("meta_data.json")
}
func (self *configDrive) FetchUserdata() ([]byte, error) {
return self.readFile("user_data") return self.readFile("user_data")
} }

View File

@ -2,6 +2,7 @@ package datasource
type Datasource interface { type Datasource interface {
ConfigRoot() string ConfigRoot() string
Fetch() ([]byte, error) FetchMetadata() ([]byte, error)
FetchUserdata() ([]byte, error)
Type() string Type() string
} }

View File

@ -16,7 +16,11 @@ func (self *localFile) ConfigRoot() string {
return "" return ""
} }
func (self *localFile) Fetch() ([]byte, error) { func (self *localFile) FetchMetadata() ([]byte, error) {
return []byte{}, nil
}
func (self *localFile) FetchUserdata() ([]byte, error) {
return ioutil.ReadFile(self.path) return ioutil.ReadFile(self.path)
} }

View File

@ -14,7 +14,11 @@ func (self *metadataService) ConfigRoot() string {
return "" return ""
} }
func (ms *metadataService) Fetch() ([]byte, error) { func (self *metadataService) FetchMetadata() ([]byte, error) {
return []byte{}, nil
}
func (ms *metadataService) FetchUserdata() ([]byte, error) {
client := pkg.NewHttpClient() client := pkg.NewHttpClient()
return client.Get(ms.url) return client.Get(ms.url)
} }

View File

@ -26,7 +26,11 @@ func (self *procCmdline) ConfigRoot() string {
return "" return ""
} }
func (self *procCmdline) Fetch() ([]byte, error) { func (self *procCmdline) FetchMetadata() ([]byte, error) {
return []byte{}, nil
}
func (self *procCmdline) FetchUserdata() ([]byte, error) {
contents, err := ioutil.ReadFile(self.Location) contents, err := ioutil.ReadFile(self.Location)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -77,7 +77,7 @@ func TestProcCmdlineAndFetchConfig(t *testing.T) {
p := NewProcCmdline() p := NewProcCmdline()
p.Location = file.Name() p.Location = file.Name()
cfg, err := p.Fetch() cfg, err := p.FetchUserdata()
if err != nil { if err != nil {
t.Errorf("Test produced error: %v", err) t.Errorf("Test produced error: %v", err)
} }

View File

@ -34,10 +34,11 @@ type CloudConfig struct {
Update UpdateConfig Update UpdateConfig
Units []system.Unit Units []system.Unit
} }
WriteFiles []system.File `yaml:"write_files"` WriteFiles []system.File `yaml:"write_files"`
Hostname string Hostname string
Users []system.User Users []system.User
ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"` ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"`
NetworkConfigPath string
} }
type warner func(format string, v ...interface{}) type warner func(format string, v ...interface{})

26
initialize/meta_data.go Normal file
View File

@ -0,0 +1,26 @@
package initialize
import (
"encoding/json"
)
func ParseMetaData(contents string) (cfg CloudConfig, err error) {
var metadata struct {
SSHAuthorizedKeyMap map[string]string `json:"public_keys"`
Hostname string `json:"hostname"`
NetworkConfig struct {
ContentPath string `json:"content_path"`
} `json:"network_config"`
}
if err = json.Unmarshal([]byte(contents), &metadata); err != nil {
return
}
cfg.SSHAuthorizedKeys = make([]string, 0, len(metadata.SSHAuthorizedKeyMap))
for _, key := range metadata.SSHAuthorizedKeyMap {
cfg.SSHAuthorizedKeys = append(cfg.SSHAuthorizedKeys, key)
}
cfg.Hostname = metadata.Hostname
cfg.NetworkConfigPath = metadata.NetworkConfig.ContentPath
return
}