blob: 892a756351e05a7d7c79d2c65a4ef116a0f8d584 [file] [log] [blame]
Mateusz Zalegac437dc42022-07-07 13:01:43 +02001package main
2
3import (
4 "context"
5 "fmt"
Mateusz Zalegac437dc42022-07-07 13:01:43 +02006 "log"
7
8 "github.com/spf13/cobra"
9
Mateusz Zalega18a67b02022-08-02 13:37:50 +020010 "source.monogon.dev/metropolis/cli/metroctl/core"
Mateusz Zalegac437dc42022-07-07 13:01:43 +020011 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
12 "source.monogon.dev/metropolis/node/core/identity"
13 "source.monogon.dev/metropolis/proto/api"
14)
15
16var approveCmd = &cobra.Command{
17 Short: "Approves a candidate node, if specified; lists nodes pending approval otherwise.",
18 Use: "approve [node-id]",
19 Args: cobra.MaximumNArgs(1), // One positional argument: node ID
20 Run: doApprove,
21}
22
23func init() {
24 rootCmd.AddCommand(approveCmd)
25}
26
Mateusz Zalegac437dc42022-07-07 13:01:43 +020027// nodeById returns the node matching id, if it exists within nodes.
28func nodeById(nodes []*api.Node, id string) *api.Node {
29 for _, n := range nodes {
30 if identity.NodeID(n.Pubkey) == id {
31 return n
32 }
33 }
34 return nil
35}
36
37func doApprove(cmd *cobra.Command, args []string) {
Mateusz Zalega18a67b02022-08-02 13:37:50 +020038 cc := dialAuthenticated()
Mateusz Zalegac437dc42022-07-07 13:01:43 +020039 mgmt := api.NewManagementClient(cc)
40
41 // Get a list of all nodes pending approval by calling Management.GetNodes.
42 // We need this list regardless of whether we're actually approving nodes, or
43 // just listing them.
Mateusz Zalega18a67b02022-08-02 13:37:50 +020044 ctx := clicontext.WithInterrupt(context.Background())
45 nodes, err := core.GetNodes(ctx, mgmt, "node.state == NODE_STATE_NEW")
Mateusz Zalegac437dc42022-07-07 13:01:43 +020046 if err != nil {
47 log.Fatalf("While fetching a list of nodes pending approval: %v", err)
48 }
49
50 if len(args) == 0 {
51 // If no id was given, just list the nodes pending approval.
52 if len(nodes) != 0 {
53 for _, n := range nodes {
54 fmt.Print(identity.NodeID(n.Pubkey))
55 }
56 } else {
57 log.Print("There are no nodes pending approval at this time.")
58 }
59 } else {
60 // Otherwise, try to approve the node matching the id.
61 tgtNodeId := args[0]
62
63 n := nodeById(nodes, tgtNodeId)
64 if n == nil {
65 log.Fatalf("Couldn't find a new node matching id %s", tgtNodeId)
66 }
67 _, err := mgmt.ApproveNode(ctx, &api.ApproveNodeRequest{
68 Pubkey: n.Pubkey,
69 })
70 if err != nil {
71 log.Fatalf("While approving node %s: %v", tgtNodeId, err)
72 }
73 log.Printf("Approved node %s.", tgtNodeId)
74 }
75}