hetzner_rhai/examples/04_server_actions.rhai
2025-07-17 14:03:49 +02:00

43 lines
1.8 KiB
Plaintext

// This script demonstrates how to perform various actions on a server,
// such as rebooting, resetting, and managing rescue mode.
// Initialize the Hetzner client with your API token.
let client = new_hetzner_client(get_env("HETZNER_API_TOKEN"));
// Replace this with the ID of the server you want to manage.
let server_id = 1234567; // FIXME: Replace with a real server ID
// --- Reboot Server ---
// A soft reboot is attempted first. If it doesn't succeed, a hard reset is performed.
print(`Rebooting server with ID: ${server_id}...`);
client.reboot(server_id);
print("Reboot command sent.");
// --- Reset Server ---
// This is equivalent to a hard reset (power cycle).
// print(`Resetting server with ID: ${server_id}...`);
// client.reset(server_id);
// print("Reset command sent.");
// --- Enable Rescue Mode ---
// Enables rescue mode for a server. This is useful for maintenance and recovery.
// The `enable_rescue_mode` function can be called in three ways:
// 1. With only the server ID:
// This will enable rescue mode and add all SSH keys from your account to the rescue system.
// let root_password = client.enable_rescue_mode(server_id);
// 2. With a single SSH key ID:
// let ssh_key_id = 98765; // FIXME: Replace with your SSH key ID
// let root_password = client.enable_rescue_mode(server_id, ssh_key_id);
// 3. With an array of SSH key IDs:
// let ssh_key_ids = [98765, 54321]; // FIXME: Replace with your SSH key IDs
// let root_password = client.enable_rescue_mode(server_id, ssh_key_ids);
// print(`Rescue mode enabled. Root password: ${root_password}`);
// --- Disable Rescue Mode ---
// Disables rescue mode and reboots the server back into its normal OS.
// print(`Disabling rescue mode for server ID: ${server_id}...`);
// client.disable_rescue_mode(server_id);
// print("Rescue mode disabled.");