Sauce Please
Posted by gargamel on 2009-08-26 02:31:50 UTC

So, since I actually had a reason to use this on a customer box today, here’s a fun little bit of bashscript madness that I’ve been playing around with:

source_http() {
  eval "$(wget -q -t 1 --timeout=5 -O - $1)" 
}

Looks dumb, no? It is, especially if the remote location that you source is not absolutely secure. It can be handy at times, however. For example, I’ve got a source-based package builder (kinda like a really, really simple portage) that I use to install some common stuff on customer servers. It sources the meat of its internal functions through HTTP, and it also uses this mechanism to absorb the build scripts into its environment so that I don’t have to have redundant copies of the build scripts on every server that gets an ffmpeg install.

For another example, assume that you’ve got a big nasty bunch of functions, aliases, environment variables and so on, and you’d like to be able to use them regardless of the computer that you’re using. You could do something like this in your .bashrc:

source_http() {
  eval "$(wget -q -t 1 --timeout=5 -O - $1)" 
}

if [ -f "/path/to/my/functions" ]
then
  . /path/to/my/functions
else
  source_http http://myserver.tld/path/to/my/functions
fi

What you end up with there is a check to see if the functions exist locally. If they don’t for some reason (like if you’re using drbdlinks), then it falls back to sourcing the functions via HTTP. If that fails, of course, you’re still left without your functions, but it’s worth a shot, I’d imagine.

Anyways, that’s all I’ve got right now. Enjoy.


Post A New Comment
Name (required)
Email (required)
Site

Comments

Posted by Raam Dev on 2009-08-26 21:09:18 UTC
This is awesome. I've been meaning to set something like this after you showed it to me awhile back, but I misplaced my notes. Now that you've got it posted on your blog I have no excuse. :)

Thanks!