[Ronin Windows] Set computer name and domain name for AWS instances
Categories
(Infrastructure & Operations :: RelOps: Puppet, task)
Tracking
(Not tracked)
People
(Reporter: markco, Assigned: markco)
References
Details
https://github.com/mozilla-releng/OpenCloudConfig/blob/master/userdata/OCC-Bootstrap.psm1
function Set-ComputerName {
param (
[string] $instanceId = ((New-Object Net.WebClient).DownloadString('http://169.254.169.254/latest/meta-data/instance-id')),
[string] $dnsHostname = [System.Net.Dns]::GetHostName(),
[string[]] $rebootReasons = @()
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
Write-Log -message ('{0} :: instanceId: {1}, dnsHostname: {2}.' -f $($MyInvocation.MyCommand.Name), $instanceId, $dnsHostname) -severity 'INFO'
if ((bool) -and (-not ($dnsHostname -ieq $instanceId))) {
[Environment]::SetEnvironmentVariable("COMPUTERNAME", "$instanceId", "Machine")
$env:COMPUTERNAME = $instanceId
(Get-WmiObject Win32_ComputerSystem).Rename($instanceId)
$rebootReasons += 'host renamed'
Write-Log -message ('{0} :: host renamed from: {1} to {2}.' -f $($MyInvocation.MyCommand.Name), $dnsHostname, $instanceId) -severity 'INFO'
} else {
Write-Log -message ('{0} :: hostname: {1} matches instance id: {2}.' -f $($MyInvocation.MyCommand.Name), $dnsHostname, $instanceId) -severity 'DEBUG'
}
return $rebootReasons
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
function Set-DomainName {
param (
[string] $publicKeys = (New-Object Net.WebClient).DownloadString('http://169.254.169.254/latest/meta-data/public-keys'),
[string] $workerType = $(if ($publicKeys.StartsWith('0=mozilla-taskcluster-worker-')) { $publicKeys.Replace('0=mozilla-taskcluster-worker-', '') } else { (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/user-data' -UseBasicParsing | ConvertFrom-Json).workerType }),
[string] $az = (New-Object Net.WebClient).DownloadString('http://169.254.169.254/latest/meta-data/placement/availability-zone')
)
begin {
Write-Log -message ('{0} :: begin - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
process {
switch -wildcard ($az) {
'eu-central-1*'{
$dnsRegion = 'euc1'
}
'us-east-1*'{
$dnsRegion = 'use1'
}
'us-east-2*'{
$dnsRegion = 'use2'
}
'us-west-1*'{
$dnsRegion = 'usw1'
}
'us-west-2*'{
$dnsRegion = 'usw2'
}
}
Write-Log -message ('{0} :: availabilityZone: {1}, dnsRegion: {2}.' -f $($MyInvocation.MyCommand.Name), $az, $dnsRegion) -severity 'INFO'
if (Test-Path -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Domain') {
$currentDomain = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'NV Domain').'NV Domain'
} elseif (Test-Path -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Domain') {
$currentDomain = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'Domain').'Domain'
} else {
$currentDomain = $env:USERDOMAIN
}
$domain = ('{0}.{1}.mozilla.com' -f $workerType, $dnsRegion)
if (-not ($currentDomain -ieq $domain)) {
[Environment]::SetEnvironmentVariable('USERDOMAIN', "$domain", 'Machine')
$env:USERDOMAIN = $domain
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'Domain' -Value "$domain"
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'NV Domain' -Value "$domain"
Write-Log -message ('{0} :: domain set to: {1}' -f $($MyInvocation.MyCommand.Name), $domain) -severity 'INFO'
} else {
Write-Log -message ('{0} :: current domain: {1} matches expected domain: {2}.' -f $($MyInvocation.MyCommand.Name), $currentDomain, $domain) -severity 'DEBUG'
}
}
end {
Write-Log -message ('{0} :: end - {1:o}' -f $($MyInvocation.MyCommand.Name), (Get-Date).ToUniversalTime()) -severity 'DEBUG'
}
}
| Assignee | ||
Comment 1•6 years ago
|
||
landed with https://github.com/mozilla-platform-ops/ronin_puppet/pull/70.
Profile: roles_profiles::profiles::ec2_instance_configuration
Facts created in modules/win_shared/facts.d/facts_win_aws_metadata.ps1:
custom_win_instance_id
custom_win_availability_zone
custom_win_workertype
Facts are used to construct name and domain in modules/win_aws/manifests/ec2_instance_name.pp.
Description
•