| 1 | #!/bin/sh
|
|---|
| 2 |
|
|---|
| 3 | #
|
|---|
| 4 | # rpmbuild-bot.sh: RPM Build Bot version 1.1.0.
|
|---|
| 5 | #
|
|---|
| 6 | # Author: Dmitriy Kuminov <coding@dmik.org>
|
|---|
| 7 | #
|
|---|
| 8 | # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 9 | # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 10 | #
|
|---|
| 11 | # Synopsis
|
|---|
| 12 | # --------
|
|---|
| 13 | #
|
|---|
| 14 | # This script performs a build of RPM packages from a given .spec file in
|
|---|
| 15 | # controlled environment to guarantee consistent results when building RPMs
|
|---|
| 16 | # different machines. It uses a separate file rpmbuild-bot-env.sh located
|
|---|
| 17 | # in the same directory to set up the environment and control the build
|
|---|
| 18 | # process. The main purpose of this script is to build RPM packages for a
|
|---|
| 19 | # specific distribution channel maintaining distribution-siecific rules.
|
|---|
| 20 | #
|
|---|
| 21 | # Usage
|
|---|
| 22 | # -----
|
|---|
| 23 | #
|
|---|
| 24 | # > rpmbuild-bot.sh SPEC[=VERSION]
|
|---|
| 25 | # > [ upload[=REPO] | test[=MODE] | clean[=test] | remove[=REPO] ]
|
|---|
| 26 | # > [-f]
|
|---|
| 27 | #
|
|---|
| 28 | # MYAPP is the name of the RPM package spec file (extension is optional,
|
|---|
| 29 | # .spec is assumed). The spec file is searched in the SPECS directory of the
|
|---|
| 30 | # rpmbuild tree (usually $USER/rpmbuild/SPECS, rpmbuild --eval='%_specdir'
|
|---|
| 31 | # will show the exact location). This may be overriden by giving a spec file
|
|---|
| 32 | # with a path specification.
|
|---|
| 33 | #
|
|---|
| 34 | # The second argument defines the command to perform. The default command is
|
|---|
| 35 | # "build". The following sections will describe each command.
|
|---|
| 36 | #
|
|---|
| 37 | # Building packages
|
|---|
| 38 | # -----------------
|
|---|
| 39 | #
|
|---|
| 40 | # The "build" is the main command which is used to generate packages for
|
|---|
| 41 | # distribution. This command does the following:
|
|---|
| 42 | #
|
|---|
| 43 | # 1. Build all RPM packages for all architectures specified in
|
|---|
| 44 | # $RPMBUILD_BOT_ARCH_LIST. The packages are stored in the RPMS
|
|---|
| 45 | # directory of the rpmbuild tree.
|
|---|
| 46 | # 2. Build the source RPM. Stored in the SRPMS directory.
|
|---|
| 47 | # 3. Create a ZIP package from the RPMs for the architecture specified
|
|---|
| 48 | # last in $RPMBUILD_BOT_ARCH_LIST.
|
|---|
| 49 | #
|
|---|
| 50 | # The build process for each architecture is stored in a log file in the logs
|
|---|
| 51 | # directory of the rpmbuild tree (`rpmbuild --eval='%_topdir'/logs`). Creation
|
|---|
| 52 | # of the source RPM and ZIP files is also logged, into separate log files.
|
|---|
| 53 | #
|
|---|
| 54 | # The "build" command will fail if the log directory contains files from a
|
|---|
| 55 | # successfull run of another "build" command for this package. This is to
|
|---|
| 56 | # prevent overwriting successfully built packages that are not yet uploaded to
|
|---|
| 57 | # the distribution repository (see the "upload" command). You should either
|
|---|
| 58 | # run the "upload" command or use the -f option to force removal of these
|
|---|
| 59 | # log files and the corresponding package files if you are absolutely sure they
|
|---|
| 60 | # should be discarded.
|
|---|
| 61 | #
|
|---|
| 62 | # The "build" command will also check if there is a directory named SPEC in the
|
|---|
| 63 | # same directory where the given .spec file resides. If such a directory
|
|---|
| 64 | # exists, all files in it are assumed to be auxiliary source files used by the
|
|---|
| 65 | # .spec file via SourceN: directives. These files will be automatically copied
|
|---|
| 66 | # to the SOURCES directory in the rpmbuild tree befure starting the build
|
|---|
| 67 | # process.
|
|---|
| 68 | #
|
|---|
| 69 | # Doing test builds
|
|---|
| 70 | # -----------------
|
|---|
| 71 | #
|
|---|
| 72 | # The "test" command is used to build packages for one architecture for testing
|
|---|
| 73 | # purposes. In this more, neither the source RPM nor the ZIP file is created.
|
|---|
| 74 | # Also, a special option is automatically passed to rpmbuild to clear the %dist
|
|---|
| 75 | # variable to indicate that this is a local, non-distribution build. Such
|
|---|
| 76 | # packages will always be "older" than the corresponding builds with %dist
|
|---|
| 77 | # so that they will be automatically updated on the next yum update to the
|
|---|
| 78 | # %dist ones. The packages built in "test" mode are NOT intended for
|
|---|
| 79 | # distribution, they are meant only for local testing before performing the
|
|---|
| 80 | # full build of everything.
|
|---|
| 81 | #
|
|---|
| 82 | # It is possible to configure which steps of the build to perform using the MODE
|
|---|
| 83 | # argument to the "test" command which may take one of the following values:
|
|---|
| 84 | #
|
|---|
| 85 | # prep Execute the %prep section of the spec file only.
|
|---|
| 86 | # build Execute the %build section only (requres %prep to be executed
|
|---|
| 87 | # before). May be run multiple times.
|
|---|
| 88 | # install Execute the %install sectuion only (requires "prep" and "build"
|
|---|
| 89 | # to be executed before). May be run multiple times.
|
|---|
| 90 | # pack Create the RPM packages (requires "prep", "build" and "install"
|
|---|
| 91 | # to be executed before). Note that this step will also execute
|
|---|
| 92 | # the %clean section so that a new "install" execution is
|
|---|
| 93 | # necessary for "pack" to succeed.
|
|---|
| 94 | #
|
|---|
| 95 | # When no MODE argument is given, all steps are executed in a proper order.
|
|---|
| 96 | #
|
|---|
| 97 | # The results of the "test" command are stored in a log file in the logs/test
|
|---|
| 98 | # directory of the rpmbuild tree. The previous log file, if any, is given a .bak
|
|---|
| 99 | # extension (the previous .bak file will be deleted).
|
|---|
| 100 | #
|
|---|
| 101 | # The "test" command will copy auxiliary source files for the .spec file, if any,
|
|---|
| 102 | # to the proper location before rpmbuild execution -- the same way the "build"
|
|---|
| 103 | # command does it.
|
|---|
| 104 | #
|
|---|
| 105 | # Uploading packages
|
|---|
| 106 | # ------------------
|
|---|
| 107 | #
|
|---|
| 108 | # The "upload" command will upload the packages built with the "build"
|
|---|
| 109 | # command to the official distribution channel configured in
|
|---|
| 110 | # rpmbuild-bot-env.sh. The REPO argument specifies one of the configured
|
|---|
| 111 | # repositories. When REPO is not given, the default repository is used
|
|---|
| 112 | # (usually experimental or similar).
|
|---|
| 113 | #
|
|---|
| 114 | # Note that the "upload" command needs log files from the "build" command
|
|---|
| 115 | # and will fail otherwise.
|
|---|
| 116 | #
|
|---|
| 117 | # Upon successful completion, the "upload" command will remove all uploaded
|
|---|
| 118 | # RPM and ZIP packages and will move all "build" log files to logs/archive.
|
|---|
| 119 | #
|
|---|
| 120 | # Cleaning packages
|
|---|
| 121 | # -----------------
|
|---|
| 122 | #
|
|---|
| 123 | # The "clean" command will delete packages built with the "build" command
|
|---|
| 124 | # and their log files without uploading them to a repository. This is useful
|
|---|
| 125 | # when the successful build needs to be canceled for some reason (wrong source
|
|---|
| 126 | # tree state, wrong patches etc.). Note that normally you don't need to use
|
|---|
| 127 | # this command; it's an emergency-only tool.
|
|---|
| 128 | #
|
|---|
| 129 | # The "clean" command needs log files from the "build" command and will fail
|
|---|
| 130 | # otherwise.
|
|---|
| 131 | #
|
|---|
| 132 | # If the "clean" command is given a "test" argument, it will clean up the
|
|---|
| 133 | # results of the "test" command instead of "build". The log file from the
|
|---|
| 134 | # "test" command needs to be present or the command will fail.
|
|---|
| 135 | #
|
|---|
| 136 | # Removing packages
|
|---|
| 137 | # -----------------
|
|---|
| 138 | #
|
|---|
| 139 | # The "remove" command allows to remove a particular version of the packages
|
|---|
| 140 | # built with the "build" command and uploaded with the "upload" command from a
|
|---|
| 141 | # repository. This is useful when the successful build needs to be canceled for
|
|---|
| 142 | # some reason (wrong source tree state, wrong patches etc.). Note that normally
|
|---|
| 143 | # you don't need to use this command; it's an emergency-only tool.
|
|---|
| 144 | #
|
|---|
| 145 | # The "remove" command needs log files from the "build" and "upload" commands
|
|---|
| 146 | # and will fail otherwise. It also requires the VERSION argument for the SPEC
|
|---|
| 147 | # parameter to be given (to specify the version of the packages to remove) and
|
|---|
| 148 | # accepts the REPO argument just like the "upload" command does (to specify a
|
|---|
| 149 | # repository to remove the packages from).
|
|---|
| 150 | #
|
|---|
| 151 | # Note that the log files from the "build" and "upload" commands are also removed
|
|---|
| 152 | # by this command upon sucessful package removal.
|
|---|
| 153 | #
|
|---|
| 154 | # Return value
|
|---|
| 155 | # ------------
|
|---|
| 156 | #
|
|---|
| 157 | # The rpmbuild-bot.sh script will return a zero exit code upon successful
|
|---|
| 158 | # completion and non-zero otherwise. The script output and log files should be
|
|---|
| 159 | # inspected to check for a reason of the failure.
|
|---|
| 160 | #
|
|---|
| 161 |
|
|---|
| 162 | #
|
|---|
| 163 | # Helpers.
|
|---|
| 164 | #
|
|---|
| 165 |
|
|---|
| 166 | run()
|
|---|
| 167 | {
|
|---|
| 168 | "$@"
|
|---|
| 169 | local rc=$?
|
|---|
| 170 | if test $rc != 0 ; then
|
|---|
| 171 | echo "ERROR: The following command failed with error code $rc:"
|
|---|
| 172 | echo $@
|
|---|
| 173 | exit $rc
|
|---|
| 174 | fi
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | log_run()
|
|---|
| 178 | {
|
|---|
| 179 | log="$1"
|
|---|
| 180 | shift
|
|---|
| 181 | "$@" >"$log" 2>&1
|
|---|
| 182 | local rc=$?
|
|---|
| 183 | if test $rc != 0 ; then
|
|---|
| 184 | echo "ERROR: The following command failed with error code $rc:"
|
|---|
| 185 | echo $@
|
|---|
| 186 | echo "You will find more information in file '$log'."
|
|---|
| 187 | echo "Here are the last 10 lines of output:"
|
|---|
| 188 | echo ""
|
|---|
| 189 | tail "$log" -n 10
|
|---|
| 190 | exit $rc
|
|---|
| 191 | fi
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | warn()
|
|---|
| 195 | {
|
|---|
| 196 | echo "WARNING: $1"
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | die()
|
|---|
| 200 | {
|
|---|
| 201 | echo "ERROR: $1"
|
|---|
| 202 | exit 1
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | check_dir_var()
|
|---|
| 206 | {
|
|---|
| 207 | eval local val="\$$1"
|
|---|
| 208 | [ -n "$val" ] || die "$1 is empty."
|
|---|
| 209 | [ -d "$val" ] || die "$1 is '$val', not a valid directory."
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | read_file_list()
|
|---|
| 213 | {
|
|---|
| 214 | # $1 = file list list filename
|
|---|
| 215 | # $2 = var name where to save read file names (optional)
|
|---|
| 216 | # $3 = function name to call for each file (optional)
|
|---|
| 217 |
|
|---|
| 218 | local list="$1"
|
|---|
| 219 | local _read_file_list_ret=
|
|---|
| 220 |
|
|---|
| 221 | # Check timestamps.
|
|---|
| 222 | while read l; do
|
|---|
| 223 | local file="${l#*|}"
|
|---|
| 224 | local ts="${l%%|*}"
|
|---|
| 225 | [ "$file" = "$ts" ] && die "Line '$l' in '$list' does not contain timestamps."
|
|---|
| 226 | [ -n "$3" ] && eval $3
|
|---|
| 227 | [ -f "$file" ] || die "File '$file' is not found."
|
|---|
| 228 | echo "Checking tmestamp of $file..."
|
|---|
| 229 | local act_ts=`stat -c '%Y' "$file"`
|
|---|
| 230 | if [ "$ts" != "$act_ts" ] ; then
|
|---|
| 231 | die "Recorded timestamp $ts doesn't match actual timestamp $act_ts for '$file'."
|
|---|
| 232 | fi
|
|---|
| 233 | _read_file_list_ret="$_read_file_list_ret${_read_file_list_ret:+ }$file"
|
|---|
| 234 | done < "$list"
|
|---|
| 235 | # Return the files (if requested).
|
|---|
| 236 | [ -n "$2" ] && eval "$2=\$_read_file_list_ret"
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | usage()
|
|---|
| 240 | {
|
|---|
| 241 | echo "Usage:"
|
|---|
| 242 | sed -n -e "s/rpmbuild-bot.sh/${0##*/}/g" -e 's/^# > / /p' < "$0"
|
|---|
| 243 | exit 255
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | sync_aux_src()
|
|---|
| 247 | {
|
|---|
| 248 | [ -n "$src_dir" ] || die "src_dir is empty."
|
|---|
| 249 | [ -n "$spec_full" ] || die "spec_full is empty."
|
|---|
| 250 |
|
|---|
| 251 | # Aux source dir is expected along the .spec file.
|
|---|
| 252 | local aux_src_dir="${spec_full%.spec}"
|
|---|
| 253 |
|
|---|
| 254 | # Return early if there is no aux source dir.
|
|---|
| 255 | [ -d "$aux_src_dir" ] || return
|
|---|
| 256 |
|
|---|
| 257 | echo "Copying auxiliary sources for '$spec' to $src_dir..."
|
|---|
| 258 |
|
|---|
| 259 | for f in "$aux_src_dir"/* ; do
|
|---|
| 260 | local ts=`stat -c '%Y' "$f"`
|
|---|
| 261 | local trg_ts=
|
|---|
| 262 | local trg_f="$src_dir/${f##*/}"
|
|---|
| 263 | [ -f "$trg_f" ] && trg_ts=`stat -c '%Y' "$trg_f"`
|
|---|
| 264 | if [ "$ts" != "$trg_ts" ] ; then
|
|---|
| 265 | echo "Copying $f..."
|
|---|
| 266 | run cp -p "$f" "$trg_f"
|
|---|
| 267 | fi
|
|---|
| 268 | done
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | build_cmd()
|
|---|
| 272 | {
|
|---|
| 273 | local base_arch="${RPMBUILD_BOT_ARCH_LIST##* }"
|
|---|
| 274 |
|
|---|
| 275 | echo "Spec file: $spec_full"
|
|---|
| 276 | echo "Targets: $RPMBUILD_BOT_ARCH_LIST + SRPM + ZIP ($base_arch)"
|
|---|
| 277 |
|
|---|
| 278 | sync_aux_src
|
|---|
| 279 |
|
|---|
| 280 | if [ -f "$spec_list" ] ; then
|
|---|
| 281 | if [ -z "$force" ] ; then
|
|---|
| 282 | die "File '$spec_list' already exists.
|
|---|
| 283 | This file indicates a successful build that was not yet uploaded.
|
|---|
| 284 | Either run the '"'upload'"' command to upload the generated RPM and ZIP
|
|---|
| 285 | packages to the distribution repository or use the -f option to
|
|---|
| 286 | force their removal if you are sure they should be discarded."
|
|---|
| 287 | fi
|
|---|
| 288 |
|
|---|
| 289 | echo "Detected successful build in $spec_list, cleaning up due to -f option..."
|
|---|
| 290 | local files=
|
|---|
| 291 | read_file_list "$spec_list" files
|
|---|
| 292 | for f in $files; do
|
|---|
| 293 | echo "Removing $f..."
|
|---|
| 294 | run rm -f "$f"
|
|---|
| 295 | done
|
|---|
| 296 | unset files
|
|---|
| 297 |
|
|---|
| 298 | echo "Removing $log_base.*.log and .list files..."
|
|---|
| 299 | rm -f "$log_base".*.log "$log_base".*.list "$log_base".list
|
|---|
| 300 | fi
|
|---|
| 301 |
|
|---|
| 302 | # Generate RPMs.
|
|---|
| 303 | for arch in $RPMBUILD_BOT_ARCH_LIST ; do
|
|---|
| 304 | echo "Creating RPMs for '$arch' target (logging to $log_base.$arch.log)..."
|
|---|
| 305 | log_run "$log_base.$arch.log" rpmbuild.exe --target=$arch -bb "$spec_full"
|
|---|
| 306 | done
|
|---|
| 307 |
|
|---|
| 308 | # Generate SRPM.
|
|---|
| 309 | echo "Creating SRPM (logging to $log_base.srpm.log)..."
|
|---|
| 310 | log_run "$log_base.srpm.log" rpmbuild -bs "$spec_full"
|
|---|
| 311 |
|
|---|
| 312 | # Find SRPM file name in the log.
|
|---|
| 313 | local src_rpm=`grep "^Wrote: \+.*\.src\.rpm$" "$log_base.srpm.log" | sed -e "s#^Wrote: \+##g"`
|
|---|
| 314 | [ -n "$src_rpm" ] || die "Cannot find .src.rpm file name in '$log_base.srpm.log'."
|
|---|
| 315 |
|
|---|
| 316 | # Find package version.
|
|---|
| 317 | local ver_full="${src_rpm%.src.rpm}"
|
|---|
| 318 | ver_full="${ver_full##*/}"
|
|---|
| 319 | [ "${ver_full%%-[0-9]*}" = "$spec_name" ] || die \
|
|---|
| 320 | "SRPM name '${src_rpm##*/}' does not match .spec name ('$spec_name').
|
|---|
| 321 | Either rename '$spec_name.spec' to '${ver_full%%-[0-9]*}.spec' or set 'Name:' tag to '$spec_name'."
|
|---|
| 322 | ver_full="${ver_full#${spec_name}-}"
|
|---|
| 323 | [ -n "$ver_full" ] || die "Cannot deduce package version from '$src_rpm'."
|
|---|
| 324 |
|
|---|
| 325 | # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
|
|---|
| 326 | local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_base.$base_arch.log" | sed -e "s#^Wrote: \+##g"`"
|
|---|
| 327 | [ -n "$rpms" ] || die "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_base.base_arch.log'."
|
|---|
| 328 |
|
|---|
| 329 | local ver_full_zip=`echo $ver_full | tr . _`
|
|---|
| 330 | local zip="$zip_dir/$spec_name-$ver_full_zip.zip"
|
|---|
| 331 |
|
|---|
| 332 | # Generate ZIP.
|
|---|
| 333 | echo "Creating ZIP (logging to $log_base.zip.log)..."
|
|---|
| 334 | create_zip()
|
|---|
| 335 | {(
|
|---|
| 336 | run cd "$zip_dir"
|
|---|
| 337 | rm -r "@unixroot" 2> /dev/null
|
|---|
| 338 | # Note no quoters around $rpms - it's to split at EOL.
|
|---|
| 339 | for f in $rpms ; do
|
|---|
| 340 | echo "Unpacking $f..."
|
|---|
| 341 | run rpm2cpio "$f" | cpio -idm
|
|---|
| 342 | done
|
|---|
| 343 | rm -f "$zip" 2> /dev/null
|
|---|
| 344 | echo "Creating '$zip'..."
|
|---|
| 345 | run zip -mry9 "$zip" "@unixroot"
|
|---|
| 346 | )}
|
|---|
| 347 | log_run "$log_base.zip.log" create_zip
|
|---|
| 348 |
|
|---|
| 349 | local ver_list="$log_base.$ver_full.list"
|
|---|
| 350 |
|
|---|
| 351 | # Generate list of all generated packages for further reference.
|
|---|
| 352 | echo "Creating list file ($ver_list)..."
|
|---|
| 353 | echo `stat -c '%Y' "$src_rpm"`"|$src_rpm" > "$ver_list"
|
|---|
| 354 | echo `stat -c '%Y' "$zip"`"|$zip" >> "$ver_list"
|
|---|
| 355 | # Save base arch RPMs.
|
|---|
| 356 | for f in $rpms ; do
|
|---|
| 357 | echo `stat -c '%Y' "$f"`"|$f" >> "$ver_list"
|
|---|
| 358 | done
|
|---|
| 359 | # Save other arch RPMs.
|
|---|
| 360 | for arch in ${RPMBUILD_BOT_ARCH_LIST%${base_arch}} ; do
|
|---|
| 361 | rpms="`grep "^Wrote: \+.*\.$arch\.rpm$" "$log_base.$arch.log" | sed -e "s#^Wrote: \+##g"`"
|
|---|
| 362 | [ -n "$rpms" ] || die "Cannot find .$arch.rpm file names in '$log_base.arch.log'."
|
|---|
| 363 | for f in $rpms ; do
|
|---|
| 364 | echo `stat -c '%Y' "$f"`"|$f" >> "$ver_list"
|
|---|
| 365 | done
|
|---|
| 366 | done
|
|---|
| 367 |
|
|---|
| 368 | # Everything succeeded. Symlink the list file so that "upload" can find it.
|
|---|
| 369 | run ln -s "${ver_list##*/}" "$log_base.list"
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | test_cmd()
|
|---|
| 373 | {
|
|---|
| 374 | echo "Spec file: $spec_full"
|
|---|
| 375 |
|
|---|
| 376 | sync_aux_src
|
|---|
| 377 |
|
|---|
| 378 | local base_arch="${RPMBUILD_BOT_ARCH_LIST##* }"
|
|---|
| 379 | local cmds=
|
|---|
| 380 |
|
|---|
| 381 | [ -z "$command_arg" ] && command_arg="all"
|
|---|
| 382 |
|
|---|
| 383 | case "$command_arg" in
|
|---|
| 384 | all)
|
|---|
| 385 | cmds="$cmds -bb"
|
|---|
| 386 | ;;
|
|---|
| 387 | prep)
|
|---|
| 388 | cmds="$cmds -bp --short-circuit"
|
|---|
| 389 | ;;
|
|---|
| 390 | build)
|
|---|
| 391 | cmds="$cmds -bc --short-circuit"
|
|---|
| 392 | ;;
|
|---|
| 393 | install)
|
|---|
| 394 | cmds="$cmds -bi --short-circuit"
|
|---|
| 395 | ;;
|
|---|
| 396 | pack)
|
|---|
| 397 | cmds="$cmds -bb --short-circuit"
|
|---|
| 398 | ;;
|
|---|
| 399 | *)
|
|---|
| 400 | die "Invalid test build sub-command '$command_arg'."
|
|---|
| 401 | ;;
|
|---|
| 402 | esac
|
|---|
| 403 |
|
|---|
| 404 | local log_file="$log_dir/test/$spec_name.log"
|
|---|
| 405 |
|
|---|
| 406 | if [ -f "$log_file" ] ; then
|
|---|
| 407 | rm -f "$log_file.bak"
|
|---|
| 408 | run mv "$log_file" "$log_file.bak"
|
|---|
| 409 | fi
|
|---|
| 410 |
|
|---|
| 411 | echo "Doing test RPM build for '$base_arch' target (logging to $log_file)..."
|
|---|
| 412 | log_run "$log_file" rpmbuild.exe "--define=dist %nil" --target=$base_arch $cmds $spec_full
|
|---|
| 413 |
|
|---|
| 414 | # Show the generated RPMs when appropriate.
|
|---|
| 415 | if [ "$command_arg" = "all" -o "$command_arg" = "pack" ] ; then
|
|---|
| 416 | # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
|
|---|
| 417 | local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_file" | sed -e "s#^Wrote: \+##g"`"
|
|---|
| 418 | if [ -n "$rpms" ] ; then
|
|---|
| 419 | echo "Successfully generated the following RPMs:"
|
|---|
| 420 | for f in $rpms; do
|
|---|
| 421 | echo "$f"
|
|---|
| 422 | done
|
|---|
| 423 | else
|
|---|
| 424 | warn "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_file'."
|
|---|
| 425 | fi
|
|---|
| 426 | fi
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | repo_dir_for_file()
|
|---|
| 430 | {
|
|---|
| 431 | # $1 = input file name
|
|---|
| 432 | # $2 = var name to save dir to
|
|---|
| 433 |
|
|---|
| 434 | [ -n "$1" -a -n "$2" ] || die "Invalid arguments."
|
|---|
| 435 |
|
|---|
| 436 | local _repo_dir_for_file_ret=
|
|---|
| 437 | case "$1" in
|
|---|
| 438 | *.src.rpm)
|
|---|
| 439 | eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_srpm"
|
|---|
| 440 | ;;
|
|---|
| 441 | *.*.rpm)
|
|---|
| 442 | local arch="${1%.rpm}"
|
|---|
| 443 | arch="${arch##*.}"
|
|---|
| 444 | [ -n "$arch" ] || die "No arch spec in file name '$1'."
|
|---|
| 445 | eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_rpm"
|
|---|
| 446 | ;;
|
|---|
| 447 | *.zip)
|
|---|
| 448 | eval _repo_dir_for_file_ret="$RPMBUILD_BOT_UPLOAD_REPO_LAYOUT_zip"
|
|---|
| 449 | ;;
|
|---|
| 450 | esac
|
|---|
| 451 |
|
|---|
| 452 | eval "$2=\$_repo_dir_for_file_ret"
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | upload_cmd()
|
|---|
| 456 | {
|
|---|
| 457 | # Check settings.
|
|---|
| 458 | test -n "$RPMBUILD_BOT_UPLOAD_REPO_LIST" || die "RPMBUILD_BOT_UPLOAD_REPO_LIST is empty."
|
|---|
| 459 |
|
|---|
| 460 | local repo="$command_arg"
|
|---|
| 461 | [ -z "$repo" ] && repo="${RPMBUILD_BOT_UPLOAD_REPO_LIST%% *}"
|
|---|
| 462 |
|
|---|
| 463 | check_dir_var "RPMBUILD_BOT_UPLOAD_${repo}_DIR"
|
|---|
| 464 |
|
|---|
| 465 | eval local base="\$RPMBUILD_BOT_UPLOAD_${repo}_DIR"
|
|---|
| 466 |
|
|---|
| 467 | [ -f "$spec_list" ] || die \
|
|---|
| 468 | "File '$spec_list' is not found.
|
|---|
| 469 | You may need to build the packages using the 'build' command."
|
|---|
| 470 |
|
|---|
| 471 | local files=
|
|---|
| 472 | read_file_list "$spec_list" files
|
|---|
| 473 | for f in $files; do
|
|---|
| 474 | local d=
|
|---|
| 475 | repo_dir_for_file "$f" d
|
|---|
| 476 | [ -n "$d" ] || die "Unsupported file name '$f' in '$spec_list'."
|
|---|
| 477 | [ -d "$d" ] || die "'$d' is not a directory."
|
|---|
| 478 | [ -f "$d/${f##*/}" -a -z "$force" ] && die \
|
|---|
| 479 | "File '$d/${f##*/}' already exists.
|
|---|
| 480 | Use the -f option to force uploading if you are sure the existing
|
|---|
| 481 | packages in the repository should be discarded."
|
|---|
| 482 | echo "Copying $f to $d..."
|
|---|
| 483 | run cp -p "$f" "$d"
|
|---|
| 484 | done
|
|---|
| 485 |
|
|---|
| 486 | # On success, delete the uploaded packages and archive log files.
|
|---|
| 487 | for f in $files; do
|
|---|
| 488 | echo "Removing $f..."
|
|---|
| 489 | run rm -f "$f"
|
|---|
| 490 | done
|
|---|
| 491 |
|
|---|
| 492 | # Note: versioned .list file will remain in archive forever (for further reference).
|
|---|
| 493 | echo "Removing old '$spec_name' logs from $log_dir/archive..."
|
|---|
| 494 | rm -f "$log_dir/archive/$spec_name".*.log "$log_dir/archive/$spec_name".list
|
|---|
| 495 | echo "Moving '$spec_name' logs to $log_dir/archive..."
|
|---|
| 496 | run mv "$log_base".*.log "$log_base".*.list "$log_base".list "$log_dir/archive/"
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | clean_cmd()
|
|---|
| 500 | {
|
|---|
| 501 | if [ "$command_arg" = "test" ] ; then
|
|---|
| 502 | # Cleanup after "test" command.
|
|---|
| 503 | local base_arch="${RPMBUILD_BOT_ARCH_LIST##* }"
|
|---|
| 504 | local log_file="$log_dir/test/$spec_name.log"
|
|---|
| 505 |
|
|---|
| 506 | [ -f "$log_file" ] || die "File '$test_log' is not found."
|
|---|
| 507 |
|
|---|
| 508 | # Find all RPM packages for the base arch (note the quotes around `` - it's to preserve multi-line result).
|
|---|
| 509 | local rpms="`grep "^Wrote: \+.*\.\($base_arch\.rpm\|noarch\.rpm\)$" "$log_file" | sed -e "s#^Wrote: \+##g"`"
|
|---|
| 510 | if [ -n "$rpms" ] ; then
|
|---|
| 511 | for f in $rpms; do
|
|---|
| 512 | echo "Removing $f..."
|
|---|
| 513 | run rm -f "$f"
|
|---|
| 514 | done
|
|---|
| 515 | echo "Removing $log_file[.bak]..."
|
|---|
| 516 | run rm -f "$log_file" "$log_file".bak
|
|---|
| 517 | else
|
|---|
| 518 | die "Cannot find .$base_arch.rpm/.noarch.rpm file names in '$log_file'."
|
|---|
| 519 | fi
|
|---|
| 520 |
|
|---|
| 521 | return
|
|---|
| 522 | fi
|
|---|
| 523 |
|
|---|
| 524 | # Cleanup after "build command".
|
|---|
| 525 | [ -f "$spec_list" ] || die \
|
|---|
| 526 | "File '$spec_list' is not found.
|
|---|
| 527 | You man need to build the packages using the 'build' command."
|
|---|
| 528 |
|
|---|
| 529 | local files=
|
|---|
| 530 | read_file_list "$spec_list" files
|
|---|
| 531 |
|
|---|
| 532 | for f in $files; do
|
|---|
| 533 | echo "Removing $f..."
|
|---|
| 534 | run rm -f "$f"
|
|---|
| 535 | done
|
|---|
| 536 |
|
|---|
| 537 | echo "Removing '$spec_name' logs from $log_dir..."
|
|---|
| 538 | rm -f "$log_base".*.log "$log_base".*.list "$log_base".list
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | remove_cmd()
|
|---|
| 542 | {
|
|---|
| 543 | # Check settings.
|
|---|
| 544 | [ -n "$spec_ver" ] || die "SPEC parameter lacks version specification."
|
|---|
| 545 |
|
|---|
| 546 | test -n "$RPMBUILD_BOT_UPLOAD_REPO_LIST" || die "RPMBUILD_BOT_UPLOAD_REPO_LIST is empty."
|
|---|
| 547 |
|
|---|
| 548 | local repo="$command_arg"
|
|---|
| 549 | [ -z "$repo" ] && repo="${RPMBUILD_BOT_UPLOAD_REPO_LIST%% *}"
|
|---|
| 550 |
|
|---|
| 551 | check_dir_var "RPMBUILD_BOT_UPLOAD_${repo}_DIR"
|
|---|
| 552 |
|
|---|
| 553 | eval local base="\$RPMBUILD_BOT_UPLOAD_${repo}_DIR"
|
|---|
| 554 |
|
|---|
| 555 | local ver_list="$log_dir/archive/$spec_name.$spec_ver.list"
|
|---|
| 556 | [ -f "$ver_list" ] || die "File '$ver_list' is not found."
|
|---|
| 557 |
|
|---|
| 558 | local files=
|
|---|
| 559 | read_file_list "$ver_list" files 'local dir=; repo_dir_for_file $file dir; file="${dir}/${file##*/}"'
|
|---|
| 560 |
|
|---|
| 561 | for f in $files; do
|
|---|
| 562 | echo "Removing $f..."
|
|---|
| 563 | run rm -f "$f"
|
|---|
| 564 | done
|
|---|
| 565 |
|
|---|
| 566 | echo "Removing $ver_list..."
|
|---|
| 567 | run rm -f "$ver_list"
|
|---|
| 568 |
|
|---|
| 569 | # Also remove the logs of last "build" if we are removing the last "build" package.
|
|---|
| 570 | if [ -L "$log_dir/archive/$spec_name.list" -a \
|
|---|
| 571 | `readlink "$log_dir/archive/$spec_name.list"` = "$spec_name.$spec_ver.list" ] ; then
|
|---|
| 572 | echo "Removing '$spec_name' logs from $log_dir/archive..."
|
|---|
| 573 | rm -f "$log_dir/archive/$spec_name".*.log "$log_dir/archive/$spec_name".list
|
|---|
| 574 | fi
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | #
|
|---|
| 578 | # Main.
|
|---|
| 579 | #
|
|---|
| 580 |
|
|---|
| 581 | # Parse command line.
|
|---|
| 582 | while [ -n "$1" ] ; do
|
|---|
| 583 | case "$1" in
|
|---|
| 584 | -*)
|
|---|
| 585 | options="$*"
|
|---|
| 586 | while [ -n "$1" ] ; do
|
|---|
| 587 | case "$1" in
|
|---|
| 588 | -f) force="-f"
|
|---|
| 589 | ;;
|
|---|
| 590 | *) usage
|
|---|
| 591 | ;;
|
|---|
| 592 | esac
|
|---|
| 593 | shift
|
|---|
| 594 | done
|
|---|
| 595 | break
|
|---|
| 596 | ;;
|
|---|
| 597 | *)
|
|---|
| 598 | if [ -z "$spec" ] ; then
|
|---|
| 599 | spec="$1"
|
|---|
| 600 | else
|
|---|
| 601 | command="$1"
|
|---|
| 602 | fi
|
|---|
| 603 | ;;
|
|---|
| 604 | esac
|
|---|
| 605 | shift
|
|---|
| 606 | done
|
|---|
| 607 |
|
|---|
| 608 | [ -n "$spec" ] || usage
|
|---|
| 609 | [ -z "$command" ] && command="build"
|
|---|
| 610 |
|
|---|
| 611 | spec_ver="${spec#*=}"
|
|---|
| 612 | spec="${spec%=*}"
|
|---|
| 613 | [ "$spec" = "$spec_ver" ] && spec_ver=
|
|---|
| 614 |
|
|---|
| 615 | command_name="${command%=*}"
|
|---|
| 616 | command_arg="${command#*=}"
|
|---|
| 617 | [ "$command_name" = "$command_arg" ] && command_arg=
|
|---|
| 618 |
|
|---|
| 619 | need_spec_file=
|
|---|
| 620 |
|
|---|
| 621 | # Validate commands.
|
|---|
| 622 | case "$command_name" in
|
|---|
| 623 | build|test)
|
|---|
| 624 | need_spec_file=1
|
|---|
| 625 | ;;
|
|---|
| 626 | upload|clean|remove)
|
|---|
| 627 | ;;
|
|---|
| 628 | *) usage
|
|---|
| 629 | ;;
|
|---|
| 630 | esac
|
|---|
| 631 |
|
|---|
| 632 | # Query all rpmbuild macros in a single run as it may be slow.
|
|---|
| 633 | eval `rpmbuild.exe --eval='rpmbuild_dir="%_topdir" ; spec_dir="%_specdir" ; src_dir="%_sourcedir"' | tr '\\\' /`
|
|---|
| 634 |
|
|---|
| 635 | [ -n "$rpmbuild_dir" -a -d "$rpmbuild_dir" ] || die "Falied to get %_topdir from rpmbuild or not directory ($rpmbuild_dir)."
|
|---|
| 636 | [ -n "$spec_dir" -a -d "$spec_dir" ] || die "Falied to get %_specdir from rpmbuild or not directory ($spec_dir)."
|
|---|
| 637 | [ -n "$src_dir" -a -d "$src_dir" ] || die "Falied to get %_sourcedir from rpmbuild or not directory ($src_dir)."
|
|---|
| 638 |
|
|---|
| 639 | log_dir="$rpmbuild_dir/logs"
|
|---|
| 640 | zip_dir="$rpmbuild_dir/zip"
|
|---|
| 641 |
|
|---|
| 642 | spec=`echo $spec | tr '\\\' /`
|
|---|
| 643 |
|
|---|
| 644 | spec_name="${spec##*/}"
|
|---|
| 645 |
|
|---|
| 646 | if [ "$spec_name" = "$spec" ] ; then
|
|---|
| 647 | # No path information, use SPECS
|
|---|
| 648 | spec_full="$spec_dir/${spec_name%.spec}.spec"
|
|---|
| 649 | else
|
|---|
| 650 | # Has some path, use it.
|
|---|
| 651 | spec_full="${spec%.spec}.spec"
|
|---|
| 652 | fi
|
|---|
| 653 |
|
|---|
| 654 | spec_name="${spec_name%.spec}"
|
|---|
| 655 |
|
|---|
| 656 | [ -z "$need_spec_file" -o -f "$spec_full" ] || die "Spec file '$spec_full' is not found."
|
|---|
| 657 |
|
|---|
| 658 | # Prepare some (non-rpmbuild-standard) directories.
|
|---|
| 659 | run mkdir -p "$log_dir"
|
|---|
| 660 | run mkdir -p "$log_dir/archive"
|
|---|
| 661 | run mkdir -p "$log_dir/test"
|
|---|
| 662 | run mkdir -p "$zip_dir"
|
|---|
| 663 |
|
|---|
| 664 | [ -z "$command" ] && command="build"
|
|---|
| 665 |
|
|---|
| 666 | command_name="${command%=*}"
|
|---|
| 667 | comand_arg="${command#*=}"
|
|---|
| 668 | [ "$command_name" = "$command_arg" ] && command_arg=
|
|---|
| 669 |
|
|---|
| 670 | log_base="$log_dir/$spec_name"
|
|---|
| 671 | spec_list="$log_base.list"
|
|---|
| 672 |
|
|---|
| 673 | echo "Package: $spec_name"
|
|---|
| 674 | echo "Command: $command $options"
|
|---|
| 675 |
|
|---|
| 676 | # Set up the rpmbuild-bot environment.
|
|---|
| 677 | . "${0%%.sh}-env.sh"
|
|---|
| 678 |
|
|---|
| 679 | # Check common settings.
|
|---|
| 680 | test -n "$RPMBUILD_BOT_ARCH_LIST" || die "RPMBUILD_BOT_ARCH_LIST is empty."
|
|---|
| 681 |
|
|---|
| 682 | run eval "${command_name}_cmd"
|
|---|
| 683 |
|
|---|
| 684 | echo "All done."
|
|---|
| 685 |
|
|---|
| 686 | exit 0
|
|---|