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 (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-20 16:18:55 +00:00
|
|
|
"path"
|
2015-02-20 03:05:17 +00:00
|
|
|
"strings"
|
2015-02-19 03:05:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-03-25 22:27:33 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
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"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/proc_cmdline"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/url"
|
2015-05-24 00:39:54 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/initialize"
|
2015-02-19 03:05:23 +00:00
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2015-03-25 22:27:33 +00:00
|
|
|
"github.com/rancherio/os/cmd/cloudinit/hostname"
|
2015-03-29 09:37:31 +00:00
|
|
|
rancherNetwork "github.com/rancherio/os/cmd/network"
|
2015-02-20 03:05:17 +00:00
|
|
|
rancherConfig "github.com/rancherio/os/config"
|
2015-04-16 06:03:27 +00:00
|
|
|
"github.com/rancherio/os/util"
|
2015-02-19 03:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
datasourceInterval = 100 * time.Millisecond
|
|
|
|
datasourceMaxInterval = 30 * time.Second
|
|
|
|
datasourceTimeout = 5 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-04-16 06:03:27 +00:00
|
|
|
baseConfigDir string
|
|
|
|
outputDir string
|
|
|
|
outputFile string
|
|
|
|
metaDataFile string
|
|
|
|
scriptFile string
|
|
|
|
rancherYml string
|
|
|
|
save bool
|
|
|
|
execute bool
|
|
|
|
network bool
|
|
|
|
sshKeyName string
|
|
|
|
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-04-16 06:03:27 +00:00
|
|
|
flags.StringVar(&baseConfigDir, "base-config-dir", "/var/lib/rancher/conf/cloud-config.d", "base cloud config")
|
2015-02-20 16:18:55 +00:00
|
|
|
flags.StringVar(&outputDir, "dir", "/var/lib/rancher/conf", "working directory")
|
2015-03-18 12:27:37 +00:00
|
|
|
flags.StringVar(&outputFile, "file", "cloud-config-processed.yml", "output cloud config file name")
|
2015-04-16 06:03:27 +00:00
|
|
|
flags.StringVar(&metaDataFile, "metadata", "metadata", "output metdata file name")
|
2015-03-18 12:27:37 +00:00
|
|
|
flags.StringVar(&scriptFile, "script-file", "cloud-config-script", "output cloud config script file name")
|
|
|
|
flags.StringVar(&rancherYml, "rancher", "cloud-config-rancher.yml", "output cloud config rancher file name")
|
2015-02-20 16:18:55 +00:00
|
|
|
flags.StringVar(&sshKeyName, "ssh-key-name", "rancheros-cloud-config", "SSH key name")
|
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 {
|
|
|
|
scriptOutput := path.Join(outputDir, scriptFile)
|
|
|
|
cloudConfigOutput := path.Join(outputDir, outputFile)
|
|
|
|
rancherYmlOutput := path.Join(outputDir, rancherYml)
|
|
|
|
metaDataOutput := path.Join(outputDir, metaDataFile)
|
|
|
|
|
|
|
|
os.Remove(scriptOutput)
|
|
|
|
os.Remove(cloudConfigOutput)
|
|
|
|
os.Remove(rancherYmlOutput)
|
|
|
|
os.Remove(metaDataOutput)
|
|
|
|
|
|
|
|
if len(scriptBytes) > 0 {
|
|
|
|
log.Infof("Writing to %s", scriptOutput)
|
|
|
|
if err := ioutil.WriteFile(scriptOutput, scriptBytes, 500); err != nil {
|
|
|
|
log.Errorf("Error while writing file %s: %v", scriptOutput, err)
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
cloudConfigBytes = append([]byte("#cloud-config\n"), cloudConfigBytes...)
|
|
|
|
log.Infof("Writing to %s", cloudConfigOutput)
|
|
|
|
if err := ioutil.WriteFile(cloudConfigOutput, cloudConfigBytes, 500); err != nil {
|
|
|
|
log.Errorf("Error while writing file %s: %v", cloudConfigOutput, err)
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
ccData := make(map[string]interface{})
|
|
|
|
if err := yaml.Unmarshal(cloudConfigBytes, ccData); err != nil {
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
if rancher, ok := ccData["rancher"]; ok {
|
|
|
|
bytes, err := yaml.Marshal(rancher)
|
|
|
|
if err != nil {
|
2015-03-18 12:27:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
if err = ioutil.WriteFile(rancherYmlOutput, bytes, 400); err != nil {
|
|
|
|
return err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(metaDataOutput, metaDataBytes, 400); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
log.Fatalf("Failed to read rancher config %v", 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-17 13:18:33 +00:00
|
|
|
func mergeBaseConfig(current, currentScript []byte) ([]byte, []byte, error) {
|
2015-04-16 06:03:27 +00:00
|
|
|
files, err := ioutil.ReadDir(baseConfigDir)
|
2015-02-19 03:05:23 +00:00
|
|
|
if err != nil {
|
2015-04-16 06:03:27 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Infof("%s does not exist, not merging", baseConfigDir)
|
2015-04-17 13:18:33 +00:00
|
|
|
return current, currentScript, nil
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("Failed to read %s: %v", baseConfigDir, err)
|
2015-04-17 13:18:33 +00:00
|
|
|
return nil, nil, err
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:33 +00:00
|
|
|
scriptResult := currentScript
|
2015-04-16 06:03:27 +00:00
|
|
|
result := []byte{}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
input := path.Join(baseConfigDir, file.Name())
|
|
|
|
content, err := ioutil.ReadFile(input)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to read %s: %v", input, err)
|
|
|
|
// ignore error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:33 +00:00
|
|
|
if config.IsScript(string(content)) {
|
|
|
|
scriptResult = content
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
log.Infof("Merging %s", input)
|
2015-04-17 13:18:33 +00:00
|
|
|
|
|
|
|
if isCompose(string(content)) {
|
|
|
|
content, err = toCompose(content)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to convert %s to cloud-config syntax: %v", input, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
result, err = util.MergeBytes(result, content)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to merge bytes: %v", err)
|
2015-04-17 13:18:33 +00:00
|
|
|
return nil, nil, err
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
if len(result) == 0 {
|
2015-04-17 13:18:33 +00:00
|
|
|
return current, scriptResult, nil
|
2015-02-19 03:05:23 +00:00
|
|
|
} else {
|
2015-04-17 13:18:33 +00:00
|
|
|
result, err := util.MergeBytes(result, current)
|
|
|
|
return result, scriptResult, err
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
func saveCloudConfig() error {
|
|
|
|
var userDataBytes []byte
|
|
|
|
var metadata datasource.Metadata
|
2015-02-19 03:05:23 +00:00
|
|
|
|
2015-04-16 06:03:27 +00:00
|
|
|
ds, err := currentDatasource()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to select datasource: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds != nil {
|
|
|
|
log.Infof("Fetching user-data from datasource %v", ds.Type())
|
|
|
|
userDataBytes, err = ds.FetchUserdata()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed fetching user-data from datasource: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Fetching meta-data from datasource of type %v", ds.Type())
|
|
|
|
metadata, err = ds.FetchMetadata()
|
2015-03-18 12:27:37 +00:00
|
|
|
if err != nil {
|
2015-04-16 06:03:27 +00:00
|
|
|
log.Errorf("Failed fetching meta-data from datasource: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 00:39:54 +00:00
|
|
|
userDataBytes = substituteUserDataVars(userDataBytes, metadata)
|
2015-04-16 06:03:27 +00:00
|
|
|
userData := string(userDataBytes)
|
|
|
|
scriptBytes := []byte{}
|
|
|
|
|
|
|
|
if config.IsScript(userData) {
|
|
|
|
scriptBytes = userDataBytes
|
|
|
|
userDataBytes = []byte{}
|
|
|
|
} else if isCompose(userData) {
|
|
|
|
if userDataBytes, err = toCompose(userDataBytes); err != nil {
|
|
|
|
log.Errorf("Failed to convert to compose syntax: %v", err)
|
|
|
|
return err
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-04-16 06:03:27 +00:00
|
|
|
} else if config.IsCloudConfig(userData) {
|
|
|
|
// nothing to do
|
|
|
|
} else {
|
|
|
|
log.Errorf("Unrecognized cloud-init\n%s", userData)
|
|
|
|
userDataBytes = []byte{}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 13:18:33 +00:00
|
|
|
if userDataBytes, scriptBytes, err = mergeBaseConfig(userDataBytes, scriptBytes); err != nil {
|
2015-04-16 06:03:27 +00:00
|
|
|
log.Errorf("Failed to merge base config: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveFiles(userDataBytes, scriptBytes, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSaveCloudConfig() (*config.CloudConfig, error) {
|
|
|
|
cloudConfig := path.Join(outputDir, outputFile)
|
|
|
|
|
|
|
|
ds := file.NewDatasource(cloudConfig)
|
|
|
|
if !ds.IsAvailable() {
|
|
|
|
log.Infof("%s does not exist", cloudConfig)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ccBytes, err := ds.FetchUserdata()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to read user-data from %s: %v", cloudConfig, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cc config.CloudConfig
|
|
|
|
err = yaml.Unmarshal(ccBytes, &cc)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to unmarshall user-data from %s: %v", cloudConfig, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeCloudConfig() error {
|
|
|
|
ccu, err := getSaveCloudConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var metadata datasource.Metadata
|
|
|
|
|
|
|
|
metaDataBytes, err := ioutil.ReadFile(path.Join(outputDir, metaDataFile))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = yaml.Unmarshal(metaDataBytes, &metadata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Merging cloud-config from meta-data and user-data")
|
|
|
|
cc := mergeConfigs(ccu, metadata)
|
|
|
|
|
2015-03-25 22:27:33 +00:00
|
|
|
if cc.Hostname != "" {
|
|
|
|
//set hostname
|
|
|
|
if err := hostname.SetHostname(cc.Hostname); err != nil {
|
|
|
|
log.Fatal(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 _, user := range cc.Users {
|
|
|
|
if user.Name == "" {
|
|
|
|
continue
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
2015-02-22 00:07:29 +00:00
|
|
|
if len(user.SSHAuthorizedKeys) > 0 {
|
2015-03-18 12:27:37 +00:00
|
|
|
authorizeSSHKeys(user.Name, user.SSHAuthorizedKeys, sshKeyName)
|
2015-02-22 00:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-23 19:00:24 +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-03-25 22:27:33 +00:00
|
|
|
log.Fatal(err)
|
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() {
|
|
|
|
flags.Parse(rancherConfig.FilterGlobalConfig(os.Args[1:]))
|
|
|
|
|
|
|
|
if save {
|
|
|
|
err := saveCloudConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save cloud config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if execute {
|
|
|
|
err := executeCloudConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save cloud config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mergeConfigs merges certain options from md (meta-data from the datasource)
|
|
|
|
// onto cc (a CloudConfig derived from user-data), if they are not already set
|
|
|
|
// on cc (i.e. user-data always takes precedence)
|
|
|
|
func mergeConfigs(cc *config.CloudConfig, md datasource.Metadata) (out config.CloudConfig) {
|
|
|
|
if cc != nil {
|
|
|
|
out = *cc
|
|
|
|
}
|
|
|
|
|
|
|
|
if md.Hostname != "" {
|
|
|
|
if out.Hostname != "" {
|
2015-02-23 19:00:24 +00:00
|
|
|
log.Infof("Warning: user-data hostname (%s) overrides metadata hostname (%s)\n", out.Hostname, md.Hostname)
|
2015-02-19 03:05:23 +00:00
|
|
|
} else {
|
|
|
|
out.Hostname = md.Hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, key := range md.SSHPublicKeys {
|
|
|
|
out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDatasources creates a slice of possible Datasources for cloudinit based
|
|
|
|
// on the different source command-line flags.
|
2015-02-20 03:05:17 +00:00
|
|
|
func getDatasources(cfg *rancherConfig.Config) []datasource.Datasource {
|
2015-02-19 03:05:23 +00:00
|
|
|
dss := make([]datasource.Datasource, 0, 5)
|
2015-02-20 03:05:17 +00:00
|
|
|
|
|
|
|
for _, ds := range cfg.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-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() {
|
|
|
|
err := rancherNetwork.ApplyNetworkConfigs(&rancherConfig.NetworkConfig{
|
|
|
|
Interfaces: map[string]rancherConfig.InterfaceConfig{
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCompose(bytes []byte) ([]byte, error) {
|
|
|
|
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
|
|
|
}
|
2015-05-24 00:39:54 +00:00
|
|
|
|
|
|
|
func substituteUserDataVars(userDataBytes []byte, metadata datasource.Metadata) []byte {
|
|
|
|
env := initialize.NewEnvironment("", "", "", "", metadata)
|
|
|
|
userData := env.Apply(string(userDataBytes))
|
|
|
|
|
|
|
|
return []byte(userData)
|
|
|
|
}
|