2014-05-23 00:48:26 +04:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configDrive struct {
|
2014-07-20 07:18:25 +04:00
|
|
|
root string
|
|
|
|
readFile func(filename string) ([]byte, error)
|
2014-05-23 00:48:26 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 22:36:06 +04:00
|
|
|
func NewConfigDrive(root string) *configDrive {
|
2014-07-20 07:18:25 +04:00
|
|
|
return &configDrive{root, ioutil.ReadFile}
|
2014-06-18 22:36:06 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 02:17:53 +04:00
|
|
|
func (cd *configDrive) IsAvailable() bool {
|
|
|
|
_, err := os.Stat(cd.root)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configDrive) AvailabilityChanges() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (cd *configDrive) ConfigRoot() string {
|
2014-07-20 07:18:25 +04:00
|
|
|
return cd.openstackRoot()
|
2014-05-23 00:48:26 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 07:18:25 +04:00
|
|
|
// FetchMetadata attempts to retrieve metadata from ec2/2009-04-04/meta_data.json.
|
2014-06-21 08:11:57 +04:00
|
|
|
func (cd *configDrive) FetchMetadata() ([]byte, error) {
|
2014-07-20 07:18:25 +04:00
|
|
|
return cd.tryReadFile(path.Join(cd.ec2Root(), "meta_data.json"))
|
2014-06-18 22:58:18 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 07:18:25 +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.
|
2014-06-21 08:11:57 +04:00
|
|
|
func (cd *configDrive) FetchUserdata() ([]byte, error) {
|
2014-07-20 07:18:25 +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
|
2014-05-23 00:48:26 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (cd *configDrive) Type() string {
|
2014-05-23 00:48:26 +04:00
|
|
|
return "cloud-drive"
|
|
|
|
}
|
2014-06-18 22:36:06 +04:00
|
|
|
|
2014-07-20 07:18:25 +04:00
|
|
|
func (cd *configDrive) ec2Root() string {
|
|
|
|
return path.Join(cd.root, "ec2", Ec2ApiVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configDrive) openstackRoot() string {
|
|
|
|
return path.Join(cd.root, "openstack", "latest")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {
|
|
|
|
data, err := cd.readFile(filename)
|
2014-06-18 22:36:06 +04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|