kairos-agent/cmd/agent/reset.go
Ettore Di Giacinto b2e49776a3 Split off cli into separate binaries (#37)
* 🎨 Split off cli into separate binaries

This commit splits off the cli into 3 binaries:
- agent
- cli
- provider

The provider now is a separate component that can be tested by itself
and have its own lifecycle. This paves the way to a ligher c3os variant,
HA support and other features that can be provided on runtime.

This is working, but still there are low hanging fruit to care about.

Fixes #14

* 🤖 Add provider bin to releases

* ⚙️ Handle signals

* ⚙️ Reduce buildsize footprint

* 🎨 Scan for providers also in /system/providers

* 🤖 Run goreleaser

* 🎨 Refactoring
2022-07-04 22:39:34 +02:00

79 lines
1.6 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"sync"
"time"
"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(banner)
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
}