release.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/usr/bin/env bash
  2. set -e
  3. # This script looks for bundles built by make.sh, and releases them on a
  4. # public S3 bucket.
  5. #
  6. # Bundles should be available for the VERSION string passed as argument.
  7. #
  8. # The correct way to call this script is inside a container built by the
  9. # official Dockerfile at the root of the Docker source code. The Dockerfile,
  10. # make.sh and release.sh should all be from the same source code revision.
  11. set -o pipefail
  12. # Print a usage message and exit.
  13. usage() {
  14. cat >&2 <<'EOF'
  15. To run, I need:
  16. - to be in a container generated by the Dockerfile at the top of the Docker
  17. repository;
  18. - to be provided with the name of an S3 bucket, in environment variable
  19. AWS_S3_BUCKET;
  20. - to be provided with AWS credentials for this S3 bucket, in environment
  21. variables AWS_ACCESS_KEY and AWS_SECRET_KEY;
  22. - the passphrase to unlock the GPG key which will sign the deb packages
  23. (passed as environment variable GPG_PASSPHRASE);
  24. - a generous amount of good will and nice manners.
  25. The canonical way to run me is to run the image produced by the Dockerfile: e.g.:"
  26. docker run -e AWS_S3_BUCKET=get-staging.docker.io \
  27. -e AWS_ACCESS_KEY=AKI1234... \
  28. -e AWS_SECRET_KEY=sEs4mE... \
  29. -e GPG_PASSPHRASE=m0resEs4mE... \
  30. -i -t --privileged \
  31. docker ./hack/release.sh
  32. EOF
  33. exit 1
  34. }
  35. [ "$AWS_S3_BUCKET" ] || usage
  36. [ "$AWS_ACCESS_KEY" ] || usage
  37. [ "$AWS_SECRET_KEY" ] || usage
  38. [ "$GPG_PASSPHRASE" ] || usage
  39. [ -d /go/src/github.com/dotcloud/docker ] || usage
  40. cd /go/src/github.com/dotcloud/docker
  41. [ -x hack/make.sh ] || usage
  42. RELEASE_BUNDLES=(
  43. binary
  44. cross
  45. tgz
  46. ubuntu
  47. )
  48. if [ "$1" != '--release-regardless-of-test-failure' ]; then
  49. RELEASE_BUNDLES=(
  50. test-unit test-integration
  51. "${RELEASE_BUNDLES[@]}"
  52. test-integration-cli
  53. )
  54. fi
  55. VERSION=$(cat VERSION)
  56. BUCKET=$AWS_S3_BUCKET
  57. # These are the 2 keys we've used to sign the deb's
  58. # release (get.docker.io)
  59. # GPG_KEY="36A1D7869245C8950F966E92D8576A8BA88D21E9"
  60. # test (test.docker.io)
  61. # GPG_KEY="740B314AE3941731B942C66ADF4FD13717AAD7D6"
  62. setup_s3() {
  63. # Try creating the bucket. Ignore errors (it might already exist).
  64. s3cmd mb s3://$BUCKET 2>/dev/null || true
  65. # Check access to the bucket.
  66. # s3cmd has no useful exit status, so we cannot check that.
  67. # Instead, we check if it outputs anything on standard output.
  68. # (When there are problems, it uses standard error instead.)
  69. s3cmd info s3://$BUCKET | grep -q .
  70. # Make the bucket accessible through website endpoints.
  71. s3cmd ws-create --ws-index index --ws-error error s3://$BUCKET
  72. }
  73. # write_to_s3 uploads the contents of standard input to the specified S3 url.
  74. write_to_s3() {
  75. DEST=$1
  76. F=`mktemp`
  77. cat > $F
  78. s3cmd --acl-public --mime-type='text/plain' put $F $DEST
  79. rm -f $F
  80. }
  81. s3_url() {
  82. case "$BUCKET" in
  83. get.docker.io|test.docker.io)
  84. echo "https://$BUCKET"
  85. ;;
  86. *)
  87. s3cmd ws-info s3://$BUCKET | awk -v 'FS=: +' '/http:\/\/'$BUCKET'/ { gsub(/\/+$/, "", $2); print $2 }'
  88. ;;
  89. esac
  90. }
  91. build_all() {
  92. if ! ./hack/make.sh "${RELEASE_BUNDLES[@]}"; then
  93. echo >&2
  94. echo >&2 'The build or tests appear to have failed.'
  95. echo >&2
  96. echo >&2 'You, as the release maintainer, now have a couple options:'
  97. echo >&2 '- delay release and fix issues'
  98. echo >&2 '- delay release and fix issues'
  99. echo >&2 '- did we mention how important this is? issues need fixing :)'
  100. echo >&2
  101. echo >&2 'As a final LAST RESORT, you (because only you, the release maintainer,'
  102. echo >&2 ' really knows all the hairy problems at hand with the current release'
  103. echo >&2 ' issues) may bypass this checking by running this script again with the'
  104. echo >&2 ' single argument of "--release-regardless-of-test-failure", which will skip'
  105. echo >&2 ' running the test suite, and will only build the binaries and packages. Please'
  106. echo >&2 ' avoid using this if at all possible.'
  107. echo >&2
  108. echo >&2 'Regardless, we cannot stress enough the scarcity with which this bypass'
  109. echo >&2 ' should be used. If there are release issues, we should always err on the'
  110. echo >&2 ' side of caution.'
  111. echo >&2
  112. exit 1
  113. fi
  114. }
  115. upload_release_build() {
  116. src="$1"
  117. dst="$2"
  118. latest="$3"
  119. echo
  120. echo "Uploading $src"
  121. echo " to $dst"
  122. echo
  123. s3cmd --follow-symlinks --preserve --acl-public put "$src" "$dst"
  124. if [ "$latest" ]; then
  125. echo
  126. echo "Copying to $latest"
  127. echo
  128. s3cmd --acl-public cp "$dst" "$latest"
  129. fi
  130. # get hash files too (see hash_files() in hack/make.sh)
  131. for hashAlgo in md5 sha256; do
  132. if [ -e "$src.$hashAlgo" ]; then
  133. echo
  134. echo "Uploading $src.$hashAlgo"
  135. echo " to $dst.$hashAlgo"
  136. echo
  137. s3cmd --follow-symlinks --preserve --acl-public --mime-type='text/plain' put "$src.$hashAlgo" "$dst.$hashAlgo"
  138. if [ "$latest" ]; then
  139. echo
  140. echo "Copying to $latest.$hashAlgo"
  141. echo
  142. s3cmd --acl-public cp "$dst.$hashAlgo" "$latest.$hashAlgo"
  143. fi
  144. fi
  145. done
  146. }
  147. release_build() {
  148. GOOS=$1
  149. GOARCH=$2
  150. binDir=bundles/$VERSION/cross/$GOOS/$GOARCH
  151. tgzDir=bundles/$VERSION/tgz/$GOOS/$GOARCH
  152. binary=docker-$VERSION
  153. tgz=docker-$VERSION.tgz
  154. latestBase=
  155. if [ -z "$NOLATEST" ]; then
  156. latestBase=docker-latest
  157. fi
  158. # we need to map our GOOS and GOARCH to uname values
  159. # see https://en.wikipedia.org/wiki/Uname
  160. # ie, GOOS=linux -> "uname -s"=Linux
  161. s3Os=$GOOS
  162. case "$s3Os" in
  163. darwin)
  164. s3Os=Darwin
  165. ;;
  166. freebsd)
  167. s3Os=FreeBSD
  168. ;;
  169. linux)
  170. s3Os=Linux
  171. ;;
  172. *)
  173. echo >&2 "error: can't convert $s3Os to an appropriate value for 'uname -s'"
  174. exit 1
  175. ;;
  176. esac
  177. s3Arch=$GOARCH
  178. case "$s3Arch" in
  179. amd64)
  180. s3Arch=x86_64
  181. ;;
  182. 386)
  183. s3Arch=i386
  184. ;;
  185. arm)
  186. s3Arch=armel
  187. # someday, we might potentially support mutliple GOARM values, in which case we might get armhf here too
  188. ;;
  189. *)
  190. echo >&2 "error: can't convert $s3Arch to an appropriate value for 'uname -m'"
  191. exit 1
  192. ;;
  193. esac
  194. s3Dir=s3://$BUCKET/builds/$s3Os/$s3Arch
  195. latest=
  196. latestTgz=
  197. if [ "$latestBase" ]; then
  198. latest="$s3Dir/$latestBase"
  199. latestTgz="$s3Dir/$latestBase.tgz"
  200. fi
  201. if [ ! -x "$binDir/$binary" ]; then
  202. echo >&2 "error: can't find $binDir/$binary - was it compiled properly?"
  203. exit 1
  204. fi
  205. if [ ! -f "$tgzDir/$tgz" ]; then
  206. echo >&2 "error: can't find $tgzDir/$tgz - was it packaged properly?"
  207. exit 1
  208. fi
  209. upload_release_build "$binDir/$binary" "$s3Dir/$binary" "$latest"
  210. upload_release_build "$tgzDir/$tgz" "$s3Dir/$tgz" "$latestTgz"
  211. }
  212. # Upload the 'ubuntu' bundle to S3:
  213. # 1. A full APT repository is published at $BUCKET/ubuntu/
  214. # 2. Instructions for using the APT repository are uploaded at $BUCKET/ubuntu/index
  215. release_ubuntu() {
  216. [ -e bundles/$VERSION/ubuntu ] || {
  217. echo >&2 './hack/make.sh must be run before release_ubuntu'
  218. exit 1
  219. }
  220. # Sign our packages
  221. dpkg-sig -g "--passphrase $GPG_PASSPHRASE" -k releasedocker \
  222. --sign builder bundles/$VERSION/ubuntu/*.deb
  223. # Setup the APT repo
  224. APTDIR=bundles/$VERSION/ubuntu/apt
  225. mkdir -p $APTDIR/conf $APTDIR/db
  226. s3cmd sync s3://$BUCKET/ubuntu/db/ $APTDIR/db/ || true
  227. cat > $APTDIR/conf/distributions <<EOF
  228. Codename: docker
  229. Components: main
  230. Architectures: amd64 i386
  231. EOF
  232. # Add the DEB package to the APT repo
  233. DEBFILE=bundles/$VERSION/ubuntu/lxc-docker*.deb
  234. reprepro -b $APTDIR includedeb docker $DEBFILE
  235. # Sign
  236. for F in $(find $APTDIR -name Release); do
  237. gpg -u releasedocker --passphrase $GPG_PASSPHRASE \
  238. --armor --sign --detach-sign \
  239. --output $F.gpg $F
  240. done
  241. # Upload keys
  242. s3cmd sync /.gnupg/ s3://$BUCKET/ubuntu/.gnupg/
  243. gpg --armor --export releasedocker > bundles/$VERSION/ubuntu/gpg
  244. s3cmd --acl-public put bundles/$VERSION/ubuntu/gpg s3://$BUCKET/gpg
  245. # Upload repo
  246. s3cmd --acl-public sync $APTDIR/ s3://$BUCKET/ubuntu/
  247. cat <<EOF | write_to_s3 s3://$BUCKET/ubuntu/index
  248. # Check that HTTPS transport is available to APT
  249. if [ ! -e /usr/lib/apt/methods/https ]; then
  250. apt-get update
  251. apt-get install -y apt-transport-https
  252. fi
  253. # Add the repository to your APT sources
  254. echo deb $(s3_url)/ubuntu docker main > /etc/apt/sources.list.d/docker.list
  255. # Then import the repository key
  256. apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
  257. # Install docker
  258. apt-get update ; apt-get install -y lxc-docker
  259. #
  260. # Alternatively, just use the curl-able install.sh script provided at $(s3_url)
  261. #
  262. EOF
  263. # Add redirect at /ubuntu/info for URL-backwards-compatibility
  264. rm -rf /tmp/emptyfile && touch /tmp/emptyfile
  265. s3cmd --acl-public --add-header='x-amz-website-redirect-location:/ubuntu/' --mime-type='text/plain' put /tmp/emptyfile s3://$BUCKET/ubuntu/info
  266. echo "APT repository uploaded. Instructions available at $(s3_url)/ubuntu"
  267. }
  268. # Upload binaries and tgz files to S3
  269. release_binaries() {
  270. [ -e bundles/$VERSION/cross/linux/amd64/docker-$VERSION ] || {
  271. echo >&2 './hack/make.sh must be run before release_binaries'
  272. exit 1
  273. }
  274. for d in bundles/$VERSION/cross/*/*; do
  275. GOARCH="$(basename "$d")"
  276. GOOS="$(basename "$(dirname "$d")")"
  277. release_build "$GOOS" "$GOARCH"
  278. done
  279. # TODO create redirect from builds/*/i686 to builds/*/i386
  280. cat <<EOF | write_to_s3 s3://$BUCKET/builds/index
  281. # To install, run the following command as root:
  282. curl -O $(s3_url)/builds/Linux/x86_64/docker-$VERSION && chmod +x docker-$VERSION && sudo mv docker-$VERSION /usr/local/bin/docker
  283. # Then start docker in daemon mode:
  284. sudo /usr/local/bin/docker -d
  285. EOF
  286. # Add redirect at /builds/info for URL-backwards-compatibility
  287. rm -rf /tmp/emptyfile && touch /tmp/emptyfile
  288. s3cmd --acl-public --add-header='x-amz-website-redirect-location:/builds/' --mime-type='text/plain' put /tmp/emptyfile s3://$BUCKET/builds/info
  289. if [ -z "$NOLATEST" ]; then
  290. echo "Advertising $VERSION on $BUCKET as most recent version"
  291. echo $VERSION | write_to_s3 s3://$BUCKET/latest
  292. fi
  293. }
  294. # Upload the index script
  295. release_index() {
  296. sed "s,url='https://get.docker.io/',url='$(s3_url)/'," hack/install.sh | write_to_s3 s3://$BUCKET/index
  297. }
  298. release_test() {
  299. if [ -e "bundles/$VERSION/test" ]; then
  300. s3cmd --acl-public sync bundles/$VERSION/test/ s3://$BUCKET/test/
  301. fi
  302. }
  303. setup_gpg() {
  304. # Make sure that we have our keys
  305. mkdir -p /.gnupg/
  306. s3cmd sync s3://$BUCKET/ubuntu/.gnupg/ /.gnupg/ || true
  307. gpg --list-keys releasedocker >/dev/null || {
  308. gpg --gen-key --batch <<EOF
  309. Key-Type: RSA
  310. Key-Length: 4096
  311. Passphrase: $GPG_PASSPHRASE
  312. Name-Real: Docker Release Tool
  313. Name-Email: docker@dotcloud.com
  314. Name-Comment: releasedocker
  315. Expire-Date: 0
  316. %commit
  317. EOF
  318. }
  319. }
  320. main() {
  321. build_all
  322. setup_s3
  323. setup_gpg
  324. release_binaries
  325. release_ubuntu
  326. release_index
  327. release_test
  328. }
  329. main
  330. echo
  331. echo
  332. echo "Release complete; see $(s3_url)"
  333. echo