From 1468058d1178b675e4ef8a81112bacc783cbdf8f Mon Sep 17 00:00:00 2001 From: Anthony Shortland Date: Fri, 26 Oct 2012 15:57:35 -0700 Subject: [PATCH] Fixes #31: Added support for "--format rpm" option to stubbs:archive to support building RPMs for one or more specified modules. --- modules/stubbs/commands/add-module/default.sh | 2 + modules/stubbs/commands/archive/default.sh | 213 +++++++++++------- modules/stubbs/commands/archive/format.option | 11 + modules/stubbs/commands/archive/options.sh | 37 +-- .../stubbs/commands/archive/release.option | 11 + modules/stubbs/stubbs.1 | 20 +- modules/stubbs/templates/rerun-module.spec | 48 ++++ modules/stubbs/tests/archive-1-test.sh | 9 + 8 files changed, 258 insertions(+), 93 deletions(-) create mode 100644 modules/stubbs/commands/archive/format.option create mode 100644 modules/stubbs/commands/archive/release.option create mode 100644 modules/stubbs/templates/rerun-module.spec diff --git a/modules/stubbs/commands/add-module/default.sh b/modules/stubbs/commands/add-module/default.sh index fa0c89a..7814e48 100644 --- a/modules/stubbs/commands/add-module/default.sh +++ b/modules/stubbs/commands/add-module/default.sh @@ -68,6 +68,8 @@ cat < $RERUN_MODULES/$MODULE/metadata || rerun_die diff --git a/modules/stubbs/commands/archive/default.sh b/modules/stubbs/commands/archive/default.sh index 8debfa5..33a6a1a 100644 --- a/modules/stubbs/commands/archive/default.sh +++ b/modules/stubbs/commands/archive/default.sh @@ -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 ] --modules <*> [ --release|-r <1>] [ --version|-v <>] # Source common function library . $RERUN_MODULE_DIR/lib/functions.sh || { echo >&2 "failed laoding function library" ; exit 1 ; } @@ -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 diff --git a/modules/stubbs/commands/archive/format.option b/modules/stubbs/commands/archive/format.option new file mode 100644 index 0000000..69892ba --- /dev/null +++ b/modules/stubbs/commands/archive/format.option @@ -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 + diff --git a/modules/stubbs/commands/archive/options.sh b/modules/stubbs/commands/archive/options.sh index a1c4506..1d0303a 100644 --- a/modules/stubbs/commands/archive/options.sh +++ b/modules/stubbs/commands/archive/options.sh @@ -1,7 +1,7 @@ -# 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 ] --modules <*> [ --release|-r <1>] [ --version|-v <>] # print USAGE and exit rerun_option_usage() { @@ -9,29 +9,30 @@ rerun_option_usage() { 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 @@ -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 diff --git a/modules/stubbs/commands/archive/release.option b/modules/stubbs/commands/archive/release.option new file mode 100644 index 0000000..2c4f637 --- /dev/null +++ b/modules/stubbs/commands/archive/release.option @@ -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 + diff --git a/modules/stubbs/stubbs.1 b/modules/stubbs/stubbs.1 index b512951..c3bdf59 100644 --- a/modules/stubbs/stubbs.1 +++ b/modules/stubbs/stubbs.1 @@ -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 @@ -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 ] --modules <"*"> [--release <1>] [--version <>] "build a self extracting rerun" .PP \f[I]OPTIONS\f[] @@ -145,6 +145,13 @@ 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[] @@ -152,6 +159,13 @@ arguments: \f[I]true\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[] @@ -238,7 +252,7 @@ arguments: \f[I]true\f[] .PP Successful completion: 0 .SH AUTHORS -alexh +anthony .SH "SEE ALSO" rerun .SH KEYWORDS diff --git a/modules/stubbs/templates/rerun-module.spec b/modules/stubbs/templates/rerun-module.spec new file mode 100644 index 0000000..e51e981 --- /dev/null +++ b/modules/stubbs/templates/rerun-module.spec @@ -0,0 +1,48 @@ +# +# Rerun module RPM specification file. +# +# This SPEC file is setup to be used by stubbs:archive which will supply values +# for %{module}, %{desc}, %{version} and %{requires} from the module metadata, +# and %{release} from the command line: +# +Summary: Rerun %{module} module +Name: rerun-%{module} +Version: %{version} +Release: %{release} + +License: ASL 2.0 +Group: Applications/System + +Source: rerun-%{module}-%{version}.tgz +Requires: %{requires} + +# Disables debug packages and stripping of binaries: +%global _enable_debug_package 0 +%global debug_package %{nil} +%global __os_install_post %{nil} + +%description +%{desc} + +%prep + +%setup + +%build + +%install +rm -rf %{buildroot} +install -d -m 755 %{buildroot}/usr/lib/rerun/modules/%{module} +mv %{_builddir}/rerun-%{module}-%{version}/* %{buildroot}/usr/lib/rerun/modules/%{module} + +%clean + +%files +%defattr(-,root,root) +/usr/lib/rerun/modules/%{module} + +%changelog + +%pre + +%post diff --git a/modules/stubbs/tests/archive-1-test.sh b/modules/stubbs/tests/archive-1-test.sh index ae4ec7a..706d88e 100644 --- a/modules/stubbs/tests/archive-1-test.sh +++ b/modules/stubbs/tests/archive-1-test.sh @@ -51,3 +51,12 @@ it_runs_fully_optioned() { rm /tmp/rerun.bin.$$ } +it_builds_the_stubbs_module_rpm() { + TMPDIR=$(/bin/mktemp -d) + pushd $TMPDIR + rerun stubbs:archive --format rpm --modules stubbs + rpm -qi -p rerun-stubbs-1.0.0-1.noarch.rpm + popd + rm -rf $TMPDIR +} +