summaryrefslogtreecommitdiff
blob: 193b1bddd7e1454ceb8fae47096c9eba8628c8c4 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/bin/bash
#
# rolltarball.sh - rolls a tarball for distribution
#
# Copyright 2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# Before commiting large changes, or if you don't completely understand a 
# small change you are about to commit, please consult the Primary Maintainer
# first to make sure it won't break things. Thanks!
# 
# Contributors:
#	Michael Stewart <vericgar@gentoo.org> (Primary Maintainer)
#	Christian Parpart <trapni@gentoo.org>
#
# Changes:
#	05-Jun-2005	Complete rewrite to clean up code
#
MYVERSION='$Revision$'
MYVERSION=${MYVERSION#* }
MYVERSION=${MYVERSION% *}

# ********** Begin functions **********

usage() {
	
	if [ -n "$1" ]
	then
		eerror $1
	else
		cat <<-USAGE_HEADER
			Gentoo Apache Tarball Generator
			Version: ${MYVERSION}
		USAGE_HEADER
	fi

# there are no tabs in the following!!!
	cat <<USAGE

Usage: $0 [options] ebuild

Where options is any of:
-a  --ask           Display what would be done, then ask to do it (default)
-A  --no-ask        Disable ask
-t  --color         Turn on color
-T  --no-color      Turn off color
-c  --copyto=path   Copy tarball to local path (default: /usr/portage/distfiles)
-C  --no-copy       Don't copy tarball
-d  --devspace      Upload to devspace (default)
-D  --no-devspace   Don't upload to devspace
-g  --digest        Create the digest (default)
-G  --no-digest     Don't create the digest
-e  --ebuild        Modify ebuild (default)
-E  --no-ebuild     Don't modify ebuild
-h  --help          Display this output
-m  --mirror        Upload to mirror://gentoo
-M  --no-mirror     Don't upload to mirror://gentoo (default)
-p  --pretend       Only display what would be done
    --quiet         Set verbosity to 0
-q                  Lower verbosity by 1
-s  --datestamp     Use alternate datestamp
-u  --user=username Gentoo Username (Required)
    --verbosity=n   Verbosity Level (0-4)
-v                  Increase verbosity by 1

For convienence, ~/.apache-rolltarball will be sourced so you can set any 
of the following instead of using the command line:

ASK            Ask before doing anything (0 = no, 1 = yes)
COLOR          Output in color (0 = no, 1 = yes)
COPYTO         Local path to copy tarball to (set to blank to disable copy)
G_USER         Gentoo Username
MOD_EBUILD     Modify ebuild (0 = no, 1 = yes)
UPLOAD_DEV     Whether to upload to devspace (0 = no, 1 = yes)
UPLOAD_MIRROR  Whether to upload to gentoo mirrors (0 = no, 1 = yes)
VERBOSE        Level of verbosity (0-3)

USAGE

	exit
}


eerror() {
	echo -e " ${BAD}*${NORMAL} ${*}"
}


die() {
	if [ "$#" -gt 0 ]
	then
		eerror ${*}
	fi
	exit 1
}


einfo() {
	if [ "${VERBOSE}" -ge "1" ]
	then
		echo -e " ${GOOD}*${NORMAL} ${*}"
	fi
}


ebegin() {
	if [ "${VERBOSE}" -ge "1" ]
	then
		echo -e " ${GOOD}*${NORMAL} ${*}..."
	fi
}


eend() {

	if [ "$#" -eq 0 ] || ([ -n "$1" ] && [ "$1" -eq 0 ])
	then
		if [ "${VERBOSE}" -ge "1" ]
		then
			echo -e "${ENDCOL}  ${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
		fi
	else
		retval=$1

		if [ "$#" -ge 2 ]
		then
			shift
			eerror "${*}"
		fi
		if [ "${VERBOSE}" -ge "1" ]
		then
			echo -e "${ENDCOL}  ${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
		fi	
		return ${retval}
	fi

}		


ewarn() {
	if [ "${VERBOSE}" -ge "2" ]
	then
		echo -e " ${WARN}*${NORMAL} ${*}"
	fi
}


edebug() {
	if [ "${VERBOSE}" -ge "4" ]
	then
		echo -e " ${HILITE}*${NORMAL} ${*}"
	fi
}


nocolor() {
	COLS="80"
	ENDCOL=" *****>>"
	GOOD=
	WARN=
	BAD=
	NORMAL=
	HILITE=
	BRACKET=
	COLOR=0
	edebug "Color disabled"
}


color() {
	COLS=$(stty size 2> /dev/null)
	COLS=${COLS#* }
	COLS=$((${COLS} - 7))
	ENDCOL=$'\e[A\e['${COLS}'G'
	GOOD=$'\e[32;01m'
	WARN=$'\e[33;01m'
	BAD=$'\e[31;01m'
	NORMAL=$'\e[0m'
	HILITE=$'\e[36;01m'
	BRACKET=$'\e[34;01m'
	COLOR=1
	edebug "Color enabled"
}	

# ********** End functions, begin primary code **********

# Defaults
# If you change these, change usage()
ASK=1
COLOR=1
COPYTO=/usr/portage/distfiles
DIGEST=1
G_USER=
MOD_EBUILD=1
PRETEND=0
UPLOAD_DEV=1
UPLOAD_MIRROR=0
VERBOSE=1

# load configuration
if [ -e ~/.apache-rolltarball ]
then
	. ~/.apache-rolltarball
	edebug "Loaded configuration from ~/.apache-rolltarball"
fi

if [ "${COLOR}" -eq "0" ]
then
	nocolor;
else
	color;
fi

# Process command line
until [ -z "$1" ]
do
	case "$1" in
		--*)
			# long options
			OPTFULL=${1/--/}
			OPT=${OPTFULL%=*}
			VALUE=${OPTFULL#*=}
			case "${OPT}" in
				ask)			ASK=1;;
				no-ask)			ASK=0;;
				color)			color;;
				no-color)		nocolor;;
				copyto)			COPYTO=${VALUE};;
				no-copy)		COPYTO=;;
				datestamp)		DATESTAMP=${VALUE};;
				devspace)		UPLOAD_DEV=1;;
				no-devspace)	UPLOAD_DEV=0;;
				digest)			DIGEST=1;;
				no-digest)		DIGEST=0;;
				ebuild)			MOD_EBUILD=1;;
				no-ebuild)		MOD_EBUILD=0;;
				help)			usage;;
				mirror)			UPLOAD_MIRROR=1;;
				no-mirror)		UPLOAD_MIRROR=0;;
				pretend)		PRETEND=1;;
				quiet)			VERBOSE=0;;
				user)			G_USER=${VALUE};;
				verbosity)		VERBOSE=${VALUE};;
				*)
					usage "Unknown option: --${OPT}"
				;;
			esac
			shift
		;;
		-*)
			# short options
			OPTLIST=${1/-/}
			shift
			while [ -n "${OPTLIST}" ]
			do
				OPT=${OPTLIST:0:1}
				OPTLIST=${OPTLIST#?}
				case "${OPT}" in
					a)	ASK=1;;
					A)	ASK=0;;
					c)	COPYTO=$1; shift;;
					C)	COPYTO=;;
					d)	UPLOAD_DEV=1;;
					D)	UPLOAD_DEV=0;;
					e)	MOD_EBUILD=1;;
					E)	MOD_EBUILD=0;;
					g)	DIGEST=1;;
					G)	DIGEST=0;;
					h)	usage;;
					m)	UPLOAD_MIRROR=1;;
					M)	UPLOAD_MIRROR=0;;
					p)	PRETEND=1;;
					q)	VERBOSE=$((${VERBOSE} - 1));;
					s)	DATESTAMP=$1; shift;;
					t)	color;;
					T)	nocolor;;
					u)	G_USER=$1; shift;;
					v)	VERBOSE=$((${VERBOSE} + 1));;
					*)
						usage "Unknown option: -${OPT}"
					;;
				esac
			done
		;;
		*)
			if [ -n "${EBUILD}" ]
			then
				usage "Only one ebuild can be specified"
			else
				EBUILD=$1
				shift
			fi
		;;
	esac
done

if [ -z "${EBUILD}" ]
then
	usage "You must specify an ebuild"
fi

if [ "${EBUILD##*.}" != "ebuild" ]
then
	usage "You must specify an ebuild"
fi

if [ ! -f ${EBUILD} ]
then
	die "Ebuild ${EBUILD} does not exist or is not a file"
fi

if [ "${VERBOSE}" -lt "0" ]
then
	VERBOSE=0
fi

if [ "${VERBOSE}" -gt "4" ]
then
	VERBOSE=4
fi

if [ "${VERBOSE}" -ge "3" ]
then
	edebug "Program output enabled"
	exec 9>&1
else
	edebug "Program output disabled"
	exec 9>/dev/null
fi

if [ "${ASK}" -eq "1" ]
then
	PRETEND=1
fi

if [ -z "${G_USER}" ]
then
# Autodetect not available for SVN
#	G_USER=$(cat CVS/Root)
#	G_USER=${G_USER/:ext:/}
#	G_USER=${G_USER%@*}
#	einfo "Detected Gentoo Developer: ${G_USER}"
	usage "Gentoo Developer Not specified!"
fi

edebug "Current configuration:"
edebug "  ASK: ${ASK}"
edebug "  COLOR: ${COLOR}"
edebug "  COPYTO: ${COPYTO}"
edebug "  DIGEST: ${DIGEST}"
edebug "  EBUILD: ${EBUILD}"
edebug "  G_USER: ${G_USER}"
edebug "  MOD_EBUILD: ${MOD_EBUILD}"
edebug "  PRETEND: ${PRETEND}"
edebug "  UPLOAD_DEV: ${UPLOAD_DEV}"
edebug "  UPLOAD_MIRROR: ${UPLOAD_MIRROR}"
edebug "  VERBOSE: ${VERBOSE}"

my_mtime=$(stat --format=%Y $0)

ebegin "Updating tree"
svn up >&9
eend $? "svn update failed!" || die

new_mtime=$(stat --format=%Y $0)
if [ "${my_mtime}" -ne "${new_mtime}" ]
then
	einfo "A new version of $0 is available"
	einfo "Please restart $0"
	die
fi

edebug "Detecting settings for tarball based on ebuild name"

EBUILD_BASE=$(basename ${EBUILD})
EBUILD_NAME=${EBUILD_BASE/-[0-9]*/}
TB_VER=${EBUILD_BASE/${EBUILD_NAME}-/}
TB_VER=${TB_VER/.ebuild/}
DATESTAMP=${DATESTAMP:-$(date +%Y%m%d)}

case ${EBUILD_NAME} in
	apache)
		TREE=${TB_VER%.*}
		TB=gentoo-apache-${TB_VER}-${DATESTAMP}.tar.bz2
		TB_DIR=gentoo-apache-${TB_VER}
		;;
	gentoo-webroot-default)
		TREE=gentoo-webroot-default
		TB=gentoo-webroot-default-${TB_VER}.tar.bz2
		TB_DIR=gentoo-webroot-default-${TB_VER}
		MOD_EBUILD=0
		UPLOAD_MIRROR=1
		UPLOAD_DEV=0
		einfo "Have you version-bumped gentoo-webroot-default?"
		einfo "If not, press Ctrl-C now and do so, specifying the new ebuild"
		sleep 5
		;;
	*)	
		die "Don't know what to do for ebuild: ${EBUILD_NAME}";;
esac

edebug "  TREE: ${TREE}"
edebug "  TB: ${TB}"
edebug "  TB_DIR: ${TB_DIR}"

# simply returns true or false based on whether we are in pretend mod or not
pretend() {
	if [ "${PRETEND}" -eq 1 ]
	then
		true
		return $?
	else
		false
		return $?
	fi
}


# we put all this in a function so that we can simply call it again when
# the result of asking is yes.
CURTIME=`date -u`
build_tarball() {

	pretend && einfo "Actions to be taken:"

	pretend && einfo "  Create ${TB} from ${TREE}/"
	pretend || {
		ebegin "Creating ${TB} from ${TREE}/"
		edebug "Copy recursive, hard-link where possible: ${TREE} -> ${TB_DIR}"
		cp -Rl ${TREE} ${TB_DIR} >&9 || eend $? "Copy failed" || die
		edebug "Create ${TB_DIR}/DATESTAMP with current time (${CURTIME}), packager (${G_USER}), and script version (${MYVERSION})"
		echo ${CURTIME} > ${TB_DIR}/DATESTAMP
		echo "Packaged by ${G_USER}" >> ${TB_DIR}/DATESTAMP
		echo "$0 v${MYVERSION}" >> ${TB_DIR}/DATESTAMP
		edebug "Create bzip2-ed tarball ${TB} from ${TB_DIR} excluding .svn"
		tar --create --bzip2 --verbose --exclude=.svn --exclude=*~ --file ${TB} ${TB_DIR} >&9
		eend $? "Tarball creation failed" || die
		edebug "Remove temporary directory" 
		rm -rf ${TB_DIR} || ewarn "Couldn't clean up, manually remove ${TB_DIR}/"
	}

	if [ -n "${COPYTO}" ]
	then
		if [ -d ${COPYTO} -a -w ${COPYTO} ]
		then
			pretend && einfo "  Copy ${TB} to ${COPYTO}"
			pretend || {
				ebegin "Copying ${TB} to ${COPYTO}"
				cp ${TB} ${COPYTO} >&9
				eend $?
			}
		else
			ewarn "${COPYTO} is not a directory or not writable, skipping copy"
		fi
	else
		edebug "Copy not enabled"
	fi

	if [ "${UPLOAD_DEV}" -eq 1 ]
	then
		pretend && einfo "  Upload ${TB} to"
		pretend && einfo "      http://dev.gentoo.org/~${G_USER}/dist/apache/"
		pretend || {
			einfo "Uploading ${TB} to"
			ebegin "    http://dev.gentoo.org/~${G_USER}/dist/apache/"
			edebug "Making directories on dev.gentoo.org: ~/public_html/dist/apache"
			ssh ${G_USER}@dev.gentoo.org 'mkdir -pm 0755 ~/public_html/dist/apache/' >&9 || eend $? "Failed to make directories" || die
			
			edebug "scp ${TB} ${G_USER}@dev.gentoo.org:~/public_html/dist/apache"
			scp ${TB} ${G_USER}@dev.gentoo.org:~/public_html/dist/apache >&9 || eend $? "Failed to upload" || die
			edebug "Setting tarball permissions to 0644"
			ssh ${G_USER}@dev.gentoo.org "chmod 0755 ~/public_html/dist ~/public_html/dist/apache" && ssh ${G_USER}@dev.gentoo.org "chmod 0644 ~/public_html/dist/apache/${TB}"
			eend $? "Failed to set permissions" || die
		}
	else
		edebug "Upload to devspace not enabled"
	fi

	if [ "${UPLOAD_MIRROR}" -eq 1 ]
	then
		pretend && einfo "  Upload ${TB} to mirror://gentoo/"
		pretend || {
			ebegin "Uploading ${TB} to mirror://gentoo/"
			edebug "scp ${TB} ${G_USER}@dev.gentoo.org:/space/distfiles-local"
			scp ${TB} ${G_USER}@dev.gentoo.org:/space/distfiles-local >&9 || eend $? "Failed to upload" || die
			edebug "Setting tarball permissions to 0644"
			ssh ${G_USER}@dev.gentoo.org "chmod 0644 /space/distfiles-local/${TB}"
			eend $? "Failed to set permissions" || die
			einfo "Please remember it can take up to 24 hours for full propogation"
			einfo "Make sure the tarball is available before marking a package stable"
		}
	else
		edebug "Upload to mirrors not enabled"
	fi

	if [ "${MOD_EBUILD}" -eq 1 ]
	then
		if [ -r ${EBUILD} ]
		then
			pretend && einfo "  Update GENTOO_PATCHSTAMP and GENTOO_DEVELOPER"
			pretend || {
				ebegin "Updating GENTOO_PATCHSTAMP and GENTOO_DEVELOPER"
				sed -i -e "s/GENTOO_PATCHSTAMP=\".*\"/GENTOO_PATCHSTAMP=\"${DATESTAMP}\"/" ${EBUILD} && 
				sed -i -e "s/GENTOO_DEVELOPER=\".*\"/GENTOO_DEVELOPER=\"${G_USER}\"/" ${EBUILD}
				eend $? "Failed to modify ebuild" || {
					einfo "It's highly recommended that you delete the ebuild"
					einfo "and cvs up and then modify the ebuild manually."
					die
				}
			}
		else
			ewarn "Unable to write to ebuild - skipping modification"
		fi
	else
		edebug "Modify ebuild not enabled"
	fi
	
	if [ "${DIGEST}" -eq 1 ]
	then
		pretend && einfo "  Regenerate digests"
		pretend || {
			ebegin "Regenerating digests"
			ebuild --force ${EBUILD} manifest >&9
			eend $?
		}
	else
		edebug "Regenerate digest not enabled"
	fi
	
	pretend && einfo "No actions actually taken"
	if [ "${ASK}" -eq 1 ]
	then
		einfo "Would you like to perform the above actions?"
		echo -n "Type 'Yes' or 'No'> "
		read ask_in
		if [ "${ask_in}" == "Yes" -o "${ask_in}" == "yes" ]
		then
			ASK=0
			PRETEND=0
			build_tarball
		fi
	fi

}

build_tarball

# vim:ai:noet:ts=4:nowrap