-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-dsosds.sh
executable file
·176 lines (151 loc) · 3.54 KB
/
build-dsosds.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
USAGE=$( cat <<EOF
$(basename $0) -o,--dsosds-out OUT_DIR
[--dsosds-repo GIT_REPO] [--dsosds-branch GIT_REF]
Descriptions:
Build dsosds, a grafana plugin for accessing dsos. The OUT_DIR is the output
directory where the plugin files (and NodeJS dependencies) will be put in.
EOF
)
SCRIPT_DIR=$(dirname $0)
cd ${SCRIPT_DIR}
SCRIPT_DIR=${PWD} # get the full path
# Work from the top dir
cd ../
SRCDIR=${PWD}
source config.sh
if [[ -t 1 ]]; then
# Enable color for terminal
RST='\e[0m'
RED='\e[31m'
YLW='\e[33m'
fi
# Simple logging functions
_LOG() {
local _TS=$(date -Iseconds)
echo -e $(date -Iseconds) "$@"
}
_INFO() {
_LOG "${YLW}INFO:${RST}" "$@"
}
_ERROR() {
_LOG "${RED}ERROR:${RST}" "$@"
}
_ERROR_EXIT() {
_ERROR "$@"
exit -1
}
# Parameters
DSOSDS=${DSOSDS:-}
DSOSDS_REPO=${DSOSDS_REPO:-}
DSOSDS_BRANCH=${DSOSDS_BRANCH:-}
DSOSDS_BUILD_CONT=${DSOSDS_BUILD_CONT:-dsosds-build}
DSOSDS_BUILD_IMG=${DSOSDS_BUILD_IMG:-ovishpc/ldms-dev:${BUILD_TAG}}
DEBUG=
opt2var() {
local V=$1
V=${V#--}
V=${V//-/_}
V=${V^^}
echo $V
}
handle_opt() {
local NAME=$1
local L=$(opt2var $1)
local R=$2
[[ -n "$R" ]] || _ERROR_EXIT "$NAME requires an argument"
eval ${L}=${R}
}
# convert --arg=value to --arg "value"
ARGS=( )
for X in "$@"; do
if [[ "$X" == --*=* ]]; then
ARGS+=( "${X%%=*}" "${X#*=}" )
else
ARGS+=( "$X" )
fi
done
set -- "${ARGS[@]}"
while (($#)); do
case "$1" in
-o|--dsosds-out)
handle_opt --dsosds-out $2
shift
;;
--dsosds-repo|--dsosds-branch)
handle_opt $1 $2
shift
;;
--debug)
DEBUG=1
;;
-h|-?|--help)
cat <<<"$USAGE"
exit 0
;;
*)
_ERROR_EXIT "Unknown option: $1"
;;
esac
shift
done
if [[ -z "${DSOSDS_REPO}" ]]; then
_INFO "DSOSDS_REPO is not specified .. skip dsosds build"
exit 0
fi
[[ -n "${DSOSDS}" ]] || _ERROR_EXIT "'-o DSOSDS' option is required"
mkdir -p "${DSOSDS}"
DSOSDS=$( realpath ${DSOSDS} )
_INFO "==== Parameter Info ============================================="
for X in DSOSDS_{OUT,REPO,BRANCH} ; do
_INFO "$X: ${!X}"
done
_INFO "================================================================="
# Check if the container has already existed
CONT_PS=( $(docker ps -a --format '{{.Names}}' --filter "name=${DSOSDS_BUILD_CONT}") )
if ((${#CONT_PS})); then
# DSOSDS_BUILD_CONT has already been created
if [[ -z "${DEBUG}" ]]; then
_ERROR_EXIT "${DSOSDS_BUILD_CONT} build container existed. Please remove the container."
fi
# We are in debug mode
_INFO "${DSOSDS_BUILD_CONT} existed"
# determine if it is running
CONT_PS=( docker ps --format '{{.Names}}' --filter "name=${DSOSDS_BUILD_CONT}" )
if ((${#CONT_PS})); then
_INFO "${DSOSDS_BUILD_CONT} is running"
else
docker start ${DSOSDS_BUILD_CONT} \
|| _ERROR_EXIT "Cannot start container: ${DSOSDS_BUILD_CONT}"
fi
else
mkdir -p ${DSOSDS}
docker run -itd --name ${DSOSDS_BUILD_CONT} --hostname ${DSOSDS_BUILD_CONT} \
-v ${DSOSDS}:${DSOSDS}:rw \
${DSOSDS_BUILD_IMG} /bin/bash
fi
# prep npm temp build dir
#{ cat <<EOF
#mkdir /.npm
#chown ${UID}:${UID} /.npm
#EOF
#} | docker exec -i ${DSOSDS_BUILD_CONT} /bin/bash
{ cat <<EOF
cd ${DSOSDS}
rm -rf * .git/ .npm/
set -e
echo "==== Checking out ${DSOSDS_REPO} - ${DSOSDS_BRANCH} ===="
git init .
git remote add origin ${DSOSDS_REPO}
git fetch origin ${DSOSDS_BRANCH}
git checkout -b out FETCH_HEAD
echo "==== Installing the dependencies ===="
mkdir .npm
npm install --cache ${DSOSDS}/.npm --production
EOF
} | docker exec -u ${UID} -i ${DSOSDS_BUILD_CONT} /bin/bash
RC=$?
if [[ -z "${DEBUG}" ]]; then
docker rm -f ${DSOSDS_BUILD_CONT}
fi
exit $RC