Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network helper #3308

Merged
merged 3 commits into from
Jul 14, 2022
Merged

Network helper #3308

merged 3 commits into from
Jul 14, 2022

Conversation

jvonau
Copy link
Contributor

@jvonau jvonau commented Jul 14, 2022

@holta holta added this to the 8.0 milestone Jul 14, 2022
@holta holta merged commit b221e0c into iiab:master Jul 14, 2022
@holta
Copy link
Member

holta commented Jul 14, 2022

@jvonau can you summarize Tips/Recommendations here + with PR #3173 ~= #3302, so we can post to https://github.com/iiab/iiab/tree/master/roles/network#readme or FAQ etc?

(Or is https://github.com/iiab/iiab/wiki/IIAB-Networking a better place, to help @tim-moody and implementers broadly?)

@jvonau
Copy link
Contributor Author

jvonau commented Jul 14, 2022

Both mate-terminal and x-terminal-emulator work with mate, @holta as a test on RasPiOS, save the below as /usr/local/sbin/netwarn.sh and make it executable.

#!/bin/bash

if ! -f /etc/iiab/install-flags/iiab-network-complete; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream wifi internet active first if needed. You will be prompted for your password. Do you want to Proceed?"
fi

rc=$?

if [[ $rc == “1” ]]; then
    exit 1
fi

#mate-terminal --command /usr/local/bin/iiab-network
x-terminal-emulator -e /usr/local/bin/iiab-network

netwarn.sh should work popping up a box, yes runs iiab-network. Formatting and wording could use a little work and perhaps not closing the terminal when complete might be wanted.

Should the above test work then cross desktop coverage is a bunch easier.

The below works on mate to auto launch on login, save the below as ~/.config/autostart/iiab-network.desktop

[Desktop Entry]
Type=Application
Exec=/usr/local/sbin/netwarn.sh
Hidden=false
Name[en_CA]=iiab-network
Name=iiab-network
Comment[en_CA]=iiab-network
Comment=iiab-network
X-MATE-Autostart-Delay=2

This is all works fine and dandy post firstboot. Should really add some other Name[en_CA] like entries to cover other languages but it is a start and what others to add?

To embed this into the image think I'll look to seed the existing 'oem' user on the pre-canned image and see if it survives the firstboot sequence. Any other potential locations that might work? I tried /etc/xdg/autostart/ but haven't hit the secret sauce just yet.
reminder to self: check if /etc/hosts retained the needed entries before doing anything else, just answer 'no' to the above.

@holta
Copy link
Member

holta commented Jul 14, 2022

Should really add some other Name[en_CA] like entries to cover other languages but it is a start and what others to add?

English is enough for most IT people.

In the far-off future, locale / language could be auto-detected, but that's really not necessary at this time.

@holta
Copy link
Member

holta commented Jul 15, 2022

  1. Above-mentioned code does not run for a couple reasons:
root@box:~# netwarn.sh
/usr/local/sbin/netwarn.sh: line 3: -f: command not found
Unable to init server: Could not connect: Connection refused

(zenity:3231): Gtk-WARNING **: 12:14:09.079: cannot open display:
Unable to init server: Could not connect: Connection refused

(x-terminal-emulator:3232): Gtk-WARNING **: 12:14:09.211: cannot open display:
  1. I patched the code in a couple areas, as follows:
#!/bin/bash

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream wifi internet active first if needed. You will be prompted for your pass\
word. Do you want to Proceed?"
fi

rc=$?

if [[ $rc != "0" ]]; then
    exit $rc
fi

#mate-terminal --command /usr/local/bin/iiab-network
x-terminal-emulator -e /usr/local/bin/iiab-network
  1. That part then runs on 64-bit Raspberry Pi OS "with desktop" (2022-04-04, stage4 version of the OS) on RPi 4 — when launched by the user graphically logged in — if they click Yes below:

image

  1. However after running mkdir ~/.config/autostart/ and then pasting in the above 9-line ~/.config/autostart/iiab-network.desktop — the zenity pop-up does not appear on reboot (of RasPiOS with desktop).

  2. If you need working code for RasPiOS with desktop, consider @georgejhunt's here:

- name: Make home page autostart on localhost (the server's console) if session manager is LXDE (rpi)
stat:
path: /etc/xdg/lxsession/LXDE-pi/autostart
register: lxde_present
- name: Check for Chromium name change
stat:
path: /usr/bin/chromium
register: chromium_present
- name: Add chromium-browser to /etc/xdg/lxsession/LXDE-pi/autostart if session manager is LXDE
lineinfile:
path: /etc/xdg/lxsession/LXDE-pi/autostart
regexp: '^/usr/bin/chromium-browser'
line: '/usr/bin/chromium-browser --disable-restore-session-state http://box/home'
when: lxde_present.stat.exists and not chromium_present.stat.exists
- name: Add chromium to /etc/xdg/lxsession/LXDE-pi/autostart if session manager is LXDE
lineinfile:
path: /etc/xdg/lxsession/LXDE-pi/autostart
regexp: '^/usr/bin/chromium'
line: '/usr/bin/chromium --disable-restore-session-state http://box/home'
when: lxde_present.stat.exists and chromium_present.stat.exists

Related:

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

I get the msg box with the text and yes/no first when launched from the gui terminal

Screenshot at 2022-07-15 15-07-27

'yes' opens a terminal and prompts for sudo, so does 'no' so that is a bug of sorts, need to lookup the exit codes for zenity. Just closing the new terminal without entering the pw seems fine for now given the flag test does work now.

Screenshot at 2022-07-15 15-08-09

Entering PW runs iiab-network

Screenshot at 2022-07-15 15-08-31

patched the code in a couple areas, as follows:

Yea I was looking at interoperability between the distros,

(x-terminal-emulator:3232): Gtk-WARNING **: 12:14:09.211: cannot open display:

Were you running that from a ssh session? When called from a terminal in the GUI it should pickup the display and looks like 'x-terminal-emulator' works with RasPiOS given the pic. That is all I was after, confirmation that x-terminal-emulator was supported, thanks for that.

However after running mkdir ~/.config/autostart/ and then pasting in the above 9-line ~/.config/autostart/iiab-network.desktop — the zenity pop-up does not appear on reboot (of RasPiOS with desktop).

I clearly said that was intended for mate and didn't ask for that to be tried knowing the last line is mate specific but a data point anyway. I didn't expect that would work from that ~/ location, given /etc/xdg/lxsession/LXDE-pi/autostart is being modded for 'chromium' think that would be the spot to run /usr/local/sbin/netwarn.sh on RasPiOS

@holta
Copy link
Member

holta commented Jul 15, 2022

Correction/Clarification that /home/root/.config/autostart/iiab-network.desktop does nothing. but that was later changed to...

  • /home/iiab-admin/.config/autostart/iiab-network.desktop which forcibly runs iiab-network on boot of this RasPiOS with desktop on RPi 4 (in an x-terminal-emulator window). Then the x-terminal-emulator window disappears when iiab-network completes (about 3min16sec later).

  • In any case, the zenity prompt never appears, and you do not get to click 'Yes' or 'No'. I don't know why RasPiOS appears to act quite differently from Ubuntu Mate on RPi (but FYI).

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

Faulty logic, try this one as netwarn.sh remembering that the popup only occurs when /etc/iiab/install-flags/iiab-network-complete is absent, remove that for repeated test attempts should iiab-network run successfully.

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream internet active first. You will be prompted for your password. Should you not want to re-configure the network just close the terminal ('X' on the right corner of the terminal window) without entering your password"

    x-terminal-emulator -e /usr/local/bin/iiab-network
fi

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

https://help.gnome.org/users/zenity/stable/usage.html.en
Zenity returns the following exit codes:
Exit Code | Description
0 | The user has pressed either OK or Close.
1 | The user has either pressed Cancel, or used the window functions to close the dialog.
-1 | An unexpected error has occurred.
5 | The dialog has been closed because the timeout h

@holta
Copy link
Member

holta commented Jul 15, 2022

Faulty logic, try this one as netwarn.sh remembering that the popup only occurs when /etc/iiab/install-flags/iiab-network-complete is absent, remove that for repeated test attempts should iiab-network run successfully.

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream internet active first. You will be prompted for your password. Should you not want to re-configure the network just close the terminal ('X' on the right corner of the terminal window) without entering your password"

    x-terminal-emulator -e /usr/local/bin/iiab-network
fi

The above forces iiab-network to run on RasPiOS — ignoring the user's choice of 'Yes' versus 'No'.

In short: even if you click 'No', the graphical user (iiab-admin typically) has full sudo privileges even without a password — and so forces iiab-network to run (user's Yes/No choice is disregarded).

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

Correction/Clarification that /home/root/.config/autostart/iiab-network.desktop does nothing. but that was later changed to...

* `/home/iiab-admin/.config/autostart/iiab-network.desktop` which forcibly runs iiab-network on boot of this RasPiOS with desktop on RPi 4 (in an x-terminal-emulator window).  Then the x-terminal-emulator window disappears when iiab-network completes (about 3min16sec later).

/home/user/.config is not the ideal place given the random names that could be present as 'user' with the advent of removing the 'pi' user upstream. Think for RasPiOS the ideal location to call /usr/local/sbin/netwarn.sh might be /etc/xdg/lxsession/LXDE-pi/autostart like how 'chromium' is auto-launched.

@holta
Copy link
Member

holta commented Jul 15, 2022

If this kind of thing[*] can be made reliable, Mint (currently growing more popular every day among, among recent IIAB implementers) should be included too.

[*] Also introductory guidance provided via auto-launch of browser showing http://box.lan, which of course is the entire basis for @georgejhunt's PR #2609.

(Various browser intros being excellent avenues for onboarding different kinds of non-technical operators and end users using these graphical desktops. e.g. as IIAB's http://box.lan web interface evolves, this also brings great flexibility as this "critical hand-holding" evolves into the future. Hopefully for Mint and Ubuntu Desktop too in due course. As these intro materials get progressively friendlier over time — teacher tips, technical vitals, community building essentials, etc).

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

The above forces iiab-network to run on RasPiOS — ignoring the user's choice of 'Yes' versus 'No'.

In short: even if you click 'No', the graphical user (iiab-admin typically) has full sudo privileges even without a password — and so forces iiab-network to run (user's Yes/No choice is disregarded).

Ok sudo is not prompting for a password, think that is a stock RasPiOS feature, can't recall. Think it comes back down to using the exit codes from zenity.

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream wifi internet active first if needed. You will be prompted for your password. Do you want to Proceed?"
    rc=$?
    if [ $rc == "1" ]; then
        exit 1
    fi
    x-terminal-emulator -e /usr/local/bin/iiab-network
fi

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

X out of the box and 'no' both skip the terminal while 'yes' prompts for me and runs. exit 1 is not quite right, wasn't an error, just a choice so exit 0 could be used also.

@holta
Copy link
Member

holta commented Jul 15, 2022

sudo is not prompting for a password, think that is a stock RasPiOS feature

Yes. RasPiOS enables sudo-without-password automatically, as outlined here:

https://askubuntu.com/questions/147241/execute-sudo-without-password/147265#147265

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

If this kind of thing[*] can be made reliable, Mint (currently growing more popular every day among, among recent IIAB implementers) should be included too.

[*] Also introductory guidance provided via auto-launch of browser showing http://box.lan, which of course is the entire basis for @georgejhunt's PR #2609.

(Various browser intros being excellent avenues for onboarding different kinds of non-technical operators and end users using these graphical desktops. e.g. as IIAB's http://box.lan web interface evolves, this also brings great flexibility as this "critical hand-holding" evolves into the future. Hopefully for Mint and Ubuntu Desktop too in due course. As these intro materials get progressively friendlier over time — teacher tips, technical vitals, community building essentials, etc).

auto-launching on ubuntu & mint is a magnitude harder given the various desktops that could be installed, but there should be a common spot somewhere that all desktops share. Just trolling the filesystem /etc/xdg/autostart/ looks promising but haven't hit the secret sauce just yet.

@holta
Copy link
Member

holta commented Jul 15, 2022

auto-launching on ubuntu & mint is a magnitude harder given the various desktops

Good point.

Best we can hope for is to work well with the dominant/default graphical desktop manager. (And Mate as well, in the case of Mint, if that works out.)

@jvonau
Copy link
Contributor Author

jvonau commented Jul 15, 2022

Notes from ubuntu Mate install

jerry@box:~$ locate autostart
/etc/xdg/autostart
/etc/xdg/autostart/at-spi-dbus-bus.desktop
/etc/xdg/autostart/ayatana-indicator-application.desktop
/etc/xdg/autostart/ayatana-indicator-datetime.desktop
/etc/xdg/autostart/ayatana-indicator-keyboard.desktop
/etc/xdg/autostart/ayatana-indicator-messages.desktop
/etc/xdg/autostart/ayatana-indicator-notifications.desktop
/etc/xdg/autostart/ayatana-indicator-power.desktop
/etc/xdg/autostart/ayatana-indicator-printers.desktop
/etc/xdg/autostart/ayatana-indicator-session.desktop
/etc/xdg/autostart/ayatana-indicator-sound.desktop
/etc/xdg/autostart/blueman.desktop
/etc/xdg/autostart/geoclue-demo-agent.desktop
/etc/xdg/autostart/gnome-keyring-pkcs11.desktop
/etc/xdg/autostart/gnome-keyring-secrets.desktop
/etc/xdg/autostart/gnome-keyring-ssh.desktop
/etc/xdg/autostart/iiab-network.desktop
/etc/xdg/autostart/im-launch.desktop
/etc/xdg/autostart/magnus-autostart.desktop
/etc/xdg/autostart/mate-optimus.desktop
/etc/xdg/autostart/mate-power-manager.desktop
/etc/xdg/autostart/mate-screensaver.desktop
/etc/xdg/autostart/mate-settings-daemon.desktop
/etc/xdg/autostart/nm-applet.desktop
/etc/xdg/autostart/onboard-autostart.desktop
/etc/xdg/autostart/orca-autostart.desktop
/etc/xdg/autostart/org.gnome.DejaDup.Monitor.desktop
/etc/xdg/autostart/org.gnome.Evolution-alarm-notify.desktop
/etc/xdg/autostart/org.gnome.SettingsDaemon.DiskUtilityNotify.desktop
/etc/xdg/autostart/polkit-mate-authentication-agent-1.desktop
/etc/xdg/autostart/print-applet.desktop
/etc/xdg/autostart/pulseaudio.desktop
/etc/xdg/autostart/snap-userd-autostart.desktop
/etc/xdg/autostart/spice-vdagent.desktop
/etc/xdg/autostart/ubuntu-advantage-notification.desktop
/etc/xdg/autostart/ubuntu-mate-welcome-autostart.desktop
/etc/xdg/autostart/ubuntu-report-on-upgrade.desktop
/etc/xdg/autostart/update-notifier.desktop
/etc/xdg/autostart/user-dirs-update-gtk.desktop
/etc/xdg/autostart/xdg-user-dirs.desktop
/home/jerry/.config/autostart
/snap/gnome-3-38-2004/113/etc/xdg/autostart
/snap/gnome-3-38-2004/113/etc/xdg/autostart/xdg-user-dirs.desktop
/snap/gnome-3-38-2004/113/usr/share/gdm/autostart
/snap/gnome-3-38-2004/113/usr/share/gdm/autostart/LoginWindow
/snap/gnome-3-38-2004/113/usr/share/gdm/autostart/LoginWindow/libcanberra-ready-sound.desktop
/snap/gnome-3-38-2004/113/usr/share/gnome/autostart
/snap/gnome-3-38-2004/113/usr/share/gnome/autostart/libcanberra-login-sound.desktop
/snap/snapd/16019/etc/xdg/autostart
/snap/snapd/16019/etc/xdg/autostart/snap-userd-autostart.desktop
/snap/snapd/16299/etc/xdg/autostart
/snap/snapd/16299/etc/xdg/autostart/snap-userd-autostart.desktop
/usr/lib/systemd/systemd-xdg-autostart-condition
/usr/lib/systemd/user/xdg-desktop-autostart.target
/usr/lib/systemd/user-generators/systemd-xdg-autostart-generator
/usr/lib/ubuntu-mate/caja-dropbox-autostart
/usr/share/arctica-greeter/guest-session/skel/.config/autostart
/usr/share/arctica-greeter/guest-session/skel/.config/autostart/arctica-greeter-guest-session-startup.desktop
/usr/share/gdm/autostart
/usr/share/gdm/autostart/LoginWindow
/usr/share/gdm/autostart/LoginWindow/libcanberra-ready-sound.desktop
/usr/share/gdm/autostart/LoginWindow/spice-vdagent.desktop
/usr/share/gdm/greeter/autostart
/usr/share/gdm/greeter/autostart/spice-vdagent.desktop
/usr/share/gdm/greeter/autostart/xbrlapi.desktop
/usr/share/gnome/autostart
/usr/share/gnome/autostart/libcanberra-login-sound.desktop
/usr/share/lightdm/guest-session/skel/.config/autostart
/usr/share/lightdm/guest-session/skel/.config/autostart/guest-session-startup.desktop
/usr/share/man/man8/systemd-xdg-autostart-generator.8.gz
/usr/share/mate/autostart
/usr/share/mate/autostart/ayatana-indicator-keyboard.desktop
/usr/share/mate/autostart/caja-dropbox-autostart.desktop
/usr/share/mate/autostart/dock-replace.desktop
/usr/share/mate/autostart/mate-hud.desktop
/usr/share/mate/autostart/nm-applet.desktop
/usr/share/mate/autostart/print-applet.desktop
/usr/share/mate/autostart/ubuntu-mate-settings-overlay.desktop

@jvonau
Copy link
Contributor Author

jvonau commented Jul 16, 2022

Wonder if forced reboot is a good idea?

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --text="You need to provision the network. Ensure you have your upstream wifi internet active first if needed. You will be prompted for your password and a reboot will follow. Do you want to Proceed?"
    rc=$?
    if [ $rc == "1" ]; then
        exit 1
    fi
    x-terminal-emulator -e /usr/local/bin/iiab-network
    reboot
fi

perhaps in /usr/local/bin/iiab-network?
How does the formatting look on RasPiOS? From the above pic it looks a bit ugly, and could use some work.

side note: /usr/share/mate/autostart/ works for iiab-network.desktop

@holta
Copy link
Member

holta commented Jul 16, 2022

Wonder if forced reboot is a good idea?

Debatable. Maybe capitalize REBOOT in the pop-up warning?

How does the formatting look on RasPiOS?

Vertical box is ugly. But so be it; that can always be cleaned up later if nec.

@jvonau jvonau mentioned this pull request Jul 16, 2022
@jvonau
Copy link
Contributor Author

jvonau commented Jul 17, 2022

/home/user/.config is not the ideal place given the random names that could be present as 'user' with the advent of removing the 'pi' user upstream. Think for RasPiOS the ideal location to call /usr/local/sbin/netwarn.sh might be /etc/xdg/lxsession/LXDE-pi/autostart like how 'chromium' is auto-launched.

#2609 (comment)

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@/etc/xdg/lxsession/LXDE-pi/sshpwd-lxde-iiab.sh
/usr/bin/chromium-browser --ignore-certificate-errors --disable-restore-session-state http://box/home

@holta any significant difference between using '@' to launch programs? And did you try calling /usr/local/sbin/netwarn.sh from /etc/xdg/lxsession/LXDE-pi/autostart?

@holta
Copy link
Member

holta commented Jul 17, 2022

@holta any significant difference between using '@' to launch programs?

The @ is optional. With @ present, if there is an error on first try, the system will attempt to run the command up to 4 more times before giving up.

https://forums.raspberrypi.com/viewtopic.php?t=294014

(Not sure we want that!)

did you try calling /usr/local/sbin/netwarn.sh from /etc/xdg/lxsession/LXDE-pi/autostart?

Yes, it works. FYI the browser partly covers up the prompt pop-up about 1 second later (no worries).

Tested with /usr/local/sbin/netwarn.sh containing:

zenity --width 350 --question --text="You need to provision the network.\n\nEnsure you have your upstream internet active first.  You'll be prompted for your password."

[[ $? == 0 ]] &&
    x-terminal-emulator -e "echo YAYAYA; sleep 5"

Wonder if forced reboot is a good idea?

Add another prompt prior to rebooting? (Forced reboots very much violate operator expectations!)

@holta
Copy link
Member

holta commented Jul 17, 2022

Yes, it works. FYI the browser partly covers up the prompt pop-up about 1 second later (no worries).

Correction/Clarification:

  • The zenity pop-up appears.
  • But the x-terminal-emulator (and its command) never appear — no matter whether you click 'Yes' or 'No'.

@jvonau
Copy link
Contributor Author

jvonau commented Jul 17, 2022

Did '--width 350' fix the formatting issues? try any other values? The x-terminal-emluator not running I suspect is your use of [[ $? == 0 ]] && where I used if [ $rc == "1" ]; then in #3308 (comment) Think I can stage the reboot question, let me work something up.

@jvonau
Copy link
Contributor Author

jvonau commented Jul 17, 2022

if ! [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    zenity --question --width=200 --text="You need to provision the network. Ensure you have your upstream internet active first. You might be prompted for your password. Should you not want to provision the network at this time just click NO"
    rc=$?
    if [ $rc == "1" ]; then
        exit 0
    fi
    x-terminal-emulator -e /usr/local/bin/iiab-network
    rc=$?
    if [ $rc == "1" ]; then
        zenity --question --width=200 --text="Network exited with error, please review /opt/iiab/iiab/iiab-network.log"
        exit 1
    fi
    zenity --question --width=200 --text="A REBOOT is recommended, would you like to REBOOT now?"
    rc=$?
    if [ $rc == "1" ]; then
        exit 0
    fi
    x-terminal-emulator -e /usr/sbin/reboot
fi

@holta
Copy link
Member

holta commented Jul 17, 2022

I suspect is your use of [[ $? == 0 ]] && where I used if [ $rc == "1" ]; then

  1. Both above failed.
  2. The problem was the lack of #!/bin/bash at the top of /usr/local/sbin/netwarn.sh
  3. FYI when run from autostart, each line blocks (i.e. waits for the line above to complete, before running the next line).
    • Whereas when run from the command-line, all lines in netwarn.sh are dispatched rapid-fire (i.e. almost simultaneously).
    • This makes testing without an actual reboot near-impossible — certainly on "RasPiOS with desktop", as testing without a reboot leads to (A) incorrect $rc return codes corresponding to the wrong lines, and (B) the "REBOOT now?" pop-up appears more than 3 minutes before it's supposed to :/
  4. Double square brackets should be used (instead of single square brackets, for string comparison) to avoid string problems.
  5. Many revisions/suggestions in the /usr/local/sbin/netwarn.sh below — tested on "RasPiOS with desktop":
#!/bin/bash

if [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    exit 0
fi

zenity --question --width=350 --text="IIAB needs to configure networking:\n\n► Internet must be live before you begin.\n►You might be prompted for your password.\n\nContinue?"
if [[ $? != "0" ]]; then
    exit 0
fi

x-terminal-emulator -e /usr/local/bin/iiab-network
if [[ $? != "0" ]]; then
    zenity --warning --width=350 --text="iiab-network exited with error.\n\nPlease review /opt/iiab/iiab/iiab-network.log"
    exit 1
fi

zenity --question --width=350 --text="iiab-network complete.\n\nWould you like to REBOOT now? (Recommended)"
if [[ $? == "0" ]]; then
    x-terminal-emulator -e "sudo reboot"
fi

@holta
Copy link
Member

holta commented Jul 17, 2022

incorrect $rc return codes corresponding to the wrong lines

CLARIF: $rc or $? make no difference.

As https://askubuntu.com/questions/627019/blocking-start-of-terminal mentions, very ugly hacks are required if one wants to guarantee the correct return code from x-terminal-emulator across different OS's (i.e. blocking/waiting until it's actually completed, by messing with PID's).

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

So that was for comparison, 2 The first example had the shbang line, whatever. 3 probably running via 'sh' without the shbang line present, which makes [[]] (edit and == ) non-POSIX. Now width=350 or width=200 stretch the box across the top of the desktop instead of a nice sized box that I see. Got a pic of what you are seeing with that set? Not sure I like the if [ -f ] exit 0 as nothing really ran, just didn't meet the conditional.

@holta
Copy link
Member

holta commented Jul 18, 2022

On RasPiOS, width=350 is the minimum to avoid disjointed, ugly, vertical rectangles (screenshot already posted, this is basically what you get with anorexic width=200 on RasPiOS).

Possibly the situation is different on other OS's using different fonts/windowing/etc.

3 probably running via 'sh' without the shbang line present, which makes [[]] (edit and == ) non-POSIX

[CORRECTION BELOW.] No. I verified that bash (not sh) is running in both cases, and that [[ condition ]] [runs, albeit not always correctly] in both cases. Strange yes, but it's related to the blocking / asynchronous execution issues — i.e. it depends on whether the script was invoked by autostart or not.

@holta
Copy link
Member

holta commented Jul 18, 2022

CORRECTION:

I fell in the trap of assuming $SHELL value /bin/bash was meaningful. Unfortunately it is not, when run within scripts.

  • The meaningful way of displaying the running shell is to extract it from ps -p $$ (which when invoked from autostart on RasPiOS is indeed sh == /usr/bin/sh == /usr/bin/dash == Debian Almquist shell).
  • Whereas when the same script is invoked from the command-line, ps -p $$ instead displays bash.

Conclusion: avoid all problems by putting #!/bin/bash on top of bash scripts, in case they're later invoked in unexpected ways (e.g. via XDG's autostart).

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

Formatting looks ok, didn't like the display of 'please check iiab-network.log' when X'ing out of the box how about this?

#!/bin/bash

if [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    exit
fi

zenity --question --width=350 --text="IIAB needs to configure networking:\n\n► Internet must be live before you begin.\n►You might be prompted for your password.\n\nContinue?"
case $? in
    -1|5)
        exit 1;;

    1)
        exit 0;;

    0)
        x-terminal-emulator -e /usr/local/bin/iiab-network;;
esac

if [[ $? != "0" ]]; then
    zenity --warning --width=350 --text="iiab-network exited with error.\n\nPlease review /opt/iiab/iiab/iiab-network.log"
    exit 1
fi

zenity --question --width=350 --text="iiab-network complete.\n\nWould you like to REBOOT now? (Recommended)"
if [[ $? == "0" ]]; then
    x-terminal-emulator -e "sudo reboot"
fi

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

Here is an light bulb moment, think lightdm on ubuntu is ignoring the shbang line at the top of the script...

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

For the life of me using "!" didn't work under bash or dash when called by lightdm and works the same with or without '#!/bin/sh' at the top of the file. Give this test a spin.

#!/bin/sh

if [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    exit
fi

zenity --question --width=350 --text="IIAB needs to configure networking:\n\n► Internet must be live before you begin.\n►You might be prompted for your password.\n\nContinue?"
case $? in
    -1|5)
        exit 1
        ;;

    1)
        exit 0
        ;;

    0)
        x-terminal-emulator -e /usr/local/bin/iiab-network
        ;;
esac

if [ "$?" = "1" ]; then
    zenity --warning --width=350 --text="iiab-network exited with error.\n\nPlease review /opt/iiab/iiab/iiab-network.log"
    exit 1
fi

zenity --question --width=350 --text="iiab-network complete.\n\nWould you like to REBOOT now? (Recommended)"
if [ "$?" = "0" ]; then
    x-terminal-emulator -e "sudo reboot"
fi

@holta
Copy link
Member

holta commented Jul 18, 2022

  1. If someone adds a graphical desktop to a headless OS, will this XDG autostart exist to then kick off netwarn.sh? (Not that distro fanatic corner cases are very important, as engineering needs to focus on field educators primarily, but am just asking...)

  2. If DASH (Debian's shell) is required, how about /usr/local/sbin/netwarn.sh below, that builds on Network helper #3308 (comment) just above?

    This latest (below) was repeatedly tested on 64-bit RasPiOS with desktop on RPi 4 — it works very well — also the pop-ups are now more presentable thanks to further visual clean-ups: (e.g. expanded from --width=350 to 360 to make prompt sentences + spacing more readable, etc)

#!/bin/sh
# Use sh not bash, to align with Ubuntu LightDM which *ignores* shebang line

if [ -f /etc/iiab/install-flags/iiab-network-complete ]; then
    exit
fi

zenity --question --width=360 --text="IIAB needs to configure networking:\n\n► Internet must be live before you begin.\n►You might be prompted for your password.\n\nContinue?  (This can take 2-3 minutes)"
rc=$?
if [ "$rc" != "0" ]; then
    exit $rc
fi

x-terminal-emulator -e /usr/local/bin/iiab-network
rc=$?
if [ "$rc" != "0" ]; then
    zenity --warning --width=360 --text="iiab-network exited with error: $rc\n\nPlease review /opt/iiab/iiab/iiab-network.log"
    exit $rc
fi

zenity --question --width=360 --text="iiab-network complete.\n\nWould you like to REBOOT now?  (Recommended)"
if [ "$?" = "0" ]; then
    x-terminal-emulator -e "sudo reboot"
fi

@holta
Copy link
Member

holta commented Jul 18, 2022

QUESTION: Does iiab-network have a mechanism to prevent it from being run twice (simultaneously) ?

e.g. if Admin Console of any other user or script invoked roles/network simultaneously — are these kinds of things trapped / avoided / explained?

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

Like I said above there is no way that lightdm accepts '!' anywhere in the script, period. Think iiab-admin does for it's self but iiab-network is just a script with no concurrent checking, I didn't think people would be that foolish to launch it from 2 different login points at the same relative time.

@jvonau
Copy link
Contributor Author

jvonau commented Jul 18, 2022

Question 1 currently mate would be the only other supported desktop after RasPiOS, but others can be added later and we should cover ssh logins also, lets move this conversation to #3313 after #3314 is merged.

@tim-moody
Copy link
Contributor

I don't think ansible will run two sessions simultaneously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants