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-10-10 22:27:27 +04:00
|
|
|
package waagent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-01-23 04:35:39 +03:00
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
2014-10-10 22:27:27 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type waagent struct {
|
|
|
|
root string
|
|
|
|
readFile func(filename string) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasource(root string) *waagent {
|
|
|
|
return &waagent{root, ioutil.ReadFile}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) IsAvailable() bool {
|
|
|
|
_, err := os.Stat(path.Join(a.root, "provisioned"))
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) AvailabilityChanges() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) ConfigRoot() string {
|
|
|
|
return a.root
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
func (a *waagent) FetchMetadata() (metadata datasource.Metadata, err error) {
|
|
|
|
var metadataBytes []byte
|
|
|
|
if metadataBytes, err = a.tryReadFile(path.Join(a.root, "SharedConfig.xml")); err != nil {
|
|
|
|
return
|
2014-10-10 22:27:27 +04:00
|
|
|
}
|
|
|
|
if len(metadataBytes) == 0 {
|
2015-01-23 04:35:39 +03:00
|
|
|
return
|
2014-10-10 22:27:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Instance struct {
|
|
|
|
Id string `xml:"id,attr"`
|
|
|
|
Address string `xml:"address,attr"`
|
|
|
|
InputEndpoints struct {
|
|
|
|
Endpoints []struct {
|
|
|
|
LoadBalancedPublicAddress string `xml:"loadBalancedPublicAddress,attr"`
|
|
|
|
} `xml:"Endpoint"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SharedConfig struct {
|
|
|
|
Incarnation struct {
|
|
|
|
Instance string `xml:"instance,attr"`
|
|
|
|
}
|
|
|
|
Instances struct {
|
|
|
|
Instances []Instance `xml:"Instance"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
var m SharedConfig
|
|
|
|
if err = xml.Unmarshal(metadataBytes, &m); err != nil {
|
|
|
|
return
|
2014-10-10 22:27:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var instance Instance
|
2015-01-23 04:35:39 +03:00
|
|
|
for _, i := range m.Instances.Instances {
|
|
|
|
if i.Id == m.Incarnation.Instance {
|
2014-10-10 22:27:27 +04:00
|
|
|
instance = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:35:39 +03:00
|
|
|
metadata.PrivateIPv4 = net.ParseIP(instance.Address)
|
2014-10-10 22:27:27 +04:00
|
|
|
for _, e := range instance.InputEndpoints.Endpoints {
|
|
|
|
host, _, err := net.SplitHostPort(e.LoadBalancedPublicAddress)
|
|
|
|
if err == nil {
|
2015-01-23 04:35:39 +03:00
|
|
|
metadata.PublicIPv4 = net.ParseIP(host)
|
2014-10-10 22:27:27 +04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 04:35:39 +03:00
|
|
|
return
|
2014-10-10 22:27:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) FetchUserdata() ([]byte, error) {
|
|
|
|
return a.tryReadFile(path.Join(a.root, "CustomData"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) Type() string {
|
|
|
|
return "waagent"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *waagent) tryReadFile(filename string) ([]byte, error) {
|
|
|
|
fmt.Printf("Attempting to read from %q\n", filename)
|
|
|
|
data, err := a.readFile(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|