-
Notifications
You must be signed in to change notification settings - Fork 5
/
remoteaccess.ps1
97 lines (83 loc) · 3.23 KB
/
remoteaccess.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#Requires -Version 7.0
function Connect-TmuxSession (
# Use Terraform workspace variable as default session name
[string]$Workspace=($env:TF_WORKSPACE ? $env:TF_WORKSPACE : "default")
) {
if ($env:TMUX) {
Write-Warning "Session already exixts: $env:TMUX"
return
}
if (!(Get-Command tmux -ErrorAction SilentlyContinue)) {
Write-Warning "tmux not found"
return
}
if (!$Workspace -and (Get-Command terraform)) {
# Getworkspace name from current Terraform workspace, if installed
$Workspace = $(terraform workspace show 2>$null)
}
# Set locale as it may be missing and is required for tmux
$defaultLocale = "en_US.UTF-8"
$env:LANG ??= $defaultLocale
$lcLocale = (locale -a | Where-Object {$_ -eq $env:LANG})
$env:LC_ALL ??= $lcLocale
$prexistingSession = $(tmux ls -F "#S" 2>$null) -match "^${Workspace}$"
if (!$prexistingSession) {
# Start session, but do not yet attach
tmux new -d -s $Workspace
# Initialize session
Write-Verbose "`$env:TF_WORKSPACE='$Workspace'"
tmux send-keys -t $Workspace "Init-TmuxSession -Workspace $Workspace -Path '${env:PATH}'" Enter
}
# Attach to session
tmux attach-session -d -t $Workspace
}
Set-Alias ct Connect-TmuxSession
function End-TmuxSession (
[string]$Workspace
) {
if ($Workspace) {
tmux kill-session -t $Workspace
} else {
pkill -f tmux
}
}
Set-Alias et End-TmuxSession
function Init-TmuxSession (
[string]$Workspace,
[string]$Path
)
{
$script:environmentVariableNames = @()
# Inherit PATH
$env:PATH = $Path
$script:environmentVariableNames += "PATH"
$regexCallback = {
$terraformEnvironmentVariableName = "ARM_$($args[0])".ToUpper()
$script:environmentVariableNames += $terraformEnvironmentVariableName
"`n`$env:${terraformEnvironmentVariableName}"
}
# Set Terraform workspace to the name of the session
$env:TF_WORKSPACE = $Workspace
$script:environmentVariableNames += "TF_WORKSPACE"
$terraformDirectory = Find-TerraformDirectory
if ($terraformDirectory) {
$terraformWorkspaceVars = (Join-Path $terraformDirectory "${Workspace}.tfvars")
if (Test-Path $terraformWorkspaceVars) {
# Match relevant lines first
$terraformVarsFileContent = (Get-Content $terraformWorkspaceVars | Select-String "(?m)^[^#\w]*(client_id|client_secret|subscription_id|tenant_id)")
if ($terraformVarsFileContent) {
$envScript = [regex]::replace($terraformVarsFileContent,"(client_id|client_secret|subscription_id|tenant_id)",$regexCallback,[System.Text.RegularExpressions.RegexOptions]::Multiline)
if ($envScript) {
Write-Verbose $envScript
Invoke-Expression $envScript
} else {
Write-Warning "[regex]::replace removed all content from script"
}
} else {
Write-Verbose "No matches"
}
}
}
Get-ChildItem -Path Env: -Recurse -Include $script:environmentVariableNames | Sort-Object -Property Name
Write-Debug "Environment variables: $script:environmentVariableNames"
}