github.com/jfsmig/consul@v1.4.5/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  
   372     if ! test -d "$1"
   373     then
   374        err "ERROR: '$1' is not a directory. build_consul must be called with the path to the top level source as the first argument'"
   375        return 1
   376     fi
   377  
   378     local sdir="$1"
   379     local build_os="$2"
   380     local build_arch="$3"
   381     local extra_dir_name="$4"
   382     local extra_dir=""
   383  
   384     if test -n "${extra_dir_name}"
   385     then
   386        extra_dir="${extra_dir_name}/"
   387     fi
   388  
   389     pushd ${sdir} > /dev/null
   390     if is_set "${CONSUL_DEV}"
   391     then
   392        if test -z "${XC_OS}"
   393        then
   394           XC_OS=$(go env GOOS)
   395        fi
   396  
   397        if test -z "${XC_ARCH}"
   398        then
   399           XC_ARCH=$(go env GOARCH)
   400        fi
   401     fi
   402     XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"}
   403     XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"}
   404  
   405     if test -z "${build_os}"
   406     then
   407        build_os="${XC_OS}"
   408     fi
   409  
   410     if test -z "${build_arch}"
   411     then
   412        build_arch="${XC_ARCH}"
   413     fi
   414  
   415     local use_gox=1
   416     is_set "${NOGOX}" && use_gox=0
   417     which gox > /dev/null || use_gox=0
   418  
   419     status_stage "==> Building Consul - OSes: ${build_os}, Architectures: ${build_arch}"
   420     mkdir pkg.bin.new 2> /dev/null
   421     if is_set "${use_gox}"
   422     then
   423        status "Using gox for concurrent compilation"
   424  
   425        CGO_ENABLED=0 gox \
   426           -os="${build_os}" \
   427           -arch="${build_arch}" \
   428           -osarch="!darwin/arm !darwin/arm64 !freebsd/arm"  \
   429           -ldflags="${GOLDFLAGS}" \
   430           -output "pkg.bin.new/${extra_dir}{{.OS}}_{{.Arch}}/consul" \
   431           -tags="${GOTAGS}" \
   432           .
   433  
   434        if test $? -ne 0
   435        then
   436           err "ERROR: Failed to build Consul"
   437           rm -r pkg.bin.new
   438           return 1
   439        fi
   440     else
   441        status "Building sequentially with go install"
   442        for os in ${build_os}
   443        do
   444           for arch in ${build_arch}
   445           do
   446              outdir="pkg.bin.new/${extra_dir}${os}_${arch}"
   447              osarch="${os}/${arch}"
   448              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"
   449              then
   450                 continue
   451              fi
   452  
   453              if test "${os}" == "solaris" -a "${arch}" != "amd64"
   454              then
   455                 continue
   456              fi
   457  
   458              echo "--->   ${osarch}"
   459  
   460  
   461              mkdir -p "${outdir}"
   462              GOBIN_EXTRA=""
   463              if test "${os}" != "$(go env GOHOSTOS)" -o "${arch}" != "$(go env GOHOSTARCH)"
   464              then
   465                 GOBIN_EXTRA="${os}_${arch}/"
   466              fi
   467              binname="consul"
   468              if [ $os == "windows" ];then
   469                  binname="consul.exe"
   470              fi
   471              CGO_ENABLED=0 GOOS=${os} GOARCH=${arch} go install -ldflags "${GOLDFLAGS}" -tags "${GOTAGS}" && cp "${MAIN_GOPATH}/bin/${GOBIN_EXTRA}${binname}" "${outdir}/${binname}"
   472              if test $? -ne 0
   473              then
   474                 err "ERROR: Failed to build Consul for ${osarch}"
   475                 rm -r pkg.bin.new
   476                 return 1
   477              fi
   478           done
   479        done
   480     fi
   481  
   482     build_consul_post "${sdir}" "${extra_dir_name}"
   483     if test $? -ne 0
   484     then
   485        err "ERROR: Failed postprocessing Consul binaries"
   486        return 1
   487     fi
   488     return 0
   489  }