Skip to content

Commit

Permalink
Merged in feature/resource-group-param (pull request deuill#18)
Browse files Browse the repository at this point in the history
Feature/resource group param

Approved-by: James Stow <james.stow@antstream.com>
  • Loading branch information
Matthew Tyas committed Apr 12, 2019
2 parents 7d83f53 + c56960b commit a65fd38
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 23 deletions.
54 changes: 31 additions & 23 deletions modules/azure.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Common fuctions for Azure operations

function GetSasToken($storageAccountName, $resourceGroup, $subscriptionName)
{
{
$StartTime = Get-Date
$EndTime = $startTime.AddYears(100)

Expand All @@ -22,11 +22,11 @@ function GetSasToken($storageAccountName, $resourceGroup, $subscriptionName)
{
Write-Host "No matching sub found for $subscriptionName"
VarDump($foundSubs)
Exit(1)
Exit(1)
}

$sub = Select-AzureRmSubscription -SubscriptionName $subscriptionName -ErrorAction Stop

$keys = Get-AzureRmStorageAccountKey -AccountName $storageAccountName -ResourceGroupName $resourceGroup -ErrorAction Stop
$key = $keys[0].Value

Expand Down Expand Up @@ -60,18 +60,18 @@ function Remove-RmVMFull
.SYNOPSIS
This function is used to remove any Azure VMs as well as any attached disks. By default, this function creates a job
due to the time it takes to remove an Azure VM.
.EXAMPLE
PS> Get-AzureRmVm -Name 'AS-GS111' | Remove-RmVMFull OR Remove-RmVMFull -Name $VMName -ResourceGroupName $azureResourceGroupName -Wait
This example removes the Azure VM As-GS111 as well as any disks attached to it.
.PARAMETER VMName
The name of an Azure VM. This has an alias of Name which can be used as pipeline input from the Get-AzureRmVM cmdlet.
.PARAMETER ResourceGroupName
The name of the resource group the Azure VM is a part of.
.PARAMETER Wait
If you'd rather wait for the Azure VM to be removed before returning control to the console, use this switch parameter.
If not, it will create a job and return a PSJob back.
Expand All @@ -83,15 +83,15 @@ function Remove-RmVMFull
[ValidateNotNullOrEmpty()]
[Alias('Name')]
[string]$VMName,

[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ResourceGroupName,

[Parameter()]
[ValidateNotNullOrEmpty()]
[switch]$Wait

)
process {
try
Expand All @@ -104,7 +104,7 @@ function Remove-RmVMFull
'ResourceGroupName' = $ResourceGroupName
}
$vm = Get-AzureRmVm @commonParams

#region Get the VM ID
$azResourceParams = @{
'ResourceName' = $VMName
Expand All @@ -114,7 +114,7 @@ function Remove-RmVMFull
$vmResource = Get-AzureRmResource @azResourceParams
$vmId = $vmResource.Properties.VmId
#endregion

#region Remove the boot diagnostics disk
if ($vm.DiagnosticsProfile.bootDiagnostics)
{
Expand All @@ -132,11 +132,11 @@ function Remove-RmVMFull
'ResourceGroupName' = $diagSaRg
'Name' = $diagSa
}

Get-AzureRmStorageAccount @saParams | Get-AzureStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzureStorageContainer -Force
}
#endregion

Write-Host 'Removing the Azure VM...'
$null = $vm | Remove-AzureRmVM -Force
Write-Host 'Removing the Azure network interface...'
Expand All @@ -150,25 +150,25 @@ function Remove-RmVMFull
{
Write-Host 'Removing the Public IP Address...'
Remove-AzureRmPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
}
}
}
}
}



## Remove the OS disk
Write-Host 'Removing OS disk...'
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri
$osDiskContainerName = $osDiskUri.Split('/')[-2]
## TODO: Does not account for resouce group

## TODO: Does not account for resouce group
$osDiskStorageAcct = Get-AzureRmStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split('/')[2].Split('.')[0] }
$osDiskStorageAcct | Remove-AzureStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split('/')[-1] -ea Ignore

#region Remove the status blob
Write-Host 'Removing the OS disk status blob...'
$osDiskStorageAcct | Get-AzureStorageBlob -Container $osDiskContainerName -Blob "$($vm.Name)*.status" | Remove-AzureStorageBlob
#endregion

## Remove any other attached disks
if ($vm.DataDiskNames.Count -gt 0)
{
Expand All @@ -180,7 +180,7 @@ function Remove-RmVMFull
}
}
}

if ($Wait.IsPresent)
{
& $scriptBlock -VMName $VMName -ResourceGroupName $ResourceGroupName
Expand All @@ -193,7 +193,7 @@ function Remove-RmVMFull
'ArgumentList' = @($VMName, $ResourceGroupName)
'Name' = "Azure VM $VMName Removal"
}
Start-Job @jobParams
Start-Job @jobParams
}
}
catch
Expand Down Expand Up @@ -238,3 +238,11 @@ function DownloadAzureStorageBlob {
Write-Warning -Message ('Failed to download Azure Storage blob' -f $Blob);
}
}

function GetKeyForStorageAccount {
param ([string] $resourceGroup, [string] $accountName)

Write-Host "Getting Access Key for Storage Account: $accountName"
$accessKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroup -AccountName $accountName).Value[0];
return $accessKey
}
48 changes: 48 additions & 0 deletions modules/remoting-alt.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function CopyUsingSSHSFTP {
#will recurse switch break single files?
<#
.Synopsis
Copy files and folders from source to destination server
.Example
DownloadAzureStorageBlob $StorageAccountName $StorageAccountKey $Destination;
#>
[CmdletBinding()]
param (
$orig,
$destFolder,
$ComputerName,
$port,
$username,
$password
)

Write-Host "Copy $orig to $destFolder in VM $ComputerName using psftp."
#dos2unix $orig
Write-Host "Starting copy"

$program = "..\..\libraries\putty\psftp.exe"
$cmd = @(
"cd /$destFolder",
"put -r $orig"
"bye"
)
# Detect hostkey
$output1 = [String]($cmd | & $program -batch -pw $password "$username@$ComputerName" 2>&1 )
$output1 | Out-File "temp.txt" -Force # It is horrible but I am in a hurry
$output2 = Get-Content "temp.txt"
foreach ($line in $output2)
{
if ($line -like "ssh-rsa 2048 *")
{
$hostkey = $line -replace "ssh-rsa 2048 ",""
Write-Host "Hostkey detected."
}
}
Write-Host "Hostkey=$hostkey."

# Do the copy with Hostkey
[String]($cmd | & $program -batch -pw $password "$username@$ComputerName" -hostkey $hostkey 2>&1 )

Write-Host "Copy finished."
}
74 changes: 74 additions & 0 deletions modules/resource-locator.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<#
.SYNOPSIS
Functions that return locations of external resources that comply with our new naming convention/
This is basically a dynamic config file
Author: Matthew Tyas
#>

#Azure Resources
function GetVMImageName($environment, $branch)
{
return "GameServerBaseImage_${environment}_${branch}"
}

function GetVmName($environment)
{
return "as-gstemplate-$environment"
}

function GetASLAddress($environment, $region)
{
return "https://as-${environment}-asl-${region}.azurewebsites.net/api"
}

function GetVirtualNetwork($environment, $region)
{
return "as-${environment}-vnet-GS-${region}"
}

function GetNetworkSecurityGroupName($environment)
{
return "as-${environment}-nsg-GS"
}

function GetAzureResourceGroupName($environment, $region)
{
return "as-${environment}-rg-GS-${region}"
}

function GetEmulatorStorageResourceGroupName($environment, $region)
{
return "as-${environment}-rg-storage-${region}"
}
# Storage Accounts

function GetImageStorageAccount($environment, $region)
{
return "as${environment}rggs${region}disks01"
}

function GetDiagnosticsStorageAccountName($environment, $region)
{
return "as${environment}rggs${region}diag01"
}

# Azure storage account name for the emulators and roms
function GetEmulatorStorageAccount($environment, $region)
{
return "as${environment}storage${region}".ToLower()
}

function GetEmulatorFileShareName()
{
return "binaries"
}

function GetGameSettingsStorageAccount()
{
return "asdevblob"
}

function GetRomStorageAccount($environment, $region)
{
return "as${environment}storage${region}".ToLower()
}
Loading

0 comments on commit a65fd38

Please sign in to comment.