-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutDown-Dev.ps1
67 lines (50 loc) · 2.44 KB
/
ShutDown-Dev.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
param(
[string]$Configuration
)
Write-Output "`nINFO: Starting script, looking up GCP Project..."
function Get-GcloudConfigs() {
$outputCmd = "gcloud config configurations list --format='csv(name,is_active,PROJECT)'"
$output = $(Invoke-Expression $outputCmd)
$configs = ConvertFrom-Csv -InputObject $output | where project -ne $null
return $configs
}
if (!([string]::IsNullOrEmpty(${Configuration}))) {
Write-Output "Looking up GCP Configurations..."
$configs = Get-GcloudConfigs
$oldConfig = $configs | where is_active -eq $true
if ($configs.name -notcontains $Configuration) {
$Raise_Error = "ERROR: Configuration $Configuration not found."; Throw $Raise_Error
}
elseif ($oldConfig.name -ne $Configuration) {
$TargetConfig = $configs | where name -eq $Configuration
Write-Output "Activating configuration $($TargetConfig.Name) to manage $($TargetConfig.project)."
gcloud config configurations activate $TargetConfig.Name
}
}
$projectId = Get-GcloudConfigs | where is_active -eq $true | select -ExpandProperty Project
$projectName = gcloud projects describe $projectId --format="value(name)"
if ($projectName -like 'shd*' -or $projectName -like 'prd*') {
$Raise_Error = "ERROR: This looks like a PROD project, please double check: ${projectName} / ${projectId}"; Throw $Raise_Error
}
Write-Output "INFO: Current GCP project: ${projectName} / ${projectId}`n"
Read-Host "Continue?"
Write-Output "Listing MIGs..."
$outputCmd = "gcloud compute instance-groups managed list --format='csv(name,LOCATION,size,autoscaled)'";
$output = $(Invoke-Expression $outputCmd)
$migs = ConvertFrom-Csv -InputObject $output
Write-Output $migs
foreach($mig in $migs | where size -gt 0 ) {
Write-Output "Processing $($mig.name)..."
if ($mig.autoscaled -eq 'yes') {
gcloud compute instance-groups managed stop-autoscaling $($mig.name) --region=$($mig.location)
}
gcloud compute instance-groups managed resize $($mig.name) --region=$($mig.location) --size=0
}
Write-Output "Looking up SQL Instances..."
$outputCmd = "gcloud sql instances list '--format=csv(name,database_version,LOCATION,TIER,PRIVATE_ADDRESS,STATUS)' --filter='STATUS=RUNNABLE' ";
$output = $(Invoke-Expression $outputCmd)
$instances = ConvertFrom-Csv -InputObject $output
foreach ($i in $instances) {
Write-Output "Processing $($i.name)..."
gcloud sql instances patch $($i.name) --activation-policy=NEVER
}