-
Notifications
You must be signed in to change notification settings - Fork 60
/
cleanup-aws.ps1
executable file
·62 lines (47 loc) · 2.06 KB
/
cleanup-aws.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
#!/usr/local/bin/pwsh
. Tests/powershell-helpers.ps1
Test-EnvVar AWS_ACCESS_KEY_ID
Test-EnvVar AWS_SECRET_ACCESS_KEY
Test-EnvVar AWS_SUBNET_ID
Test-EnvVar AWS_SECURITY_GROUP_ID
if (-not (Test-AppExists "vagrant")) {
Write-Output "Please install vagrant from vagrantup.com."
exit 1
}
Write-Output "Vagrant installed - good."
if (-not (Test-AppExists "aws")) {
Write-Output "Please install aws-cli. See https://docs.aws.amazon.com/cli/latest/userguide/installing.html."
exit 1
}
Test-PluginInstalled "vagrant-aws"
Test-PluginInstalled "vagrant-aws-winrm"
Test-PluginInstalled "vagrant-dsc"
Test-PluginInstalled "vagrant-winrm-syncedfolders"
#get key name from file
$files = @(Get-ChildItem -filter "vagrant_*.pem")
if ($files.length -eq 0) {
Write-Output "No key pair (vagrant_GUID.pem) found - unable to cleanup."
exit 1
}
$keyName = $files[0].BaseName
$env:KEY_NAME = $keyName
Write-Output "Using key pair $keyName.pem"
# this is a global action, so it doesn't get saved outside of the docker container when running
Write-Output "Adding vagrant box"
vagrant box add OctopusDeploy/dsc-test-server-windows-server-2019 https://s3-ap-southeast-2.amazonaws.com/octopus-vagrant-boxes/vagrant/json/OctopusDeploy/amazon-ebs/dsc-test-server-windows-server-2019.json --force
Write-Output "Ensuring vagrant box is latest"
vagrant box update --box OctopusDeploy/dsc-test-server-windows-server-2019 --provider aws
#todo: check vagrant status and exit cleanly if not running
Write-Output "Running 'vagrant destroy -f'"
& vagrant destroy -f
$VagrantDestroyExitCode=$LASTEXITCODE
Write-Output "'vagrant destroy' exited with exit code $VagrantDestroyExitCode"
Write-Output "Removing local key-pair"
remove-item $files[0].Name -force
Write-Output "Deleting aws key-pair"
& aws ec2 delete-key-pair --key-name $keyName --region ap-southeast-2
if ($VagrantDestroyExitCode -ne 0) {
Write-Output "Vagrant destroy failed with exit code $VagrantDestroyExitCode"
Write-Output "##teamcity[buildStatus text='{build.status.text}. Vagrant cleanup failed. Action required.']"
exit $VagrantDestroyExitCode
}