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-03-18 20:00:41 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
2015-01-27 02:45:15 +03:00
|
|
|
"net"
|
2014-03-21 21:35:18 +04:00
|
|
|
"os"
|
2014-03-18 20:00:41 +04:00
|
|
|
"path"
|
2014-09-12 02:03:55 +04:00
|
|
|
"regexp"
|
2014-03-21 21:35:18 +04:00
|
|
|
"strings"
|
2014-07-11 02:44:52 +04:00
|
|
|
|
2015-11-09 11:24:52 +03:00
|
|
|
"github.com/coreos/coreos-cloudinit/config"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-03-18 20:00:41 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultSSHKeyName = "coreos-cloudinit"
|
|
|
|
|
|
|
|
type Environment struct {
|
2014-03-21 21:35:18 +04:00
|
|
|
root string
|
2014-06-18 22:36:06 +04:00
|
|
|
configRoot string
|
2014-03-21 21:35:18 +04:00
|
|
|
workspace string
|
|
|
|
sshKeyName string
|
|
|
|
substitutions map[string]string
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-25 04:46:06 +04:00
|
|
|
// TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
|
2015-01-27 04:35:08 +03:00
|
|
|
func NewEnvironment(root, configRoot, workspace, sshKeyName string, metadata datasource.Metadata) *Environment {
|
2015-01-27 02:45:15 +03:00
|
|
|
firstNonNull := func(ip net.IP, env string) string {
|
|
|
|
if ip == nil {
|
|
|
|
return env
|
2014-06-25 05:49:49 +04:00
|
|
|
}
|
2015-01-27 02:45:15 +03:00
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
substitutions := map[string]string{
|
|
|
|
"$public_ipv4": firstNonNull(metadata.PublicIPv4, os.Getenv("COREOS_PUBLIC_IPV4")),
|
|
|
|
"$private_ipv4": firstNonNull(metadata.PrivateIPv4, os.Getenv("COREOS_PRIVATE_IPV4")),
|
|
|
|
"$public_ipv6": firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")),
|
|
|
|
"$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")),
|
2014-03-21 21:35:18 +04:00
|
|
|
}
|
2015-01-27 04:35:08 +03:00
|
|
|
return &Environment{root, configRoot, workspace, sshKeyName, substitutions}
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Workspace() string {
|
|
|
|
return path.Join(e.root, e.workspace)
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Root() string {
|
|
|
|
return e.root
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) ConfigRoot() string {
|
|
|
|
return e.configRoot
|
2014-06-18 22:36:06 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) SSHKeyName() string {
|
|
|
|
return e.sshKeyName
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) SetSSHKeyName(name string) {
|
|
|
|
e.sshKeyName = name
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
2014-03-21 21:35:18 +04:00
|
|
|
|
2014-09-12 02:03:55 +04:00
|
|
|
// Apply goes through the map of substitutions and replaces all instances of
|
|
|
|
// the keys with their respective values. It supports escaping substitutions
|
|
|
|
// with a leading '\'.
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Apply(data string) string {
|
|
|
|
for key, val := range e.substitutions {
|
2014-09-12 02:03:55 +04:00
|
|
|
matchKey := strings.Replace(key, `$`, `\$`, -1)
|
|
|
|
replKey := strings.Replace(key, `$`, `$$`, -1)
|
|
|
|
|
|
|
|
// "key" -> "val"
|
|
|
|
data = regexp.MustCompile(`([^\\]|^)`+matchKey).ReplaceAllString(data, `${1}`+val)
|
|
|
|
// "\key" -> "key"
|
|
|
|
data = regexp.MustCompile(`\\`+matchKey).ReplaceAllString(data, replKey)
|
2014-03-21 21:35:18 +04:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
2014-07-11 02:44:52 +04:00
|
|
|
func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
|
|
|
|
ef := system.EnvFile{
|
2014-10-23 22:46:08 +04:00
|
|
|
File: &system.File{File: config.File{
|
2014-07-11 02:44:52 +04:00
|
|
|
Path: "/etc/environment",
|
2014-09-22 06:22:27 +04:00
|
|
|
}},
|
2014-07-11 02:44:52 +04:00
|
|
|
Vars: map[string]string{},
|
|
|
|
}
|
|
|
|
if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PUBLIC_IPV4"] = ip
|
|
|
|
}
|
|
|
|
if ip, ok := e.substitutions["$private_ipv4"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PRIVATE_IPV4"] = ip
|
|
|
|
}
|
2014-09-11 19:35:09 +04:00
|
|
|
if ip, ok := e.substitutions["$public_ipv6"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PUBLIC_IPV6"] = ip
|
|
|
|
}
|
|
|
|
if ip, ok := e.substitutions["$private_ipv6"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PRIVATE_IPV6"] = ip
|
|
|
|
}
|
2014-07-11 02:44:52 +04:00
|
|
|
if len(ef.Vars) == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return &ef
|
|
|
|
}
|
|
|
|
}
|