Skip to content

Commit

Permalink
AzureRM.Compute.Experements
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Sep 1, 2017
1 parent 57f9e73 commit 040b552
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ FakesAssemblies/
/tools/*.dll
*.GhostDoc.xml
pingme.txt
groupMapping*.json
groupMapping*.json
.vscode/
93 changes: 49 additions & 44 deletions experiments/Compute.Experiments/Az.Compute.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@

function New-AzVm {
param (
[Parameter(Mandatory = $true)] [string] $Name,
[Parameter(Mandatory = $true)] [string] $ResourceGroup,
[Parameter(Mandatory = $true)] [string] $Name,
[Parameter(Mandatory = $true)] [string] $ResourceGroup,

# Generate a random as a hash of the name so it will be idempotent (tack on resource group?).
[Parameter(DontShow)]
$Random = $(Get-Hash $Name),
$Random = $(Get-Hash $Name),

[string] $Location = "",
[string] $Location = "",

[string] $Image = "WinServer2016",
[string] $Size = "Standard_DS1_v2",
[string] $Image = "WinServer2016",
[string] $Size = "Standard_DS1_v2",

[string] $VnetName = "$($Name)Vnet",
[string] $SubnetAddressPrefix = "192.168.1.0/24",
[string] $VnetAddressPrefix = "192.168.0.0/16",
[string] $VnetName = "$($Name)Vnet",
[string] $SubnetAddressPrefix = "192.168.1.0/24",
[string] $VnetAddressPrefix = "192.168.0.0/16",

[string] $PublicIpName = "$($Name)PublicIp",
[string] $PublicIpDnsLabel = "$Name-$Random".ToLower(),
[string] $PublicIpAllocationMethod = "Static",
[int] $PublicIpIdleTimeoutInMinutes = 4,
[string] $PublicIpName = "$($Name)PublicIp",
[string] $PublicIpDnsLabel = "$Name-$Random".ToLower(),
[string] $PublicIpAllocationMethod = "Static",
[int] $PublicIpIdleTimeoutInMinutes = 4,

[string] $NsgName = "$($Name)Nsg",
[int[]] $NsgOpenPorts = $null,
[string] $NsgName = "$($Name)Nsg",
[int[]] $NsgOpenPorts = $null,

[string] $NicName = "$($Name)Nic"

# Storage - OS Disk Size.
# Compute: "this goes above and beyond the 80% scenario".
)

try {

# Build credentials.
Expand Down Expand Up @@ -98,11 +98,16 @@ function New-AzVm {
$pip = Use-Pip -Name $PublicIpName -ResourceGroup $ResourceGroup -Location $Location -DnsLabel $PublicIpDnsLabel -AllocationMethod $PublicIpAllocationMethod -IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;

Write-Info "Ensuring NSG...";
$nsg = Use-Nsg -Name $PublicIpName -ResourceGroup $ResourceGroup -Location $Location -OpenPorts $NsgOpenPorts;
$Global:nsg = $nsg;
$nsg = Use-Nsg -Name $NsgName -ResourceGroup $ResourceGroup -Location $Location -OpenPorts $NsgOpenPorts;

Write-Info "Ensuring NIC...";
$nic = Use-Nic -Name $NicName -ResourceGroup $ResourceGroup -Location $Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id;
$nic = Use-Nic `
-Name $NicName `
-ResourceGroup $ResourceGroup `
-Location $Location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $pip.Id `
-NetworkSecurityGroupId $nsg.Id;

# TODO: Add disk options (https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-sample-create-vm-from-managed-os-disks?toc=%2fpowershell%2fmodule%2ftoc.json)?
# https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmosdisk?view=azurermps-4.2.0
Expand Down Expand Up @@ -139,12 +144,12 @@ Export-ModuleMember -Function New-AzVm

function Use-ResourceGroup {
param (
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location
)

$rg = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $ResourceGroup } | Select-Object -First 1 -Wait;

if($rg -eq $null) {
return New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location;
} else {
Expand All @@ -154,13 +159,13 @@ function Use-ResourceGroup {

function Use-Vnet {
param (
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $SubnetAddressPrefix,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $SubnetAddressPrefix,
[Parameter(Mandatory=$true)] [string] $VnetAddressPrefix
)

$vnet = Get-AzureRmVirtualNetwork | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;

if($vnet -eq $null) {
Expand All @@ -176,14 +181,14 @@ function Use-Vnet {

function Use-Pip {
param (
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $DnsLabel,
[Parameter(Mandatory=$true)] [string] $AllocationMethod,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $DnsLabel,
[Parameter(Mandatory=$true)] [string] $AllocationMethod,
[Parameter(Mandatory=$true)] [int] $IdleTimeoutInMinutes
)

$pip = Get-AzureRmPublicIpAddress | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;

if($pip -eq $null) {
Expand All @@ -196,12 +201,12 @@ function Use-Pip {

function Use-Nsg {
param (
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [int[]] $OpenPorts
)

$nsg = Get-AzureRmNetworkSecurityGroup | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;

if($nsg -eq $null) {
Expand All @@ -212,7 +217,7 @@ function Use-Nsg {
{
$nsgRule = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority $priority -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $port -Access Allow;
$rules.Add($nsgRule);

$priority--;
}

Expand All @@ -225,14 +230,14 @@ function Use-Nsg {

function Use-Nic {
param (
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $SubnetId,
[Parameter(Mandatory=$true)] [string] $PublicIpAddressId,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $ResourceGroup,
[Parameter(Mandatory=$true)] [string] $Location,
[Parameter(Mandatory=$true)] [string] $SubnetId,
[Parameter(Mandatory=$true)] [string] $PublicIpAddressId,
[Parameter(Mandatory=$true)] [psobject] $NetworkSecurityGroupId
)

$nic = Get-AzureRmNetworkInterface | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;

if($nic -eq $null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Resources\AzureRM.Resources.psd1
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Network\AzureRM.Network.psd1
Import-Module .\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Compute\AzureRM.Compute.psd1
Import-Module .\..\..\experiments\Compute.Experiments\AzureRM.Compute.Experiments.psd1

# Login
$credentials = Get-Content -Path "C:\Users\sergey\Desktop\php-test.json" | ConvertFrom-Json
$clientSecret = ConvertTo-SecureString $credentials.clientSecret -AsPlainText -Force
$pscredentials = New-Object System.Management.Automation.PSCredential($credentials.applicationId, $clientSecret)
Login-AzureRmAccount -ServicePrincipal -Credential $pscredentials -TenantId $credentials.tenantId | Out-Null

New-AzVm

# clean-up
Remove-AzureRmResourceGroup -Name $resourceGroupName
Binary file not shown.
132 changes: 132 additions & 0 deletions experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
function New-AzVm {
# Images
Write-Host "Load images..."
$jsonImages = Get-Content -Path "images.json" | ConvertFrom-Json
Write-Host "done"

# an array of @{ Type = ...; Name = ...; Image = ... }
$images = $jsonImages.outputs.aliases.value.psobject.Properties | ForEach-Object {
# e.g. "Linux"
$type = $_.Name
$_.Value.psobject.Properties | ForEach-Object {
New-Object -TypeName psobject -Property @{
# e.g. "Linux"
Type = $type;
# e.g. "CentOs"
Name = $_.Name;
# e.g. @{ publisher = "OpenLogic"; offer = "CentOS"; sku = "7.3"; version = "latest" }
Image = $_.Value
}
}
}

# Find VM Image
$vmImageName = "Win2012R2Datacenter"
$vmImage = $images | Where-Object { $_.Name -eq $vmImageName } | Select-Object -First 1

Write-Host $vmImage

# Location
Write-Host "Load locations..."
$location = (Get-AzureRmLocation | Select-Object -First 1 -Wait).Location
$location = "eastus"
Write-Host "done"

# Resource Group
$resourceGroupName = "resourceGroupTest"
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location

# Virtual Network
$virtualNetworkName = "virtualNetworkTest"
$virtualNetworkAddressPrefix = "192.168.0.0/16"
$subnet = @{ Name = "subnetTest"; AddressPrefix = "192.168.1.0/24" }
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $subnet.Name -AddressPrefix $subnet.AddressPrefix
$virtualNetwork = New-AzureRmVirtualNetwork `
-ResourceGroupName $resourceGroupName `
-Location $location `
-Name $virtualNetworkName `
-AddressPrefix $virtualNetworkAddressPrefix `
-Subnet $subnetConfig

# Piblic IP
$publicIpAddressName = "publicIpAddressTest"
$publicIpAddress = New-AzureRmPublicIpAddress `
-ResourceGroupName $resourceGroupName `
-Location $location `
-AllocationMethod Static `
-Name $publicIpAddressName

# Security Group (it may have several rules(ports))
$securityGroupName = "securityGroupTest"
$securityRule = @{
Name = "securityRuleTest";
Protocol = "Tcp";
Priority = 1000;
Access = "Allow";
Direction = "Inbound";
SourcePortRange = "*";
SourceAddressPrefix = "*";
DestinationPortRange = 3389;
DestinationAddressPrefix = "*";
}
$securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
-Name $securityRule.Name `
-Protocol $securityRule.Protocol `
-Priority $securityRule.Priority `
-Access $securityRule.Access `
-Direction $securityRule.Direction `
-SourcePortRange $securityRule.SourcePortRange `
-SourceAddressPrefix $securityRule.SourceAddressPrefix `
-DestinationPortRange $securityRule.DestinationPortRange `
-DestinationAddressPrefix $securityRule.DestinationAddressPrefix
$securityGroup = New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $resourceGroupName `
-Location $location `
-Name $securityGroupName `
-SecurityRules $securityRuleConfig

# Network Interface
$networkInterfaceName = "networkInterfaceTest"
$networkInterface = New-AzureRmNetworkInterface `
-ResourceGroupName $resourceGroupName `
-Location $location `
-Name $networkInterfaceName `
-PublicIpAddressId $publicIpAddress.Id `
-SubnetId $virtualNetwork.Subnets[0].Id `
-NetworkSecurityGroupId $securityGroup.Id

# VM
# $vmCredentials = Get-Credential
$vm = @{ Name = "vmTest"; Size = "Standard_DS2" }
$vmConfig = New-AzureRmVMConfig -VMName $vm.Name -VMSize $vm.Size
$vmComputer = $vm.Name
$vmComputerPassword = "E5v7e9!@%f";
$vmComputerUser = "special";
switch ($vmImage.Type) {
"Windows" {
$password = ConvertTo-SecureString $vmComputerPassword -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($vmComputerUser, $password);
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
-Windows `
-ComputerName $vmComputer `
-Credential $cred
}
"Linux" {

}
}

$vmImageImage = $vmImage.Image
Write-Host $vmImageImage
$vmConfig = $vmConfig `
| Set-AzureRmVMSourceImage `
-PublisherName $vmImageImage.publisher `
-Offer $vmImageImage.offer `
-Skus $vmImageImage.sku `
-Version $vmImageImage.version `
| Add-AzureRmVMNetworkInterface -Id $networkInterface.Id

New-AzureRmVm -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig
}

Export-ModuleMember -Function New-AzVm
Loading

0 comments on commit 040b552

Please sign in to comment.