Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 6 | "log" |
| 7 | |
| 8 | "github.com/spf13/cobra" |
| 9 | |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 10 | "source.monogon.dev/metropolis/cli/metroctl/core" |
Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 11 | 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 | |
| 16 | var 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 | |
| 23 | func init() { |
| 24 | rootCmd.AddCommand(approveCmd) |
| 25 | } |
| 26 | |
Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 27 | // nodeById returns the node matching id, if it exists within nodes. |
| 28 | func 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 | |
| 37 | func doApprove(cmd *cobra.Command, args []string) { |
Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame^] | 38 | ctx := clicontext.WithInterrupt(context.Background()) |
| 39 | cc := dialAuthenticated(ctx) |
Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 40 | mgmt := api.NewManagementClient(cc) |
| 41 | |
| 42 | // Get a list of all nodes pending approval by calling Management.GetNodes. |
| 43 | // We need this list regardless of whether we're actually approving nodes, or |
| 44 | // just listing them. |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 45 | nodes, err := core.GetNodes(ctx, mgmt, "node.state == NODE_STATE_NEW") |
Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 46 | 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 | } |