bashbrew.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #!/bin/bash
  2. set -e
  3. # so we can have fancy stuff like !(pattern)
  4. shopt -s extglob
  5. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  6. library="$dir/../library"
  7. src="$dir/src"
  8. logs="$dir/logs"
  9. namespaces='library stackbrew'
  10. docker='docker'
  11. library="$(readlink -f "$library")"
  12. src="$(readlink -f "$src")"
  13. logs="$(readlink -f "$logs")"
  14. self="$(basename "$0")"
  15. usage() {
  16. cat <<EOUSAGE
  17. usage: $self [build|push] [options] [repo[:tag] ...]
  18. ie: $self build --all
  19. $self push debian ubuntu:12.04
  20. This script processes the specified Docker images using the corresponding
  21. repository manifest files.
  22. common options:
  23. --all Build all repositories specified in library
  24. --docker="$docker"
  25. Use a custom Docker binary
  26. --help, -h, -? Print this help message
  27. --library="$library"
  28. Where to find repository manifest files
  29. --logs="$logs"
  30. Where to store the build logs
  31. --namespaces="$namespaces"
  32. Space separated list of namespaces to tag images in after
  33. building
  34. build options:
  35. --no-build Don't build, print what would build
  36. --no-clone Don't pull/clone Git repositories
  37. --src="$src"
  38. Where to store cloned Git repositories (GOPATH style)
  39. push options:
  40. --no-push Don't push, print what would push
  41. EOUSAGE
  42. }
  43. # arg handling
  44. opts="$(getopt -o 'h?' --long 'all,docker:,help,library:,logs:,namespaces:,no-build,no-clone,no-push,src:' -- "$@" || { usage >&2 && false; })"
  45. eval set -- "$opts"
  46. doClone=1
  47. doBuild=1
  48. doPush=1
  49. buildAll=
  50. while true; do
  51. flag=$1
  52. shift
  53. case "$flag" in
  54. --all) buildAll=1 ;;
  55. --docker) docker="$1" && shift ;;
  56. --help|-h|'-?') usage && exit 0 ;;
  57. --library) library="$1" && shift ;;
  58. --logs) logs="$1" && shift ;;
  59. --namespaces) namespaces="$1" && shift ;;
  60. --no-build) doBuild= ;;
  61. --no-clone) doClone= ;;
  62. --no-push) doPush= ;;
  63. --src) src="$1" && shift ;;
  64. --) break ;;
  65. *)
  66. {
  67. echo "error: unknown flag: $flag"
  68. usage
  69. } >&2
  70. exit 1
  71. ;;
  72. esac
  73. done
  74. # which subcommand
  75. subcommand="$1"
  76. case "$subcommand" in
  77. build|push)
  78. shift
  79. ;;
  80. *)
  81. {
  82. echo "error: unknown subcommand: $1"
  83. usage
  84. } >&2
  85. exit 1
  86. ;;
  87. esac
  88. repos=()
  89. if [ "$buildAll" ]; then
  90. repos=( "$library"/!(MAINTAINERS) )
  91. fi
  92. repos+=( "$@" )
  93. repos=( "${repos[@]%/}" )
  94. if [ "${#repos[@]}" -eq 0 ]; then
  95. {
  96. echo 'error: no repos specified'
  97. usage
  98. } >&2
  99. exit 1
  100. fi
  101. # globals for handling the repo queue and repo info parsed from library
  102. queue=()
  103. declare -A repoGitRepo=()
  104. declare -A repoGitRef=()
  105. declare -A repoGitDir=()
  106. logDir="$logs/$subcommand-$(date +'%Y-%m-%d--%H-%M-%S')"
  107. mkdir -p "$logDir"
  108. latestLogDir="$logs/latest" # this gets shiny symlinks to the latest buildlog for each repo we've seen since the creation of the logs dir
  109. mkdir -p "$latestLogDir"
  110. didFail=
  111. # gather all the `repo:tag` combos to build
  112. for repoTag in "${repos[@]}"; do
  113. repo="${repoTag%%:*}"
  114. tag="${repoTag#*:}"
  115. [ "$repo" != "$tag" ] || tag=
  116. if [ "$repo" = 'http' -o "$repo" = 'https' ] && [[ "$tag" == //* ]]; then
  117. # IT'S A URL!
  118. repoUrl="$repo:${tag%:*}"
  119. repo="$(basename "$repoUrl")"
  120. if [ "${tag##*:}" != "$tag" ]; then
  121. tag="${tag##*:}"
  122. else
  123. tag=
  124. fi
  125. repoTag="${repo}${tag:+:$tag}"
  126. echo "$repoTag ($repoUrl)" >> "$logDir/repos.txt"
  127. cmd=( curl -sSL --compressed "$repoUrl" )
  128. else
  129. if [ -f "$repo" ]; then
  130. repoFile="$repo"
  131. repo="$(basename "$repoFile")"
  132. repoTag="${repo}${tag:+:$tag}"
  133. else
  134. repoFile="$library/$repo"
  135. fi
  136. repoFile="$(readlink -f "$repoFile")"
  137. echo "$repoTag ($repoFile)" >> "$logDir/repos.txt"
  138. cmd=( cat "$repoFile" )
  139. fi
  140. if [ "${repoGitRepo[$repoTag]}" ]; then
  141. queue+=( "$repoTag" )
  142. continue
  143. fi
  144. # parse the repo library file
  145. IFS=$'\n'
  146. repoTagLines=( $("${cmd[@]}" | grep -vE '^#|^\s*$') )
  147. unset IFS
  148. tags=()
  149. for line in "${repoTagLines[@]}"; do
  150. tag="$(echo "$line" | awk -F ': +' '{ print $1 }')"
  151. repoDir="$(echo "$line" | awk -F ': +' '{ print $2 }')"
  152. gitUrl="${repoDir%%@*}"
  153. commitDir="${repoDir#*@}"
  154. gitRef="${commitDir%% *}"
  155. gitDir="${commitDir#* }"
  156. if [ "$gitDir" = "$commitDir" ]; then
  157. gitDir=
  158. fi
  159. gitRepo="${gitUrl#*://}"
  160. gitRepo="${gitRepo%/}"
  161. gitRepo="${gitRepo%.git}"
  162. gitRepo="${gitRepo%/}"
  163. gitRepo="$src/$gitRepo"
  164. if [ "$subcommand" == 'build' ]; then
  165. if [ -z "$doClone" ]; then
  166. if [ "$doBuild" -a ! -d "$gitRepo" ]; then
  167. echo >&2 "error: directory not found: $gitRepo"
  168. exit 1
  169. fi
  170. else
  171. if [ ! -d "$gitRepo" ]; then
  172. mkdir -p "$(dirname "$gitRepo")"
  173. echo "Cloning $repo ($gitUrl) ..."
  174. git clone -q "$gitUrl" "$gitRepo"
  175. else
  176. # if we don't have the "ref" specified, "git fetch" in the hopes that we get it
  177. if ! (
  178. cd "$gitRepo"
  179. git rev-parse --verify "${gitRef}^{commit}" &> /dev/null
  180. ); then
  181. echo "Fetching $repo ($gitUrl) ..."
  182. (
  183. cd "$gitRepo"
  184. git fetch -q --all
  185. git fetch -q --tags
  186. )
  187. fi
  188. fi
  189. # disable any automatic garbage collection too, just to help make sure we keep our dangling commit objects
  190. ( cd "$gitRepo" && git config gc.auto 0 )
  191. fi
  192. fi
  193. repoGitRepo[$repo:$tag]="$gitRepo"
  194. repoGitRef[$repo:$tag]="$gitRef"
  195. repoGitDir[$repo:$tag]="$gitDir"
  196. tags+=( "$repo:$tag" )
  197. done
  198. if [ "$repo" = "$repoTag" ]; then
  199. # add all tags we just parsed
  200. queue+=( "${tags[@]}" )
  201. else
  202. queue+=( "$repoTag" )
  203. fi
  204. done
  205. set -- "${queue[@]}"
  206. while [ "$#" -gt 0 ]; do
  207. repoTag="$1"
  208. gitRepo="${repoGitRepo[$repoTag]}"
  209. gitRef="${repoGitRef[$repoTag]}"
  210. gitDir="${repoGitDir[$repoTag]}"
  211. shift
  212. if [ -z "$gitRepo" ]; then
  213. echo >&2 'Unknown repo:tag:' "$repoTag"
  214. didFail=1
  215. continue
  216. fi
  217. thisLog="$logDir/$subcommand-$repoTag.log"
  218. touch "$thisLog"
  219. ln -sf "$thisLog" "$latestLogDir/$(basename "$thisLog")"
  220. case "$subcommand" in
  221. build)
  222. echo "Processing $repoTag ..."
  223. if ! ( cd "$gitRepo" && git rev-parse --verify "${gitRef}^{commit}" &> /dev/null ); then
  224. echo "- failed; invalid ref: $gitRef"
  225. didFail=1
  226. continue
  227. fi
  228. dockerfilePath="$gitDir/Dockerfile"
  229. dockerfilePath="${dockerfilePath#/}" # strip leading "/" (for when gitDir is '') because "git show" doesn't like it
  230. if ! dockerfile="$(cd "$gitRepo" && git show "$gitRef":"$dockerfilePath")"; then
  231. echo "- failed; missing '$dockerfilePath' at '$gitRef' ?"
  232. didFail=1
  233. continue
  234. fi
  235. IFS=$'\n'
  236. froms=( $(echo "$dockerfile" | awk 'toupper($1) == "FROM" { print $2 ~ /:/ ? $2 : $2":latest" }') )
  237. unset IFS
  238. for from in "${froms[@]}"; do
  239. for queuedRepoTag in "$@"; do
  240. if [ "$from" = "$queuedRepoTag" ]; then
  241. # a "FROM" in this image is being built later in our queue, so let's bail on this image for now and come back later
  242. echo "- deferred; FROM $from"
  243. set -- "$@" "$repoTag"
  244. continue 3
  245. fi
  246. done
  247. done
  248. if [ "$doBuild" ]; then
  249. if ! (
  250. set -x
  251. cd "$gitRepo"
  252. git reset -q HEAD
  253. git checkout -q -- .
  254. git clean -dfxq
  255. git checkout -q "$gitRef" --
  256. cd "$gitRepo/$gitDir"
  257. "$dir/git-set-mtimes"
  258. ) &>> "$thisLog"; then
  259. echo "- failed 'git checkout'; see $thisLog"
  260. didFail=1
  261. continue
  262. fi
  263. if ! (
  264. set -x
  265. "$docker" build -t "$repoTag" "$gitRepo/$gitDir"
  266. ) &>> "$thisLog"; then
  267. echo "- failed 'docker build'; see $thisLog"
  268. didFail=1
  269. continue
  270. fi
  271. for namespace in $namespaces; do
  272. if ! (
  273. set -x
  274. "$docker" tag "$repoTag" "$namespace/$repoTag"
  275. ) &>> "$thisLog"; then
  276. echo "- failed 'docker tag'; see $thisLog"
  277. didFail=1
  278. continue
  279. fi
  280. done
  281. fi
  282. ;;
  283. push)
  284. for namespace in $namespaces; do
  285. if [ "$doPush" ]; then
  286. echo "Pushing $namespace/$repoTag..."
  287. if ! "$docker" push "$namespace/$repoTag" &> "$thisLog" < /dev/null; then
  288. echo >&2 "- $namespace/$repoTag failed to push; see $thisLog"
  289. fi
  290. else
  291. echo "$docker push" "$namespace/$repoTag"
  292. fi
  293. done
  294. ;;
  295. esac
  296. done
  297. [ -z "$didFail" ]