mirror of
https://github.com/rancher/os.git
synced 2025-06-23 05:27:03 +00:00
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
113 lines
2.2 KiB
Go
113 lines
2.2 KiB
Go
package init
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"syscall"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
dockerClient "github.com/fsouza/go-dockerclient"
|
|
"github.com/rancherio/os/compose"
|
|
"github.com/rancherio/os/config"
|
|
"github.com/rancherio/os/docker"
|
|
)
|
|
|
|
func hasImage(name string) bool {
|
|
stamp := path.Join(STATE, name)
|
|
if _, err := os.Stat(stamp); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func findImages(cfg *config.CloudConfig) ([]string, error) {
|
|
log.Debugf("Looking for images at %s", config.IMAGES_PATH)
|
|
|
|
result := []string{}
|
|
|
|
dir, err := os.Open(config.IMAGES_PATH)
|
|
if os.IsNotExist(err) {
|
|
log.Debugf("Not loading images, %s does not exist")
|
|
return result, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
files, err := dir.Readdirnames(0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, fileName := range files {
|
|
if ok, _ := path.Match(config.IMAGES_PATTERN, fileName); ok {
|
|
log.Debugf("Found %s", fileName)
|
|
result = append(result, fileName)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func loadImages(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
|
images, err := findImages(cfg)
|
|
if err != nil || len(images) == 0 {
|
|
return cfg, err
|
|
}
|
|
|
|
client, err := docker.NewSystemClient()
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
for _, image := range images {
|
|
if hasImage(image) {
|
|
continue
|
|
}
|
|
|
|
inputFileName := path.Join(config.IMAGES_PATH, image)
|
|
input, err := os.Open(inputFileName)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
defer input.Close()
|
|
|
|
log.Infof("Loading images from %s", inputFileName)
|
|
err = client.LoadImage(dockerClient.LoadImageOptions{
|
|
InputStream: input,
|
|
})
|
|
log.Infof("Done loading images from %s", inputFileName)
|
|
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func SysInit() error {
|
|
cfg, err := config.LoadConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = config.ChainCfgFuncs(cfg,
|
|
loadImages,
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
|
return cfg, compose.RunServices(cfg)
|
|
},
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
|
syscall.Sync()
|
|
return cfg, nil
|
|
},
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
|
log.Infof("RancherOS %s started", config.VERSION)
|
|
return cfg, nil
|
|
})
|
|
return err
|
|
}
|