2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 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-10-18 02:36:22 +04:00
|
|
|
|
2014-07-31 00:56:36 +04:00
|
|
|
package configdrive
|
2014-05-23 00:48:26 +04:00
|
|
|
|
|
|
|
import (
|
2015-01-23 04:35:39 +03:00
|
|
|
"encoding/json"
|
2014-08-15 21:29:08 +04:00
|
|
|
"fmt"
|
2014-05-23 00:48:26 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-01-23 04:35:39 +03:00
|
|
|
|
2015-03-28 20:08:30 +03:00
|
|
|
"github.com/vtolstov/cloudinit/datasource"
|
2014-05-23 00:48:26 +04:00
|
|
|
)
|
|
|
|
|
2014-07-31 00:56:36 +04:00
|
|
|
const (
|
|
|
|
openstackApiVersion = "latest"
|
|
|
|
)
|
|
|
|
|
2014-05-23 00:48:26 +04:00
|
|
|
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-07-31 00:56:36 +04:00
|
|
|
func NewDatasource(root string) *configDrive {
|
2014-08-12 04:57:10 +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
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
func (cd *configDrive) FetchMetadata() (metadata datasource.Metadata, err error) {
|
|
|
|
var data []byte
|
|
|
|
var m struct {
|
|
|
|
SSHAuthorizedKeyMap map[string]string `json:"public_keys"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
NetworkConfig struct {
|
|
|
|
ContentPath string `json:"content_path"`
|
|
|
|
} `json:"network_config"`
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:16:39 +03:00
|
|
|
if data, err = cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "meta_data.json")); err != nil || len(data) == 0 {
|
2015-01-23 04:35:39 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(data), &m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap
|
|
|
|
metadata.Hostname = m.Hostname
|
2015-02-14 09:28:09 +03:00
|
|
|
if m.NetworkConfig.ContentPath != "" {
|
|
|
|
metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
|
|
|
|
}
|
2015-01-23 04:35:39 +03:00
|
|
|
|
|
|
|
return
|
2014-06-18 22:58:18 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (cd *configDrive) FetchUserdata() ([]byte, error) {
|
2014-08-15 21:29:08 +04:00
|
|
|
return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data"))
|
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-08-15 21:29:08 +04:00
|
|
|
func (cd *configDrive) openstackRoot() string {
|
|
|
|
return path.Join(cd.root, "openstack")
|
2014-07-20 07:18:25 +04:00
|
|
|
}
|
|
|
|
|
2014-08-15 21:29:08 +04:00
|
|
|
func (cd *configDrive) openstackVersionRoot() string {
|
|
|
|
return path.Join(cd.openstackRoot(), openstackApiVersion)
|
2014-07-20 07:18:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {
|
2014-08-15 21:29:08 +04:00
|
|
|
fmt.Printf("Attempting to read from %q\n", filename)
|
2014-07-20 07:18:25 +04:00
|
|
|
data, err := cd.readFile(filename)
|
2014-06-18 22:36:06 +04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|