1#!/usr/bin/env bash
2set -eo pipefail
3
4function help() {
5  local -r NAME=$(basename "$0")
6  local -r BOLD="\e[1m"
7  local -r RESET="\e[0m"
8  local -r help=$(cat << EOF
9${BOLD}NAME${RESET}
10\t$NAME - Build delivery using an ${BOLD}Centos 7 docker image${RESET}.
11${BOLD}SYNOPSIS${RESET}
12\t$NAME [-h|--help] [examples|dotnet|java|python|all|reset]
13${BOLD}DESCRIPTION${RESET}
14\tBuild Google OR-Tools deliveries.
15\tYou ${BOLD}MUST${RESET} define the following variables before running this script:
16\t* ORTOOLS_TOKEN: secret use to decrypt keys to sign .Net and Java packages.
17
18${BOLD}OPTIONS${RESET}
19\t-h --help: display this help text
20\tdotnet: build all .Net packages
21\tjava: build all Java packages
22\tpython: build all Pyhon packages
23\texamples: build examples archives
24\tall: build everything (default)
25
26${BOLD}EXAMPLES${RESET}
27Using export to define the ${BOLD}ORTOOLS_TOKEN${RESET} env and only building the Java packages:
28export ORTOOLS_TOKEN=SECRET
29$0 java
30
31note: the 'export ...' should be placed in your bashrc to avoid any leak
32of the secret in your bash history
33EOF
34)
35echo -e "$help"
36}
37
38function assert_defined(){
39  if [[ -z "${!1}" ]]; then
40    >&2 echo "Variable '${1}' must be defined"
41    exit 1
42  fi
43}
44
45function build_devel() {
46  # Check all prerequisite
47  command -v docker
48
49  # Clean
50  #docker image rm -f ortools:linux_devel 2>/dev/null
51  #docker image rm -f ortools:linux_env 2>/dev/null
52
53  cd "${RELEASE_DIR}" || exit 2
54
55  # Build env
56  docker build --tag ortools:linux_env \
57    --target=env \
58    -f "${RELEASE_DIR}/Dockerfile" "${RELEASE_DIR}"
59
60  # Build devel
61  assert_defined ORTOOLS_BRANCH
62  assert_defined ORTOOLS_SHA1
63
64  docker build --tag ortools:linux_devel \
65    --build-arg ORTOOLS_GIT_BRANCH="${ORTOOLS_BRANCH}" \
66    --build-arg ORTOOLS_GIT_SHA1="${ORTOOLS_SHA1}" \
67    --target=devel \
68    -f Dockerfile .
69}
70
71function build_delivery() {
72  assert_defined ORTOOLS_BRANCH
73  assert_defined ORTOOLS_SHA1
74  assert_defined ORTOOLS_TOKEN
75  assert_defined ORTOOLS_DELIVERY
76
77  # Clean
78  docker image rm -f ortools:linux_delivery 2>/dev/null
79
80  cd "${RELEASE_DIR}" || exit 2
81
82  # Build delivery
83  docker build --tag ortools:linux_delivery \
84    --build-arg ORTOOLS_GIT_BRANCH="${ORTOOLS_BRANCH}" \
85    --build-arg ORTOOLS_GIT_SHA1="${ORTOOLS_SHA1}" \
86    --build-arg ORTOOLS_TOKEN="${ORTOOLS_TOKEN}" \
87    --build-arg ORTOOLS_DELIVERY="${ORTOOLS_DELIVERY}" \
88    --target=delivery \
89    -f Dockerfile .
90}
91
92function build_examples() {
93  local -r ORTOOLS_DELIVERY=examples
94  build_delivery
95}
96
97function build_dotnet() {
98  local -r ORTOOLS_DELIVERY=dotnet
99  build_delivery
100
101  docker run --rm --init \
102  -w /root/or-tools \
103  -v "${ROOT_DIR}/export":/export \
104  ortools:linux_delivery /bin/bash -c \
105  "cp export/*nupkg /export/"
106}
107
108# Java build
109function build_java() {
110  local -r ORTOOLS_DELIVERY=java
111  build_delivery
112
113  docker run --rm --init \
114  -w /root/or-tools \
115  -v "${ROOT_DIR}/export":/export \
116  ortools:linux_delivery /bin/bash -c \
117  "cp export/*.jar* /export/"
118}
119
120function build_archive() {
121  local -r ORTOOLS_DELIVERY=archive
122  build_delivery
123}
124
125function build_python() {
126  local -r ORTOOLS_DELIVERY=python
127  build_delivery
128}
129
130# Main
131function main() {
132  case ${1} in
133    -h | --help)
134      help; exit ;;
135  esac
136
137  assert_defined ORTOOLS_TOKEN
138  echo "ORTOOLS_TOKEN: FOUND"
139
140  local -r ROOT_DIR="$(cd -P -- "$(dirname -- "$0")/../.." && pwd -P)"
141  echo "ROOT_DIR: '${ROOT_DIR}'"
142
143  local -r RELEASE_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
144  echo "RELEASE_DIR: '${RELEASE_DIR}'"
145
146  local -r ORTOOLS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
147  local -r ORTOOLS_SHA1=$(git rev-parse --verify HEAD)
148  build_devel
149
150  mkdir -p export
151  case ${1} in
152    cxx|dotnet|java|python|archive|examples)
153      "build_$1"
154      exit ;;
155    all)
156      build_dotnet
157      build_java
158      #build_python
159      build_examples
160      exit ;;
161    *)
162      >&2 echo "Target '${1}' unknown"
163      exit 1
164  esac
165  exit 0
166}
167
168main "${1:-all}"
169