|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euxo pipefail |
| 3 | +############################################################################### |
| 4 | +# ACTION REQUIRED: REPLACE THE URL AND REVISION WITH YOUR DEPLOYMENT REPO INFO |
| 5 | +############################################################################### |
| 6 | +# Please read the notes below the script if you are cloning a private repository |
| 7 | +# Example: DEPLOY_SOURCEGRAPH_DOCKER_FORK_REVISION='v4.1.3' |
| 8 | +DEPLOY_SOURCEGRAPH_DOCKER_FORK_REVISION="$1" |
| 9 | +DEPLOY_SOURCEGRAPH_DOCKER_FORK_CLONE_URL="$2" |
| 10 | +##################### NO CHANGES REQUIRED BELOW THIS LINE ##################### |
| 11 | +if [ "$2" = "" ]; then |
| 12 | + DEPLOY_SOURCEGRAPH_DOCKER_FORK_CLONE_URL='https://github.com/sourcegraph/deploy-sourcegraph-docker.git' |
| 13 | +fi |
| 14 | +DEPLOY_SOURCEGRAPH_DOCKER_CHECKOUT='/root/deploy-sourcegraph-docker' |
| 15 | +DOCKER_COMPOSE_VERSION='1.29.2' |
| 16 | +DOCKER_DAEMON_CONFIG_FILE='/etc/docker/daemon.json' |
| 17 | +DOCKER_DATA_ROOT='/mnt/docker-data' |
| 18 | +PERSISTENT_DISK_DEVICE_NAME='/dev/sdb' |
| 19 | +PERSISTENT_DISK_LABEL='sourcegraph' |
| 20 | +# Install git |
| 21 | +sudo apt-get update -y |
| 22 | +sudo apt-get install -y git |
| 23 | +# Clone the deployment repository |
| 24 | +git clone "${DEPLOY_SOURCEGRAPH_DOCKER_FORK_CLONE_URL}" "${DEPLOY_SOURCEGRAPH_DOCKER_CHECKOUT}" |
| 25 | +cd "${DEPLOY_SOURCEGRAPH_DOCKER_CHECKOUT}" |
| 26 | +git checkout "${DEPLOY_SOURCEGRAPH_DOCKER_FORK_REVISION}" |
| 27 | +# Format (if unformatted) and then mount the attached volume |
| 28 | +device_fs=$(sudo lsblk "${PERSISTENT_DISK_DEVICE_NAME}" --noheadings --output fsType) |
| 29 | +if [ "${device_fs}" == "" ]; then ## only format the volume if it isn't already formatted |
| 30 | + sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard "${PERSISTENT_DISK_DEVICE_NAME}" |
| 31 | +fi |
| 32 | +sudo e2label "${PERSISTENT_DISK_DEVICE_NAME}" "${PERSISTENT_DISK_LABEL}" |
| 33 | +sudo mkdir -p "${DOCKER_DATA_ROOT}" |
| 34 | +sudo mount -o discard,defaults "${PERSISTENT_DISK_DEVICE_NAME}" "${DOCKER_DATA_ROOT}" |
| 35 | +# Mount file system by label on reboot |
| 36 | +sudo echo "LABEL=${PERSISTENT_DISK_LABEL} ${DOCKER_DATA_ROOT} ext4 discard,defaults,nofail 0 2" | sudo tee -a /etc/fstab |
| 37 | +sudo umount "${DOCKER_DATA_ROOT}" |
| 38 | +sudo mount -a |
| 39 | +# Install, configure, and enable Docker |
| 40 | +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 41 | +sudo apt-get update -y |
| 42 | +sudo apt-get install -y software-properties-common |
| 43 | +sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
| 44 | +sudo apt-get update -y |
| 45 | +apt-cache policy docker-ce |
| 46 | +apt-get install -y docker-ce docker-ce-cli containerd.io |
| 47 | +## Enable Docker at startup |
| 48 | +sudo systemctl enable --now docker |
| 49 | +## Install jq for scripting |
| 50 | +sudo apt-get update -y |
| 51 | +sudo apt-get install -y jq |
| 52 | +## Initialize the config file with empty json if it doesn't exist |
| 53 | +if [ ! -f "${DOCKER_DAEMON_CONFIG_FILE}" ]; then |
| 54 | + mkdir -p $(dirname "${DOCKER_DAEMON_CONFIG_FILE}") |
| 55 | + echo '{}' >"${DOCKER_DAEMON_CONFIG_FILE}" |
| 56 | +fi |
| 57 | +## Point Docker storage to mounted volume |
| 58 | +tmp_config=$(mktemp) |
| 59 | +trap "rm -f ${tmp_config}" EXIT |
| 60 | +sudo cat "${DOCKER_DAEMON_CONFIG_FILE}" | sudo jq --arg DATA_ROOT "${DOCKER_DATA_ROOT}" '.["data-root"]=$DATA_ROOT' >"${tmp_config}" |
| 61 | +sudo cat "${tmp_config}" >"${DOCKER_DAEMON_CONFIG_FILE}" |
| 62 | +## Restart Docker daemon to pick up new changes |
| 63 | +sudo systemctl restart --now docker |
| 64 | +# Install Docker Compose |
| 65 | +curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose |
| 66 | +chmod +x /usr/local/bin/docker-compose |
| 67 | +curl -L "https://raw.githubusercontent.com/docker/compose/${DOCKER_COMPOSE_VERSION}/contrib/completion/bash/docker-compose" -o /etc/bash_completion.d/docker-compose |
| 68 | +# Start Sourcegraph with Docker Compose |
| 69 | +cd "${DEPLOY_SOURCEGRAPH_DOCKER_CHECKOUT}"/docker-compose |
| 70 | +docker-compose up -d --remove-orphans |
0 commit comments