2014-10-17 15:36:22 -07:00
|
|
|
/*
|
|
|
|
Copyright 2014 CoreOS, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-07-30 13:56:36 -07:00
|
|
|
package configdrive
|
2014-05-22 13:48:26 -07:00
|
|
|
|
|
|
|
import (
|
2014-08-15 10:29:08 -07:00
|
|
|
"fmt"
|
2014-05-22 13:48:26 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2014-07-30 13:56:36 -07:00
|
|
|
const (
|
|
|
|
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 {
|
2014-08-11 17:57:10 -07:00
|
|
|
return &configDrive{root, 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-06-20 21:11:57 -07:00
|
|
|
func (cd *configDrive) FetchMetadata() ([]byte, error) {
|
2014-08-15 10:29:08 -07:00
|
|
|
return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "meta_data.json"))
|
2014-06-18 11:58:18 -07:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
func (cd *configDrive) FetchUserdata() ([]byte, error) {
|
2014-08-15 10:29:08 -07:00
|
|
|
return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data"))
|
2014-05-22 13:48:26 -07:00
|
|
|
}
|
|
|
|
|
2014-08-18 12:20:25 -07:00
|
|
|
func (cd *configDrive) FetchNetworkConfig(filename string) ([]byte, error) {
|
2014-09-13 21:02:08 -07:00
|
|
|
if filename == "" {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
2014-08-18 12:20:25 -07:00
|
|
|
return cd.tryReadFile(path.Join(cd.openstackRoot(), filename))
|
|
|
|
}
|
|
|
|
|
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-08-15 10:29:08 -07:00
|
|
|
func (cd *configDrive) openstackRoot() string {
|
|
|
|
return path.Join(cd.root, "openstack")
|
2014-07-19 20:18:25 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 10:29:08 -07:00
|
|
|
func (cd *configDrive) openstackVersionRoot() string {
|
|
|
|
return path.Join(cd.openstackRoot(), openstackApiVersion)
|
2014-07-19 20:18:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {
|
2014-08-15 10:29:08 -07:00
|
|
|
fmt.Printf("Attempting to read from %q\n", filename)
|
2014-07-19 20:18:25 -07:00
|
|
|
data, err := cd.readFile(filename)
|
2014-06-18 11:36:06 -07:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|