-
Notifications
You must be signed in to change notification settings - Fork 25
/
client.go
386 lines (346 loc) · 12 KB
/
client.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Package rig provides an easy way to add multi-protocol connectivity and
// multi-os operation support to your application's Host objects by
// embedding or directly using the Client or Connection objects.
//
// Rig's core functionality revolves around providing a unified interface
// for interacting with remote systems. This includes managing services,
// file systems, package managers, and getting OS release information,
// abstracting away the intricacies of different operating systems and
// communication protocols.
//
// The protocol implementations aim to provide out-of-the-box default
// behavior similar to what you would expect when using the official
// clients like openssh "ssh" command instead of having to deal with
// implementing ssh config parsing, key managemnt, agent forwarding
// and so on yourself.
//
// To get started, see [Client]
package rig
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/log"
"github.com/k0sproject/rig/v2/os"
"github.com/k0sproject/rig/v2/packagemanager"
"github.com/k0sproject/rig/v2/protocol"
"github.com/k0sproject/rig/v2/remotefs"
"github.com/k0sproject/rig/v2/retry"
)
// Client is a swiss army knife client that can perform actions and run
// commands on target hosts running on multiple operating systems and
// using different protocols for communication.
//
// It provides a consistent interface to the host's init system,
// package manager, file system, and more, regardless of the protocol
// or the remote operating system. It also provides a consistent
// interface to the host's operating system's basic functions in a
// similar manner as the stdlib's os package does for the local system,
// for example chmod, stat, and so on.
//
// The easiest way to set up a client instance is through a protocol
// config struct, like [protocol/ssh.Config]
// or the unified [CompositeConfig] and then use the [NewClient]
// function to create a new client.
type Client struct {
options *ClientOptions
connectionConfigurer ConnectionConfigurer
connection protocol.Connection
once sync.Once
mu sync.Mutex
initErr error
cmd.Runner
log.LoggerInjectable
*PackageManagerService
*InitSystemService
*RemoteFSService
*OSReleaseService
*SudoService
sudoOnce sync.Once
sudoClone *Client
}
// ClientWithConfig is a [Client] that is suitable for embedding into
// a host object that is unmarshalled from YAML configuration.
//
// When embedded into a "host" object like this:
//
// type Host struct {
// rig.ClientWithConfig `yaml:",inline"`
// // ...
// }
//
// And having a configuration YAML like this:
//
// hosts:
// - ssh:
// address: 10.0.0.1
// user: root
//
// You can unmarshal the configuration and start using the clients on the host objects:
//
// if err := host.Connect(context.Background()); err != nil {
// log.Fatal(err)
// }
// out, err := host.ExecOutput("ls")
//
// The available protocols are defined in the [CompositeConfig] struct.
type ClientWithConfig struct {
mu sync.Mutex
ConnectionConfig CompositeConfig `yaml:",inline"`
*Client `yaml:"-"`
}
// Setup allows applying options to the connection to configure subcomponents.
func (c *ClientWithConfig) Setup(opts ...ClientOption) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.Client != nil {
return nil
}
opts = append(opts, WithConnectionConfigurer(&c.ConnectionConfig))
client, err := NewClient(opts...)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
c.Client = client
return nil
}
// Connect to the host. Unlike in [Client.Connect], the Connect method here
// accepts a variadic list of options similar to [NewClient]. This is to
// allow configuring the connection before connecting, since you won't
// be calling [NewClient] to create the [ClientWithConfig] instance when
// unmarshalling from a configuration file.
func (c *ClientWithConfig) Connect(ctx context.Context, opts ...ClientOption) error {
if err := c.Setup(opts...); err != nil { //nolint:contextcheck // it's the trace logger
return err
}
return c.Client.Connect(ctx)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface, it unmarshals and
// sets up a connection from a YAML configuration.
func (c *ClientWithConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type configuredConnection ClientWithConfig
conn := (*configuredConnection)(c)
if err := unmarshal(conn); err != nil {
return fmt.Errorf("unmarshal client config: %w", err)
}
return c.Setup()
}
// NewClient returns a new Connection object with the given options.
//
// You must use either WithConnection to provide a pre-configured connection
// or WithConnectionConfigurer to provide a connection configurer.
//
// An example SSH connection via ssh.Config::
//
// client, err := rig.NewClient(WithConnectionConfigurer(&ssh.Config{Address: "10.0.0.1"}))
//
// Using the [CompositeConfig] struct:
//
// client, err := rig.NewClient(WithConnectionConfigurer(&rig.CompositeConfig{SSH: &ssh.Config{...}}))
//
// If you want to use a pre-configured connection, you can use WithConnection:
//
// conn, err := ssh.NewConnection(ssh.Config{...})
// client, err := rig.NewClient(WithConnection(conn))
//
// Once you have a client, you can use it to interact with the remote host.
//
// err := client.Connect(context.Background())
// if err != nil {
// log.Fatal(err)
// }
// out, err := client.ExecOutput("ls")
//
// To see all of the available ways to run commands, see [cmd.Executor].
func NewClient(opts ...ClientOption) (*Client, error) {
options := NewClientOptions(opts...)
if err := options.Validate(); err != nil {
return nil, fmt.Errorf("validate client options: %w", err)
}
conn := &Client{options: options}
if err := conn.setup(); err != nil {
return nil, err
}
return conn, nil
}
func (c *Client) setupConnection() error {
conn, err := c.options.GetConnection()
if err != nil {
return fmt.Errorf("get connection: %w", err)
}
log.Trace(context.Background(), "connection from configurer", log.HostAttr(conn))
c.connection = conn
return nil
}
func (c *Client) setup(opts ...ClientOption) error {
c.once.Do(func() {
if len(opts) > 0 {
c.options.Apply(opts...)
}
c.initErr = c.setupConnection()
if c.initErr != nil {
return
}
log.Trace(context.Background(), "client setup", log.HostAttr(c.connection))
logger := log.GetLogger(c.connection)
log.Trace(context.Background(), "logger from connection", "is_nil", logger == nil, "is_null", logger == log.Null)
log.InjectLogger(logger, c)
c.Runner = c.options.GetRunner(c.connection)
log.InjectLogger(logger, c.Runner)
c.SudoService = c.options.GetSudoService(c.Runner)
c.InitSystemService = c.options.GetInitSystemService(c.Runner)
c.RemoteFSService = c.options.GetRemoteFSService(c.Runner)
c.PackageManagerService = c.options.GetPackageManagerService(c.Runner)
})
return c.initErr
}
// Service returns a manager for a named service on the remote host using
// the host's init system if one can be detected. This can be used to
// start, stop, restart, and check the status of services.
//
// You most likely need to use this with Sudo:
//
// service, err := client.Sudo().Service("nginx")
func (c *Client) Service(name string) (*Service, error) {
is, err := c.InitSystemService.GetServiceManager()
if err != nil {
return nil, fmt.Errorf("get service manager: %w", err)
}
return &Service{runner: c.Runner, initsys: is, name: name}, nil
}
// String returns a printable representation of the connection, which will usually look
// something like: `address:port` or `user@address:port`.
func (c *Client) String() string {
if c.connection == nil {
if c.connectionConfigurer == nil {
return "[uninitialized connection]"
}
return c.connectionConfigurer.String()
}
return c.connection.String()
}
// Clone returns a copy of the connection with the given additional options applied.
func (c *Client) Clone(opts ...ClientOption) *Client {
options := c.options.Clone()
options.Apply(opts...)
clone := &Client{
options: options,
}
_ = clone.setup()
return clone
}
// Sudo returns a copy of the connection with a Runner that uses sudo.
func (c *Client) Sudo() *Client {
c.sudoOnce.Do(func() {
c.sudoClone = c.Clone(
WithRunner(c.SudoService.SudoRunner()),
WithConnection(c.connection),
WithLogger(log.WithAttrs(c.Log(), log.KeySudo, true)),
)
})
return c.sudoClone
}
func (c *Client) connect(ctx context.Context) error {
if conn, ok := c.connection.(protocol.ConnectorWithContext); ok {
return conn.Connect(ctx) //nolint:wrapcheck // done below
}
if conn, ok := c.connection.(protocol.Connector); ok {
return conn.Connect() //nolint:wrapcheck // done below
}
return nil
}
// Connect to the host. The connection is attempted until the context is done or the
// protocol implementation returns an error indicating that the connection can't be
// established by retrying. If a context without a deadline is used, a 10 second
// timeout is used.
func (c *Client) Connect(ctx context.Context) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.connection == nil {
return fmt.Errorf("%w: connection not properly intialized", protocol.ErrAbort)
}
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
}
if !c.options.ShouldRetry() {
if err := c.connect(ctx); err != nil {
return fmt.Errorf("client connect: %w", err)
}
return nil
}
err := retry.DoWithContext(ctx, func(ctx context.Context) error {
return c.connect(ctx)
}, retry.If(
func(err error) bool { return !errors.Is(err, protocol.ErrAbort) },
))
if err != nil {
return fmt.Errorf("client connect: %w", err)
}
return nil
}
// Disconnect from the host.
func (c *Client) Disconnect() {
c.mu.Lock()
defer c.mu.Unlock()
if c.connection == nil {
return
}
if conn, ok := c.connection.(protocol.Disconnector); ok {
conn.Disconnect()
}
}
var errInteractiveNotSupported = errors.New("the connection does not provide interactive exec support")
// ExecInteractive executes a command on the host and passes stdin/stdout/stderr as-is to the session.
func (c *Client) ExecInteractive(cmd string, stdin io.Reader, stdout, stderr io.Writer) error {
if conn, ok := c.connection.(protocol.InteractiveExecer); ok {
if err := conn.ExecInteractive(cmd, stdin, stdout, stderr); err != nil {
return fmt.Errorf("exec interactive: %w", err)
}
return nil
}
return errInteractiveNotSupported
}
// The service Getters would be available and working via the embedding alrady, but the
// accessors are provided here directly on the Client mainly for discoverability in docs.
// FS returns an fs.FS compatible filesystem interface for accessing files on the host.
//
// If the filesystem can't be accessed, a filesystem that returns an error for all operations is returned
// instead. If you need to handle the error, you can use c.RemoteFSService.GetFS() directly.
func (c *Client) FS() remotefs.FS {
return c.RemoteFSService.FS()
}
// PackageManager for the host's operating system. This can be used to install or remove packages.
//
// If a known package manager can't be detected, a PackageManager that returns an error for all operations is returned.
// If you need to handle the error, you can use client.PackageManagerService.GetPackageManager() (packagemanager.PackageManager, error) directly.
func (c *Client) PackageManager() packagemanager.PackageManager {
return c.PackageManagerService.PackageManager()
}
// OS returns the host's operating system version and release information or an error if it can't be determined.
func (c *Client) OS() (*os.Release, error) {
os, err := c.OSReleaseService.GetOSRelease()
if err != nil {
return nil, fmt.Errorf("get os release: %w", err)
}
return os, nil
}
// Protocol returns the protocol used to connect to the host.
func (c *Client) Protocol() string {
if c.connection == nil {
return "uninitialized"
}
return c.connection.Protocol()
}
// Address returns the address of the host.
func (c *Client) Address() string {
if c.connection != nil {
return c.connection.IPAddress()
}
return ""
}