Showing posts with label LInux. Show all posts
Showing posts with label LInux. Show all posts

Friday, August 31, 2012

Simple RPM for Shell Scripts

Some of the folks down stream from me think that tar archives and customized install scripts were too difficult to deal with. Thus they requested that I begin migrating my install packages to RPMs. I felt that this was not an unreasonable request, thus began a research project trying to learn everything I could. I decided to start with two small projects the first simply two files and the second about twelve.  I found many examples and some good write ups but even the simple examples were more complicated than what I needed. I eventually figured it out and have simplified the process to the following 5 steps, for the most basic install.


Step1: Create a linux user called "rpmbuild "
For various reasons it is best not to do this as the root user.

Step2: Create required directories under the rpmbuild user account

Because I have used this account to create multiple packages I've setup the following directory structure

Shell> mkdir -p /home/rpmbuild/packages/mygreatrpm
Shell> cd /home/rpmbuild/packages/mygreatrpm
Shell> mkdir BUILD RPMS SOURCES SPECS SRPMS

Step3: Copy your Shell scripts to the SOURCES directory
Step4: Create your spec file in the SPECS directory

Shell> vi SPECS/MyScript.spec

#
%define _topdir /home/rpmbuild/packages/mygreatrpm
Name            : MyScript
Summary         : Does something REALLY great
Version         : %{VERSION}
Release         : %{REV}
Group:          : Applications/Databases
License:        : (c) MyCompany
BuildArch       : noarch
BuildRoot       : %{_topdir}/%{name}-%{version}-root

# Use "Requires" for any dependencies, for example:
# Requires        : tomcat6

# Description gives information about the rpm package. This can be expanded up to multiple lines.
%description
This tool is designed to watch your kids, take out the trash, feed the cat and clean the refridgerator.

# Prep is used to set up the environment for building the rpm package
# Expansion of source tar balls are done in this section
%prep

# Used to compile and to build the source
%build

# The installation.
# We actually just put all our install files into a directory structure that
# mimics a server directory structure here
%install
rm -rf $RPM_BUILD_ROOT
install -d -m 755 $RPM_BUILD_ROOT/opt/MyTools
cp ../SOURCES/myscript.dat $RPM_BUILD_ROOT/opt/MyTools/.
cp ../SOURCES/myscript.sh $RPM_BUILD_ROOT/opt/MyTools/.

%post
echo "Installed %{name} scripts to /opt/MyTools"
# Contains a list of the files that are part of the package
# See useful directives such as attr here: http://www.rpm.org/max-rpm-snapshot/s1-rpm-specref-files-list-directives.html
%files
%defattr(755, root, root)
/opt/MyTools/myscript.dat
/opt/MyTools/myscript.sh
# Used to store any changes between versions
%changelog
* Fri Aug 31 2012 firstname lastname
- Initial release.

STEP 5: Create the package
Shell> cd /home/rpmbuild/packages/mygreatrpm
Shell> rpmbuild -bb --define 'VERSION 1.0' --define 'REV 1234' SPECS/MyScript.spec
...snip...
/home/rpmbuild/packages/mygreatrpm/RPMS/noarch/MyScript-1.0-1234.noarch.rpm


Monday, August 27, 2012

Script runs as root but not as crontab? or
Why does my script run as the root user but won't run from cron.daily? or
What user does cron.daily run as?

I recently ran into a problem with a Subversion update script I wished to execute daily. Unfortunately the svn update failed due to a permissions error. This made little sense to me as I believed if I ran the script as root the script should work. It turns out that on some *nix systems (RH Linux and other variants) the root user has a home directory called /root. Within this directory is where the .subversion directory sits which contains your connection information.  Thus solution was to set the HOME variable in my script, as crontab doesn't necessarily pick up environment variables that you get when you log in.

Below you will find the simple fix.


#!/bin/sh
HOME=/root
export HOME=${HOME}
cd /usr/local/svn_workspace/trunk
/opt/CollabNet_Subversion/bin/svn update