cloudinit/datasource/configdrive/configdrive.go

70 lines
1.6 KiB
Go
Raw Normal View History

package configdrive
import (
"io/ioutil"
"os"
"path"
)
const (
ec2ApiVersion = "2009-04-04"
openstackApiVersion = "latest"
)
type configDrive struct {
root string
readFile func(filename string) ([]byte, error)
}
func NewDatasource(root string) *configDrive {
2014-08-12 04:57:10 +04:00
return &configDrive{root, ioutil.ReadFile}
}
func (cd *configDrive) IsAvailable() bool {
_, err := os.Stat(cd.root)
return !os.IsNotExist(err)
}
func (cd *configDrive) AvailabilityChanges() bool {
return true
}
func (cd *configDrive) ConfigRoot() string {
return cd.openstackRoot()
}
2014-08-07 08:31:43 +04:00
// FetchMetadata attempts to retrieve metadata from ec2/2009-04-04/meta-data.json.
func (cd *configDrive) FetchMetadata() ([]byte, error) {
2014-08-07 08:31:43 +04:00
return cd.tryReadFile(path.Join(cd.ec2Root(), "meta-data.json"))
}
2014-08-07 08:31:43 +04:00
// FetchUserdata attempts to retrieve the userdata from ec2/2009-04-04/user-data.
// If no data is found, it will attempt to read from openstack/latest/user_data.
func (cd *configDrive) FetchUserdata() ([]byte, error) {
2014-08-07 08:31:43 +04:00
bytes, err := cd.tryReadFile(path.Join(cd.ec2Root(), "user-data"))
if bytes == nil && err == nil {
bytes, err = cd.tryReadFile(path.Join(cd.openstackRoot(), "user_data"))
}
return bytes, err
}
func (cd *configDrive) Type() string {
return "cloud-drive"
}
func (cd *configDrive) ec2Root() string {
return path.Join(cd.root, "ec2", ec2ApiVersion)
}
func (cd *configDrive) openstackRoot() string {
return path.Join(cd.root, "openstack", openstackApiVersion)
}
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {
data, err := cd.readFile(filename)
if os.IsNotExist(err) {
err = nil
}
return data, err
}