Skip to content

Commit fcc39b2

Browse files
committed
Launcher: config properly stops (or kills if possible) config-admin-agent after reverting operations regardless if they were successfull or not
1 parent 3f7dda1 commit fcc39b2

2 files changed

Lines changed: 45 additions & 28 deletions

File tree

launcher-config/internal/cmd/revert.go

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var revertCmd = &cobra.Command{
7272
var removedUserCert *x509.Certificate
7373
var restoredMetadata bool
7474
var restoredProfiles bool
75+
var errorCode int
7576
sigs := make(chan os.Signal, 1)
7677
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
7778
go func() {
@@ -119,7 +120,7 @@ var revertCmd = &cobra.Command{
119120
fmt.Println("Successfully restored metadata")
120121
restoredMetadata = true
121122
} else {
122-
errorCode := internal.ErrMetadataRestore
123+
errorCode = internal.ErrMetadataRestore
123124
if !cmd.RemoveAll {
124125
if removedUserCert != nil {
125126
if !addUserCert(removedUserCert) {
@@ -158,9 +159,10 @@ var revertCmd = &cobra.Command{
158159
}
159160
}
160161
}
162+
var agentConnected bool
161163
if cmd.RemoveLocalCert || cmd.UnmapIPs || cmd.UnmapCDN {
162-
agentStarted := internal.ConnectAgentIfNeeded() == nil
163-
if agentStarted {
164+
agentConnected = internal.ConnectAgentIfNeeded() == nil
165+
if agentConnected {
164166
fmt.Println("Communicating with config-admin-agent to remove local cert and/or host mappings...")
165167
} else {
166168
if isAdmin {
@@ -169,9 +171,10 @@ var revertCmd = &cobra.Command{
169171
fmt.Println("Running config-admin to remove local cert and/or host mappings, authorize it if needed...")
170172
}
171173
}
172-
err, exitCode := internal.RunRevert(cmd.UnmapIPs, cmd.RemoveLocalCert, cmd.UnmapCDN, !cmd.RemoveAll)
173-
if err == nil && exitCode == common.ErrSuccess {
174-
if agentStarted {
174+
var err error
175+
err, errorCode = internal.RunRevert(cmd.UnmapIPs, cmd.RemoveLocalCert, cmd.UnmapCDN, !cmd.RemoveAll)
176+
if err == nil && errorCode == common.ErrSuccess {
177+
if agentConnected {
175178
fmt.Println("Successfully communicated with config-admin-agent")
176179
} else {
177180
fmt.Println("Successfully ran config-admin")
@@ -181,11 +184,11 @@ var revertCmd = &cobra.Command{
181184
fmt.Println("Received error:")
182185
fmt.Println(err)
183186
}
184-
if exitCode != common.ErrSuccess {
187+
if errorCode != common.ErrSuccess {
185188
fmt.Println("Received exit code:")
186-
fmt.Println(exitCode)
189+
fmt.Println(errorCode)
187190
}
188-
errorCode := internal.ErrAdminRevert
191+
errorCode = internal.ErrAdminRevert
189192
if !cmd.RemoveAll {
190193
if removedUserCert != nil {
191194
if !addUserCert(removedUserCert) {
@@ -203,36 +206,49 @@ var revertCmd = &cobra.Command{
203206
}
204207
}
205208
}
206-
if agentStarted {
209+
if agentConnected {
207210
fmt.Println("Failed to communicate with config-admin-agent")
208211
} else {
209212
fmt.Println("Failed to run config-admin")
210213
}
211-
os.Exit(errorCode)
212214
}
213215
}
214-
if stopAgent && internal.ConnectAgentIfNeeded() == nil {
215-
errorCode := common.ErrSuccess
216-
fmt.Println("Trying to stop config-admin-agent.")
217-
err := internal.StopAgentIfNeeded()
216+
if stopAgent {
218217
failedStopAgent := true
219-
if err == nil {
220-
if internal.ConnectAgentIfNeededWithRetries(false) {
221-
fmt.Println("Stopped config-admin-agent")
222-
failedStopAgent = false
218+
if agentConnected {
219+
fmt.Println("Trying to stop config-admin-agent.")
220+
err := internal.StopAgentIfNeeded()
221+
if err == nil {
222+
if internal.ConnectAgentIfNeededWithRetries(false) {
223+
fmt.Println("Stopped config-admin-agent")
224+
failedStopAgent = false
225+
} else {
226+
fmt.Println("Failed to stop config-admin-agent")
227+
}
223228
} else {
224-
fmt.Println("Failed to stop config-admin-agent")
229+
fmt.Println("Failed to trying stopping config-admin-agent")
230+
fmt.Println(err)
225231
}
226-
} else {
227-
fmt.Println("Failed to trying stopping config-admin-agent")
228-
fmt.Println(err)
229232
}
230-
if isAdmin && failedStopAgent {
231-
_, err := commonProcess.Kill(common.GetExeFileName(true, common.LauncherConfigAdminAgent))
232-
if err == nil {
233-
fmt.Println("Successfully killed config-admin-agent.")
233+
if failedStopAgent {
234+
exeFileName := common.GetExeFileName(true, common.LauncherConfigAdminAgent)
235+
if _, _, err := commonProcess.Process(exeFileName); err == nil {
236+
if isAdmin {
237+
_, err := commonProcess.Kill(exeFileName)
238+
if err == nil {
239+
fmt.Println("Successfully killed config-admin-agent.")
240+
failedStopAgent = false
241+
} else {
242+
fmt.Println("Failed to kill config-admin-agent")
243+
}
244+
} else {
245+
fmt.Println("Re-run as administrator to kill config-admin-agent")
246+
}
234247
}
235248
}
249+
if failedStopAgent && errorCode == common.ErrSuccess {
250+
errorCode = internal.ErrRevertStopAgent
251+
}
236252
os.Exit(errorCode)
237253
}
238254
},
@@ -277,7 +293,7 @@ func InitRevert() {
277293
"stopAgent",
278294
"g",
279295
false,
280-
"Stop the config-admin-agent if it is running after all operations are successful",
296+
"Stop the config-admin-agent if it is running after all operations",
281297
)
282298
err := revertCmd.Flags().MarkHidden("stopAgent")
283299
if err != nil {

launcher-config/internal/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ const (
2323
ErrStartAgentVerify
2424
ErrAdminSetup
2525
ErrAdminSetupRevert
26+
ErrRevertStopAgent
2627
)

0 commit comments

Comments
 (0)