mirror of
https://github.com/rancher/os.git
synced 2025-09-01 06:40:31 +00:00
Refactor configuration to compose syntax
This commit is contained in:
@@ -8,9 +8,10 @@ import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/rancherio/os/config"
|
||||
"github.com/rancherio/os/util"
|
||||
"github.com/rancherio/rancher-compose/project"
|
||||
)
|
||||
|
||||
func runBootstrapContainers(cfg *config.Config) error {
|
||||
func autoformat(cfg *config.Config) error {
|
||||
if len(cfg.State.Autoformat) == 0 || util.ResolveDevice(cfg.State.Dev) != "" {
|
||||
return nil
|
||||
}
|
||||
@@ -54,24 +55,22 @@ outer:
|
||||
|
||||
if format != "" {
|
||||
log.Infof("Auto formatting : %s", format)
|
||||
return runContainersFrom("", cfg, append([]config.ContainerConfig{
|
||||
{
|
||||
Id: "auto-format",
|
||||
Cmd: "--name auto-format " +
|
||||
"--rm " +
|
||||
"--net=none " +
|
||||
"--privileged " +
|
||||
"autoformat " +
|
||||
format,
|
||||
return runServices("autoformat", cfg, map[string]*project.ServiceConfig{
|
||||
"autoformat": {
|
||||
Net: "none",
|
||||
Privileged: true,
|
||||
Image: "autoformat",
|
||||
Command: format,
|
||||
},
|
||||
}, cfg.BootstrapContainers...))
|
||||
"udev": cfg.BootstrapContainers["udev"],
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func autoformat(cfg *config.Config) error {
|
||||
return runContainersFrom("", cfg, cfg.BootstrapContainers)
|
||||
func runBootstrapContainers(cfg *config.Config) error {
|
||||
return runServices("bootstrap", cfg, cfg.BootstrapContainers)
|
||||
}
|
||||
|
||||
func startDocker(cfg *config.Config) (chan interface{}, error) {
|
||||
|
132
init/sysinit.go
132
init/sysinit.go
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/rancherio/os/config"
|
||||
"github.com/rancherio/os/docker"
|
||||
"github.com/rancherio/os/util"
|
||||
|
||||
"github.com/rancherio/rancher-compose/project"
|
||||
)
|
||||
|
||||
func importImage(client *dockerClient.Client, name, fileName string) error {
|
||||
@@ -107,50 +109,63 @@ func loadImages(cfg *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runContainersFrom(startFrom string, cfg *config.Config, containerConfigs []config.ContainerConfig) error {
|
||||
foundStart := false
|
||||
func runServices(name string, cfg *config.Config, configs map[string]*project.ServiceConfig) error {
|
||||
project := project.NewProject(name, &docker.ContainerFactory{})
|
||||
enabled := make(map[string]bool)
|
||||
|
||||
for i, containerConfig := range containerConfigs {
|
||||
container := docker.NewContainer(config.DOCKER_SYSTEM_HOST, &containerConfig)
|
||||
|
||||
if util.Contains(cfg.Disable, containerConfig.Id) {
|
||||
log.Infof("%s is disabled : %v", containerConfig.Id, cfg.Disable)
|
||||
continue
|
||||
}
|
||||
|
||||
if foundStart || startFrom == "" {
|
||||
|
||||
if containerConfig.CreateOnly {
|
||||
log.Infof("Creating [%d/%d] %s", i+1, len(containerConfigs), containerConfig.Id)
|
||||
container.Create()
|
||||
} else {
|
||||
log.Infof("Running [%d/%d] %s", i+1, len(containerConfigs), containerConfig.Id)
|
||||
container.StartAndWait()
|
||||
}
|
||||
|
||||
if container.Err != nil {
|
||||
log.Errorf("Failed to run %v: %v", containerConfig.Id, container.Err)
|
||||
}
|
||||
|
||||
if containerConfig.ReloadConfig {
|
||||
log.Info("Reloading configuration")
|
||||
err := cfg.Reload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return runContainersFrom(containerConfig.Id, cfg, cfg.SystemContainers)
|
||||
}
|
||||
} else if startFrom == containerConfig.Id {
|
||||
foundStart = true
|
||||
for name, serviceConfig := range configs {
|
||||
if err := project.AddConfig(name, serviceConfig); err != nil {
|
||||
log.Infof("Failed loading service %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
project.ReloadCallback = func() error {
|
||||
err := cfg.Reload()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, addon := range cfg.EnabledAddons {
|
||||
if _, ok := enabled[addon]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if config, ok := cfg.Addons[addon]; ok {
|
||||
for name, s := range config.SystemContainers {
|
||||
if err := project.AddConfig(name, s); err != nil {
|
||||
log.Errorf("Failed to load %s : %v", name, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bytes, err := util.LoadResource(addon)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to load %s : %v", addon, err)
|
||||
continue
|
||||
}
|
||||
|
||||
err = project.Load(bytes)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to load %s : %v", addon, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
enabled[addon] = true
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
err := project.ReloadCallback()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to reload %s : %v", name, err)
|
||||
return err
|
||||
}
|
||||
return project.Up()
|
||||
}
|
||||
|
||||
func runContainers(cfg *config.Config) error {
|
||||
return runContainersFrom("", cfg, cfg.SystemContainers)
|
||||
return runServices("system-init", cfg, cfg.SystemContainers)
|
||||
}
|
||||
|
||||
func tailConsole(cfg *config.Config) error {
|
||||
@@ -163,29 +178,26 @@ func tailConsole(cfg *config.Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, container := range cfg.SystemContainers {
|
||||
if container.Id != config.CONSOLE_CONTAINER {
|
||||
continue
|
||||
}
|
||||
|
||||
c := docker.NewContainer(config.DOCKER_SYSTEM_HOST, &container).Lookup()
|
||||
if c.Err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("Tailing console : %s", c.Name)
|
||||
return client.Logs(dockerClient.LogsOptions{
|
||||
Container: c.Name,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
Follow: true,
|
||||
OutputStream: os.Stdout,
|
||||
ErrorStream: os.Stderr,
|
||||
})
|
||||
console, ok := cfg.SystemContainers[config.CONSOLE_CONTAINER]
|
||||
if !ok {
|
||||
log.Error("Console not found")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Error("Console not found")
|
||||
return nil
|
||||
c := docker.NewContainerFromService(config.DOCKER_SYSTEM_HOST, config.CONSOLE_CONTAINER, console)
|
||||
if c.Err != nil {
|
||||
return c.Err
|
||||
}
|
||||
|
||||
log.Infof("Tailing console : %s", c.Name)
|
||||
return client.Logs(dockerClient.LogsOptions{
|
||||
Container: c.Name,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
Follow: true,
|
||||
OutputStream: os.Stdout,
|
||||
ErrorStream: os.Stderr,
|
||||
})
|
||||
}
|
||||
|
||||
func SysInit() error {
|
||||
@@ -201,6 +213,10 @@ func SysInit() error {
|
||||
syscall.Sync()
|
||||
return nil
|
||||
},
|
||||
func(cfg *config.Config) error {
|
||||
log.Info("RancherOS booted")
|
||||
return nil
|
||||
},
|
||||
tailConsole,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user