2015-03-18 13:21:32 +00:00
|
|
|
package init
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"syscall"
|
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-18 13:21:32 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-05-24 00:21:28 +00:00
|
|
|
"github.com/docker/libcompose/project/options"
|
2016-10-17 21:47:44 +00:00
|
|
|
"github.com/rancher/os/cmd/control"
|
2015-10-12 11:50:17 +00:00
|
|
|
"github.com/rancher/os/compose"
|
|
|
|
"github.com/rancher/os/config"
|
|
|
|
"github.com/rancher/os/docker"
|
2015-03-18 13:21:32 +00:00
|
|
|
)
|
|
|
|
|
2016-10-17 21:47:44 +00:00
|
|
|
const (
|
|
|
|
systemImagesPreloadDirectory = "/var/lib/rancher/preload/system-docker"
|
|
|
|
)
|
|
|
|
|
2015-03-18 13:21:32 +00:00
|
|
|
func hasImage(name string) bool {
|
|
|
|
stamp := path.Join(STATE, name)
|
|
|
|
if _, err := os.Stat(stamp); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
func findImages(cfg *config.CloudConfig) ([]string, error) {
|
2015-03-18 13:21:32 +00:00
|
|
|
log.Debugf("Looking for images at %s", config.IMAGES_PATH)
|
|
|
|
|
|
|
|
result := []string{}
|
|
|
|
|
|
|
|
dir, err := os.Open(config.IMAGES_PATH)
|
|
|
|
if os.IsNotExist(err) {
|
2016-01-15 13:18:30 +00:00
|
|
|
log.Debugf("Not loading images, %s does not exist", config.IMAGES_PATH)
|
2015-03-18 13:21:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
func loadImages(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
2015-03-18 13:21:32 +00:00
|
|
|
images, err := findImages(cfg)
|
|
|
|
if err != nil || len(images) == 0 {
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, err
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := docker.NewSystemClient()
|
|
|
|
if err != nil {
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, err
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, image := range images {
|
|
|
|
if hasImage(image) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
inputFileName := path.Join(config.IMAGES_PATH, image)
|
|
|
|
input, err := os.Open(inputFileName)
|
|
|
|
if err != nil {
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, err
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer input.Close()
|
|
|
|
|
|
|
|
log.Infof("Loading images from %s", inputFileName)
|
2016-05-24 00:21:28 +00:00
|
|
|
if _, err = client.ImageLoad(context.Background(), input, true); err != nil {
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, err
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
2016-02-03 12:24:12 +00:00
|
|
|
|
|
|
|
log.Infof("Done loading images from %s", inputFileName)
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, nil
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SysInit() error {
|
2016-06-02 01:41:55 +00:00
|
|
|
cfg := config.LoadConfig()
|
2015-03-18 13:21:32 +00:00
|
|
|
|
2016-10-17 21:47:44 +00:00
|
|
|
if err := control.PreloadImages(docker.NewSystemClient, systemImagesPreloadDirectory); err != nil {
|
|
|
|
log.Errorf("Failed to preload System Docker images: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:41:55 +00:00
|
|
|
_, err := config.ChainCfgFuncs(cfg,
|
2015-03-18 13:21:32 +00:00
|
|
|
loadImages,
|
2015-09-23 11:36:28 +00:00
|
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
2016-06-12 19:02:07 +00:00
|
|
|
p, err := compose.GetProject(cfg, false, true)
|
2015-10-02 07:57:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
2016-05-24 00:21:28 +00:00
|
|
|
return cfg, p.Up(context.Background(), options.Up{
|
|
|
|
Create: options.Create{
|
|
|
|
NoRecreate: true,
|
|
|
|
},
|
2016-06-06 22:31:34 +00:00
|
|
|
Log: cfg.Rancher.Log,
|
2016-05-24 00:21:28 +00:00
|
|
|
})
|
2015-08-04 21:45:38 +00:00
|
|
|
},
|
2015-09-23 11:36:28 +00:00
|
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
2015-03-18 13:21:32 +00:00
|
|
|
syscall.Sync()
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, nil
|
2015-03-18 13:21:32 +00:00
|
|
|
},
|
2015-09-23 11:36:28 +00:00
|
|
|
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
2015-03-30 21:19:21 +00:00
|
|
|
log.Infof("RancherOS %s started", config.VERSION)
|
2015-09-23 11:36:28 +00:00
|
|
|
return cfg, nil
|
|
|
|
})
|
|
|
|
return err
|
2015-03-18 13:21:32 +00:00
|
|
|
}
|