chore: basic support for shutdown/poweroff flags
This adds basic support for shutdown/poweroff flags. it can distringuish between halt/shutdown/reboot. In the case of Talos halt/shutdown is same op. Signed-off-by: Noel Georgi <git@frezbo.dev>
This commit is contained in:
parent
b84277d7dc
commit
2f2eca8617
@ -301,7 +301,7 @@ func main() {
|
||||
return
|
||||
// Azure uses the hv_utils kernel module to shutdown the node in hyper-v by calling perform_shutdown which will call orderly_poweroff which will call /sbin/poweroff.
|
||||
case "poweroff", "shutdown":
|
||||
poweroff.Main()
|
||||
poweroff.Main(os.Args)
|
||||
|
||||
return
|
||||
case "wrapperd":
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/siderolabs/gen/slices"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@ -19,8 +20,18 @@ import (
|
||||
"github.com/siderolabs/talos/pkg/machinery/role"
|
||||
)
|
||||
|
||||
// Action is the action to be performed by the poweroff command.
|
||||
type Action string
|
||||
|
||||
const (
|
||||
// Shutdown is the action to shutdown the machine.
|
||||
Shutdown Action = "shutdown"
|
||||
// Reboot is the action to reboot the machine.
|
||||
Reboot Action = "reboot"
|
||||
)
|
||||
|
||||
// Main is the entrypoint into /sbin/poweroff.
|
||||
func Main() {
|
||||
func Main(args []string) {
|
||||
ctx := context.Background()
|
||||
|
||||
md := metadata.Pairs()
|
||||
@ -32,8 +43,35 @@ func Main() {
|
||||
log.Fatalf(fmt.Errorf("error while creating machinery client: %w", err).Error())
|
||||
}
|
||||
|
||||
switch ActionFromArgs(args) {
|
||||
case Shutdown:
|
||||
err = client.Shutdown(adminCtx)
|
||||
if err != nil {
|
||||
log.Fatalf(fmt.Errorf("error while sending shutdown command: %w", err).Error())
|
||||
}
|
||||
case Reboot:
|
||||
err = client.Reboot(adminCtx)
|
||||
if err != nil {
|
||||
log.Fatalf(fmt.Errorf("error while sending reboot command: %w", err).Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ActionFromArgs returns the action to be performed based on the arguments.
|
||||
func ActionFromArgs(args []string) Action {
|
||||
if len(args) > 1 {
|
||||
if slices.Contains(args[1:], func(s string) bool {
|
||||
return s == "--halt" || s == "-H" || s == "--poweroff" || s == "-P" || s == "-p"
|
||||
}) {
|
||||
return Shutdown
|
||||
}
|
||||
|
||||
if slices.Contains(args[1:], func(s string) bool {
|
||||
return s == "--reboot" || s == "-r"
|
||||
}) {
|
||||
return Reboot
|
||||
}
|
||||
}
|
||||
|
||||
return Shutdown
|
||||
}
|
||||
|
105
internal/app/poweroff/poweroff_test.go
Normal file
105
internal/app/poweroff/poweroff_test.go
Normal file
@ -0,0 +1,105 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package poweroff_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/siderolabs/talos/internal/app/poweroff"
|
||||
)
|
||||
|
||||
func TestParseArgs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
action poweroff.Action
|
||||
}{
|
||||
{
|
||||
name: "shutdown no args",
|
||||
args: []string{"shutdown"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "shutdown with reboot",
|
||||
args: []string{"shutdown", "-r"},
|
||||
action: poweroff.Reboot,
|
||||
},
|
||||
{
|
||||
name: "shutdown with reboot long",
|
||||
args: []string{"shutdown", "--reboot"},
|
||||
action: poweroff.Reboot,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff",
|
||||
args: []string{"shutdown", "-P"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff long",
|
||||
args: []string{"shutdown", "--poweroff"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff and reboot",
|
||||
args: []string{"shutdown", "-h", "-r"},
|
||||
action: poweroff.Reboot,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff, reboot and timer",
|
||||
args: []string{"shutdown", "-h", "-r", "+0"},
|
||||
action: poweroff.Reboot,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff and halt",
|
||||
args: []string{"shutdown", "-h", "-H"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "shutdown with poweroff and halt long",
|
||||
args: []string{"shutdown", "-h", "--halt"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "poweroff no args",
|
||||
args: []string{"poweroff"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "poweroff with halt",
|
||||
args: []string{"poweroff", "--halt"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "poweroff with poweroff",
|
||||
args: []string{"poweroff", "-p"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "poweroff with poweroff long",
|
||||
args: []string{"poweroff", "--poweroff"},
|
||||
action: poweroff.Shutdown,
|
||||
},
|
||||
{
|
||||
name: "poweroff with reboot",
|
||||
args: []string{"poweroff", "--reboot"},
|
||||
action: poweroff.Reboot,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
action := poweroff.ActionFromArgs(tt.args)
|
||||
if action != tt.action {
|
||||
t.Errorf("expected %q, got %q", tt.action, action)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user