{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "description": "A SpanDock server (hub or satellite) on an Azure VM: Ubuntu 24.04, no inbound rule (SpanDock only connects out), boot diagnostics on. A few minutes after it boots, the VM's Serial console (or Boot diagnostics -> Serial log) shows the link that activates the server, once. https://www.spandock.com/docs/deploy"
  },
  "parameters": {
    "vmName": { "type": "string", "defaultValue": "spandock", "metadata": { "description": "The VM's name." } },
    "vmSize": { "type": "string", "defaultValue": "Standard_B2s", "metadata": { "description": "2 vCPU and 4 GB suit up to about 50 developers." } },
    "diskGB": { "type": "int", "defaultValue": 30, "minValue": 30, "maxValue": 4095 },
    "adminUsername": { "type": "string", "defaultValue": "spandockadmin" },
    "sshPublicKey": { "type": "string", "metadata": { "description": "An SSH public key for the admin user (Azure requires one; SSH stays closed unless you open it)." } },
    "joinCode": { "type": "securestring", "defaultValue": "", "metadata": { "description": "Only for a satellite: a single-use join code from the hub (Settings -> Scaling -> Add server)." } },
    "spandockVersion": { "type": "string", "defaultValue": "", "metadata": { "description": "A release such as v0.57.0; empty installs the latest." } },
    "location": { "type": "string", "defaultValue": "[resourceGroup().location]" }
  },
  "variables": {
    "cloudInit": "[format('#cloud-config\npackage_update: true\npackages:\n  - curl\n  - ca-certificates\nruncmd:\n  - [bash, -c, \"curl -fsSL --retry 3 https://www.spandock.com/deploy/install-server.sh | SPANDOCK_VERSION=''{0}'' SPANDOCK_JOIN_CODE=''{1}'' bash\"]\n', parameters('spandockVersion'), parameters('joinCode'))]",
    "vnetName": "[concat(parameters('vmName'), '-vnet')]",
    "nsgName": "[concat(parameters('vmName'), '-nsg')]",
    "ipName": "[concat(parameters('vmName'), '-ip')]",
    "nicName": "[concat(parameters('vmName'), '-nic')]",
    "tags": { "app": "spandock" }
  },
  "resources": [
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2023-09-01",
      "name": "[variables('nsgName')]",
      "location": "[parameters('location')]",
      "tags": "[variables('tags')]",
      "properties": { "securityRules": [] }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2023-09-01",
      "name": "[variables('vnetName')]",
      "location": "[parameters('location')]",
      "tags": "[variables('tags')]",
      "dependsOn": ["[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"],
      "properties": {
        "addressSpace": { "addressPrefixes": ["10.70.0.0/24"] },
        "subnets": [{
          "name": "default",
          "properties": {
            "addressPrefix": "10.70.0.0/24",
            "networkSecurityGroup": { "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]" }
          }
        }]
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "2023-09-01",
      "name": "[variables('ipName')]",
      "location": "[parameters('location')]",
      "tags": "[variables('tags')]",
      "sku": { "name": "Standard" },
      "properties": { "publicIPAllocationMethod": "Static" }
    },
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2023-09-01",
      "name": "[variables('nicName')]",
      "location": "[parameters('location')]",
      "tags": "[variables('tags')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]",
        "[resourceId('Microsoft.Network/publicIPAddresses', variables('ipName'))]"
      ],
      "properties": {
        "ipConfigurations": [{
          "name": "ipconfig1",
          "properties": {
            "privateIPAllocationMethod": "Dynamic",
            "subnet": { "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), 'default')]" },
            "publicIPAddress": { "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('ipName'))]" }
          }
        }]
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2024-03-01",
      "name": "[parameters('vmName')]",
      "location": "[parameters('location')]",
      "tags": "[variables('tags')]",
      "dependsOn": ["[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"],
      "properties": {
        "hardwareProfile": { "vmSize": "[parameters('vmSize')]" },
        "storageProfile": {
          "imageReference": { "publisher": "Canonical", "offer": "ubuntu-24_04-lts", "sku": "server", "version": "latest" },
          "osDisk": { "createOption": "FromImage", "diskSizeGB": "[parameters('diskGB')]", "managedDisk": { "storageAccountType": "StandardSSD_LRS" }, "deleteOption": "Delete" }
        },
        "osProfile": {
          "computerName": "[parameters('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "customData": "[base64(variables('cloudInit'))]",
          "linuxConfiguration": {
            "disablePasswordAuthentication": true,
            "ssh": { "publicKeys": [{ "path": "[format('/home/{0}/.ssh/authorized_keys', parameters('adminUsername'))]", "keyData": "[parameters('sshPublicKey')]" }] }
          }
        },
        "networkProfile": { "networkInterfaces": [{ "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]", "properties": { "deleteOption": "Delete" } }] },
        "diagnosticsProfile": { "bootDiagnostics": { "enabled": true } },
        "securityProfile": { "securityType": "TrustedLaunch", "uefiSettings": { "secureBootEnabled": true, "vTpmEnabled": true } }
      }
    }
  ],
  "outputs": {
    "activate": {
      "type": "string",
      "value": "Open the VM -> Help -> Serial console (or Boot diagnostics -> Serial log): the activation link appears a few minutes after boot."
    }
  }
}
