2017-02-22 20:06:39 +00:00
|
|
|
// Copyright 2016 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 gce
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2017-02-22 23:55:32 +00:00
|
|
|
"github.com/rancher/os/config/cloudinit/datasource"
|
|
|
|
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
2017-02-22 20:06:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
apiVersion = "computeMetadata/v1/"
|
|
|
|
metadataPath = apiVersion + "instance/"
|
|
|
|
userdataPath = apiVersion + "instance/attributes/user-data"
|
|
|
|
)
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
type MetadataService struct {
|
|
|
|
metadata.Service
|
2017-02-22 20:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
func NewDatasource(root string) *MetadataService {
|
|
|
|
return &MetadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, http.Header{"Metadata-Flavor": {"Google"}})}
|
2017-02-22 20:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
|
2017-02-22 20:06:39 +00:00
|
|
|
public, err := ms.fetchIP("network-interfaces/0/access-configs/0/external-ip")
|
|
|
|
if err != nil {
|
|
|
|
return datasource.Metadata{}, err
|
|
|
|
}
|
|
|
|
local, err := ms.fetchIP("network-interfaces/0/ip")
|
|
|
|
if err != nil {
|
|
|
|
return datasource.Metadata{}, err
|
|
|
|
}
|
|
|
|
hostname, err := ms.fetchString("hostname")
|
|
|
|
if err != nil {
|
|
|
|
return datasource.Metadata{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return datasource.Metadata{
|
|
|
|
PublicIPv4: public,
|
|
|
|
PrivateIPv4: local,
|
|
|
|
Hostname: hostname,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
func (ms MetadataService) Type() string {
|
2017-02-22 20:06:39 +00:00
|
|
|
return "gce-metadata-service"
|
|
|
|
}
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
func (ms MetadataService) fetchString(key string) (string, error) {
|
|
|
|
data, err := ms.FetchData(ms.MetadataURL() + key)
|
2017-02-22 20:06:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2017-02-23 01:29:01 +00:00
|
|
|
func (ms MetadataService) fetchIP(key string) (net.IP, error) {
|
2017-02-22 20:06:39 +00:00
|
|
|
str, err := ms.fetchString(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if str == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip := net.ParseIP(str); ip != nil {
|
|
|
|
return ip, nil
|
|
|
|
}
|
2017-02-23 01:29:01 +00:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q as IP address", str)
|
2017-02-22 20:06:39 +00:00
|
|
|
}
|