From 1d1bf1942c4031bd7eb22823594789d6cd4b766a Mon Sep 17 00:00:00 2001 From: Krishna C Vutukuri Date: Tue, 23 Aug 2016 12:08:42 -0700 Subject: [PATCH] Adding Azure Demo (#1922) --- demos/Azure/Azure-Demo.ps1 | 65 +++++++++++ demos/Azure/Compute-Linux.json | 200 +++++++++++++++++++++++++++++++++ demos/Azure/readme.md | 11 ++ 3 files changed, 276 insertions(+) create mode 100644 demos/Azure/Azure-Demo.ps1 create mode 100644 demos/Azure/Compute-Linux.json create mode 100644 demos/Azure/readme.md diff --git a/demos/Azure/Azure-Demo.ps1 b/demos/Azure/Azure-Demo.ps1 new file mode 100644 index 0000000000..e2bf701ae1 --- /dev/null +++ b/demos/Azure/Azure-Demo.ps1 @@ -0,0 +1,65 @@ +### The techniques used in this demo are documented at +### https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/ + +### Import AzureRM.Profile.NetCore.Preview and AzureRM.Resources.NetCore.Preview modules. +### AzureRM.NetCore.Preview is a wrapper module that pulls in these modules +### +### Because of issue https://github.com/PowerShell/PowerShell/issues/1618, +### currently you will not be able to use "Install-Module AzureRM.NetCore.Preview" from +### PowerShellGallery. You can use the following workaround until the issue is fixed: +### +### Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination +### +### Ensure $ENV:PSMODULEPATH is updated with the location you used to install. +Import-Module AzureRM.NetCore.Preview + +### Supply your Azure Credentials +Login-AzureRMAccount + +### Get a name for Azure Resource Group +$resourceGroupName = "PSAzDemo" + (new-guid | % guid) -replace "-","" +$resourceGroupName + +### Creating a New Azure Resource Group +New-AzureRMResourceGroup -Name $resourceGroupName -Location "West US" + +### Deploy an Ubuntu 14.04 VM using Resource Manager cmdlets +### Template is available is at +### http://armviz.io/#/?load=https:%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F101-vm-simple-linux%2Fazuredeploy.json +$dnsLabelPrefix = $resourceGroupName | % tolower +$dnsLabelPrefix +$password = Convertto-Securestring -String "PowerShellRocks!" -AsPlainText -Force +New-AzureRMResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile ./Compute-Linux.json -adminUserName psuser -adminPassword $password -dnsLabelPrefix $dnsLabelPrefix + +### Monitor the status of the deployment +Get-AzureRMResourceGroupDeployment -ResourceGroupName $resourceGroupName + +### Discover the resources we created by the previous deployment +Find-AzureRMResource -ResourceGroupName $resourceGroupName | select Name,ResourceType,Location + +### Get the state of the VM we created +### Notice: The VM is in running state +Get-AzureRMResource -ResourceName MyUbuntuVM -ResourceType Microsoft.Compute/virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | % properties | % instanceview | % statuses + +### Discover the Operations we can perform on the compute resource +### Notice: Operations like "Power Off Virtual Machine", "Start Virtual Machine", "Create Snapshot", "Delete Snapshot", "Delete Virtual Machine" +Get-AzureRMProviderOperation -OperationSearchString Microsoft.Compute/* | select OperationName,Operation + +### Power Off the Virtual Machine we created +Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Compute/virtualMachines -ResourceName MyUbuntuVM -Action poweroff + +### Check the VM State again. It should be stopped now. +Get-AzureRMResource -ResourceName MyUbuntuVM -ResourceType Microsoft.Compute/virtualMachines -ResourceGroupName $resourceGroupName -ODataQuery '$expand=instanceView' | % properties | % instanceview | % statuses + +### As you know, you may still be incurring charges even if the VM is in stopped state +### Deallocate the resource to avoid this charge +Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Compute/virtualMachines -ResourceName MyUbuntuVM -Action deallocate + +### The following command removes the Virtual Machine +Remove-AzureRmResource -ResourceName MyUbuntuVM -ResourceType Microsoft.Compute/virtualMachines -ResourceGroupName $resourceGroupName + +### Look at the resources that still exists +Find-AzureRMResource -ResourceGroupName $resourceGroupName | select Name,ResourceType,Location + +### Remove the ResourceGroup which removes all the resources in the ResourceGroup +Remove-AzureRmResourceGroup -Name $resourceGroupName diff --git a/demos/Azure/Compute-Linux.json b/demos/Azure/Compute-Linux.json new file mode 100644 index 0000000000..c829db408b --- /dev/null +++ b/demos/Azure/Compute-Linux.json @@ -0,0 +1,200 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "adminUsername": { + "type": "string", + "metadata": { + "description": "User name for the Virtual Machine." + } + }, + "adminPassword": { + "type": "securestring", + "metadata": { + "description": "Password for the Virtual Machine." + } + }, + "dnsLabelPrefix": { + "type": "string", + "metadata": { + "description": "Unique DNS Name for the Public IP used to access the Virtual Machine." + } + }, + "ubuntuOSVersion": { + "type": "string", + "defaultValue": "14.04.2-LTS", + "allowedValues": [ + "12.04.5-LTS", + "14.04.2-LTS", + "15.10", + "16.04.0-LTS" + ], + "metadata": { + "description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version. Allowed values: 12.04.5-LTS, 14.04.2-LTS, 15.10, 16.04.0-LTS." + } + } + }, + "variables": { + "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'salinuxvm')]", + "dataDisk1VhdName": "datadisk1", + "imagePublisher": "Canonical", + "imageOffer": "UbuntuServer", + "OSDiskName": "osdiskforlinuxsimple", + "nicName": "myVMNic", + "addressPrefix": "10.0.0.0/16", + "subnetName": "Subnet", + "subnetPrefix": "10.0.0.0/24", + "storageAccountType": "Standard_LRS", + "publicIPAddressName": "myPublicIP", + "publicIPAddressType": "Dynamic", + "vmStorageAccountContainerName": "vhds", + "vmName": "MyUbuntuVM", + "vmSize": "Standard_D1", + "virtualNetworkName": "MyVNET", + "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]", + "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]", + "apiVersion": "2015-06-15" + }, + "resources": [ + { + "type": "Microsoft.Storage/storageAccounts", + "name": "[variables('storageAccountName')]", + "apiVersion": "2016-01-01", + "location": "[resourceGroup().location]", + "sku": { + "name": "[variables('storageAccountType')]" + }, + "kind": "Storage", + "properties": {} + }, + { + "apiVersion": "[variables('apiVersion')]", + "type": "Microsoft.Network/publicIPAddresses", + "name": "[variables('publicIPAddressName')]", + "location": "[resourceGroup().location]", + "properties": { + "publicIPAllocationMethod": "[variables('publicIPAddressType')]", + "dnsSettings": { + "domainNameLabel": "[parameters('dnsLabelPrefix')]" + } + } + }, + { + "apiVersion": "[variables('apiVersion')]", + "type": "Microsoft.Network/virtualNetworks", + "name": "[variables('virtualNetworkName')]", + "location": "[resourceGroup().location]", + "properties": { + "addressSpace": { + "addressPrefixes": [ + "[variables('addressPrefix')]" + ] + }, + "subnets": [ + { + "name": "[variables('subnetName')]", + "properties": { + "addressPrefix": "[variables('subnetPrefix')]" + } + } + ] + } + }, + { + "apiVersion": "[variables('apiVersion')]", + "type": "Microsoft.Network/networkInterfaces", + "name": "[variables('nicName')]", + "location": "[resourceGroup().location]", + "dependsOn": [ + "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]", + "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]" + ], + "properties": { + "ipConfigurations": [ + { + "name": "ipconfig1", + "properties": { + "privateIPAllocationMethod": "Dynamic", + "publicIPAddress": { + "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]" + }, + "subnet": { + "id": "[variables('subnetRef')]" + } + } + } + ] + } + }, + { + "apiVersion": "[variables('apiVersion')]", + "type": "Microsoft.Compute/virtualMachines", + "name": "[variables('vmName')]", + "location": "[resourceGroup().location]", + "dependsOn": [ + "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]", + "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]" + ], + "properties": { + "hardwareProfile": { + "vmSize": "[variables('vmSize')]" + }, + "osProfile": { + "computerName": "[variables('vmName')]", + "adminUsername": "[parameters('adminUsername')]", + "adminPassword": "[parameters('adminPassword')]" + }, + "storageProfile": { + "imageReference": { + "publisher": "[variables('imagePublisher')]", + "offer": "[variables('imageOffer')]", + "sku": "[parameters('ubuntuOSVersion')]", + "version": "latest" + }, + "osDisk": { + "name": "osdisk", + "vhd": { + "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), variables('apiVersion')).primaryEndpoints.blob, variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]" + }, + "caching": "ReadWrite", + "createOption": "FromImage" + }, + "dataDisks": [ + { + "name": "datadisk1", + "diskSizeGB": "100", + "lun": 0, + "vhd": { + "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), variables('apiVersion')).primaryEndpoints.blob, variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]" + }, + "createOption": "Empty" + } + ] + }, + "networkProfile": { + "networkInterfaces": [ + { + "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]" + } + ] + }, + "diagnosticsProfile": { + "bootDiagnostics": { + "enabled": "true", + "storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), variables('apiVersion')).primaryEndpoints.blob)]" + } + } + } + } + ], + "outputs": { + "hostname": { + "type": "string", + "value": "[concat(parameters('dnsLabelPrefix'), '.', resourceGroup().location, '.cloudapp.azure.com')]" + }, + "sshCommand": { + "type": "string", + "value": "[concat('ssh ', parameters('adminUsername'), '@', parameters('dnsLabelPrefix'), '.', resourceGroup().location, '.cloudapp.azure.com')]" + } + } +} \ No newline at end of file diff --git a/demos/Azure/readme.md b/demos/Azure/readme.md new file mode 100644 index 0000000000..b2bd4dade8 --- /dev/null +++ b/demos/Azure/readme.md @@ -0,0 +1,11 @@ +## Demo: Managing Azure using PowerShell + +This demo (Azure-Demo.ps1) shows management of Azure Compute resource using Azure Resource Management (ARM) cmdlets. + +## Prerequisites ## +- Install PowerShell 6.0.0-alpha.9 +- Install AzureRM.NetCore.Preview, AzureRM.Profile.NetCore.Preview and AzureRM.Resources.NetCore.Preview modules to a local directory. + - The instructions for downloading these modules are in Azure-Demo.ps1 file. + - You have to use the command "Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination " + + \ No newline at end of file