Skip to content

Commit

Permalink
Issue crc-org#11 Synchronize menu states
Browse files Browse the repository at this point in the history
Update menu item states after each start/stop/delete
operation.
  • Loading branch information
anjannath committed Feb 17, 2020
1 parent d3d2952 commit 35a973f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CodeReady Containers/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 72 additions & 14 deletions CodeReady Containers/DaemonCommander/Handlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -95,29 +124,51 @@ 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 {
Expand Down Expand Up @@ -147,10 +198,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)
}
Expand Down

0 comments on commit 35a973f

Please sign in to comment.