Control your backup NAS from the desktop
Wake Up on Command
If the user clicks the Up button, the callback notifies the state machine in line 36, which uses its transition rules to determine the next steps of the application. Line 36 sends the wake
event to the machine with the Event()
function. The machine then blocks until the NAS wakes up, before finally sending the UP
message on the stateReporter
channel. Because I don't want the GUI to freeze while the machine is blocking, the callback wraps the call to the machine's Event()
function in a Go routine that continues to run concurrently while the callback completes.
Events from channel stateReporter
are intercepted by a Go routine running concurrently starting in line 51. It uses a select
statement to listen on the channel. As soon as the state machine announces a new state, the code jumps to one of the two case blocks handling the up or down states. These in turn prompt the GUI to refresh the display to reflect the new state.
The new container generated starting in line 74 lines up all the widgets, both visible and invisible. NewHBox()
in line 80 defines the sub-container at the bottom of the main GUI container, and the three buttons for controlling the app are arranged horizontally next to each other at the bottom of the application window. Meanwhile, the main container uses NewVBox()
to stack the rest of the widgets on top of each other; they include the text and icon for the state, the progress bar, and the sub-container.
Line 88 dumps everything into the application window. Finally, line 89 jumps to the endless GUI loop by calling ShowAndRun()
. From there, the GUI intercepts mouse input from the user and displays the GUI changes initiated by the program code running smoothly and concurrently.
Not Below 400 Pixels
The Fyne framework not only runs on desktop interfaces, but it was designed to work on mobile devices as well. This is why, among other things that look awkward at first, it does not provide an option for setting an application window to a defined minimum size after the program start.
That's a good strategy in the grand scheme of cross-platform framework development. However it's quite jarring if an application like Syno comes up as a mini window measuring 50x50 pixels that you can hardly see on the desktop. Fortunately, you can use a trick to define a minimum size. The app stuffs the main container with an empty image measuring 400x0 pixels, which invisibly becomes part of the VBox with the widgets. This forces the renderer to open an application window with a width of at least 400 pixels wide from the outset.
Switching Off with sudo
The last part of the application in Listing 4 defines the utility function isPingable()
, which checks whether the NAS is operational. This could of course be done with a component from the net standard library from the Go repository, but for simplicity's sake the function simply calls the Ping program in the shell. Depending on that command's return code, the function returns a true or false value to the caller.
Listing 4
util.go
01 package main 02 import ( 03 "os/exec" 04 ) 05 const synIP = "192.168.3.33" 06 func shutdownNAS() { 07 cmd := exec.Command("ssh", "synuser@"+synIP, "sudo", "/sbin/poweroff") 08 if err := cmd.Run(); err != nil { 09 panic(err) 10 } 11 } 12 func isPingable() bool { 13 cmd := exec.Command("ping", "-c", "1", "-t", "3", synIP) 14 if err := cmd.Run(); err != nil { 15 return false 16 } 17 return true 18 }
To shut down the NAS when the user presses the Down button, the shutdownNAS()
function contacts the NAS at its IP address starting in line 6 and logs into a predefined SSH account. It then issues the sudo poweroff
command in the opened shell. This causes the monster disk array to shut down and disconnect itself from the power supply. Meanwhile, the state machine notices this in the course of its continuous checks and the GUI jumps to the DOWN visualization.
To do this, the controlling host needs to be able to use SSH to log in to the NAS without entering a password. This is typically achieved by storing the public key of a key pair maintained by the controlling host in the ~/.ssh/authorized_keys
file on the NAS. Also, the user on the NAS needs sudo privileges to execute the poweroff
command. This is handled by the line
synouser ALL=(ALL) NOPASSWD: /sbin/poweroff
in /etc/sudoers
file on the NAS.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.