forked from hectorj2f/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleetctl.go
814 lines (695 loc) · 23.9 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/coreos/fleet/api"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/etcd"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/schema"
"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.
####################################################################
`
clientDriverAPI = "API"
clientDriverEtcd = "etcd"
)
var (
out *tabwriter.Writer
globalFlagset = flag.NewFlagSet("fleetctl", flag.ExitOnError)
// set of top-level commands
commands []*Command
// global API client used by commands
cAPI client.API
// flags used by all commands
globalFlags = struct {
Debug bool
Version bool
Help bool
ClientDriver string
ExperimentalAPI bool
Endpoint string
RequestTimeout float64
KeyFile string
CertFile string
CAFile string
Tunnel string
KnownHostsFile string
StrictHostKeyChecking bool
SSHTimeout float64
EtcdKeyPrefix string
}{}
// flags used by multiple commands
sharedFlags = struct {
Sign bool
Full bool
NoLegend bool
NoBlock bool
BlockAttempts int
Fields string
}{}
// used to cache MachineStates
machineStates map[string]*machine.MachineState
)
func init() {
// call this as early as possible to ensure we always have timestamps
// on fleetctl logs
log.EnableTimestamps()
globalFlagset.BoolVar(&globalFlags.Help, "help", false, "Print usage information and exit")
globalFlagset.BoolVar(&globalFlags.Help, "h", false, "Print usage information and exit")
globalFlagset.BoolVar(&globalFlags.Debug, "debug", false, "Print out more debug information to stderr")
globalFlagset.BoolVar(&globalFlags.Version, "version", false, "Print the version and exit")
globalFlagset.StringVar(&globalFlags.ClientDriver, "driver", clientDriverEtcd, fmt.Sprintf("Adapter used to execute fleetctl commands. Options include %q and %q.", clientDriverAPI, clientDriverEtcd))
globalFlagset.StringVar(&globalFlags.Endpoint, "endpoint", "http://127.0.0.1:4001", fmt.Sprintf("Location of the fleet API if --driver=%s. Alternatively, if --driver=%s, location of the etcd API.", clientDriverAPI, clientDriverEtcd))
globalFlagset.StringVar(&globalFlags.EtcdKeyPrefix, "etcd-key-prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd (development use only!)")
globalFlagset.StringVar(&globalFlags.KeyFile, "key-file", "", "Location of TLS key file used to secure communication with the fleet API or etcd")
globalFlagset.StringVar(&globalFlags.CertFile, "cert-file", "", "Location of TLS cert file used to secure communication with the fleet API or etcd")
globalFlagset.StringVar(&globalFlags.CAFile, "ca-file", "", "Location of TLS CA file used to secure communication with the fleet API or etcd")
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.Float64Var(&globalFlags.SSHTimeout, "ssh-timeout", 10.0, "Amount of time in seconds to allow for SSH connection initialization before failing.")
globalFlagset.StringVar(&globalFlags.Tunnel, "tunnel", "", "Establish an SSH tunnel through the provided address for communication with fleet and etcd.")
globalFlagset.Float64Var(&globalFlags.RequestTimeout, "request-timeout", 3.0, "Amount of time in seconds to allow a single request before considering it failed.")
// deprecated flags
globalFlagset.BoolVar(&globalFlags.ExperimentalAPI, "experimental-api", false, hidden)
globalFlagset.StringVar(&globalFlags.KeyFile, "etcd-keyfile", "", hidden)
globalFlagset.StringVar(&globalFlags.CertFile, "etcd-certfile", "", hidden)
globalFlagset.StringVar(&globalFlags.CAFile, "etcd-cafile", "", hidden)
}
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,
cmdDestroyUnit,
cmdFDForward,
cmdHelp,
cmdJournal,
cmdListMachines,
cmdListUnitFiles,
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
}
func maybeAddNewline(s string) string {
if !strings.HasSuffix(s, "\n") {
s = s + "\n"
}
return s
}
func stderr(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, maybeAddNewline(format), args...)
}
func stdout(format string, args ...interface{}) {
fmt.Fprintf(os.Stdout, maybeAddNewline(format), args...)
}
// 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(cReg registry.ClusterRegistry) (string, bool) {
fv := version.SemVersion
lv, err := cReg.LatestDaemonVersion()
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 {
log.SetVerbosity(1)
}
if globalFlags.Version {
args = []string{"version"}
} else if len(args) < 1 || globalFlags.Help {
args = []string{"help"}
}
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 {
stderr("%v", err)
os.Exit(2)
}
break
}
}
if cmd == nil {
stderr("%v: unknown subcommand: %q", cliName, args[0])
stderr("Run '%v help' for usage.", cliName)
os.Exit(2)
}
if sharedFlags.Sign {
stderr("WARNING: The signed/verified units feature is DEPRECATED and cannot be used.")
os.Exit(2)
}
if cmd.Name != "help" && cmd.Name != "version" {
var err error
cAPI, err = getClient()
if err != nil {
stderr("Unable to initialize client: %v", err)
os.Exit(1)
}
}
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)
}
}
})
}
// getClient initializes a client of fleet based on CLI flags
func getClient() (client.API, error) {
// The user explicitly set --experimental-api=true, so it trumps the
// --driver flag. This behavior exists for backwards-compatibilty.
if globalFlags.ExperimentalAPI {
return getHTTPClient()
}
switch globalFlags.ClientDriver {
case clientDriverAPI:
return getHTTPClient()
case clientDriverEtcd:
return getRegistryClient()
}
return nil, fmt.Errorf("unrecognized driver %q", globalFlags.ClientDriver)
}
func getHTTPClient() (client.API, error) {
ep, err := url.Parse(globalFlags.Endpoint)
if err != nil {
return nil, err
}
if len(ep.Scheme) == 0 {
return nil, errors.New("URL scheme undefined")
}
tun := getTunnelFlag()
tunneling := tun != ""
dialUnix := ep.Scheme == "unix" || ep.Scheme == "file"
tunnelFunc := net.Dial
if tunneling {
sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), true, getSSHTimeoutFlag())
if err != nil {
return nil, fmt.Errorf("failed initializing SSH client: %v", err)
}
if dialUnix {
tgt := ep.Path
tunnelFunc = func(string, string) (net.Conn, error) {
log.V(1).Infof("Establishing remote fleetctl proxy to %s", tgt)
cmd := fmt.Sprintf(`fleetctl fd-forward %s`, tgt)
return ssh.DialCommand(sshClient, cmd)
}
} else {
tunnelFunc = sshClient.Dial
}
}
dialFunc := tunnelFunc
if dialUnix {
// This commonly happens if the user misses the leading slash after the scheme.
// For example, "unix://var/run/fleet.sock" would be parsed as host "var".
if len(ep.Host) > 0 {
return nil, fmt.Errorf("unable to connect to host %q with scheme %q", ep.Host, ep.Scheme)
}
// The Path field is only used for dialing and should not be used when
// building any further HTTP requests.
sockPath := ep.Path
ep.Path = ""
// If not tunneling to the unix socket, http.Client will dial it directly.
// http.Client does not natively support dialing a unix domain socket, so the
// dial function must be overridden.
if !tunneling {
dialFunc = func(string, string) (net.Conn, error) {
return net.Dial("unix", sockPath)
}
}
// http.Client doesn't support the schemes "unix" or "file", but it
// is safe to use "http" as dialFunc ignores it anyway.
ep.Scheme = "http"
// The Host field is not used for dialing, but will be exposed in debug logs.
ep.Host = "domain-sock"
}
tlsConfig, err := pkg.ReadTLSConfigFiles(globalFlags.CAFile, globalFlags.CertFile, globalFlags.KeyFile)
if err != nil {
return nil, err
}
trans := pkg.LoggingHTTPTransport{
Transport: http.Transport{
Dial: dialFunc,
TLSClientConfig: tlsConfig,
},
}
hc := http.Client{
Transport: &trans,
}
return client.NewHTTPClient(&hc, *ep)
}
func getRegistryClient() (client.API, error) {
var dial func(string, string) (net.Conn, error)
tun := getTunnelFlag()
if tun != "" {
sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), false, getSSHTimeoutFlag())
if err != nil {
return nil, fmt.Errorf("failed initializing SSH client: %v", err)
}
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)
}
}
tlsConfig, err := pkg.ReadTLSConfigFiles(globalFlags.CAFile, globalFlags.CertFile, globalFlags.KeyFile)
if err != nil {
return nil, err
}
trans := &http.Transport{
Dial: dial,
TLSClientConfig: tlsConfig,
}
timeout := getRequestTimeoutFlag()
machines := []string{globalFlags.Endpoint}
eClient, err := etcd.NewClient(machines, trans, timeout)
if err != nil {
return nil, err
}
reg := registry.NewEtcdRegistry(eClient, globalFlags.EtcdKeyPrefix)
if msg, ok := checkVersion(reg); !ok {
stderr(msg)
}
return &client.RegistryClient{Registry: reg}, nil
}
// 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 Unit from a given filename
// It returns the Unit or nil, and any error encountered
func getUnitFromFile(file string) (*unit.UnitFile, 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.NewUnitFile(string(out))
}
func getTunnelFlag() string {
tun := globalFlags.Tunnel
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func getSSHTimeoutFlag() time.Duration {
return time.Duration(globalFlags.SSHTimeout*1000) * time.Millisecond
}
func getRequestTimeoutFlag() time.Duration {
return time.Duration(globalFlags.RequestTimeout*1000) * time.Millisecond
}
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 findUnits(args []string) (sus []schema.Unit, err error) {
units, err := cAPI.Units()
if err != nil {
return nil, err
}
uMap := make(map[string]*schema.Unit, len(units))
for _, u := range units {
u := u
uMap[u.Name] = u
}
filtered := make([]schema.Unit, 0)
for _, v := range args {
v = unitNameMangle(v)
u, ok := uMap[v]
if !ok {
continue
}
filtered = append(filtered, *u)
}
return filtered, nil
}
func createUnit(name string, uf *unit.UnitFile) (*schema.Unit, error) {
if uf == nil {
return nil, fmt.Errorf("nil unit provided")
}
u := schema.Unit{
Name: name,
Options: schema.MapUnitFileToSchemaUnitOptions(uf),
}
// TODO(jonboulle): this dependency on the API package is awkward, and
// redundant with the check in api.unitsResource.set, but it is a
// workaround to implementing the same check in the RegistryClient. It
// will disappear once RegistryClient is deprecated.
if err := api.ValidateName(name); err != nil {
return nil, err
}
if err := api.ValidateOptions(u.Options); err != nil {
return nil, err
}
j := &job.Job{Unit: *uf}
if err := j.ValidateRequirements(); err != nil {
log.Warningf("Unit %s: %v", name, err)
}
err := cAPI.CreateUnit(&u)
if err != nil {
return nil, fmt.Errorf("failed creating unit %s: %v", name, err)
}
log.V(1).Infof("Created Unit(%s) in Registry", name)
return &u, nil
}
// lazyCreateUnits iterates over a set of unit names and, for each, attempts to
// ensure that a unit by that name exists in the Registry, by checking a number
// of conditions and acting on the first one that succeeds, in order of:
// 1. a unit by that name already existing in the Registry
// 2. a unit file by that name existing on disk
// 3. a corresponding unit template (if applicable) existing in the Registry
// 4. a corresponding unit template (if applicable) existing on disk
// Any error encountered during these steps is returned immediately (i.e.
// subsequent Jobs are not acted on). An error is also returned if none of the
// above conditions match a given Job.
func lazyCreateUnits(args []string) error {
for _, arg := range args {
// TODO(jonboulle): this loop is getting too unwieldy; factor it out
arg = maybeAppendDefaultUnitType(arg)
name := unitNameMangle(arg)
// First, check if there already exists a Unit by the given name in the Registry
u, err := cAPI.Unit(name)
if err != nil {
return fmt.Errorf("error retrieving Unit(%s) from Registry: %v", name, err)
}
if u != nil {
log.V(1).Infof("Found Unit(%s) in Registry, no need to recreate it", name)
warnOnDifferentLocalUnit(arg, u)
continue
}
// Failing that, assume the name references a local unit file on disk, and attempt to load that, if it exists
if _, err := os.Stat(arg); !os.IsNotExist(err) {
unit, err := getUnitFromFile(arg)
if err != nil {
return fmt.Errorf("failed getting Unit(%s) from file: %v", arg, err)
}
u, err = createUnit(name, unit)
if err != nil {
return err
}
continue
}
// Otherwise (if the unit file does not exist), check if the name appears to be an instance unit,
// and if so, check for a corresponding template unit in the Registry
uni := unit.NewUnitNameInfo(name)
if uni == nil {
return fmt.Errorf("error extracting information from unit name %s", name)
} else if !uni.IsInstance() {
return fmt.Errorf("unable to find Unit(%s) in Registry or on filesystem", name)
}
tmpl, err := cAPI.Unit(uni.Template)
if err != nil {
return fmt.Errorf("error retrieving template Unit(%s) from Registry: %v", uni.Template, err)
}
// Finally, if we could not find a template unit in the Registry, check the local disk for one instead
var uf *unit.UnitFile
if tmpl == nil {
file := path.Join(path.Dir(arg), uni.Template)
if _, err := os.Stat(file); os.IsNotExist(err) {
return fmt.Errorf("unable to find Unit(%s) or template Unit(%s) in Registry or on filesystem", name, uni.Template)
}
uf, err = getUnitFromFile(file)
if err != nil {
return fmt.Errorf("failed getting template Unit(%s) from file: %v", uni.Template, err)
}
} else {
warnOnDifferentLocalUnit(arg, tmpl)
uf = schema.MapSchemaUnitOptionsToUnitFile(tmpl.Options)
}
// If we found a template unit, create a near-identical instance unit in
// the Registry - same unit file as the template, but different name
u, err = createUnit(name, uf)
if err != nil {
return err
}
}
return nil
}
func warnOnDifferentLocalUnit(loc string, su *schema.Unit) {
suf := schema.MapSchemaUnitOptionsToUnitFile(su.Options)
if _, err := os.Stat(loc); !os.IsNotExist(err) {
luf, err := getUnitFromFile(loc)
if err == nil && luf.Hash() != suf.Hash() {
stderr("WARNING: Unit %s in registry differs from local unit file %s", su.Name, loc)
return
}
}
if uni := unit.NewUnitNameInfo(path.Base(loc)); uni != nil && uni.IsInstance() {
file := path.Join(path.Dir(loc), uni.Template)
if _, err := os.Stat(file); !os.IsNotExist(err) {
tmpl, err := getUnitFromFile(file)
if err == nil && tmpl.Hash() != suf.Hash() {
stderr("WARNING: Unit %s in registry differs from local template unit file %s", su.Name, uni.Template)
}
}
}
}
func lazyLoadUnits(args []string) ([]*schema.Unit, error) {
units := make([]string, 0, len(args))
for _, j := range args {
units = append(units, unitNameMangle(j))
}
return setTargetStateOfUnits(units, job.JobStateLoaded)
}
func lazyStartUnits(args []string) ([]*schema.Unit, error) {
units := make([]string, 0, len(args))
for _, j := range args {
units = append(units, unitNameMangle(j))
}
return setTargetStateOfUnits(units, job.JobStateLaunched)
}
// setTargetStateOfUnits ensures that the target state for the given Units is set
// to the given state in the Registry.
// On success, a slice of the Units for which a state change was made is returned.
// Any error encountered is immediately returned (i.e. this is not a transaction).
func setTargetStateOfUnits(units []string, state job.JobState) ([]*schema.Unit, error) {
triggered := make([]*schema.Unit, 0)
for _, name := range units {
u, err := cAPI.Unit(name)
if err != nil {
return nil, fmt.Errorf("error retrieving unit %s from registry: %v", name, err)
} else if u == nil {
return nil, fmt.Errorf("unable to find unit %s", name)
} else if job.JobState(u.DesiredState) == state {
log.V(1).Infof("Unit(%s) already %s, skipping.", u.Name, u.DesiredState)
continue
}
log.V(1).Infof("Setting Unit(%s) target state to %s", u.Name, state)
cAPI.SetUnitTargetState(u.Name, string(state))
triggered = append(triggered, u)
}
return triggered, nil
}
// waitForUnitStates polls each of the indicated units until each of their
// states is equal to that which the caller indicates, or until the
// polling operation times out. waitForUnitStates 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 waitForUnitStates(units []string, js job.JobState, maxAttempts int, out io.Writer) chan error {
errchan := make(chan error)
var wg sync.WaitGroup
for _, name := range units {
wg.Add(1)
go checkUnitState(name, js, maxAttempts, out, &wg, errchan)
}
go func() {
wg.Wait()
close(errchan)
}()
return errchan
}
func checkUnitState(name string, js job.JobState, maxAttempts int, out io.Writer, wg *sync.WaitGroup, errchan chan error) {
defer wg.Done()
sleep := 500 * time.Millisecond
if maxAttempts < 1 {
for {
if assertUnitState(name, js, out) {
return
}
time.Sleep(sleep)
}
} else {
for attempt := 0; attempt < maxAttempts; attempt++ {
if assertUnitState(name, js, out) {
return
}
time.Sleep(sleep)
}
errchan <- fmt.Errorf("timed out waiting for unit %s to report state %s", name, js)
}
}
func assertUnitState(name string, js job.JobState, out io.Writer) (ret bool) {
u, err := cAPI.Unit(name)
if err != nil {
log.Warningf("Error retrieving Unit(%s) from Registry: %v", name, err)
return
}
if u == nil || job.JobState(u.CurrentState) != js {
return
}
ret = true
msg := fmt.Sprintf("Unit %s %s", name, u.CurrentState)
if u.MachineID != "" {
ms := cachedMachineState(u.MachineID)
if ms != nil {
msg = fmt.Sprintf("%s on %s", msg, machineFullLegend(*ms, false))
}
}
fmt.Fprintln(out, msg)
return
}
func machineState(machID string) (*machine.MachineState, error) {
machines, err := cAPI.Machines()
if err != nil {
return nil, err
}
for _, ms := range machines {
if ms.ID == machID {
return &ms, nil
}
}
return nil, nil
}
// cachedMachineState makes a best-effort to retrieve the MachineState of the given machine ID.
// It memoizes MachineState information for the life of a fleetctl invocation.
// Any error encountered retrieving the list of machines is ignored.
func cachedMachineState(machID string) (ms *machine.MachineState) {
if machineStates == nil {
machineStates = make(map[string]*machine.MachineState)
ms, err := cAPI.Machines()
if err != nil {
return nil
}
for i, m := range ms {
machineStates[m.ID] = &ms[i]
}
}
return machineStates[machID]
}
// unitNameMangle tries to turn a string that might not be a unit name into a
// sensible unit name.
func unitNameMangle(arg string) string {
return maybeAppendDefaultUnitType(path.Base(arg))
}
func maybeAppendDefaultUnitType(arg string) string {
if !unit.RecognizedUnitType(arg) {
arg = unit.DefaultUnitType(arg)
}
return arg
}
// suToGlobal returns whether or not a schema.Unit refers to a global unit
func suToGlobal(su schema.Unit) bool {
u := job.Unit{
Unit: *schema.MapSchemaUnitOptionsToUnitFile(su.Options),
}
return u.IsGlobal()
}