// This script demonstrates how to rebuild a server with a new image using install_image. // The install_image function is the Hetzner Cloud equivalent of the traditional installimage command. // 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 rebuild. // WARNING: This will DESTROY ALL DATA on the target server! let server_id = 1234567; // FIXME: Replace with a real server ID // The install_image function rebuilds a server by overwriting its disk with a new image. // This is equivalent to the traditional installimage command used on dedicated servers. // // Available image types: // - system: Official OS images (e.g., "ubuntu-22.04", "debian-12") // - backup: Automatic backups of your servers // - snapshot: Manual snapshots you've created // - app: Application images (if available) // // The function accepts either an image name or image ID: // client.install_image(server_id, "ubuntu-22.04"); // By name // client.install_image(server_id, "15512617"); // By ID // Get current server information before rebuilding. print("Getting current server information..."); let server = client.get_server(server_id); print(`Server: ${server.name} (ID: ${server.id})`); print(`Current Status: ${server.status}`); print(`Current Image: ${server.image.name}`); print(""); // List available system images to choose from. print("Available system images (first 5):"); let system_images_params = new_list_images_params_builder() .with_type("system") .with_status("available"); let system_images = client.list_images(system_images_params); for i in 0..5 { if i < system_images.len() { let img = system_images[i]; print(` ${img.id}: ${img.name} (${img.type})`); } } print(""); // List backup images for this server (if any). print("Backup images for this server:"); let backup_images_params = new_list_images_params_builder() .with_type("backup") .with_bound_to(server_id.to_string()); let backup_images = client.list_images(backup_images_params); if backup_images.len() > 0 { for i in 0..backup_images.len() { let img = backup_images[i]; print(` ${img.id}: ${img.name} (${img.type})`); } } else { print(" No backup images found for this server"); } print(""); // --- Rebuild Server with System Image --- // WARNING: This will DESTROY ALL DATA on the server! // The server will be automatically powered off before the rebuild. // Example 1: Rebuild with Ubuntu 22.04 (by name) // WARNING: Uncomment the following lines to perform the actual rebuild // print("Initiating server rebuild with Ubuntu 22.04..."); // client.install_image(server_id, "ubuntu-22.04"); // print("✅ Server rebuild request sent successfully!"); print("⚠️ install_image call is commented out for safety. Uncomment to execute."); print(""); // Example 2: Rebuild with specific image ID // client.install_image(server_id, "15512617"); // Replace with actual image ID // print("Server rebuild initiated with image ID 15512617"); // --- Restore from Backup --- // If backup images are available, you can restore from them: if backup_images.len() > 0 { let backup_img = backup_images[0]; // client.install_image(server_id, backup_img.id.to_string()); // print(`Server restore initiated from backup: ${backup_img.name}`); // print(`Backup available: ${backup_img.name} (ID: ${backup_img.id})`); } else { // print("No backup images available for this server"); } // --- Monitor Rebuild Progress --- // The following section demonstrates how to monitor rebuild progress // Uncomment when you uncomment the install_image call above /* print("Monitoring server status during rebuild..."); print("Note: Server rebuild typically takes 1-3 minutes to complete."); print(""); // Poll server status every 5 seconds to monitor progress let max_attempts = 60; // Maximum 5 minutes of polling let attempt = 0; let rebuild_seen = false; let running_count = 0; while attempt < max_attempts { let current_server = client.get_server(server_id); let status = current_server.status; // Use status directly print(`Attempt ${attempt + 1}: Server status is '${status}'`); // Check for rebuilding status (case-insensitive) if status == "Rebuilding" || status == "rebuilding" { rebuild_seen = true; print(" → Server is currently being rebuilt..."); } else if status == "Off" || status == "off" { print(" → Server is powered off (normal during rebuild)"); } else if status == "Running" || status == "running" { if rebuild_seen { print(""); print("🎉 Server rebuild completed successfully!"); print(`✅ Server is now running with image: ${current_server.image.name}`); // Verify the image changed if current_server.image.name == "ubuntu-22.04" || current_server.image.name == "Ubuntu 22.04" { print("✅ Image verification: Ubuntu 22.04 installation confirmed!"); } else { print(`⚠️ Image verification: Expected 'ubuntu-22.04', got '${current_server.image.name}'`); } break; // Exit immediately after rebuild completion } else { print(" → Server is running (waiting for rebuild to start...)"); } } else { print(` → Server status: ${status}`); } attempt = attempt + 1; if attempt < max_attempts { print(" Waiting 5 seconds before next check..."); sleep(5); // Wait 5 seconds before polling again } } if attempt >= max_attempts { print(""); print("⚠️ Timeout: Server rebuild is taking longer than expected."); print(" Check the Hetzner Cloud console for current status."); } print(""); print("💡 Tip: You can also monitor rebuild progress in the Hetzner Cloud console."); */ print(""); print("To use this example:"); print("1. Replace server_id with your actual server ID"); print("2. Uncomment the install_image call and monitoring section"); print("3. Run the script to rebuild your server"); print(""); print("⚠️ Remember: install_image will DESTROY ALL DATA on the target server!");