-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
1032 lines (940 loc) · 31.2 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/subpop/go-log"
"golang.org/x/term"
"github.com/briandowns/spinner"
"github.com/redhatinsights/rhc/internal/http"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
type LogMessage struct {
level log.Level
message error
}
const (
colorGreen = "\u001B[32m"
colorYellow = "\u001B[33m"
colorRed = "\u001B[31m"
colorReset = "\u001B[0m"
)
// userInterfaceSettings manages standard output preference.
// It tracks colors, icons and machine-readable output (e.g. json).
//
// It is instantiated via uiSettings by calling configureUISettings.
type userInterfaceSettings struct {
// isMachineReadable describes the machine-readable mode (e.g., `--format json`)
isMachineReadable bool
// isRich describes the ability to display colors and animations
isRich bool
iconOK string
iconInfo string
iconError string
}
// uiSettings is an instance that keeps actual data of output preference.
//
// It is managed by calling the configureUISettings method.
var uiSettings = userInterfaceSettings{}
// configureUISettings is called by the CLI library when it loads up.
// It sets up the uiSettings object.
func configureUISettings(ctx *cli.Context) {
if ctx.Bool("no-color") {
uiSettings = userInterfaceSettings{
isRich: false,
isMachineReadable: false,
iconOK: "✓",
iconInfo: "·",
iconError: "𐄂",
}
} else {
uiSettings = userInterfaceSettings{
isRich: true,
isMachineReadable: false,
iconOK: colorGreen + "●" + colorReset,
iconInfo: colorYellow + "●" + colorReset,
iconError: colorRed + "●" + colorReset,
}
}
}
// showProgress calls function and, when it is possible display spinner with
// some progress message.
func showProgress(
progressMessage string,
function func() error,
) error {
var s *spinner.Spinner
if uiSettings.isRich {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = progressMessage
s.Start()
// Stop spinner after running function
defer func() { s.Stop() }()
}
return function()
}
// showTimeDuration shows table with duration of each sub-action
func showTimeDuration(durations map[string]time.Duration) {
if log.CurrentLevel() >= log.LevelDebug {
fmt.Println()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "STEP\tDURATION\t")
for step, duration := range durations {
_, _ = fmt.Fprintf(w, "%v\t%v\t\n", step, duration.Truncate(time.Millisecond))
}
_ = w.Flush()
}
}
// showErrorMessages shows table with all error messages gathered during action
func showErrorMessages(action string, errorMessages map[string]LogMessage) error {
if hasPriorityErrors(errorMessages, log.CurrentLevel()) {
if !uiSettings.isMachineReadable {
fmt.Println()
fmt.Printf("The following errors were encountered during %s:\n\n", action)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(w, "TYPE\tSTEP\tERROR\t")
for step, logMsg := range errorMessages {
if logMsg.level <= log.CurrentLevel() {
_, _ = fmt.Fprintf(w, "%v\t%v\t%v\n", logMsg.level, step, logMsg.message)
}
}
_ = w.Flush()
if hasPriorityErrors(errorMessages, log.LevelError) {
return cli.Exit("", 1)
}
}
}
return nil
}
// getConfProfile will retrieve the profile the system is configured
func getConfProfile(client *http.Client) (map[string]interface{}, error) {
var profile map[string]interface{}
url, err := GuessAPIURL()
if err != nil {
return nil, err
}
res, err := client.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("cannot get system profile: %s", res.Status)
}
// Read the response body
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &profile); err != nil {
return nil, fmt.Errorf("cannot unmarshal profile: %s", res.Status)
}
return profile, nil
}
// showConfProfile shows a list of the system profile enabled features gathered from API
// https://github.com/RedHatInsights/config-manager/blob/master/internal/http/v2/openapi.json
func showConfProfile(p *map[string]interface{}) {
services := []string{}
for service, status := range *p {
switch status.(type) {
case bool:
if service == "active" {
service = "remote configuration"
}
services = append(services, service)
}
}
output := strings.Join(services, `, `)
fmt.Printf("%v", output)
}
// registerRHSM tries to register system against Red Hat Subscription Management server (candlepin server)
func registerRHSM(ctx *cli.Context) (string, error) {
uuid, err := getConsumerUUID()
if err != nil {
return "Unable to get consumer UUID", cli.Exit(err, 1)
}
var successMsg string
if uuid == "" {
username := ctx.String("username")
password := ctx.String("password")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
if len(activationKeys) == 0 {
if username == "" {
password = ""
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Username: ")
_ = scanner.Scan()
username = strings.TrimSpace(scanner.Text())
}
if password == "" {
fmt.Print("Password: ")
data, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "Unable to read password", cli.Exit(err, 1)
}
password = string(data)
fmt.Printf("\n\n")
}
}
var s *spinner.Spinner
if uiSettings.isRich {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Connecting to Red Hat Subscription Management..."
s.Start()
defer s.Stop()
}
var err error
if len(activationKeys) > 0 {
err = registerActivationKey(
organization,
ctx.StringSlice("activation-key"),
ctx.String("server"))
} else {
var orgs []string
if organization != "" {
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
} else {
orgs, err = registerUsernamePassword(username, password, "", ctx.String("server"))
/* When organization was not specified using CLI option --organization, and it is
required, because user is member of more than one organization, then ask for
the organization. */
if len(orgs) > 0 {
// Stop spinner to be able to display message and ask for organization
if uiSettings.isRich {
s.Stop()
}
// Ask for organization and display hint with list of organizations
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Available Organizations:")
writer := tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
for i, org := range orgs {
_, _ = fmt.Fprintf(writer, "%v\t", org)
if (i+1)%4 == 0 {
_, _ = fmt.Fprint(writer, "\n")
}
}
_ = writer.Flush()
fmt.Print("\nOrganization: ")
_ = scanner.Scan()
organization = strings.TrimSpace(scanner.Text())
fmt.Printf("\n")
// Start spinner again
if uiSettings.isRich {
s.Start()
}
// Try to register once again with given organization
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
}
}
}
if err != nil {
return "Unable to register system to RHSM", cli.Exit(err, 1)
}
successMsg = "Connected to Red Hat Subscription Management"
} else {
successMsg = "This system is already connected to Red Hat Subscription Management"
}
return successMsg, nil
}
// beforeConnectAction ensures that user has supplied a correct CLI options
// and there is no conflict between provided options
func beforeConnectAction(ctx *cli.Context) error {
// First check if machine-readable format is used
err := setupFormatOption(ctx)
if err != nil {
return err
}
username := ctx.String("username")
password := ctx.String("password")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
if len(activationKeys) > 0 {
if username != "" {
return fmt.Errorf("--username and --activation-key can not be used together")
}
if organization == "" {
return fmt.Errorf("--organization is required, when --activation-key is used")
}
}
// When machine-readable format is used, then additional requirements have to be met
if uiSettings.isMachineReadable {
if username != "" {
if password == "" {
return fmt.Errorf("--password is required, when --username and machine-readable format are used")
}
}
}
return nil
}
// connectAction tries to register system against Red Hat Subscription Management,
// gather the profile information that the system will configure
// connect system to Red Hat Insights, and it also tries to start rhcd service
func connectAction(ctx *cli.Context) error {
var connectResult ConnectResult
connectResult.format = ctx.String("format")
uid := os.Getuid()
if uid != 0 {
errMsg := "non-root user cannot connect system"
exitCode := 1
if uiSettings.isMachineReadable {
connectResult.UID = uid
connectResult.UIDError = errMsg
return cli.Exit(connectResult, exitCode)
} else {
return cli.Exit(fmt.Errorf("error: %s", errMsg), exitCode)
}
}
hostname, err := os.Hostname()
if uiSettings.isMachineReadable {
connectResult.Hostname = hostname
}
if err != nil {
exitCode := 1
if uiSettings.isMachineReadable {
connectResult.HostnameError = err.Error()
return cli.Exit(connectResult, exitCode)
} else {
return cli.Exit(err, exitCode)
}
}
interactivePrintf("Connecting %v to %v.\nThis might take a few seconds.\n\n", hostname, Provider)
var start time.Time
durations := make(map[string]time.Duration)
errorMessages := make(map[string]LogMessage)
/* 1. Register to RHSM, because we need to get consumer certificate. This blocks following action */
start = time.Now()
var returnedMsg string
returnedMsg, err = registerRHSM(ctx)
if err != nil {
connectResult.RHSMConnected = false
errorMessages["rhsm"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot connect to Red Hat Subscription Management: %w",
err)}
if uiSettings.isMachineReadable {
connectResult.RHSMConnectError = errorMessages["rhsm"].message.Error()
} else {
fmt.Printf(
"%v Cannot connect to Red Hat Subscription Management\n",
uiSettings.iconError,
)
}
} else {
connectResult.RHSMConnected = true
interactivePrintf("%v %v\n", uiSettings.iconOK, returnedMsg)
}
durations["rhsm"] = time.Since(start)
/* 2. Register insights-client */
if errors, exist := errorMessages["rhsm"]; exist {
if errors.level == log.LevelError {
fmt.Printf(
"%v Skipping connection to Red Hat Insights\n",
uiSettings.iconError,
)
}
} else {
start = time.Now()
err = showProgress(" Connecting to Red Hat Insights...", registerInsights)
if err != nil {
connectResult.InsightsConnected = false
errorMessages["insights"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot connect to Red Hat Insights: %w", err)}
if uiSettings.isMachineReadable {
connectResult.InsightsError = errorMessages["insights"].message.Error()
} else {
fmt.Printf("%v Cannot connect to Red Hat Insights\n", uiSettings.iconError)
}
} else {
connectResult.InsightsConnected = true
interactivePrintf("%v Connected to Red Hat Insights\n", uiSettings.iconOK)
}
durations["insights"] = time.Since(start)
}
/* 3. Start yggdrasil (rhcd) service */
if rhsmErrMsg, exist := errorMessages["rhsm"]; exist && rhsmErrMsg.level == log.LevelError {
connectResult.YggdrasilStarted = false
interactivePrintf(
"%v Skipping activation of %v service\n",
uiSettings.iconError,
ServiceName,
)
} else {
start = time.Now()
progressMessage := fmt.Sprintf(" Activating the %v service", ServiceName)
err = showProgress(progressMessage, activateService)
if err != nil {
connectResult.YggdrasilStarted = false
errorMessages[ServiceName] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("cannot activate %s service: %w",
ServiceName, err)}
if uiSettings.isMachineReadable {
connectResult.YggdrasilStartedError = errorMessages[ServiceName].message.Error()
} else {
fmt.Printf("%v Cannot activate the %v service\n", uiSettings.iconError, ServiceName)
}
} else {
connectResult.YggdrasilStarted = true
interactivePrintf("%v Activated the %v service\n", uiSettings.iconOK, ServiceName)
}
durations[ServiceName] = time.Since(start)
}
/* 4. Show Configuration Profile */
if len(errorMessages) == 0 {
// Update the configuration file
// Call D-bus to get the CA directory from rhsm
if err = getRHSMConfigOption("rhsm.ca_cert_dir", &config.CADir); err != nil {
errorMessages[ServiceName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot get rhsm configuration: %w",
err)}
}
// Generate a new http client configured with mutual TLS certificate
tlsConfig, err := config.CreateTLSClientConfig()
if err != nil {
errorMessages[ServiceName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot configure TLS: %w",
err)}
}
client := http.NewHTTPClient(tlsConfig)
// Get the user profile
profile, err := getConfProfile(client)
if err != nil {
errorMessages[ServiceName] = LogMessage{
level: log.LevelWarn,
message: fmt.Errorf("cannot get the user profile: %w",
err)}
} else {
if !uiSettings.isMachineReadable {
fmt.Printf("%v Enabled console.redhat.com services: ", uiSettings.iconInfo)
showConfProfile(&profile)
fmt.Printf("\n")
}
}
interactivePrintf("\nSuccessfully connected to Red Hat!\n")
}
if !uiSettings.isMachineReadable {
/* 5. Show footer message */
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
/* 6. Optionally display duration time of each sub-action */
showTimeDuration(durations)
}
err = showErrorMessages("connect", errorMessages)
if err != nil {
return err
}
return cli.Exit(connectResult, 0)
}
// setupFormatOption ensures the user has supplied a correct `--format` flag
// and set values in uiSettings, when JSON format is used.
func setupFormatOption(ctx *cli.Context) error {
// This is run after the `app.Before()` has been run,
// the uiSettings is already set up for us to modify.
format := ctx.String("format")
switch format {
case "":
return nil
case "json":
uiSettings.isMachineReadable = true
uiSettings.isRich = false
return nil
default:
err := fmt.Errorf(
"unsupported format: %s (supported formats: %s)",
format,
`"json"`,
)
return cli.Exit(err, 1)
}
}
// DisconnectResult is structure holding information about result of
// disconnect command. The result could be printed in machine-readable format.
type DisconnectResult struct {
Hostname string `json:"hostname"`
HostnameError string `json:"hostname_error,omitempty"`
UID int `json:"uid"`
UIDError string `json:"uid_error,omitempty"`
RHSMDisconnected bool `json:"rhsm_disconnected"`
RHSMDisconnectedError string `json:"rhsm_disconnect_error,omitempty"`
InsightsDisconnected bool `json:"insights_disconnected"`
InsightsDisconnectedError string `json:"insights_disconnected_error,omitempty"`
YggdrasilStopped bool `json:"yggdrasil_stopped"`
YggdrasilStoppedError string `json:"yggdrasil_stopped_error,omitempty"`
format string
}
// Error implement error interface for structure DisconnectResult
func (disconnectResult DisconnectResult) Error() string {
var result string
switch disconnectResult.format {
case "json":
data, err := json.MarshalIndent(disconnectResult, "", " ")
if err != nil {
return err.Error()
}
result = string(data)
default:
result = "error: unsupported document format: " + disconnectResult.format
}
return result
}
// ConnectResult is structure holding information about results
// of connect command. The result could be printed in machine-readable format.
type ConnectResult struct {
Hostname string `json:"hostname"`
HostnameError string `json:"hostname_error,omitempty"`
UID int `json:"uid"`
UIDError string `json:"uid_error,omitempty"`
RHSMConnected bool `json:"rhsm_connected"`
RHSMConnectError string `json:"rhsm_connect_error,omitempty"`
InsightsConnected bool `json:"insights_connected"`
InsightsError string `json:"insights_connect_error,omitempty"`
YggdrasilStarted bool `json:"yggdrasil_started"`
YggdrasilStartedError string `json:"yggdrasil_started_error,omitempty"`
format string
}
// Error implement error interface for structure ConnectResult
func (connectResult ConnectResult) Error() string {
var result string
switch connectResult.format {
case "json":
data, err := json.MarshalIndent(connectResult, "", " ")
if err != nil {
return err.Error()
}
result = string(data)
default:
result = "error: unsupported document format: " + connectResult.format
}
return result
}
// beforeDisconnectAction ensures the used has supplied a correct `--format` flag
func beforeDisconnectAction(ctx *cli.Context) error {
return setupFormatOption(ctx)
}
// interactivePrintf is method for printing human-readable output. It suppresses output, when
// machine-readable format is used.
func interactivePrintf(format string, a ...interface{}) {
if !uiSettings.isMachineReadable {
fmt.Printf(format, a...)
}
}
// disconnectAction tries to stop (yggdrasil) rhcd service, disconnect from Red Hat Insights,
// and finally it unregisters system from Red Hat Subscription Management
func disconnectAction(ctx *cli.Context) error {
var disconnectResult DisconnectResult
disconnectResult.format = ctx.String("format")
uid := os.Getuid()
if uid != 0 {
errMsg := "non-root user cannot disconnect system"
exitCode := 1
if uiSettings.isMachineReadable {
disconnectResult.UID = uid
disconnectResult.UIDError = errMsg
return cli.Exit(disconnectResult, exitCode)
} else {
return cli.Exit(fmt.Errorf("error: %s", errMsg), exitCode)
}
}
hostname, err := os.Hostname()
if uiSettings.isMachineReadable {
disconnectResult.Hostname = hostname
}
if err != nil {
exitCode := 1
if uiSettings.isMachineReadable {
disconnectResult.HostnameError = err.Error()
return cli.Exit(disconnectResult, exitCode)
} else {
return cli.Exit(err, exitCode)
}
}
interactivePrintf("Disconnecting %v from %v.\nThis might take a few seconds.\n\n", hostname, Provider)
var start time.Time
durations := make(map[string]time.Duration)
errorMessages := make(map[string]LogMessage)
/* 1. Deactivate yggdrasil (rhcd) service */
start = time.Now()
progressMessage := fmt.Sprintf(" Deactivating the %v service", ServiceName)
err = showProgress(progressMessage, deactivateService)
if err != nil {
errMsg := fmt.Sprintf("Cannot deactivate %s service: %v", ServiceName, err)
errorMessages[ServiceName] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("%v", errMsg)}
disconnectResult.YggdrasilStopped = false
disconnectResult.YggdrasilStoppedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.YggdrasilStopped = true
interactivePrintf("%v Deactivated the %v service\n", uiSettings.iconOK, ServiceName)
}
durations[ServiceName] = time.Since(start)
/* 2. Disconnect from Red Hat Insights */
start = time.Now()
err = showProgress(" Disconnecting from Red Hat Insights...", unregisterInsights)
if err != nil {
errMsg := fmt.Sprintf("Cannot disconnect from Red Hat Insights: %v", err)
errorMessages["insights"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("%v", errMsg)}
disconnectResult.InsightsDisconnected = false
disconnectResult.InsightsDisconnectedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.InsightsDisconnected = true
interactivePrintf("%v Disconnected from Red Hat Insights\n", uiSettings.iconOK)
}
durations["insights"] = time.Since(start)
/* 3. Unregister system from Red Hat Subscription Management */
err = showProgress(
" Disconnecting from Red Hat Subscription Management...", unregister,
)
if err != nil {
errMsg := fmt.Sprintf("Cannot disconnect from Red Hat Subscription Management: %v", err)
errorMessages["rhsm"] = LogMessage{
level: log.LevelError,
message: fmt.Errorf("%v", errMsg)}
disconnectResult.RHSMDisconnected = false
disconnectResult.RHSMDisconnectedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.RHSMDisconnected = true
interactivePrintf("%v Disconnected from Red Hat Subscription Management\n", uiSettings.iconOK)
}
durations["rhsm"] = time.Since(start)
if !uiSettings.isMachineReadable {
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
showTimeDuration(durations)
err = showErrorMessages("disconnect", errorMessages)
if err != nil {
return err
}
}
return cli.Exit(disconnectResult, 0)
}
// canonicalFactAction tries to gather canonical facts about system,
// and it prints JSON with facts to stdout.
func canonicalFactAction(_ *cli.Context) error {
// NOTE: CLI context is not useful for anything
facts, err := GetCanonicalFacts()
if err != nil {
return cli.Exit(err, 1)
}
data, err := json.MarshalIndent(facts, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
// SystemStatus is structure holding information about system status
// When more file format is supported, then add more tags for fields
// like xml:"hostname"
type SystemStatus struct {
SystemHostname string `json:"hostname"`
HostnameError string `json:"hostname_error,omitempty"`
RHSMConnected bool `json:"rhsm_connected"`
RHSMError string `json:"rhsm_error,omitempty"`
InsightsConnected bool `json:"insights_connected"`
InsightsError string `json:"insights_error,omitempty"`
YggdrasilRunning bool `json:"yggdrasil_running"`
YggdrasilError string `json:"yggdrasil_error,omitempty"`
returnCode int
}
// printJSONStatus tries to print the system status as JSON to stdout.
// When marshaling of systemStatus fails, then error is returned
func printJSONStatus(systemStatus *SystemStatus) error {
data, err := json.MarshalIndent(systemStatus, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
// beforeStatusAction ensures the user has supplied a correct `--format` flag.
func beforeStatusAction(ctx *cli.Context) error {
return setupFormatOption(ctx)
}
// statusAction tries to print status of system. It means that it gives
// answer on following questions:
// 1. Is system registered to Red Hat Subscription Management?
// 2. Is system connected to Red Hat Insights?
// 3. Is yggdrasil.service (rhcd.service) running?
// Status can be printed as human-readable text or machine-readable JSON document.
// Format is influenced by --format json CLI option stored in CLI context
func statusAction(ctx *cli.Context) (err error) {
var systemStatus SystemStatus
var machineReadablePrintFunc func(systemStatus *SystemStatus) error
format := ctx.String("format")
switch format {
case "json":
machineReadablePrintFunc = printJSONStatus
default:
break
}
// When printing of status is requested, then print machine-readable file format
// at the end of this function
if uiSettings.isMachineReadable {
defer func(systemStatus *SystemStatus) {
err = machineReadablePrintFunc(systemStatus)
// When it was not possible to print status to machine-readable format, then
// change returned error to CLI exit error to be able to set exit code to
// a non-zero value
if err != nil {
err = cli.Exit(
fmt.Errorf("unable to print status as %s document: %s", format, err.Error()),
1)
}
// When any of status is not correct, then return 1 exit code
if systemStatus.returnCode != 0 {
err = cli.Exit("", 1)
}
}(&systemStatus)
}
hostname, err := os.Hostname()
if err != nil {
if uiSettings.isMachineReadable {
systemStatus.HostnameError = err.Error()
} else {
return cli.Exit(err, 1)
}
}
if uiSettings.isMachineReadable {
systemStatus.SystemHostname = hostname
} else {
fmt.Printf("Connection status for %v:\n\n", hostname)
}
/* 1. Get Status of RHSM */
err = rhsmStatus(&systemStatus)
if err != nil {
return cli.Exit(err, 1)
}
/* 2. Get status of insights-client */
insightStatus(&systemStatus)
/* 3. Get status of yggdrasil (rhcd) service */
err = serviceStatus(&systemStatus)
if err != nil {
return cli.Exit(err, 1)
}
if !uiSettings.isMachineReadable {
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
}
// At the end check if all statuses are correct.
// If not, return 1 exit code without any message.
if systemStatus.returnCode != 0 {
return cli.Exit("", 1)
}
return nil
}
// mainAction is triggered in the case, when no sub-command is specified
func mainAction(c *cli.Context) error {
type GenerationFunc func() (string, error)
var generationFunc GenerationFunc
if c.Bool("generate-man-page") {
generationFunc = c.App.ToMan
} else if c.Bool("generate-markdown") {
generationFunc = c.App.ToMarkdown
} else {
cli.ShowAppHelpAndExit(c, 0)
}
data, err := generationFunc()
if err != nil {
return cli.Exit(err, 1)
}
fmt.Println(data)
return nil
}
// beforeAction is triggered before other actions are triggered
func beforeAction(c *cli.Context) error {
/* Load the configuration values from the config file specified*/
filePath := c.String("config")
if filePath != "" {
inputSource, err := altsrc.NewTomlSourceFromFile(filePath)
if err != nil {
return err
}
if err := altsrc.ApplyInputSourceValues(c, inputSource, c.App.Flags); err != nil {
return err
}
}
config = Conf{
LogLevel: c.String(cliLogLevel),
CertFile: c.String(cliCertFile),
KeyFile: c.String(cliKeyFile),
}
level, err := log.ParseLevel(config.LogLevel)
if err != nil {
return cli.Exit(err, 1)
}
log.SetLevel(level)
// When environment variable NO_COLOR or --no-color CLI option is set, then do not display colors
// and animations too. The NO_COLOR environment variable have to have value "1" or "true",
// "True", "TRUE" to take effect
// When no-color is not set, then try to detect if the output goes to some file. In this case
// colors nor animations will not be printed to file.
if !isTerminal(os.Stdout.Fd()) {
err := c.Set("no-color", "true")
if err != nil {
log.Debug("Unable to set no-color flag to \"true\"")
}
}
// Set up standard output preference: colors, icons, etc.
configureUISettings(c)
return nil
}
var config = Conf{}
func main() {
app := cli.NewApp()
app.Name = ShortName
app.Version = Version
app.Usage = "control the system's connection to " + Provider
app.Description = "The " + app.Name + " command controls the system's connection to " + Provider + ".\n\n" +
"To connect the system using an activation key:\n" +
"\t" + app.Name + " connect --organization ID --activation-key KEY\n\n" +
"To connect the system using a username and password:\n" +
"\t" + app.Name + " connect --username USERNAME --password PASSWORD\n\n" +
"To disconnect the system:\n" +
"\t" + app.Name + " disconnect\n\n" +
"Run '" + app.Name + " command --help' for more details."
log.SetFlags(0)
log.SetPrefix("")
defaultConfigFilePath, err := ConfigPath()
if err != nil {
log.Fatal(err)
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "generate-man-page",
Hidden: true,
},
&cli.BoolFlag{
Name: "generate-markdown",
Hidden: true,
},
&cli.BoolFlag{
Name: "no-color",
Hidden: false,
Value: false,
EnvVars: []string{"NO_COLOR"},
},
&cli.StringFlag{
Name: "config",
Hidden: true,
Value: defaultConfigFilePath,
TakesFile: true,
Usage: "Read config values from `FILE`",
},
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliCertFile,
Hidden: true,
Usage: "Use `FILE` as the client certificate",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliKeyFile,
Hidden: true,
Usage: "Use `FILE` as the client's private key",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: cliLogLevel,
Value: "info",
Hidden: true,
Usage: "Set the logging output level to `LEVEL`",
}),
}
app.Commands = []*cli.Command{
{
Name: "connect",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Usage: "register with `USERNAME`",
Aliases: []string{"u"},
},
&cli.StringFlag{
Name: "password",
Usage: "register with `PASSWORD`",
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "organization",
Usage: "register with `ID`",
Aliases: []string{"o"},
},
&cli.StringSliceFlag{
Name: "activation-key",
Usage: "register with `KEY`",
Aliases: []string{"a"},
},
&cli.StringFlag{
Name: "server",
Hidden: true,
Usage: "register against `URL`",
},
&cli.StringFlag{
Name: "format",
Usage: "prints output of connection in machine-readable format (supported formats: \"json\")",
Aliases: []string{"f"},
},
},
Usage: "Connects the system to " + Provider,
UsageText: fmt.Sprintf("%v connect [command options]", app.Name),
Description: fmt.Sprintf("The connect command connects the system to Red Hat Subscription Management, Red Hat Insights and %v and activates the %v service that enables %v to interact with the system. For details visit: https://red.ht/connector", Provider, ServiceName, Provider),
Before: beforeConnectAction,
Action: connectAction,
},
{
Name: "disconnect",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Usage: "prints output of disconnection in machine-readable format (supported formats: \"json\")",
Aliases: []string{"f"},
},
},
Usage: "Disconnects the system from " + Provider,
UsageText: fmt.Sprintf("%v disconnect", app.Name),
Description: fmt.Sprintf("The disconnect command disconnects the system from Red Hat Subscription Management, Red Hat Insights and %v and deactivates the %v service. %v will no longer be able to interact with the system.", Provider, ServiceName, Provider),
Before: beforeDisconnectAction,
Action: disconnectAction,
},
{