Browse code

Lock release build to specific versions

Reorganize to make more friendly for docker build behavior. Note that
after this change openshift/origin-release:latest can no longer be used
with hack/build-release.sh.

Add a new hack/env stub that makes it easy to run containerized if
necessary.

Clayton Coleman authored on 2016/06/11 03:20:26
Showing 6 changed files
... ...
@@ -11,6 +11,7 @@
11 11
 /examples/sample-app/openshift.local.*
12 12
 /examples/sample-app/logs/openshift.log
13 13
 /dind-*.rc
14
+/os-version-defs
14 15
 *.swp
15 16
 .vimrc
16 17
 .vagrant-openshift.json*
... ...
@@ -71,7 +71,7 @@ update: build
71 71
 	hack/update-generated-docs.sh
72 72
 	hack/update-generated-swagger-descriptions.sh
73 73
 	hack/update-generated-swagger-spec.sh
74
-.PHONE: update
74
+.PHONY: update
75 75
 
76 76
 # Run unit tests.
77 77
 #
... ...
@@ -25,25 +25,18 @@ rm -rf "${context}"
25 25
 mkdir -p "${context}"
26 26
 mkdir -p "${OS_OUTPUT}"
27 27
 
28
-# Generate version definitions.
29
-# You can commit a specific version by specifying OS_GIT_COMMIT="" prior to build
30
-os::build::get_version_vars
31
-os::build::save_version_vars "${context}/os-version-defs"
32
-
33
-echo "++ Building release ${OS_GIT_VERSION}"
34
-
35
-# Create the input archive.
36
-git archive --format=tar -o "${context}/archive.tar" "${OS_GIT_COMMIT}"
37
-tar -rf "${context}/archive.tar" -C "${context}" os-version-defs
38
-if [[ -n "${OVERRIDE_BUILD-}" ]]; then
39
-  tar -rf "${context}/archive.tar" -C "${context}" ${OVERRIDE_BUILD[@]}
40
-fi
41
-gzip -f "${context}/archive.tar"
28
+container="$( os::build::environment::create /bin/sh -c "OS_ONLY_BUILD_PLATFORMS=${OS_ONLY_BUILD_PLATFORMS-} make build-cross" )"
29
+trap "os::build::environment::cleanup ${container}" EXIT
42 30
 
43 31
 # Perform the build and release in Docker.
44
-cat "${context}/archive.tar.gz" | docker run -e "OS_ONLY_BUILD_PLATFORMS=${OS_ONLY_BUILD_PLATFORMS-}" -i --cidfile="${context}/cid" openshift/origin-release
45
-docker cp $(cat ${context}/cid):/go/src/github.com/openshift/origin/_output/local/releases "${OS_OUTPUT}"
32
+(
33
+  OS_GIT_TREE_STATE=clean # set this because we will be pulling from git archive
34
+  os::build::get_version_vars
35
+  echo "++ Building release ${OS_GIT_VERSION}"
36
+)
37
+os::build::environment::withsource "${container}" "${OS_GIT_COMMIT:-HEAD}"
38
+# Get the command output
39
+docker cp "${container}:/go/src/github.com/openshift/origin/_output/local/releases" "${OS_OUTPUT}"
46 40
 echo "${OS_GIT_COMMIT}" > "${OS_LOCAL_RELEASEPATH}/.commit"
47
-docker rm $(cat ${context}/cid)
48 41
 
49 42
 ret=$?; ENDTIME=$(date +%s); echo "$0 took $(($ENDTIME - $STARTTIME)) seconds"; exit "$ret"
... ...
@@ -21,6 +21,9 @@ readonly OS_ROOT=$(
21 21
   fi
22 22
 )
23 23
 
24
+readonly OS_BUILD_ENV_GOLANG="${OS_BUILD_ENV_GOLANG:-1.6}"
25
+readonly OS_BUILD_ENV_IMAGE="${OS_BUILD_ENV_IMAGE:-openshift/origin-release:golang-${OS_BUILD_ENV_GOLANG}}"
26
+
24 27
 readonly OS_OUTPUT_SUBPATH="${OS_OUTPUT_SUBPATH:-_output/local}"
25 28
 readonly OS_OUTPUT="${OS_ROOT}/${OS_OUTPUT_SUBPATH}"
26 29
 readonly OS_LOCAL_RELEASEPATH="${OS_OUTPUT}/releases"
... ...
@@ -862,4 +865,107 @@ function os::build::find-binary() {
862 862
   local path=$( (ls -t $(os::build::get-bin-output-path "${os_root}")/${bin}) 2>/dev/null || true | head -1 )
863 863
   echo "$path"
864 864
 }
865
-readonly -f os::build::find-binary
866 865
\ No newline at end of file
866
+readonly -f os::build::find-binary
867
+
868
+# os::build::environment::create creates a docker container with the default variables.
869
+# arguments are passed directly to the container, OS_BUILD_ENV_GOLANG, OS_BUILD_ENV_IMAGE,
870
+# and OS_RELEASE_DOCKER_ARGS can be used to customize the container. The docker socket
871
+# is mounted by default and the output of the command is the container id.
872
+function os::build::environment::create() {
873
+  set -o errexit
874
+  local golang_version="${OS_BUILD_ENV_GOLANG}"
875
+  local release_image="${OS_BUILD_ENV_IMAGE}"
876
+  local additional_context="${OS_BUILD_ENV_DOCKER_ARGS:-}"
877
+  if [[ -z "${additional_context}" && "${OS_BUILD_ENV_USE_DOCKER:-y}" == "y" ]]; then
878
+    additional_context="--privileged -v /var/run/docker.sock:/var/run/docker.sock"
879
+
880
+    if [[ "${OS_BUILD_ENV_LOCAL_DOCKER:-n}" == "y" ]]; then
881
+      # if OS_BUILD_ENV_LOCAL_DOCKER==y, add the local OS_ROOT as the bind mount to the working dir
882
+      # and set the running user to the current user
883
+      local workingdir
884
+      workingdir=$( os::build::environment::release::workingdir )
885
+      additional_context="${additional_context} -v ${OS_ROOT}:${workingdir} -u $(id -u)"
886
+    elif [[ -n "${OS_BUILD_ENV_REUSE_VOLUME:-}" ]]; then
887
+      # if OS_BUILD_ENV_REUSE_VOLUME is set, create a docker volume to store the working output so
888
+      # successive iterations can reuse shared code.
889
+      local workingdir
890
+      workingdir=$( os::build::environment::release::workingdir )
891
+      name="$( echo "${OS_BUILD_ENV_REUSE_VOLUME}" | tr '[:upper:]' '[:lower:]' )"
892
+      docker volume create --name "${name}" > /dev/null
893
+      additional_context="${additional_context} -v ${name}:${workingdir}"
894
+    fi
895
+  fi
896
+
897
+  # Create a new container to from the release environment
898
+  docker create ${additional_context} "${release_image}" "$@"
899
+}
900
+readonly -f os::build::environment::create
901
+
902
+# os::build::environment::release::workingdir calculates the working directory for the current
903
+# release image.
904
+function os::build::environment::release::workingdir() {
905
+  set -o errexit
906
+  # get working directory
907
+  local container
908
+  container="$(docker create "${release_image}")"
909
+  local workingdir
910
+  workingdir="$(docker inspect -f '{{ index . "Config" "WorkingDir" }}' "${container}")"
911
+  docker rm "${container}" > /dev/null
912
+  echo "${workingdir}"
913
+}
914
+readonly -f os::build::environment::release::workingdir
915
+
916
+# os::build::environment::cleanup stops and removes the container named in the argument
917
+# (unless OS_BUILD_ENV_LEAVE_CONTAINER is set, in which case it will only stop the container).
918
+function os::build::environment::cleanup() {
919
+  local container=$1
920
+  docker stop --time=0 "${container}" > /dev/null || true
921
+  if [[ -z "${OS_BUILD_ENV_LEAVE_CONTAINER:-}" ]]; then
922
+    docker rm "${container}" > /dev/null
923
+  fi
924
+}
925
+readonly -f os::build::environment::cleanup
926
+
927
+# os::build::environment::withsource starts the container provided as the first argument
928
+# after copying in the contents of the current Git repository at HEAD (or, if specified,
929
+# the ref specified in the second argument).
930
+function os::build::environment::withsource() {
931
+  local container=$1
932
+  local commit=${2:-HEAD}
933
+
934
+  if [[ -n "${OS_BUILD_ENV_LOCAL_DOCKER:-}" ]]; then
935
+    # running locally, no change necessary
936
+    os::build::get_version_vars
937
+    os::build::save_version_vars "${OS_ROOT}/os-version-defs"
938
+  else
939
+    # Generate version definitions. Tree state is clean because we are pulling from git directly.
940
+    OS_GIT_TREE_STATE=clean os::build::get_version_vars
941
+    os::build::save_version_vars "/tmp/os-version-defs"
942
+
943
+    local workingdir
944
+    workingdir="$(docker inspect -f '{{ index . "Config" "WorkingDir" }}' "${container}")"
945
+    tar -cf - -C /tmp/ os-version-defs | docker cp - "${container}:${workingdir}"
946
+    git archive --format=tar "${commit}" | docker cp - "${container}:${workingdir}"
947
+  fi
948
+
949
+  docker start "${container}" > /dev/null
950
+  docker logs -f "${container}"
951
+}
952
+readonly -f os::build::environment::withsource
953
+
954
+# os::build::environment::run launches the container with the provided arguments and
955
+# the current commit (defaults to HEAD). The container is automatically cleaned up.
956
+function os::build::environment::run() {
957
+  local commit="${OS_GIT_COMMIT:-HEAD}"
958
+  local volume="${OS_BUILD_ENV_REUSE_VOLUME:-}"
959
+  if [[ -z "${OS_BUILD_ENV_REUSE_VOLUME:-}" ]]; then
960
+    volume="origin-build-$( git rev-parse "${commit}" )"
961
+  fi
962
+
963
+  local container
964
+  container="$( OS_BUILD_ENV_REUSE_VOLUME=${volume} os::build::environment::create "$@" )"
965
+  trap "os::build::environment::cleanup ${container}" EXIT
966
+
967
+  os::build::environment::withsource "${container}" "${commit}"
968
+}
969
+readonly -f os::build::environment::run
867 970
new file mode 100755
... ...
@@ -0,0 +1,19 @@
0
+#!/bin/bash
1
+
2
+# This script generates release zips into _output/releases. It requires the openshift/origin-release
3
+# image to be built prior to executing this command via hack/build-base-images.sh.
4
+
5
+# NOTE:   only committed code is built.
6
+
7
+set -o errexit
8
+set -o nounset
9
+set -o pipefail
10
+
11
+OS_ROOT=$(dirname "${BASH_SOURCE}")/..
12
+source "${OS_ROOT}/hack/lib/init.sh"
13
+os::log::install_errexit
14
+
15
+# Go to the top of the tree.
16
+cd "${OS_ROOT}"
17
+
18
+os::build::environment::run "$@"
0 19
\ No newline at end of file
... ...
@@ -7,6 +7,11 @@ set -o pipefail
7 7
 OS_ROOT=$(dirname "${BASH_SOURCE}")/..
8 8
 source "${OS_ROOT}/hack/lib/init.sh"
9 9
 
10
+if ! git status &> /dev/null; then
11
+  echo "SKIPPED: Not a Git repository"
12
+  exit 0
13
+fi
14
+
10 15
 "${OS_ROOT}/hack/build-go.sh" tools/rebasehelpers/commitchecker
11 16
 
12 17
 # Find binary