push.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. set -e
  3. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  4. # TODO config file of some kind
  5. : ${LIBRARY:="$dir/../library"} # where we get the "library/*" repo manifests
  6. : ${LOGS:="$dir/logs"} # where "docker build" logs go
  7. : ${NAMESPACES:='library stackbrew'} # after we build, also tag each image as "NAMESPACE/repo:tag"
  8. LIBRARY="$(readlink -f "$LIBRARY")"
  9. LOGS="$(readlink -f "$LOGS")"
  10. # TODO actually log stuff
  11. # arg handling: all args are [repo|repo:tag]
  12. usage() {
  13. cat <<EOUSAGE
  14. usage: $0 [options] [repo[:tag] ...]
  15. ie: $0 --all
  16. $0 debian ubuntu:12.04
  17. This script pushes the specified docker images from LIBRARY that are built
  18. and tagged in the specified NAMESPACES.
  19. options:
  20. --help, -h, -? Print this help message
  21. --all Pushes all docker images built for the given NAMESPACES
  22. variables:
  23. # where to find repository manifest files
  24. LIBRARY="$LIBRARY"
  25. # which namespaces to push
  26. NAMESPACES="$NAMESPACES"
  27. EOUSAGE
  28. }
  29. opts="$(getopt -o 'h?' --long 'all,help' -- "$@" || { usage >&2 && false; })"
  30. eval set -- "$opts"
  31. buildAll=
  32. while true; do
  33. flag=$1
  34. shift
  35. case "$flag" in
  36. --help|-h|'-?')
  37. uasge
  38. exit 0
  39. ;;
  40. --all)
  41. buildAll=1
  42. ;;
  43. --)
  44. shift
  45. break
  46. ;;
  47. *)
  48. echo >&2 "error: unknown flag: $flag"
  49. usage >&2
  50. exit 1
  51. ;;
  52. esac
  53. done
  54. repos=()
  55. if [ "$buildAll" ]; then
  56. repos=( $(cd "$LIBRARY" && echo *) )
  57. fi
  58. repos+=( "$@" )
  59. repos=( "${repos[@]%/}" )
  60. if [ "${#repos[@]}" -eq 0 ]; then
  61. echo >&2 'error: no repos specified'
  62. usage >&2
  63. exit 1
  64. fi
  65. # this is to prevent parsing a manifest twice
  66. declare -A validRepo=()
  67. doPush() {
  68. for repoTag in "$@"; do
  69. for namespace in $NAMESPACES; do
  70. # TODO get rid of the echo and actually push
  71. echo "docker push" "$namespace/$repoTag"
  72. done
  73. done
  74. }
  75. for repoTag in "${repos[@]}"; do
  76. if [ "$repoTag" = 'MAINTAINERS' ]; then
  77. continue
  78. fi
  79. if [ "${validRepo[$repoTag]}" ]; then
  80. doPush "$repoTag"
  81. continue
  82. fi
  83. repo="${repoTag%:*}"
  84. # parse the repo library file
  85. IFS=$'\n'
  86. tagList=( $(awk -F ': +' '!/^#|^\s*$/ { print $1 }' "$LIBRARY/$repo") )
  87. unset IFS
  88. tags=()
  89. for tag in "${tagList[@]}"; do
  90. validRepo[$repo:$tag]=1
  91. tags+=( "$repo:$tag" )
  92. done
  93. if [ "$repo" = "$repoTag" ]; then
  94. doPush "${tags[@]}"
  95. elif [ "${validRepo[$repoTag]}" ]; then
  96. doPush "$repoTag"
  97. else
  98. echo >&2 "warning: specified repo is not in the LIBRARY, skipping: $repoTag"
  99. fi
  100. done