|
1 | | -#!/bin/bash |
2 | | - |
3 | 1 | # © Copyright IBM Corporation 2019 |
4 | 2 | # |
5 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|
14 | 12 | # See the License for the specific language governing permissions and |
15 | 13 | # limitations under the License. |
16 | 14 |
|
17 | | -# Script to build the monitor agent programs from within a Docker container |
18 | | - |
19 | | -export PATH="${PATH}:/usr/lib/go-${GOVERSION}/bin:/go/bin" |
20 | | -export CGO_CFLAGS="-I/opt/mqm/inc/" |
21 | | -export CGO_LDFLAGS_ALLOW="-Wl,-rpath.*" |
| 15 | +# This simple script builds a Docker container whose purpose is simply |
| 16 | +# to compile the binary components of the monitoring programs, and then to copy those |
| 17 | +# programs to a local temporary directory. |
| 18 | +GOPATH="/go" |
22 | 19 |
|
23 | | -# Which monitor programs are to be built. By default, build the complete set available. |
24 | | -# It can be overridden by setting the value on the "docker run" command with |
25 | | -# a "-e MONITORS=..." flag. |
26 | | -if [ -z "$MONITORS" ] |
| 20 | +TAG="mq-metric-samples-gobuild" |
| 21 | +# Assume repo tags have been created in a sensible order |
| 22 | +VER=`git tag -l | sort | tail -1 | sed "s/^v//g"` |
| 23 | +if [ -z "$VER" ] |
27 | 24 | then |
28 | | - cd $GOPATH/src/$ORG/$REPO |
29 | | - MONITORS=`ls cmd` |
| 25 | + VER="latest" |
30 | 26 | fi |
| 27 | +echo "Building container $TAG:$VER" |
| 28 | + |
| 29 | +# Build a container that has all the pieces needed to compile the Go programs for MQ |
| 30 | +docker build --build-arg GOPATH_ARG=$GOPATH -t $TAG:$VER . |
| 31 | +rc=$? |
31 | 32 |
|
32 | | -# And do the builds into the bin directory |
33 | | -cd $GOPATH |
34 | | -for m in $MONITORS |
35 | | -do |
36 | | - srcdir=src/$ORG/$REPO/cmd/$m |
37 | | - |
38 | | - echo "Building $m" |
39 | | - if [ ! -z "$BUILD_EXTRA_INJECT" ] |
40 | | - then |
41 | | - BUILD_EXTRA_LDFLAGS="-ldflags" |
42 | | - fi |
43 | | - |
44 | | - go build -o bin/$m $BUILD_EXTRA_LDFLAGS "$BUILD_EXTRA_INJECT" $srcdir/*.go |
45 | | - |
46 | | - # Copy the supporting scripts into the output directory |
47 | | - if [ -r $srcdir/$m.sh ] |
48 | | - then |
49 | | - cp $srcdir/*.sh bin |
50 | | - chmod a+rx bin/*.sh |
51 | | - fi |
52 | | - if [ -r $srcdir/$m.mqsc ] |
53 | | - then |
54 | | - cp $srcdir/*.mqsc bin |
55 | | - fi |
56 | | -done |
| 33 | +# Set the userid we will run the container as |
| 34 | +uid=`id -u` |
| 35 | +gid=`id -g` |
| 36 | + |
| 37 | +if [ $rc -eq 0 ] |
| 38 | +then |
| 39 | + # Run the image to do the compilation and extract the files |
| 40 | + # from it into a local directory mounted into the container. |
| 41 | + OUTDIR=$HOME/tmp/mq-metric-samples/bin |
| 42 | + rm -rf $OUTDIR |
| 43 | + mkdir -p $OUTDIR |
| 44 | + |
| 45 | + # Get some variables to pass the build information into the compile steps |
| 46 | + buildStamp=`date +%Y%m%d-%H%M%S` |
| 47 | + gitCommit=`git rev-list -1 HEAD --abbrev-commit` |
| 48 | + |
| 49 | + # Set this for any special status |
| 50 | + extraInfo="" |
| 51 | + |
| 52 | + # Add "-e MONITORS=..." to only compile a subset of the monitor programs |
| 53 | + # Mount an output directory |
| 54 | + # Delete the container once it's done its job |
| 55 | + docker run --rm \ |
| 56 | + --user $uid:$gid \ |
| 57 | + -v $OUTDIR:$GOPATH/bin \ |
| 58 | + -e BUILD_EXTRA_INJECT="-X \"main.BuildStamp=$buildStamp $extraInfo\" -X \"main.GitCommit=$gitCommit\"" \ |
| 59 | + $TAG:$VER |
| 60 | + echo "Compiled programs should now be in $OUTDIR" |
| 61 | +fi |
0 commit comments