Install Steam on persistent data disk that is never destroyed by terraform
This commit is contained in:
@@ -54,6 +54,19 @@ resource "azurerm_network_interface" "vm_nic" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "azurerm_managed_disk" "data_disk" {
|
||||||
|
name = "${var.prefix}-winvm-datadisk"
|
||||||
|
location = azurerm_resource_group.rg.location
|
||||||
|
resource_group_name = azurerm_resource_group.rg.name
|
||||||
|
storage_account_type = "Premium_LRS"
|
||||||
|
create_option = "Empty"
|
||||||
|
disk_size_gb = var.datadisk_size_gb
|
||||||
|
|
||||||
|
lifecycle {
|
||||||
|
prevent_destroy = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resource "random_password" "admin_password" {
|
resource "random_password" "admin_password" {
|
||||||
length = 16
|
length = 16
|
||||||
special = false
|
special = false
|
||||||
@@ -65,6 +78,8 @@ resource "azurerm_windows_virtual_machine" "vm" {
|
|||||||
resource_group_name = azurerm_resource_group.rg.name
|
resource_group_name = azurerm_resource_group.rg.name
|
||||||
location = azurerm_resource_group.rg.location
|
location = azurerm_resource_group.rg.location
|
||||||
size = var.vm_size
|
size = var.vm_size
|
||||||
|
priority = var.vm_priority
|
||||||
|
eviction_policy = var.vm_priority == "Spot" ? "Deallocate" : null
|
||||||
|
|
||||||
admin_username = var.vm_admin_username
|
admin_username = var.vm_admin_username
|
||||||
admin_password = random_password.admin_password.result
|
admin_password = random_password.admin_password.result
|
||||||
@@ -86,6 +101,13 @@ resource "azurerm_windows_virtual_machine" "vm" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "azurerm_virtual_machine_data_disk_attachment" "data_disk_attachment" {
|
||||||
|
managed_disk_id = azurerm_managed_disk.data_disk.id
|
||||||
|
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
|
||||||
|
lun = var.datadisk_lun
|
||||||
|
caching = "ReadWrite"
|
||||||
|
}
|
||||||
|
|
||||||
resource "azurerm_virtual_machine_extension" "provision_software" {
|
resource "azurerm_virtual_machine_extension" "provision_software" {
|
||||||
name = "provision-software"
|
name = "provision-software"
|
||||||
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
|
virtual_machine_id = azurerm_windows_virtual_machine.vm.id
|
||||||
@@ -93,9 +115,11 @@ resource "azurerm_virtual_machine_extension" "provision_software" {
|
|||||||
type = "CustomScriptExtension"
|
type = "CustomScriptExtension"
|
||||||
type_handler_version = "1.10"
|
type_handler_version = "1.10"
|
||||||
|
|
||||||
|
depends_on = [azurerm_virtual_machine_data_disk_attachment.data_disk_attachment]
|
||||||
|
|
||||||
protected_settings = <<SETTINGS
|
protected_settings = <<SETTINGS
|
||||||
{
|
{
|
||||||
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(templatefile("${path.module}/scripts/provision-software.ps1.tpl", {tailscale_authkey = var.tailscale_authkey}))}')) | Out-File -filepath provision-software.ps1\" && powershell -ExecutionPolicy Unrestricted -File provision-software.ps1"
|
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(templatefile("${path.module}/scripts/provision-software.ps1.tpl", { tailscale_authkey = var.tailscale_authkey, datadisk_lun = var.datadisk_lun, datadisk_drive_letter = var.datadisk_drive_letter }))}')) | Out-File -filepath provision-software.ps1\" && powershell -ExecutionPolicy Unrestricted -File provision-software.ps1"
|
||||||
}
|
}
|
||||||
SETTINGS
|
SETTINGS
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
$DataDiskLun = '${datadisk_lun}'
|
||||||
|
$DataDiskDriveLetter = '${datadisk_drive_letter}'
|
||||||
|
|
||||||
$TailscaleUri = 'https://pkgs.tailscale.com/stable/tailscale-setup-1.88.3-amd64.msi'
|
$TailscaleUri = 'https://pkgs.tailscale.com/stable/tailscale-setup-1.88.3-amd64.msi'
|
||||||
$TailscaleMsi = 'tailscale.msi'
|
$TailscaleMsi = 'tailscale.msi'
|
||||||
$TailscaleInstalledExe = 'C:\Program Files\Tailscale\tailscale.exe'
|
$TailscaleInstalledExe = 'C:\Program Files\Tailscale\tailscale.exe'
|
||||||
@@ -15,6 +20,20 @@ $SunshineInstallerExe = 'sunshine-installer.exe'
|
|||||||
$SteamInstallerUri = 'https://cdn.fastly.steamstatic.com/client/installer/SteamSetup.exe'
|
$SteamInstallerUri = 'https://cdn.fastly.steamstatic.com/client/installer/SteamSetup.exe'
|
||||||
$SteamInstallerExe = 'steam-installer.exe'
|
$SteamInstallerExe = 'steam-installer.exe'
|
||||||
|
|
||||||
|
Write-Host "Setting up data disk"
|
||||||
|
|
||||||
|
$DataDisk = Get-Disk | Where-Object { $_.Location -Like "*LUN $DataDiskLun*" }
|
||||||
|
if ($DataDisk.PartitionStyle -eq 'raw') {
|
||||||
|
Initialize-Disk -UniqueId $DataDisk.UniqueId -PartitionStyle GPT
|
||||||
|
New-Partition -DiskId $DataDisk.UniqueId -UseMaximumSize -DriveLetter $DataDiskDriveLetter
|
||||||
|
Format-Volume -DriveLetter $DataDiskDriveLetter -FileSystem NTFS -NewFileSystemLabel DataDisk -Confirm:$False
|
||||||
|
} else {
|
||||||
|
$Partition = Get-Partition -DiskNumber $DataDisk.DiskNumber | Where-Object { $_.Type -eq "Basic" }
|
||||||
|
Set-Partition -InputObject $Partition -NewDriveLetter $DataDiskDriveLetter
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Done setting up data disk"
|
||||||
|
|
||||||
Write-Host "Provisioning software"
|
Write-Host "Provisioning software"
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -66,11 +85,17 @@ Write-Host 'Done installing Sunshine, configure at https://localhost:47990/'
|
|||||||
##
|
##
|
||||||
# Steam
|
# Steam
|
||||||
Write-Host "Installing Steam"
|
Write-Host "Installing Steam"
|
||||||
Write-Host "Downloading Steam installer from $SteamInstallerUri to $SteamInstallerExe"
|
|
||||||
Invoke-WebRequest -UseBasicParsing -Uri $SteamInstallerUri -OutFile $SteamInstallerExe
|
|
||||||
|
|
||||||
Write-Host "Installing Steam from $SteamInstallerExe"
|
if(Test-Path "$($DataDiskDriveLetter):\Steam\Steam.exe") {
|
||||||
Start-Process $SteamInstallerExe -ArgumentList '/S' -Wait -NoNewWindow -PassThru
|
Write-Host "Steam is already installed, skipping"
|
||||||
|
} else {
|
||||||
|
Write-Host "Downloading Steam installer from $SteamInstallerUri to $SteamInstallerExe"
|
||||||
|
Invoke-WebRequest -UseBasicParsing -Uri $SteamInstallerUri -OutFile $SteamInstallerExe
|
||||||
|
|
||||||
|
Write-Host "Installing Steam from $SteamInstallerExe"
|
||||||
|
New-Item -Path "$($DataDiskDriveLetter):\" -Name 'Steam' -ItemType Directory
|
||||||
|
Start-Process $SteamInstallerExe -ArgumentList '/S', "/D=$($DataDiskDriveLetter):\Steam" -Wait -NoNewWindow -PassThru
|
||||||
|
}
|
||||||
|
|
||||||
Write-Host 'Done installing Steam'
|
Write-Host 'Done installing Steam'
|
||||||
|
|
||||||
|
|||||||
@@ -59,12 +59,36 @@ variable "vm_size" {
|
|||||||
default = "Standard_NG16ads_V620_v1"
|
default = "Standard_NG16ads_V620_v1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "vm_priority" {
|
||||||
|
description = "Priority of the VM, can be Regular or Spot. Spot is cheaper, but can be evicted at any time"
|
||||||
|
type = string
|
||||||
|
default = "Regular"
|
||||||
|
}
|
||||||
|
|
||||||
variable "vm_admin_username" {
|
variable "vm_admin_username" {
|
||||||
description = "VM admin username, password will be generated randomly"
|
description = "VM admin username, password will be generated randomly"
|
||||||
type = string
|
type = string
|
||||||
default = "jona"
|
default = "jona"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "datadisk_size_gb" {
|
||||||
|
description = "Size of the persisted datadisk in gb"
|
||||||
|
type = number
|
||||||
|
default = 1024
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "datadisk_lun" {
|
||||||
|
description = "Location identifier, this is used to identify the disk within the VM"
|
||||||
|
type = string
|
||||||
|
default = "10"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "datadisk_drive_letter" {
|
||||||
|
description = "Drive letter to mount the datadisk to"
|
||||||
|
type = string
|
||||||
|
default = "Z"
|
||||||
|
}
|
||||||
|
|
||||||
variable "tailscale_authkey" {
|
variable "tailscale_authkey" {
|
||||||
description = "Tailscale auth key for unattended login"
|
description = "Tailscale auth key for unattended login"
|
||||||
type = string
|
type = string
|
||||||
|
|||||||
Reference in New Issue
Block a user