provisioning-catalog/components/devel_user_vol/taskserv/prepare

77 lines
3 KiB
Text
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env nu
# Prepare script for devel_user_vol taskserv.
# Runs on the operator side (HCLOUD_TOKEN must be set in the environment).
# Ensures the Hetzner volume exists and is attached to the target server
# before the install script runs on the node.
let vars_path = ($env.PROVISIONING_VARS? | default "")
if ($vars_path | is-empty) or not ($vars_path | path exists) {
print $"🛑 PROVISIONING_VARS not set or not found: ($vars_path)"
exit 1
}
let wk_data = (open $vars_path)
let vol_name = ($wk_data | get -o taskserv.volume_name | default "")
let vol_size = ($wk_data | get -o taskserv.volume_size_gb | default 50 | into int)
let vol_loc = ($wk_data | get -o taskserv.hcloud_location | default "fsn1")
let fs_type = ($wk_data | get -o taskserv.fs_type | default "ext4")
let server = ($wk_data | get -o server.hostname | default "")
if ($vol_name | is-empty) {
print "🛑 taskserv.volume_name is not set"
exit 1
}
if ($server | is-empty) {
print "🛑 server.hostname is not set in workspace vars"
exit 1
}
if ($env.HCLOUD_TOKEN? | default "" | is-empty) {
print "🛑 HCLOUD_TOKEN not set — required for Hetzner API operations"
exit 1
}
# ── Volume: create if absent ───────────────────────────────────────────────────
let describe = (do { ^hcloud volume describe $vol_name -o json } | complete)
if $describe.exit_code == 0 {
let vol = ($describe.stdout | from json)
print $" Volume '($vol_name)' exists — ($vol.size)GB at ($vol.location.name)"
# ── Attach: ensure it is attached to the correct server ───────────────────
let srv_raw = ($vol | get -o server | default null)
let current = if ($srv_raw | describe) starts-with "record" {
($srv_raw | get -o name | default "")
} else { "" }
if $current == $server {
print $"✓ Volume '($vol_name)' already attached to '($server)'"
} else if ($current | is-not-empty) {
print $"🛑 Volume '($vol_name)' is attached to '($current)' — detach it first"
print $" Run: hcloud volume detach ($vol_name)"
exit 1
} else {
let att = (do { ^hcloud volume attach $vol_name --server $server } | complete)
if $att.exit_code != 0 {
print $"🛑 Failed to attach volume: ($att.stderr)"
exit 1
}
print $"✓ Volume '($vol_name)' attached to '($server)'"
}
} else {
print $"⚙ Volume '($vol_name)' not found — creating ($vol_size)GB at ($vol_loc)"
let create = (do {
^hcloud volume create
--name $vol_name
--size ($vol_size | into string)
--location $vol_loc
--format $fs_type
--server $server
} | complete)
if $create.exit_code != 0 {
print $"🛑 Failed to create volume: ($create.stderr)"
exit 1
}
print $"✓ Volume '($vol_name)' created — ($vol_size)GB at ($vol_loc), attached to '($server)'"
}