github.com/jojicluka/consul-api@v1.4.5/build-support/scripts/build-docker.sh (about)

     1  #!/bin/bash
     2  SCRIPT_NAME="$(basename ${BASH_SOURCE[0]})"
     3  pushd $(dirname ${BASH_SOURCE[0]}) > /dev/null
     4  SCRIPT_DIR=$(pwd)
     5  pushd ../.. > /dev/null
     6  SOURCE_DIR=$(pwd)
     7  popd > /dev/null
     8  pushd ../functions > /dev/null
     9  FN_DIR=$(pwd)
    10  popd > /dev/null
    11  popd > /dev/null
    12  
    13  source "${SCRIPT_DIR}/functions.sh"
    14  
    15  function usage {
    16  cat <<-EOF
    17  Usage: ${SCRIPT_NAME} (consul|ui|ui-legacy|static-assets) [<options ...>]
    18  
    19  Description:
    20     This script will build the various Consul components within docker containers
    21     and copy all the relevant artifacts out of the containers back to the source.
    22  
    23  Options:
    24     -i | --image      IMAGE       Alternative Docker image to run the build within.
    25                                 
    26     -s | --source     DIR         Path to source to build.
    27                                   Defaults to "${SOURCE_DIR}"
    28                                   
    29     -r | --refresh                Enables refreshing the docker image prior to building.
    30     
    31     -h | --help                   Print this help text.
    32  EOF
    33  }
    34  
    35  function err_usage {
    36     err "$1"
    37     err ""
    38     err "$(usage)"
    39  }
    40  
    41  function main {
    42     declare    image=
    43     declare    sdir="${SOURCE_DIR}"
    44     declare -i refresh=0
    45     declare    command=""
    46     
    47     while test $# -gt 0
    48     do
    49        case "$1" in
    50           -h | --help )
    51              usage
    52              return 0
    53              ;;
    54           -i | --image )
    55              if test -z "$2"
    56              then
    57                 err_usage "ERROR: option -i/--image requires an argument"
    58                 return 1
    59              fi
    60              
    61              image="$2"
    62              shift 2
    63              ;;
    64           -s | --source )
    65              if test -z "$2"
    66              then
    67                 err_usage "ERROR: option -s/--source requires an argument"
    68                 return 1
    69              fi
    70              
    71              if ! test -d "$2"
    72              then
    73                 err_usage "ERROR: '$2' is not a directory and not suitable for the value of -s/--source"
    74                 return 1
    75              fi
    76              
    77              sdir="$2"
    78              shift 2
    79              ;;
    80           -r | --refresh )
    81              refresh=1
    82              shift
    83              ;;
    84           consul | ui | ui-legacy | static-assets )
    85              command="$1"
    86              shift
    87              ;;
    88           * )
    89              err_usage "ERROR: Unknown argument '$1'"
    90              return 1
    91              ;;
    92        esac
    93     done
    94     
    95     if test -z "${command}"
    96     then
    97        err_usage "ERROR: No command specified"
    98        return 1
    99     fi
   100     
   101     case "${command}" in 
   102        consul )
   103           if is_set "${refresh}"
   104           then
   105              status_stage "==> Refreshing Consul build container image"
   106              export GO_BUILD_TAG="${image:-${GO_BUILD_CONTAINER_DEFAULT}}"
   107              refresh_docker_images "${sdir}" go-build-image || return 1
   108           fi
   109           status_stage "==> Building Consul"
   110           build_consul "${sdir}" "" "${image}" || return 1
   111           ;;
   112        static-assets )
   113           if is_set "${refresh}"
   114           then
   115              status_stage "==> Refreshing Consul build container image"
   116              export GO_BUILD_TAG="${image:-${GO_BUILD_CONTAINER_DEFAULT}}"
   117              refresh_docker_images "${sdir}" go-build-image || return 1
   118           fi
   119           status_stage "==> Building Static Assets"
   120           build_assetfs "${sdir}" "${image}" || return 1
   121           ;;
   122        ui )
   123           if is_set "${refresh}"
   124           then
   125              status_stage "==> Refreshing UI build container image"
   126              export UI_BUILD_TAG="${image:-${UI_BUILD_CONTAINER_DEFAULT}}"
   127              refresh_docker_images "${sdir}" ui-build-image || return 1
   128           fi
   129           status_stage "==> Building UI"
   130           build_ui "${sdir}" "${image}" || return 1
   131           status "==> UI Built with Version: $(ui_version ${sdir}/pkg/web_ui/v2/index.html), Logo: $(ui_logo_type ${sdir}/pkg/web_ui/v2/index.html)"
   132           ;;
   133        ui-legacy )
   134           if is_set "${refresh}"
   135           then
   136              status_stage "==> Refreshing Legacy UI build container image"
   137              export UI_LEGACY_BUILD_TAG="${image:-${UI_LEGACY_BUILD_CONTAINER_DEFAULT}}"
   138              refresh_docker_images "${sdir}" ui-legacy-build-image || return 1
   139           fi
   140           status_stage "==> Building UI"
   141           build_ui_legacy "${sdir}" "${image}" || return 1
   142           ;;
   143        * )
   144           err_usage "ERROR: Unknown command: '${command}'"
   145           return 1
   146           ;;
   147     esac
   148     
   149     return 0
   150  }
   151  
   152  main "$@"
   153  exit $?