gerrit-init.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. set -e
  6. http_port=8080
  7. ssh_port=29418
  8. while test $# -ne 0; do
  9. case "$1" in
  10. -v)
  11. version="$2"
  12. shift
  13. ;;
  14. -d)
  15. rundir="$2"
  16. shift
  17. ;;
  18. --http-port)
  19. http_port="$2"
  20. shift
  21. ;;
  22. --ssh-port)
  23. ssh_port="$2"
  24. shift
  25. ;;
  26. *)
  27. rundir="$1"
  28. ;;
  29. esac
  30. shift
  31. done
  32. if [ -z "$rundir" ]; then
  33. rundir=$(mktemp -d)
  34. fi
  35. this_dir=$(dirname $0)
  36. gerrit_exe="$this_dir/gerrit.war"
  37. account_id=101
  38. full_name='Test Account'
  39. maximum_page_size='25'
  40. password='test-password'
  41. preferred_email="test-username@test.org"
  42. registered_on=$(date '+%Y-%m-%d %H:%M:%S.000%:::z')
  43. username='test-username'
  44. # The python code below for picking the "latest" gerrit release is cribbed and
  45. # ported from the javascript at:
  46. #
  47. # http://gerrit-releases.storage.googleapis.com/index.html
  48. url='https://www.googleapis.com/storage/v1beta2/b/gerrit-releases/o?projection=noAcl'
  49. curl --retry 30 --ssl-reqd -s $url | python <(cat <<EOF
  50. # Receives Gerrit version via command line and reads json-encoded
  51. # text from stdin in the format:
  52. #
  53. # {
  54. # "items": [
  55. # {
  56. # "name": "gerrit-<version>.war",
  57. # "md5Hash": "<base64 encoded md5sum>",
  58. # },
  59. # {
  60. # "name": "gerrit-<version>.war",
  61. # "md5Hash": "<base64 encoded md5sum>",
  62. # },
  63. # ...
  64. # }
  65. #
  66. # ...and prints the name and md5sum of the corresponding *.war file.
  67. import json
  68. import re
  69. import sys
  70. requested_version = sys.argv[1] if len(sys.argv) > 1 else None
  71. # Disable using -rc versions. This is a temporary hack to avoid
  72. # picking up version 2.9-rc0, which requires java 7. These lines
  73. # should be un-commented after this bug is fixed:
  74. # https://code.google.com/p/chromium/issues/detail?id=346369
  75. #gerrit_re = re.compile('gerrit(?:-full)?-([0-9.]+)(-rc[0-9]+)?[.]war')
  76. gerrit_re = re.compile('gerrit(?:-full)?-([0-9.]+)[.]war')
  77. j = json.load(sys.stdin)
  78. items = [(x, gerrit_re.match(x['name'])) for x in j['items']]
  79. #items = [(x, m.group(1), m.group(2)) for x, m in items if m]
  80. items = [(x, m.group(1), '') for x, m in items if m]
  81. def _cmp(a, b):
  82. an = a[1].split('.')
  83. bn = b[1].split('.')
  84. while len(an) < len(bn):
  85. an.append('0')
  86. while len(bn) < len(an):
  87. bn.append('0')
  88. an.append(a[2][3:] if a[2] else '1000')
  89. bn.append(b[2][3:] if b[2] else '1000')
  90. for i in range(len(an)):
  91. if an[i] != bn[i]:
  92. return -1 if int(an[i]) > int(bn[i]) else 1
  93. return 0
  94. if requested_version:
  95. for info, version in items:
  96. if version == requested_version:
  97. print '"%s" "%s"' % (info['name'], info['md5Hash'])
  98. sys.exit(0)
  99. print >> sys.stderr, 'No such Gerrit version: %s' % requested_version
  100. sys.exit(1)
  101. items.sort(cmp=_cmp)
  102. for x in items:
  103. print '"%s" "%s"' % (x[0]['name'], x[0]['md5Hash'])
  104. sys.exit(0)
  105. EOF
  106. ) "$version" | xargs | while read name md5; do
  107. # Download the requested gerrit version if necessary, and verify the md5sum.
  108. target="$this_dir/$name"
  109. net_sum=$(echo -n $md5 | base64 -d | od -tx1 | head -1 | cut -d ' ' -f 2- |
  110. sed 's/ //g')
  111. if [ -f "$target" ]; then
  112. file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
  113. if [ "$file_sum" = "$net_sum" ]; then
  114. ln -sf "$name" "$gerrit_exe"
  115. break
  116. else
  117. rm -rf "$target"
  118. fi
  119. fi
  120. curl --retry 30 --ssl-reqd -s -o "$target" \
  121. "https://gerrit-releases.storage.googleapis.com/$name"
  122. file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
  123. if [ "$file_sum" != "$net_sum" ]; then
  124. echo "ERROR: md5sum mismatch when downloading $name" 1>&2
  125. rm -rf "$target"
  126. exit 1
  127. else
  128. ln -sf "$name" "$gerrit_exe"
  129. fi
  130. done
  131. if [ ! -e "$gerrit_exe" ]; then
  132. echo "ERROR: No $gerrit_exe file or link present, and unable " 1>&2
  133. echo " to download the latest version." 1>&2
  134. exit 1
  135. fi
  136. # By default, gerrit only accepts https connections, which is a good thing. But
  137. # for testing, it's convenient to enable plain http. Also, turn off all email
  138. # notifications.
  139. mkdir -p "${rundir}/etc"
  140. cat <<EOF > "${rundir}/etc/gerrit.config"
  141. [auth]
  142. type = DEVELOPMENT_BECOME_ANY_ACCOUNT
  143. gitBasicAuth = true
  144. [gerrit]
  145. canonicalWebUrl = http://$(hostname):${http_port}/
  146. [httpd]
  147. listenUrl = http://*:${http_port}/
  148. [sshd]
  149. listenAddress = *:${ssh_port}
  150. [sendemail]
  151. enable = false
  152. [container]
  153. javaOptions = -Duser.home=${rundir}/tmp
  154. EOF
  155. # Initialize the gerrit instance.
  156. java -jar "$gerrit_exe" init --no-auto-start --batch --install-plugin=download-commands -d "${rundir}"
  157. # Create SSH key pair for the first user.
  158. mkdir -p "${rundir}/tmp"
  159. ssh-keygen -t rsa -q -f "${rundir}/tmp/id_rsa" -N ""
  160. ssh_public_key="$(cat ${rundir}/tmp/id_rsa.pub)"
  161. # Set up the first user, with admin priveleges.
  162. cat <<EOF | java -jar "$gerrit_exe" gsql -d "${rundir}" > /dev/null
  163. INSERT INTO ACCOUNTS (FULL_NAME, MAXIMUM_PAGE_SIZE, PREFERRED_EMAIL, REGISTERED_ON, ACCOUNT_ID) VALUES ('${full_name}', ${maximum_page_size}, '${preferred_email}', '${registered_on}', ${account_id});
  164. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'gerrit:${username}');
  165. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'username:${username}');
  166. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EMAIL_ADDRESS, PASSWORD) VALUES (${account_id}, '${preferred_email}', '${password}');
  167. INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) VALUES (${account_id}, 1);
  168. INSERT INTO ACCOUNT_SSH_KEYS (ACCOUNT_ID, SSH_PUBLIC_KEY, VALID, SEQ) VALUES (${account_id}, '${ssh_public_key}', 'Y', 0);
  169. EOF
  170. # Create a netrc file to authenticate as the first user.
  171. cat <<EOF > "${rundir}/tmp/.netrc"
  172. machine localhost login ${username} password ${password}
  173. EOF
  174. # Create a .git-credentials file, to enable password-less push.
  175. cat <<EOF > "${rundir}/tmp/.git-credentials"
  176. http://${username}:${password}@localhost:${http_port}
  177. EOF
  178. cat <<EOF
  179. To start gerrit server:
  180. ${rundir}/bin/gerrit.sh start
  181. To use the REST API:
  182. curl --netrc-file ${rundir}/tmp/.netrc http://localhost:${http_port}/<endpoint>
  183. To use SSH API:
  184. ssh ${username}@localhost -p ${ssh_port} -i ${rundir}/tmp/id_rsa gerrit
  185. To enable 'git push' without a password prompt:
  186. git config credential.helper 'store --file=${rundir}/tmp/.git-credentials'
  187. To stop the server:
  188. ${rundir}/bin/gerrit.sh stop
  189. EOF