179 lines
5.2 KiB
Text
179 lines
5.2 KiB
Text
|
|
#!/bin/bash
|
|||
|
|
# provisioning {{provisioning_vers}} AWS EBS volume creation and attachment: {{now}}
|
|||
|
|
{%- if debug and debug == "yes" %} set -x {% endif %}
|
|||
|
|
[ -z "$AWS_DEFAULT_REGION" ] && echo "AWS region not configured" && exit 1
|
|||
|
|
|
|||
|
|
{# Validate prerequisites #}
|
|||
|
|
{%- if storages and storages|length > 0 %}
|
|||
|
|
|
|||
|
|
echo "=== Creating AWS EBS Volumes ==="
|
|||
|
|
{%- for storage in storages %}
|
|||
|
|
{%- if storage.name %}
|
|||
|
|
|
|||
|
|
# Create EBS volume: {{ storage.name }}
|
|||
|
|
echo ""
|
|||
|
|
echo "Creating EBS volume: {{ storage.name }}"
|
|||
|
|
|
|||
|
|
{%- if storage.size %}
|
|||
|
|
VOLUME_SIZE={{ storage.size }}
|
|||
|
|
{%- elif defaults.size and defaults.size|int > 0 %}
|
|||
|
|
VOLUME_SIZE={{ defaults.size }}
|
|||
|
|
{%- else %}
|
|||
|
|
VOLUME_SIZE=20
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- if storage.zone %}
|
|||
|
|
AVAILABILITY_ZONE="{{ storage.zone }}"
|
|||
|
|
{%- elif server and server.zone %}
|
|||
|
|
AVAILABILITY_ZONE="{{ server.zone }}"
|
|||
|
|
{%- elif defaults.zone %}
|
|||
|
|
AVAILABILITY_ZONE="{{ defaults.zone }}"
|
|||
|
|
{%- else %}
|
|||
|
|
AVAILABILITY_ZONE="us-east-1a"
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- if storage.voltype %}
|
|||
|
|
VOLUME_TYPE="{{ storage.voltype }}"
|
|||
|
|
{%- elif defaults.voltype %}
|
|||
|
|
VOLUME_TYPE="{{ defaults.voltype }}"
|
|||
|
|
{%- else %}
|
|||
|
|
VOLUME_TYPE="gp3"
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
# Create volume with specified parameters
|
|||
|
|
VOLUME_ID=$(^aws ec2 create-volume \
|
|||
|
|
--size $VOLUME_SIZE \
|
|||
|
|
--availability-zone $AVAILABILITY_ZONE \
|
|||
|
|
--volume-type $VOLUME_TYPE \
|
|||
|
|
{%- if storage.encrypted and storage.encrypted == true %} \
|
|||
|
|
--encrypted \
|
|||
|
|
{%- endif %} \
|
|||
|
|
{%- if storage.labels %} \
|
|||
|
|
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value="{{ storage.name }}"},{Key=Labels,Value="{{ storage.labels }}"}]' \
|
|||
|
|
{%- else %} \
|
|||
|
|
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value="{{ storage.name }}"}]' \
|
|||
|
|
{%- endif %} \
|
|||
|
|
--query 'VolumeId' \
|
|||
|
|
--output text 2>/dev/null)
|
|||
|
|
|
|||
|
|
if [ -z "$VOLUME_ID" ]; then
|
|||
|
|
echo "❌ Failed to create EBS volume {{ storage.name }}"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
echo "✅ EBS volume {{ storage.name }} created: $VOLUME_ID (Size: $VOLUME_SIZE GB, Type: $VOLUME_TYPE)"
|
|||
|
|
|
|||
|
|
{%- if server and server.hostname %}
|
|||
|
|
|
|||
|
|
# Wait for volume to be available
|
|||
|
|
echo "Waiting for volume to be available..."
|
|||
|
|
aws ec2 wait volume-available --volume-ids $VOLUME_ID --region $AWS_DEFAULT_REGION 2>/dev/null
|
|||
|
|
|
|||
|
|
# Get server instance ID
|
|||
|
|
INSTANCE_ID=$(^aws ec2 describe-instances \
|
|||
|
|
--filters "Name=tag:hostname,Values={{ server.hostname }}" "Name=instance-state-name,Values=running" \
|
|||
|
|
--query "Reservations[0].Instances[0].InstanceId" \
|
|||
|
|
--output text 2>/dev/null)
|
|||
|
|
|
|||
|
|
if [ -n "$INSTANCE_ID" ] && [ "$INSTANCE_ID" != "None" ]; then
|
|||
|
|
echo "Attaching volume to {{ server.hostname }}..."
|
|||
|
|
|
|||
|
|
{%- if storage.voldevice %}
|
|||
|
|
DEVICE="{{ storage.voldevice }}"
|
|||
|
|
{%- else %}
|
|||
|
|
# Determine next available device
|
|||
|
|
DEVICE="/dev/sdf"
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
ATTACH_RESULT=$(^aws ec2 attach-volume \
|
|||
|
|
--volume-id $VOLUME_ID \
|
|||
|
|
--instance-id $INSTANCE_ID \
|
|||
|
|
--device $DEVICE \
|
|||
|
|
--query 'State' \
|
|||
|
|
--output text 2>/dev/null)
|
|||
|
|
|
|||
|
|
if [ "$ATTACH_RESULT" != "attaching" ] && [ "$ATTACH_RESULT" != "attached" ]; then
|
|||
|
|
echo "⚠️ Volume created but attachment to {{ server.hostname }} may have failed"
|
|||
|
|
echo "Manual attachment may be required using:"
|
|||
|
|
echo " aws ec2 attach-volume --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device $DEVICE"
|
|||
|
|
else
|
|||
|
|
echo "✅ Volume $VOLUME_ID attached to {{ server.hostname }} on device $DEVICE"
|
|||
|
|
fi
|
|||
|
|
else
|
|||
|
|
echo "ℹ️ Server {{ server.hostname }} not found in running state"
|
|||
|
|
echo "Volume created but not attached: $VOLUME_ID"
|
|||
|
|
echo "Manual attachment required when server is running"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
{%- else %}
|
|||
|
|
|
|||
|
|
echo "ℹ️ No server specified for attachment"
|
|||
|
|
echo "Volume created but not attached: $VOLUME_ID"
|
|||
|
|
echo "To attach this volume to an instance:"
|
|||
|
|
echo " aws ec2 attach-volume --volume-id $VOLUME_ID --instance-id <instance-id> --device /dev/sdf"
|
|||
|
|
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- if storage.backup and storage.backup.enabled %}
|
|||
|
|
|
|||
|
|
# Configure volume snapshots for backup
|
|||
|
|
echo "Creating volume snapshot schedule for {{ storage.name }}..."
|
|||
|
|
{%- if storage.backup.schedule %}
|
|||
|
|
SNAPSHOT_SCHEDULE="{{ storage.backup.schedule }}"
|
|||
|
|
{%- else %}
|
|||
|
|
SNAPSHOT_SCHEDULE="daily"
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- if storage.backup.retention %}
|
|||
|
|
RETENTION={{ storage.backup.retention }}
|
|||
|
|
{%- else %}
|
|||
|
|
RETENTION=7
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
echo "ℹ️ Snapshot schedule: $SNAPSHOT_SCHEDULE, Retention: $RETENTION days"
|
|||
|
|
echo "Note: Automated snapshots require Data Lifecycle Manager configuration"
|
|||
|
|
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- endif %}
|
|||
|
|
{%- endfor %}
|
|||
|
|
|
|||
|
|
echo ""
|
|||
|
|
echo "=== EBS Volume Creation Complete ==="
|
|||
|
|
{%- else %}
|
|||
|
|
|
|||
|
|
echo "No EBS volumes defined for this configuration"
|
|||
|
|
|
|||
|
|
{%- endif %}
|
|||
|
|
|
|||
|
|
{%- if iops_optimization %}
|
|||
|
|
|
|||
|
|
# Additional IOPS optimization for performance-critical volumes
|
|||
|
|
echo ""
|
|||
|
|
echo "=== Optimizing Volume Performance ==="
|
|||
|
|
|
|||
|
|
{%- for storage in storages %}
|
|||
|
|
{%- if storage.iops %}
|
|||
|
|
|
|||
|
|
echo "Setting IOPS for {{ storage.name }}..."
|
|||
|
|
VOLUME_ID=$(^aws ec2 describe-volumes \
|
|||
|
|
--filters "Name=tag:Name,Values={{ storage.name }}" \
|
|||
|
|
--query "Volumes[0].VolumeId" \
|
|||
|
|
--output text 2>/dev/null)
|
|||
|
|
|
|||
|
|
if [ -n "$VOLUME_ID" ] && [ "$VOLUME_ID" != "None" ]; then
|
|||
|
|
^aws ec2 modify-volume-attribute \
|
|||
|
|
--volume-id $VOLUME_ID \
|
|||
|
|
--iops {{ storage.iops }} \
|
|||
|
|
--query 'VolumeAttribute.Iops' \
|
|||
|
|
--output text 2>/dev/null
|
|||
|
|
echo "✅ IOPS optimized for $VOLUME_ID ({{ storage.iops }} IOPS)"
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
{%- endif %}
|
|||
|
|
{%- endfor %}
|
|||
|
|
|
|||
|
|
echo "=== Performance Optimization Complete ==="
|
|||
|
|
|
|||
|
|
{%- endif %}
|