Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 116 additions & 62 deletions connect_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 != "" {
Expand All @@ -73,6 +80,11 @@ func beforeConnectAction(ctx *cli.Context) error {
}
}

err = checkFeatureInput(&enabledFeatures, &disabledFeatures)
if err != nil {
return err
}

return checkForUnknownArgs(ctx)
}

Expand Down Expand Up @@ -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)
Copy link
Contributor

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.

} else {
if uiSettings.isMachineReadable {
connectResult.DisabledFeatures = append(connectResult.EnabledFeatures, feature.ID)
}
featuresStr = append(featuresStr, "[ ]"+feature.ID)
Copy link
Contributor

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.

}
}
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{
Expand All @@ -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")
Expand Down
17 changes: 9 additions & 8 deletions disconnect_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"encoding/json"
"fmt"
"github.com/subpop/go-log"
"github.com/urfave/cli/v2"
"os"
"time"

"github.com/subpop/go-log"
"github.com/urfave/cli/v2"
)

// DisconnectResult is structure holding information about result of
Expand Down Expand Up @@ -103,10 +104,10 @@ func disconnectAction(ctx *cli.Context) error {
message: fmt.Errorf("%v", errMsg)}
disconnectResult.YggdrasilStopped = false
disconnectResult.YggdrasilStoppedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
interactivePrintf(" [%v] %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.YggdrasilStopped = true
interactivePrintf("%v Deactivated the %v service\n", uiSettings.iconOK, ServiceName)
interactivePrintf(" [%v] Deactivated the %v service\n", uiSettings.iconOK, ServiceName)
}
durations[ServiceName] = time.Since(start)

Expand All @@ -120,10 +121,10 @@ func disconnectAction(ctx *cli.Context) error {
message: fmt.Errorf("%v", errMsg)}
disconnectResult.InsightsDisconnected = false
disconnectResult.InsightsDisconnectedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
interactivePrintf(" [%v] %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.InsightsDisconnected = true
interactivePrintf("%v Disconnected from Red Hat Insights\n", uiSettings.iconOK)
interactivePrintf(" [%v] Disconnected from Red Hat Insights\n", uiSettings.iconOK)
}
durations["insights"] = time.Since(start)

Expand All @@ -139,10 +140,10 @@ func disconnectAction(ctx *cli.Context) error {

disconnectResult.RHSMDisconnected = false
disconnectResult.RHSMDisconnectedError = errMsg
interactivePrintf("%v %v\n", uiSettings.iconError, errMsg)
interactivePrintf(" [%v] %v\n", uiSettings.iconError, errMsg)
} else {
disconnectResult.RHSMDisconnected = true
interactivePrintf("%v Disconnected from Red Hat Subscription Management\n", uiSettings.iconOK)
interactivePrintf(" [%v] Disconnected from Red Hat Subscription Management\n", uiSettings.iconOK)
}
durations["rhsm"] = time.Since(start)

Expand Down
Loading
Loading