<commit_before>﻿<#
.SYNOPSIS
    Cycle-Nodes
.DESCRIPTION
    Cycles Nodes in the dotnet-ci azure pool
.PARAMETER Password
    Password to create the dotnet-bot account with
.PARAMETER Pattern
    Pattern of machine names to cycle
.PARAMETER NewImage
    New image to place on those machines
.PARAMETER AllowMissingMachines
    If machines are not currently in azure, allows creation
.PARAMETER Verbose
    Verbose tracing of the script
#>

param (
    [string]$Password = $(Read-Host -assecurestring -prompt "Password for user"),
    [string]$Pattern = $(Read-Host -prompt "Pattern for replacement"),
    [string]$NewImage = $null,
    [switch]$AllowMissingMachines = $false,
    [switch]$Verbose = $false
)

$ServiceName="dotnet-ci-nodes"
$Username="dotnet-bot"

# Read the inventory

$machines = Get-Content inventory.txt | ConvertFrom-Csv

# Walk the machines, append the current count and check against the regex provided.  If it matches,
# then cycle the machine to the new image.

foreach ($machine in $machines)
{
    for ($i = 1; $i -le $machine.Count; $i++)
    {
        $fullMachineName = $machine.BaseName + $i

        if ($Verbose)
        {
            Write-Host -NoNewLine "Checking machine $fullMachineName against pattern $Pattern..."
        }
        
        if ($fullMachineName -match $Pattern)
        {
            if ($Verbose)
            {
                Write-Host "Matched"
            }
            
            $imageToUse = $NewImage

            if ($imageToUse -eq $null)
            {
                $imageToUse = $NewImage
            }

            [int]$port = [int]$machine.RdpSshStart;
            $port += $i
            
            Write-Host "Cycling $fullMachineName to $NewImage with RDP/SSH on $port"

            # Check for existence of the old machine

            $existingVM = Get-AzureVM -ServiceName $ServiceName -Name $fullMachineName

            if (-not $existingVM)
            {
                if ($AllowMissingMachines)
                {
                    Write-Warning "VM $fullMachineName did not exist...creating"
                }
                else
                {
                    throw "VM $fullMachineName did not exist."
                }
            }
            else
            {
                # Delete the old machine
            
                Remove-AzureVM -ServiceName $ServiceName -Name $fullMachineName -DeleteVHD
            }
                    
            switch ($machine.OSType)
            {<commit_msg>Add a check for a missing image, and add some logging<commit_after>﻿<#
.SYNOPSIS
    Cycle-Nodes
.DESCRIPTION
    Cycles Nodes in the dotnet-ci azure pool
.PARAMETER Password
    Password to create the dotnet-bot account with
.PARAMETER Pattern
    Pattern of machine names to cycle
.PARAMETER NewImage
    New image to place on those machines
.PARAMETER AllowMissingMachines
    If machines are not currently in azure, allows creation
.PARAMETER Verbose
    Verbose tracing of the script
#>

param (
    [string]$Password = $(Read-Host -assecurestring -prompt "Password for user"),
    [string]$Pattern = $(Read-Host -prompt "Pattern for replacement"),
    [string]$NewImage = $null,
    [switch]$AllowMissingMachines = $false,
    [switch]$Verbose = $false
)

$ServiceName="dotnet-ci-nodes"
$Username="dotnet-bot"

Write-Host "Reading inventory from inventory.txt"

# Read the inventory

$machines = Get-Content inventory.txt | ConvertFrom-Csv

# Walk the machines, append the current count and check against the regex provided.  If it matches,
# then cycle the machine to the new image.

foreach ($machine in $machines)
{
    for ($i = 1; $i -le $machine.Count; $i++)
    {
        $fullMachineName = $machine.BaseName + $i

        if ($Verbose)
        {
            Write-Host -NoNewLine "Checking machine $fullMachineName against pattern $Pattern..."
        }
        
        if ($fullMachineName -match $Pattern)
        {
            if ($Verbose)
            {
                Write-Host "Matched"
            }
            
            $imageToUse = $NewImage

            if ($imageToUse -eq $null)
            {
                $imageToUse = $NewImage
            }

            # Check to see whether the new vm image actually exists

            $newImageCheck = Get-AzureVMImage $imageToUse

            if (!$newImageCheck)
            {
                throw "VM $imageToUse did not exist, aborting"
            }

            [int]$port = [int]$machine.RdpSshStart;
            $port += $i
            
            Write-Host "Cycling $fullMachineName to $NewImage with RDP/SSH on $port"

            # Check for existence of the old machine

            $existingVM = Get-AzureVM -ServiceName $ServiceName -Name $fullMachineName

            if (-not $existingVM)
            {
                if ($AllowMissingMachines)
                {
                    Write-Warning "VM $fullMachineName did not exist...creating"
                }
                else
                {
                    throw "VM $fullMachineName did not exist."
                }
            }
            else
            {
                # Delete the old machine
            
                Remove-AzureVM -ServiceName $ServiceName -Name $fullMachineName -DeleteVHD
            }
                    
            switch ($machine.OSType)
            {
{'fraction_non_alphanumeric': 0.08535221766679091, 'fraction_numerical': 0.0003727171077152441, 'mean_word_length': 1.6929252383341695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3292784', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1420, 'n_words': 447}