provisioning-catalog/providers/digitalocean/templates/digitalocean_droplets.j2

78 lines
2.3 KiB
Django/Jinja

#!/bin/bash
# DigitalOcean Droplet provisioning script
# Generated by provisioning {{ provisioning_version }} at {{ now }}
# Reference: DigitalOcean API v2 - https://docs.digitalocean.com/reference/api/
set -e
{% if debug and debug == "yes" %}set -x{% endif %}
# Validate required environment
if [ -z "$DIGITALOCEAN_TOKEN" ]; then
echo "ERROR: DIGITALOCEAN_TOKEN environment variable not set"
exit 1
fi
# Droplet configuration
DROPLET_NAME="{{ droplet.name }}"
DROPLET_REGION="{{ droplet.region | default('nyc3') }}"
DROPLET_SIZE="{{ droplet.size | default('s-1vcpu-1gb-30gb') }}"
DROPLET_IMAGE="{{ droplet.image | default('ubuntu-22-04-x64') }}"
DROPLET_BACKUPS="{{ droplet.backups | default('false') }}"
DROPLET_IPV6="{{ droplet.ipv6 | default('true') }}"
# Validate required fields
if [ -z "$DROPLET_NAME" ]; then
echo "ERROR: Droplet name is required"
exit 1
fi
# Build JSON payload
cat > /tmp/droplet_payload.json << EOF
{
"name": "$DROPLET_NAME",
"region": "$DROPLET_REGION",
"size": "$DROPLET_SIZE",
"image": "$DROPLET_IMAGE",
"backups": $DROPLET_BACKUPS,
"ipv6": $DROPLET_IPV6
{% if droplet.ssh_keys %},"ssh_keys": [{{ droplet.ssh_keys | join(', ') }}]{% endif %}
}
EOF
# Create droplet using doctl (DigitalOcean CLI)
doctl compute droplet create \
--region "$DROPLET_REGION" \
--image "$DROPLET_IMAGE" \
--size "$DROPLET_SIZE" \
{% if droplet.backups == 'true' %}--enable-backups {% endif %}\
{% if droplet.ipv6 == 'true' %}--enable-ipv6 {% endif %}\
{% if droplet.ssh_keys %} --ssh-keys {{ droplet.ssh_keys | join(',') }} {% endif %}\
--wait \
--format ID \
--no-header \
"$DROPLET_NAME"
DROPLET_ID=$(doctl compute droplet list --format Name,ID --no-header | grep "^${DROPLET_NAME} " | awk '{print $NF}')
if [ -z "$DROPLET_ID" ]; then
echo "ERROR: Failed to create droplet $DROPLET_NAME"
exit 1
fi
echo "✓ Droplet $DROPLET_NAME created successfully"
echo " ID: $DROPLET_ID"
echo " Region: $DROPLET_REGION"
echo " Size: $DROPLET_SIZE"
{% if droplet.tags %}
# Apply tags
{% for tag in droplet.tags %}
doctl compute droplet tag "$DROPLET_ID" --tag-name "{{ tag }}"
{% endfor %}
echo "✓ Tags applied: {{ droplet.tags | join(', ') }}"
{% endif %}
# Output droplet info
doctl compute droplet get "$DROPLET_ID" --format ID,Name,Status,Memory,VCPUs,Region --no-header
exit 0