-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path90_Setup-Autoscaling.ps1
More file actions
110 lines (90 loc) · 3.07 KB
/
Copy path90_Setup-Autoscaling.ps1
File metadata and controls
110 lines (90 loc) · 3.07 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
# Set up MJS Cluster for Auto-Resizing
# https://www.mathworks.com/help/matlab-parallel-server/set-up-your-mjs-cluster-for-resizing.html
# Copyright 2026 The MathWorks, Inc.
function Get-AutoscalingArguments {
<#
.SYNOPSIS
Build the autoscaling command-line arguments based on environment configuration.
#>
$UsePrivateIPMapping = if ($Env:CommunicationMode -eq 'PrivateIP') { 'True' } else { 'False' }
$Arguments = "--use-private-ip-mapping $UsePrivateIPMapping"
if ($Env:DnsSearchSuffix) {
$Arguments += " --dns-search-suffix $Env:DnsSearchSuffix"
}
return $Arguments
}
function Get-PythonExecutablePath {
<#
.SYNOPSIS
Get the full path to the Python executable.
#>
$PythonPath = (Get-Command 'py' -ErrorAction Stop).Source
return $PythonPath
}
function Register-AutoscalingTask {
<#
.SYNOPSIS
Register a scheduled task to periodically run the autoscaling script.
.PARAMETER AutoscalingArgs
Command-line arguments to pass to the autoscaling script.
#>
param (
[Parameter(Mandatory = $true)]
[string]$AutoscalingArgs
)
$TaskName = 'Autoscaling Task for MATLAB Parallel Server'
$ExistingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($ExistingTask) {
Write-Output "Autoscaling task already exists"
return
}
Write-Output "Creating autoscaling task"
$PythonPath = Get-PythonExecutablePath
Write-Output "Python executable found at: $PythonPath"
$ScriptPath = "$Env:ProgramFiles\MathWorks\autoscaling\autoscaling.py"
$PeriodInMinutes = 1
$Action = New-ScheduledTaskAction `
-Execute $PythonPath `
-Argument "`"$ScriptPath`" $AutoscalingArgs"
$Trigger = New-ScheduledTaskTrigger `
-Once `
-At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes $PeriodInMinutes)
$Principal = New-ScheduledTaskPrincipal `
-UserId 'SYSTEM' `
-LogonType ServiceAccount `
-RunLevel Highest
Register-ScheduledTask `
-TaskName $TaskName `
-Action $Action `
-Trigger $Trigger `
-Principal $Principal
Write-Output "Autoscaling task '$TaskName' created successfully"
}
function Initialize-Autoscaling {
<#
.SYNOPSIS
Set up autoscaling for the MJS cluster if conditions are met.
#>
if ($Env:NodeType -ne 'headnode') {
Write-Output "Current node is not the head-node. Skipping autoscaling setup."
return
}
if ($Env:EnableAutoscaling -ne 'Yes') {
Write-Output "Autoscaling is not enabled. Skipping autoscaling setup."
return
}
if ($Env:MATLABRelease -lt 'R2022a') {
Write-Output "WARNING: Auto-Resizing is only available for R2022a and later"
return
}
$AutoscalingArgs = Get-AutoscalingArguments
Write-Output "Autoscaling arguments: $AutoscalingArgs"
Register-AutoscalingTask -AutoscalingArgs $AutoscalingArgs
}
try {
Initialize-Autoscaling
} catch {
Write-Error "Failed to set up autoscaling: $_"
throw
}