forked from coreos/fleet
/
fleetctl.go
552 lines (467 loc) · 15 KB
/
fleetctl.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
log "github.com/coreos/fleet/third_party/github.com/golang/glog"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/sign"
"github.com/coreos/fleet/ssh"
"github.com/coreos/fleet/unit"
"github.com/coreos/fleet/version"
)
const (
cliName = "fleetctl"
cliDescription = "fleetctl is a command-line interface to fleet, the cluster-wide CoreOS init system."
oldVersionWarning = `####################################################################
WARNING: fleetctl (%s) is older than the latest registered
version of fleet found in the cluster (%s). You are strongly
recommended to upgrade fleetctl to prevent incompatibility issues.
####################################################################
`
)
var (
out *tabwriter.Writer
globalFlagset = flag.NewFlagSet("fleetctl", flag.ExitOnError)
// set of top-level commands
commands []*Command
// global Registry used by commands
registryCtl registry.Registry
// flags used by all commands
globalFlags = struct {
Debug bool
Verbosity int
Version bool
Endpoint string
EtcdKeyPrefix string
KnownHostsFile string
StrictHostKeyChecking bool
Tunnel string
}{}
// flags used by multiple commands
sharedFlags = struct {
Sign bool
Full bool
NoLegend bool
NoBlock bool
BlockAttempts int
Fields string
}{}
)
func init() {
globalFlagset.BoolVar(&globalFlags.Debug, "debug", false, "Print out more debug information to stderr. Equivalent to --verbosity=1")
globalFlagset.BoolVar(&globalFlags.Version, "version", false, "Print the version and exit")
globalFlagset.IntVar(&globalFlags.Verbosity, "verbosity", 0, "Log at a specified level of verbosity to stderr.")
globalFlagset.StringVar(&globalFlags.Endpoint, "endpoint", "http://127.0.0.1:4001", "etcd endpoint for fleet")
globalFlagset.StringVar(&globalFlags.EtcdKeyPrefix, "etcd-key-prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd (development use only!)")
globalFlagset.StringVar(&globalFlags.KnownHostsFile, "known-hosts-file", ssh.DefaultKnownHostsFile, "File used to store remote machine fingerprints. Ignored if strict host key checking is disabled.")
globalFlagset.BoolVar(&globalFlags.StrictHostKeyChecking, "strict-host-key-checking", true, "Verify host keys presented by remote machines before initiating SSH connections.")
globalFlagset.StringVar(&globalFlags.Tunnel, "tunnel", "", "Establish an SSH tunnel through the provided address for communication with fleet and etcd.")
}
type Command struct {
Name string // Name of the Command and the string to use to invoke it
Summary string // One-sentence summary of what the Command does
Usage string // Usage options/arguments
Description string // Detailed description of command
Flags flag.FlagSet // Set of flags associated with this command
Run func(args []string) int // Run a command with the given arguments, return exit status
}
func init() {
out = new(tabwriter.Writer)
out.Init(os.Stdout, 0, 8, 1, '\t', 0)
commands = []*Command{
cmdCatUnit,
cmdDebugInfo,
cmdDestroyUnit,
cmdHelp,
cmdJournal,
cmdListMachines,
cmdListUnits,
cmdLoadUnits,
cmdSSH,
cmdStartUnit,
cmdStatusUnits,
cmdStopUnit,
cmdSubmitUnit,
cmdUnloadUnit,
cmdVerifyUnit,
cmdVersion,
}
}
func getAllFlags() (flags []*flag.Flag) {
return getFlags(globalFlagset)
}
func getFlags(flagset *flag.FlagSet) (flags []*flag.Flag) {
flags = make([]*flag.Flag, 0)
flagset.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
})
return
}
// checkVersion makes a best-effort attempt to verify that fleetctl is at least as new as the
// latest fleet version found registered in the cluster. If any errors are encountered or fleetctl
// is >= the latest version found, it returns true. If it is < the latest found version, it returns
// false and a scary warning to the user.
func checkVersion() (string, bool) {
fv := version.SemVersion
lv, err := registryCtl.GetLatestVersion()
if err != nil {
log.Errorf("error attempting to check latest fleet version in Registry: %v", err)
} else if lv != nil && fv.LessThan(*lv) {
return fmt.Sprintf(oldVersionWarning, fv.String(), lv.String()), false
}
return "", true
}
func main() {
// parse global arguments
globalFlagset.Parse(os.Args[1:])
var args = globalFlagset.Args()
getFlagsFromEnv(cliName, globalFlagset)
if globalFlags.Debug && globalFlags.Verbosity < 1 {
globalFlags.Verbosity = 1
}
// configure glog, which uses the global command line options
if globalFlags.Verbosity > 0 {
flag.CommandLine.Lookup("v").Value.Set(strconv.Itoa(globalFlags.Verbosity))
flag.CommandLine.Lookup("logtostderr").Value.Set("true")
}
// no command specified - trigger help
if len(args) < 1 {
args = append(args, "help")
}
// deal specially with --version
if globalFlags.Version {
args[0] = "version"
}
var cmd *Command
// determine which Command should be run
for _, c := range commands {
if c.Name == args[0] {
cmd = c
if err := c.Flags.Parse(args[1:]); err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
break
}
}
if cmd == nil {
fmt.Printf("%v: unknown subcommand: %q\n", cliName, args[0])
fmt.Printf("Run '%v help' for usage.\n", cliName)
os.Exit(2)
}
// TODO(jonboulle): increase cleverness of registry initialization
if cmd.Name != "help" && cmd.Name != "version" {
registryCtl = getRegistry()
msg, ok := checkVersion()
if !ok {
fmt.Fprint(os.Stderr, msg)
}
}
os.Exit(cmd.Run(cmd.Flags.Args()))
}
// getFlagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the given prefix, and any dashes are replaced by
// underscores - for example: some-flag => PREFIX_SOME_FLAG
func getFlagsFromEnv(prefix string, fs *flag.FlagSet) {
alreadySet := make(map[string]bool)
fs.Visit(func(f *flag.Flag) {
alreadySet[f.Name] = true
})
fs.VisitAll(func(f *flag.Flag) {
if !alreadySet[f.Name] {
key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
val := os.Getenv(key)
if val != "" {
fs.Set(f.Name, val)
}
}
})
}
// getRegistry initializes a connection to the Registry
func getRegistry() registry.Registry {
tun := getTunnelFlag()
machines := []string{globalFlags.Endpoint}
client := etcd.NewClient(machines)
if tun != "" {
sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), false)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed initializing SSH client: %v\n", err)
os.Exit(1)
}
dial := func(network, addr string) (net.Conn, error) {
tcpaddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return sshClient.DialTCP(network, nil, tcpaddr)
}
tr := http.Transport{
Dial: dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client.SetTransport(&tr)
}
return registry.New(client, globalFlags.EtcdKeyPrefix)
}
// getChecker creates and returns a HostKeyChecker, or nil if any error is encountered
func getChecker() *ssh.HostKeyChecker {
if !globalFlags.StrictHostKeyChecking {
return nil
}
keyFile := ssh.NewHostKeyFile(globalFlags.KnownHostsFile)
return ssh.NewHostKeyChecker(keyFile)
}
// getUnitFromFile attempts to load a Job from a given filename
// It returns the Job or nil, and any error encountered
func getUnitFromFile(file string) (*unit.Unit, error) {
out, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
unitName := path.Base(file)
log.V(1).Infof("Unit(%s) found in local filesystem", unitName)
return unit.NewUnit(string(out)), nil
}
func getTunnelFlag() string {
tun := globalFlags.Tunnel
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func machineIDLegend(ms machine.MachineState, full bool) string {
legend := ms.ID
if !full {
legend = fmt.Sprintf("%s...", ms.ShortID())
}
return legend
}
func machineFullLegend(ms machine.MachineState, full bool) string {
legend := machineIDLegend(ms, full)
if len(ms.PublicIP) > 0 {
legend = fmt.Sprintf("%s/%s", legend, ms.PublicIP)
}
return legend
}
func findJobs(args []string) (jobs []job.Job, err error) {
jobs = make([]job.Job, len(args))
for i, v := range args {
name := unitNameMangle(v)
j, err := registryCtl.GetJob(name)
if err != nil {
return nil, fmt.Errorf("error retrieving Job(%s) from Registry: %v", name, err)
} else if j == nil {
return nil, fmt.Errorf("could not find Job(%s)", name)
}
jobs[i] = *j
}
return jobs, nil
}
func createJob(jobName string, unit *unit.Unit) (*job.Job, error) {
j := job.NewJob(jobName, *unit)
if err := registryCtl.CreateJob(j); err != nil {
return nil, fmt.Errorf("failed creating job %s: %v", j.Name, err)
}
log.V(1).Infof("Created Job(%s) in Registry", j.Name)
return j, nil
}
// signJob signs the Unit of a Job using the public keys in the local SSH
// agent, and pushes the resulting SignatureSet to the Registry
func signJob(j *job.Job) error {
sc, err := sign.NewSignatureCreatorFromSSHAgent()
if err != nil {
return fmt.Errorf("failed creating SignatureCreator: %v", err)
}
ss, err := sc.SignJob(j)
if err != nil {
return fmt.Errorf("failed signing Job(%s): %v", j.Name, err)
}
err = registryCtl.CreateSignatureSet(ss)
if err != nil {
return fmt.Errorf("failed storing Job signature in registry: %v", err)
}
log.V(1).Infof("Signed Job(%s)", j.Name)
return nil
}
// verifyJob attempts to verify the signature of the provided Job's unit using
// the public keys in the local SSH agent
func verifyJob(j *job.Job) error {
sv, err := sign.NewSignatureVerifierFromSSHAgent()
if err != nil {
return fmt.Errorf("failed creating SignatureVerifier: %v", err)
}
ss, err := registryCtl.GetSignatureSetOfJob(j.Name)
if err != nil {
return fmt.Errorf("failed attempting to retrieve SignatureSet of Job(%s): %v", j.Name, err)
}
verified, err := sv.VerifyJob(j, ss)
if err != nil {
return fmt.Errorf("failed attempting to verify Job(%s): %v", j.Name, err)
} else if !verified {
return fmt.Errorf("unable to verify Job(%s)", j.Name)
}
log.V(1).Infof("Verified signature of Job(%s)", j.Name)
return nil
}
func lazyCreateJobs(args []string, signAndVerify bool) error {
for _, arg := range args {
jobName := unitNameMangle(arg)
j, err := registryCtl.GetJob(jobName)
if err != nil {
log.V(1).Infof("Error retrieving Job(%s) from Registry: %v", jobName, err)
continue
}
if j != nil {
log.V(1).Infof("Found Job(%s) in Registry, no need to recreate it", jobName)
if signAndVerify {
if err := verifyJob(j); err != nil {
return err
}
}
continue
}
unit, err := getUnitFromFile(arg)
if err != nil {
return fmt.Errorf("failed getting Unit(%s) from file: %v", jobName, err)
}
j, err = createJob(jobName, unit)
if err != nil {
return err
}
if signAndVerify {
if err := signJob(j); err != nil {
return err
}
}
}
return nil
}
func lazyLoadJobs(args []string) ([]string, error) {
triggered := make([]string, 0)
for _, v := range args {
name := unitNameMangle(v)
j, err := registryCtl.GetJob(name)
if err != nil {
return nil, fmt.Errorf("error retrieving Job(%s) from Registry: %v", name, err)
} else if j == nil || j.State == nil {
return nil, fmt.Errorf("unable to determine state of job %s", name)
} else if *(j.State) == job.JobStateLoaded || *(j.State) == job.JobStateLaunched {
log.V(1).Infof("Job(%s) already %s, skipping.", j.Name, *(j.State))
continue
}
log.V(1).Infof("Setting Job(%s) target state to loaded", j.Name)
registryCtl.SetJobTargetState(j.Name, job.JobStateLoaded)
triggered = append(triggered, j.Name)
}
return triggered, nil
}
func lazyStartJobs(args []string) ([]string, error) {
triggered := make([]string, 0)
for _, v := range args {
name := unitNameMangle(v)
j, err := registryCtl.GetJob(name)
if err != nil {
return nil, fmt.Errorf("error retrieving Job(%s) from Registry: %v", name, err)
} else if j == nil {
return nil, fmt.Errorf("unable to find Job(%s)", name)
} else if j.State == nil {
return nil, fmt.Errorf("unable to determine current state of Job")
} else if *(j.State) == job.JobStateLaunched {
log.V(1).Infof("Job(%s) already %s, skipping.", j.Name, *(j.State))
continue
}
log.V(1).Infof("Setting Job(%s) target state to launched", j.Name)
registryCtl.SetJobTargetState(j.Name, job.JobStateLaunched)
triggered = append(triggered, j.Name)
}
return triggered, nil
}
// waitForJobStates polls each of the indicated jobs until each of their
// states is equal to that which the caller indicates, or until the
// polling operation times out. waitForJobStates will retry forever, or
// up to maxAttempts times before timing out if maxAttempts is greater
// than zero. Returned is an error channel used to communicate when
// timeouts occur. The returned error channel will be closed after all
// polling operation is complete.
func waitForJobStates(jobs []string, js job.JobState, maxAttempts int, out io.Writer) chan error {
errchan := make(chan error)
var wg sync.WaitGroup
for _, name := range jobs {
wg.Add(1)
go checkJobState(name, js, maxAttempts, out, &wg, errchan)
}
go func() {
wg.Wait()
close(errchan)
}()
return errchan
}
func checkJobState(jobName string, js job.JobState, maxAttempts int, out io.Writer, wg *sync.WaitGroup, errchan chan error) {
defer wg.Done()
sleep := 100 * time.Millisecond
if maxAttempts < 1 {
for {
if assertJobState(jobName, js) {
return
}
time.Sleep(sleep)
}
} else {
for attempt := 0; attempt < maxAttempts; attempt++ {
if assertJobState(jobName, js) {
return
}
time.Sleep(sleep)
}
errchan <- fmt.Errorf("timed out waiting for job %s to report state %s", jobName, js)
}
}
func assertJobState(name string, js job.JobState) bool {
j, err := registryCtl.GetJob(name)
if err != nil {
log.Warningf("Error retrieving Job(%s) from Registry: %v", name, err)
return false
}
if j == nil || j.State == nil || *(j.State) != js {
return false
}
msg := fmt.Sprintf("Job %s %s", name, *(j.State))
tgt, err := registryCtl.GetJobTarget(name)
if err != nil {
log.Warningf("Error retrieving target information for Job(%s) from Registry: %v", name, err)
} else if tgt != "" {
if ms, _ := registryCtl.GetMachineState(tgt); ms != nil {
msg = fmt.Sprintf("%s on %s", msg, machineFullLegend(*ms, false))
}
}
fmt.Fprintln(out, msg)
return true
}
// unitNameMangle tries to turn a string that might not be a unit name into a
// sensible unit name.
func unitNameMangle(baseName string) string {
name := path.Base(baseName)
if !unit.RecognizedUnitType(name) {
return unit.DefaultUnitType(name)
}
return name
}