File: main.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (347 lines) | stat: -rw-r--r-- 7,719 bytes parent folder | download | duplicates (3)
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
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"net"
	"os"
	"strings"
	"sync"

	"github.com/kisom/goutils/lib"
	"github.com/pkg/sftp"
	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/agent"
)

func usage(w io.Writer) {
	fmt.Fprintf(w, `Usage: %s [-a addresses] [-c chunksize] [-h] [-u user] command args

Flags:
        -a addresses    The comma-separated list of servers to send
                        to. There must not be any spaces in this list.

        -c chunk        The size of chunks to transfer, in bytes. The
                        default is 16MB.

        -u user         The SSH username to use. It must be the
                        same for all hosts. Defaults to the value
                        of the USER environment variable.

Commands:
	exec, run: run a command on the servers
        The args list must be the command line to send to the hosts.

	upload, up, push, send: upload a file to the servers
        The first argument is the local file to upload, and the second
        is the filename to store it as on the remote.

	download, down, pull, fetch: download a file from the servers
        The first argument is the filename to fetch.The second argument
        is the base name for the local file. It will have a '-' and the
        hostname appended.

`, lib.ProgName())
}

var chunkSize = 16777216

var modes = ssh.TerminalModes{
	ssh.ECHO:          0,
	ssh.TTY_OP_ISPEED: 14400,
	ssh.TTY_OP_OSPEED: 14400,
}

func sshAgent() ssh.AuthMethod {
	a, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err == nil {
		return ssh.PublicKeysCallback(agent.NewClient(a).Signers)
	}

	lib.Err(lib.ExitFailure, err, "failed to authenticate with SSH agent")
	return nil
}

func sshConfig(user string) *ssh.ClientConfig {
	return &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{
			sshAgent(),
		},
	}
}

func scanner(host string, in io.Reader, out io.Writer) {
	scanner := bufio.NewScanner(in)
	for scanner.Scan() {
		line := scanner.Text()
		fmt.Fprintf(out, "[%s] %s\n", host, line)
	}
}

func logError(host string, err error, format string, args ...interface{}) {
	msg := fmt.Sprintf(format, args...)
	log.Printf("[%s] FAILED: %s: %v\n", host, msg, err)
}

func exec(wg *sync.WaitGroup, user, host string, commands []string) {
	var shutdown []func() error

	defer func() {
		for i := len(shutdown) - 1; i >= 0; i-- {
			err := shutdown[i]()
			if err != nil && err != io.EOF {
				logError(host, err, "shutting down")
			}
		}
	}()
	defer wg.Done()

	conf := sshConfig(user)
	conn, err := ssh.Dial("tcp", host+":22", conf)
	if err != nil {
		logError(host, err, "failed to connect")
		return
	}
	shutdown = append(shutdown, conn.Close)

	session, err := conn.NewSession()
	if err != nil {
		logError(host, err, "failed to establish session")
		return
	}
	shutdown = append(shutdown, session.Close)

	if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
		session.Close()
		logError(host, err, "request for pty failed")
		return
	}

	stdout, err := session.StdoutPipe()
	if err != nil {
		logError(host, err, "failed to setup standard output")
		return
	}
	go scanner(host, stdout, os.Stdout)

	stderr, err := session.StderrPipe()
	if err != nil {
		logError(host, err, "failed to setup standard error")
		return
	}
	go scanner(host, stderr, os.Stderr)

	for _, command := range commands {
		err = session.Run(command)
		if err != nil {
			logError(host, err, "running command failed")
			return
		}
	}
}

func upload(wg *sync.WaitGroup, user, host, local, remote string) {
	var shutdown []func() error

	defer func() {
		for i := len(shutdown) - 1; i >= 0; i-- {
			err := shutdown[i]()
			if err != nil && err != io.EOF {
				logError(host, err, "shutting down")
			}
		}
	}()
	defer wg.Done()

	conf := sshConfig(user)
	conn, err := ssh.Dial("tcp", host+":22", conf)
	if err != nil {
		logError(host, err, "failed to connect")
		return
	}
	shutdown = append(shutdown, conn.Close)

	sftp, err := sftp.NewClient(conn)
	if err != nil {
		logError(host, err, "setting up SFTP client")
		return
	}
	shutdown = append(shutdown, sftp.Close)

	remoteFile, err := sftp.Create(remote)
	if err != nil {
		logError(host, err, "creating file %s on remote", remote)
		return
	}
	shutdown = append(shutdown, remoteFile.Close)

	localFile, err := os.Open(local)
	if err != nil {
		logError(host, err, "opening local file")
		return
	}
	shutdown = append(shutdown, localFile.Close)

	var buf = make([]byte, chunkSize)
	for {
		var n int
		n, err = localFile.Read(buf)
		if n > 0 {
			_, err = remoteFile.Write(buf[:n])
			if err != nil {
				logError(host, err, "writing chunk")
				return
			}
			fmt.Printf("[%s] wrote %d-byte chunk\n", host, n)
		}

		if err == io.EOF {
			break
		} else if err != nil {
			logError(host, err, "reading chunk")
			return
		}
	}
	fmt.Printf("[%s] %s uploaded to %s\n", host, remote, local)
}

func download(wg *sync.WaitGroup, user, host, local, remote string) {
	var shutdown []func() error

	defer func() {
		for i := len(shutdown) - 1; i >= 0; i-- {
			err := shutdown[i]()
			if err != nil && err != io.EOF {
				logError(host, err, "shutting down")
			}
		}
	}()
	defer wg.Done()

	conf := sshConfig(user)
	conn, err := ssh.Dial("tcp", host+":22", conf)
	if err != nil {
		logError(host, err, "failed to connect")
		return
	}
	shutdown = append(shutdown, conn.Close)

	sftp, err := sftp.NewClient(conn)
	if err != nil {
		logError(host, err, "setting up SFTP client")
		return
	}
	shutdown = append(shutdown, sftp.Close)

	remoteFile, err := sftp.Open(remote)
	if err != nil {
		logError(host, err, "opening file %s on remote", remote)
		return
	}
	shutdown = append(shutdown, remoteFile.Close)

	local = local + "-" + host
	localFile, err := os.Create(local)
	if err != nil {
		logError(host, err, "opening local file")
		return
	}
	shutdown = append(shutdown, localFile.Close)

	var buf = make([]byte, chunkSize)
	for {
		var n int
		n, err = remoteFile.Read(buf)
		if n > 0 {
			_, err = localFile.Write(buf[:n])
			if err != nil {
				logError(host, err, "writing chunk")
				return
			}
			fmt.Printf("[%s] wrote %d-byte chunk\n", host, n)
		}

		if err == io.EOF {
			break
		} else if err != nil {
			logError(host, err, "reading chunk")
			return
		}
	}
	fmt.Printf("[%s] %s downloaded to %s\n", host, remote, local)
}

func init() {
	flag.Usage = func() {
		usage(os.Stdout)
	}
}

func main() {
	var hostsFlag, user string
	flag.StringVar(&hostsFlag, "a", "", "`hosts` to run on")
	flag.IntVar(&chunkSize, "c", chunkSize, "`size` in bytes of transfer chunks")
	flag.StringVar(&user, "u", os.Getenv("USER"), "`username` to run commands as")
	flag.Parse()

	hosts := strings.Split(hostsFlag, ",")
	if len(hosts) == 0 {
		os.Exit(0)
	}

	if flag.NArg() < 1 {
		usage(os.Stderr)
		os.Exit(1)
	}

	cmd := flag.Arg(0)
	var wg = new(sync.WaitGroup)

	switch cmd {
	case "exec", "run":
		if flag.NArg() < 2 {
			usage(os.Stderr)
			os.Exit(1)
		}

		commands := []string{strings.Join(flag.Args()[1:], " ")}
		for _, host := range hosts {
			wg.Add(1)
			go exec(wg, user, host, commands)
		}
	case "upload", "up", "push", "send":
		if flag.NArg() != 3 {
			usage(os.Stderr)
			os.Exit(1)
		}

		for _, host := range hosts {
			wg.Add(1)
			go upload(wg, user, host, flag.Arg(1), flag.Arg(2))
		}
	case "download", "down", "pull", "fetch":
		if flag.NArg() != 3 {
			usage(os.Stderr)
			os.Exit(1)
		}

		for _, host := range hosts {
			wg.Add(1)
			go download(wg, user, host, flag.Arg(2), flag.Arg(1))
		}
	case "help":
		usage(os.Stdout)
		os.Exit(0)
	default:
		fmt.Fprintf(os.Stderr, "Unknown command %s.\n", cmd)
		usage(os.Stderr)
		os.Exit(1)
	}

	log.Printf("waiting for sessions to complete")
	wg.Wait()
}