Skip to content

Commit

Permalink
Fixes dtolabs#31: Added support for "--format rpm" option to stubbs:a…
Browse files Browse the repository at this point in the history
…rchive to support building RPMs for one or more specified modules.
  • Loading branch information
Anthony Shortland committed Oct 26, 2012
1 parent 343bb3c commit 1468058
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 93 deletions.
2 changes: 2 additions & 0 deletions modules/stubbs/commands/add-module/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ cat <<EOF
# $(date)
NAME=$MODULE
DESCRIPTION="$DESC"
VERSION=
REQUIRES=
EOF
) > $RERUN_MODULES/$MODULE/metadata || rerun_die
Expand Down
213 changes: 137 additions & 76 deletions modules/stubbs/commands/archive/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Build a self extracting archive
#
#/ usage: stubbs:archive [ --file|-f <>] --modules <*> [ --version|-v <>]
#/ usage: rerun stubbs:archive [ --file|-f <>] [ --format|-f <bin>] --modules <*> [ --release|-r <1>] [ --version|-v <>]

# Source common function library
. $RERUN_MODULE_DIR/lib/functions.sh || { echo >&2 "failed laoding function library" ; exit 1 ; }
Expand All @@ -18,91 +18,152 @@
. $RERUN_MODULE_DIR/commands/archive/options.sh
}

CWD=$(pwd); #remember current working directory.
buildbinarchive() {
CWD=$(pwd); #remember current working directory.

# Check if file option was specified and if not set it to rerun.bin.
[ -z "${FILE}" ] && FILE=rerun.bin
# Check if file option was specified and if not set it to rerun.bin.
[ -z "${FILE}" ] && FILE=rerun.bin

# Prepend curren working directory if relative file path.
[[ ${FILE} == "/"* ]] || FILE=$CWD/$FILE
# Prepend curren working directory if relative file path.
[[ ${FILE} == "/"* ]] || FILE=$CWD/$FILE

[ ! -d $(dirname ${FILE}) ] && rerun_option_error "directory not found: $(dirname ${FILE})"
[ ! -d $(dirname ${FILE}) ] && rerun_option_error "directory not found: $(dirname ${FILE})"

# Default version to blank if unspecified.
[ -z "${VERSION}" ] && VERSION=
# Default version to blank if unspecified.
[ -z "${VERSION}" ] && VERSION=

# create a work directory the archive content
export PAYLOAD=`mktemp -d /tmp/rerun.stubbs:archive.XXXXXX` || rerun_die
# create a work directory the archive content
export PAYLOAD=`mktemp -d /tmp/rerun.stubbs:archive.XXXXXX` || rerun_die

#
# Start preparing the payload content.
#
#
# Start preparing the payload content.
#

# Iterate through the the specified modules and add them to payload
mkdir -p $PAYLOAD/rerun/modules || rerun_die
pushd $RERUN_MODULES >/dev/null || rerun_die
for module in $MODULES
do
# Check for a commands subdir to be sure it looks like a module
if [ -d $RERUN_MODULES/$module/commands ]
then
# Iterate through the the specified modules and add them to payload
mkdir -p $PAYLOAD/rerun/modules || rerun_die
pushd $RERUN_MODULES >/dev/null || rerun_die
for module in $MODULES
do
# Check for a commands subdir to be sure it looks like a module
if [ -d $RERUN_MODULES/$module/commands ]
then
cp -r $RERUN_MODULES/$module $PAYLOAD/rerun/modules || rerun_die
fi
done
popd >/dev/null

# Copy rerun itself to the payload
cp $RERUN $PAYLOAD/rerun || rerun_die

# Copy in the extract and launcher scripts used during execution
for template in $RERUN_MODULE_DIR/templates/{extract,launcher}
do
# replace the template substitution tokens ...
sed -e "s/@GENERATOR@/stubbs:archive/" \
-e "s/@DATE@/$(date)/" \
-e "s/@USER@/$USER/" \
-e "s/@VERSION@/$VERSION/" \
$template > $PAYLOAD/$(basename $template) || rerun_die
# ... and save it to the payload --^
done

#
# Archive the content
#

cd $PAYLOAD

# make the payload.tar file
tar cf payload.tar launcher extract rerun || rerun_die

# compress and base64 encode the tar file
if [ -e "payload.tar" ]; then
gzip -c payload.tar | openssl enc -base64 > payload.tgz.base64 || rerun_die

if [ -e "payload.tgz.base64" ]; then
#
# Prepend the extract script to the payload.
# and thus turn the thing into a shell script!
#
cat extract payload.tgz.base64 > ${FILE} || rerun_die
else
rerun_die "$PAYLOAD/payload.tgz.base64 does not exist"
fi
else
rerun_die "payload.tar does not exist"
fi

#
# Make the archive executable
#
chmod +x ${FILE} || rerun_die "failed setting archive executable"
#
# Clean up the temp directory
#
rm -rf $PAYLOAD
fi
done
popd >/dev/null

# Copy rerun itself to the payload
cp $RERUN $PAYLOAD/rerun || rerun_die

# Copy in the extract and launcher scripts used during execution
for template in $RERUN_MODULE_DIR/templates/{extract,launcher}
do
# replace the template substitution tokens ...
sed -e "s/@GENERATOR@/stubbs:archive/" \
-e "s/@DATE@/$(date)/" \
-e "s/@USER@/$USER/" \
-e "s/@VERSION@/$VERSION/" \
$template > $PAYLOAD/$(basename $template) || rerun_die
# ... and save it to the payload --^
done

#
# Archive the content
#

cd $PAYLOAD

# make the payload.tar file
tar cf payload.tar launcher extract rerun || rerun_die

# compress and base64 encode the tar file
if [ -e "payload.tar" ]; then
gzip -c payload.tar | openssl enc -base64 > payload.tgz.base64 || rerun_die

if [ -e "payload.tgz.base64" ]; then
#
# Prepend the extract script to the payload.
# and thus turn the thing into a shell script!
#
cat extract payload.tgz.base64 > ${FILE} || rerun_die
else
rerun_die "$PAYLOAD/payload.tgz.base64 does not exist"
fi
else
rerun_die "payload.tar does not exist"
fi

#
# Make the archive executable
#
chmod +x ${FILE} || rerun_die "failed setting archive executable"
#
# Clean up the temp directory
#
rm -rf $PAYLOAD


echo "Wrote self extracting archive script: ${FILE}"
}

buildrpmarchive() {
for MODULE_DIR in $RERUN_MODULES/$MODULES
do
if [[ -r $MODULE_DIR/metadata ]]
then
# Setup a temporary directory to build the RPM:
RPMTOPDIR=$(/bin/mktemp -d) || rerun_die "couldn't make a temporary directory to build the rpm"
mkdir $RPMTOPDIR/SOURCES || rerun_die

# Source the module metadata:
unset NAME DESCRIPTION VERSION REQUIRES
. $MODULE_DIR/metadata

[[ -n $NAME ]] || rerun_die "no module name in \"$MODULE_DIR/metadata\""
[[ -n $DESCRIPTION ]] || rerun_die "no description in \"$MODULE_DIR/metadata\""

if [[ -z $VERSION ]]
then
VERSION=1.0.0
echo "warning: no version in \"$MODULE_DIR/metadata\", defaulting to $VERSION"
fi

if [[ -z $REQUIRES ]]
then
REQUIRES=rerun
else
REQUIRES=rerun,$REQUIRES
fi

# Build the RPM source archive:
pushd $MODULE_DIR > /dev/null
/bin/tar --transform="s/^\./rerun-${NAME}-${VERSION}/" -zcf $RPMTOPDIR/SOURCES/rerun-${NAME}-${VERSION}.tgz . || rerun_die "failed to build \"$RPMTOPDIR/SOURCES/rerun-${NAME}-${VERSION}.tgz\""

# Build the RPM:
/usr/bin/rpmbuild --quiet --target noarch --define "_topdir $RPMTOPDIR" --define "module ${NAME}" --define "desc $DESCRIPTION" --define "version $VERSION" --define "release $RELEASE" --define "requires $REQUIRES" -bb $RERUN_MODULE_DIR/templates/rerun-module.spec || rerun_die "failed to build rerun-$NAME-$VERSION-$RELEASE.noarch.rpm"
popd > /dev/null

# Move the RPM:
/bin/mv $RPMTOPDIR/RPMS/noarch/rerun-$NAME-$VERSION-$RELEASE.noarch.rpm . || rerun_die "failed to move rerun-$NAME-$VERSION-$RELEASE.noarch.rpm"
echo "Wrote $(basename $MODULE_DIR) module rpm : rerun-$NAME-$VERSION-$RELEASE.noarch.rpm"

# Clean up the temporary directory:
/bin/rm -rf $RPMTOPDIR
fi
done
}
case $FORMAT in
bin)
buildbinarchive
;;
rpm)
buildrpmarchive
;;
*)
rerun_die "invalid archive format \"$FORMAT\""
;;
esac

echo "Wrote self extracting archive script: ${FILE}"
exit 0

# Done
Expand Down
11 changes: 11 additions & 0 deletions modules/stubbs/commands/archive/format.option
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# generated by stubbs:add-option
# Fri Oct 26 14:40:44 PDT 2012
NAME=format
DESCRIPTION="Specify the format of the archive(s) to build"
ARGUMENTS=true
REQUIRED=false
SHORT=f
LONG=format
DEFAULT=bin
EXPORT=false

37 changes: 23 additions & 14 deletions modules/stubbs/commands/archive/options.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
# generated by add-option
# Fri Sep 16 08:04:55 PDT 2011
# Generated by stubbs:add-option. Do not edit, if using stubbs.
# Created: Fri Oct 26 14:41:19 PDT 2012
#
#/ usage: stubbs:archive --file|-f <> [--modules|-m <>] --version|-v <>
#/ usage: stubbs:archive [ --file|-f <>] [ --format|-f <bin>] --modules <*> [ --release|-r <1>] [ --version|-v <>]

# print USAGE and exit
rerun_option_usage() {
grep '^#/ usage:' <"$0" | cut -c4- >&2
return 2
}

# print USAGE and exit
# print SYNTAX and exit
rerun_option_error() {
if [[ "$RERUN_COLOR" == "true" ]]
then echo -e ${red}"SYNTAX: $*"${_red} >&2
else echo "SYNTAX: $*" >&2
then echo >&2 -e """SYNTAX: $*"""
else echo >&2 "SYNTAX: $*"
fi
exit 2
}


# check option has its argument
rerun_option_check() {
[ "$1" -lt 2 ] && rerun_option_error
[ "$1" -lt 2 ] && rerun_option_usage
}

# options: [file modules version]
# options: [file format modules release version]
while [ "$#" -gt 0 ]; do
OPT="$1"
case "$OPT" in
-f|--file) rerun_option_check $# ; FILE=$2 ; shift ;;
-m|--modules) rerun_option_check $# ; MODULES=$2 ; shift ;;
-v|--version) rerun_option_check $# ; VERSION=$2 ; shift ;;
# unknown option
--file|-f) rerun_option_check $# ; FILE=$2 ; shift ;;
--format|-f) rerun_option_check $# ; FORMAT=$2 ; shift ;;
--modules) rerun_option_check $# ; MODULES=$2 ; shift ;;
--release|-r) rerun_option_check $# ; RELEASE=$2 ; shift ;;
--version|-v) rerun_option_check $# ; VERSION=$2 ; shift ;;
# help option
-?)
rerun_option_usage
exit 2
Expand All @@ -44,4 +45,12 @@ while [ "$#" -gt 0 ]; do
done

# If defaultable options variables are unset, set them to their DEFAULT
[ -z "$MODULES" ] && MODULES="*"
[ -z "$FORMAT" ] && FORMAT="bin"
[ -z "$MODULES" ] && MODULES=""*""
[ -z "$RELEASE" ] && RELEASE="1"
# Check required options are set
[ -z "$MODULES" ] && { echo >&2 "missing required option: --modules" ; return 2 ; }
# If option variables are declared exportable, export them.

#
return 0
11 changes: 11 additions & 0 deletions modules/stubbs/commands/archive/release.option
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# generated by stubbs:add-option
# Fri Oct 26 14:41:19 PDT 2012
NAME=release
DESCRIPTION="Specify the release number of the archive"
ARGUMENTS=true
REQUIRED=false
SHORT=r
LONG=release
DEFAULT=1
EXPORT=false

20 changes: 17 additions & 3 deletions modules/stubbs/stubbs.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH stubbs 1 "Sun Oct 14 13:20:08 EDT 2012" "Version 1" "Rerun User Manual"
.TH stubbs 1 "Fri Oct 26 15:54:56 PDT 2012" "Version 1" "Rerun User Manual"
.SH NAME
stubbs \- "Simple rerun module builder"
.PP
Expand Down Expand Up @@ -134,7 +134,7 @@ required: \f[I]false\f[] ,
arguments: \f[I]true\f[]
.RS
.RE
.SH stubbs:archive \f[][--file <>] --modules <"*"> [--version <>]
.SH stubbs:archive \f[][--file <>] [--format <bin>] --modules <"*"> [--release <1>] [--version <>]
"build a self extracting rerun"
.PP
\f[I]OPTIONS\f[]
Expand All @@ -145,13 +145,27 @@ arguments: \f[I]true\f[]
.RS
.RE
.TP
.B \--format \f[]"Specify the format of the archive(s) to build"\f[]
required: \f[I]false\f[] ,
arguments: \f[I]true\f[]
, default: \f[I]bin\f[]
.RS
.RE
.TP
.B \--modules \f[]"modules glob pattern"\f[]
required: \f[I]true\f[] ,
arguments: \f[I]true\f[]
, default: \f[I]"*"\f[]
.RS
.RE
.TP
.B \--release \f[]"Specify the release number of the archive"\f[]
required: \f[I]false\f[] ,
arguments: \f[I]true\f[]
, default: \f[I]1\f[]
.RS
.RE
.TP
.B \--version \f[]"the archive version"\f[]
required: \f[I]false\f[] ,
arguments: \f[I]true\f[]
Expand Down Expand Up @@ -238,7 +252,7 @@ arguments: \f[I]true\f[]
.PP
Successful completion: 0
.SH AUTHORS
alexh
anthony
.SH "SEE ALSO"
rerun
.SH KEYWORDS
Expand Down
Loading

0 comments on commit 1468058

Please sign in to comment.