2014-03-18 20:00:41 +04:00
|
|
|
package initialize
|
2014-03-05 04:36:05 +04:00
|
|
|
|
|
|
|
import (
|
2014-05-10 07:33:34 +04:00
|
|
|
"errors"
|
2014-03-13 03:52:32 +04:00
|
|
|
"fmt"
|
2014-03-05 04:36:05 +04:00
|
|
|
"log"
|
|
|
|
|
2014-03-13 06:36:31 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/third_party/launchpad.net/goyaml"
|
2014-03-05 04:36:05 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
|
|
|
)
|
2014-03-06 02:30:38 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
// CloudConfigFile represents a CoreOS specific configuration option that can generate
|
|
|
|
// an associated system.File to be written to disk
|
|
|
|
type CloudConfigFile interface {
|
|
|
|
// File should either return (*system.File, error), or (nil, nil) if nothing
|
|
|
|
// needs to be done for this configuration option.
|
|
|
|
File(root string) (*system.File, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloudConfigUnit represents a CoreOS specific configuration option that can generate
|
2014-06-06 03:12:40 +04:00
|
|
|
// associated system.Units to be created/enabled appropriately
|
2014-05-10 07:33:34 +04:00
|
|
|
type CloudConfigUnit interface {
|
2014-06-06 03:12:40 +04:00
|
|
|
Units(root string) ([]system.Unit, error)
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloudConfig encapsulates the entire cloud-config configuration file and maps directly to YAML
|
2014-03-05 04:36:05 +04:00
|
|
|
type CloudConfig struct {
|
2014-03-13 22:39:31 +04:00
|
|
|
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
|
|
|
|
Coreos struct {
|
2014-05-07 04:44:18 +04:00
|
|
|
Etcd EtcdEnvironment
|
2014-05-10 07:36:26 +04:00
|
|
|
Fleet FleetEnvironment
|
2014-05-07 04:44:18 +04:00
|
|
|
OEM OEMRelease
|
2014-05-10 07:33:34 +04:00
|
|
|
Update UpdateConfig
|
|
|
|
Units []system.Unit
|
2014-03-12 03:13:07 +04:00
|
|
|
}
|
2014-04-02 02:02:12 +04:00
|
|
|
WriteFiles []system.File `yaml:"write_files"`
|
|
|
|
Hostname string
|
|
|
|
Users []system.User
|
2014-05-10 07:33:34 +04:00
|
|
|
ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"`
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 12:06:38 +04:00
|
|
|
type warner func(format string, v ...interface{})
|
|
|
|
|
2014-05-13 02:46:38 +04:00
|
|
|
// warnOnUnrecognizedKeys parses the contents of a cloud-config file and calls
|
|
|
|
// warn(msg, key) for every unrecognized key (i.e. those not present in CloudConfig)
|
2014-05-10 12:06:38 +04:00
|
|
|
func warnOnUnrecognizedKeys(contents string, warn warner) {
|
|
|
|
// Generate a map of all understood cloud config options
|
|
|
|
var cc map[string]interface{}
|
|
|
|
b, _ := goyaml.Marshal(&CloudConfig{})
|
|
|
|
goyaml.Unmarshal(b, &cc)
|
2014-05-13 02:46:38 +04:00
|
|
|
|
2014-05-10 12:06:38 +04:00
|
|
|
// Now unmarshal the entire provided contents
|
|
|
|
var c map[string]interface{}
|
|
|
|
goyaml.Unmarshal([]byte(contents), &c)
|
2014-05-13 02:46:38 +04:00
|
|
|
|
2014-05-10 12:06:38 +04:00
|
|
|
// Check that every key in the contents exists in the cloud config
|
|
|
|
for k, _ := range c {
|
|
|
|
if _, ok := cc[k]; !ok {
|
|
|
|
warn("Warning: unrecognized key %q in provided cloud config - ignoring section", k)
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 02:46:38 +04:00
|
|
|
|
2014-05-13 03:33:28 +04:00
|
|
|
// Check for unrecognized coreos options, if any are set
|
2014-05-13 02:46:38 +04:00
|
|
|
coreos, ok := c["coreos"]
|
2014-05-13 03:33:28 +04:00
|
|
|
if ok {
|
|
|
|
set := coreos.(map[interface{}]interface{})
|
|
|
|
known := cc["coreos"].(map[interface{}]interface{})
|
|
|
|
for k, _ := range set {
|
|
|
|
key := k.(string)
|
|
|
|
if _, ok := known[key]; !ok {
|
|
|
|
warn("Warning: unrecognized key %q in coreos section of provided cloud config - ignoring", key)
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 02:46:38 +04:00
|
|
|
}
|
2014-05-13 03:33:28 +04:00
|
|
|
|
|
|
|
// Check for any badly-specified users, if any are set
|
|
|
|
users, ok := c["users"]
|
|
|
|
if ok {
|
|
|
|
var known map[string]interface{}
|
|
|
|
b, _ := goyaml.Marshal(&system.User{})
|
|
|
|
goyaml.Unmarshal(b, &known)
|
|
|
|
|
|
|
|
set := users.([]interface{})
|
|
|
|
for _, u := range set {
|
|
|
|
user := u.(map[interface{}]interface{})
|
|
|
|
for k, _ := range user {
|
|
|
|
key := k.(string)
|
|
|
|
if _, ok := known[key]; !ok {
|
|
|
|
warn("Warning: unrecognized key %q in user section of cloud config - ignoring", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any badly-specified files, if any are set
|
|
|
|
files, ok := c["write_files"]
|
|
|
|
if ok {
|
|
|
|
var known map[string]interface{}
|
|
|
|
b, _ := goyaml.Marshal(&system.File{})
|
|
|
|
goyaml.Unmarshal(b, &known)
|
|
|
|
|
|
|
|
set := files.([]interface{})
|
|
|
|
for _, f := range set {
|
|
|
|
file := f.(map[interface{}]interface{})
|
|
|
|
for k, _ := range file {
|
|
|
|
key := k.(string)
|
|
|
|
if _, ok := known[key]; !ok {
|
|
|
|
warn("Warning: unrecognized key %q in file section of cloud config - ignoring", key)
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 02:46:38 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-10 12:06:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCloudConfig instantiates a new CloudConfig from the given contents (a
|
|
|
|
// string of YAML), returning any error encountered. It will ignore unknown
|
|
|
|
// fields but log encountering them.
|
2014-03-21 21:35:18 +04:00
|
|
|
func NewCloudConfig(contents string) (*CloudConfig, error) {
|
2014-03-05 04:36:05 +04:00
|
|
|
var cfg CloudConfig
|
2014-03-21 21:35:18 +04:00
|
|
|
err := goyaml.Unmarshal([]byte(contents), &cfg)
|
2014-05-10 12:06:38 +04:00
|
|
|
if err != nil {
|
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
warnOnUnrecognizedKeys(contents, log.Printf)
|
|
|
|
return &cfg, nil
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc CloudConfig) String() string {
|
|
|
|
bytes, err := goyaml.Marshal(cc)
|
2014-03-13 03:52:32 +04:00
|
|
|
if err != nil {
|
2014-03-05 04:36:05 +04:00
|
|
|
return ""
|
|
|
|
}
|
2014-03-13 03:52:32 +04:00
|
|
|
|
|
|
|
stringified := string(bytes)
|
|
|
|
stringified = fmt.Sprintf("#cloud-config\n%s", stringified)
|
|
|
|
|
|
|
|
return stringified
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
// Apply renders a CloudConfig to an Environment. This can involve things like
|
|
|
|
// configuring the hostname, adding new users, writing various configuration
|
|
|
|
// files to disk, and manipulating systemd services.
|
2014-03-18 20:00:41 +04:00
|
|
|
func Apply(cfg CloudConfig, env *Environment) error {
|
2014-03-13 09:30:24 +04:00
|
|
|
if cfg.Hostname != "" {
|
2014-03-18 20:00:41 +04:00
|
|
|
if err := system.SetHostname(cfg.Hostname); err != nil {
|
2014-03-13 09:30:24 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Set hostname to %s", cfg.Hostname)
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
for _, user := range cfg.Users {
|
|
|
|
if user.Name == "" {
|
|
|
|
log.Printf("User object has no 'name' field, skipping")
|
|
|
|
continue
|
2014-03-23 22:06:43 +04:00
|
|
|
}
|
2014-03-13 21:56:59 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if system.UserExists(&user) {
|
|
|
|
log.Printf("User '%s' exists, ignoring creation-time fields", user.Name)
|
|
|
|
if user.PasswordHash != "" {
|
|
|
|
log.Printf("Setting '%s' user's password", user.Name)
|
|
|
|
if err := system.SetUserPassword(user.Name, user.PasswordHash); err != nil {
|
|
|
|
log.Printf("Failed setting '%s' user's password: %v", user.Name, err)
|
2014-03-13 21:56:59 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
} else {
|
|
|
|
log.Printf("Creating user '%s'", user.Name)
|
|
|
|
if err := system.CreateUser(&user); err != nil {
|
|
|
|
log.Printf("Failed creating user '%s': %v", user.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 21:56:59 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if len(user.SSHAuthorizedKeys) > 0 {
|
|
|
|
log.Printf("Authorizing %d SSH keys for user '%s'", len(user.SSHAuthorizedKeys), user.Name)
|
|
|
|
if err := system.AuthorizeSSHKeys(user.Name, env.SSHKeyName(), user.SSHAuthorizedKeys); err != nil {
|
|
|
|
return err
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
|
|
|
if user.SSHImportGithubUser != "" {
|
|
|
|
log.Printf("Authorizing github user %s SSH keys for CoreOS user '%s'", user.SSHImportGithubUser, user.Name)
|
|
|
|
if err := SSHImportGithubUser(user.Name, user.SSHImportGithubUser); err != nil {
|
|
|
|
return err
|
2014-03-19 19:54:45 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
|
|
|
if user.SSHImportURL != "" {
|
|
|
|
log.Printf("Authorizing SSH keys for CoreOS user '%s' from '%s'", user.Name, user.SSHImportURL)
|
|
|
|
if err := SSHImportKeysFromURL(user.Name, user.SSHImportURL); err != nil {
|
|
|
|
return err
|
2014-03-23 02:26:18 +04:00
|
|
|
}
|
2014-03-13 21:56:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 22:39:31 +04:00
|
|
|
if len(cfg.SSHAuthorizedKeys) > 0 {
|
2014-03-18 20:00:41 +04:00
|
|
|
err := system.AuthorizeSSHKeys("core", env.SSHKeyName(), cfg.SSHAuthorizedKeys)
|
2014-03-05 04:36:05 +04:00
|
|
|
if err == nil {
|
|
|
|
log.Printf("Authorized SSH keys for core user")
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
for _, ccf := range []CloudConfigFile{cfg.Coreos.OEM, cfg.Coreos.Update, cfg.ManageEtcHosts} {
|
|
|
|
f, err := ccf.File(env.Root())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f != nil {
|
|
|
|
cfg.WriteFiles = append(cfg.WriteFiles, *f)
|
2014-03-12 04:46:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:36:26 +04:00
|
|
|
for _, ccu := range []CloudConfigUnit{cfg.Coreos.Etcd, cfg.Coreos.Fleet, cfg.Coreos.Update} {
|
2014-06-06 03:12:40 +04:00
|
|
|
u, err := ccu.Units(env.Root())
|
2014-05-10 07:33:34 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-06 03:12:40 +04:00
|
|
|
cfg.Coreos.Units = append(cfg.Coreos.Units, u...)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
for _, file := range cfg.WriteFiles {
|
2014-06-05 23:48:32 +04:00
|
|
|
path, err := system.WriteFile(&file, env.Root())
|
|
|
|
if err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
return err
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
2014-06-05 23:48:32 +04:00
|
|
|
log.Printf("Wrote file %s to filesystem", path)
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
commands := make(map[string]string, 0)
|
|
|
|
reload := false
|
|
|
|
for _, unit := range cfg.Coreos.Units {
|
2014-06-04 03:49:26 +04:00
|
|
|
dst := unit.Destination(env.Root())
|
2014-05-10 07:33:34 +04:00
|
|
|
if unit.Content != "" {
|
|
|
|
log.Printf("Writing unit %s to filesystem at path %s", unit.Name, dst)
|
|
|
|
if err := system.PlaceUnit(&unit, dst); err != nil {
|
|
|
|
return err
|
2014-04-15 20:00:19 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
log.Printf("Placed unit %s at %s", unit.Name, dst)
|
|
|
|
reload = true
|
|
|
|
}
|
2014-03-20 02:52:24 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if unit.Mask {
|
|
|
|
log.Printf("Masking unit file %s", unit.Name)
|
2014-06-04 03:49:26 +04:00
|
|
|
if err := system.MaskUnit(&unit, env.Root()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if unit.Runtime {
|
|
|
|
log.Printf("Ensuring runtime unit file %s is unmasked", unit.Name)
|
|
|
|
if err := system.UnmaskUnit(&unit, env.Root()); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
return err
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if unit.Enable {
|
|
|
|
if unit.Group() != "network" {
|
2014-05-27 02:16:24 +04:00
|
|
|
log.Printf("Enabling unit file %s", unit.Name)
|
|
|
|
if err := system.EnableUnitFile(unit.Name, unit.Runtime); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
return err
|
2014-03-25 01:12:52 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
log.Printf("Enabled unit %s", unit.Name)
|
|
|
|
} else {
|
|
|
|
log.Printf("Skipping enable for network-like unit %s", unit.Name)
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 02:52:24 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if unit.Group() == "network" {
|
|
|
|
commands["systemd-networkd.service"] = "restart"
|
|
|
|
} else if unit.Command != "" {
|
|
|
|
commands[unit.Name] = unit.Command
|
2014-05-06 00:16:07 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
2014-05-06 00:16:07 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if reload {
|
|
|
|
if err := system.DaemonReload(); err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("failed systemd daemon-reload: %v", err))
|
2014-03-20 02:52:24 +04:00
|
|
|
}
|
2014-03-13 02:43:51 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
for unit, command := range commands {
|
|
|
|
log.Printf("Calling unit command '%s %s'", command, unit)
|
|
|
|
res, err := system.RunUnitCommand(command, unit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-04-02 02:02:12 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
log.Printf("Result of '%s %s': %s", command, unit, res)
|
2014-04-02 02:02:12 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 04:36:05 +04:00
|
|
|
return nil
|
|
|
|
}
|