@@ -9,48 +9,99 @@ Import-Module Az
99Import-Module $PSScriptRoot / ../ common/ Configuration.psm1 - Force
1010Import-Module $PSScriptRoot / ../ common/ Logging.psm1 - Force
1111
12- # Get SRE config
13- # --------------
14- $config = Get-SreConfig ($sreId );
12+
13+ # Get config and original context before changing subscription
14+ # ------------------------------------------------------------
15+ $config = Get-SreConfig $sreId
1516$originalContext = Get-AzContext
16- $_ = Set-AzContext - SubscriptionId $config.sre.subscriptionName ;
17-
18-
19- # Find VM with private IP address matching the provided last octect
20- # # Turn provided last octect into full IP address in the data subnet
21- $vmIpAddress = ($config.sre.network.subnets.data.prefix + " ." + $ipLastOctet )
22- Add-LogMessage - Level Info " Finding VM with IP ${vmIpAddress} ..."
23- # # Get all compute VMs
24- $computeVms = Get-AzVM - ResourceGroupName $config.sre.dsvm.rg
25- # # Get the NICs attached to all the compute VMs
26- $computeVmNicIds = ($computeVms | ForEach-Object { (Get-AzVM - ResourceGroupName $config.sre.dsvm.rg - Name $_.Name ).NetworkProfile.NetworkInterfaces.Id })
27- $computeVmNics = ($computeVmNicIds | ForEach-Object { Get-AzNetworkInterface - ResourceGroupName $config.sre.dsvm.rg - Name $_.Split (" /" )[-1 ] })
28- # # Filter the NICs to the one matching the desired IP address and get the name of the VM it is attached to
29- $computeVmName = ($computeVmNics | Where-Object { $_.IpConfigurations.PrivateIpAddress -match $vmIpAddress })[0 ].VirtualMachine.Id.Split(" /" )[-1 ]
30-
31- # Run remote scripts
32- $diagnostic_scripts = @ (" check_ldap_connection.sh" , " restart_name_resolution_service.sh" , " rerun_realm_join.sh" , " restart_sssd_service.sh" )
33- $testHost = $config.shm.dc.fqdn
34- $ldapUser = $config.sre.users.ldap.dsvm.samAccountName
35- $domainLower = $config.shm.domain.fqdn
36- $servicePath = $config.shm.domain.serviceOuPath
17+ $_ = Set-AzContext - SubscriptionId $config.sre.subscriptionName
18+
19+
20+ # Find VM with private IP address matching the provided last octet
21+ # ----------------------------------------------------------------
22+ Add-LogMessage - Level Info " Finding compute VM with last IP octet: $ipLastOctet "
23+ $vmId = Get-AzNetworkInterface - ResourceGroupName $config.sre.dsvm.rg | Where-Object { ($_.IpConfigurations.PrivateIpAddress ).Split(" ." ) -eq $ipLastOctet } | ForEach-Object { $_.VirtualMachine.Id }
24+ $vm = Get-AzVM - ResourceGroupName $config.sre.dsvm.rg | Where-Object { $_.Id -eq $vmId }
25+ if ($? ) {
26+ Add-LogMessage - Level Success " Found compute VM '$ ( $vm.Name ) '"
27+ } else {
28+ Add-LogMessage - Level Fatal " Could not find VM with last IP octet '$ipLastOctet '"
29+ }
3730
31+ # Run remote diagnostic scripts
32+ # -----------------------------
33+ Add-LogMessage - Level Info " Running diagnostic scripts on VM $ ( $vm.Name ) ..."
3834$params = @ {
3935 TEST_HOST = $config.shm.dc.fqdn
4036 LDAP_USER = $config.sre.users.ldap.dsvm.samAccountName
4137 DOMAIN_LOWER = $config.shm.domain.fqdn
42- SERVICE_PATH = " '" + $config.shm.domain.serviceOuPath + " '"
38+ SERVICE_PATH = " '$ ( $config.shm.domain.serviceOuPath ) '"
39+ }
40+ foreach ($scriptNamePair in ((" LDAP connection" , " check_ldap_connection.sh" ),
41+ (" name resolution" , " restart_name_resolution_service.sh" ),
42+ (" realm join" , " rerun_realm_join.sh" ),
43+ (" SSSD service" , " restart_sssd_service.sh" ))) {
44+ $name , $diagnostic_script = $scriptNamePair
45+ $scriptPath = Join-Path $PSScriptRoot " .." " secure_research_environment" " remote" " compute_vm" " scripts" $diagnostic_script
46+ Add-LogMessage - Level Info " [ ] Configuring $name ($diagnostic_script ) on compute VM '$ ( $vm.Name ) '"
47+ $result = Invoke-RemoteScript - Shell " UnixShell" - ScriptPath $scriptPath - VMName $vm.Name - ResourceGroupName $config.sre.dsvm.rg - Parameter $params
48+ $success = $?
49+ Write-Output $result.Value
50+ if ($success ) {
51+ Add-LogMessage - Level Success " Configuring $name on $ ( $vm.Name ) was successful"
52+ } else {
53+ Add-LogMessage - Level Failure " Configuring $name on $ ( $vm.Name ) failed!"
54+ }
55+ }
56+
57+
58+ # Get LDAP secret from the KeyVault
59+ # ---------------------------------
60+ Add-LogMessage - Level Info " [ ] Loading LDAP secret from key vault '$ ( $config.sre.keyVault.name ) '"
61+ $kvLdapPassword = (Get-AzKeyVaultSecret - VaultName $config.sre.keyVault.Name - Name $config.sre.keyVault.secretNames.dsvmLdapPassword ).SecretValueText;
62+ if ($kvLdapPassword ) {
63+ Add-LogMessage - Level Success " Found LDAP secret in the key vault"
64+ } else {
65+ Add-LogMessage - Level Fatal " Could not load LDAP secret from key vault '$ ( $config.sre.keyVault.name ) '"
4366}
4467
45- Add-LogMessage - Level Info " Running diagnostic scripts on VM ${computeVmName} ..."
4668
47- foreach ($diagnostic_script in $diagnostic_scripts ) {
48- $scriptPath = Join-Path $PSScriptRoot " remote_scripts" $diagnostic_script
49- $result = Invoke-AzVMRunCommand - ResourceGroupName $config.sre.dsvm.rg - Name " $computeVmName " `
50- - CommandId ' RunShellScript' - ScriptPath $scriptPath `
51- - Parameter $params
52- Write-Output $result.Value ;
69+ # Set LDAP secret on the compute VM
70+ # ---------------------------------
71+ Add-LogMessage - Level Info " [ ] Setting LDAP secret on compute VM '$ ( $vm.Name ) '"
72+ $scriptPath = Join-Path $PSScriptRoot " .." " secure_research_environment" " remote" " compute_vm" " scripts" " reset_ldap_password.sh"
73+ $params = @ {
74+ ldapPassword = " `" $kvLdapPassword `" "
75+ }
76+ $result = Invoke-RemoteScript - Shell " UnixShell" - ScriptPath $scriptPath - VMName $vm.Name - ResourceGroupName $config.sre.dsvm.rg - Parameter $params
77+ $success = $?
78+ Write-Output $result.Value
79+ if ($success ) {
80+ Add-LogMessage - Level Success " Setting LDAP secret on compute VM $ ( $vm.Name ) was successful"
81+ } else {
82+ Add-LogMessage - Level Fatal " Setting LDAP secret on compute VM $ ( $vm.Name ) failed!"
83+ }
84+
85+
86+ # Set LDAP secret in local Active Directory on the SHM DC
87+ # -------------------------------------------------------
88+ $_ = Set-AzContext - SubscriptionId $config.shm.subscriptionName
89+ $scriptPath = Join-Path $PSScriptRoot " .." " secure_research_environment" " remote" " compute_vm" " scripts" " ResetLdapPasswordOnAD.ps1"
90+ $params = @ {
91+ samAccountName = " `" $ ( $config.sre.users.ldap.dsvm.samAccountName ) `" "
92+ ldapPassword = " `" $kvLdapPassword `" "
5393}
94+ Add-LogMessage - Level Info " [ ] Setting LDAP secret in local AD on '$ ( $config.shm.dc.vmName ) '"
95+ $result = Invoke-RemoteScript - Shell " PowerShell" - ScriptPath $scriptPath - VMName $config.shm.dc.vmName - ResourceGroupName $config.shm.dc.rg - Parameter $params
96+ $success = $?
97+ Write-Output $result.Value
98+ if ($success ) {
99+ Add-LogMessage - Level Success " Setting LDAP secret on SHM DC was successful"
100+ } else {
101+ Add-LogMessage - Level Fatal " Setting LDAP secret on SHM DC failed!"
102+ }
103+
54104
55- # Switch back to previous subscription
56- $_ = Set-AzContext - Context $originalContext ;
105+ # Switch back to original subscription
106+ # ------------------------------------
107+ $_ = Set-AzContext - Context $originalContext
0 commit comments