-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added levels of connectivity to connect command #179
Open
jirihnidek
wants to merge
3
commits into
main
Choose a base branch
from
jhnidek/connect_levels_of_connectivity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,30 @@ package main | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/subpop/go-log" | ||
"github.com/urfave/cli/v2" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/subpop/go-log" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// 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"` | ||
Hostname string `json:"hostname"` | ||
HostnameError string `json:"hostname_error,omitempty"` | ||
UID int `json:"uid"` | ||
UIDError string `json:"uid_error,omitempty"` | ||
EnabledFeatures []string `json:"enabled_features"` | ||
DisabledFeatures []string `json:"disabled_features"` | ||
RHSMConnected bool `json:"rhsm_connected"` | ||
RHSMConnectError string `json:"rhsm_connect_error,omitempty"` | ||
ContentEnabled bool `json:"content_enabled"` | ||
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 | ||
} | ||
|
||
|
@@ -56,6 +61,8 @@ func beforeConnectAction(ctx *cli.Context) error { | |
password := ctx.String("password") | ||
organization := ctx.String("organization") | ||
activationKeys := ctx.StringSlice("activation-key") | ||
enabledFeatures := ctx.StringSlice("enable-feature") | ||
disabledFeatures := ctx.StringSlice("disable-feature") | ||
|
||
if len(activationKeys) > 0 { | ||
if username != "" { | ||
|
@@ -73,6 +80,11 @@ func beforeConnectAction(ctx *cli.Context) error { | |
} | ||
} | ||
|
||
err = checkFeatureInput(&enabledFeatures, &disabledFeatures) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return checkForUnknownArgs(ctx) | ||
} | ||
|
||
|
@@ -112,13 +124,30 @@ func connectAction(ctx *cli.Context) error { | |
|
||
interactivePrintf("Connecting %v to %v.\nThis might take a few seconds.\n\n", hostname, Provider) | ||
|
||
var featuresStr []string | ||
for _, feature := range KnownFeatures { | ||
if feature.Enabled { | ||
if uiSettings.isMachineReadable { | ||
connectResult.EnabledFeatures = append(connectResult.EnabledFeatures, feature.ID) | ||
} | ||
featuresStr = append(featuresStr, "["+symbolOK+"]"+feature.ID) | ||
} else { | ||
if uiSettings.isMachineReadable { | ||
connectResult.DisabledFeatures = append(connectResult.EnabledFeatures, feature.ID) | ||
} | ||
featuresStr = append(featuresStr, "[ ]"+feature.ID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there should be a space between the checkbox and the text. |
||
} | ||
} | ||
featuresListStr := strings.Join(featuresStr, ", ") | ||
interactivePrintf("Features preferences: %s\n\n", featuresListStr) | ||
|
||
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) | ||
returnedMsg, err = registerRHSM(ctx, ContentFeature.Enabled) | ||
if err != nil { | ||
connectResult.RHSMConnected = false | ||
errorMessages["rhsm"] = LogMessage{ | ||
|
@@ -127,77 +156,102 @@ func connectAction(ctx *cli.Context) error { | |
err)} | ||
if uiSettings.isMachineReadable { | ||
connectResult.RHSMConnectError = errorMessages["rhsm"].message.Error() | ||
connectResult.ContentEnabled = false | ||
} else { | ||
fmt.Printf( | ||
"%v Cannot connect to Red Hat Subscription Management\n", | ||
" [%v] Cannot connect to Red Hat Subscription Management\n", | ||
uiSettings.iconError, | ||
) | ||
fmt.Printf( | ||
" [%v] Skipping generation of Red Hat repository file\n", | ||
uiSettings.iconError, | ||
) | ||
} | ||
} else { | ||
connectResult.RHSMConnected = true | ||
interactivePrintf("%v %v\n", uiSettings.iconOK, returnedMsg) | ||
interactivePrintf(" [%v] %v\n", uiSettings.iconOK, returnedMsg) | ||
if ContentFeature.Enabled { | ||
if uiSettings.isMachineReadable { | ||
connectResult.ContentEnabled = true | ||
} | ||
interactivePrintf(" [%v] Content ... Red Hat repository file generated\n", uiSettings.iconOK) | ||
} else { | ||
if uiSettings.isMachineReadable { | ||
connectResult.ContentEnabled = false | ||
} | ||
interactivePrintf(" [ ] Content ... Red Hat repository file not generated\n") | ||
} | ||
} | ||
durations["rhsm"] = time.Since(start) | ||
|
||
/* 2. Register insights-client */ | ||
if errors, exist := errorMessages["rhsm"]; exist { | ||
if errors.level == log.LevelError { | ||
interactivePrintf( | ||
"%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) | ||
if AnalyticsFeature.Enabled { | ||
if errors, exist := errorMessages["rhsm"]; exist { | ||
if errors.level == log.LevelError { | ||
interactivePrintf( | ||
" [%v] Skipping connection to Red Hat Insights\n", | ||
uiSettings.iconError, | ||
) | ||
} | ||
} else { | ||
connectResult.InsightsConnected = true | ||
interactivePrintf("%v Connected to Red Hat Insights\n", uiSettings.iconOK) | ||
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] Analytics ... Cannot connect to Red Hat Insights\n", uiSettings.iconError) | ||
} | ||
} else { | ||
connectResult.InsightsConnected = true | ||
interactivePrintf(" [%v] Analytics ... Connected to Red Hat Insights\n", uiSettings.iconOK) | ||
} | ||
durations["insights"] = time.Since(start) | ||
} | ||
durations["insights"] = time.Since(start) | ||
} else { | ||
interactivePrintf(" [ ] Analytics ... Connecting to Red Hat Insights disabled\n") | ||
} | ||
|
||
/* 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 { | ||
if ManagementFeature.Enabled { | ||
/* 3. Start yggdrasil (rhcd) service */ | ||
if rhsmErrMsg, exist := errorMessages["rhsm"]; exist && rhsmErrMsg.level == log.LevelError { | ||
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() | ||
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 { | ||
interactivePrintf(" [%v] Remote Management ... Cannot activate the %v service\n", uiSettings.iconError, ServiceName) | ||
} | ||
} else { | ||
fmt.Printf("%v Cannot activate the %v service\n", uiSettings.iconError, ServiceName) | ||
connectResult.YggdrasilStarted = true | ||
interactivePrintf(" [%v] Remote Management ... Activated the %v service\n", uiSettings.iconOK, ServiceName) | ||
} | ||
} else { | ||
connectResult.YggdrasilStarted = true | ||
interactivePrintf("%v Activated the %v service\n", uiSettings.iconOK, ServiceName) | ||
durations[ServiceName] = time.Since(start) | ||
} | ||
durations[ServiceName] = time.Since(start) | ||
interactivePrintf("\nSuccessfully connected to Red Hat!\n") | ||
} else { | ||
interactivePrintf(" [ ] Management .... Starting %s service disabled\n", ServiceName) | ||
} | ||
|
||
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be a space between the checkbox and the text.