Using Make for server maintenance (part 1)
August 5, 2009
I admit it. I like Make. Mostly because it allows you to enter command line entries directly into a build script. Here’s a simplistic introduction.
First of all, you’ll want to download all the proper versions of the utilities you want to use and move them onto a file share somewhere which easily mountable. I just got a NetGear ReadyNAS which I like a lot. You’ll want to keep copies of the exact versions you use because a lot of folks are hiding their older versions.
Now, about that Makefile. I use Fedora Core, and when I create a clean server it doesn’t have much on it. So let’s define a macro for dependencies that’s easy to maintain:
DEPS=openldap.i386 gcc gd.i386 gd-devel libjpeg.i386 libjpeg-devel.i386
$(DEPS) : if test -z `yum list | grep ^$@ | grep installed | sed -e "s/ //g"`; then\ yum -y install $@; \ fi; installdeps: $(DEPS)
Now calling make installdeps will loop through find and install your dependencies.
How does this work? Let’s take a look:
DEPS=[a list]
$(DEPS):
[some command line entries]
installdeps: $(DEPS)
Make sees your list (called a macro) assigned to DEPS. Just below that, if it sees your exact list of dependencies (dereferenced in Make via $(variable)), it will run the command line entries for each item in your list (that’s the cool part) — this is called the target. But you don’t want to type all those dependencies again (a la make openldap.i386 gcc etc etc...), so we make a second target that invokes the first: installdeps.
Note the formatting, Make can behave like Python — if it’s on the same line as the target, it will be called prior to running anything below the target (you can also list as many dependencies as you need). The commands below the target must be indented with tabs.
In part 2 I’ll go over some conditional syntax and (what I believe to be) some best practices.