2015-03-26 10:23:27 +03: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-26 10:43:37 +03:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2015-03-26 10:23:27 +03:00
|
|
|
|
2015-11-09 11:24:52 +03:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
2015-03-26 10:23:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultAddress = "http://169.254.169.254/"
|
|
|
|
apiVersion = "openstack/latest"
|
|
|
|
userdataUrl = apiVersion + "/user_data"
|
|
|
|
metadataPath = apiVersion + "/meta_data.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Address struct {
|
|
|
|
IPAddress string `json:"ip_address"`
|
|
|
|
Netmask string `json:"netmask"`
|
|
|
|
Cidr int `json:"cidr"`
|
|
|
|
Gateway string `json:"gateway"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Interface struct {
|
|
|
|
IPv4 *Address `json:"ipv4"`
|
|
|
|
IPv6 *Address `json:"ipv6"`
|
|
|
|
MAC string `json:"mac"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Interfaces struct {
|
|
|
|
Public []Interface `json:"public"`
|
|
|
|
Private []Interface `json:"private"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DNS struct {
|
|
|
|
Nameservers []string `json:"nameservers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metadata struct {
|
2015-03-26 11:53:54 +03:00
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Interfaces Interfaces `json:"interfaces"`
|
|
|
|
PublicKeys map[string]string `json:"public_keys"`
|
|
|
|
DNS DNS `json:"dns"`
|
2015-03-26 10:23:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type metadataService struct {
|
|
|
|
metadata.MetadataService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasource(root string) *metadataService {
|
|
|
|
return &metadataService{MetadataService: metadata.NewDatasource(root, apiVersion, userdataUrl, metadataPath)}
|
|
|
|
}
|
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
|
|
|
|
var data []byte
|
|
|
|
var m Metadata
|
2015-03-26 10:23:27 +03:00
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
if data, err = ms.FetchData(ms.MetadataUrl()); err != nil || len(data) == 0 {
|
|
|
|
return
|
2015-03-26 10:23:27 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-26 10:23:27 +03:00
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
if len(m.Interfaces.Public) > 0 {
|
|
|
|
if m.Interfaces.Public[0].IPv4 != nil {
|
|
|
|
metadata.PublicIPv4 = net.ParseIP(m.Interfaces.Public[0].IPv4.IPAddress)
|
|
|
|
}
|
|
|
|
if m.Interfaces.Public[0].IPv6 != nil {
|
|
|
|
metadata.PublicIPv6 = net.ParseIP(m.Interfaces.Public[0].IPv6.IPAddress)
|
2015-03-26 10:23:27 +03:00
|
|
|
}
|
2015-03-26 10:43:37 +03:00
|
|
|
}
|
|
|
|
if len(m.Interfaces.Private) > 0 {
|
|
|
|
if m.Interfaces.Private[0].IPv4 != nil {
|
|
|
|
metadata.PrivateIPv4 = net.ParseIP(m.Interfaces.Private[0].IPv4.IPAddress)
|
|
|
|
}
|
|
|
|
if m.Interfaces.Private[0].IPv6 != nil {
|
|
|
|
metadata.PrivateIPv6 = net.ParseIP(m.Interfaces.Private[0].IPv6.IPAddress)
|
2015-03-26 10:23:27 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-30 12:05:49 +03:00
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
metadata.Hostname = m.Hostname
|
|
|
|
metadata.SSHPublicKeys = map[string]string{}
|
2015-03-26 11:53:54 +03:00
|
|
|
metadata.SSHPublicKeys[strconv.Itoa(0)] = m.PublicKeys["root"]
|
2015-03-26 10:43:37 +03:00
|
|
|
metadata.NetworkConfig = data
|
2015-03-26 10:23:27 +03:00
|
|
|
|
2015-03-26 10:43:37 +03:00
|
|
|
return
|
2015-03-26 10:23:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms metadataService) Type() string {
|
|
|
|
return "openstack-metadata-service"
|
|
|
|
}
|