|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +export SOURCEGRAPH_VERSION=3.10.1 |
| 4 | +export USER_HOME=/root |
| 5 | +export SOURCEGRAPH_CONFIG=/etc/sourcegraph |
| 6 | +export SOURCEGRAPH_DATA=/var/opt/sourcegraph |
| 7 | +export PATH=$PATH:/usr/local/bin |
| 8 | +export DEBIAN_FRONTEND=noninteractive |
| 9 | +export CAROOT=${SOURCEGRAPH_CONFIG} |
| 10 | +export MKCERT_VERSION=1.4.1 # https://github.com/FiloSottile/mkcert/releases |
| 11 | +export IP_ADDRESS=$(echo $(hostname -I) | awk '{print $1;}') |
| 12 | + |
| 13 | +apt update |
| 14 | +apt-get -y upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" |
| 15 | + |
| 16 | +# Required utils |
| 17 | +apt install -y \ |
| 18 | + git \ |
| 19 | + nano \ |
| 20 | + zip |
| 21 | + |
| 22 | +# Reset firewall to only allow 22, 80, 443, and 2633 |
| 23 | +echo "y" | ufw reset |
| 24 | +ufw default allow outgoing |
| 25 | +ufw default deny incoming |
| 26 | +ufw allow 22/tcp |
| 27 | +ufw allow 80/tcp |
| 28 | +ufw allow 443/tcp |
| 29 | +ufw allow 2633/tcp |
| 30 | +ufw allow 2633/tcp |
| 31 | +ufw disable && echo "y" | ufw enable |
| 32 | + |
| 33 | +# Create the required Sourcegraph directories |
| 34 | +mkdir -p ${SOURCEGRAPH_CONFIG}/management |
| 35 | +mkdir -p ${SOURCEGRAPH_DATA} |
| 36 | + |
| 37 | +# Install mkcert and generate root CA, certificate and key |
| 38 | +wget https://github.com/FiloSottile/mkcert/releases/download/v${MKCERT_VERSION}/mkcert-v${MKCERT_VERSION}-linux-amd64 -O /usr/local/bin/mkcert |
| 39 | +chmod a+x /usr/local/bin/mkcert |
| 40 | + |
| 41 | +# Use the public ip address of the instance as hostnae for the self-signed cert as DigitalOcean doesn't have public DNS hostnames |
| 42 | +mkcert -install |
| 43 | +mkcert -cert-file ${SOURCEGRAPH_CONFIG}/sourcegraph.crt -key-file ${SOURCEGRAPH_CONFIG}/sourcegraph.key ${IP_ADDRESS} |
| 44 | + |
| 45 | +# |
| 46 | +# Configure the nginx.conf file for SSL. |
| 47 | +# |
| 48 | +cat > ${SOURCEGRAPH_CONFIG}/nginx.conf <<EOL |
| 49 | +# From https://github.com/sourcegraph/sourcegraph/blob/master/cmd/server/shared/assets/nginx.conf |
| 50 | +# You can adjust the configuration to add additional TLS or HTTP features. |
| 51 | +# Read more at https://docs.sourcegraph.com/admin/nginx |
| 52 | +
|
| 53 | +error_log stderr; |
| 54 | +pid /var/run/nginx.pid; |
| 55 | +
|
| 56 | +# Do not remove. The contents of sourcegraph_main.conf can change between |
| 57 | +# versions and may include improvements to the configuration. |
| 58 | +include nginx/sourcegraph_main.conf; |
| 59 | +
|
| 60 | +events { |
| 61 | +} |
| 62 | +
|
| 63 | +http { |
| 64 | + server_tokens off; |
| 65 | +
|
| 66 | + # SAML redirect response headers are sometimes large |
| 67 | + proxy_buffer_size 128k; |
| 68 | + proxy_buffers 8 256k; |
| 69 | + proxy_busy_buffers_size 256k; |
| 70 | +
|
| 71 | + # Do not remove. The contents of sourcegraph_http.conf can change between |
| 72 | + # versions and may include improvements to the configuration. |
| 73 | + include nginx/sourcegraph_http.conf; |
| 74 | +
|
| 75 | + access_log off; |
| 76 | + upstream backend { |
| 77 | + # Do not remove. The contents of sourcegraph_backend.conf can change |
| 78 | + # between versions and may include improvements to the configuration. |
| 79 | + include nginx/sourcegraph_backend.conf; |
| 80 | + } |
| 81 | +
|
| 82 | + # Redirect all HTTP traffic to HTTPS |
| 83 | + server { |
| 84 | + listen 7080 default_server; |
| 85 | + return 301 https://\$host\$request_uri; |
| 86 | + } |
| 87 | +
|
| 88 | + server { |
| 89 | + # Do not remove. The contents of sourcegraph_server.conf can change |
| 90 | + # between versions and may include improvements to the configuration. |
| 91 | + include nginx/sourcegraph_server.conf; |
| 92 | +
|
| 93 | + listen 7443 ssl http2 default_server; |
| 94 | + ssl_certificate sourcegraph.crt; |
| 95 | + ssl_certificate_key sourcegraph.key; |
| 96 | +
|
| 97 | + location / { |
| 98 | + proxy_pass http://backend; |
| 99 | + proxy_set_header Host \$http_host; |
| 100 | + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
| 101 | + proxy_set_header X-Forwarded-Proto \$scheme; |
| 102 | + } |
| 103 | +
|
| 104 | + # SAML redirect response headers are sometimes large |
| 105 | + proxy_buffer_size 128k; |
| 106 | + proxy_buffers 8 256k; |
| 107 | + proxy_busy_buffers_size 256k; |
| 108 | +
|
| 109 | + location '/.well-known/acme-challenge' { |
| 110 | + default_type "text/plain"; |
| 111 | + root /var/www/html; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +EOL |
| 116 | + |
| 117 | + |
| 118 | +# Use the same certificate for the management console |
| 119 | +cp ${SOURCEGRAPH_CONFIG}/sourcegraph.crt ${SOURCEGRAPH_CONFIG}/management/cert.pem |
| 120 | +cp ${SOURCEGRAPH_CONFIG}/sourcegraph.key ${SOURCEGRAPH_CONFIG}/management/key.pem |
| 121 | + |
| 122 | +# Zip the CA Root key and certificate for easy downloading |
| 123 | +zip -j ${USER_HOME}/sourcegraph-root-ca.zip ${SOURCEGRAPH_CONFIG}/sourcegraph.crt ${SOURCEGRAPH_CONFIG}/sourcegraph.key |
| 124 | + |
| 125 | +cat > ${USER_HOME}/sourcegraph-start <<EOL |
| 126 | +#!/usr/bin/env bash |
| 127 | +
|
| 128 | +SOURCEGRAPH_VERSION=${SOURCEGRAPH_VERSION} |
| 129 | +
|
| 130 | +# Disable exit on non 0 as these may fail, which is ok |
| 131 | +# because failure will only occur if the network exists |
| 132 | +# or if the sourcegraph container doesn't exist. |
| 133 | +set +e |
| 134 | +docker network create sourcegraph > /dev/null 2>&1 |
| 135 | +docker container rm -f sourcegraph > /dev/null 2>&1 |
| 136 | +
|
| 137 | +# Enable exit on non 0 |
| 138 | +set -e |
| 139 | +
|
| 140 | +echo "[info]: Starting Sourcegraph \${SOURCEGRAPH_VERSION}" |
| 141 | +
|
| 142 | +docker container run \\ |
| 143 | + --name sourcegraph \\ |
| 144 | + -d \\ |
| 145 | + --restart always \\ |
| 146 | + \\ |
| 147 | + --network sourcegraph \\ |
| 148 | + --hostname sourcegraph \\ |
| 149 | + --network-alias sourcegraph \\ |
| 150 | + \\ |
| 151 | + -p 80:7080 \\ |
| 152 | + -p 443:7443 \\ |
| 153 | + -p 2633:2633 \\ |
| 154 | + -p 127.0.0.1:3370:3370 \\ |
| 155 | + \\ |
| 156 | + -v ${SOURCEGRAPH_CONFIG}:${SOURCEGRAPH_CONFIG} \\ |
| 157 | + -v ${SOURCEGRAPH_DATA}:${SOURCEGRAPH_DATA} \\ |
| 158 | + \\ |
| 159 | + sourcegraph/server:\${SOURCEGRAPH_VERSION} |
| 160 | +EOL |
| 161 | + |
| 162 | +cat > ${USER_HOME}/sourcegraph-stop <<EOL |
| 163 | +#!/usr/bin/env bash |
| 164 | +
|
| 165 | +echo "[info]: Stopping Sourcegraph" |
| 166 | +docker container stop sourcegraph > /dev/null 2>&1 docker container rm sourcegraph |
| 167 | +EOL |
| 168 | + |
| 169 | +cat > ${USER_HOME}/sourcegraph-upgrade <<EOL |
| 170 | +#!/usr/bin/env bash |
| 171 | +
|
| 172 | +./sourcegraph-stop |
| 173 | +
|
| 174 | +read -p "Sourcegraph version to upgrade to: " VERSION |
| 175 | +sed -i -E "s/SOURCEGRAPH_VERSION=[0-9\.]+/SOURCEGRAPH_VERSION=\$VERSION/g" ./sourcegraph-start |
| 176 | +
|
| 177 | +./sourcegraph-start |
| 178 | +EOL |
| 179 | + |
| 180 | +cat > ${USER_HOME}/sourcegraph-restart <<EOL |
| 181 | +#!/usr/bin/env bash |
| 182 | +
|
| 183 | +./sourcegraph-stop |
| 184 | +./sourcegraph-start |
| 185 | +EOL |
| 186 | + |
| 187 | +chmod +x ${USER_HOME}/sourcegraph-* |
| 188 | +${USER_HOME}/sourcegraph-start |
| 189 | + |
| 190 | +# Truncate the `global_state` db table so a unique site_id will be generated upon launch |
| 191 | +docker container exec -it sourcegraph psql -U postgres sourcegraph --command "DELETE FROM global_state WHERE 1=1;" |
| 192 | + |
| 193 | +apt -y autoremove |
| 194 | +apt -y autoclean |
| 195 | + |
| 196 | +cat > /etc/update-motd.d/99-one-click <<EOL |
| 197 | +#!/bin/sh |
| 198 | +# |
| 199 | +# Configured as part of the DigitalOcean 1-Click Image build process |
| 200 | +
|
| 201 | +IP_ADDRESS=$(echo $(hostname -I) | awk '{print $1;}') |
| 202 | +cat <<EOF |
| 203 | +
|
| 204 | +******************************************************************************** |
| 205 | +
|
| 206 | +Welcome to the Sourcegraph 1-Click App Droplet. |
| 207 | +
|
| 208 | +For help and more information, visit https://docs.sourcegraph.com/ |
| 209 | +
|
| 210 | +## Accessing Sourcegraph |
| 211 | +
|
| 212 | +Sourcegraph is running as the sourcegraph/server Docker container with two different access points: |
| 213 | + - Sourcegraph web app: https://${IP_ADDRESS} |
| 214 | + - Sourcegraph management console: https://${IP_ADDRESS}:2633 |
| 215 | +
|
| 216 | +## Controlling Sourcegraph |
| 217 | +
|
| 218 | +There are four scripts in the /root directory for controlling Sourcegraph: |
| 219 | + - sourcegraph-start |
| 220 | + - sourcegraph-stop |
| 221 | + - sourcegraph-restart |
| 222 | + - sourcegraph-upgrade |
| 223 | +
|
| 224 | +## Server resources |
| 225 | +
|
| 226 | + - Sourcegraph configuration files are in /etc/sourcegraph |
| 227 | + - Sourcegraph data files are in /var/opt/sourcegraph |
| 228 | +
|
| 229 | +## PostgreSQL access |
| 230 | +
|
| 231 | +Access the PostgreSQL db inside the Docker container by running: docker container exec -it sourcegraph psql -U postgres sourcegraph |
| 232 | +
|
| 233 | +## Security |
| 234 | +
|
| 235 | +To keep this Droplet secure, UFW is blocking all in-bound ports except 20, 80, 443, and 2633 (Critical config management console). |
| 236 | +
|
| 237 | +To delete this message of the day: rm -rf $(readlink -f ${0}) |
| 238 | +
|
| 239 | +******************************************************************************** |
| 240 | +EOF |
| 241 | +EOL |
0 commit comments