1
0
mirror of https://github.com/rancher/os.git synced 2025-09-01 06:40:31 +00:00

fix current console detection

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit
2017-07-14 18:05:57 +10:00
parent d9d3c2b0d8
commit 6fcc1e3967
5 changed files with 44 additions and 24 deletions

View File

@@ -6,7 +6,6 @@ import (
"os/exec"
"runtime"
"strings"
"syscall"
"github.com/codegangsta/cli"
"github.com/rancher/os/config"
@@ -58,11 +57,12 @@ func autologinAction(c *cli.Context) error {
banner = strings.Replace(banner, "\\l", tty, -1)
banner = strings.Replace(banner, "\\\\", "\\", -1)
banner = banner + "\n"
banner = banner + "Autologin " + CurrentConsole() + "\n"
fmt.Printf(banner)
loginBin := ""
args := []string{}
if cfg.Rancher.Console == "Acentos" || cfg.Rancher.Console == "Afedora" {
if CurrentConsole() == "centos" || CurrentConsole() == "fedora" {
// For some reason, centos and fedora ttyS0 and tty1 don't work with `login -f rancher`
// until I make time to read their source, lets just give us a way to get work done
loginBin = "bash"
@@ -78,18 +78,18 @@ func autologinAction(c *cli.Context) error {
fmt.Printf("error finding %s in path: %s", cmd.Args[0], err)
return err
}
os.Setenv("PS1", `[`+cfg.Rancher.Console+`: \l \u@\h \W]\$`)
os.Setenv("PS1", `[`+CurrentConsole()+`: \l \u@\h \W]\$`)
return syscall.Exec(loginBinPath, args, os.Environ())
// Causes all sorts of issues
//return syscall.Exec(loginBinPath, args, os.Environ())
cmd = exec.Command(loginBinPath, args...)
cmd.Env = os.Environ()
// cmd = exec.Command(loginBinPath, args...)
// cmd.Env = os.Environ()
// cmd.Stderr = os.Stderr
// cmd.Stdout = os.Stdout
// cmd.Stdin = os.Stdin
// if err := cmd.Run(); err != nil {
// log.Errorf("\nError starting %s: %s", cmd.Args[0], err)
// }
// return nil
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
log.Errorf("\nError starting %s: %s", cmd.Args[0], err)
}
return nil
}

View File

@@ -2,18 +2,19 @@ package control
import (
"fmt"
"io/ioutil"
"sort"
"strings"
"golang.org/x/net/context"
"github.com/codegangsta/cli"
"github.com/docker/docker/reference"
composeConfig "github.com/docker/libcompose/config"
"github.com/docker/libcompose/project/options"
"github.com/rancher/os/cmd/control/service"
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/docker"
"github.com/rancher/os/log"
"github.com/rancher/os/util"
"github.com/rancher/os/util/network"
@@ -57,7 +58,7 @@ func consoleSwitch(c *cli.Context) error {
cfg := config.LoadConfig()
validateConsole(newConsole, cfg)
if newConsole == currentConsole() {
if newConsole == CurrentConsole() {
log.Warnf("Console is already set to %s", newConsole)
}
@@ -127,10 +128,10 @@ func consoleEnable(c *cli.Context) error {
func consoleList(c *cli.Context) error {
cfg := config.LoadConfig()
consoles := availableConsoles(cfg)
currentConsole := currentConsole()
CurrentConsole := CurrentConsole()
for _, console := range consoles {
if console == currentConsole {
if console == CurrentConsole {
fmt.Printf("current %s\n", console)
} else if console == cfg.Rancher.Console {
fmt.Printf("enabled %s\n", console)
@@ -159,12 +160,28 @@ func availableConsoles(cfg *config.CloudConfig) []string {
return consoles
}
func currentConsole() (console string) {
consoleBytes, err := ioutil.ReadFile("/run/console-done")
if err == nil {
console = strings.TrimSpace(string(consoleBytes))
} else {
// CurrentConsole gets the name of the console that's running
func CurrentConsole() (console string) {
// TODO: replace this docker container look up with a libcompose service lookup?
// sudo system-docker inspect --format "{{.Config.Image}}" console
client, err := docker.NewSystemClient()
if err != nil {
log.Warnf("Failed to detect current console: %v", err)
}
return
}
info, err := client.ContainerInspect(context.Background(), "console")
if err != nil {
log.Warnf("Failed to detect current console: %v", err)
return
}
// parse image name, then remove os- prefix and the console suffix
image, err := reference.ParseNamed(info.Config.Image)
if err != nil {
log.Warnf("Failed to detect current console(%s): %v", info.Config.Image, err)
return
}
console = strings.TrimPrefix(strings.TrimSuffix(image.Name(), "console"), "rancher/os-")
return
}

View File

@@ -126,7 +126,7 @@ func consoleInitFunc() error {
log.Error(err)
}
if err := ioutil.WriteFile(consoleDone, []byte(cfg.Rancher.Console), 0644); err != nil {
if err := ioutil.WriteFile(consoleDone, []byte(CurrentConsole()), 0644); err != nil {
log.Error(err)
}

View File

@@ -22,6 +22,7 @@ const (
)
func dockerInitAction(c *cli.Context) error {
// TODO: this should be replaced by a "Console ready event watcher"
for {
if _, err := os.Stat(consoleDone); err == nil {
break

View File

@@ -10,6 +10,8 @@ import (
)
var (
// TODO: move this into the os-config file so it can be customised.
recoveryDockerService = composeConfig.ServiceConfigV1{
Image: config.OsBase,
Command: yaml.Command{