mirror of
https://github.com/rancher/os.git
synced 2025-09-09 02:31:36 +00:00
Some refactoring
This commit is contained in:
@@ -12,6 +12,19 @@ import (
|
|||||||
"github.com/rancherio/os/util"
|
"github.com/rancherio/os/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CONSOLE_CONTAINER = "console"
|
||||||
|
DOCKER_BIN = "/usr/bin/docker"
|
||||||
|
DOCKER_SYSTEM_HOST = "unix:///var/run/system-docker.sock"
|
||||||
|
DOCKER_HOST = "unix:///var/run/docker.sock"
|
||||||
|
IMAGES_PATH = "/"
|
||||||
|
IMAGES_PATTERN = "images*.tar"
|
||||||
|
SYS_INIT = "/sbin/init-sys"
|
||||||
|
USER_INIT = "/sbin/init-user"
|
||||||
|
MODULES_ARCHIVE = "/modules.tar"
|
||||||
|
DEBUG = true
|
||||||
|
)
|
||||||
|
|
||||||
type InitFunc func(*Config) error
|
type InitFunc func(*Config) error
|
||||||
|
|
||||||
type ContainerConfig struct {
|
type ContainerConfig struct {
|
||||||
@@ -23,27 +36,23 @@ type ContainerConfig struct {
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
//BootstrapContainers []ContainerConfig `json:"bootstrapContainers,omitempty"`
|
//BootstrapContainers []ContainerConfig `json:"bootstrapContainers,omitempty"`
|
||||||
|
//UserContainers []ContainerConfig `json:"userContainser,omitempty"`
|
||||||
ConsoleContainer string `json:"consoleContainer,omitempty"`
|
ConsoleContainer string `json:"consoleContainer,omitempty"`
|
||||||
Debug bool `json:"debug,omitempty"`
|
Debug bool `json:"debug,omitempty"`
|
||||||
Disable []string `json:"disable,omitempty"`
|
Disable []string `json:"disable,omitempty"`
|
||||||
DockerEndpoint string `json:"dockerEndpoint,omitempty"`
|
|
||||||
Dns []string `json:"dns,omitempty"`
|
Dns []string `json:"dns,omitempty"`
|
||||||
ImagesPath string `json:"ImagesPath,omitempty"`
|
|
||||||
ImagesPattern string `json:"ImagesPattern,omitempty"`
|
|
||||||
ModulesArchive string `json:"modulesArchive,omitempty"`
|
|
||||||
Rescue bool `json:"rescue,omitempty"`
|
Rescue bool `json:"rescue,omitempty"`
|
||||||
RescueContainer ContainerConfig `json:"rescueContainer,omitempty"`
|
RescueContainer ContainerConfig `json:"rescueContainer,omitempty"`
|
||||||
StateDevFSType string `json:"stateDevFsType,omitempty"`
|
State ConfigState `json:"state,omitempty"`
|
||||||
StateDev string `json:"stateDev,omitempty"`
|
|
||||||
StateRequired bool `json:"stateRequired,omitempty"`
|
|
||||||
SysInit string `json:"sysInit,omitempty"`
|
|
||||||
SystemContainers []ContainerConfig `json:"systemContainers,omitempty"`
|
SystemContainers []ContainerConfig `json:"systemContainers,omitempty"`
|
||||||
SystemDockerArgs []string `json:"systemDockerArgs,omitempty"`
|
SystemDockerArgs []string `json:"systemDockerArgs,omitempty"`
|
||||||
UserContainers []ContainerConfig `json:"userContainser,omitempty"`
|
|
||||||
UserInit string `json:"userInit,omitempty"`
|
|
||||||
DockerBin string `json:"dockerBin,omitempty"`
|
|
||||||
Modules []string `json:"modules,omitempty"`
|
Modules []string `json:"modules,omitempty"`
|
||||||
Respawn []string `json:"respawn,omitempty"`
|
}
|
||||||
|
|
||||||
|
type ConfigState struct {
|
||||||
|
FsType string `json:"fsType,omitempty"`
|
||||||
|
Dev string `json:"dev,omitempty"`
|
||||||
|
Required bool `json:"required,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Dump() string {
|
func (c *Config) Dump() string {
|
||||||
|
@@ -2,24 +2,18 @@ package config
|
|||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
ConsoleContainer: "console",
|
Debug: DEBUG,
|
||||||
DockerBin: "/usr/bin/docker",
|
|
||||||
Debug: true,
|
|
||||||
DockerEndpoint: "unix:/var/run/docker.sock",
|
|
||||||
Dns: []string{
|
Dns: []string{
|
||||||
"8.8.8.8",
|
"8.8.8.8",
|
||||||
"8.8.4.4",
|
"8.8.4.4",
|
||||||
},
|
},
|
||||||
ImagesPath: "/",
|
State: ConfigState{
|
||||||
ImagesPattern: "images*.tar",
|
Required: false,
|
||||||
StateRequired: false,
|
Dev: "LABEL=RANCHER_STATE",
|
||||||
StateDev: "LABEL=RANCHER_STATE",
|
FsType: "auto",
|
||||||
StateDevFSType: "auto",
|
},
|
||||||
SysInit: "/sbin/init-sys",
|
|
||||||
SystemDockerArgs: []string{"docker", "-d", "-s", "overlay", "-b", "none"},
|
SystemDockerArgs: []string{"docker", "-d", "-s", "overlay", "-b", "none"},
|
||||||
UserInit: "/sbin/init-user",
|
|
||||||
Modules: []string{},
|
Modules: []string{},
|
||||||
ModulesArchive: "/modules.tar",
|
|
||||||
SystemContainers: []ContainerConfig{
|
SystemContainers: []ContainerConfig{
|
||||||
{
|
{
|
||||||
Cmd: []string{
|
Cmd: []string{
|
||||||
|
@@ -12,11 +12,15 @@ const (
|
|||||||
INTERVAL = 100
|
INTERVAL = 100
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(cfg *config.Config) (*dockerClient.Client, error) {
|
func NewSystemClient() (*dockerClient.Client, error) {
|
||||||
endpoint := "unix:///var/run/docker.sock"
|
return NewClient(config.DOCKER_SYSTEM_HOST)
|
||||||
if cfg != nil {
|
}
|
||||||
endpoint = cfg.DockerEndpoint
|
|
||||||
}
|
func NewDefaultClient() (*dockerClient.Client, error) {
|
||||||
|
return NewClient(config.DOCKER_HOST)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(endpoint string) (*dockerClient.Client, error) {
|
||||||
client, err := dockerClient.NewClient(endpoint)
|
client, err := dockerClient.NewClient(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -28,7 +28,7 @@ type Container struct {
|
|||||||
detach bool
|
detach bool
|
||||||
Config *runconfig.Config
|
Config *runconfig.Config
|
||||||
HostConfig *runconfig.HostConfig
|
HostConfig *runconfig.HostConfig
|
||||||
cfg *config.Config
|
dockerHost string
|
||||||
container *dockerClient.Container
|
container *dockerClient.Container
|
||||||
containerCfg *config.ContainerConfig
|
containerCfg *config.ContainerConfig
|
||||||
}
|
}
|
||||||
@@ -47,16 +47,17 @@ func getHash(containerCfg *config.ContainerConfig) (string, error) {
|
|||||||
return hex.EncodeToString(hash.Sum([]byte{})), nil
|
return hex.EncodeToString(hash.Sum([]byte{})), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartAndWait(cfg *config.Config, containerCfg *config.ContainerConfig) error {
|
func StartAndWait(dockerHost string, containerCfg *config.ContainerConfig) error {
|
||||||
container := NewContainer(cfg, containerCfg).start(true)
|
container := NewContainer(dockerHost, containerCfg).start(true)
|
||||||
return container.Err
|
return container.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(cfg *config.Config, containerCfg *config.ContainerConfig) *Container {
|
func NewContainer(dockerHost string, containerCfg *config.ContainerConfig) *Container {
|
||||||
return &Container{
|
c := &Container{
|
||||||
cfg: cfg,
|
dockerHost: dockerHost,
|
||||||
containerCfg: containerCfg,
|
containerCfg: containerCfg,
|
||||||
}
|
}
|
||||||
|
return c.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) returnErr(err error) *Container {
|
func (c *Container) returnErr(err error) *Container {
|
||||||
@@ -76,7 +77,7 @@ func (c *Container) Lookup() *Container {
|
|||||||
return c.returnErr(err)
|
return c.returnErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := NewClient(c.cfg)
|
client, err := NewClient(c.dockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.returnErr(err)
|
return c.returnErr(err)
|
||||||
}
|
}
|
||||||
@@ -153,7 +154,7 @@ func (c *Container) Stage() *Container {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := NewClient(c.cfg)
|
client, err := NewClient(c.dockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
return c
|
return c
|
||||||
@@ -185,7 +186,7 @@ func (c *Container) Delete() *Container {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := NewClient(c.cfg)
|
client, err := NewClient(c.dockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.returnErr(err)
|
return c.returnErr(err)
|
||||||
}
|
}
|
||||||
@@ -235,7 +236,7 @@ func (c *Container) start(wait bool) *Container {
|
|||||||
return c.returnErr(err)
|
return c.returnErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := NewClient(c.cfg)
|
client, err := NewClient(c.dockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.returnErr(err)
|
return c.returnErr(err)
|
||||||
}
|
}
|
||||||
|
62
init/init.go
62
init/init.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/rancherio/os/config"
|
"github.com/rancherio/os/config"
|
||||||
@@ -123,13 +122,13 @@ func mountCgroups(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func extractModules(cfg *config.Config) error {
|
func extractModules(cfg *config.Config) error {
|
||||||
if _, err := os.Stat(cfg.ModulesArchive); os.IsNotExist(err) {
|
if _, err := os.Stat(config.MODULES_ARCHIVE); os.IsNotExist(err) {
|
||||||
log.Debug("Modules do not exist")
|
log.Debug("Modules do not exist")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Extracting modules")
|
log.Debug("Extracting modules")
|
||||||
return util.ExtractTar(cfg.ModulesArchive, "/")
|
return util.ExtractTar(config.MODULES_ARCHIVE, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func setResolvConf(cfg *config.Config) error {
|
func setResolvConf(cfg *config.Config) error {
|
||||||
@@ -214,62 +213,37 @@ func MainInit() {
|
|||||||
func mountState(cfg *config.Config) error {
|
func mountState(cfg *config.Config) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
dev := util.ResolveDevice(cfg.StateDev)
|
if cfg.State.Dev != "" {
|
||||||
log.Debugf("Mounting state device %s", dev)
|
dev := util.ResolveDevice(cfg.State.Dev)
|
||||||
|
log.Debugf("Mounting state device %s to %s", dev, STATE)
|
||||||
|
|
||||||
fsType := cfg.StateDevFSType
|
fsType := cfg.State.FsType
|
||||||
log.Debugf("FsType has been set to %s", fsType)
|
if fsType == "auto" {
|
||||||
if fsType == "auto" {
|
fsType, err = util.GetFsType(dev)
|
||||||
actualFsType, fsErr := util.GetFsType(dev)
|
|
||||||
if fsErr != nil {
|
|
||||||
return fsErr
|
|
||||||
}
|
}
|
||||||
fsType = actualFsType
|
|
||||||
}
|
|
||||||
err = util.Mount(dev, STATE, fsType, "")
|
|
||||||
|
|
||||||
if err != nil {
|
if err == nil {
|
||||||
if cfg.StateRequired {
|
log.Debugf("FsType has been set to %s", fsType)
|
||||||
return err
|
err = util.Mount(dev, STATE, fsType, "")
|
||||||
} else {
|
|
||||||
log.Debugf("State will not be persisted")
|
|
||||||
err = util.Mount("none", STATE, "tmpfs", "")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && cfg.State.Required {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//for _, i := range []string{"docker", "images"} {
|
if err != nil || cfg.State.Dev == "" {
|
||||||
// dir := path.Join(STATE, i)
|
log.Debugf("State will not be persisted")
|
||||||
// err = os.MkdirAll(dir, 0755)
|
err = util.Mount("none", STATE, "tmpfs", "")
|
||||||
// if err != nil {
|
}
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//log.Debugf("Bind mounting %s to %s", path.Join(STATE, "docker"), DOCKER)
|
return err
|
||||||
//err = util.Mount(path.Join(STATE, "docker"), DOCKER, "", "bind")
|
|
||||||
//if err != nil && cfg.StateRequired {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func pause(cfg *config.Config) error {
|
|
||||||
time.Sleep(5 + time.Minute)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunInit() error {
|
func RunInit() error {
|
||||||
var cfg config.Config
|
var cfg config.Config
|
||||||
|
|
||||||
os.Setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
|
os.Setenv("PATH", "/sbin:/usr/bin")
|
||||||
os.Setenv("DOCKER_RAMDISK", "true")
|
os.Setenv("DOCKER_RAMDISK", "true")
|
||||||
|
|
||||||
initFuncs := []config.InitFunc{
|
initFuncs := []config.InitFunc{
|
||||||
|
@@ -131,6 +131,7 @@ download()
|
|||||||
local hash=$1
|
local hash=$1
|
||||||
|
|
||||||
if ! check $hash $file; then
|
if ! check $hash $file; then
|
||||||
|
echo Downloading $url to $file
|
||||||
curl -sL $url > $file
|
curl -sL $url > $file
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
FROM base
|
FROM base
|
||||||
RUN rm /sbin/poweroff /sbin/reboot /sbin/halt
|
RUN rm /sbin/poweroff /sbin/reboot /sbin/halt
|
||||||
|
RUN sed -i '/^root/s!/bin/sh!/bin/bash!' /etc/passwd
|
||||||
|
RUN echo -e 'RancherOS\n' > /etc/issue
|
||||||
COPY scripts/dockerimages/scripts/console.sh /
|
COPY scripts/dockerimages/scripts/console.sh /
|
||||||
CMD ["/console.sh"]
|
CMD ["/console.sh"]
|
||||||
|
@@ -41,6 +41,6 @@ else
|
|||||||
zcat ${HD_GZ} > ${HD}
|
zcat ${HD_GZ} > ${HD}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
qemu-system-x86_64 -kernel ${KERNEL} -initrd ${INITRD_TEST} -m 1024 -net nic,vlan=0,model=virtio -net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher -drive if=virtio,file=${HD} -machine accel=kvm -cpu host -smp 4 -append "x $@"
|
qemu-system-x86_64 -serial stdio -kernel ${KERNEL} -initrd ${INITRD_TEST} -m 1024 -net nic,vlan=0,model=virtio -net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher -drive if=virtio,file=${HD} -machine accel=kvm -cpu host -smp 4 -append "console=ttyS0 $@"
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
@@ -47,11 +47,11 @@ func hasImage(name string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func findImages(cfg *config.Config) ([]string, error) {
|
func findImages(cfg *config.Config) ([]string, error) {
|
||||||
log.Debugf("Looking for images at %s", cfg.ImagesPath)
|
log.Debugf("Looking for images at %s", config.IMAGES_PATH)
|
||||||
|
|
||||||
result := []string{}
|
result := []string{}
|
||||||
|
|
||||||
dir, err := os.Open(cfg.ImagesPath)
|
dir, err := os.Open(config.IMAGES_PATH)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
log.Debugf("Not loading images, %s does not exist")
|
log.Debugf("Not loading images, %s does not exist")
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -68,7 +68,7 @@ func findImages(cfg *config.Config) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, fileName := range files {
|
for _, fileName := range files {
|
||||||
if ok, _ := path.Match(cfg.ImagesPattern, fileName); ok {
|
if ok, _ := path.Match(config.IMAGES_PATTERN, fileName); ok {
|
||||||
log.Debugf("Found %s", fileName)
|
log.Debugf("Found %s", fileName)
|
||||||
result = append(result, fileName)
|
result = append(result, fileName)
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,7 @@ func loadImages(cfg *config.Config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := docker.NewClient(cfg)
|
client, err := docker.NewDefaultClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ func loadImages(cfg *config.Config) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
inputFileName := path.Join(cfg.ImagesPath, image)
|
inputFileName := path.Join(config.IMAGES_PATH, image)
|
||||||
input, err := os.Open(inputFileName)
|
input, err := os.Open(inputFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -122,7 +122,7 @@ func runContainers(cfg *config.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, containerConfig := range containerConfigs {
|
for _, containerConfig := range containerConfigs {
|
||||||
container := docker.NewContainer(cfg, &containerConfig)
|
container := docker.NewContainer(config.DOCKER_HOST, &containerConfig)
|
||||||
container.Parse()
|
container.Parse()
|
||||||
|
|
||||||
if util.Contains(cfg.Disable, containerConfig.Id) {
|
if util.Contains(cfg.Disable, containerConfig.Id) {
|
||||||
|
Reference in New Issue
Block a user