2015-02-23 19:00:24 +00:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
// Copyright 2015 Rancher Labs, 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.
|
|
|
|
|
2015-02-19 21:43:37 +00:00
|
|
|
package cloudinit
|
2015-02-19 03:05:23 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-23 11:36:28 +00:00
|
|
|
"errors"
|
2015-02-19 03:05:23 +00:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-20 03:05:17 +00:00
|
|
|
"strings"
|
2015-02-19 03:05:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-11-26 12:41:42 +00:00
|
|
|
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
2015-03-25 22:27:33 +00:00
|
|
|
|
2015-02-20 03:05:17 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-02-19 03:05:23 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/configdrive"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/file"
|
2015-03-29 09:37:31 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean"
|
2015-02-19 03:05:23 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata/ec2"
|
2015-12-20 05:37:57 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata/packet"
|
2015-02-19 03:05:23 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/proc_cmdline"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/url"
|
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2015-07-29 07:45:06 +00:00
|
|
|
"github.com/rancher/netconf"
|
2015-10-12 11:50:17 +00:00
|
|
|
rancherConfig "github.com/rancher/os/config"
|
2015-02-19 03:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
datasourceInterval = 100 * time.Millisecond
|
|
|
|
datasourceMaxInterval = 30 * time.Second
|
|
|
|
datasourceTimeout = 5 * time.Minute
|
2015-07-29 06:52:15 +00:00
|
|
|
sshKeyName = "rancheros-cloud-config"
|
2015-02-19 03:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-07-29 06:52:15 +00:00
|
|
|
save bool
|
|
|
|
execute bool
|
|
|
|
network bool
|
|
|
|
flags *flag.FlagSet
|
2015-02-19 03:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-02-20 16:18:55 +00:00
|
|
|
flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
2015-03-18 12:27:37 +00:00
|
|
|
flags.BoolVar(&network, "network", true, "use network based datasources")
|
2015-02-20 16:18:55 +00:00
|
|
|
flags.BoolVar(&save, "save", false, "save cloud config and exit")
|
2015-02-23 19:00:24 +00:00
|
|
|
flags.BoolVar(&execute, "execute", false, "execute saved cloud config")
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
func saveFiles(cloudConfigBytes, scriptBytes []byte, metadata datasource.Metadata) error {
|
2015-09-23 11:36:28 +00:00
|
|
|
os.MkdirAll(rancherConfig.CloudConfigDir, os.ModeDir|0600)
|
2015-07-29 06:52:15 +00:00
|
|
|
os.Remove(rancherConfig.CloudConfigScriptFile)
|
2015-09-23 11:36:28 +00:00
|
|
|
os.Remove(rancherConfig.CloudConfigBootFile)
|
2015-07-29 06:52:15 +00:00
|
|
|
os.Remove(rancherConfig.MetaDataFile)
|
2015-04-16 06:03:27 +00:00
|
|
|
|
|
|
|
if len(scriptBytes) > 0 {
|
2015-07-29 06:52:15 +00:00
|
|
|
log.Infof("Writing to %s", rancherConfig.CloudConfigScriptFile)
|
|
|
|
if err := ioutil.WriteFile(rancherConfig.CloudConfigScriptFile, scriptBytes, 500); err != nil {
|
|
|
|
log.Errorf("Error while writing file %s: %v", rancherConfig.CloudConfigScriptFile, err)
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
if err := ioutil.WriteFile(rancherConfig.CloudConfigBootFile, cloudConfigBytes, 400); err != nil {
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Infof("Written to %s:\n%s", rancherConfig.CloudConfigBootFile, string(cloudConfigBytes))
|
2015-03-18 12:27:37 +00:00
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
metaDataBytes, err := yaml.Marshal(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
if err = ioutil.WriteFile(rancherConfig.MetaDataFile, metaDataBytes, 400); err != nil {
|
2015-04-16 06:03:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-29 06:52:15 +00:00
|
|
|
log.Infof("Written to %s:\n%s", rancherConfig.MetaDataFile, string(metaDataBytes))
|
2015-04-16 06:03:27 +00:00
|
|
|
|
2015-03-18 12:27:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
func currentDatasource() (datasource.Datasource, error) {
|
2015-02-20 03:05:17 +00:00
|
|
|
cfg, err := rancherConfig.LoadConfig()
|
|
|
|
if err != nil {
|
2015-08-20 14:42:33 +00:00
|
|
|
log.WithFields(log.Fields{"err": err}).Error("Failed to read rancher config")
|
|
|
|
return nil, err
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 03:05:17 +00:00
|
|
|
dss := getDatasources(cfg)
|
2015-02-19 03:05:23 +00:00
|
|
|
if len(dss) == 0 {
|
2015-04-16 06:03:27 +00:00
|
|
|
return nil, nil
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ds := selectDatasource(dss)
|
2015-04-16 06:03:27 +00:00
|
|
|
return ds, nil
|
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
func saveCloudConfig() error {
|
2015-09-23 11:36:28 +00:00
|
|
|
userDataBytes, metadata, err := fetchUserData()
|
2015-04-16 06:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userData := string(userDataBytes)
|
|
|
|
scriptBytes := []byte{}
|
|
|
|
|
|
|
|
if config.IsScript(userData) {
|
|
|
|
scriptBytes = userDataBytes
|
|
|
|
userDataBytes = []byte{}
|
|
|
|
} else if isCompose(userData) {
|
2015-09-23 11:36:28 +00:00
|
|
|
if userDataBytes, err = composeToCloudConfig(userDataBytes); err != nil {
|
|
|
|
log.Errorf("Failed to convert compose to cloud-config syntax: %v", err)
|
2015-04-16 06:03:27 +00:00
|
|
|
return err
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
} else if config.IsCloudConfig(userData) {
|
2015-09-23 11:36:28 +00:00
|
|
|
if _, err := rancherConfig.ReadConfig(userDataBytes, false); err != nil {
|
|
|
|
log.WithFields(log.Fields{"cloud-config": userData, "err": err}).Warn("Failed to parse cloud-config, not saving.")
|
2015-08-20 14:20:51 +00:00
|
|
|
userDataBytes = []byte{}
|
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
} else {
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Errorf("Unrecognized user-data\n%s", userData)
|
2015-04-16 06:03:27 +00:00
|
|
|
userDataBytes = []byte{}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
if _, err := rancherConfig.ReadConfig(userDataBytes, false); err != nil {
|
|
|
|
log.WithFields(log.Fields{"cloud-config": userData, "err": err}).Warn("Failed to parse cloud-config")
|
|
|
|
return errors.New("Failed to parse cloud-config")
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return saveFiles(userDataBytes, scriptBytes, metadata)
|
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
func fetchUserData() ([]byte, datasource.Metadata, error) {
|
|
|
|
var metadata datasource.Metadata
|
|
|
|
ds, err := currentDatasource()
|
|
|
|
if err != nil || ds == nil {
|
|
|
|
log.Errorf("Failed to select datasource: %v", err)
|
|
|
|
return nil, metadata, err
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Infof("Fetching user-data from datasource %v", ds.Type())
|
|
|
|
userDataBytes, err := ds.FetchUserdata()
|
2015-04-16 06:03:27 +00:00
|
|
|
if err != nil {
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Errorf("Failed fetching user-data from datasource: %v", err)
|
|
|
|
return nil, metadata, err
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Infof("Fetching meta-data from datasource of type %v", ds.Type())
|
|
|
|
metadata, err = ds.FetchMetadata()
|
2015-04-16 06:03:27 +00:00
|
|
|
if err != nil {
|
2015-09-23 11:36:28 +00:00
|
|
|
log.Errorf("Failed fetching meta-data from datasource: %v", err)
|
|
|
|
return nil, metadata, err
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
2015-09-23 11:36:28 +00:00
|
|
|
return userDataBytes, metadata, nil
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func executeCloudConfig() error {
|
2015-09-23 11:36:28 +00:00
|
|
|
cc, err := rancherConfig.LoadConfig()
|
2015-04-16 06:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:00:24 +00:00
|
|
|
if len(cc.SSHAuthorizedKeys) > 0 {
|
2015-03-18 12:27:37 +00:00
|
|
|
authorizeSSHKeys("rancher", cc.SSHAuthorizedKeys, sshKeyName)
|
2015-06-12 06:31:53 +00:00
|
|
|
authorizeSSHKeys("docker", cc.SSHAuthorizedKeys, sshKeyName)
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-02-22 00:07:29 +00:00
|
|
|
for _, file := range cc.WriteFiles {
|
|
|
|
f := system.File{File: file}
|
2015-04-09 15:01:09 +00:00
|
|
|
fullPath, err := system.WriteFile(&f, "/")
|
2015-02-22 00:07:29 +00:00
|
|
|
if err != nil {
|
2015-08-20 14:42:33 +00:00
|
|
|
log.WithFields(log.Fields{"err": err, "path": fullPath}).Error("Error writing file")
|
|
|
|
continue
|
2015-02-22 00:07:29 +00:00
|
|
|
}
|
|
|
|
log.Printf("Wrote file %s to filesystem", fullPath)
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Main() {
|
2015-09-23 11:36:28 +00:00
|
|
|
flags.Parse(os.Args[1:])
|
2015-04-16 06:03:27 +00:00
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
log.Infof("Running cloud-init: save=%v, execute=%v", save, execute)
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
if save {
|
|
|
|
err := saveCloudConfig()
|
|
|
|
if err != nil {
|
2015-08-20 14:20:51 +00:00
|
|
|
log.WithFields(log.Fields{"err": err}).Error("Failed to save cloud-config")
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if execute {
|
|
|
|
err := executeCloudConfig()
|
|
|
|
if err != nil {
|
2015-08-20 14:20:51 +00:00
|
|
|
log.WithFields(log.Fields{"err": err}).Error("Failed to execute cloud-config")
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDatasources creates a slice of possible Datasources for cloudinit based
|
|
|
|
// on the different source command-line flags.
|
2015-07-29 06:52:15 +00:00
|
|
|
func getDatasources(cfg *rancherConfig.CloudConfig) []datasource.Datasource {
|
2015-02-19 03:05:23 +00:00
|
|
|
dss := make([]datasource.Datasource, 0, 5)
|
2015-02-20 03:05:17 +00:00
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
for _, ds := range cfg.Rancher.CloudInit.Datasources {
|
2015-02-20 16:18:55 +00:00
|
|
|
parts := strings.SplitN(ds, ":", 2)
|
2015-02-20 03:05:17 +00:00
|
|
|
|
|
|
|
switch parts[0] {
|
|
|
|
case "ec2":
|
2015-03-18 12:27:37 +00:00
|
|
|
if network {
|
|
|
|
if len(parts) == 1 {
|
|
|
|
dss = append(dss, ec2.NewDatasource(ec2.DefaultAddress))
|
|
|
|
} else {
|
|
|
|
dss = append(dss, ec2.NewDatasource(parts[1]))
|
|
|
|
}
|
2015-02-20 03:05:17 +00:00
|
|
|
}
|
|
|
|
case "file":
|
|
|
|
if len(parts) == 2 {
|
|
|
|
dss = append(dss, file.NewDatasource(parts[1]))
|
|
|
|
}
|
|
|
|
case "url":
|
2015-05-14 05:51:49 +00:00
|
|
|
if network {
|
|
|
|
if len(parts) == 2 {
|
|
|
|
dss = append(dss, url.NewDatasource(parts[1]))
|
|
|
|
}
|
2015-02-20 03:05:17 +00:00
|
|
|
}
|
|
|
|
case "cmdline":
|
2015-05-14 05:51:49 +00:00
|
|
|
if network {
|
|
|
|
if len(parts) == 1 {
|
|
|
|
dss = append(dss, proc_cmdline.NewDatasource())
|
|
|
|
}
|
2015-02-20 03:05:17 +00:00
|
|
|
}
|
|
|
|
case "configdrive":
|
|
|
|
if len(parts) == 2 {
|
|
|
|
dss = append(dss, configdrive.NewDatasource(parts[1]))
|
|
|
|
}
|
2015-03-29 09:37:31 +00:00
|
|
|
case "digitalocean":
|
|
|
|
if network {
|
|
|
|
if len(parts) == 1 {
|
|
|
|
dss = append(dss, digitalocean.NewDatasource(digitalocean.DefaultAddress))
|
|
|
|
} else {
|
|
|
|
dss = append(dss, digitalocean.NewDatasource(parts[1]))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
enableDoLinkLocal()
|
|
|
|
}
|
2015-04-03 22:01:19 +00:00
|
|
|
case "gce":
|
|
|
|
if network {
|
|
|
|
gceCloudConfigFile, err := GetAndCreateGceDataSourceFilename()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not retrieve GCE CloudConfig %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dss = append(dss, file.NewDatasource(gceCloudConfigFile))
|
|
|
|
}
|
2015-12-20 05:37:57 +00:00
|
|
|
case "packet":
|
|
|
|
if !network {
|
|
|
|
enablePacketNetwork(&cfg.Rancher)
|
|
|
|
}
|
|
|
|
dss = append(dss, packet.NewDatasource("https://metadata.packet.net/"))
|
2015-02-20 03:05:17 +00:00
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-02-20 03:05:17 +00:00
|
|
|
|
2015-02-19 03:05:23 +00:00
|
|
|
return dss
|
|
|
|
}
|
|
|
|
|
2015-03-29 09:37:31 +00:00
|
|
|
func enableDoLinkLocal() {
|
2015-07-29 07:45:06 +00:00
|
|
|
err := netconf.ApplyNetworkConfigs(&netconf.NetworkConfig{
|
|
|
|
Interfaces: map[string]netconf.InterfaceConfig{
|
2015-03-29 09:37:31 +00:00
|
|
|
"eth0": {
|
|
|
|
IPV4LL: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to apply link local on eth0: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 03:05:23 +00:00
|
|
|
// selectDatasource attempts to choose a valid Datasource to use based on its
|
|
|
|
// current availability. The first Datasource to report to be available is
|
|
|
|
// returned. Datasources will be retried if possible if they are not
|
|
|
|
// immediately available. If all Datasources are permanently unavailable or
|
|
|
|
// datasourceTimeout is reached before one becomes available, nil is returned.
|
|
|
|
func selectDatasource(sources []datasource.Datasource) datasource.Datasource {
|
|
|
|
ds := make(chan datasource.Datasource)
|
|
|
|
stop := make(chan struct{})
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for _, s := range sources {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(s datasource.Datasource) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
duration := datasourceInterval
|
|
|
|
for {
|
2015-02-23 19:00:24 +00:00
|
|
|
log.Infof("Checking availability of %q\n", s.Type())
|
2015-02-19 03:05:23 +00:00
|
|
|
if s.IsAvailable() {
|
|
|
|
ds <- s
|
|
|
|
return
|
|
|
|
} else if !s.AvailabilityChanges() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case <-time.After(duration):
|
|
|
|
duration = pkg.ExpBackoff(duration, datasourceMaxInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var s datasource.Datasource
|
|
|
|
select {
|
|
|
|
case s = <-ds:
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(datasourceTimeout):
|
|
|
|
}
|
|
|
|
|
|
|
|
close(stop)
|
|
|
|
return s
|
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
|
|
|
|
func isCompose(content string) bool {
|
|
|
|
return strings.HasPrefix(content, "#compose\n")
|
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
func composeToCloudConfig(bytes []byte) ([]byte, error) {
|
2015-04-16 06:03:27 +00:00
|
|
|
compose := make(map[interface{}]interface{})
|
2015-04-16 05:57:59 +00:00
|
|
|
err := yaml.Unmarshal(bytes, &compose)
|
2015-04-16 06:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:33 +00:00
|
|
|
return yaml.Marshal(map[interface{}]interface{}{
|
|
|
|
"rancher": map[interface{}]interface{}{
|
|
|
|
"services": compose,
|
|
|
|
},
|
|
|
|
})
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|