-
Notifications
You must be signed in to change notification settings - Fork 1
/
switcher.go
189 lines (153 loc) · 4.69 KB
/
switcher.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
/*
go-swarm is a Go library and ccommand-line tool for managing the creation
and maintenance of Docker Swarm cluster.
Copyright (C) 2021 Sovereign Cloud Australia Pty Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package swarm
import (
"context"
"fmt"
"net"
"os"
"sync"
"time"
"github.com/aucloud/go-runcmd"
log "github.com/sirupsen/logrus"
)
// Switcher is the interface that describes how to switch between Docker Nodes
// Implementations must implement the `Swithc()` method that should return an
// appropriate `runcmd.Runner` interface type for operating on Docker Nodes.
type Switcher interface {
fmt.Stringer
Switch(ctx context.Context, nodeAddr string) error
SwitchVia(ctx context.Context, nodeAddr string) error
Runner() runcmd.Runner
}
type nullSwitcher struct{}
func NewNullSwitcher() (Switcher, error) { return &nullSwitcher{}, nil }
func (s *nullSwitcher) String() string { return "" }
func (s *nullSwitcher) Switch(ctx context.Context, addr string) error { return nil }
func (s *nullSwitcher) SwitchVia(ctx context.Context, addr string) error { return nil }
func (s *nullSwitcher) Runner() runcmd.Runner { return nil }
type localSwitcher struct {
sync.RWMutex
runner runcmd.Runner
}
// NewLocalSwitcher constructs a new Switcher that talks directly to a local
// Docker UNIX Socket on a single-node
func NewLocalSwitcher() (Switcher, error) {
return &localSwitcher{}, nil
}
func (s *localSwitcher) String() string {
return "local://"
}
func (s *localSwitcher) Runner() runcmd.Runner {
s.RLock()
defer s.RUnlock()
return s.runner
}
func (s *localSwitcher) Switch(ctx context.Context, host string) error {
runner, err := runcmd.NewLocalRunner()
if err != nil {
log.WithError(err).Error("error creating local runner")
return fmt.Errorf("error creating local runner: %w", err)
}
s.Lock()
s.runner = runner
s.Unlock()
return nil
}
func (s *localSwitcher) SwitchVia(ctx context.Context, host string) error {
return s.Switch(ctx, host)
}
type sshSwitcher struct {
sync.RWMutex
runner runcmd.Runner
user string
addr string
jump string
key string
}
// NewSSHSwitcher constructs a new Switcher that connect to remote Docker nodes'
// UNIX Sockets over SSH
func NewSSHSwitcher(user, addr, key string, timeout time.Duration) (Switcher, error) {
key = os.ExpandEnv(key)
s := &sshSwitcher{
user: user,
addr: addr,
key: key,
}
if addr != "" {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := s.Switch(ctx, addr); err != nil {
return nil, err
}
}
return s, nil
}
func (s *sshSwitcher) String() string {
return fmt.Sprintf("ssh://%s@%s", s.user, s.addr)
}
func (s *sshSwitcher) Runner() runcmd.Runner {
s.RLock()
defer s.RUnlock()
return s.runner
}
func (s *sshSwitcher) Switch(ctx context.Context, nodeAddr string) error {
_, port, err := net.SplitHostPort(s.addr)
if err != nil {
if addrError, ok := err.(*net.AddrError); ok && addrError.Err == "missing port in address" {
port = "22"
} else {
return fmt.Errorf("error parsing addr: %w", err)
}
}
if port == "" {
port = "22"
}
addr := fmt.Sprintf("%s:%s", nodeAddr, port)
runner, err := runcmd.NewRemoteKeyAuthRunner(ctx, s.user, addr, s.key)
if err != nil {
return fmt.Errorf("error creating remote runner: %w", err)
}
s.Lock()
s.addr = addr
s.runner = runner
s.Unlock()
return nil
}
func (s *sshSwitcher) SwitchVia(ctx context.Context, nodeAddr string) error {
_, port, err := net.SplitHostPort(s.addr)
if err != nil {
if addrError, ok := err.(*net.AddrError); ok && addrError.Err == "missing port in address" {
port = "22"
} else {
return fmt.Errorf("error parsing addr: %w", err)
}
}
if port == "" {
port = "22"
}
addr := fmt.Sprintf("%s:%s", nodeAddr, port)
runner, err := runcmd.NewRemoteKeyAuthRunnerViaJumphost(ctx, s.user, addr, s.addr, s.key)
if err != nil {
return fmt.Errorf("error creating remote runner: %w", err)
}
s.Lock()
s.jump = s.addr
s.addr = addr
s.runner = runner
s.Unlock()
return nil
}