{
 "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
  "virtualMachineName": {
   "type": "string",
   "defaultValue": "mygpuvm",
   "metadata": {
    "description": "The name of the GPU virtual machine to create."
   }
  },
  "virtualMachineSize": {
   "type": "string",
   "defaultValue": "Standard_NC12",
   "allowedValues": [
    "Standard_NC6",
    "Standard_NC12",
    "Standard_NC24",
    "Standard_NC24r"
   ],
   "metadata": {
    "description": "The size of the GPU virtual machine to create."
   }
  },
  "adminUsername": {
   "type": "string",
   "defaultValue": "sshuser",
   "metadata": {
    "description": "These credentials can be used to remotely access the cluster."
   }
  },
  "adminPassword": {
   "type": "securestring",
   "metadata": {
    "description": "The password must be at least 10 characters in length and must contain at least one digit, one non-alphanumeric character, and one upper or lower case letter."
   }
  },
  "existingVirtualNetworkName": {
   "type": "string",
   "metadata": {
    "description": "The name of an existing virtual network."
   }
  },
  "existingSubnetName": {
   "type": "string",
   "metadata": {
    "description": "The name of a subnet in an existing virtual network."
   }
  }
 },
 "variables": {
  "diagnosticsStorageAccount": {
   "name": "[concat('diagsto00', uniqueString(resourceGroup().id))]",
   "type": "Standard_LRS"
  },
  "networkInterfaceName": "[concat(toLower(parameters('virtualMachineName')), '-nic')]",
  "publicIpAddressName": "[concat(parameters('virtualMachineName'), '-ip')]",
  "publicIpAddressType": "Dynamic",
  "networkSecurityGroupName": "[concat(parameters('virtualMachineName'), '-nsg')]",
  "vnetId": "[resourceId('Microsoft.Network/virtualNetworks', parameters('existingVirtualNetworkName'))]",
  "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('existingSubnetName'))]"
 },
 "resources": [
  {
   "name": "[parameters('virtualMachineName')]",
   "type": "Microsoft.Compute/virtualMachines",
   "apiVersion": "2016-04-30-preview",
   "location": "[resourceGroup().location]",
   "dependsOn": [
    "[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]",
    "[concat('Microsoft.Storage/storageAccounts/', variables('diagnosticsStorageAccount').name)]"
   ],
   "properties": {
    "osProfile": {
     "computerName": "[parameters('virtualMachineName')]",
     "adminUsername": "[parameters('adminUsername')]",
     "adminPassword": "[parameters('adminPassword')]"
    },
    "hardwareProfile": {
     "vmSize": "[parameters('virtualMachineSize')]"
    },
    "storageProfile": {
     "imageReference": {
      "publisher": "Canonical",
      "offer": "UbuntuServer",
      "sku": "16.04-LTS",
      "version": "latest"
     },
     "osDisk": {
      "createOption": "fromImage",
      "managedDisk": {
       "storageAccountType": "Standard_LRS"
      }
     },
     "dataDisks": [ ]
    },
    "networkProfile": {
     "networkInterfaces": [
      {
       "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
      }
     ]
    },
    "diagnosticsProfile": {
     "bootDiagnostics": {
      "enabled": true,
      "storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts', variables('diagnosticsStorageAccount').name), '2015-06-15').primaryEndpoints['blob']]"
     }
    }
   },
   "resources": [
    {
     "name": "scriptextension",
     "type": "extensions",
     "location": "[resourceGroup().location]",
     "apiVersion": "2015-06-15",
     "dependsOn": [
      "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
     ],
     "tags": {
      "displayName": "scriptextension"
     },
     "properties": {
      "publisher": "Microsoft.Azure.Extensions",
      "type": "CustomScript",
      "typeHandlerVersion": "2.0",
      "autoUpgradeMinorVersion": true,
      "settings": {
       "fileUris": [
        "[uri(deployment().properties.templateLink.uri, 'gpu-setup.sh')]"
       ],
       "commandToExecute": "./gpu-setup.sh"
      }
     }
    }
   ]
  },
  {
   "name": "[variables('diagnosticsStorageAccount').name]",
   "type": "Microsoft.Storage/storageAccounts",
   "apiVersion": "2015-06-15",
   "location": "[resourceGroup().location]",
   "properties": {
    "accountType": "[variables('diagnosticsStorageAccount').type]"
   }
  },
  {
   "name": "[variables('networkInterfaceName')]",
   "type": "Microsoft.Network/networkInterfaces",
   "apiVersion": "2016-09-01",
   "location": "[resourceGroup().location]",
   "dependsOn": [
    "[concat('Microsoft.Network/networkSecurityGroups/', variables('networkSecurityGroupName'))]"
   ],
   "properties": {
    "ipConfigurations": [
     {
      "name": "ipconfig1",
      "properties": {
       "subnet": {
        "id": "[variables('subnetRef')]"
       },
       "privateIPAllocationMethod": "Dynamic"
      }
     }
    ],
    "networkSecurityGroup": {
     "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
    }
   }
  },
  {
   "name": "[variables('networkSecurityGroupName')]",
   "type": "Microsoft.Network/networkSecurityGroups",
   "apiVersion": "2016-09-01",
   "location": "[resourceGroup().location]",
   "properties": {
    "securityRules": [
     {
      "name": "default-allow-ssh",
      "properties": {
       "priority": 1000,
       "sourceAddressPrefix": "*",
       "protocol": "TCP",
       "destinationPortRange": "22",
       "access": "Allow",
       "direction": "Inbound",
       "sourcePortRange": "*",
       "destinationAddressPrefix": "*"
      }
     }
    ]
   }
  }
 ],
 "outputs": {
  "gpuvm": {
   "type": "object",
   "value": "[reference(resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName')))]"
  }
 }
}
