1
0
mirror of https://github.com/rancher/os.git synced 2025-07-02 09:41:47 +00:00

Move more commands to subcommands of ros

This commit is contained in:
Josh Curl 2016-10-20 11:58:34 -07:00
parent b767e5e1a7
commit 21fb3ebfa9
No known key found for this signature in database
GPG Key ID: 82B504B9BCCFA677
10 changed files with 78 additions and 59 deletions

View File

@ -44,6 +44,13 @@ func Main() {
HideHelp: true, HideHelp: true,
Subcommands: consoleSubcommands(), Subcommands: consoleSubcommands(),
}, },
{
Name: "console-init",
Hidden: true,
HideHelp: true,
SkipFlagParsing: true,
Action: consoleInitAction,
},
{ {
Name: "dev", Name: "dev",
Hidden: true, Hidden: true,
@ -51,6 +58,13 @@ func Main() {
SkipFlagParsing: true, SkipFlagParsing: true,
Action: devAction, Action: devAction,
}, },
{
Name: "docker-init",
Hidden: true,
HideHelp: true,
SkipFlagParsing: true,
Action: dockerInitAction,
},
{ {
Name: "engine", Name: "engine",
Usage: "manage which Docker engine is used", Usage: "manage which Docker engine is used",
@ -85,6 +99,13 @@ func Main() {
SkipFlagParsing: true, SkipFlagParsing: true,
Action: preloadImagesAction, Action: preloadImagesAction,
}, },
{
Name: "switch-console",
Hidden: true,
HideHelp: true,
SkipFlagParsing: true,
Action: switchConsoleAction,
},
{ {
Name: "tls", Name: "tls",
Usage: "setup tls configuration", Usage: "setup tls configuration",
@ -98,6 +119,13 @@ func Main() {
SkipFlagParsing: true, SkipFlagParsing: true,
Action: udevSettleAction, Action: udevSettleAction,
}, },
{
Name: "user-docker",
Hidden: true,
HideHelp: true,
SkipFlagParsing: true,
Action: userDockerAction,
},
installCommand, installCommand,
selinuxCommand(), selinuxCommand(),
} }

View File

@ -83,7 +83,7 @@ func consoleSwitch(c *cli.Context) error {
Labels: map[string]string{ Labels: map[string]string{
config.SCOPE: config.SYSTEM, config.SCOPE: config.SYSTEM,
}, },
Command: []string{"/usr/bin/switch-console", newConsole}, Command: []string{"/usr/bin/ros", "switch-console", newConsole},
VolumesFrom: []string{"all-volumes"}, VolumesFrom: []string{"all-volumes"},
}) })
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package console package control
import ( import (
"bytes" "bytes"
@ -12,6 +12,7 @@ import (
"syscall" "syscall"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/rancher/os/cmd/cloudinitexecute" "github.com/rancher/os/cmd/cloudinitexecute"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"github.com/rancher/os/util" "github.com/rancher/os/util"
@ -29,7 +30,7 @@ type symlink struct {
oldname, newname string oldname, newname string
} }
func Main() { func consoleInitAction(c *cli.Context) error {
cfg := config.LoadConfig() cfg := config.LoadConfig()
if _, err := os.Stat(rancherHome); os.IsNotExist(err) { if _, err := os.Stat(rancherHome); os.IsNotExist(err) {
@ -124,10 +125,10 @@ func Main() {
respawnBinPath, err := exec.LookPath("respawn") respawnBinPath, err := exec.LookPath("respawn")
if err != nil { if err != nil {
log.Fatal(err) return err
} }
log.Fatal(syscall.Exec(respawnBinPath, []string{"respawn", "-f", "/etc/respawn.conf"}, os.Environ())) return syscall.Exec(respawnBinPath, []string{"respawn", "-f", "/etc/respawn.conf"}, os.Environ())
} }
func generateRespawnConf(cmdline string) string { func generateRespawnConf(cmdline string) string {

View File

@ -1,4 +1,4 @@
package dockerinit package control
import ( import (
"fmt" "fmt"
@ -10,18 +10,18 @@ import (
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"github.com/rancher/os/util" "github.com/rancher/os/util"
) )
const ( const (
consoleDone = "/run/console-done" dockerConf = "/var/lib/rancher/conf/docker"
dockerConf = "/var/lib/rancher/conf/docker" dockerDone = "/run/docker-done"
dockerDone = "/run/docker-done" dockerLog = "/var/log/docker.log"
dockerLog = "/var/log/docker.log"
) )
func Main() { func dockerInitAction(c *cli.Context) error {
for { for {
if _, err := os.Stat(consoleDone); err == nil { if _, err := os.Stat(consoleDone); err == nil {
break break
@ -54,7 +54,7 @@ func Main() {
mountInfo, err := ioutil.ReadFile("/proc/self/mountinfo") mountInfo, err := ioutil.ReadFile("/proc/self/mountinfo")
if err != nil { if err != nil {
log.Fatal(err) return err
} }
for _, mount := range strings.Split(string(mountInfo), "\n") { for _, mount := range strings.Split(string(mountInfo), "\n") {
@ -66,7 +66,7 @@ func Main() {
args := []string{ args := []string{
"bash", "bash",
"-c", "-c",
fmt.Sprintf(`[ -e %s ] && source %s; exec /usr/bin/dockerlaunch %s %s $DOCKER_OPTS >> %s 2>&1`, dockerConf, dockerConf, dockerBin, strings.Join(os.Args[1:], " "), dockerLog), fmt.Sprintf(`[ -e %s ] && source %s; exec /usr/bin/dockerlaunch %s %s $DOCKER_OPTS >> %s 2>&1`, dockerConf, dockerConf, dockerBin, strings.Join(c.Args(), " "), dockerLog),
} }
cfg := config.LoadConfig() cfg := config.LoadConfig()
@ -75,5 +75,5 @@ func Main() {
log.Error(err) log.Error(err)
} }
log.Fatal(syscall.Exec("/bin/bash", args, os.Environ())) return syscall.Exec("/bin/bash", args, os.Environ())
} }

View File

@ -16,10 +16,6 @@ import (
"github.com/rancher/os/util/network" "github.com/rancher/os/util/network"
) )
const (
dockerDone = "/run/docker-done"
)
func engineSubcommands() []cli.Command { func engineSubcommands() []cli.Command {
return []cli.Command{ return []cli.Command{
{ {

View File

@ -1,31 +1,32 @@
package switchconsole package control
import ( import (
"os" "errors"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/libcompose/project/options" "github.com/docker/libcompose/project/options"
"github.com/rancher/os/compose" "github.com/rancher/os/compose"
"github.com/rancher/os/config" "github.com/rancher/os/config"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
func Main() { func switchConsoleAction(c *cli.Context) error {
if len(os.Args) != 2 { if len(c.Args()) != 1 {
log.Fatal("Must specify exactly one existing container") return errors.New("Must specify exactly one existing container")
} }
newConsole := os.Args[1] newConsole := c.Args()[0]
cfg := config.LoadConfig() cfg := config.LoadConfig()
project, err := compose.GetProject(cfg, true, false) project, err := compose.GetProject(cfg, true, false)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
if newConsole != "default" { if newConsole != "default" {
if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil { if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil {
log.Fatal(err) return err
} }
} }
@ -36,10 +37,12 @@ func Main() {
if err = project.Up(context.Background(), options.Up{ if err = project.Up(context.Background(), options.Up{
Log: true, Log: true,
}, "console"); err != nil { }, "console"); err != nil {
log.Fatal(err) return err
} }
if err = project.Restart(context.Background(), 10, "docker"); err != nil { if err = project.Restart(context.Background(), 10, "docker"); err != nil {
log.Errorf("Failed to restart Docker: %v", err) log.Errorf("Failed to restart Docker: %v", err)
} }
return nil
} }

View File

@ -1,4 +1,4 @@
package userdocker package control
import ( import (
"io" "io"
@ -13,9 +13,9 @@ import (
"path/filepath" "path/filepath"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
composeClient "github.com/docker/libcompose/docker/client" composeClient "github.com/docker/libcompose/docker/client"
"github.com/docker/libcompose/project" "github.com/docker/libcompose/project"
"github.com/rancher/os/cmd/control"
"github.com/rancher/os/compose" "github.com/rancher/os/compose"
"github.com/rancher/os/config" "github.com/rancher/os/config"
rosDocker "github.com/rancher/os/docker" rosDocker "github.com/rancher/os/docker"
@ -25,24 +25,30 @@ import (
const ( const (
DEFAULT_STORAGE_CONTEXT = "console" DEFAULT_STORAGE_CONTEXT = "console"
DOCKER_PID_FILE = "/var/run/docker.pid" DOCKER_PID_FILE = "/var/run/docker.pid"
DOCKER_COMMAND = "docker-init"
userDocker = "user-docker" userDocker = "user-docker"
sourceDirectory = "/engine" sourceDirectory = "/engine"
destDirectory = "/var/lib/rancher/engine" destDirectory = "/var/lib/rancher/engine"
) )
func Main() { var (
DOCKER_COMMAND = []string{
"ros",
"docker-init",
}
)
func userDockerAction(c *cli.Context) error {
if err := copyBinaries(sourceDirectory, destDirectory); err != nil { if err := copyBinaries(sourceDirectory, destDirectory); err != nil {
log.Fatal(err) return err
} }
if err := syscall.Mount("/host/sys", "/sys", "", syscall.MS_BIND|syscall.MS_REC, ""); err != nil { if err := syscall.Mount("/host/sys", "/sys", "", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
log.Fatal(err) return err
} }
cfg := config.LoadConfig() cfg := config.LoadConfig()
log.Fatal(startDocker(cfg)) return startDocker(cfg)
} }
func copyBinaries(source, dest string) error { func copyBinaries(source, dest string) error {
@ -98,15 +104,15 @@ func copyBinaries(source, dest string) error {
return nil return nil
} }
func writeCerts(cfg *config.CloudConfig) error { func writeConfigCerts(cfg *config.CloudConfig) error {
outDir := control.ServerTlsPath outDir := ServerTlsPath
if err := os.MkdirAll(outDir, 0700); err != nil { if err := os.MkdirAll(outDir, 0700); err != nil {
return err return err
} }
caCertPath := filepath.Join(outDir, control.CaCert) caCertPath := filepath.Join(outDir, CaCert)
caKeyPath := filepath.Join(outDir, control.CaKey) caKeyPath := filepath.Join(outDir, CaKey)
serverCertPath := filepath.Join(outDir, control.ServerCert) serverCertPath := filepath.Join(outDir, ServerCert)
serverKeyPath := filepath.Join(outDir, control.ServerKey) serverKeyPath := filepath.Join(outDir, ServerKey)
if cfg.Rancher.Docker.CACert != "" { if cfg.Rancher.Docker.CACert != "" {
if err := util.WriteFileAtomic(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil { if err := util.WriteFileAtomic(caCertPath, []byte(cfg.Rancher.Docker.CACert), 0400); err != nil {
return err return err
@ -160,7 +166,7 @@ func startDocker(cfg *config.CloudConfig) error {
log.Debugf("User Docker args: %v", args) log.Debugf("User Docker args: %v", args)
if dockerCfg.TLS { if dockerCfg.TLS {
if err := writeCerts(cfg); err != nil { if err := writeConfigCerts(cfg); err != nil {
return err return err
} }
} }
@ -173,7 +179,7 @@ func startDocker(cfg *config.CloudConfig) error {
cmd := []string{"docker-runc", "exec", "--", info.ID, "env"} cmd := []string{"docker-runc", "exec", "--", info.ID, "env"}
log.Info(dockerCfg.AppendEnv()) log.Info(dockerCfg.AppendEnv())
cmd = append(cmd, dockerCfg.AppendEnv()...) cmd = append(cmd, dockerCfg.AppendEnv()...)
cmd = append(cmd, DOCKER_COMMAND) cmd = append(cmd, DOCKER_COMMAND...)
cmd = append(cmd, args...) cmd = append(cmd, args...)
log.Infof("Running %v", cmd) log.Infof("Running %v", cmd)

View File

@ -9,4 +9,3 @@ RUN sed -i 's/rancher:!/rancher:*/g' /etc/shadow && \
echo '## allow password less for docker user' >> /etc/sudoers && \ echo '## allow password less for docker user' >> /etc/sudoers && \
echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers echo 'docker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
COPY prompt.sh /etc/profile.d/ COPY prompt.sh /etc/profile.d/
CMD ["/usr/sbin/console.sh"]

View File

@ -8,16 +8,12 @@ import (
"github.com/rancher/cniglue" "github.com/rancher/cniglue"
"github.com/rancher/os/cmd/cloudinitexecute" "github.com/rancher/os/cmd/cloudinitexecute"
"github.com/rancher/os/cmd/cloudinitsave" "github.com/rancher/os/cmd/cloudinitsave"
"github.com/rancher/os/cmd/console"
"github.com/rancher/os/cmd/control" "github.com/rancher/os/cmd/control"
"github.com/rancher/os/cmd/dockerinit"
"github.com/rancher/os/cmd/network" "github.com/rancher/os/cmd/network"
"github.com/rancher/os/cmd/power" "github.com/rancher/os/cmd/power"
"github.com/rancher/os/cmd/respawn" "github.com/rancher/os/cmd/respawn"
"github.com/rancher/os/cmd/switchconsole"
"github.com/rancher/os/cmd/sysinit" "github.com/rancher/os/cmd/sysinit"
"github.com/rancher/os/cmd/systemdocker" "github.com/rancher/os/cmd/systemdocker"
"github.com/rancher/os/cmd/userdocker"
"github.com/rancher/os/cmd/wait" "github.com/rancher/os/cmd/wait"
"github.com/rancher/os/dfs" "github.com/rancher/os/dfs"
osInit "github.com/rancher/os/init" osInit "github.com/rancher/os/init"
@ -26,10 +22,7 @@ import (
var entrypoints = map[string]func(){ var entrypoints = map[string]func(){
"cloud-init-execute": cloudinitexecute.Main, "cloud-init-execute": cloudinitexecute.Main,
"cloud-init-save": cloudinitsave.Main, "cloud-init-save": cloudinitsave.Main,
"console": console.Main,
"console.sh": console.Main,
"docker": docker.Main, "docker": docker.Main,
"docker-init": dockerinit.Main,
"dockerlaunch": dfs.Main, "dockerlaunch": dfs.Main,
"halt": power.Halt, "halt": power.Halt,
"init": osInit.MainInit, "init": osInit.MainInit,
@ -39,9 +32,7 @@ var entrypoints = map[string]func(){
"respawn": respawn.Main, "respawn": respawn.Main,
"ros-sysinit": sysinit.Main, "ros-sysinit": sysinit.Main,
"shutdown": power.Main, "shutdown": power.Main,
"switch-console": switchconsole.Main,
"system-docker": systemdocker.Main, "system-docker": systemdocker.Main,
"user-docker": userdocker.Main,
"wait-for-docker": wait.Main, "wait-for-docker": wait.Main,
"cni-glue": glue.Main, "cni-glue": glue.Main,
"bridge": bridge.Main, "bridge": bridge.Main,

View File

@ -135,7 +135,6 @@ rancher:
read_only: true read_only: true
volumes: volumes:
- /usr/bin/ros:/usr/bin/dockerlaunch:ro - /usr/bin/ros:/usr/bin/dockerlaunch:ro
- /usr/bin/ros:/usr/bin/user-docker:ro
- /usr/bin/ros:/usr/bin/system-docker:ro - /usr/bin/ros:/usr/bin/system-docker:ro
- /usr/bin/ros:/sbin/poweroff:ro - /usr/bin/ros:/sbin/poweroff:ro
- /usr/bin/ros:/sbin/reboot:ro - /usr/bin/ros:/sbin/reboot:ro
@ -147,13 +146,9 @@ rancher:
- /usr/bin/ros:/usr/bin/cloud-init-save:ro - /usr/bin/ros:/usr/bin/cloud-init-save:ro
- /usr/bin/ros:/usr/sbin/netconf:ro - /usr/bin/ros:/usr/sbin/netconf:ro
- /usr/bin/ros:/usr/sbin/wait-for-docker:ro - /usr/bin/ros:/usr/sbin/wait-for-docker:ro
- /usr/bin/ros:/usr/bin/switch-console:ro
- /usr/bin/ros:/usr/bin/console:ro
- /usr/bin/ros:/usr/sbin/console.sh:ro
- /usr/bin/ros:/usr/sbin/docker-init:ro
console: console:
image: {{.OS_REPO}}/os-console:{{.VERSION}}{{.SUFFIX}} image: {{.OS_REPO}}/os-console:{{.VERSION}}{{.SUFFIX}}
command: console command: ros console-init
labels: labels:
io.rancher.os.scope: system io.rancher.os.scope: system
io.rancher.os.after: network io.rancher.os.after: network
@ -316,7 +311,7 @@ rancher:
{{else -}} {{else -}}
image: {{.OS_REPO}}/os-docker:1.11.2{{.SUFFIX}} image: {{.OS_REPO}}/os-docker:1.11.2{{.SUFFIX}}
{{end -}} {{end -}}
command: /usr/bin/user-docker command: ros user-docker
environment: environment:
- HTTP_PROXY - HTTP_PROXY
- HTTPS_PROXY - HTTPS_PROXY