This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
/
fleetctl.go
1280 lines (1095 loc) · 39.7 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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The fleet Authors
//
// 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"
"math"
"net"
"net/http"
"net/url"
"os"
"path"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
etcd "github.com/coreos/etcd/client"
"github.com/coreos/fleet/api"
"github.com/coreos/fleet/client"
"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"
defaultEndpoint = "unix:///var/run/fleet.sock"
defaultSleepTime = 2000 * time.Millisecond
)
var (
out *tabwriter.Writer
// 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
SSHUserName string
EtcdKeyPrefix string
}{}
// flags used by multiple commands
sharedFlags = struct {
Sign bool
Full bool
NoLegend bool
NoBlock bool
Replace bool
BlockAttempts int
Fields string
SSHPort int
}{}
// current command being executed
currentCommand string
// used to cache MachineStates
machineStates map[string]*machine.MachineState
cmdExitCode int
)
var cmdFleet = &cobra.Command{
Use: cliName,
Short: cliDescription,
// SuggestFor: []string{"fleetctl"},
Run: func(cCmd *cobra.Command, args []string) {
cCmd.HelpFunc()(cCmd, args)
},
}
func init() {
out = getTabOutWithWriter(os.Stdout)
// call this as early as possible to ensure we always have timestamps
// on fleetctl logs
log.EnableTimestamps()
cobra.EnablePrefixMatching = true
cmdFleet.PersistentFlags().BoolVar(&globalFlags.Help, "help", false, "Print usage information and exit")
cmdFleet.PersistentFlags().BoolVar(&globalFlags.Help, "h", false, "Print usage information and exit")
cmdFleet.PersistentFlags().BoolVar(&globalFlags.Debug, "debug", false, "Print out more debug information to stderr")
cmdFleet.PersistentFlags().BoolVar(&globalFlags.Version, "version", false, "Print the version and exit")
cmdFleet.PersistentFlags().StringVar(&globalFlags.ClientDriver, "driver", clientDriverAPI, fmt.Sprintf("Adapter used to execute fleetctl commands. Options include %q and %q.", clientDriverAPI, clientDriverEtcd))
cmdFleet.PersistentFlags().StringVar(&globalFlags.Endpoint, "endpoint", defaultEndpoint, fmt.Sprintf("Location of the fleet API if --driver=%s. Alternatively, if --driver=%s, location of the etcd API.", clientDriverAPI, clientDriverEtcd))
cmdFleet.PersistentFlags().StringVar(&globalFlags.EtcdKeyPrefix, "etcd-key-prefix", registry.DefaultKeyPrefix, "Keyspace for fleet data in etcd (development use only!)")
cmdFleet.PersistentFlags().StringVar(&globalFlags.KeyFile, "key-file", "", "Location of TLS key file used to secure communication with the fleet API or etcd")
cmdFleet.PersistentFlags().StringVar(&globalFlags.CertFile, "cert-file", "", "Location of TLS cert file used to secure communication with the fleet API or etcd")
cmdFleet.PersistentFlags().StringVar(&globalFlags.CAFile, "ca-file", "", "Location of TLS CA file used to secure communication with the fleet API or etcd")
cmdFleet.PersistentFlags().StringVar(&globalFlags.KnownHostsFile, "known-hosts-file", ssh.DefaultKnownHostsFile, "File used to store remote machine fingerprints. Ignored if strict host key checking is disabled.")
cmdFleet.PersistentFlags().BoolVar(&globalFlags.StrictHostKeyChecking, "strict-host-key-checking", true, "Verify host keys presented by remote machines before initiating SSH connections.")
cmdFleet.PersistentFlags().Float64Var(&globalFlags.SSHTimeout, "ssh-timeout", 10.0, "Amount of time in seconds to allow for SSH connection initialization before failing.")
cmdFleet.PersistentFlags().StringVar(&globalFlags.Tunnel, "tunnel", "", "Establish an SSH tunnel through the provided address for communication with fleet and etcd.")
cmdFleet.PersistentFlags().Float64Var(&globalFlags.RequestTimeout, "request-timeout", 3.0, "Amount of time in seconds to allow a single request before considering it failed.")
cmdFleet.PersistentFlags().StringVar(&globalFlags.SSHUserName, "ssh-username", "core", "Username to use when connecting to CoreOS instance.")
// deprecated flags
cmdFleet.PersistentFlags().BoolVar(&globalFlags.ExperimentalAPI, "experimental-api", true, "DEPRECATED: do not use this flag.")
cmdFleet.PersistentFlags().StringVar(&globalFlags.KeyFile, "etcd-keyfile", "", "DEPRECATED: do not use this flag.")
cmdFleet.PersistentFlags().StringVar(&globalFlags.CertFile, "etcd-certfile", "", "DEPRECATED: do not use this flag.")
cmdFleet.PersistentFlags().StringVar(&globalFlags.CAFile, "etcd-cafile", "", "DEPRECATED: do not use this flag.")
}
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 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() {
getFlagsFromEnv(cliName, cmdFleet.PersistentFlags())
if globalFlags.Debug {
log.EnableDebug()
}
// call this as early as possible to ensure we always have timestamps
// on fleetctl logs
log.EnableTimestamps()
if len(os.Args) == 1 {
cmdFleet.HelpFunc()(cmdFleet, os.Args)
os.Exit(0)
}
if os.Args[1] == "--version" || os.Args[1] == "-v" {
runVersion(cmdVersion, nil)
os.Exit(0)
}
// determine currentCommand. We only need this for --replace and its
// functional tests, so just handle those for now in the switch...
// "The rest" doesn't care about "currentCommand"
if len(os.Args) > 1 {
for i := 1; i < len(os.Args); i++ {
switch os.Args[i] {
case "start":
currentCommand = "start"
case "load":
currentCommand = "load"
case "submit":
currentCommand = "submit"
default:
continue
}
}
}
if sharedFlags.Sign {
stderr("WARNING: The signed/verified units feature is DEPRECATED and cannot be used.")
os.Exit(2)
}
// if --driver is not set, but --endpoint looks like an etcd
// server, set the driver to etcd
if globalFlags.Endpoint != "" && globalFlags.ClientDriver == "" {
if u, err := url.Parse(strings.Split(globalFlags.Endpoint, ",")[0]); err == nil {
if _, port, err := net.SplitHostPort(u.Host); err == nil && (port == "4001" || port == "2379") {
log.Debugf("Defaulting to --driver=%s as --endpoint appears to be etcd", clientDriverEtcd)
globalFlags.ClientDriver = clientDriverEtcd
}
}
}
cmdFleet.SetUsageFunc(usageFunc)
cmdFleet.SetHelpTemplate(`{{.UsageString}}`)
if err := cmdFleet.Execute(); err != nil {
stderr("cannot execute cmdFleet: %v", err)
}
os.Exit(cmdExitCode)
}
// 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 *pflag.FlagSet) {
alreadySet := make(map[string]bool)
fs.Visit(func(f *pflag.Flag) {
alreadySet[f.Name] = true
})
fs.VisitAll(func(f *pflag.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)
}
}
})
}
func getClientAPI(cCmd *cobra.Command) client.API {
var err error
cAPI, err = getClient(cCmd)
if err != nil {
stderr("Unable to initialize client: %v", err)
os.Exit(1)
}
return cAPI
}
// getClient initializes a client of fleet based on CLI flags
func getClient(cCmd *cobra.Command) (client.API, error) {
clientDriver, _ := cmdFleet.PersistentFlags().GetString("driver")
switch clientDriver {
case clientDriverAPI:
return getHTTPClient(cCmd)
case clientDriverEtcd:
return getRegistryClient(cCmd)
}
return nil, fmt.Errorf("unrecognized driver %q", clientDriver)
}
func getHTTPClient(cCmd *cobra.Command) (client.API, error) {
endPoint, _ := cmdFleet.PersistentFlags().GetString("endpoint")
endpoints := strings.Split(endPoint, ",")
if len(endpoints) > 1 {
log.Warningf("multiple endpoints provided but only the first (%s) is used", endpoints[0])
}
ep, err := url.Parse(endpoints[0])
if err != nil {
return nil, err
}
if len(ep.Scheme) == 0 {
return nil, errors.New("URL scheme undefined")
}
tun := getTunnelFlag(cCmd)
tunneling := tun != ""
dialUnix := ep.Scheme == "unix" || ep.Scheme == "file"
SSHUserName, _ := cmdFleet.PersistentFlags().GetString("ssh-username")
tunnelFunc := net.Dial
if tunneling {
sshClient, err := ssh.NewSSHClient(SSHUserName, tun, getChecker(cCmd), true, getSSHTimeoutFlag(cCmd))
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.Debugf("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"
}
CAFile, _ := cmdFleet.PersistentFlags().GetString("ca-file")
CertFile, _ := cmdFleet.PersistentFlags().GetString("cert-file")
KeyFile, _ := cmdFleet.PersistentFlags().GetString("key-file")
tlsConfig, err := pkg.ReadTLSConfigFiles(CAFile, CertFile, 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 getEndpoint() string {
// The user explicitly set --experimental-api=false, so it trumps the
// --driver flag. This behavior exists for backwards-compatibilty.
experimentalAPI, _ := cmdFleet.PersistentFlags().GetBool("experimental-api")
endPoint, _ := cmdFleet.PersistentFlags().GetString("endpoint")
if !experimentalAPI {
// Additionally, if the user set --experimental-api=false and did
// not change the value of --endpoint, they likely want to use the
// old default value.
if endPoint == defaultEndpoint {
endPoint = "http://127.0.0.1:2379,http://127.0.0.1:4001"
}
}
return endPoint
}
func getRegistryClient(cCmd *cobra.Command) (client.API, error) {
var dial func(string, string) (net.Conn, error)
SSHUserName, _ := cmdFleet.PersistentFlags().GetString("ssh-username")
tun := getTunnelFlag(cCmd)
if tun != "" {
sshClient, err := ssh.NewSSHClient(SSHUserName, tun, getChecker(cCmd), false, getSSHTimeoutFlag(cCmd))
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)
}
}
CAFile, _ := cmdFleet.PersistentFlags().GetString("ca-file")
CertFile, _ := cmdFleet.PersistentFlags().GetString("cert-file")
KeyFile, _ := cmdFleet.PersistentFlags().GetString("key-file")
tlsConfig, err := pkg.ReadTLSConfigFiles(CAFile, CertFile, KeyFile)
if err != nil {
return nil, err
}
trans := &http.Transport{
Dial: dial,
TLSClientConfig: tlsConfig,
}
eCfg := etcd.Config{
Endpoints: strings.Split(getEndpoint(), ","),
Transport: trans,
HeaderTimeoutPerRequest: getRequestTimeoutFlag(cCmd),
}
eClient, err := etcd.New(eCfg)
if err != nil {
return nil, err
}
etcdKeyPrefix, _ := cmdFleet.PersistentFlags().GetString("etcd-key-prefix")
kAPI := etcd.NewKeysAPI(eClient)
reg := registry.NewEtcdRegistry(kAPI, 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(cCmd *cobra.Command) *ssh.HostKeyChecker {
strictHostKeyChecking, _ := cmdFleet.PersistentFlags().GetBool("strict-host-key-checking")
if !strictHostKeyChecking {
return nil
}
knownHostsFile, _ := cmdFleet.PersistentFlags().GetString("known-hosts-file")
keyFile := ssh.NewHostKeyFile(knownHostsFile)
return ssh.NewHostKeyChecker(keyFile)
}
// getUnitFile attempts to get a UnitFile configuration
// It takes a unit file name as a parameter and tries first to lookup
// the unit from the local disk. If it fails, it checks if the provided
// file name may reference an instance of a template unit, if so, it
// tries to get the template configuration either from the registry or
// the local disk.
// It returns a UnitFile configuration or nil; and any error ecountered
func getUnitFile(cCmd *cobra.Command, file string) (*unit.UnitFile, error) {
var uf *unit.UnitFile
name := unitNameMangle(file)
log.Debugf("Looking for Unit(%s) or its corresponding template", name)
// Assume that the file references a local unit file on disk and
// attempt to load it, if it exists
if _, err := os.Stat(file); !os.IsNotExist(err) {
uf, err = getUnitFromFile(file)
if err != nil {
return nil, fmt.Errorf("failed getting Unit(%s) from file: %v", file, err)
}
} else {
// Otherwise (if the unit file does not exist), check if the
// name appears to be an instance of a template unit
info := unit.NewUnitNameInfo(name)
if info == nil {
return nil, fmt.Errorf("error extracting information from unit name %s", name)
} else if !info.IsInstance() {
return nil, fmt.Errorf("unable to find Unit(%s) in Registry or on filesystem", name)
}
// If it is an instance check for a corresponding template
// unit in the Registry or disk.
// If we found a template unit, later we create a
// near-identical instance unit in the Registry - same
// unit file as the template, but different name
uf, err = getUnitFileFromTemplate(cCmd, info, file)
if err != nil {
return nil, fmt.Errorf("failed getting Unit(%s) from template: %v", file, err)
}
}
log.Debugf("Found Unit(%s)", name)
return uf, nil
}
// 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.Debugf("Unit(%s) found in local filesystem", unitName)
return unit.NewUnitFile(string(out))
}
// getUnitFileFromTemplate attempts to get a Unit from a template unit that
// is either in the registry or on the file system
// It takes two arguments, the template information and the unit file name
// It returns the Unit or nil; and any error encountered
func getUnitFileFromTemplate(cCmd *cobra.Command, uni *unit.UnitNameInfo, fileName string) (*unit.UnitFile, error) {
var uf *unit.UnitFile
tmpl, err := cAPI.Unit(uni.Template)
if err != nil {
return nil, fmt.Errorf("error retrieving template Unit(%s) from Registry: %v", uni.Template, err)
}
if tmpl != nil {
isLocalUnitDifferent(cCmd, fileName, tmpl, false)
uf = schema.MapSchemaUnitOptionsToUnitFile(tmpl.Options)
log.Debugf("Template Unit(%s) found in registry", uni.Template)
} else {
// Finally, if we could not find a template unit in the Registry,
// check the local disk for one instead
filePath := path.Join(path.Dir(fileName), uni.Template)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("unable to find template Unit(%s) in Registry or on filesystem", uni.Template)
}
uf, err = getUnitFromFile(filePath)
if err != nil {
return nil, fmt.Errorf("unable to load template Unit(%s) from file: %v", uni.Template, err)
}
}
return uf, nil
}
func getTunnelFlag(cCmd *cobra.Command) string {
tun, _ := cmdFleet.PersistentFlags().GetString("tunnel")
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func getSSHTimeoutFlag(cCmd *cobra.Command) time.Duration {
sshTimeout, _ := cmdFleet.PersistentFlags().GetFloat64("ssh-timeout")
return time.Duration(sshTimeout*1000) * time.Millisecond
}
func getRequestTimeoutFlag(cCmd *cobra.Command) time.Duration {
reqTimeout, _ := cmdFleet.PersistentFlags().GetFloat64("request-timeout")
return time.Duration(reqTimeout*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.Debugf("Created Unit(%s) in Registry", name)
return &u, nil
}
// checkReplaceUnitState checks if the unit should be replaced.
// It takes a Unit object as a parameter.
// It returns 0 on success and if the unit should be replaced, 1 if the
// unit should not be replaced; and any error encountered.
func checkReplaceUnitState(unit *schema.Unit) (int, error) {
// We replace units only for 'submit', 'load' and
// 'start' commands.
allowedReplace := map[string][]job.JobState{
"submit": []job.JobState{
job.JobStateInactive,
},
"load": []job.JobState{
job.JobStateInactive,
job.JobStateLoaded,
},
"start": []job.JobState{
job.JobStateInactive,
job.JobStateLoaded,
job.JobStateLaunched,
},
}
if allowedJobs, ok := allowedReplace[currentCommand]; ok {
for _, j := range allowedJobs {
if job.JobState(unit.DesiredState) == j {
return 0, nil
}
}
// Report back to caller that we are not allowed to
// cross unit transition states
stderr("Warning: can not replace Unit(%s) in state '%s', use the appropriate command", unit.Name, unit.DesiredState)
} else {
// This function should only be called from 'submit',
// 'load' and 'start' upper paths.
return 1, fmt.Errorf("error: replacing units is not supported in this context")
}
return 1, nil
}
// checkUnitCreation checks if the unit should be created.
// It takes a unit file path as a parameter.
// It returns 0 on success and if the unit should be created, 1 if the
// unit should not be created; and any error encountered.
func checkUnitCreation(cCmd *cobra.Command, arg string) (int, error) {
name := unitNameMangle(arg)
// First, check if there already exists a Unit by the given name in the Registry
unit, err := cAPI.Unit(name)
if err != nil {
return 1, fmt.Errorf("error retrieving Unit(%s) from Registry: %v", name, err)
}
replace, _ := cCmd.Flags().GetBool("replace")
// check if the unit is running
if unit == nil {
if replace {
log.Debugf("Unit(%s) was not found in Registry", name)
}
// Create a new unit
return 0, nil
}
// if replace is not set then we warn in case the units differ
different, err := isLocalUnitDifferent(cCmd, arg, unit, false)
// if replace is set then we fail for errors
if replace {
if err != nil {
return 1, err
} else if different {
return checkReplaceUnitState(unit)
} else {
stdout("Found same Unit(%s) in Registry, nothing to do", unit.Name)
}
} else if different == false {
log.Debugf("Found same Unit(%s) in Registry, no need to recreate it", name)
}
return 1, 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(cCmd *cobra.Command, args []string) error {
errchan := make(chan error)
blockAttempts, _ := cCmd.Flags().GetInt("block-attempts")
var wg sync.WaitGroup
for _, arg := range args {
arg = maybeAppendDefaultUnitType(arg)
name := unitNameMangle(arg)
ret, err := checkUnitCreation(cCmd, arg)
if err != nil {
return err
} else if ret != 0 {
continue
}
// Assume that the name references a local unit file on
// disk or if it is an instance unit and if so get its
// corresponding unit
uf, err := getUnitFile(cCmd, arg)
if err != nil {
return err
}
_, err = createUnit(name, uf)
if err != nil {
return err
}
wg.Add(1)
go checkUnitState(name, job.JobStateInactive, blockAttempts, os.Stdout, &wg, errchan)
}
go func() {
wg.Wait()
close(errchan)
}()
haserr := false
for msg := range errchan {
stderr("Error waiting on unit creation: %v", msg)
haserr = true
}
if haserr {
return fmt.Errorf("One or more errors creating units")
}
return nil
}
// matchLocalFileAndUnit compares a file with a Unit
// Returns true if the contents of the file matches the unit one, false
// otherwise; and any error encountered.
func matchLocalFileAndUnit(file string, su *schema.Unit) (bool, error) {
a := schema.MapSchemaUnitOptionsToUnitFile(su.Options)
_, err := os.Stat(file)
if err != nil {
return false, err
}
b, err := getUnitFromFile(file)
if err != nil {
return false, err
}
return unit.MatchUnitFiles(a, b), nil
}
// isLocalUnitDifferent compares a Unit on the file system with a one
// provided from the Registry.
// isLocalUnitDifferent first tries to load the passed Unit from the
// local file system and compares it with the Unit that is in the
// Registry. If it fails to load that Unit from the filesystem and
// fatal was not set, it will check again if that file name is an
// instance of a template, if so it will load the template Unit and
// compare it with the provided Unit.
// It takes three arguments; a path to the local Unit on the file system,
// the Unit in the registry, and a last boolean to fail in case fatal errors
// happen.
// Returns true if the local Unit on file system is different from the
// one provided, false otherwise; and any error encountered.
func isLocalUnitDifferent(cCmd *cobra.Command, file string, su *schema.Unit, fatal bool) (bool, error) {
replace, _ := cCmd.Flags().GetBool("replace")
result, err := matchLocalFileAndUnit(file, su)
if err == nil {
// Warn in case unit differs from local file
if result == false && !replace {
stderr("WARNING: Unit %s in registry differs from local unit file %s. Add --replace to override.", su.Name, file)
}
return !result, nil
} else if fatal {
return false, err
}
info := unit.NewUnitNameInfo(path.Base(file))
if info == nil {
return false, fmt.Errorf("error extracting information from unit name %s", file)
} else if !info.IsInstance() {
return false, fmt.Errorf("error Unit %s does not seem to be a template unit", file)
}
templFile := path.Join(path.Dir(file), info.Template)
result, err = matchLocalFileAndUnit(templFile, su)
if err == nil {
// Warn in case unit differs from local template unit file
if result == false && !replace {
stderr("WARNING: Unit %s in registry differs from local template unit file %s. Add --replace to override.", su.Name, file)
}
return !result, nil
}
return false, err
}
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.Debugf("Unit(%s) already %s, skipping.", u.Name, u.DesiredState)
continue
}
log.Debugf("Setting Unit(%s) target state to %s", u.Name, state)
if err := cAPI.SetUnitTargetState(u.Name, string(state)); err != nil {
return nil, err
}
triggered = append(triggered, u)
}
return triggered, nil
}
// getBlockAttempts gets the correct value of how many attempts to try
// before giving up on an operation.
// It returns a negative value which means do not block, if zero is
// returned then it means try forever, and if a positive value is
// returned then try up to that value
func getBlockAttempts(cCmd *cobra.Command) int {
// By default we wait forever
var attempts int = 0
if sharedFlags.BlockAttempts > 0 {
attempts = sharedFlags.BlockAttempts
}
if sharedFlags.NoBlock {
attempts = -1
}
return attempts
}
// tryWaitForUnitStates tries to wait for units to reach the desired state.
// It takes 5 arguments, the units to wait for, the desired state, the
// desired JobState, how many attempts before timing out and a writer
// interface.
// tryWaitForUnitStates polls each of the indicated units until they
// reach the desired state. If maxAttempts is negative, then it will not
// wait, it will assume that all units reached their desired state.
// If maxAttempts is zero tryWaitForUnitStates will retry forever, and
// if it is greater than zero, it will retry up to the indicated value.
// It returns nil on success or error on failure.
func tryWaitForUnitStates(units []string, state string, js job.JobState, maxAttempts int, out io.Writer) error {
// We do not wait just assume we reached the desired state
if maxAttempts <= -1 {
for _, name := range units {
stdout("Triggered unit %s %s", name, state)
}
return nil
}
errchan := waitForUnitStates(units, js, maxAttempts, out)
for err := range errchan {
stderr("Error waiting for units: %v", err)
return err
}
return 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() {