Browse Source

migrate the extra options from build.sh

Joe Ferguson 11 years ago
parent
commit
53b0d6e1a5
1 changed files with 54 additions and 51 deletions
  1. 54 51
      bashbrew/push.sh

+ 54 - 51
bashbrew/push.sh

@@ -1,15 +1,17 @@
 #!/bin/bash
 set -e
 
+# so we can have fancy stuff like !(pattern)
+shopt -s extglob
+
 dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
 
-# TODO config file of some kind
-: ${LIBRARY:="$dir/../library"} # where we get the "library/*" repo manifests
-: ${LOGS:="$dir/logs"} # where "docker build" logs go
-: ${NAMESPACES:='library stackbrew'} # after we build, also tag each image as "NAMESPACE/repo:tag"
+library="$dir/../library"
+logs="$dir/logs"
+namespaces='library stackbrew'
 
-LIBRARY="$(readlink -f "$LIBRARY")"
-LOGS="$(readlink -f "$LOGS")"
+library="$(readlink -f "$library")"
+logs="$(readlink -f "$logs")"
 # TODO actually log stuff
 
 # arg handling: all args are [repo|repo:tag]
@@ -20,45 +22,47 @@ usage: $0 [options] [repo[:tag] ...]
    ie: $0 --all
        $0 debian ubuntu:12.04
 
-   This script pushes the specified docker images from LIBRARY that are built
-   and tagged in the specified NAMESPACES.
+   This script pushes the specified docker images from library that are built
+   and tagged in the specified namespaces.
 
 options:
   --help, -h, -?     Print this help message
-  --all              Pushes all docker images built for the given NAMESPACES
-
-variables:
-  # where to find repository manifest files
-  LIBRARY="$LIBRARY"
-
-  # which namespaces to push
-  NAMESPACES="$NAMESPACES"
+  --all              Pushes all docker images built for the given namespaces
+  --no-push          Don't actually push the images to the Docker Hub
+  --library="$library"
+                     Where to find repository manifest files
+  --namespaces="$namespaces"
+                     Space separated list of namespaces to tag images in after
+                     building
 
 EOUSAGE
 }
 
-opts="$(getopt -o 'h?' --long 'all,help' -- "$@" || { usage >&2 && false; })"
+opts="$(getopt -o 'h?' --long 'all,help,no-push,library:,namespaces:' -- "$@" || { usage >&2 && false; })"
 eval set -- "$opts"
 
+doPush=1
 buildAll=
 while true; do
 	flag=$1
 	shift
 	case "$flag" in
 		--help|-h|'-?')
-			uasge
+			usage
 			exit 0
 			;;
-		--all)
-			buildAll=1
-			;;
+		--all) buildAll=1 ;;
+		--no-push) doPush= ;;
+		--library) library="$1" && shift ;;
+		--namespaces) namespaces="$1" && shift ;;
 		--)
-			shift
 			break
 			;;
 		*)
-			echo >&2 "error: unknown flag: $flag"
-			usage >&2
+			{
+				echo "error: unknown flag: $flag"
+				usage
+			} >&2
 			exit 1
 			;;
 	esac
@@ -66,10 +70,10 @@ done
 
 repos=()
 if [ "$buildAll" ]; then
-	repos=( $(cd "$LIBRARY" && echo *) )
+	repos=( "$library"/!(MAINTAINERS) )
 fi
-
 repos+=( "$@" )
+
 repos=( "${repos[@]%/}" )
 
 if [ "${#repos[@]}" -eq 0 ]; then
@@ -78,46 +82,45 @@ if [ "${#repos[@]}" -eq 0 ]; then
 	exit 1
 fi
 
-# this is to prevent parsing a manifest twice
-declare -A validRepo=()
-
-doPush() {
-	for repoTag in "$@"; do
-		for namespace in $NAMESPACES; do
-			# TODO get rid of the echo and actually push
-			echo "docker push" "$namespace/$repoTag"
-		done
-	done
-}
-
 for repoTag in "${repos[@]}"; do
-	if [ "$repoTag" = 'MAINTAINERS' ]; then
-		continue
-	fi
+	repo="${repoTag%%:*}"
+	tag="${repoTag#*:}"
+	[ "$repo" != "$tag" ] || tag=
 	
-	if [ "${validRepo[$repoTag]}" ]; then
-		doPush "$repoTag"
-		continue
+	if [ -f "$repo" ]; then
+		repoFile="$repo"
+		repo="$(basename "$repoFile")"
+		repoTag="${repo}${tag:+:$tag}"
+	else
+		repoFile="$library/$repo"
 	fi
 	
-	repo="${repoTag%:*}"
+	repoFile="$(readlink -f "$repoFile")"
 	
 	# parse the repo library file
 	IFS=$'\n'
-	tagList=( $(awk -F ': +' '!/^#|^\s*$/ { print $1 }' "$LIBRARY/$repo") )
+	tagList=( $(awk -F ': +' '!/^#|^\s*$/ { print $1 }' "$repoFile") )
 	unset IFS
 	
 	tags=()
 	for tag in "${tagList[@]}"; do
-		validRepo[$repo:$tag]=1
 		tags+=( "$repo:$tag" )
 	done
 	
+	pushes=()
 	if [ "$repo" = "$repoTag" ]; then
-		doPush "${tags[@]}"
-	elif [ "${validRepo[$repoTag]}" ]; then
-		doPush "$repoTag"
+		pushes=( "${tags[@]}" )
 	else
-		echo >&2 "warning: specified repo is not in the LIBRARY, skipping: $repoTag"
+		pushes+=( "$repoTag" )
 	fi
+	
+	for pushTag in "${pushes[@]}"; do
+		for namespace in $namespaces; do
+			if [ "$doPush" ]; then
+				docker push "$namespace/$pushTag"
+			else
+				echo "docker push" "$namespace/$pushTag"
+			fi
+		done
+	done
 done