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