gerrit-init.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/v1/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. from __future__ import print_function
  68. import json
  69. import re
  70. import sys
  71. requested_version = sys.argv[1] if len(sys.argv) > 1 else None
  72. # Disable using -rc versions. This is a temporary hack to avoid
  73. # picking up version 2.9-rc0, which requires java 7. These lines
  74. # should be un-commented after this bug is fixed:
  75. # https://code.google.com/p/chromium/issues/detail?id=346369
  76. #gerrit_re = re.compile('gerrit(?:-full)?-([0-9.]+)(-rc[0-9]+)?[.]war')
  77. gerrit_re = re.compile('gerrit(?:-full)?-([0-9.]+)[.]war')
  78. j = json.load(sys.stdin)
  79. items = [(x, gerrit_re.match(x['name'])) for x in j['items']]
  80. #items = [(x, m.group(1), m.group(2)) for x, m in items if m]
  81. items = [(x, m.group(1), '') for x, m in items if m]
  82. def _cmp(a, b):
  83. an = a[1].split('.')
  84. bn = b[1].split('.')
  85. while len(an) < len(bn):
  86. an.append('0')
  87. while len(bn) < len(an):
  88. bn.append('0')
  89. an.append(a[2][3:] if a[2] else '1000')
  90. bn.append(b[2][3:] if b[2] else '1000')
  91. for i in range(len(an)):
  92. if an[i] != bn[i]:
  93. return -1 if int(an[i]) > int(bn[i]) else 1
  94. return 0
  95. if requested_version:
  96. for info, version in items:
  97. if version == requested_version:
  98. print('"%s" "%s"' % (info['name'], info['md5Hash']))
  99. sys.exit(0)
  100. print('No such Gerrit version: %s' % requested_version, file=sys.stderr)
  101. sys.exit(1)
  102. items.sort(cmp=_cmp)
  103. for x in items:
  104. print('"%s" "%s"' % (x[0]['name'], x[0]['md5Hash']))
  105. sys.exit(0)
  106. EOF
  107. ) "$version" | xargs | while read name md5; do
  108. # Download the requested gerrit version if necessary, and verify the md5sum.
  109. target="$this_dir/$name"
  110. net_sum=$(echo -n $md5 | base64 -d | od -tx1 | head -1 | cut -d ' ' -f 2- |
  111. sed 's/ //g')
  112. if [ -f "$target" ]; then
  113. file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
  114. if [ "$file_sum" = "$net_sum" ]; then
  115. ln -sf "$name" "$gerrit_exe"
  116. break
  117. else
  118. rm -rf "$target"
  119. fi
  120. fi
  121. curl --retry 30 --ssl-reqd -s -o "$target" \
  122. "https://gerrit-releases.storage.googleapis.com/$name"
  123. file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
  124. if [ "$file_sum" != "$net_sum" ]; then
  125. echo "ERROR: md5sum mismatch when downloading $name" 1>&2
  126. rm -rf "$target"
  127. exit 1
  128. else
  129. ln -sf "$name" "$gerrit_exe"
  130. fi
  131. done
  132. if [ ! -e "$gerrit_exe" ]; then
  133. echo "ERROR: No $gerrit_exe file or link present, and unable " 1>&2
  134. echo " to download the latest version." 1>&2
  135. exit 1
  136. fi
  137. # By default, gerrit only accepts https connections, which is a good thing. But
  138. # for testing, it's convenient to enable plain http. Also, turn off all email
  139. # notifications.
  140. mkdir -p "${rundir}/etc"
  141. cat <<EOF > "${rundir}/etc/gerrit.config"
  142. [auth]
  143. type = DEVELOPMENT_BECOME_ANY_ACCOUNT
  144. gitBasicAuth = true
  145. [gerrit]
  146. canonicalWebUrl = http://$(hostname):${http_port}/
  147. [httpd]
  148. listenUrl = http://*:${http_port}/
  149. [sshd]
  150. listenAddress = *:${ssh_port}
  151. [sendemail]
  152. enable = false
  153. [container]
  154. javaOptions = -Duser.home=${rundir}/tmp
  155. EOF
  156. # Initialize the gerrit instance.
  157. java -jar "$gerrit_exe" init --no-auto-start --batch --install-plugin=download-commands -d "${rundir}"
  158. # Create SSH key pair for the first user.
  159. mkdir -p "${rundir}/tmp"
  160. ssh-keygen -t rsa -q -f "${rundir}/tmp/id_rsa" -N ""
  161. ssh_public_key="$(cat ${rundir}/tmp/id_rsa.pub)"
  162. # Set up the first user, with admin privileges.
  163. cat <<EOF | java -jar "$gerrit_exe" gsql -d "${rundir}" > /dev/null
  164. 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});
  165. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'gerrit:${username}');
  166. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'username:${username}');
  167. INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EMAIL_ADDRESS, PASSWORD) VALUES (${account_id}, '${preferred_email}', '${password}');
  168. INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) VALUES (${account_id}, 1);
  169. INSERT INTO ACCOUNT_SSH_KEYS (ACCOUNT_ID, SSH_PUBLIC_KEY, VALID, SEQ) VALUES (${account_id}, '${ssh_public_key}', 'Y', 0);
  170. EOF
  171. # Create a netrc file to authenticate as the first user.
  172. cat <<EOF > "${rundir}/tmp/.netrc"
  173. machine localhost login ${username} password ${password}
  174. EOF
  175. # Create a .git-credentials file, to enable password-less push.
  176. cat <<EOF > "${rundir}/tmp/.git-credentials"
  177. http://${username}:${password}@localhost:${http_port}
  178. EOF
  179. cat <<EOF
  180. To start gerrit server:
  181. ${rundir}/bin/gerrit.sh start
  182. To use the REST API:
  183. curl --netrc-file ${rundir}/tmp/.netrc http://localhost:${http_port}/<endpoint>
  184. To use SSH API:
  185. ssh ${username}@localhost -p ${ssh_port} -i ${rundir}/tmp/id_rsa gerrit
  186. To enable 'git push' without a password prompt:
  187. git config credential.helper 'store --file=${rundir}/tmp/.git-credentials'
  188. To stop the server:
  189. ${rundir}/bin/gerrit.sh stop
  190. EOF