1#!/bin/bash
2
3[[ $IGNERR == 1 ]] || set -e
4[[ $VERBOSE == 1 ]] && set -x
5
6error() {
7	>&2 echo "$0: There are errors."
8	exit 1
9}
10
11if [[ -z $_Dbg_DEBUGGER_LEVEL ]]; then
12	trap error ERR
13fi
14
15export PYTHONWARNINGS=ignore
16
17#----------------------------------------------------------------------------------------------
18
19if [[ $1 == --help || $1 == help ]]; then
20	cat <<-END
21		Generate RediSearch distribution packages.
22
23		[ARGVARS...] pack.sh [--help|help] [<module-so-path>]
24
25		Argument variables:
26		VERBOSE=1     Print commands
27		IGNERR=1      Do not abort on error
28
29		RAMP=1              Generate RAMP package
30		DEPS=1              Generate dependency packages
31		RELEASE=1           Generate "release" packages (artifacts/release/)
32		SNAPSHOT=1          Generate "shapshot" packages (artifacts/snapshot/)
33		JUST_PRINT=1        Only print package names, do not generate
34
35		PACKAGE_NAME=name   Package stem name
36		VARIANT=name        Build variant (empty for standard packages)
37		BRANCH=name         Branch name for snapshot packages
38		GITSHA=1            Append Git SHA to shapshot package names
39
40		ARTDIR=dir          Directory in which packages are created
41		
42		RAMP_YAML=path      RAMP configuration file path
43		RAMP_ARGS=args      Extra arguments to RAMP
44
45	END
46	exit 0
47fi
48
49#----------------------------------------------------------------------------------------------
50
51HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
52ROOT=$HERE
53READIES=$ROOT/deps/readies
54. $READIES/shibumi/functions
55CURDIR="$PWD"
56
57#----------------------------------------------------------------------------------------------
58
59ARCH=$($READIES/bin/platform --arch)
60[[ $ARCH == x64 ]] && ARCH="x86_64"
61
62OS=$($READIES/bin/platform --os)
63[[ $OS == linux ]] && OS="Linux"
64
65OSNICK=$($READIES/bin/platform --osnick)
66[[ $OSNICK == trusty ]] && OSNICK=ubuntu14.04
67[[ $OSNICK == xenial ]] && OSNICK=ubuntu16.04
68[[ $OSNICK == bionic ]] && OSNICK=ubuntu18.04
69[[ $OSNICK == focal ]] && OSNICK=ubuntu20.04
70[[ $OSNICK == centos7 ]] && OSNICK=rhel7
71[[ $OSNICK == centos8 ]] && OSNICK=rhel8
72
73#----------------------------------------------------------------------------------------------
74
75MODULE_SO="$1"
76
77RAMP=${RAMP:-1}
78DEPS=${DEPS:-1}
79
80RELEASE=${RELEASE:-1}
81SNAPSHOT=${SNAPSHOT:-1}
82
83if [[ $JUST_PRINT != 1 ]]; then
84	[[ -z $ARTDIR ]] && { echo ARTDIR undefined; exit 1; }
85	ARTDIR=$(realpath $ARTDIR)
86fi
87
88#----------------------------------------------------------------------------------------------
89
90PACKAGE_NAME=${PACKAGE_NAME:-redisearch-oss}
91
92DEP_NAMES="debug"
93
94#----------------------------------------------------------------------------------------------
95
96pack_ramp() {
97	cd $ROOT
98
99	local platform="$OS-$OSNICK-$ARCH"
100	local stem=${PACKAGE_NAME}.${platform}
101
102	if [[ $SNAPSHOT == 0 ]]; then
103		local verspec=${SEMVER}${VARIANT}
104		local packdir=.
105		local s3base=""
106	else
107		local verspec=${BRANCH}${VARIANT}-snapshot
108		local packdir=snapshots
109		local s3base=snapshots/
110	fi
111
112	local fq_package=$stem.${verspec}.zip
113
114	[[ ! -d $ARTDIR/$packdir ]] && mkdir -p $ARTDIR/$packdir
115
116	local packfile=$ARTDIR/$packdir/$fq_package
117
118	local xtx_vars=""
119	for dep in $DEP_NAMES; do
120		eval "export NAME_${dep}=${PACKAGE_NAME}_${dep}"
121		local dep_fname=${PACKAGE_NAME}-${dep}.${platform}.${verspec}.tgz
122		eval "export PATH_${dep}=${s3base}${dep_fname}"
123		local dep_sha256="$ARTDIR/$packdir/${dep_fname}.sha256"
124		eval "export SHA256_${dep}=$(cat $dep_sha256)"
125
126		xtx_vars+=" -e NAME_$dep -e PATH_$dep -e SHA256_$dep"
127	done
128
129	if [[ -z $RAMP_YAML ]]; then
130		RAMP_YAML=$ROOT/ramp.yml
131	fi
132
133	python2 $READIES/bin/xtx \
134		$xtx_vars \
135		-e NUMVER -e SEMVER \
136		$RAMP_YAML > /tmp/ramp.yml
137
138	local ramp="$(command -v python2) -m RAMP.ramp"
139	rm -f /tmp/ramp.fname
140
141	# CURDIR is required so ramp will detect the right git commit
142	cd $CURDIR
143	$ramp pack -m /tmp/ramp.yml $RAMP_ARGS --packname-file /tmp/ramp.fname --verbose --debug \
144		-o $packfile $MODULE_SO >/tmp/ramp.err 2>&1 || true
145
146	if [[ ! -e $packfile ]]; then
147		>&2 echo "Error generating RAMP file:"
148		>&2 cat /tmp/ramp.err
149		exit 1
150	else
151		local packname=`cat /tmp/ramp.fname`
152	fi
153
154	echo "Created $packname"
155	cd $ROOT
156}
157
158#----------------------------------------------------------------------------------------------
159
160pack_deps() {
161	local dep="$1"
162
163	cd $ROOT
164
165	local depdir=$(cat $ARTDIR/$dep.dir)
166
167	local platform="$OS-$OSNICK-$ARCH"
168
169	local stem=${PACKAGE_NAME}-${dep}.${platform}
170	local fq_package=$stem.${SEMVER}${VARIANT}.tgz
171	local tar_path=$ARTDIR/$fq_package
172	local dep_prefix_dir=$(cat $ARTDIR/$dep.prefix)
173
174	{ cd $depdir ;\
175	  cat $ARTDIR/$dep.files | \
176	  xargs tar -c --sort=name --owner=root:0 --group=root:0 --mtime='UTC 1970-01-01' --transform "s,^,$dep_prefix_dir," 2>> /tmp/pack.err | \
177	  gzip -n - > $tar_path ; E=$?; } || true
178	sha256sum $tar_path | gawk '{print $1}' > $tar_path.sha256
179
180	mkdir -p $ARTDIR/snapshots
181	cd $ARTDIR/snapshots
182	if [[ ! -z $BRANCH ]]; then
183		local snap_package=$stem.${BRANCH}${VARIANT}-snapshot.tgz
184		ln -sf ../$fq_package $snap_package
185		ln -sf ../$fq_package.sha256 $snap_package.sha256
186	fi
187}
188
189#----------------------------------------------------------------------------------------------
190
191NUMVER=$(NUMERIC=1 $ROOT/getver)
192SEMVER=$($ROOT/getver)
193
194[[ -z $BRANCH ]] && BRANCH=${CIRCLE_BRANCH:-`git rev-parse --abbrev-ref HEAD`}
195BRANCH=${BRANCH//[^A-Za-z0-9._-]/_}
196if [[ $GITSHA == 1 ]]; then
197	GIT_COMMIT=$(git describe --always --abbrev=7 --dirty="+" 2>/dev/null || git rev-parse --short HEAD)
198	BRANCH="${BRANCH}-${GIT_COMMIT}"
199fi
200
201if [[ ! -z $VARIANT ]]; then
202	VARIANT=-${VARIANT}
203fi
204
205RELEASE_ramp=${PACKAGE_NAME}.$OS-$OSNICK-$ARCH.$SEMVER${VARIANT}.zip
206SNAPSHOT_ramp=${PACKAGE_NAME}.$OS-$OSNICK-$ARCH.${BRANCH}${VARIANT}-snapshot.zip
207
208if [[ $JUST_PRINT == 1 ]]; then
209	if [[ $RAMP == 1 ]]; then
210		[[ $RELEASE == 1 ]] && echo $RELEASE_ramp
211		[[ $SNAPSHOT == 1 ]] && echo $SNAPSHOT_ramp
212	fi
213	# if [[ $DEPS == 1 ]]; then
214	# 	[[ $RELEASE == 1 ]] && echo $RELEASE_deps
215	# 	[[ $SNAPSHOT == 1 ]] && echo $SNAPSHOT_deps
216	# fi
217	exit 0
218fi
219
220#----------------------------------------------------------------------------------------------
221
222mkdir -p $ARTDIR
223
224if [[ $DEPS == 1 ]]; then
225	# set up `debug` dep
226	echo $(dirname $(realpath $MODULE_SO)) > $ARTDIR/debug.dir
227	echo $(basename $(realpath $MODULE_SO)).debug > $ARTDIR/debug.files
228	echo "" > $ARTDIR/debug.prefix
229
230	echo "Building dependencies ..."
231	for dep in $DEP_NAMES; do
232		(pack_deps $dep)
233	done
234	echo "Done."
235fi
236
237#----------------------------------------------------------------------------------------------
238
239if [[ $RAMP == 1 ]]; then
240	if ! command -v redis-server > /dev/null; then
241		>&2 echo "$0: Cannot find redis-server. Aborting."
242		exit 1
243	fi
244
245	echo "Building RAMP files ..."
246
247	[[ -z $MODULE_SO ]] && >&2 echo "$0: Nothing to pack. Aborting." && exit 1
248	[[ ! -f $MODULE_SO ]] && >&2 echo "$0: $MODULE_SO does not exist. Aborting." && exit 1
249	MODULE_SO=$(realpath $MODULE_SO)
250
251	[[ $RELEASE == 1 ]] && SNAPSHOT=0 pack_ramp
252	[[ $SNAPSHOT == 1 ]] && pack_ramp
253
254	echo "Done."
255fi
256