summaryrefslogtreecommitdiffstats
path: root/scripts/compose_functions
blob: 9b55f880d235734458694d70f8e79d59c2ffec03 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
##
## Obtain various platform information
##

OS=`uname`
if [ "${OS}" != "Linux" ] ; then
	printf "'$0' is ONLY available on '${OS}'!\n"
	exit 255
fi

if [ -f "/etc/redhat-release" ] ; then
	DISTRIBUTION=`cat /etc/redhat-release | cut -c1-7`
	DIST_VERSION=`cat /etc/redhat-release | tr -d [:alpha:][:blank:][\(\)]`
	if [ "${DISTRIBUTION}" = "Fedora " ] ; then
		MESSAGE="[built for Fedora ${DIST_VERSION}]"
	elif [ "${DISTRIBUTION}" = "Red Hat" ] ; then
		MESSAGE="[built for Red Hat ${DIST_VERSION}]"
	else
		printf "'$0' is ONLY available on 'Fedora' or 'Red Hat' "
		printf "distributions!\n"
		exit 255
	fi
else
	printf "'$0' is ONLY available on 'Fedora' or 'Red Hat' distributions!\n"
	exit 255
fi


##
## Establish various shared variables
##

PKI_DIR="pki"
export PKI_DIR

PKI_BASE_DIR="${PKI_DIR}/base"
export PKI_BASE_DIR

PKI_DOGTAG_DIR="${PKI_DIR}/dogtag"
export PKI_DOGTAG_DIR

PKI_REDHAT_DIR="${PKI_DIR}/redhat"
export PKI_REDHAT_DIR

PKI_FILE_LIST="CMakeLists.txt COPYING CPackConfig.cmake ConfigureChecks.cmake DefineOptions.cmake README cmake_uninstall.cmake.in config.h.cmake pylint-build-scan.py dogtag.pylintrc tox.ini"
export PKI_FILE_LIST

PKI_CMAKE_DIR="cmake"
export PKI_CMAKE_DIR

PKI_BASE_MANIFEST="CMakeLists.txt"
export PKI_BASE_MANIFEST

PKI_DOGTAG_MANIFEST="CMakeLists.txt"
export PKI_DOGTAG_MANIFEST

PKI_REDHAT_MANIFEST="CMakeLists.txt"
export PKI_REDHAT_MANIFEST

RHEL="/rhel/"

##
## Usage statement
##

Usage()
{
	printf "\n"
	printf "Usage:  $0 [options] <target>\n\n"
	printf "        where <target> is one of the following:\n\n"
	printf "            srpm         - copies a spec file from local source,\n"
	printf "                           generates a tarball from local source,\n"
	printf "                           copies any relevant patches from the\n"
	printf "                           local 'patches' directory, and\n"
	printf "                           produces an SRPM\n"
	printf "                           [suitable for use by 'mock']\n\n"
	printf "            rpms         - copies a spec file from local source,\n"
	printf "                           generates a tarball from local source,\n"
	printf "                           copies any relevant patches from the\n"
	printf "                           local 'patches' directory, and\n"
	printf "                           produces an SRPM and one or more RPMS\n"
	printf "                           ${MESSAGE}\n\n"
	printf "            hybrid_srpm  - copies a spec file from local source,\n"
	printf "                           generates a tarball from local source,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM\n"
	printf "                           [suitable for use by 'mock']\n\n"
	printf "            hybrid_rpms  - copies a spec file from local source,\n"
	printf "                           generates a tarball from local source,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM and one or more RPMS\n"
	printf "                           ${MESSAGE}\n\n"
	printf "            patched_srpm - copies a spec file from local source,\n"
	printf "                           fetches a non-RHEL tarball from the spec's URL,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM\n"
	printf "                           [suitable for use by 'mock']\n\n"
	printf "            patched_rpms - copies a spec file from local source,\n"
	printf "                           fetches a non-RHEL tarball from the spec's URL,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM and one or more RPMS\n"
	printf "                           ${MESSAGE}\n\n"
	printf "            patched_rhel_srpm - copies a spec file from local source,\n"
	printf "                           fetches a RHEL tarball from the spec's URL,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM\n"
	printf "                           [suitable for use by 'mock']\n\n"
	printf "            patched_rhel_rpms - copies a spec file from local source,\n"
	printf "                           fetches a RHEL tarball from the spec's URL,\n"
	printf "                           fetches any relevant patches from the\n"
	printf "                           spec's URL, and\n"
	printf "                           produces an SRPM and one or more RPMS\n"
	printf "                           ${MESSAGE}\n\n"
	printf "Options:\n"
	printf "  --without-javadoc        do not build Javadoc RPMS\n\n"
	printf "  --without-server         do not build server RPMS\n\n"
	printf "  --createrepo=<repository_config> create a \n"
	printf "                           repository with the rpms built."
	printf "                           Provide the configuration file with the details. \n\n"
}


##
## Copy Specified Patches to SOURCES
##
Fetch_Patch_Files()
{
	if [ $# -ne 2 ] ; then
		Usage
		exit 255
	fi

	SPECFILE=$1
	TARGET_DIR=$2

	if [ ! -f ${SPECFILE} ] ; then
		printf "ERROR:  '${SPECFILE}' is missing!\n\n"
		Usage
		exit 255
	elif [ ! -d ${TARGET_DIR} ] ; then
		printf "ERROR:  '${TARGET_DIR}' does NOT exist!\n\n"
		Usage
		exit 255
	fi

	component_name_marker="Name"
	component_version_marker="Version"
	component_release_marker="Release"
	component_source_marker="Source"
	component_patch_marker="Patch"

	component_name=""
	component_version=""
	component_release=""
	component_source=""
	component_url=""
	component_patch=""

	exec < ${SPECFILE}
	while read line; do
		entry=`echo $line | cut -d: -f 1`
		if [ "${entry:0:4}" = "${component_name_marker}" ] ; then
			component_name=`echo $line | cut -d' ' -f 2`
		elif [ "${entry:0:7}" = "${component_version_marker}" ] ; then
			component_version=`echo $line | cut -d' ' -f 2`
		elif [ "${entry:0:7}" = "${component_release_marker}" ] ; then
			data=`echo $line | cut -d'%' -f 1`
			component_release=`echo $data | cut -d' ' -f 2`
		elif [ "${entry:0:6}" = "${component_source_marker}" ] ; then
			value=`echo $line | cut -d' ' -f 2`
			component_source=`echo $value | sed -e "s/\%{name}/${component_name}/g" -e "s/\%{version}/${component_version}/g" -e "s/\%{release}/${component_release}/g" -e "s/\%{?prerel}//g"`
			if [[ "$component_source" =~ "$RHEL" ]] ; then
				if [ ${FETCH_RHEL_PATCH_FILES} -ne 1 ] ; then
					# non-RHEL Source patch files are desired
					continue;
				fi
			else
				if [ ${FETCH_RHEL_PATCH_FILES} -eq 1 ] ; then
					# RHEL Source patch files are desired
					continue;
				fi
			fi
			component_url=`dirname ${component_source}`
		elif [ "${entry:0:5}" = "${component_patch_marker}" ] ; then
            if [ ${component_url} != "" ] ; then
				value=`echo $line | cut -d' ' -f 2`
				component_patch=${component_url}"/"`echo $value | sed -e "s/\%{name}/${component_name}/g" -e "s/\%{version}/${component_version}/g"`
				wget -q -O ${TARGET_DIR}/`basename ${component_patch}` ${component_patch}
				if [ $? -ne 0 ] ; then
					printf "ERROR:  Failed to download '${component_patch}'!\n\n"
					Usage
					exit 255
				elif [ ! -f ${TARGET_DIR}/`basename ${component_patch}` ] ; then
					printf "ERROR:  Failed to save '${TARGET_DIR}/`basename ${component_patch}`'!\n\n"
					Usage
					exit 255
				fi
			else
				printf "ERROR:  '${component_source_marker}' MUST be specified PRIOR to '${component_patch_marker}'!\n\n"
				Usage
				exit 255
			fi
		fi
	done
}


##
## Copy Specified Source Tarball to SOURCES
##
Fetch_Source_Tarball()
{
	if [ $# -ne 2 ] ; then
		Usage
		exit 255
	fi

	SPECFILE=$1
	TARGET_DIR=$2

	if [ ! -f ${SPECFILE} ] ; then
		printf "ERROR:  '${SPECFILE}' is missing!\n\n"
		Usage
		exit 255
	elif [ ! -d ${TARGET_DIR} ] ; then
		printf "ERROR:  '${TARGET_DIR}' does NOT exist!\n\n"
		Usage
		exit 255
	fi

	component_name_marker="Name"
	component_version_marker="Version"
	component_release_marker="Release"
	component_tarball_marker="Source"

	component_name=""
	component_version=""
	component_release=""
	component_tarball=""

	exec < ${SPECFILE}
	while read line; do
		entry=`echo $line | cut -d: -f 1`
		if [ "${entry:0:4}" = "${component_name_marker}" ] ; then
			component_name=`echo $line | cut -d' ' -f 2`
		elif [ "${entry:0:7}" = "${component_version_marker}" ] ; then
			component_version=`echo $line | cut -d' ' -f 2`
		elif [ "${entry:0:7}" = "${component_release_marker}" ] ; then
			data=`echo $line | cut -d'%' -f 1`
			component_release=`echo $data | cut -d' ' -f 2`
		elif [ "${entry:0:6}" = "${component_tarball_marker}" ] ; then
			value=`echo $line | cut -d' ' -f 2`
			component_tarball=`echo $value | sed -e "s/\%{name}/${component_name}/g" -e "s/\%{version}/${component_version}/g" -e "s/\%{release}/${component_release}/g" -e "s/\%{?prerel}//g"`
			if [[ "${component_tarball}" =~ "$RHEL" ]] ; then
				if [ ${FETCH_RHEL_SOURCE_TARBALL} -ne 1 ] ; then
					# non-RHEL Source URL is desired
					continue;
				fi
			else
				if [ ${FETCH_RHEL_SOURCE_TARBALL} -eq 1 ] ; then
					# RHEL Source URL is desired
					continue;
				fi
			fi
			wget -q -O ${TARGET_DIR}/`basename ${component_tarball}` ${component_tarball}
			if [ $? -ne 0 ] ; then
				printf "ERROR:  Failed to download '${component_tarball}'!\n\n"
				Usage
				exit 255
			elif [ ! -f ${TARGET_DIR}/`basename ${component_tarball}` ] ; then
				printf "ERROR:  Failed to save '${TARGET_DIR}/`basename ${component_tarball}`'!\n\n"
				Usage
				exit 255
			fi
		fi
	done
}

###
#   Create repository using the details in the config file ###
#   Function parameters - $1 - repository file path.
###

Create_repo_after_build()
{
	if [ $# -eq 0 ]; then
             echo "No repository configuration provided. Cannot create a repo."
             exit -1
	fi

	if [ ! -f $1 ]; then
             echo "$1 does not exist."
             exit -1
	fi

	. $1

	### Remove leading and trailing whitespaces ###
	repo_location=`echo $REPOSITORY_LOCATION |sed -e 's/^ *//g' -e 's/ *$//g'`
	destination=`echo $REPOSITORY_HOST|sed -e 's/^ *//g' -e 's/ *$//g'`
	uname=`echo $USERNAME |sed -e 's/^ *//g' -e 's/ *$//g'`
	pwd=`echo $PASSWORD |sed -e 's/^ *//g' -e 's/ *$//g'`

	if [ "$repo_location" != "" ] ; then
                if [ "$destination" != "" ] ; then
                        createrepo ./RPMS/noarch/
                        createrepo ./RPMS/x86_64/
                        createrepo ./SRPMS/
                        if [ $uname == "" ] ; then
                              echo "No username provided in the configuration file."
                              echo "Enter the username:"
                              read $uname
                        fi
                        if [ $pwd == "" ] ; then
                              echo "No password provided in the configuration file."
                              echo "Enter the password:"
                              read -s $pwd
                        fi
                        url="$uname@$destination:$repo_location"
                        echo $url
                        ./scp_the_repo.exp "RPMS/" $url $PASSWORD
                        ./scp_the_repo.exp "SRPMS/" $url $PASSWORD
                        if [ $? -ne 0 ]; then
                             echo "Cannot copy the repositories to $destination\n"
                             exit -1
                        fi
                        echo "Copied the repo to the location on the destination"
                        exit 0
                fi
                if [ ! -e $repo_location ] ; then
                       mkdir -p $repo_location
                fi
                cp -r RPMS SRPMS $repo_location
                createrepo $REPOSITORY_LOCATION/RPMS/noarch/
                createrepo $REPOSITORY_LOCATION/RPMS/x86_64/
                createrepo $REPOSITORY_LOCATION/SRPMS/
                echo "Build repository created at $repo_location."
                exit 0
	fi
	echo "Cannot create repository. $repo_location has empty value in repository.cfg"
}

##
## Check for command line argument validity
##

GETOPT=`getopt -o '' -l without-javadoc,without-server,work-dir:,createrepo: -n "$0" -- "$@"`

if [ $? != 0 ] ; then
    Usage
    exit 255
fi

eval set -- "$GETOPT"

while true ; do
    case "$1" in
        --without-javadoc) WITHOUT_JAVADOCS="--without javadoc" ; shift ;;
        --without-server) WITHOUT_SERVER="--without server" ; shift ;;
        --work-dir) WORK_DIR="$2" ; shift 2 ;;
        --createrepo) CREATEREPO="Y" ; REPO_CFG_FILE="$2" ; shift 2 ;;
        --) shift ; break ;;
        *) echo "$0: unrecognized option '$1'" 1>&2 ; exit 255 ;;
    esac
done

if [ $# -ne 1 ] ; then
	Usage
	exit 255
fi

OPTIONS="--define \"_topdir \`pwd\`\" $WITHOUT_JAVADOCS $WITHOUT_SERVER"

if	[ $1 = "srpm" ] ; then
	OPTIONS="$OPTIONS -bs"
	FETCH_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=0
elif [ $1 = "hybrid_srpm" ] ; then
	OPTIONS="$OPTIONS -bs"
	FETCH_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=1
elif [ $1 = "patched_srpm" ] ; then
	OPTIONS="$OPTIONS -bs"
	FETCH_SOURCE_TARBALL=1
	FETCH_RHEL_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=1
	FETCH_RHEL_PATCH_FILES=0
elif [ $1 = "patched_rhel_srpm" ] ; then
	OPTIONS="$OPTIONS -bs"
	FETCH_SOURCE_TARBALL=1
	FETCH_RHEL_SOURCE_TARBALL=1
	FETCH_PATCH_FILES=1
	FETCH_RHEL_PATCH_FILES=1
elif [ $1 = "rpms" ] ; then
	OPTIONS="$OPTIONS -ba"
	FETCH_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=0
elif [ $1 = "hybrid_rpms" ] ; then
	OPTIONS="$OPTIONS -ba"
	FETCH_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=1
elif [ $1 = "patched_rpms" ] ; then
	OPTIONS="$OPTIONS -ba"
	FETCH_SOURCE_TARBALL=1
	FETCH_RHEL_SOURCE_TARBALL=0
	FETCH_PATCH_FILES=1
	FETCH_RHEL_PATCH_FILES=0
elif [ $1 = "patched_rhel_rpms" ] ; then
	OPTIONS="$OPTIONS -ba"
	FETCH_SOURCE_TARBALL=1
	FETCH_RHEL_SOURCE_TARBALL=1
	FETCH_PATCH_FILES=1
	FETCH_RHEL_PATCH_FILES=1
else
	Usage
	exit 255
fi

RPMBUILD_CMD="rpmbuild $OPTIONS"
export RPMBUILD_CMD