|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -eux |
| 4 | + |
| 5 | +# Extract the protocol (includes trailing "://"). |
| 6 | +DEST_PROTO="$(echo $DEST_REPO | sed -nr 's,^(.*://).*,\1,p')" |
| 7 | +# Remove the protocol from the URL. |
| 8 | +DEST_URL="$(echo ${DEST_REPO/$DEST_PROTO/})" |
| 9 | +# Extract the user (includes trailing "@"). |
| 10 | +DEST_USER="$(echo $DEST_URL | sed -nr 's,^(.*@).*,\1,p')" |
| 11 | +DEST_USER=${DEST_USER:-git@} |
| 12 | +# Remove the user from the URL. |
| 13 | +DEST_URL="$(echo ${DEST_URL/$DEST_USER/})" |
| 14 | +# Extract the port (includes leading ":"). |
| 15 | +DEST_PORT="$(echo $DEST_URL | sed -nr 's,.*:([0-9]+).*,\1,p')" |
| 16 | +DEST_PORT=${DEST_PORT:-22} |
| 17 | +# Remove the port from the URL. |
| 18 | +DEST_URL="$(echo ${DEST_URL/$DEST_PORT/})" |
| 19 | +# Extract the path (includes leading "/" or ":"). |
| 20 | +DEST_PATH="$(echo $DEST_URL | sed -nr 's,[^/:]*([/:].*),\1,p')" |
| 21 | +# Remove the path from the URL. |
| 22 | +DEST_HOST="$(echo ${DEST_URL/$DEST_PATH/})" |
| 23 | + |
| 24 | + |
| 25 | +# Extract the protocol (includes trailing "://"). |
| 26 | +SRC_PROTO="$(echo $SRC_REPO | sed -nr 's,^(.*://).*,\1,p')" |
| 27 | +# Remove the protocol from the URL. |
| 28 | +SRC_URL="$(echo ${SRC_REPO/$SRC_PROTO/})" |
| 29 | +# Extract the user (includes trailing "@"). |
| 30 | +SRC_USER="$(echo $SRC_URL | sed -nr 's,^(.*@).*,\1,p')" |
| 31 | +SRC_USER=${SRC_USER:-git@} |
| 32 | +# Remove the user from the URL. |
| 33 | +SRC_URL="$(echo ${SRC_URL/$SRC_USER/})" |
| 34 | +# Extract the port (includes leading ":"). |
| 35 | +SRC_PORT="$(echo $SRC_URL | sed -nr 's,.*:([0-9]+).*,\1,p')" |
| 36 | +SRC_PORT=${SRC_PORT:-22} |
| 37 | +# Remove the port from the URL. |
| 38 | +SRC_URL="$(echo ${SRC_URL/$SRC_PORT/})" |
| 39 | +# Extract the path (includes leading "/" or ":"). |
| 40 | +SRC_PATH="$(echo $SRC_URL | sed -nr 's,[^/:]*([/:].*),\1,p')" |
| 41 | +# Remove the path from the URL. |
| 42 | +SRC_HOST="$(echo ${SRC_URL/$SRC_PATH/})" |
| 43 | +# name that can be used for the folder name |
| 44 | +SRC_PROJECT="$(echo $SRC_PATH | sed -nr 's,.*/(.*)\.git,\1,p')" |
| 45 | + |
| 46 | + |
| 47 | +mkdir -p ~/.ssh |
| 48 | + |
| 49 | +eval `ssh-agent -s` |
| 50 | +echo "$SRC_DEPLOY_KEY" | base64 -d | ssh-add - |
| 51 | +echo "$DEST_DEPLOY_KEY" | base64 -d | ssh-add - |
| 52 | +ssh -o StrictHostKeyChecking=no -T "$SRC_USER$SRC_HOST" -p "$SRC_PORT" |
| 53 | +ssh -o StrictHostKeyChecking=no -T "$DEST_USER$DEST_HOST" -p "$DEST_PORT" |
| 54 | + |
| 55 | +if [[ ! -d /storage/"$SRC_PROJECT" ]]; then |
| 56 | + cd /storage |
| 57 | + git clone --bare "$SRC_REPO" "$SRC_PROJECT" |
| 58 | + cd /storage/"$SRC_PROJECT" |
| 59 | + git remote add dest ssh://"$DEST_REPO" |
| 60 | +fi |
| 61 | + |
| 62 | +cd /storage/"$SRC_PROJECT" |
| 63 | + |
| 64 | +while true; do nc -l -p 8080 -e sh -c 'echo -e "HTTP/1.0 200 OK\r\nDate: $(date)\r\nContent-Length: 2\r\n\r\nOK"; sh /usr/local/bin/mirror.sh;'; done |
0 commit comments