This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
destroy.yml
168 lines (168 loc) · 5.32 KB
/
destroy.yml
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
- hosts: localhost
gather_facts: false
connection: local
vars_files:
- vars/defaults.yml
- vars/vars.yml
module_defaults:
group/aws:
profile: '{{ aws_profile }}'
region: '{{ aws_region }}'
tasks:
- cloudformation_facts:
stack_name: static-ecs-cluster
- set_fact:
stack_exists: "{{
ansible_facts.cloudformation['static-ecs-cluster'] |
default({'stack_outputs': {}}) |
json_query('stack_outputs.Cluster') |
ternary(true, false)
}}"
- ecs_service_info:
details: true
cluster: "{{ ansible_facts.cloudformation['static-ecs-cluster'].stack_outputs.Cluster }}"
register: service_info
when: stack_exists
- set_fact:
desired_count: "{{ service_info | json_query('services[*].desiredCount') | list | max }}"
when: service_info is defined and service_info.services is defined
- set_fact:
scale_down: "{{ (desired_count is undefined or desired_count == 0) | ternary(false, true) }}"
# We can get into a chicken-egg state where the task definitions don't exist but the ECS service does
# To be able to scale down and then delete it, we'll need to ensure the task definition exists first
- ecs_taskdefinition_info:
task_definition: ssh-ansible-targets
register: ssh_targets_taskdef_exists
- ecs_taskdefinition_info:
task_definition: ios-ansible-targets
register: ios_targets_taskdef_exists
- ecs_taskdefinition:
network_mode: bridge
family: '{{ item.family }}'
state: present
containers:
- name: '{{ item.name }}'
image: '{{ item.image }}'
memoryReservation: 40
portMappings:
- containerPort: 22
hostPort: 0
protocol: tcp
when:
- scale_down
- 'not "task_definition" in item.info'
loop:
- name: ssh-target
image: '{{ repo_url }}:cent-ssh'
family: ssh-ansible-targets
info: '{{ ssh_targets_taskdef_exists }}'
- name: ios-target
image: '{{ repo_url }}:cisco-ios-sim'
family: ios-ansible-targets
info: '{{ ios_targets_taskdef_exists }}'
- ecs_service:
state: present
cluster: "{{ cloudformation['static-ecs-cluster'].stack_outputs.Cluster }}"
name: ssh-targets-{{ item + 1 }}
deployment_configuration:
maximum_percent: 150
minimum_healthy_percent: 10
desired_count: 0
task_definition: ssh-ansible-targets
placement_strategy:
- field: instanceId
type: spread
loop: '{{ range(10) | list }}'
when:
- scale_down
- stack_exists
- ecs_service:
state: present
cluster: "{{ cloudformation['static-ecs-cluster'].stack_outputs.Cluster }}"
name: cisco-ios-targets-{{ item + 1}}
deployment_configuration:
maximum_percent: 150
minimum_healthy_percent: 10
desired_count: 0
task_definition: ios-ansible-targets
placement_strategy:
- field: instanceId
type: spread
loop: '{{ range(10) | list }}'
when:
- scale_down
- stack_exists
- name: Scale instances down to zero
cloudformation:
template: ./cfn/ecs-static-size-cluster.yaml
stack_name: static-ecs-cluster
template_parameters:
ParentVPCStack: scale-test-vpc
SubnetsReach: Public
InstanceType: c5.4xlarge
SpotBidPrice: 0.20
MaxSize: 8
DesiredCapacity: 0
MinSize: 0
KeyName: '{{ key_name | default("") }}'
when: stack_exists
register: result
failed_when:
- 'not result is success'
- 'not "DELETE_FAILED state" in result.msg'
- 'not "ROLLBACK_COMPLETE state" in result.msg'
- ecs_service:
state: absent
cluster: "{{ cloudformation['static-ecs-cluster'].stack_outputs.Cluster }}"
name: ssh-targets-{{ item + 1 }}
deployment_configuration:
maximum_percent: 150
minimum_healthy_percent: 10
desired_count: 0
task_definition: ssh-ansible-targets
placement_strategy:
- field: instanceId
type: spread
loop: '{{ range(10) | list }}'
when: stack_exists
- ecs_service:
state: absent
cluster: "{{ cloudformation['static-ecs-cluster'].stack_outputs.Cluster }}"
name: cisco-ios-targets-{{ item + 1}}
deployment_configuration:
maximum_percent: 150
minimum_healthy_percent: 10
desired_count: 0
task_definition: ios-ansible-targets
placement_strategy:
- field: instanceId
type: spread
loop: '{{ range(10) | list }}'
when: stack_exists
- ecs_taskdefinition_facts:
task_definition: ssh-ansible-targets
register: td
- ecs_taskdefinition:
arn: '{{ td.task_definition_arn }}'
family: ssh-ansible-targets
state: absent
when: td.task_definition_arn is defined
- ecs_taskdefinition_facts:
task_definition: ios-ansible-targets
register: td
- ecs_taskdefinition:
arn: '{{ td.task_definition_arn }}'
family: ios-ansible-targets
state: absent
when: td.task_definition_arn is defined
- cloudformation:
template: ./cfn/ecs-static-size-cluster.yaml
state: absent
stack_name: static-ecs-cluster
- cloudformation:
template: ./cfn/vpc-2azs.yaml
state: absent
stack_name: scale-test-vpc
template_parameters:
ClassB: 99