diff --git a/resources/user-data.sh-E b/resources/user-data.sh-E new file mode 100644 index 0000000..16e62bc --- /dev/null +++ b/resources/user-data.sh-E @@ -0,0 +1,241 @@ +#!/usr/bin/env bash + +export SOURCEGRAPH_VERSION=3.10.1 +export USER_HOME=/root +export SOURCEGRAPH_CONFIG=/etc/sourcegraph +export SOURCEGRAPH_DATA=/var/opt/sourcegraph +export PATH=$PATH:/usr/local/bin +export DEBIAN_FRONTEND=noninteractive +export CAROOT=${SOURCEGRAPH_CONFIG} +export MKCERT_VERSION=1.4.1 # https://github.com/FiloSottile/mkcert/releases +export IP_ADDRESS=$(echo $(hostname -I) | awk '{print $1;}') + +apt update +apt-get -y upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" + +# Required utils +apt install -y \ + git \ + nano \ + zip + +# Reset firewall to only allow 22, 80, 443, and 2633 +echo "y" | ufw reset +ufw default allow outgoing +ufw default deny incoming +ufw allow 22/tcp +ufw allow 80/tcp +ufw allow 443/tcp +ufw allow 2633/tcp +ufw allow 2633/tcp +ufw disable && echo "y" | ufw enable + +# Create the required Sourcegraph directories +mkdir -p ${SOURCEGRAPH_CONFIG}/management +mkdir -p ${SOURCEGRAPH_DATA} + +# Install mkcert and generate root CA, certificate and key +wget https://github.com/FiloSottile/mkcert/releases/download/v${MKCERT_VERSION}/mkcert-v${MKCERT_VERSION}-linux-amd64 -O /usr/local/bin/mkcert +chmod a+x /usr/local/bin/mkcert + +# Use the public ip address of the instance as hostnae for the self-signed cert as DigitalOcean doesn't have public DNS hostnames +mkcert -install +mkcert -cert-file ${SOURCEGRAPH_CONFIG}/sourcegraph.crt -key-file ${SOURCEGRAPH_CONFIG}/sourcegraph.key ${IP_ADDRESS} + +# +# Configure the nginx.conf file for SSL. +# +cat > ${SOURCEGRAPH_CONFIG}/nginx.conf < ${USER_HOME}/sourcegraph-start < /dev/null 2>&1 +docker container rm -f sourcegraph > /dev/null 2>&1 + +# Enable exit on non 0 +set -e + +echo "[info]: Starting Sourcegraph \${SOURCEGRAPH_VERSION}" + +docker container run \\ + --name sourcegraph \\ + -d \\ + --restart always \\ + \\ + --network sourcegraph \\ + --hostname sourcegraph \\ + --network-alias sourcegraph \\ + \\ + -p 80:7080 \\ + -p 443:7443 \\ + -p 2633:2633 \\ + -p 127.0.0.1:3370:3370 \\ + \\ + -v ${SOURCEGRAPH_CONFIG}:${SOURCEGRAPH_CONFIG} \\ + -v ${SOURCEGRAPH_DATA}:${SOURCEGRAPH_DATA} \\ + \\ + sourcegraph/server:\${SOURCEGRAPH_VERSION} +EOL + +cat > ${USER_HOME}/sourcegraph-stop < /dev/null 2>&1 docker container rm sourcegraph +EOL + +cat > ${USER_HOME}/sourcegraph-upgrade < ${USER_HOME}/sourcegraph-restart < /etc/update-motd.d/99-one-click <