Skip to content

Commit

Permalink
🌱 Webui enhancements (kairos-io#620)
Browse files Browse the repository at this point in the history
* 🌱 Make sure webui starts on alpine

Also drop to shell when there are no providers

Signed-off-by: mudler <mudler@c3os.io>

* 🌱 Suppress verbose logging on tty

Signed-off-by: mudler <mudler@c3os.io>

* 🌱 Print WebUI address

Signed-off-by: mudler <mudler@c3os.io>

* 🎨 Update banner

Signed-off-by: mudler <mudler@c3os.io>

* 🌱 Refactor, display also interfaces

Signed-off-by: mudler <mudler@c3os.io>

* 🌱 Address feedback from review

Signed-off-by: mudler <mudler@c3os.io>

Signed-off-by: mudler <mudler@c3os.io>
  • Loading branch information
mudler authored Jan 8, 2023
1 parent e4cd1a2 commit 1bc5fa6
Show file tree
Hide file tree
Showing 9 changed files with 1,183 additions and 750 deletions.
5 changes: 5 additions & 0 deletions internal/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type WebUI struct {
Disable bool `yaml:"disable"`
ListenAddress string `yaml:"listen_address"`
}

func (w WebUI) HasAddress() bool {
return w.ListenAddress != ""
}

type Config struct {
Fast bool `yaml:"fast,omitempty"`
WebUI WebUI `yaml:"webui"`
Expand Down
1,813 changes: 1,077 additions & 736 deletions internal/agent/iconunix.go

Large diffs are not rendered by default.

38 changes: 35 additions & 3 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -38,6 +39,28 @@ func optsToArgs(options map[string]string) (res []string) {
return
}

func displayInfo(agentConfig *Config) {
fmt.Println("--------------------------")
fmt.Println("No providers found, dropping to a shell. \n -- For instructions on how to install manually, see: https://kairos.io/docs/installation/manual/")
if !agentConfig.WebUI.Disable {
if !agentConfig.WebUI.HasAddress() {
ips := machine.LocalIPs()
if len(ips) > 0 {
fmt.Print("WebUI installer running at : ")
for _, ip := range ips {
fmt.Printf("%s%s ", ip, config.DefaultWebUIListenAddress)
}
fmt.Print("\n")
}
} else {
fmt.Printf("WebUI installer running at : %s\n", agentConfig.WebUI.ListenAddress)
}

ifaces := machine.Interfaces()
fmt.Printf("Network Interfaces: %s\n", strings.Join(ifaces, " "))
}
}

func ManualInstall(config string, options map[string]string) error {
dat, err := os.ReadFile(config)
if err != nil {
Expand Down Expand Up @@ -88,7 +111,7 @@ func Install(dir ...string) error {

// Reads config, and if present and offline is defined,
// runs the installation
cc, err := config.Scan(config.Directories(dir...), config.MergeBootLine)
cc, err := config.Scan(config.Directories(dir...), config.MergeBootLine, config.NoLogs)
if err == nil && cc.Install != nil && cc.Install.Auto {
r["cc"] = cc.String()
r["device"] = cc.Install.Device
Expand All @@ -110,14 +133,23 @@ func Install(dir ...string) error {
fmt.Printf("- config not found in the system: %s", err.Error())
}

_, err = bus.Manager.Publish(events.EventChallenge, events.EventPayload{Config: cc.String()})
agentConfig, err := LoadConfig()
if err != nil {
return err
}

// try to clear screen
cmd.ClearScreen()
cmd.PrintBranding(DefaultBanner)

agentConfig, err := LoadConfig()
// If there are no providers registered, we enter a shell for manual installation and print information about
// the webUI
if !bus.Manager.HasRegisteredPlugins() {
displayInfo(agentConfig)
return utils.Shell().Run()
}

_, err = bus.Manager.Publish(events.EventChallenge, events.EventPayload{Config: cc.String()})
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (b *Bus) LoadProviders() {
b.Manager.Autoload("agent-provider", "/system/providers", "/usr/local/system/providers", wd).Register()
}

func (b *Bus) HasRegisteredPlugins() bool {
return len(b.Plugins) > 0
}

func (b *Bus) Initialize() {
if b.registered {
return
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ func PrintText(f string, banner string) {
f)
}

func ClearScreen() {
fmt.Print("\033c")
}

func PrintBranding(b []byte) {
brandingFile := kairos.BrandingFile("banner")
if _, err := os.Stat(brandingFile); err == nil {
f, err := os.ReadFile(brandingFile)
if err == nil {
fmt.Println(string(f))
return
}

}
utils.PrintBanner(b)
}
3 changes: 2 additions & 1 deletion internal/webui/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/kairos-io/kairos/internal/agent"
"github.com/kairos-io/kairos/pkg/config"
"github.com/labstack/echo/v4"
process "github.com/mudler/go-processmanager"
"github.com/nxadm/tail"
Expand Down Expand Up @@ -134,7 +135,7 @@ func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c
func Start(ctx context.Context) error {

s := state{}
listen := ":8080"
listen := config.DefaultWebUIListenAddress

ec := echo.New()
assetHandler := http.FileServer(getFileSystem())
Expand Down
18 changes: 9 additions & 9 deletions overlay/files/system/oem/04_installer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ stages:
- systemctl disable getty@tty1
- systemctl enable kairos
- systemctl enable kairos-webui
- if: |
[ "$(kairos-agent state get boot)" == "livecd_boot" ]
commands:
# TODO: This is sub-optimal, we need the state to tell also what init we should run.
# At the moment it is harmless to run in both worlds
- systemctl enable kairos-webui
- rc-service kairos-webui start
# Starts installer on boot in Alpine and openRC based
- if: |
cat /proc/cmdline | grep -q "nodepair.enable"
Expand All @@ -25,11 +18,18 @@ stages:
commands:
- systemctl disable getty@tty1
- systemctl enable kairos-interactive
- systemctl enable kairos-webui
# Starts installer on boot in Alpine and openRC based
- if: |
cat /proc/cmdline | grep -q "interactive-install"
commands:
- sed -i -e 's/tty1.*//g' /etc/inittab
- echo "tty1::respawn:/usr/bin/kairos-agent interactive-install --shell" >> /etc/inittab
- rc-service kairos-webui start
boot:
- if: |
[ "$(kairos-agent state get boot)" == "livecd_boot" ] && [ -f "/sbin/openrc" ]
commands:
- rc-service kairos-webui start
- if: |
[ "$(kairos-agent state get boot)" == "livecd_boot" ] && [ -d "/usr/share/systemd" ]
commands:
- systemctl start kairos-webui
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"gopkg.in/yaml.v3"
)

const DefaultWebUIListenAddress = ":8080"

type Install struct {
Auto bool `yaml:"auto,omitempty"`
Reboot bool `yaml:"reboot,omitempty"`
Expand Down
44 changes: 44 additions & 0 deletions pkg/machine/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package machine

import (
"net"
)

func Interfaces() (in []string) {
ifaces, err := net.Interfaces()
if err != nil {
return
}
for _, i := range ifaces {
if i.Flags == net.FlagLoopback {
continue
}
in = append(in, i.Name)
}
return
}

func LocalIPs() (ips []string) {
ifaces, err := net.Interfaces()
if err != nil {
return
}
for _, i := range ifaces {
if i.Flags == net.FlagLoopback {
continue
}
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, a := range addrs {
ip, _, err := net.ParseCIDR(a.String())
if err != nil {
continue
}

ips = append(ips, ip.String())
}
}
return
}

0 comments on commit 1bc5fa6

Please sign in to comment.