blob: 218a0198658356b149c36584cc4d6655a2972513 [file] [log] [blame]
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +02001#!/bin/bash
2#
Damien Martin-Guillerez3e57fe02015-09-25 14:02:19 +02003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +02004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Scripts to configure the service that will poll git repositories for
18# in sync check.
19
20# Format of each entry is:
Philipp Wollermanna28803e2018-04-10 14:40:23 +020021# origin direction destination local-name
22# where
23# direction can be ==> or <=>
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020024REPOSITORIES=(
Philipp Wollermanna28803e2018-04-10 14:40:23 +020025 "https://bazel.googlesource.com/bazel ==> git@github.com:bazelbuild/bazel.git bazel"
Philipp Wollermanna28803e2018-04-10 14:40:23 +020026 "https://bazel.googlesource.com/bazel-toolchains <=> git@github.com:bazelbuild/bazel-toolchains.git bazel-toolchains"
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010027 "https://bazel.googlesource.com/eclipse <=> git@github.com:bazelbuild/eclipse.git eclipse"
Irina Iancu85e98b72019-01-14 21:16:55 +010028 "https://bazel.googlesource.com/java_tools ==> git@github.com:bazelbuild/java_tools.git java_tools"
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010029 "https://bazel.googlesource.com/rules_cc ==> git@github.com:bazelbuild/rules_cc.git rules_cc"
30 "https://bazel.googlesource.com/tulsi ==> git@github.com:bazelbuild/tulsi.git tulsi"
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020031)
32
Philipp Wollermann9d04b982018-04-07 22:12:00 +020033set -euxo pipefail
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020034
Philipp Wollermann9d04b982018-04-07 22:12:00 +020035# Download & decrypt gitcookies.
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010036gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-cookies.enc" | \
Philipp Wollermann9d04b982018-04-07 22:12:00 +020037 gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-cookies-key" --plaintext-file "-" --ciphertext-file "-" \
38 > /home/gitsync/.gitcookies
39chmod 0600 /home/gitsync/.gitcookies
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020040
Philipp Wollermann9d04b982018-04-07 22:12:00 +020041# Download & decrypt GitHub SSH key.
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010042gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-ssh.enc" | \
Philipp Wollermann9d04b982018-04-07 22:12:00 +020043 gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-ssh-key" --plaintext-file "-" --ciphertext-file "-" \
44 > /home/gitsync/.ssh/id_rsa
45chmod 0600 /home/gitsync/.ssh/id_rsa
Damien Martin-Guillerez2af24352017-07-25 13:54:51 +020046
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020047function clone() {
Philipp Wollermanna28803e2018-04-10 14:40:23 +020048 local origin="$1"
49 local destination="$3"
50 local name="$4"
51
52 rm -rf "${name}"
53 git clone "${origin}" "${name}"
54 pushd "${name}"
55 git remote add destination "${destination}"
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020056 popd
57}
58
59function sync() {
Philipp Wollermanna28803e2018-04-10 14:40:23 +020060 local origin="$1"
61 local direction="$2"
62 local destination="$3"
63 local name="$4"
64
65 pushd "${name}"
66 echo "Syncing ${origin} ${direction} ${destination} ..."
67 git remote update --prune
68 git checkout "origin/master" -B "master"
69 if [[ "${direction}" == "<=>" ]]; then
70 git rebase "destination/master"
71 git push -f origin master
72 fi
73 git push destination master
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020074 popd
75}
76
77# Get a local clone
78for i in "${REPOSITORIES[@]}"; do
79 clone $i
80done
81
82# Sync loop
83while true; do
84 for i in "${REPOSITORIES[@]}"; do
85 sync $i
86 done
87 # Sleep 30 seconds between each sync
88 sleep 30
89done