Skip to content

Commit ea6a141

Browse files
committed
Allow using custom flags in build.sh
1 parent b94a555 commit ea6a141

File tree

1 file changed

+98
-35
lines changed

1 file changed

+98
-35
lines changed

test/build.sh

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,33 @@ NPD_STAGING_PATH=${NPD_STAGING_PATH:-"gs://node-problem-detector-staging"}
2626
NPD_STAGING_REGISTRY=${NPD_STAGING_REGISTRY:-"gcr.io/node-problem-detector-staging"}
2727
PR_ENV_FILENAME=${PR_ENV_FILENAME:-"pr.env"}
2828
CI_ENV_FILENAME=${CI_ENV_FILENAME:-"ci.env"}
29+
CI_CUSTOM_FLAGS_ENV_FILENAME=${CI_CUSTOM_FLAGS_ENV_FILENAME:-"ci-custom-flags.env"}
2930
ROOT_PATH=$(git rev-parse --show-toplevel)
3031
GCS_URL_PREFIX="https://storage.googleapis.com/"
3132

33+
3234
function print-help() {
33-
echo "Usage: build.sh [args...]"
34-
echo "Available arguments:"
35-
echo " pr [pull_number]: Build node-problem-detector for presubmit jobs and push to staging."
36-
echo " ci: Build node-problem-detector for CI jobs and push to staging."
37-
echo " get-ci-env: Download environment variable file from staging for CI job."
35+
echo "Usage: build.sh [flags] [command]"
36+
echo
37+
echo "Available flags:"
38+
echo " -p [PR_NUMBER] Specify the pull request number when building for a presubmit job."
39+
echo " -f Use custom flags."
40+
echo
41+
echo "Available commands:"
42+
echo " help Print this help message"
43+
echo " pr Build node-problem-detector for presubmit jobs and push to staging. Flag -p is required."
44+
echo " ci Build node-problem-detector for CI jobs and push to staging."
45+
echo " get-ci-env Download environment variable file from staging for CI job."
46+
echo " install-lib Install the libraries needed."
47+
echo
48+
echo "Examples:"
49+
echo " build.sh help"
50+
echo " build.sh -p [PR_NUMBER] pr"
51+
echo " build.sh -p [PR_NUMBER] -f pr"
52+
echo " build.sh ci"
53+
echo " build.sh get-ci-env"
54+
echo " build.sh -f get-ci-env"
55+
echo " build.sh install-lib"
3856
}
3957

4058
function get-version() {
@@ -65,17 +83,43 @@ export NODE_PROBLEM_DETECTOR_TAR_HASH=$(sha1sum ${ROOT_PATH}/node-problem-detect
6583
export EXTRA_ENVS=NODE_PROBLEM_DETECTOR_IMAGE=${REGISTRY}/node-problem-detector:${TAG}
6684
EOF
6785

86+
if [[ -n "${NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS:-}" ]]; then
87+
cat >> ${ROOT_PATH}/${env_file} <<EOF
88+
export NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS="${NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS}"
89+
EOF
90+
fi
91+
6892
echo "Written env file ${ROOT_PATH}/${env_file}:"
6993
cat ${ROOT_PATH}/${env_file}
7094
}
7195

96+
function build-npd-custom-flags() {
97+
local -r kube_home="/home/kubernetes"
98+
99+
local -r km_config="${kube_home}/node-problem-detector/config/kernel-monitor.json"
100+
local -r dm_config="${kube_home}/node-problem-detector/config/docker-monitor.json"
101+
local -r sm_config="${kube_home}/node-problem-detector/config/systemd-monitor.json"
102+
103+
local -r custom_km_config="${kube_home}/node-problem-detector/config/kernel-monitor-counter.json"
104+
local -r custom_dm_config="${kube_home}/node-problem-detector/config/docker-monitor-counter.json"
105+
local -r custom_sm_config="${kube_home}/node-problem-detector/config/systemd-monitor-counter.json"
106+
107+
flags="--v=2"
108+
flags+=" --logtostderr"
109+
flags+=" --system-log-monitors=${km_config},${dm_config},${sm_config}"
110+
flags+=" --custom-plugin-monitors=${custom_km_config},${custom_dm_config},${custom_sm_config}"
111+
flags+=" --port=20256"
112+
113+
export NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS=${flags}
114+
}
115+
72116
function build-pr() {
73-
local -r PR_NUMBER="${1}"
74117
if [[ -z "${PR_NUMBER}" ]]; then
75-
echo "ERROR: pull_number is missing."
118+
echo "ERROR: PR_NUMBER is missing."
76119
print-help
77120
exit 1
78121
fi
122+
79123
# Use the PR number and current time as the name, e.g., pr261-20190314.224907.862195792
80124
local -r PR="pr${PR_NUMBER}-$(date +%Y%m%d.%H%M%S.%N)"
81125
echo "Building for PR ${PR}..."
@@ -96,42 +140,61 @@ function build-ci() {
96140
export VERSION="$(get-version)-$(date +%Y%m%d.%H%M)"
97141
export TAG="${VERSION}"
98142
make push
143+
144+
# Create the env file with and without custom flags at the same time.
145+
build-npd-custom-flags
146+
write-env-file ${CI_CUSTOM_FLAGS_ENV_FILENAME}
147+
gsutil mv ${ROOT_PATH}/${CI_CUSTOM_FLAGS_ENV_FILENAME} ${UPLOAD_PATH}
148+
149+
export NODE_PROBLEM_DETECTOR_CUSTOM_FLAGS=""
99150
write-env-file ${CI_ENV_FILENAME}
100151
gsutil mv ${ROOT_PATH}/${CI_ENV_FILENAME} ${UPLOAD_PATH}
101152
}
102153

103154
function get-ci-env() {
104-
gsutil cp ${NPD_STAGING_PATH}/ci/${CI_ENV_FILENAME} ${ROOT_PATH}/${CI_ENV_FILENAME}
155+
if [[ "${USE_CUSTOM_FLAGS}" == "true" ]]; then
156+
gsutil cp ${NPD_STAGING_PATH}/ci/${CI_CUSTOM_FLAGS_ENV_FILENAME} ${ROOT_PATH}/${CI_CUSTOM_FLAGS_ENV_FILENAME}
157+
echo "Using env file ${ROOT_PATH}/${CI_CUSTOM_FLAGS_ENV_FILENAME}:"
158+
cat ${ROOT_PATH}/${CI_CUSTOM_FLAGS_ENV_FILENAME}
159+
else
160+
gsutil cp ${NPD_STAGING_PATH}/ci/${CI_ENV_FILENAME} ${ROOT_PATH}/${CI_ENV_FILENAME}
161+
echo "Using env file ${ROOT_PATH}/${CI_ENV_FILENAME}:"
162+
cat ${ROOT_PATH}/${CI_ENV_FILENAME}
163+
fi
164+
}
165+
166+
main() {
167+
cd ${ROOT_PATH}
105168

106-
echo "Using env file ${ROOT_PATH}/${CI_ENV_FILENAME}:"
107-
cat ${ROOT_PATH}/${CI_ENV_FILENAME}
169+
if [[ "${USE_CUSTOM_FLAGS}" == "true" ]]; then
170+
build-npd-custom-flags
171+
fi
172+
173+
# This is for the deprecated usage: build.sh pr [PR_NUMBER]
174+
if [[ -z "${PR_NUMBER}" ]]; then
175+
PR_NUMBER="${2:-}"
176+
fi
177+
178+
case ${1:-} in
179+
help) print-help;;
180+
pr) build-pr;;
181+
ci) build-ci;;
182+
get-ci-env) get-ci-env;;
183+
install-lib) install-lib;;
184+
*) print-help;;
185+
esac
108186
}
109187

110188

111-
cd ${ROOT_PATH}
189+
USE_CUSTOM_FLAGS="false"
190+
PR_NUMBER=""
112191

113-
if [[ "$#" -ne 1 && "$#" -ne 2 ]]; then
114-
echo "ERROR: Illegal number of parameters."
115-
print-help
116-
exit 1
117-
fi
118-
COMMAND="${1}"
192+
while getopts "fp:" opt; do
193+
case ${opt} in
194+
f) USE_CUSTOM_FLAGS="true";;
195+
p) PR_NUMBER="${OPTARG}";;
196+
esac
197+
done
198+
shift "$((OPTIND-1))"
119199

120-
if [[ "${COMMAND}" == "pr" ]]; then
121-
if [[ "$#" -ne 2 ]]; then
122-
echo "ERROR: pull_number is missing."
123-
print-help
124-
exit 1
125-
fi
126-
build-pr "${2}"
127-
elif [[ "${COMMAND}" == "ci" ]]; then
128-
build-ci
129-
elif [[ "${COMMAND}" == "get-ci-env" ]]; then
130-
get-ci-env
131-
elif [[ "${COMMAND}" == "install-lib" ]]; then
132-
install-lib
133-
else
134-
echo "ERROR: Invalid command."
135-
print-help
136-
exit 1
137-
fi
200+
main "$@"

0 commit comments

Comments
 (0)