github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/node.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	orbcfg "github.com/caos/orbos/pkg/orb"
     8  
     9  	"github.com/AlecAivazis/survey/v2"
    10  	"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
    11  	"github.com/caos/orbos/mntr"
    12  	"github.com/caos/orbos/pkg/git"
    13  	"github.com/caos/orbos/pkg/tree"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  func NodeCommand() *cobra.Command {
    18  
    19  	return &cobra.Command{
    20  		Use:     "node [id] command",
    21  		Short:   "Work with an orbs node",
    22  		Example: `orbctl node <exec|reboot|replace> `,
    23  		Aliases: []string{"nodes", "machine", "machines"},
    24  		Args:    cobra.MinimumNArgs(1),
    25  	}
    26  }
    27  
    28  func requireMachines(monitor mntr.Monitor, gitClient *git.Client, orbConfig *orbcfg.Orb, args []string, method func(machine infra.Machine) (required bool, require func(), unrequire func())) error {
    29  	return machines(monitor, gitClient, orbConfig, func(machineIDs []string, machines map[string]infra.Machine, desired *tree.Tree) error {
    30  
    31  		var selected bool
    32  		if len(args) <= 0 {
    33  			selected = true
    34  			if err := survey.AskOne(&survey.MultiSelect{
    35  				Message: "Select machines:",
    36  				Options: machineIDs,
    37  			}, &args, survey.WithValidator(survey.Required)); err != nil {
    38  				return err
    39  			}
    40  		}
    41  
    42  		var push bool
    43  		for _, arg := range args {
    44  			machine, found := machines[arg]
    45  			if !found {
    46  				if selected {
    47  					panic(fmt.Errorf("selected machine %s not found", arg))
    48  				}
    49  
    50  				if strings.Count(arg, ".") != 2 {
    51  					return mntr.ToUserError(fmt.Errorf("machine id must have the format <provider>.<pool>.<machine>"))
    52  				}
    53  
    54  				return mntr.ToUserError(fmt.Errorf("machine %s not found", arg))
    55  			}
    56  
    57  			required, require, _ := method(machine)
    58  			if !required {
    59  				require()
    60  				push = true
    61  			}
    62  		}
    63  
    64  		if !push {
    65  			monitor.Info("Nothing changed")
    66  			return nil
    67  		}
    68  		return gitClient.PushDesiredFunc(git.OrbiterFile, desired)(monitor)
    69  	})
    70  }