Reconnect WiFi on Ubuntu Core

I am trying to reconnect to a wifi network on a Raspberry Pi with Ubuntu Core. In my snap I have:

  reconnect:
    command: systemctl restart systemd-networkd.service
    daemon: oneshot
    timer: '01:00'
    plugs: [ network-control ]

but I keep getting a permission denied error.

/snap/.../command-reconnect.wrapper: 2: exec: /bin/systemctl: Permission denied

I did connect the network-control plug manually.

I guess I am using the wrong plug?

I have a WiFi USB stick connected to the RPi but sometimes Ubuntu boots too fast, the stick hasn’t finished booting yet so the network isn’t up. My solution is to restart the networkd service but maybe there are better ways?

Thank you!

you wont be able to use systemctl from inside a snap, but with a connected network-control interface you can talk to netplan via dbus, like:

  dbus-send --system --type=method_call --print-reply \
	  --dest=io.netplan.Netplan /io/netplan/Netplan io.netplan.Netplan.Apply

so make your snap ship a shellscript that calls the above line and your wifi should reconnect …

Thanks for the suggestion. Would you know what permissions it needs?

script

#! /bin/sh
dbus-send --system --type=method_call --print-reply --dest=io.netplan.Netplan /io/netplan/Netplan io.netplan.Netplan.Apply

snapcraft.yaml

reconnect:
  command: $SNAP/bin/reconnect.sh
  daemon: oneshot
  timer: '01:00'
  plugs: [ dbus ]

But dbus doesn’t seem to be a valid plug (on the Raspberry Pi at least).

warning: |
snap has bad plugs or slots: dbus (cannot find attribute ‘bus’)

I also tried with the network-control plug which gives me:

Error org.freedesktop.DBus.Error.AccessDenied: An AppArmor policy prevents this sender from sending this message to this recipient; type=“method_call”, sender=":1.6" (uid=0 pid=3117 comm=“dbus-send --system --type=method_call --print-repl” label=“snap.snapname.reconnect (enforce)”) interface=“io.netplan.Netplan” member=“Apply” error name="(unset)" requested_reply=“0” destination=“io.netplan.Netplan” (bus)

just don’t forget to make it executable :wink:

you want just network-setup-control that includes permissions for this dbus call

The network-setup-control plug seems to do the trick. Thank you @ogra!