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-08-16 05:14:34 +04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-01-23 04:35:39 +03:00
|
|
|
"net"
|
2014-08-16 05:14:34 +04:00
|
|
|
"strconv"
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
2014-08-16 05:14:34 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultAddress = "http://169.254.169.254/"
|
|
|
|
apiVersion = "metadata/v1"
|
|
|
|
userdataUrl = apiVersion + "/user-data"
|
|
|
|
metadataPath = apiVersion + ".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 {
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Interfaces Interfaces `json:"interfaces"`
|
|
|
|
PublicKeys []string `json:"public_keys"`
|
|
|
|
DNS DNS `json:"dns"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type metadataService struct {
|
|
|
|
metadata.MetadataService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasource(root string) *metadataService {
|
|
|
|
return &metadataService{MetadataService: metadata.NewDatasource(root, apiVersion, userdataUrl, metadataPath)}
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
|
|
|
|
var data []byte
|
|
|
|
var m Metadata
|
2014-08-16 05:14:34 +04:00
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
if data, err = ms.FetchData(ms.MetadataUrl()); err != nil || len(data) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
|
|
return
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +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)
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
2015-01-23 04:35:39 +03:00
|
|
|
if m.Interfaces.Public[0].IPv6 != nil {
|
|
|
|
metadata.PublicIPv6 = net.ParseIP(m.Interfaces.Public[0].IPv6.IPAddress)
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 04:35:39 +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)
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
2015-01-23 04:35:39 +03:00
|
|
|
if m.Interfaces.Private[0].IPv6 != nil {
|
|
|
|
metadata.PrivateIPv6 = net.ParseIP(m.Interfaces.Private[0].IPv6.IPAddress)
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 04:35:39 +03:00
|
|
|
metadata.Hostname = m.Hostname
|
|
|
|
metadata.SSHPublicKeys = map[string]string{}
|
|
|
|
for i, key := range m.PublicKeys {
|
|
|
|
metadata.SSHPublicKeys[strconv.Itoa(i)] = key
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
2015-01-23 22:31:57 +03:00
|
|
|
metadata.NetworkConfig = data
|
2014-08-16 05:14:34 +04:00
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
return
|
2014-08-16 05:14:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms metadataService) Type() string {
|
|
|
|
return "digitalocean-metadata-service"
|
|
|
|
}
|