diff --git a/CodeReady Containers/AppDelegate.swift b/CodeReady Containers/AppDelegate.swift index 60d6738..a67a3d7 100644 --- a/CodeReady Containers/AppDelegate.swift +++ b/CodeReady Containers/AppDelegate.swift @@ -118,6 +118,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.statusMenuItem.image = nil } + func showClusterStatusUnknownOnStatusMenuItem() { + self.statusMenuItem.title = "Status Unknown" + self.statusMenuItem.image = NSImage(named: NSImage.statusNoneName) + } func updateMenuStates(state: MenuStates) { self.startMenuItem.isEnabled = state.startMenuEnabled self.stopMenuItem.isEnabled = state.stopMenuEnabled diff --git a/CodeReady Containers/DaemonCommander/Handlers.swift b/CodeReady Containers/DaemonCommander/Handlers.swift index e3c5d0a..5212ee4 100644 --- a/CodeReady Containers/DaemonCommander/Handlers.swift +++ b/CodeReady Containers/DaemonCommander/Handlers.swift @@ -60,7 +60,14 @@ func HandleStop() { if String(bytes: data, encoding: .utf8) == "Failed" { DispatchQueue.main.async { let appDelegate = NSApplication.shared.delegate as? AppDelegate - appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, stopMenuEnabled: false, deleteMenuEnabled: true, webconsoleMenuEnabled: true, ocLoginForDeveloperEnabled: true, ocLoginForAdminEnabled: true, copyOcLoginCommand: true)) + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, + stopMenuEnabled: false, + deleteMenuEnabled: true, + webconsoleMenuEnabled: true, + ocLoginForDeveloperEnabled: true, + ocLoginForAdminEnabled: true, + copyOcLoginCommand: true) + ) showAlertFailedAndCheckLogs(message: "Failed deleting the CRC cluster", informativeMsg: "Make sure the CRC daemon is running, or check the logs to get more information") } @@ -71,12 +78,34 @@ func HandleStop() { if stopResult.Success { DispatchQueue.main.async { let appDelegate = NSApplication.shared.delegate as? AppDelegate - appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, stopMenuEnabled: false, deleteMenuEnabled: true, webconsoleMenuEnabled: false, ocLoginForDeveloperEnabled: false, ocLoginForAdminEnabled: false, copyOcLoginCommand: false)) + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, + stopMenuEnabled: false, + deleteMenuEnabled: true, + webconsoleMenuEnabled: false, + ocLoginForDeveloperEnabled: false, + ocLoginForAdminEnabled: false, + copyOcLoginCommand: false) + ) appDelegate?.updateStatusMenuItem(status: "Stopped") displayNotification(title: "Successfully Stopped Cluster", body: "The CRC Cluster was successfully stopped") } } + if stopResult.Error != "" { + DispatchQueue.main.async { + showAlertFailedAndCheckLogs(message: "Failed to stop OpenShift cluster", informativeMsg: "\(stopResult.Error)") + let appDelegate = NSApplication.shared.delegate as? AppDelegate + appDelegate?.showClusterStatusUnknownOnStatusMenuItem() + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, + stopMenuEnabled: true, + deleteMenuEnabled: true, + webconsoleMenuEnabled: true, + ocLoginForDeveloperEnabled: true, + ocLoginForAdminEnabled: true, + copyOcLoginCommand: true) + ) + } + } } catch let jsonErr { print(jsonErr.localizedDescription) } @@ -95,29 +124,58 @@ func HandleStart() { // Adjust the menus DispatchQueue.main.async { let appDelegate = NSApplication.shared.delegate as? AppDelegate - appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, stopMenuEnabled: false, deleteMenuEnabled: true, webconsoleMenuEnabled: false, ocLoginForDeveloperEnabled: false, ocLoginForAdminEnabled: false, copyOcLoginCommand: false)) + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, + stopMenuEnabled: false, + deleteMenuEnabled: true, + webconsoleMenuEnabled: false, + ocLoginForDeveloperEnabled: false, + ocLoginForAdminEnabled: false, + copyOcLoginCommand: false)) showAlertFailedAndCheckLogs(message: "Failed to start OpenShift cluster", informativeMsg: "CodeReady Containers failed to start the OpenShift cluster, ensure the CRC daemon is running or check the logs to find more information") } + } else { + displayNotification(title: "CodeReady Containers", body: "Starting OpenShift Cluster, this could take a few minutes..") } do { let startResult = try JSONDecoder().decode(StartResult.self, from: data) - if startResult.KubeletStarted { + if startResult.Status == "Running" { DispatchQueue.main.async { let appDelegate = NSApplication.shared.delegate as? AppDelegate - appDelegate?.updateStatusMenuItem(status: startResult.Status) - appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, stopMenuEnabled: true, deleteMenuEnabled: true, webconsoleMenuEnabled: true, ocLoginForDeveloperEnabled: true, ocLoginForAdminEnabled: true, copyOcLoginCommand: true)) - - displayNotification(title: "Cluster started", body: "CodeReady Containers OpenShift Cluster is up") + appDelegate?.showClusterStartingMessageOnStatusMenuItem() + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, stopMenuEnabled: true, deleteMenuEnabled: true, webconsoleMenuEnabled: false, ocLoginForDeveloperEnabled: false, ocLoginForAdminEnabled: false, copyOcLoginCommand: false)) + } + // if vm is running but kubelet not yet started + if !startResult.KubeletStarted { + DispatchQueue.main.async { + let appDelegate = NSApplication.shared.delegate as? AppDelegate + appDelegate?.showClusterStatusUnknownOnStatusMenuItem() + displayNotification(title: "CodeReady Containers", body: "CodeReady Containers OpenShift Cluster is taking longer to start") + } + } else { + DispatchQueue.main.async { + let appDelegate = NSApplication.shared.delegate as? AppDelegate + appDelegate?.updateStatusMenuItem(status: startResult.Status) + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, stopMenuEnabled: true, deleteMenuEnabled: true, webconsoleMenuEnabled: true, ocLoginForDeveloperEnabled: true, ocLoginForAdminEnabled: true, copyOcLoginCommand: true)) + + displayNotification(title: "CodeReady Containers", body: "OpenShift Cluster is running") + } } } - if !startResult.KubeletStarted { + if startResult.Error != "" { DispatchQueue.main.async { + var errMsg = startResult.Error.split(separator: "\n") + showAlertFailedAndCheckLogs(message: "Failed to start OpenShift cluster", informativeMsg: "\(errMsg[0])") let appDelegate = NSApplication.shared.delegate as? AppDelegate - appDelegate?.updateStatusMenuItem(status: startResult.Status) - appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, stopMenuEnabled: true, deleteMenuEnabled: true, webconsoleMenuEnabled: true, ocLoginForDeveloperEnabled: true, ocLoginForAdminEnabled: true, copyOcLoginCommand: true)) - - displayNotification(title: "Cluster is still starting", body: "CodeReady Containers OpenShift Cluster is taking longer to start") + appDelegate?.showClusterStatusUnknownOnStatusMenuItem() + appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: false, + stopMenuEnabled: true, + deleteMenuEnabled: true, + webconsoleMenuEnabled: false, + ocLoginForDeveloperEnabled: false, + ocLoginForAdminEnabled: false, + copyOcLoginCommand: false) + ) } } } catch let jsonErr { @@ -147,10 +205,17 @@ func HandleDelete() { DispatchQueue.main.async { let appDelegate = NSApplication.shared.delegate as? AppDelegate appDelegate?.updateMenuStates(state: MenuStates(startMenuEnabled: true, stopMenuEnabled: false, deleteMenuEnabled: false, webconsoleMenuEnabled: false, ocLoginForDeveloperEnabled: false, ocLoginForAdminEnabled: false, copyOcLoginCommand: false)) - + appDelegate?.showClusterStatusUnknownOnStatusMenuItem() displayNotification(title: "Cluster Deleted", body: "The CRC Cluster is successfully deleted") } } + if deleteResult.Error != "" { + DispatchQueue.main.async { + showAlertFailedAndCheckLogs(message: "Failed to delete OpenShift cluster", informativeMsg: "\(deleteResult.Error)") + let appDelegate = NSApplication.shared.delegate as? AppDelegate + appDelegate?.showClusterStatusUnknownOnStatusMenuItem() + } + } } catch let jsonErr { print(jsonErr.localizedDescription) }