blob: 0e3189c399737e3fcac2a330d2b9b9a81863343d [file] [log] [blame]
szager@chromium.orgef2ba742013-07-16 22:43:59 +00001#!/bin/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
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +00006set -e
7
szager@chromium.org74958942013-09-03 22:44:03 +00008http_port=8080
9ssh_port=29418
10
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000011while test $# -ne 0; do
12 case "$1" in
13 -v)
14 version="$2"
15 shift
16 ;;
17 -d)
18 rundir="$2"
19 shift
20 ;;
szager@chromium.org74958942013-09-03 22:44:03 +000021 --http-port)
22 http_port="$2"
23 shift
24 ;;
25 --ssh-port)
26 ssh_port="$2"
27 shift
28 ;;
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000029 *)
30 rundir="$1"
31 ;;
32 esac
33 shift
34done
35
36if [ -z "$rundir" ]; then
szager@chromium.orgef2ba742013-07-16 22:43:59 +000037 rundir=$(mktemp -d)
38fi
39
szager@chromium.org3e2fff22013-07-17 22:41:21 +000040this_dir=$(dirname $0)
41gerrit_exe="$this_dir/gerrit.war"
42
szager@chromium.orgef2ba742013-07-16 22:43:59 +000043account_id=101
44full_name='Test Account'
45maximum_page_size='25'
46password='test-password'
szager@chromium.org3e2fff22013-07-17 22:41:21 +000047preferred_email="test-username@test.org"
szager@chromium.orgef2ba742013-07-16 22:43:59 +000048registered_on=$(date '+%Y-%m-%d %H:%M:%S.000%:::z')
49username='test-username'
50
51# The python code below for picking the "latest" gerrit release is cribbed and
52# ported from the javascript at:
53#
54# http://gerrit-releases.storage.googleapis.com/index.html
55url='https://www.googleapis.com/storage/v1beta2/b/gerrit-releases/o?projection=noAcl'
56curl --ssl-reqd -s $url | python <(cat <<EOF
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000057# Receives Gerrit version via command line and reads json-encoded
58# text from stdin in the format:
szager@chromium.orgef2ba742013-07-16 22:43:59 +000059#
60# {
61# "items": [
62# {
63# "name": "gerrit-<version>.war",
64# "md5Hash": "<base64 encoded md5sum>",
65# },
66# {
67# "name": "gerrit-<version>.war",
68# "md5Hash": "<base64 encoded md5sum>",
69# },
70# ...
71# }
72#
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000073# ...and prints the name and md5sum of the corresponding *.war file.
szager@chromium.orgef2ba742013-07-16 22:43:59 +000074
75import json
76import re
77import sys
78
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000079requested_version = sys.argv[1] if len(sys.argv) > 1 else None
szager@chromium.orgef2ba742013-07-16 22:43:59 +000080gerrit_re = re.compile('gerrit(?:-full)?-([0-9.]+(?:-rc[0-9]+)?)[.]war')
81j = json.load(sys.stdin)
82items = [(x, gerrit_re.match(x['name'])) for x in j['items']]
83items = [(x, m.group(1)) for x, m in items if m]
84def _cmp(a, b):
85 an = a[1].replace('-rc', '.rc').split('.')
86 bn = b[1].replace('-rc', '.rc').split('.')
87 while len(an) < len(bn):
88 an.append('0')
89 while len(bn) < len(an):
90 bn.append('0')
91 for i in range(len(an)):
92 ai = int(an[i][2:]) if 'rc' in an[i] else 1000 + int(an[i])
93 bi = int(bn[i][2:]) if 'rc' in bn[i] else 1000 + int(bn[i])
94 if ai != bi:
95 return -1 if ai > bi else 1
96 return 0
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +000097
98if requested_version:
99 for info, version in items:
100 if version == requested_version:
101 print '"%s" "%s"' % (info['name'], info['md5Hash'])
102 sys.exit(0)
103 print >> sys.stderr, 'No such Gerrit version: %s' % requested_version
104 sys.exit(1)
105
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000106items.sort(cmp=_cmp)
107for x in items:
108 if 'rc' not in x[0]['name']:
109 print '"%s" "%s"' % (x[0]['name'], x[0]['md5Hash'])
110 sys.exit(0)
111EOF
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +0000112) "$version" | xargs | while read name md5; do
113 # Download the requested gerrit version if necessary, and verify the md5sum.
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000114 target="$this_dir/$name"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000115 net_sum=$(echo -n $md5 | base64 -d | od -tx1 | head -1 | cut -d ' ' -f 2- |
116 sed 's/ //g')
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000117 if [ -f "$target" ]; then
118 file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000119 if [ "$file_sum" = "$net_sum" ]; then
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000120 ln -sf "$name" "$gerrit_exe"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000121 break
122 else
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000123 rm -rf "$target"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000124 fi
125 fi
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000126 curl --ssl-reqd -s -o "$target" \
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000127 "https://gerrit-releases.storage.googleapis.com/$name"
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000128 file_sum=$(md5sum "$target" | awk '{print $1}' | xargs)
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000129 if [ "$file_sum" != "$net_sum" ]; then
130 echo "ERROR: md5sum mismatch when downloading $name" 1>&2
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000131 rm -rf "$target"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000132 exit 1
133 else
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000134 ln -sf "$name" "$gerrit_exe"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000135 fi
136done
137
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000138if [ ! -e "$gerrit_exe" ]; then
139 echo "ERROR: No $gerrit_exe file or link present, and unable " 1>&2
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000140 echo " to download the latest version." 1>&2
141 exit 1
142fi
143
144# By default, gerrit only accepts https connections, which is a good thing. But
szager@chromium.org7d51b4f2013-09-12 19:15:35 +0000145# for testing, it's convenient to enable plain http. Also, turn off all email
146# notifications.
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000147mkdir -p "${rundir}/etc"
148cat <<EOF > "${rundir}/etc/gerrit.config"
149[auth]
szager@chromium.orgb4696232013-10-16 19:45:35 +0000150 type = DEVELOPMENT_BECOME_ANY_ACCOUNT
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000151 gitBasicAuth = true
szager@chromium.org74958942013-09-03 22:44:03 +0000152[gerrit]
153 canonicalWebUrl = http://$(hostname):${http_port}/
154[httpd]
155 listenUrl = http://*:${http_port}/
156[sshd]
157 listenAddress = *:${ssh_port}
szager@chromium.org7d51b4f2013-09-12 19:15:35 +0000158[sendemail]
159 enable = false
szager@chromium.org545260d2013-10-17 22:56:13 +0000160[container]
161 javaOptions = -Duser.home=${rundir}/tmp
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000162EOF
163
164# Initialize the gerrit instance.
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000165java -jar "$gerrit_exe" init --no-auto-start --batch -d "${rundir}"
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000166
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +0000167# Create SSH key pair for the first user.
168mkdir -p "${rundir}/tmp"
169ssh-keygen -t rsa -q -f "${rundir}/tmp/id_rsa" -N ""
170ssh_public_key="$(cat ${rundir}/tmp/id_rsa.pub)"
171
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000172# Set up the first user, with admin priveleges.
szager@chromium.org3e2fff22013-07-17 22:41:21 +0000173cat <<EOF | java -jar "$gerrit_exe" gsql -d "${rundir}" > /dev/null
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000174INSERT 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});
175INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'gerrit:${username}');
176INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) VALUES (${account_id}, 'username:${username}');
177INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EMAIL_ADDRESS, PASSWORD) VALUES (${account_id}, '${preferred_email}', '${password}');
178INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) VALUES (${account_id}, 1);
vadimsh@chromium.orgf6916482013-08-06 22:41:03 +0000179INSERT INTO ACCOUNT_SSH_KEYS (ACCOUNT_ID, SSH_PUBLIC_KEY, VALID, SEQ) VALUES (${account_id}, '${ssh_public_key}', 'Y', 0);
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000180EOF
181
182# Create a netrc file to authenticate as the first user.
szager@chromium.orgef2ba742013-07-16 22:43:59 +0000183cat <<EOF > "${rundir}/tmp/.netrc"
184machine localhost login ${username} password ${password}
185EOF
186
szager@chromium.org3592f932013-07-19 23:27:44 +0000187# Create a .git-credentials file, to enable password-less push.
188cat <<EOF > "${rundir}/tmp/.git-credentials"
szager@chromium.org74958942013-09-03 22:44:03 +0000189http://${username}:${password}@localhost:${http_port}
szager@chromium.org3592f932013-07-19 23:27:44 +0000190EOF
191
szager@chromium.org74958942013-09-03 22:44:03 +0000192cat <<EOF
193
194To start gerrit server:
195 ${rundir}/bin/gerrit.sh start
196
197To use the REST API:
198 curl --netrc-file ${rundir}/tmp/.netrc http://localhost:${http_port}/<endpoint>
199
200To use SSH API:
201 ssh ${username}@localhost -p ${ssh_port} -i ${rundir}/tmp/id_rsa gerrit
202
203To enable 'git push' without a password prompt:
204 git config credential.helper 'store --file=${rundir}/tmp/.git-credentials'
205
206To stop the server:
207 ${rundir}/bin/gerrit.sh stop
208
209EOF