13
"google.golang.org/grpc"
14
"google.golang.org/grpc/credentials/insecure"
15
"google.golang.org/grpc/metadata"
17
"github.com/siderolabs/talos/pkg/grpc/middleware/authz"
18
"github.com/siderolabs/talos/pkg/machinery/client"
19
"github.com/siderolabs/talos/pkg/machinery/constants"
20
"github.com/siderolabs/talos/pkg/machinery/role"
28
Shutdown Action = "shutdown"
30
Reboot Action = "reboot"
34
func Main(args []string) {
35
ctx := context.Background()
37
md := metadata.Pairs()
38
authz.SetMetadata(md, role.MakeSet(role.Admin))
39
adminCtx := metadata.NewOutgoingContext(ctx, md)
41
client, err := client.New(adminCtx, client.WithUnixSocket(constants.MachineSocketPath), client.WithGRPCDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())))
43
log.Fatalf(fmt.Errorf("error while creating machinery client: %w", err).Error())
46
switch ActionFromArgs(args) {
48
err = client.Shutdown(adminCtx)
50
log.Fatalf(fmt.Errorf("error while sending shutdown command: %w", err).Error())
53
err = client.Reboot(adminCtx)
55
log.Fatalf(fmt.Errorf("error while sending reboot command: %w", err).Error())
61
func ActionFromArgs(args []string) Action {
63
if slices.ContainsFunc(args[1:], func(s string) bool {
64
return s == "--halt" || s == "-H" || s == "--poweroff" || s == "-P" || s == "-p"
69
if slices.ContainsFunc(args[1:], func(s string) bool {
70
return s == "--reboot" || s == "-r"