Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Damien Martin-Guillerez | 3e57fe0 | 2015-09-25 14:02:19 +0200 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 4 | # |
| 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 Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 21 | # origin direction destination local-name |
| 22 | # where |
| 23 | # direction can be ==> or <=> |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 24 | REPOSITORIES=( |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 25 | "https://bazel.googlesource.com/bazel ==> git@github.com:bazelbuild/bazel.git bazel" |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 26 | "https://bazel.googlesource.com/bazel-toolchains <=> git@github.com:bazelbuild/bazel-toolchains.git bazel-toolchains" |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame^] | 27 | "https://bazel.googlesource.com/eclipse <=> git@github.com:bazelbuild/eclipse.git eclipse" |
Irina Iancu | 85e98b7 | 2019-01-14 21:16:55 +0100 | [diff] [blame] | 28 | "https://bazel.googlesource.com/java_tools ==> git@github.com:bazelbuild/java_tools.git java_tools" |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame^] | 29 | "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-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 33 | set -euxo pipefail |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 34 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 35 | # Download & decrypt gitcookies. |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame^] | 36 | gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-cookies.enc" | \ |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 37 | gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-cookies-key" --plaintext-file "-" --ciphertext-file "-" \ |
| 38 | > /home/gitsync/.gitcookies |
| 39 | chmod 0600 /home/gitsync/.gitcookies |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 40 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 41 | # Download & decrypt GitHub SSH key. |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame^] | 42 | gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-ssh.enc" | \ |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 43 | gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-ssh-key" --plaintext-file "-" --ciphertext-file "-" \ |
| 44 | > /home/gitsync/.ssh/id_rsa |
| 45 | chmod 0600 /home/gitsync/.ssh/id_rsa |
Damien Martin-Guillerez | 2af2435 | 2017-07-25 13:54:51 +0200 | [diff] [blame] | 46 | |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 47 | function clone() { |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 48 | 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-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 56 | popd |
| 57 | } |
| 58 | |
| 59 | function sync() { |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 60 | 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-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 74 | popd |
| 75 | } |
| 76 | |
| 77 | # Get a local clone |
| 78 | for i in "${REPOSITORIES[@]}"; do |
| 79 | clone $i |
| 80 | done |
| 81 | |
| 82 | # Sync loop |
| 83 | while true; do |
| 84 | for i in "${REPOSITORIES[@]}"; do |
| 85 | sync $i |
| 86 | done |
| 87 | # Sleep 30 seconds between each sync |
| 88 | sleep 30 |
| 89 | done |