mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-01 17:16:40 +00:00
art: Refactor agent code
Move out cmd pieces and split into its own package. Also make sure we reload plugins before bootstrapping nodes. Also commons out the agent configuration in a specific YAML file.
This commit is contained in:
committed by
Itxaka
parent
b11530714c
commit
bc7c3b95dc
@@ -1,13 +0,0 @@
|
|||||||
package main_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
|
||||||
. "github.com/onsi/gomega"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestInstaller(t *testing.T) {
|
|
||||||
RegisterFailHandler(Fail)
|
|
||||||
RunSpecs(t, "Installer Suite")
|
|
||||||
}
|
|
@@ -89,7 +89,7 @@ See https://docs.c3os.io/after_install/upgrades/#manual for documentation.
|
|||||||
v = args[0]
|
v = args[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return upgrade(v, c.String("image"), c.Bool("force"))
|
return agent.Upgrade(v, c.String("image"), c.Bool("force"))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -183,7 +183,7 @@ $ c3os rotate --network-token XXX
|
|||||||
dirs = args
|
dirs = args
|
||||||
}
|
}
|
||||||
|
|
||||||
return rotate(dirs, c.String("network-token"), c.String("api"), c.String("root-dir"), c.Bool("restart"))
|
return agent.RotateToken(dirs, c.String("network-token"), c.String("api"), c.String("root-dir"), c.Bool("restart"))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -261,8 +261,10 @@ This command is meant to be used from the boot GRUB menu, but can be started man
|
|||||||
{
|
{
|
||||||
Name: "recovery",
|
Name: "recovery",
|
||||||
Aliases: []string{"r"},
|
Aliases: []string{"r"},
|
||||||
Action: recovery,
|
Action: func(c *cli.Context) error {
|
||||||
Usage: "Starts c3os recovery mode",
|
return agent.Recovery()
|
||||||
|
},
|
||||||
|
Usage: "Starts c3os recovery mode",
|
||||||
Description: `
|
Description: `
|
||||||
Starts c3os recovery mode.
|
Starts c3os recovery mode.
|
||||||
|
|
||||||
@@ -275,9 +277,11 @@ This command is meant to be used from the boot GRUB menu, but can likely be used
|
|||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "reset",
|
Name: "reset",
|
||||||
Action: reset,
|
Action: func(c *cli.Context) error {
|
||||||
Usage: "Starts c3os reset mode",
|
return agent.Reset()
|
||||||
|
},
|
||||||
|
Usage: "Starts c3os reset mode",
|
||||||
Description: `
|
Description: `
|
||||||
Starts c3os reset mode, it will nuke completely the node data and restart fresh.
|
Starts c3os reset mode, it will nuke completely the node data and restart fresh.
|
||||||
Attention ! this will delete any persistent data on the node. It is equivalent to re-init the node right after the installation.
|
Attention ! this will delete any persistent data on the node. It is equivalent to re-init the node right after the installation.
|
||||||
|
@@ -1,131 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os/exec"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
agent "github.com/c3os-io/c3os/internal/agent"
|
|
||||||
"github.com/c3os-io/c3os/internal/c3os"
|
|
||||||
"github.com/c3os-io/c3os/internal/cmd"
|
|
||||||
"github.com/c3os-io/c3os/internal/utils"
|
|
||||||
config "github.com/c3os-io/c3os/pkg/config"
|
|
||||||
"github.com/ipfs/go-log"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
|
|
||||||
machine "github.com/c3os-io/c3os/internal/machine"
|
|
||||||
"github.com/creack/pty"
|
|
||||||
"github.com/gliderlabs/ssh"
|
|
||||||
"github.com/mudler/edgevpn/pkg/logger"
|
|
||||||
"github.com/mudler/edgevpn/pkg/node"
|
|
||||||
"github.com/mudler/edgevpn/pkg/services"
|
|
||||||
nodepair "github.com/mudler/go-nodepair"
|
|
||||||
qr "github.com/mudler/go-nodepair/qrcode"
|
|
||||||
"github.com/pterm/pterm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const recoveryAddr = "127.0.0.1:2222"
|
|
||||||
|
|
||||||
func startRecoveryService(ctx context.Context, token, name, address, loglevel string) error {
|
|
||||||
|
|
||||||
nc := config.Network(token, "", loglevel, "c3osrecovery0")
|
|
||||||
|
|
||||||
lvl, err := log.LevelFromString(loglevel)
|
|
||||||
if err != nil {
|
|
||||||
lvl = log.LevelError
|
|
||||||
}
|
|
||||||
llger := logger.New(lvl)
|
|
||||||
|
|
||||||
o, _, err := nc.ToOpts(llger)
|
|
||||||
if err != nil {
|
|
||||||
llger.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
o = append(o,
|
|
||||||
services.Alive(
|
|
||||||
time.Duration(20)*time.Second,
|
|
||||||
time.Duration(10)*time.Second,
|
|
||||||
time.Duration(10)*time.Second)...)
|
|
||||||
|
|
||||||
// opts, err := vpn.Register(vpnOpts...)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
o = append(o, services.RegisterService(llger, time.Duration(5*time.Second), name, address)...)
|
|
||||||
|
|
||||||
e, err := node.New(o...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.Start(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func recovery(c *cli.Context) error {
|
|
||||||
|
|
||||||
cmd.PrintBranding(agent.DefaultBanner)
|
|
||||||
tk := nodepair.GenerateToken()
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
serviceUUID := utils.RandStringRunes(10)
|
|
||||||
generatedPassword := utils.RandStringRunes(7)
|
|
||||||
|
|
||||||
startRecoveryService(ctx, tk, serviceUUID, recoveryAddr, "fatal")
|
|
||||||
|
|
||||||
cmd.PrintTextFromFile(c3os.BrandingFile("recovery_text"), "Recovery")
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
|
|
||||||
pterm.Info.Printfln(
|
|
||||||
"starting ssh server on '%s', password: '%s' service: '%s' ", recoveryAddr, generatedPassword, serviceUUID)
|
|
||||||
|
|
||||||
qr.Print(utils.EncodeRecoveryToken(tk, serviceUUID, generatedPassword))
|
|
||||||
|
|
||||||
go sshServer(recoveryAddr, generatedPassword)
|
|
||||||
|
|
||||||
// Wait for user input and go back to shell
|
|
||||||
utils.Prompt("")
|
|
||||||
cancel()
|
|
||||||
// give tty1 back
|
|
||||||
svc, err := machine.Getty(1)
|
|
||||||
if err == nil {
|
|
||||||
svc.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshServer(listenAdddr, password string) {
|
|
||||||
ssh.Handle(func(s ssh.Session) {
|
|
||||||
cmd := exec.Command("bash")
|
|
||||||
ptyReq, winCh, isPty := s.Pty()
|
|
||||||
if isPty {
|
|
||||||
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
|
|
||||||
f, err := pty.Start(cmd)
|
|
||||||
if err != nil {
|
|
||||||
pterm.Warning.Println("Failed reserving tty")
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
for win := range winCh {
|
|
||||||
setWinsize(f, win.Width, win.Height)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
io.Copy(f, s) // stdin
|
|
||||||
}()
|
|
||||||
io.Copy(s, f) // stdout
|
|
||||||
cmd.Wait()
|
|
||||||
} else {
|
|
||||||
io.WriteString(s, "No PTY requested.\n")
|
|
||||||
s.Exit(1)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
pterm.Info.Println(ssh.ListenAndServe(listenAdddr, nil, ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
|
|
||||||
return pass == password
|
|
||||||
}),
|
|
||||||
))
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
func setWinsize(f *os.File, w, h int) {
|
|
||||||
syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
|
|
||||||
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
|
||||||
}
|
|
@@ -1,8 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func setWinsize(f *os.File, w, h int) {
|
|
||||||
}
|
|
@@ -1,79 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
agent "github.com/c3os-io/c3os/internal/agent"
|
|
||||||
"github.com/c3os-io/c3os/internal/c3os"
|
|
||||||
"github.com/c3os-io/c3os/internal/cmd"
|
|
||||||
"github.com/c3os-io/c3os/internal/machine"
|
|
||||||
"github.com/c3os-io/c3os/internal/utils"
|
|
||||||
"github.com/pterm/pterm"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
|
||||||
|
|
||||||
func reset(c *cli.Context) error {
|
|
||||||
|
|
||||||
cmd.PrintBranding(agent.DefaultBanner)
|
|
||||||
|
|
||||||
cmd.PrintTextFromFile(c3os.BrandingFile("reset_text"), "Reset")
|
|
||||||
|
|
||||||
// We don't close the lock, as none of the following actions are expected to return
|
|
||||||
lock := sync.Mutex{}
|
|
||||||
go func() {
|
|
||||||
// Wait for user input and go back to shell
|
|
||||||
utils.Prompt("")
|
|
||||||
// give tty1 back
|
|
||||||
svc, err := machine.Getty(1)
|
|
||||||
if err == nil {
|
|
||||||
svc.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.Lock()
|
|
||||||
fmt.Println("Reset aborted")
|
|
||||||
panic(utils.Shell().Run())
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(60 * time.Second)
|
|
||||||
lock.Lock()
|
|
||||||
args := []string{"reset"}
|
|
||||||
args = append(args, "--reset-persistent")
|
|
||||||
|
|
||||||
cmd := exec.Command("elemental", args...)
|
|
||||||
cmd.Env = os.Environ()
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
pterm.Info.Println("Rebooting in 60 seconds, press Enter to abort...")
|
|
||||||
|
|
||||||
// We don't close the lock, as none of the following actions are expected to return
|
|
||||||
lock2 := sync.Mutex{}
|
|
||||||
go func() {
|
|
||||||
// Wait for user input and go back to shell
|
|
||||||
utils.Prompt("")
|
|
||||||
// give tty1 back
|
|
||||||
svc, err := machine.Getty(1)
|
|
||||||
if err == nil {
|
|
||||||
svc.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
lock2.Lock()
|
|
||||||
fmt.Println("Reboot aborted")
|
|
||||||
panic(utils.Shell().Run())
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(60 * time.Second)
|
|
||||||
lock2.Lock()
|
|
||||||
utils.Reboot()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@@ -1,40 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
machine "github.com/c3os-io/c3os/internal/machine"
|
|
||||||
"github.com/c3os-io/c3os/internal/provider"
|
|
||||||
providerConfig "github.com/c3os-io/c3os/internal/provider/config"
|
|
||||||
config "github.com/c3os-io/c3os/pkg/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
func rotate(configDir []string, newToken, apiAddress, rootDir string, restart bool) error {
|
|
||||||
if err := config.ReplaceToken(configDir, newToken); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := config.Scan(config.Directories(configDir...))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
providerCfg := &providerConfig.Config{}
|
|
||||||
err = c.Unmarshal(providerCfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = provider.SetupVPN(machine.EdgeVPNDefaultInstance, apiAddress, rootDir, false, providerCfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if restart {
|
|
||||||
svc, err := machine.EdgeVPN(machine.EdgeVPNDefaultInstance, rootDir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return svc.Restart()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@@ -1,51 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
|
|
||||||
"github.com/c3os-io/c3os/internal/github"
|
|
||||||
"github.com/c3os-io/c3os/internal/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
func upgrade(version, image string, force bool) error {
|
|
||||||
if version == "" && image == "" {
|
|
||||||
githubRepo, err := utils.OSRelease("GITHUB_REPO")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
releases, _ := github.FindReleases(context.Background(), "", githubRepo)
|
|
||||||
version = releases[0]
|
|
||||||
fmt.Println("latest release is ", version)
|
|
||||||
}
|
|
||||||
|
|
||||||
if utils.Version() == version && !force {
|
|
||||||
fmt.Println("latest version already installed. use --force to force upgrade")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
flavor := utils.Flavor()
|
|
||||||
if flavor == "" {
|
|
||||||
return errors.New("no flavor detected")
|
|
||||||
}
|
|
||||||
|
|
||||||
registry, err := utils.OSRelease("IMAGE_REPO")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
img := fmt.Sprintf("%s:%s-%s", registry, flavor, version)
|
|
||||||
if image != "" {
|
|
||||||
img = image
|
|
||||||
}
|
|
||||||
|
|
||||||
args := []string{"upgrade", "--system.uri", fmt.Sprintf("docker:%s", img)}
|
|
||||||
cmd := exec.Command("elemental", args...)
|
|
||||||
cmd.Env = os.Environ()
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
Reference in New Issue
Block a user