github.com/ojongerius/docker@v1.11.2/hack/make/build-rpm (about)

     1  #!/bin/bash
     2  set -e
     3  
     4  # subshell so that we can export PATH and TZ without breaking other things
     5  (
     6  	export TZ=UTC # make sure our "date" variables are UTC-based
     7  
     8  	source "$(dirname "$BASH_SOURCE")/.integration-daemon-start"
     9  	source "$(dirname "$BASH_SOURCE")/.detect-daemon-osarch"
    10  
    11  	# TODO consider using frozen images for the dockercore/builder-rpm tags
    12  
    13  	rpmName=docker-engine
    14  	rpmVersion="$VERSION"
    15  	rpmRelease=1
    16  
    17  	# rpmRelease versioning is as follows
    18  	# Docker 1.7.0:  version=1.7.0, release=1
    19  	# Docker 1.7.0-rc1: version=1.7.0, release=0.1.rc1
    20  	# Docker 1.7.0-cs1: version=1.7.0.cs1, release=1
    21  	# Docker 1.7.0-cs1-rc1: version=1.7.0.cs1, release=0.1.rc1
    22  	# Docker 1.7.0-dev nightly: version=1.7.0, release=0.0.YYYYMMDD.HHMMSS.gitHASH
    23  
    24  	# if we have a "-rc*" suffix, set appropriate release
    25  	if [[ "$rpmVersion" =~ .*-rc[0-9]+$ ]] ; then
    26  		rcVersion=${rpmVersion#*-rc}
    27  		rpmVersion=${rpmVersion%-rc*}
    28  		rpmRelease="0.${rcVersion}.rc${rcVersion}"
    29  	fi
    30  
    31  	DOCKER_GITCOMMIT=$(git rev-parse --short HEAD)
    32  	if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    33  		DOCKER_GITCOMMIT="$DOCKER_GITCOMMIT-unsupported"
    34  	fi
    35  
    36  	# if we have a "-dev" suffix or have change in Git, let's make this package version more complex so it works better
    37  	if [[ "$rpmVersion" == *-dev ]] || [ -n "$(git status --porcelain)" ]; then
    38  		gitUnix="$(git log -1 --pretty='%at')"
    39  		gitDate="$(date --date "@$gitUnix" +'%Y%m%d.%H%M%S')"
    40  		gitCommit="$(git log -1 --pretty='%h')"
    41  		gitVersion="${gitDate}.git${gitCommit}"
    42  		# gitVersion is now something like '20150128.112847.17e840a'
    43  		rpmVersion="${rpmVersion%-dev}"
    44  		rpmRelease="0.0.$gitVersion"
    45  	fi
    46  
    47  	# Replace any other dashes with periods
    48  	rpmVersion="${rpmVersion/-/.}"
    49  
    50  	rpmPackager="$(awk -F ': ' '$1 == "Packager" { print $2; exit }' hack/make/.build-rpm/${rpmName}.spec)"
    51  	rpmDate="$(date +'%a %b %d %Y')"
    52  
    53  	# if go-md2man is available, pre-generate the man pages
    54  	./man/md2man-all.sh -q || true
    55  	# TODO decide if it's worth getting go-md2man in _each_ builder environment to avoid this
    56  
    57  	# Convert the CHANGELOG.md file into RPM changelog format
    58  	VERSION_REGEX="^\W\W (.*) \((.*)\)$"
    59  	ENTRY_REGEX="^[-+*] (.*)$"
    60  	while read -r line || [[ -n "$line" ]]; do
    61  		if [ -z "$line" ]; then continue; fi
    62  		if [[ "$line" =~ $VERSION_REGEX ]]; then
    63  			echo >> contrib/builder/rpm/${PACKAGE_ARCH}/changelog
    64  			echo "* `date -d ${BASH_REMATCH[2]} '+%a %b %d %Y'` ${rpmPackager} - ${BASH_REMATCH[1]}" >> contrib/builder/rpm/${PACKAGE_ARCH}/changelog
    65  		fi
    66  		if [[ "$line" =~ $ENTRY_REGEX ]]; then
    67  			echo "- ${BASH_REMATCH[1]//\`}" >> contrib/builder/rpm/${PACKAGE_ARCH}/changelog
    68  		fi
    69  	done < CHANGELOG.md
    70  
    71  	builderDir="contrib/builder/rpm/${PACKAGE_ARCH}"
    72  	pkgs=( $(find "${builderDir}/"*/ -type d) )
    73  	if [ ! -z "$DOCKER_BUILD_PKGS" ]; then
    74  		pkgs=( $(echo ${DOCKER_BUILD_PKGS[@]/#/$builderDir\/}) )
    75  	fi
    76  	for dir in "${pkgs[@]}"; do
    77  		[ -d "$dir" ] || { echo >&2 "skipping nonexistent $dir"; continue; }
    78  		version="$(basename "$dir")"
    79  		suite="${version##*-}"
    80  
    81  		image="dockercore/builder-rpm:$version"
    82  		if ! docker inspect "$image" &> /dev/null; then
    83  			( set -x && docker build ${DOCKER_BUILD_ARGS} -t "$image" "$dir" )
    84  		fi
    85  
    86  		mkdir -p "$DEST/$version"
    87  		cat > "$DEST/$version/Dockerfile.build" <<-EOF
    88  			FROM $image
    89  			COPY . /usr/src/${rpmName}
    90  			RUN mkdir -p /go/src/github.com/docker && mkdir -p /go/src/github.com/opencontainers
    91  		EOF
    92  
    93  		# get the RUNC and CONTAINERD commit from the root Dockerfile, this keeps the commits in sync
    94  		awk '$1 == "ENV" && $2 == "RUNC_COMMIT" { print; exit }' Dockerfile >> "$DEST/$version/Dockerfile.build"
    95  		awk '$1 == "ENV" && $2 == "CONTAINERD_COMMIT" { print; exit }' Dockerfile >> "$DEST/$version/Dockerfile.build"
    96  
    97  		# add runc and containerd compile and install
    98  		cat >> "$DEST/$version/Dockerfile.build" <<-EOF
    99  			# Install runc
   100  			RUN git clone git://github.com/opencontainers/runc.git "/go/src/github.com/opencontainers/runc" \
   101  					&& cd "/go/src/github.com/opencontainers/runc" \
   102  					&& git checkout -q "\$RUNC_COMMIT"
   103  			RUN set -x && export GOPATH="/go" && cd "/go/src/github.com/opencontainers/runc" \
   104  					&& make BUILDTAGS="\$RUNC_BUILDTAGS" && make install
   105  			# Install containerd
   106  			RUN git clone git://github.com/docker/containerd.git "/go/src/github.com/docker/containerd" \
   107  					&& cd "/go/src/github.com/docker/containerd" \
   108  					&& git checkout -q "\$CONTAINERD_COMMIT"
   109  			RUN set -x && export GOPATH="/go" && cd "/go/src/github.com/docker/containerd" && make && make install
   110  		EOF
   111  		if [ "$DOCKER_EXPERIMENTAL" ]; then
   112  			echo 'ENV DOCKER_EXPERIMENTAL 1' >> "$DEST/$version/Dockerfile.build"
   113  		fi
   114  		cat >> "$DEST/$version/Dockerfile.build" <<-EOF
   115  			RUN mkdir -p /root/rpmbuild/SOURCES \
   116  				&& echo '%_topdir /root/rpmbuild' > /root/.rpmmacros
   117  			WORKDIR /root/rpmbuild
   118  			RUN ln -sfv /usr/src/${rpmName}/hack/make/.build-rpm SPECS
   119  			WORKDIR /root/rpmbuild/SPECS
   120  			RUN tar -r -C /usr/src -f /root/rpmbuild/SOURCES/${rpmName}.tar ${rpmName}
   121  			RUN tar -r -C /go/src/github.com/docker -f /root/rpmbuild/SOURCES/${rpmName}.tar containerd
   122  			RUN tar -r -C /go/src/github.com/opencontainers -f /root/rpmbuild/SOURCES/${rpmName}.tar runc
   123  			RUN gzip /root/rpmbuild/SOURCES/${rpmName}.tar
   124  			RUN { cat /usr/src/${rpmName}/contrib/builder/rpm/${PACKAGE_ARCH}/changelog; } >> ${rpmName}.spec && tail >&2 ${rpmName}.spec
   125  			RUN rpmbuild -ba \
   126  				--define '_gitcommit $DOCKER_GITCOMMIT' \
   127  				--define '_release $rpmRelease' \
   128  				--define '_version $rpmVersion' \
   129  				--define '_origversion $VERSION' \
   130  				--define '_experimental ${DOCKER_EXPERIMENTAL:-0}' \
   131  				${rpmName}.spec
   132  		EOF
   133  		# selinux policy referencing systemd things won't work on non-systemd versions
   134  		# of centos or rhel, which we don't support anyways
   135  		if [ "${suite%.*}" -gt 6 ] && [[ "$version" != opensuse* ]]; then
   136  			cat >> "$DEST/$version/Dockerfile.build" <<-EOF
   137  				RUN tar -cz -C /usr/src/${rpmName}/contrib -f /root/rpmbuild/SOURCES/${rpmName}-selinux.tar.gz ${rpmName}-selinux
   138  				RUN rpmbuild -ba \
   139  						--define '_gitcommit $DOCKER_GITCOMMIT' \
   140  						--define '_release $rpmRelease' \
   141  						--define '_version $rpmVersion' \
   142  						--define '_origversion $VERSION' \
   143  						${rpmName}-selinux.spec
   144  			EOF
   145  		fi
   146  		tempImage="docker-temp/build-rpm:$version"
   147  		( set -x && docker build -t "$tempImage" -f $DEST/$version/Dockerfile.build . )
   148  		docker run --rm "$tempImage" bash -c 'cd /root/rpmbuild && tar -c *RPMS' | tar -xvC "$DEST/$version"
   149  		docker rmi "$tempImage"
   150  	done
   151  
   152  	source "$(dirname "$BASH_SOURCE")/.integration-daemon-stop"
   153  ) 2>&1 | tee -a $DEST/test.log