github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/build-support/functions/20-build.sh (about) 1 function refresh_docker_images { 2 # Arguments: 3 # $1 - Path to top level Consul source 4 # $2 - Which make target to invoke (optional) 5 # 6 # Return: 7 # 0 - success 8 # * - failure 9 10 if ! test -d "$1" 11 then 12 err "ERROR: '$1' is not a directory. refresh_docker_images must be called with the path to the top level source as the first argument'" 13 return 1 14 fi 15 16 local sdir="$1" 17 local targets="$2" 18 19 test -n "${targets}" || targets="docker-images" 20 21 make -C "${sdir}" ${targets} 22 return $? 23 } 24 25 function build_ui { 26 # Arguments: 27 # $1 - Path to the top level Consul source 28 # $2 - The docker image to run the build within (optional) 29 # $3 - Version override 30 # 31 # Returns: 32 # 0 - success 33 # * - error 34 # 35 # Notes: 36 # Use the GIT_COMMIT environment variable to pass off to the build 37 38 if ! test -d "$1" 39 then 40 err "ERROR: '$1' is not a directory. build_ui must be called with the path to the top level source as the first argument'" 41 return 1 42 fi 43 44 local image_name=${UI_BUILD_CONTAINER_DEFAULT} 45 if test -n "$2" 46 then 47 image_name="$2" 48 fi 49 50 local sdir="$1" 51 local ui_dir="${1}/ui-v2" 52 53 # parse the version 54 version=$(parse_version "${sdir}") 55 56 if test -n "$3" 57 then 58 version="$3" 59 fi 60 61 local commit_hash="${GIT_COMMIT}" 62 if test -z "${commit_hash}" 63 then 64 commit_hash=$(git rev-parse --short HEAD) 65 fi 66 local logo_type="${CONSUL_BINARY_TYPE}" 67 if test "$logo_type" != "oss" 68 then 69 logo_type="enterprise" 70 fi 71 72 # make sure we run within the ui dir 73 pushd ${ui_dir} > /dev/null 74 75 status "Creating the UI Build Container with image: ${image_name} and version '${version}'" 76 local container_id=$(docker create -it -e "CONSUL_GIT_SHA=${commit_hash}" -e "CONSUL_VERSION=${version}" -e "CONSUL_BINARY_TYPE=${CONSUL_BINARY_TYPE}" ${image_name}) 77 local ret=$? 78 if test $ret -eq 0 79 then 80 status "Copying the source from '${ui_dir}' to /consul-src within the container" 81 ( 82 tar -c $(ls -A | grep -v "^(node_modules\|dist\|tmp)") | docker cp - ${container_id}:/consul-src && 83 status "Running build in container" && docker start -i ${container_id} && 84 rm -rf ${1}/ui-v2/dist && 85 status "Copying back artifacts" && docker cp ${container_id}:/consul-src/dist ${1}/ui-v2/dist 86 ) 87 ret=$? 88 docker rm ${container_id} > /dev/null 89 fi 90 91 # Check the version is baked in correctly 92 if test ${ret} -eq 0 93 then 94 local ui_vers=$(ui_version "${1}/ui-v2/dist/index.html") 95 if test "${version}" != "${ui_vers}" 96 then 97 err "ERROR: UI version mismatch. Expecting: '${version}' found '${ui_vers}'" 98 ret=1 99 fi 100 fi 101 102 # Check the logo is baked in correctly 103 if test ${ret} -eq 0 104 then 105 local ui_logo_type=$(ui_logo_type "${1}/ui-v2/dist/index.html") 106 if test "${logo_type}" != "${ui_logo_type}" 107 then 108 err "ERROR: UI logo type mismatch. Expecting: '${logo_type}' found '${ui_logo_type}'" 109 ret=1 110 fi 111 fi 112 113 # Copy UI over ready to be packaged into the binary 114 if test ${ret} -eq 0 115 then 116 rm -rf ${1}/pkg/web_ui/v2 117 mkdir -p ${1}/pkg/web_ui 118 cp -r ${1}/ui-v2/dist ${1}/pkg/web_ui/v2 119 fi 120 121 popd > /dev/null 122 return $ret 123 } 124 125 function build_ui_legacy { 126 # Arguments: 127 # $1 - Path to the top level Consul source 128 # $2 - The docker image to run the build within (optional) 129 # 130 # Returns: 131 # 0 - success 132 # * - error 133 134 if ! test -d "$1" 135 then 136 err "ERROR: '$1' is not a directory. build_ui_legacy must be called with the path to the top level source as the first argument'" 137 return 1 138 fi 139 140 local sdir="$1" 141 local ui_legacy_dir="${sdir}/ui" 142 143 local image_name=${UI_LEGACY_BUILD_CONTAINER_DEFAULT} 144 if test -n "$2" 145 then 146 image_name="$2" 147 fi 148 149 pushd ${ui_legacy_dir} > /dev/null 150 status "Creating the Legacy UI Build Container with image: ${image_name}" 151 rm -r ${sdir}/pkg/web_ui/v1 >/dev/null 2>&1 152 mkdir -p ${sdir}/pkg/web_ui/v1 153 local container_id=$(docker create -it ${image_name}) 154 local ret=$? 155 if test $ret -eq 0 156 then 157 status "Copying the source from '${ui_legacy_dir}' to /consul-src/ui within the container" 158 ( 159 docker cp . ${container_id}:/consul-src/ui && 160 status "Running build in container" && 161 docker start -i ${container_id} && 162 status "Copying back artifacts" && 163 docker cp ${container_id}:/consul-src/pkg/web_ui/v1/. ${sdir}/pkg/web_ui/v1 164 ) 165 ret=$? 166 docker rm ${container_id} > /dev/null 167 fi 168 popd > /dev/null 169 return $ret 170 } 171 172 function build_assetfs { 173 # Arguments: 174 # $1 - Path to the top level Consul source 175 # $2 - The docker image to run the build within (optional) 176 # 177 # Returns: 178 # 0 - success 179 # * - error 180 # 181 # Note: 182 # The GIT_COMMIT, GIT_DIRTY and GIT_DESCRIBE environment variables will be used if present 183 184 if ! test -d "$1" 185 then 186 err "ERROR: '$1' is not a directory. build_assetfs must be called with the path to the top level source as the first argument'" 187 return 1 188 fi 189 190 local sdir="$1" 191 local image_name=${GO_BUILD_CONTAINER_DEFAULT} 192 if test -n "$2" 193 then 194 image_name="$2" 195 fi 196 197 pushd ${sdir} > /dev/null 198 status "Creating the Go Build Container with image: ${image_name}" 199 local container_id=$(docker create -it -e GIT_COMMIT=${GIT_COMMIT} -e GIT_DIRTY=${GIT_DIRTY} -e GIT_DESCRIBE=${GIT_DESCRIBE} ${image_name} make static-assets ASSETFS_PATH=bindata_assetfs.go) 200 local ret=$? 201 if test $ret -eq 0 202 then 203 status "Copying the sources from '${sdir}/(pkg/web_ui|GNUmakefile)' to /go/src/github.com/hashicorp/consul/pkg" 204 ( 205 tar -c pkg/web_ui GNUmakefile | docker cp - ${container_id}:/go/src/github.com/hashicorp/consul && 206 status "Running build in container" && docker start -i ${container_id} && 207 status "Copying back artifacts" && docker cp ${container_id}:/go/src/github.com/hashicorp/consul/bindata_assetfs.go ${sdir}/agent/bindata_assetfs.go 208 ) 209 ret=$? 210 docker rm ${container_id} > /dev/null 211 fi 212 popd >/dev/null 213 return $ret 214 } 215 216 function build_consul_post { 217 # Arguments 218 # $1 - Path to the top level Consul source 219 # $2 - Subdirectory under pkg/bin (Optional) 220 # 221 # Returns: 222 # 0 - success 223 # * - error 224 # 225 # Notes: 226 # pkg/bin is where to place binary packages 227 # pkg.bin.new is where the just built binaries are located 228 # bin is where to place the local systems versions 229 230 if ! test -d "$1" 231 then 232 err "ERROR: '$1' is not a directory. build_consul_post must be called with the path to the top level source as the first argument'" 233 return 1 234 fi 235 236 local sdir="$1" 237 238 local extra_dir_name="$2" 239 local extra_dir="" 240 241 if test -n "${extra_dir_name}" 242 then 243 extra_dir="${extra_dir_name}/" 244 fi 245 246 pushd "${sdir}" > /dev/null 247 248 # recreate the pkg dir 249 rm -r pkg/bin/${extra_dir}* 2> /dev/null 250 mkdir -p pkg/bin/${extra_dir} 2> /dev/null 251 252 # move all files in pkg.new into pkg 253 cp -r pkg.bin.new/${extra_dir}* pkg/bin/${extra_dir} 254 rm -r pkg.bin.new 255 256 DEV_PLATFORM="./pkg/bin/${extra_dir}$(go env GOOS)_$(go env GOARCH)" 257 for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f 2>/dev/null) 258 do 259 # recreate the bin dir 260 rm -r bin/* 2> /dev/null 261 mkdir -p bin 2> /dev/null 262 263 cp ${F} bin/ 264 cp ${F} ${MAIN_GOPATH}/bin 265 done 266 267 popd > /dev/null 268 269 return 0 270 } 271 272 function build_consul { 273 # Arguments: 274 # $1 - Path to the top level Consul source 275 # $2 - Subdirectory to put binaries in under pkg/bin (optional - must specify if needing to specify the docker image) 276 # $3 - The docker image to run the build within (optional) 277 # 278 # Returns: 279 # 0 - success 280 # * - error 281 # 282 # Note: 283 # The GOLDFLAGS and GOTAGS environment variables will be used if set 284 # If the CONSUL_DEV environment var is truthy only the local platform/architecture is built. 285 # If the XC_OS or the XC_ARCH environment vars are present then only those platforms/architectures 286 # will be built. Otherwise all supported platform/architectures are built 287 288 if ! test -d "$1" 289 then 290 err "ERROR: '$1' is not a directory. build_consul must be called with the path to the top level source as the first argument'" 291 return 1 292 fi 293 294 local sdir="$1" 295 local extra_dir_name="$2" 296 local extra_dir="" 297 local image_name=${GO_BUILD_CONTAINER_DEFAULT} 298 if test -n "$3" 299 then 300 image_name="$3" 301 fi 302 303 pushd ${sdir} > /dev/null 304 status "Creating the Go Build Container with image: ${image_name}" 305 if is_set "${CONSUL_DEV}" 306 then 307 if test -z "${XC_OS}" 308 then 309 XC_OS=$(go env GOOS) 310 fi 311 312 if test -z "${XC_ARCH}" 313 then 314 XC_ARCH=$(go env GOARCH) 315 fi 316 fi 317 XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"} 318 XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"} 319 320 if test -n "${extra_dir_name}" 321 then 322 extra_dir="${extra_dir_name}/" 323 fi 324 325 local container_id=$(docker create -it -e CGO_ENABLED=0 ${image_name} gox -os="${XC_OS}" -arch="${XC_ARCH}" -osarch="!darwin/arm !freebsd/arm !darwin/arm64" -ldflags "${GOLDFLAGS}" -output "pkg/bin/${extra_dir}{{.OS}}_{{.Arch}}/consul" -tags="${GOTAGS}") 326 ret=$? 327 328 if test $ret -eq 0 329 then 330 status "Copying the source from '${sdir}' to /go/src/github.com/hashicorp/consul" 331 ( 332 tar -c $(ls | grep -v "^(ui\|ui-v2\|website\|bin\|pkg\|.git)") | docker cp - ${container_id}:/go/src/github.com/hashicorp/consul && 333 status "Running build in container" && 334 docker start -i ${container_id} && 335 status "Copying back artifacts" && 336 docker cp ${container_id}:/go/src/github.com/hashicorp/consul/pkg/bin pkg.bin.new 337 ) 338 ret=$? 339 docker rm ${container_id} > /dev/null 340 341 if test $ret -eq 0 342 then 343 build_consul_post "${sdir}" "${extra_dir_name}" 344 ret=$? 345 else 346 rm -r pkg.bin.new 2> /dev/null 347 fi 348 fi 349 popd > /dev/null 350 return $ret 351 } 352 353 function build_consul_local { 354 # Arguments: 355 # $1 - Path to the top level Consul source 356 # $2 - Space separated string of OSes to build. If empty will use env vars for determination. 357 # $3 - Space separated string of architectures to build. If empty will use env vars for determination. 358 # $4 - Subdirectory to put binaries in under pkg/bin (optional) 359 # 360 # Returns: 361 # 0 - success 362 # * - error 363 # 364 # Note: 365 # The GOLDFLAGS and GOTAGS environment variables will be used if set 366 # If the CONSUL_DEV environment var is truthy only the local platform/architecture is built. 367 # If the XC_OS or the XC_ARCH environment vars are present then only those platforms/architectures 368 # will be built. Otherwise all supported platform/architectures are built 369 # The NOGOX environment variable will be used if present. This will prevent using gox and instead 370 # build with go install. 371 # The GOXPARALLEL environment variable is used if set 372 373 if ! test -d "$1" 374 then 375 err "ERROR: '$1' is not a directory. build_consul must be called with the path to the top level source as the first argument'" 376 return 1 377 fi 378 379 local sdir="$1" 380 local build_os="$2" 381 local build_arch="$3" 382 local extra_dir_name="$4" 383 local extra_dir="" 384 385 if test -n "${extra_dir_name}" 386 then 387 extra_dir="${extra_dir_name}/" 388 fi 389 390 pushd ${sdir} > /dev/null 391 if is_set "${CONSUL_DEV}" 392 then 393 if test -z "${XC_OS}" 394 then 395 XC_OS=$(go env GOOS) 396 fi 397 398 if test -z "${XC_ARCH}" 399 then 400 XC_ARCH=$(go env GOARCH) 401 fi 402 fi 403 XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"} 404 XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"} 405 406 if test -z "${build_os}" 407 then 408 build_os="${XC_OS}" 409 fi 410 411 if test -z "${build_arch}" 412 then 413 build_arch="${XC_ARCH}" 414 fi 415 416 local use_gox=1 417 is_set "${NOGOX}" && use_gox=0 418 which gox > /dev/null || use_gox=0 419 420 status_stage "==> Building Consul - OSes: ${build_os}, Architectures: ${build_arch}" 421 mkdir pkg.bin.new 2> /dev/null 422 if is_set "${use_gox}" 423 then 424 status "Using gox for concurrent compilation" 425 426 CGO_ENABLED=0 gox \ 427 -os="${build_os}" \ 428 -arch="${build_arch}" \ 429 -osarch="!darwin/arm !darwin/arm64 !freebsd/arm" \ 430 -ldflags="${GOLDFLAGS}" \ 431 -parallel="${GOXPARALLEL:-"-1"}" \ 432 -output "pkg.bin.new/${extra_dir}{{.OS}}_{{.Arch}}/consul" \ 433 -tags="${GOTAGS}" \ 434 . 435 436 if test $? -ne 0 437 then 438 err "ERROR: Failed to build Consul" 439 rm -r pkg.bin.new 440 return 1 441 fi 442 else 443 status "Building sequentially with go install" 444 for os in ${build_os} 445 do 446 for arch in ${build_arch} 447 do 448 outdir="pkg.bin.new/${extra_dir}${os}_${arch}" 449 osarch="${os}/${arch}" 450 if test "${osarch}" == "darwin/arm" -o "${osarch}" == "darwin/arm64" -o "${osarch}" == "freebsd/arm64" -o "${osarch}" == "windows/arm" -o "${osarch}" == "windows/arm64" -o "${osarch}" == "freebsd/arm" 451 then 452 continue 453 fi 454 455 if test "${os}" == "solaris" -a "${arch}" != "amd64" 456 then 457 continue 458 fi 459 460 echo "---> ${osarch}" 461 462 463 mkdir -p "${outdir}" 464 GOBIN_EXTRA="" 465 if test "${os}" != "$(go env GOHOSTOS)" -o "${arch}" != "$(go env GOHOSTARCH)" 466 then 467 GOBIN_EXTRA="${os}_${arch}/" 468 fi 469 binname="consul" 470 if [ $os == "windows" ];then 471 binname="consul.exe" 472 fi 473 CGO_ENABLED=0 GOOS=${os} GOARCH=${arch} go install -ldflags "${GOLDFLAGS}" -tags "${GOTAGS}" && cp "${MAIN_GOPATH}/bin/${GOBIN_EXTRA}${binname}" "${outdir}/${binname}" 474 if test $? -ne 0 475 then 476 err "ERROR: Failed to build Consul for ${osarch}" 477 rm -r pkg.bin.new 478 return 1 479 fi 480 done 481 done 482 fi 483 484 build_consul_post "${sdir}" "${extra_dir_name}" 485 if test $? -ne 0 486 then 487 err "ERROR: Failed postprocessing Consul binaries" 488 return 1 489 fi 490 return 0 491 }