joukewitteveen / netctl

Profile based systemd network management
https://gitlab.archlinux.org/archlinux/netctl
GNU General Public License v3.0
176 stars 65 forks source link

Export profile and connection variables to environment when running Exec* scripts #147

Closed roobre closed 7 years ago

roobre commented 7 years ago

I think it could be really useful if the scripts called via ExecUpPost, ExecUpPre, etc had acces to the variables defined in the profile. This would allow scripts called from hooks/ or interfaces/ to perform profile-specific actions without redefining them (e.g. execute actions depending on the profile type).

Also, for DHCP-enabled connections, some extra variables like the ip address, the default gateway, and even DNS would help a lot when programatically deploying firewall rules.

What do you think about this feature?

joukewitteveen commented 7 years ago

They already have access! The profile name is available as $Profile and all other variables set in the profile file are available too. For dynamic addresses, that information should be obtained by the scripts themselves.

roobre commented 7 years ago

I'm sorry but I have to disagree.

I've created a hook containing

ExecUpPost='printenv > /tmp/env'

And after restarting netctl-auto@interface, I only get

[root@Mechon]# ~> cat /tmp/env 
ACTION=CONNECT
LANG=en_US.UTF-8
INVOCATION_ID=7436469321d64ac4a2599b99b95989f7
PWD=/
JOURNAL_STREAM=8:26432
SSID=eduroam
INTERFACE=wlan0
SHLVL=2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
_=/usr/sbin/printenv

Some extra information like the IP (mainly to check if it's dhcp-managed or not, the DNS (for the same reason) could be pretty useful imho. Are these variables being excluded on purpose for some reason?

joukewitteveen commented 7 years ago

The variables are not exported, hence they are available as shell variables and not as environment variables. This is fine since ExecUpPost= runs in the same context as netctl (technically, the network script) itself. Replace printenv with set for a fun list of everything (including functions) that is available. A list of merely the variables can be obtained by using set -o posix; set.

roobre commented 7 years ago

Indeed, I can confirm that is the case. Maybe this behaviour should be documented somewhere, the "ExecUpPost=" section in the netctl.profile man page seems like a good place to me.

Anyway, thanks for the info!

joukewitteveen commented 7 years ago

Patches are welcome. Preferably on the arch-projects mailinglist, but a PR is fine too.

roobre commented 7 years ago

I might write a couple lines then :)

Regarding the initial issue, I have one more concern. The Exec* line does indeed execute on the same context the netctl profile is in, but if a script is called from this line, it does not inherit these variables.

As (I suppose) most Exec scripts will be called this way instead of inlined, I think exporting some variables might be worth reconsidering.

Exporting them manually on the Exec line doesn't seem to work for some reason (e.g. ExecUpPost="export Ip && /usr/bin/userscript" doesn't make Ip visible from userscript.