Skip to content

Commit c972b14

Browse files
committed
Improve permissions on build-in-docker scripts
1 parent 57f9ff1 commit c972b14

File tree

5 files changed

+107
-99
lines changed

5 files changed

+107
-99
lines changed

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ RUN cd /opt/mqm \
6262
&& rm -f ./*.tar.gz
6363

6464
# Insert the script that will do the build
65-
COPY buildMonitors.sh $GOPATH
66-
RUN chmod 777 $GOPATH/buildMonitors.sh
65+
COPY buildInDocker.sh $GOPATH
66+
RUN chmod 777 $GOPATH/buildInDocker.sh
6767

68-
# Copy the rest of the source tree from this directory into the container
68+
# Copy the rest of the source tree from this directory into the container and
69+
# make sure it's readable by the user running the container
6970
COPY . $GOPATH/src/$ORG/$REPO
71+
RUN chmod -R a+rx $GOPATH/src/$ORG/$REPO
7072

7173
# Set the entrypoint to the script that will do the compilation
72-
ENTRYPOINT $GOPATH/buildMonitors.sh
74+
ENTRYPOINT $GOPATH/buildInDocker.sh

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ monitor agent directory also has sample scripts, configuration files etc to help
5353
with getting the agent running in your specific environment.
5454

5555
## Using a Docker container to build the programs
56-
You can use the `buildMain.sh` script in this directory to build a Docker container that
56+
You can use the `buildMonitors.sh` script in this directory to build a Docker container that
5757
in turn will build the binary programs and copy them to a local directory.
5858

5959
## More information

buildInDocker.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# © Copyright IBM Corporation 2019
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
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.*"
22+
export GOCACHE=/tmp/.cache
23+
24+
# Which monitor programs are to be built. By default, build the complete set available.
25+
# It can be overridden by setting the value on the "docker run" command with
26+
# a "-e MONITORS=..." flag.
27+
if [ -z "$MONITORS" ]
28+
then
29+
cd $GOPATH/src/$ORG/$REPO
30+
MONITORS=`ls cmd`
31+
fi
32+
33+
# And do the builds into the bin directory
34+
cd $GOPATH
35+
for m in $MONITORS
36+
do
37+
srcdir=src/$ORG/$REPO/cmd/$m
38+
39+
echo "Building $m"
40+
if [ ! -z "$BUILD_EXTRA_INJECT" ]
41+
then
42+
BUILD_EXTRA_LDFLAGS="-ldflags"
43+
fi
44+
45+
go build -o bin/$m $BUILD_EXTRA_LDFLAGS "$BUILD_EXTRA_INJECT" $srcdir/*.go
46+
47+
# Copy the supporting scripts into the output directory
48+
if [ -r $srcdir/$m.sh ]
49+
then
50+
cp $srcdir/*.sh bin
51+
chmod a+rx bin/*.sh
52+
fi
53+
if [ -r $srcdir/$m.mqsc ]
54+
then
55+
cp $srcdir/*.mqsc bin
56+
fi
57+
done

buildMain.sh

Lines changed: 0 additions & 56 deletions
This file was deleted.

buildMonitors.sh

100644100755
Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/bin/bash
2-
31
# © Copyright IBM Corporation 2019
42
#
53
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,43 +12,50 @@
1412
# See the License for the specific language governing permissions and
1513
# limitations under the License.
1614

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"
2219

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" ]
2724
then
28-
cd $GOPATH/src/$ORG/$REPO
29-
MONITORS=`ls cmd`
25+
VER="latest"
3026
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=$?
3132

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

Comments
 (0)