89 lines
2.7 KiB
Django/Jinja
89 lines
2.7 KiB
Django/Jinja
#!/bin/bash
|
|
# DigitalOcean Volume (Block Storage) provisioning script
|
|
# Generated by provisioning {{ provisioning_version }} at {{ now }}
|
|
# Reference: DigitalOcean API v2 Volumes - https://docs.digitalocean.com/reference/api/api-reference/#volumes
|
|
|
|
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
|
|
|
|
# Volume configuration
|
|
VOLUME_NAME="{{ volume.name }}"
|
|
VOLUME_SIZE="{{ volume.size | default('100') }}"
|
|
VOLUME_REGION="{{ volume.region | default('nyc3') }}"
|
|
VOLUME_DESCRIPTION="{{ volume.description | default('') }}"
|
|
|
|
# Validate required fields
|
|
if [ -z "$VOLUME_NAME" ]; then
|
|
echo "ERROR: Volume name is required"
|
|
exit 1
|
|
fi
|
|
|
|
# Create volume
|
|
echo "Creating volume: $VOLUME_NAME ($VOLUME_SIZE GiB in $VOLUME_REGION)..."
|
|
|
|
VOLUME_ID=$(doctl compute volume create \
|
|
--region "$VOLUME_REGION" \
|
|
--size "$VOLUME_SIZE" \
|
|
{% if volume.description %}--description "{{ volume.description }}" {% endif %}\
|
|
--format ID \
|
|
--no-header \
|
|
"$VOLUME_NAME")
|
|
|
|
if [ -z "$VOLUME_ID" ]; then
|
|
echo "ERROR: Failed to create volume $VOLUME_NAME"
|
|
exit 1
|
|
fi
|
|
|
|
echo "✓ Volume $VOLUME_NAME created successfully"
|
|
echo " ID: $VOLUME_ID"
|
|
echo " Size: $VOLUME_SIZE GiB"
|
|
echo " Region: $VOLUME_REGION"
|
|
|
|
{% if droplet and droplet.name %}
|
|
# Attach volume to droplet
|
|
echo "Attaching volume to droplet: {{ 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 "WARNING: Droplet '{{ droplet.name }}' not found. Volume created but not attached."
|
|
else
|
|
# Get the device path for this volume
|
|
DEVICE="/dev/disk/by-id/scsi-0DO_${VOLUME_ID:0:8}"
|
|
|
|
doctl compute volume attach \
|
|
"$VOLUME_ID" \
|
|
--droplet-id "$DROPLET_ID" \
|
|
--region "$VOLUME_REGION" \
|
|
--wait
|
|
|
|
echo "✓ Volume attached to droplet {{ droplet.name }}"
|
|
echo " Device path: $DEVICE (mount after SSH)"
|
|
|
|
{% if volume.automount == 'true' %}
|
|
echo " Note: Run the following commands to mount the volume:"
|
|
echo " sudo mkdir -p /mnt/{{ volume.mount_path | default('data') }}"
|
|
echo " sudo mkfs.ext4 $DEVICE # (if first time)"
|
|
echo " sudo mount $DEVICE /mnt/{{ volume.mount_path | default('data') }}"
|
|
{% endif %}
|
|
fi
|
|
{% endif %}
|
|
|
|
{% if volume.tags %}
|
|
# Apply tags
|
|
{% for tag in volume.tags %}
|
|
doctl compute volume tag "$VOLUME_ID" --tag-name "{{ tag }}"
|
|
{% endfor %}
|
|
echo "✓ Tags applied: {{ volume.tags | join(', ') }}"
|
|
{% endif %}
|
|
|
|
# Output volume details
|
|
doctl compute volume get "$VOLUME_ID" --format ID,Region,Size,Name --no-header
|
|
|
|
exit 0
|