Skip to content

Instantly share code, notes, and snippets.

@codycraven
Created August 7, 2017 23:34
Show Gist options
  • Save codycraven/c28ba26a720121c5eee845822511c4f0 to your computer and use it in GitHub Desktop.
Save codycraven/c28ba26a720121c5eee845822511c4f0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Before running this make sure Caddy and Docker are installed:
# https://gist.github.com/codycraven/bba48dcdcd87a9df4e2bb87834b5a65d
# https://gist.github.com/codycraven/24752be91b676b1c193771a652f4b60d
# Get sudo
sudo echo
# Manual configuration
echo "To setup the webserver, some information is needed:"
echo -n "- Domain name: "
read DOMAIN
echo -n "- Email for Let's Encrypt TLS: "
read EMAIL
echo ""
echo "To setup Drone with GitHub, do the following:"
echo "- 1. Visit https://github.com/settings/applications/new"
echo "- 2. Create a name in the \"Application name\" field"
echo "- 3. Set \"Homepage URL\" to: https://${DOMAIN}/"
echo "- 4. Set \"Authorization callback URL\" to: https://${DOMAIN}/authorize"
echo "- 5. Click \"Register application\""
echo -n "- 6. Provide the \"Client ID\": "
read GITHUB_CLIENT_ID
echo -n "- 7. Provide the \"Client Secret\": "
read GITHUB_CLIENT_SECRET
echo ""
echo "Security settings for Drone:"
echo -n "- GitHub orgs whose members are allowed to register, separated by commas (Optional): "
read DRONE_ORGS
DRONE_OPEN=$([ -z "$DRONE_ORGS" ] && echo "false" || echo "true")
echo -n "- GitHub users, separated by commas"
[ -z "$DRONE_ORGS" ] || echo -n " (Optional)"
echo -n ": "
read DRONE_ADMIN
# Generate Drone secret
DRONE_SECRET=$(LC_ALL=C </dev/urandom tr -dc A-Za-z0-9 | head -c 65 && echo)
# Configure webserver (Caddy)
sudo tee -a /etc/caddy/Caddyfile > /dev/null <<EOT
${DOMAIN} {
proxy / localhost:8000 {
websocket
transparent
}
tls ${EMAIL}
}
EOT
sudo systemctl restart caddy
# Setup Drone
docker pull drone/drone:0.7
sudo mkdir /etc/drone
# Set Drone server configuration
sudo tee /etc/drone/docker-compose.yml > /dev/null <<EOT
version: '3'
services:
drone-server:
image: drone/drone:0.7
ports:
- 127.0.0.1:8000:8000
volumes:
- /var/lib/drone:/var/lib/drone
restart: always
env_file:
- /etc/drone/server.env
drone-agent:
image: drone/drone:0.7
command: agent
depends_on:
- drone-server
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: always
env_file:
- /etc/drone/agent.env
EOT
sudo tee /etc/drone/server.env > /dev/null <<EOT
# Service settings
DRONE_SECRET=${DRONE_SECRET}
DRONE_HOST=https://${DOMAIN}
# Registration settings
DRONE_OPEN=${DRONE_OPEN}
EOT
if [ ! -z "$DRONE_ORGS" ]; then
echo "DRONE_ORGS=${DRONE_ORGS}" | sudo tee -a /etc/drone/server.env > /dev/null
fi
if [ ! -z "$DRONE_ADMIN" ]; then
echo "DRONE_ADMIN=${DRONE_ADMIN}" | sudo tee -a /etc/drone/server.env > /dev/null
fi
sudo tee -a /etc/drone/server.env > /dev/null <<EOT
# GitHub settings
DRONE_GITHUB=true
DRONE_GITHUB_CLIENT=${GITHUB_CLIENT_ID}
DRONE_GITHUB_SECRET=${GITHUB_CLIENT_SECRET}
EOT
# Set Drone agent configuration
sudo tee /etc/drone/agent.env > /dev/null <<EOT
DRONE_SECRET=${DRONE_SECRET}
DRONE_SERVER=wss://${DOMAIN}/ws/broker
EOT
# Setup Drone as a service
sudo tee /etc/systemd/system/drone.service > /dev/null <<EOT
[Unit]
Description=Drone server
After=docker.service caddy.service
[Service]
Restart=always
ExecStart=/usr/local/bin/docker-compose -f /etc/drone/docker-compose.yml up
ExecStop=/usr/local/bin/docker-compose -f /etc/drone/docker-compose.yml stop
[Install]
WantedBy=multi-user.target
EOT
# Start services
sudo systemctl restart caddy
sudo systemctl status caddy
sleep 5
sudo systemctl restart drone
sudo systemctl status drone
sleep 5
echo ""
# Next steps
echo "Drone initial setup complete"
echo "Visit https://${DOMAIN} to configure"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment