/
clusterRemote.go
130 lines (107 loc) · 3.04 KB
/
clusterRemote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright © 2023 Dataflows
*/
package cmd
import (
"fmt"
"os"
"strings"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
rigLog "github.com/k0sproject/rig/log"
"github.com/spf13/cobra"
"github.com/thedataflows/go-commons/pkg/config"
"github.com/thedataflows/go-commons/pkg/file"
"github.com/thedataflows/go-commons/pkg/log"
"github.com/thedataflows/kubestrap/pkg/kubestrap"
)
type ClusterRemote struct {
cmd *cobra.Command
parent *Cluster
}
var (
_ = NewClusterRemote(mycluster)
)
func init() {
}
func NewClusterRemote(parent *Cluster) *ClusterRemote {
cr := &ClusterRemote{
parent: parent,
}
cr.cmd = &cobra.Command{
Use: "remote",
Short: "Execute command remotely on the cluster",
Long: ``,
RunE: cr.RunClusterRemoteCommand,
Aliases: []string{"r"},
SilenceErrors: parent.Cmd().SilenceErrors,
SilenceUsage: parent.Cmd().SilenceUsage,
}
parent.Cmd().AddCommand(cr.cmd)
cr.cmd.Flags().StringSlice(
cr.KeyClusterRemoteHosts(),
[]string{},
"List of hosts defined in the cluster to run the command on. If not specified, will execute on all hosts",
)
// Bind flags to config
config.ViperBindPFlagSet(cr.cmd, nil)
rigLog.Log = &log.Log
return cr
}
func (c *ClusterRemote) RunClusterRemoteCommand(cmd *cobra.Command, args []string) error {
if err := c.CheckRequiredFlags(); err != nil {
return err
}
if len(args) == 0 {
return fmt.Errorf("command to execute is not specified")
}
clusterRemoteHosts := c.ClusterRemoteHosts()
// Load cluster spec
cl, err := kubestrap.NewK0sCluster(c.parent.ClusterContext(), c.parent.ClusterBootstrapPath())
if err != nil {
return err
}
hosts := cl.GetClusterSpec().Spec.Hosts.Filter(
func(h *cluster.Host) bool {
for _, filterHost := range clusterRemoteHosts {
if h.Address() == filterHost || h.Metadata.Hostname == filterHost || h.HostnameOverride == filterHost {
return true
}
}
return len(clusterRemoteHosts) == 0
},
)
currentDir := file.WorkingDirectory()
if err := os.Chdir(c.parent.ClusterBootstrapPath()); err != nil {
return err
}
defer func() { _ = os.Chdir(currentDir) }()
for i := 0; i < len(hosts); i += 1 {
err := hosts[i].Connect()
defer hosts[i].Disconnect()
if err != nil {
log.Errorf("[%s] Failed to connect: %v", hosts[i].Address(), err)
continue
}
remoteCommand := strings.Join(args, " ")
o, err := hosts[i].ExecOutput(remoteCommand)
if err != nil {
log.Errorf("[%s] Failed to execute '%s': %v", hosts[i].Address(), remoteCommand, err)
continue
}
if len(o) == 0 {
log.Infof("[%s] Executed '%s'", hosts[i].Address(), remoteCommand)
continue
}
log.Infof("[%s] Executed '%s':\n%v", hosts[i].Address(), remoteCommand, o)
}
return nil
}
func (c *ClusterRemote) CheckRequiredFlags() error {
return c.parent.CheckRequiredFlags()
}
func (c *ClusterRemote) KeyClusterRemoteHosts() string {
return "hosts"
}
func (c *ClusterRemote) ClusterRemoteHosts() []string {
return config.ViperGetStringSlice(c.cmd, c.KeyClusterRemoteHosts())
}