1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 22:32:14 +00:00

Reshuffle cloud-config

Read files cloud-config.d in alphanumeric order, then cloud-config.yml
`ros config` writes to cloud-config.yml (and cloud-config.d/private.yml - only private keys)

Add (c *CloudConfig) Save() method, use it to save the changed config

Read and apply metadata as part of LoadConfig()

Simplify ros config export logic
This commit is contained in:
Ivan Mikushin
2015-09-23 16:36:28 +05:00
parent 0ac4c783f9
commit 338abb758f
20 changed files with 658 additions and 695 deletions

View File

@@ -16,10 +16,10 @@
package cloudinit
import (
"errors"
"flag"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"time"
@@ -35,13 +35,11 @@ import (
"github.com/coreos/coreos-cloudinit/datasource/metadata/ec2"
"github.com/coreos/coreos-cloudinit/datasource/proc_cmdline"
"github.com/coreos/coreos-cloudinit/datasource/url"
"github.com/coreos/coreos-cloudinit/initialize"
"github.com/coreos/coreos-cloudinit/pkg"
"github.com/coreos/coreos-cloudinit/system"
"github.com/rancher/netconf"
"github.com/rancherio/os/cmd/cloudinit/hostname"
rancherConfig "github.com/rancherio/os/config"
"github.com/rancherio/os/util"
)
const (
@@ -49,7 +47,6 @@ const (
datasourceMaxInterval = 30 * time.Second
datasourceTimeout = 5 * time.Minute
sshKeyName = "rancheros-cloud-config"
baseConfigDir = "/var/lib/rancher/conf/cloud-config.d"
)
var (
@@ -67,8 +64,9 @@ func init() {
}
func saveFiles(cloudConfigBytes, scriptBytes []byte, metadata datasource.Metadata) error {
os.MkdirAll(rancherConfig.CloudConfigDir, os.ModeDir|0600)
os.Remove(rancherConfig.CloudConfigScriptFile)
os.Remove(rancherConfig.CloudConfigFile)
os.Remove(rancherConfig.CloudConfigBootFile)
os.Remove(rancherConfig.MetaDataFile)
if len(scriptBytes) > 0 {
@@ -79,10 +77,10 @@ func saveFiles(cloudConfigBytes, scriptBytes []byte, metadata datasource.Metadat
}
}
if err := ioutil.WriteFile(rancherConfig.CloudConfigFile, cloudConfigBytes, 400); err != nil {
if err := ioutil.WriteFile(rancherConfig.CloudConfigBootFile, cloudConfigBytes, 400); err != nil {
return err
}
log.Infof("Written to %s:\n%s", rancherConfig.CloudConfigFile, string(cloudConfigBytes))
log.Infof("Written to %s:\n%s", rancherConfig.CloudConfigBootFile, string(cloudConfigBytes))
metaDataBytes, err := yaml.Marshal(metadata)
if err != nil {
@@ -113,90 +111,12 @@ func currentDatasource() (datasource.Datasource, error) {
return ds, nil
}
func mergeBaseConfig(current, currentScript []byte) ([]byte, []byte, error) {
files, err := ioutil.ReadDir(baseConfigDir)
if err != nil {
if os.IsNotExist(err) {
log.Infof("%s does not exist, not merging", baseConfigDir)
return current, currentScript, nil
}
log.Errorf("Failed to read %s: %v", baseConfigDir, err)
return nil, nil, err
}
scriptResult := currentScript
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
}
if config.IsScript(string(content)) {
scriptResult = content
continue
}
log.Infof("Merging %s", input)
if isCompose(string(content)) {
content, err = toCompose(content)
if err != nil {
log.Errorf("Failed to convert %s to cloud-config syntax: %v", input, err)
}
}
result, err = util.MergeBytes(result, content)
if err != nil {
log.Errorf("Failed to merge bytes: %v", err)
return nil, nil, err
}
}
if len(result) == 0 {
return current, scriptResult, nil
} else {
result, err := util.MergeBytes(result, current)
return result, scriptResult, err
}
}
func saveCloudConfig() error {
var userDataBytes []byte
var metadata datasource.Metadata
ds, err := currentDatasource()
userDataBytes, metadata, err := fetchUserData()
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()
if err != nil {
log.Errorf("Failed fetching meta-data from datasource: %v", err)
return err
}
}
userDataBytes = substituteUserDataVars(userDataBytes, metadata)
userData := string(userDataBytes)
scriptBytes := []byte{}
@@ -204,75 +124,56 @@ func saveCloudConfig() error {
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)
if userDataBytes, err = composeToCloudConfig(userDataBytes); err != nil {
log.Errorf("Failed to convert compose to cloud-config syntax: %v", err)
return err
}
} else if config.IsCloudConfig(userData) {
if rancherConfig.ReadConfig(userDataBytes) == nil {
log.WithFields(log.Fields{"cloud-config": userData}).Warn("Failed to parse cloud-config, not saving.")
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.")
userDataBytes = []byte{}
}
} else {
log.Errorf("Unrecognized cloud-init\n%s", userData)
log.Errorf("Unrecognized user-data\n%s", userData)
userDataBytes = []byte{}
}
userDataBytesMerged, scriptBytes, err := mergeBaseConfig(userDataBytes, scriptBytes)
if err != nil {
log.Errorf("Failed to merge base config: %v", err)
} else if rancherConfig.ReadConfig(userDataBytesMerged) == nil {
log.WithFields(log.Fields{"cloud-config": userData}).Warn("Failed to parse merged cloud-config, not merging.")
} else {
userDataBytes = userDataBytesMerged
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")
}
return saveFiles(userDataBytes, scriptBytes, metadata)
}
func getSaveCloudConfig() (*config.CloudConfig, error) {
ds := file.NewDatasource(rancherConfig.CloudConfigFile)
if !ds.IsAvailable() {
log.Infof("%s does not exist", rancherConfig.CloudConfigFile)
return nil, nil
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
}
ccBytes, err := ds.FetchUserdata()
log.Infof("Fetching user-data from datasource %v", ds.Type())
userDataBytes, err := ds.FetchUserdata()
if err != nil {
log.Errorf("Failed to read user-data from %s: %v", rancherConfig.CloudConfigFile, err)
return nil, err
log.Errorf("Failed fetching user-data from datasource: %v", err)
return nil, metadata, err
}
var cc config.CloudConfig
err = yaml.Unmarshal(ccBytes, &cc)
log.Infof("Fetching meta-data from datasource of type %v", ds.Type())
metadata, err = ds.FetchMetadata()
if err != nil {
log.Errorf("Failed to unmarshall user-data from %s: %v", rancherConfig.CloudConfigFile, err)
return nil, err
log.Errorf("Failed fetching meta-data from datasource: %v", err)
return nil, metadata, err
}
return &cc, err
return userDataBytes, metadata, nil
}
func executeCloudConfig() error {
ccu, err := getSaveCloudConfig()
cc, err := rancherConfig.LoadConfig()
if err != nil {
return err
}
var metadata datasource.Metadata
metaDataBytes, err := ioutil.ReadFile(rancherConfig.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)
if cc.Hostname != "" {
//set hostname
if err := hostname.SetHostname(cc.Hostname); err != nil {
@@ -285,15 +186,6 @@ func executeCloudConfig() error {
authorizeSSHKeys("docker", cc.SSHAuthorizedKeys, sshKeyName)
}
for _, user := range cc.Users {
if user.Name == "" {
continue
}
if len(user.SSHAuthorizedKeys) > 0 {
authorizeSSHKeys(user.Name, user.SSHAuthorizedKeys, sshKeyName)
}
}
for _, file := range cc.WriteFiles {
f := system.File{File: file}
fullPath, err := system.WriteFile(&f, "/")
@@ -308,7 +200,7 @@ func executeCloudConfig() error {
}
func Main() {
flags.Parse(rancherConfig.FilterGlobalConfig(os.Args[1:]))
flags.Parse(os.Args[1:])
log.Infof("Running cloud-init: save=%v, execute=%v", save, execute)
@@ -327,27 +219,6 @@ func Main() {
}
}
// 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 != "" {
log.Infof("Warning: user-data hostname (%s) overrides metadata hostname (%s)\n", out.Hostname, md.Hostname)
} 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.
func getDatasources(cfg *rancherConfig.CloudConfig) []datasource.Datasource {
@@ -478,7 +349,7 @@ func isCompose(content string) bool {
return strings.HasPrefix(content, "#compose\n")
}
func toCompose(bytes []byte) ([]byte, error) {
func composeToCloudConfig(bytes []byte) ([]byte, error) {
compose := make(map[interface{}]interface{})
err := yaml.Unmarshal(bytes, &compose)
if err != nil {
@@ -491,10 +362,3 @@ func toCompose(bytes []byte) ([]byte, error) {
},
})
}
func substituteUserDataVars(userDataBytes []byte, metadata datasource.Metadata) []byte {
env := initialize.NewEnvironment("", "", "", "", metadata)
userData := env.Apply(string(userDataBytes))
return []byte(userData)
}

View File

@@ -1,100 +0,0 @@
// 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.
package cloudinit
import (
"net"
"testing"
"github.com/coreos/coreos-cloudinit/datasource"
)
func TestSubstituteUserDataVars(t *testing.T) {
for _, tt := range []struct {
metadata datasource.Metadata
input string
out string
}{
{
// Userdata with docker-compose syntax
datasource.Metadata{
PublicIPv4: net.ParseIP("192.0.2.3"),
PrivateIPv4: net.ParseIP("192.0.2.203"),
PublicIPv6: net.ParseIP("fe00:1234::"),
PrivateIPv6: net.ParseIP("fe00:5678::"),
},
`servicexyz:
image: rancher/servicexyz:v0.3.1
ports:
- "$public_ipv4:8001:8001"
- "$public_ipv6:8001:8001"
- "$private_ipv4:8001:8001"
- "$private_ipv6:8001:8001"`,
`servicexyz:
image: rancher/servicexyz:v0.3.1
ports:
- "192.0.2.3:8001:8001"
- "fe00:1234:::8001:8001"
- "192.0.2.203:8001:8001"
- "fe00:5678:::8001:8001"`,
},
{
// Userdata with cloud-config/rancher syntax
datasource.Metadata{
PublicIPv4: net.ParseIP("192.0.2.3"),
PrivateIPv4: net.ParseIP("192.0.2.203"),
PublicIPv6: net.ParseIP("fe00:1234::"),
PrivateIPv6: net.ParseIP("fe00:5678::"),
},
`write_files:
- path: /etc/environment
content: |
PRIVATE_IPV6=$private_ipv6
PUBLIC_IPV6=$public_ipv6
rancher:
network:
interfaces:
eth1:
address: $private_ipv4/16
docker:
tls_args: ['-H=$public_ipv4:2376']`,
`write_files:
- path: /etc/environment
content: |
PRIVATE_IPV6=fe00:5678::
PUBLIC_IPV6=fe00:1234::
rancher:
network:
interfaces:
eth1:
address: 192.0.2.203/16
docker:
tls_args: ['-H=192.0.2.3:2376']`,
},
{
// no metadata
datasource.Metadata{},
"address: $private_ipv4",
"address: ",
},
} {
got := substituteUserDataVars([]byte(tt.input), tt.metadata)
if string(got) != tt.out {
t.Fatalf("Userdata substitution incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
}
}
}