1
0
mirror of https://github.com/rancher/os.git synced 2025-09-12 13:17:17 +00:00

Refactor configuration to compose syntax

This commit is contained in:
Darren Shepherd
2015-03-29 02:57:15 -07:00
parent 38389b1f8e
commit c7ae14cc13
11 changed files with 535 additions and 344 deletions

View File

@@ -5,6 +5,8 @@ import (
"os"
"strings"
"github.com/rancherio/rancher-compose/project"
log "github.com/Sirupsen/logrus"
"github.com/rancherio/os/util"
"gopkg.in/yaml.v2"
@@ -16,23 +18,10 @@ func (c *Config) privilegedMerge(newConfig Config) error {
return err
}
toAppend := make([]ContainerConfig, 0, 5)
for _, newContainer := range newConfig.SystemContainers {
found := false
for i, existingContainer := range c.SystemContainers {
if existingContainer.Id != "" && newContainer.Id == existingContainer.Id {
found = true
c.SystemContainers[i] = newContainer
}
}
if !found {
toAppend = append(toAppend, newContainer)
}
for k, v := range newConfig.SystemContainers {
c.SystemContainers[k] = v
}
c.SystemContainers = append(c.SystemContainers, toAppend...)
return nil
}
@@ -42,8 +31,8 @@ func (c *Config) overlay(newConfig Config) error {
}
func (c *Config) clearReadOnly() {
c.BootstrapContainers = make([]ContainerConfig, 0)
c.SystemContainers = make([]ContainerConfig, 0)
c.BootstrapContainers = make(map[string]*project.ServiceConfig, 0)
c.SystemContainers = make(map[string]*project.ServiceConfig, 0)
}
func clearReadOnly(data map[interface{}]interface{}) map[interface{}]interface{} {
@@ -103,6 +92,12 @@ func LoadConfig() (*Config, error) {
if cfg.Debug {
log.SetLevel(log.DebugLevel)
if !util.Contains(cfg.UserDocker.Args, "-D") {
cfg.UserDocker.Args = append(cfg.UserDocker.Args, "-D")
}
if !util.Contains(cfg.SystemDocker.Args, "-D") {
cfg.SystemDocker.Args = append(cfg.SystemDocker.Args, "-D")
}
}
return cfg, nil
@@ -197,20 +192,11 @@ func Dump(private, full bool) (string, error) {
}
func (c *Config) configureConsole() error {
if !c.Console.Persistent {
return nil
}
for i := range c.SystemContainers {
// Need to modify original object, not the copy
var container *ContainerConfig = &c.SystemContainers[i]
if container.Id != CONSOLE_CONTAINER {
continue
}
if strings.Contains(container.Cmd, "--rm ") {
container.Cmd = strings.Replace(container.Cmd, "--rm ", "", 1)
if console, ok := c.SystemContainers[CONSOLE_CONTAINER]; ok {
if c.Console.Persistent {
console.Labels = append(console.Labels, REMOVE+"=false")
} else {
console.Labels = append(console.Labels, REMOVE+"=true")
}
}
@@ -221,7 +207,6 @@ func (c *Config) readGlobals() error {
return util.ShortCircuit(
c.readCmdline,
c.readArgs,
c.mergeAddons,
c.configureConsole,
)
}
@@ -233,19 +218,6 @@ func (c *Config) Reload() error {
)
}
func (c *Config) mergeAddons() error {
for _, addon := range c.EnabledAddons {
if newConfig, ok := c.Addons[addon]; ok {
log.Debugf("Enabling addon %s", addon)
if err := c.privilegedMerge(newConfig); err != nil {
return err
}
}
}
return nil
}
func (c *Config) Get(key string) (interface{}, error) {
data := make(map[interface{}]interface{})
err := util.Convert(c, &data)

View File

@@ -1,5 +1,9 @@
package config
import (
"github.com/rancherio/rancher-compose/project"
)
func NewConfig() *Config {
return &Config{
Debug: DEBUG,
@@ -43,7 +47,7 @@ func NewConfig() *Config {
Nameservers: []string{"8.8.8.8", "8.8.4.4"},
},
Interfaces: map[string]InterfaceConfig{
"eth*": {
"eth0": {
DHCP: true,
},
"lo": {
@@ -58,193 +62,238 @@ func NewConfig() *Config {
Url: "https://releases.rancher.com/os/versions.yml",
Image: "rancher/os",
},
BootstrapContainers: []ContainerConfig{
{
Id: "udev",
Cmd: "--name=udev " +
"--net=none " +
"--privileged " +
"--rm " +
"-v=/dev:/host/dev " +
"-v=/lib/modules:/lib/modules:ro " +
"udev",
BootstrapContainers: map[string]*project.ServiceConfig{
"udev": {
Net: "host",
Privileged: true,
Labels: []string{
DETACH + "=false",
},
Volumes: []string{
"/dev:/host/dev",
"/lib/modules:/lib/modules:ro",
"/lib/firmware:/lib/firmware:ro",
},
Image: "udev",
},
},
SystemContainers: []ContainerConfig{
{
Id: "udev",
Cmd: "--name=udev " +
"--net=none " +
"--privileged " +
"--rm " +
"-v=/dev:/host/dev " +
"-v=/lib/modules:/lib/modules:ro " +
"udev",
CreateOnly: true,
SystemContainers: map[string]*project.ServiceConfig{
"udev": {
Image: "udev",
Net: "host",
Privileged: true,
Labels: []string{
DETACH + "=true",
},
Environment: []string{
"DAEMON=true",
},
Volumes: []string{
"/dev:/host/dev",
"/lib/modules:/lib/modules:ro",
"/lib/firmware:/lib/firmware:ro",
},
},
{
Id: "system-volumes",
Cmd: "--name=system-volumes " +
"--net=none " +
"--read-only " +
"-v=/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt " +
"-v=/var/lib/rancher/conf:/var/lib/rancher/conf " +
"-v=/lib/modules:/lib/modules:ro " +
"-v=/var/run:/var/run " +
"-v=/var/log:/var/log " +
"state",
CreateOnly: true,
"system-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: []string{
CREATE_ONLY + "=true",
},
Volumes: []string{
"/etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt",
"/var/lib/rancher/conf:/var/lib/rancher/conf",
"/lib/modules:/lib/modules:ro",
"/lib/firmware:/lib/firmware:ro",
"/var/run:/var/run",
"/var/log:/var/log",
},
},
{
Id: "command-volumes",
Cmd: "--name=command-volumes " +
"--net=none " +
"--read-only " +
"-v=/init:/sbin/halt:ro " +
"-v=/init:/sbin/poweroff:ro " +
"-v=/init:/sbin/reboot:ro " +
"-v=/init:/sbin/shutdown:ro " +
"-v=/init:/sbin/netconf:ro " +
"-v=/init:/usr/bin/cloud-init:ro " +
"-v=/init:/usr/bin/rancherctl:ro " +
"-v=/init:/usr/bin/respawn:ro " +
"-v=/init:/usr/bin/system-docker:ro " +
"-v=/lib/modules:/lib/modules:ro " +
"-v=/usr/bin/docker:/usr/bin/docker:ro " +
"state",
CreateOnly: true,
"command-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: []string{
CREATE_ONLY + "=true",
},
Volumes: []string{
"/init:/sbin/halt:ro",
"/init:/sbin/poweroff:ro",
"/init:/sbin/reboot:ro",
"/init:/sbin/shutdown:ro",
"/init:/sbin/netconf:ro",
"/init:/usr/bin/cloud-init:ro",
"/init:/usr/bin/rancherctl:ro",
"/init:/usr/bin/respawn:ro",
"/init:/usr/bin/system-docker:ro",
"/lib/modules:/lib/modules:ro",
"/usr/bin/docker:/usr/bin/docker:ro",
},
},
{
Id: "user-volumes",
Cmd: "--name=user-volumes " +
"--net=none " +
"--read-only " +
"-v=/home:/home " +
"-v=/opt:/opt " +
"state",
CreateOnly: true,
"user-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: []string{
CREATE_ONLY + "=true",
},
Volumes: []string{
"/home:/home",
"/opt:/opt",
},
},
{
Id: "docker-volumes",
Cmd: "--name=docker-volumes " +
"--net=none " +
"--read-only " +
"-v=/var/lib/rancher:/var/lib/rancher " +
"-v=/var/lib/docker:/var/lib/docker " +
"-v=/var/lib/system-docker:/var/lib/system-docker " +
"state",
CreateOnly: true,
"docker-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: []string{
CREATE_ONLY + "=true",
},
Volumes: []string{
"/var/lib/rancher:/var/lib/rancher",
"/var/lib/docker:/var/lib/docker",
"/var/lib/system-docker:/var/lib/system-docker",
},
},
{
Id: "all-volumes",
Cmd: "--name=all-volumes " +
"--rm " +
"--net=none " +
"--read-only " +
"--volumes-from=docker-volumes " +
"--volumes-from=command-volumes " +
"--volumes-from=user-volumes " +
"--volumes-from=system-volumes " +
"state",
CreateOnly: true,
"all-volumes": {
Image: "state",
Net: "none",
ReadOnly: true,
Privileged: true,
Labels: []string{
CREATE_ONLY + "=true",
},
VolumesFrom: []string{
"docker-volumes",
"command-volumes",
"user-volumes",
"system-volumes",
},
},
{
Id: "cloud-init-pre",
Cmd: "--name=cloud-init-pre " +
"--rm " +
"--privileged " +
"--net=host " +
"-e CLOUD_INIT_NETWORK=false " +
"--volumes-from=command-volumes " +
"--volumes-from=system-volumes " +
"cloudinit",
ReloadConfig: true,
"cloud-init-pre": {
Image: "cloudinit",
Privileged: true,
Net: "host",
Labels: []string{
RELOAD_CONFIG + "=true",
DETACH + "=false",
},
Environment: []string{
"CLOUD_INIT_NETWORK=false",
},
VolumesFrom: []string{
"command-volumes",
"system-volumes",
},
},
{
Id: "network",
Cmd: "--name=network " +
"--rm " +
"--cap-add=NET_ADMIN " +
"--net=host " +
"--volumes-from=command-volumes " +
"--volumes-from=system-volumes " +
"network": {
Image: "network",
CapAdd: []string{
"NET_ADMIN",
},
Net: "host",
Labels: []string{
DETACH + "=false",
},
Links: []string{
"cloud-init-pre",
},
VolumesFrom: []string{
"command-volumes",
"system-volumes",
},
},
"cloud-init": {
Image: "cloudinit",
Privileged: true,
Labels: []string{
RELOAD_CONFIG + "=true",
DETACH + "=false",
},
Net: "host",
Links: []string{
"cloud-init-pre",
"network",
},
VolumesFrom: []string{
"command-volumes",
"system-volumes",
},
},
{
Id: "cloud-init",
Cmd: "--name=cloud-init " +
"--rm " +
"--privileged " +
"--net=host " +
"--volumes-from=command-volumes " +
"--volumes-from=system-volumes " +
"cloudinit",
ReloadConfig: true,
"ntp": {
Image: "ntp",
Privileged: true,
Net: "host",
Links: []string{
"cloud-init",
"network",
},
},
{
Id: "ntp",
Cmd: "--name=ntp " +
"--rm " +
"-d " +
"--privileged " +
"--net=host " +
"ntp",
"syslog": {
Image: "syslog",
Privileged: true,
Net: "host",
Links: []string{
"cloud-init",
"network",
},
VolumesFrom: []string{
"system-volumes",
},
},
{
Id: "syslog",
Cmd: "--name=syslog " +
"-d " +
"--rm " +
"--privileged " +
"--net=host " +
"--ipc=host " +
"--pid=host " +
"--volumes-from=system-volumes " +
"syslog",
"userdocker": {
Image: "userdocker",
Privileged: true,
Pid: "host",
Ipc: "host",
Net: "host",
Links: []string{
"network",
},
VolumesFrom: []string{
"all-volumes",
},
},
{
Id: "userdocker",
Cmd: "--name=userdocker " +
"-d " +
"--rm " +
"--restart=always " +
"--ipc=host " +
"--pid=host " +
"--net=host " +
"--privileged " +
"--volumes-from=all-volumes " +
"userdocker",
},
{
Id: "console",
Cmd: "--name=console " +
"-d " +
"--rm " +
"--privileged " +
"--volumes-from=all-volumes " +
"--restart=always " +
"--ipc=host " +
"--net=host " +
"--pid=host " +
"console",
"console": {
Image: "console",
Privileged: true,
Links: []string{
"cloud-init",
},
VolumesFrom: []string{
"all-volumes",
},
Restart: "always",
Pid: "host",
Ipc: "host",
Net: "host",
},
},
EnabledAddons: []string{},
Addons: map[string]Config{
"ubuntu-console": {
SystemContainers: []ContainerConfig{
{
Id: "console",
Cmd: "--name=ubuntu-console " +
"-d " +
"--rm " +
"--privileged " +
"--volumes-from=all-volumes " +
"--restart=always " +
"--ipc=host " +
"--net=host " +
"--pid=host " +
"rancher/ubuntuconsole:" + VERSION,
SystemContainers: map[string]*project.ServiceConfig{
"console": {
Image: "rancher/ubuntuconsole:" + VERSION,
Privileged: true,
Labels: []string{
DETACH + "=true",
},
Links: []string{
"cloud-init",
},
VolumesFrom: []string{
"all-volumes",
},
Restart: "always",
Pid: "host",
Ipc: "host",
Net: "host",
},
},
},

View File

@@ -1,5 +1,7 @@
package config
import "github.com/rancherio/rancher-compose/project"
const (
CONSOLE_CONTAINER = "console"
DOCKER_BIN = "/usr/bin/docker"
@@ -12,6 +14,14 @@ const (
USER_INIT = "/sbin/init-user"
MODULES_ARCHIVE = "/modules.tar"
DEBUG = false
LABEL = "label"
HASH = "io.rancher.os.hash"
ID = "io.rancher.os.id"
DETACH = "io.rancher.os.detach"
REMOVE = "io.rancher.os.remove"
CREATE_ONLY = "io.rancher.os.createonly"
RELOAD_CONFIG = "io.rancher.os.reloadconfig"
)
var (
@@ -22,30 +32,31 @@ var (
)
type ContainerConfig struct {
Id string `yaml:"id,omitempty"`
Cmd string `yaml:"run,omitempty"`
MigrateVolumes bool `yaml:"migrate_volumes,omitempty"`
ReloadConfig bool `yaml:"reload_config,omitempty"`
CreateOnly bool `yaml:create_only,omitempty`
Id string `yaml:"id,omitempty"`
Cmd string `yaml:"run,omitempty"`
MigrateVolumes bool `yaml:"migrate_volumes,omitempty"`
ReloadConfig bool `yaml:"reload_config,omitempty"`
CreateOnly bool `yaml:create_only,omitempty`
Service *project.ServiceConfig `yaml:service,omitempty`
}
type Config struct {
Addons map[string]Config `yaml:"addons,omitempty"`
BootstrapContainers []ContainerConfig `yaml:"bootstrap_containers,omitempty"`
CloudInit CloudInit `yaml:"cloud_init,omitempty"`
Console ConsoleConfig `yaml:"console,omitempty"`
Debug bool `yaml:"debug,omitempty"`
Disable []string `yaml:"disable,omitempty"`
EnabledAddons []string `yaml:"enabled_addons,omitempty"`
Modules []string `yaml:"modules,omitempty"`
Network NetworkConfig `yaml:"network,omitempty"`
Ssh SshConfig `yaml:"ssh,omitempty"`
State StateConfig `yaml:"state,omitempty"`
SystemContainers []ContainerConfig `yaml:"system_containers,omitempty"`
SystemDocker DockerConfig `yaml:"system_docker,omitempty"`
Upgrade UpgradeConfig `yaml:"upgrade,omitempty"`
UserContainers []ContainerConfig `yaml:"user_containers,omitempty"`
UserDocker DockerConfig `yaml:"user_docker,omitempty"`
Addons map[string]Config `yaml:"addons,omitempty"`
BootstrapContainers map[string]*project.ServiceConfig `yaml:"bootstrap_containers,omitempty"`
CloudInit CloudInit `yaml:"cloud_init,omitempty"`
Console ConsoleConfig `yaml:"console,omitempty"`
Debug bool `yaml:"debug,omitempty"`
//Disable []string `yaml:"disable,omitempty"`
EnabledAddons []string `yaml:"enabled_addons,omitempty"`
Modules []string `yaml:"modules,omitempty"`
Network NetworkConfig `yaml:"network,omitempty"`
Ssh SshConfig `yaml:"ssh,omitempty"`
State StateConfig `yaml:"state,omitempty"`
SystemContainers map[string]*project.ServiceConfig `yaml:"system_containers,omitempty"`
SystemDocker DockerConfig `yaml:"system_docker,omitempty"`
Upgrade UpgradeConfig `yaml:"upgrade,omitempty"`
UserContainers []ContainerConfig `yaml:"user_containers,omitempty"`
UserDocker DockerConfig `yaml:"user_docker,omitempty"`
}
type ConsoleConfig struct {