98 lines
3.3 KiB
Django/Jinja
98 lines
3.3 KiB
Django/Jinja
#!/bin/bash
|
||
# provisioning {{provisioning_vers}} UpCloud storage creation and attachment: {{now}}
|
||
{%- if debug and debug == "yes" %} set -x {% endif %}
|
||
[ -z "$UPCLOUD_USERNAME" ] && [ ! -r "$HOME/.config/upctl.yaml" ] && echo "UpCloud credentials not found" && exit 1
|
||
|
||
{# Validate prerequisites #}
|
||
{%- if storages and storages|length > 0 %}
|
||
|
||
echo "=== Creating UpCloud Storage Volumes ==="
|
||
{%- for storage in storages %}
|
||
{%- if storage.name %}
|
||
|
||
# Create storage: {{ storage.name }}
|
||
if upctl storage show "{{ storage.name }}" >/dev/null 2>/dev/null ; then
|
||
echo "Storage {{ storage.name }} already exists."
|
||
else
|
||
echo "Creating storage: {{ storage.name }}"
|
||
upctl storage create \
|
||
--title "{{ storage.name }}" \
|
||
{%- if storage.size %} \
|
||
--size {{ storage.size }} \
|
||
{%- elif defaults.size and defaults.size|int > 0 %}
|
||
--size {{ defaults.size }} \
|
||
{%- else %}
|
||
--size 10 \
|
||
{%- endif -%}
|
||
{%- if storage.zone %}
|
||
--zone "{{ storage.zone }}" \
|
||
{%- elif server and server.zone %} \
|
||
--zone "{{ server.zone }}" \
|
||
{%- elif defaults.zone and defaults.zone != '' %}
|
||
--zone "{{ defaults.zone }}" \
|
||
{%- else %}
|
||
--zone "es-mad1" \
|
||
{%- endif -%}
|
||
{%- if storage.voltype %} \
|
||
--tier "{{ storage.voltype }}" \
|
||
{%- elif defaults.voltype and defaults.voltype != '' %}
|
||
--tier "{{ defaults.voltype }}" \
|
||
{%- else %}
|
||
--tier "maxiops" \
|
||
{%- endif -%}
|
||
{%- if storage.encrypted and storage.encrypted == true %} \
|
||
--encrypted \
|
||
{%- endif -%}
|
||
{%- if storage.backup %} \
|
||
--backup-time "{{ storage.backup.time | default('04:00') }}" \
|
||
--backup-interval "{{ storage.backup.interval | default(24) }}" \
|
||
--backup-retention "{{ storage.backup.retention | default(30) }}" \
|
||
{%- endif -%}
|
||
{%- if debug and debug == "yes" %} \
|
||
-o json \
|
||
{%- endif %}
|
||
if [ $? -eq 0 ]; then
|
||
echo "✅ Storage {{ storage.name }} created successfully"
|
||
|
||
{# Optionally attach to server #}
|
||
{%- if server and server.hostname %}
|
||
echo "Attaching storage {{ storage.name }} to {{ server.hostname }}"
|
||
STORAGE_UUID=$(upctl storage show "{{ storage.name }}" -o json | grep -o '"uuid":"[^"]*' | cut -d'"' -f4)
|
||
SERVER_UUID=$(upctl server show "{{ server.hostname }}" -o json | grep -o '"uuid":"[^"]*' | cut -d'"' -f4)
|
||
|
||
if [ -n "$STORAGE_UUID" ] && [ -n "$SERVER_UUID" ]; then
|
||
upctl server storage attach "$SERVER_UUID" \
|
||
--storage "$STORAGE_UUID" \
|
||
{%- if storage.voldevice %} \
|
||
--address "{{ storage.voldevice }}" \
|
||
{%- endif -%}
|
||
{%- if debug and debug == "yes" %} \
|
||
-o json \
|
||
{%- endif %}
|
||
|
||
if [ $? -eq 0 ]; then
|
||
echo "✅ Storage {{ storage.name }} attached to {{ server.hostname }}"
|
||
else
|
||
echo "⚠️ Storage {{ storage.name }} created but attachment to {{ server.hostname }} failed"
|
||
fi
|
||
else
|
||
echo "⚠️ Storage {{ storage.name }} created but could not attach (UUID mismatch)"
|
||
fi
|
||
{%- else %}
|
||
echo "ℹ️ Storage {{ storage.name }} created but not attached (no server specified)"
|
||
{%- endif %}
|
||
else
|
||
echo "❌ Failed to create storage {{ storage.name }}"
|
||
fi
|
||
fi
|
||
|
||
{%- endif %}
|
||
{%- endfor %}
|
||
|
||
echo ""
|
||
echo "=== Storage Creation Complete ==="
|
||
{%- else %}
|
||
|
||
echo "No storages defined for this configuration"
|
||
|
||
{%- endif %}
|