-
Notifications
You must be signed in to change notification settings - Fork 18
/
disconnect_cmd.go
160 lines (143 loc) · 5.17 KB
/
disconnect_cmd.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
package main
import (
"encoding/json"
"fmt"
"github.com/subpop/go-log"
"github.com/urfave/cli/v2"
"os"
"time"
)
// 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)
case "":
break
default:
result = "error: unsupported document format: " + disconnectResult.format
}
return result
}
// beforeDisconnectAction ensures the used has supplied a correct `--format` flag
func beforeDisconnectAction(ctx *cli.Context) error {
err := setupFormatOption(ctx)
if err != nil {
return err
}
return checkForUnknownArgs(ctx)
}
// 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)
}