-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanup-postgresql
executable file
·220 lines (194 loc) · 9.66 KB
/
cleanup-postgresql
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env ansible-playbook
# (c) 2016 DataNexus Inc. All Rights Reserved.
#
# remove postgresql instances and associated infrastructure
---
- hosts: localhost
connection: local
vars_files:
- "{{ configuration }}"
gather_facts: true
tasks:
# the math is overkill, but it's useful if we want to get more complicated
- name: POSTGRESQL CLEANUP | setting count to {{ count }}
set_fact:
count: "{{ instances | default(1) | int * 1 }}"
- block:
- name: POSTGRESQL CLEANUP | gathering {{ region }} running instances
ec2_instance_facts:
region: "{{ region }}"
filters:
instance-state-name: running
"tag:Tenant": "{{ tenant }}"
"tag:Project": "{{ project }}"
"tag:Domain": "{{ domain }}"
"tag:Application": "{{ application }}"
"tag:Cluster": "{{ cluster | default ('a') }}"
"tag:Dataflow": "{{ dataflow | default ('none') }}"
register: instance_facts
- name: POSTGRESQL CLEANUP | gathering {{ region }} available interfaces
ec2_eni_facts:
region: "{{ region }}"
filters:
status: in-use
group-name: "{{ project }}_{{ application }}"
description: 'public internal'
register: eni_facts
- name: POSTGRESQL CLEANUP | releasing EIP in {{ region }}
ec2_eip:
region: "{{ region }}"
in_vpc: yes
state: absent
release_on_disassociation: yes
device_id: "{{ item }}"
with_items: "{{ eni_facts.interfaces | selectattr('status', 'equalto', 'in-use') | map(attribute='id') | list }}"
when:
- eni_facts.interfaces | length > 0
- name: POSTGRESQL CLEANUP | terminating instances in {{ region }}
ec2:
state: absent
region: "{{ region }}"
instance_ids: "{{ item }}"
wait: true
with_items: "{{ instance_facts.instances | selectattr('state', 'equalto', 'running') | map(attribute='id') | list }}"
when:
- instance_facts.instances | length > 0
- name: POSTGRESQL CLEANUP | gathering {{ region }} available volumes
ec2_vol_facts:
region: "{{ region }}"
filters:
status: available
register: volume_facts
- name: POSTGRESQL CLEANUP | terminating volumes in {{ region }}
ec2_vol:
state: absent
region: "{{ region }}"
id: "{{ item }}"
with_items: "{{ volume_facts.volumes | selectattr('status', 'equalto', 'available') | map(attribute='id') | list }}"
when:
- volume_facts.volumes | length > 0
- name: POSTGRESQL CLEANUP | terminating interfaces in {{ region }}
ec2_eni:
state: absent
region: "{{ region }}"
eni_id: "{{ item }}"
with_items: "{{ eni_facts.interfaces | selectattr('status', 'equalto', 'available') | map(attribute='id') | list }}"
when:
- eni_facts.interfaces | length > 0
- name: POSTGRESQL CLEANUP | removing {{ application }} key
ec2_key:
state: absent
region: "{{ region }}"
name: "{{ region }}-{{ application }}"
- name: POSTGRESQL CLEANUP | retrieving VPC ID for {{ network }} in {{ region }}
ec2_vpc_net_facts:
region: "{{ region }}"
# This filter must map exactly to what we created in the e2_vpc module
filters:
cidr_block: "{{ cidr_block }}"
register: vpc_facts
- name: POSTGRESQL CLEANUP | removing postgresql security groups
local_action:
module: ec2_group
name: "{{ project }}_postgresql"
vpc_id: "{{ (vpc_facts.vpcs|selectattr('state', 'equalto', 'available')|map(attribute='id')|list).0 }}"
region: "{{ region }}"
state: absent
when: cloud == 'aws'
- block:
- include_role:
name: azure
tasks_from: discover-resourcegroup
- name: POSTGRESQL CLEANUP | removing {{ application }} VM in {{ specified_resourcegroup.name }}
azure_rm_virtualmachine:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}-{{ application }}-{{ item }}"
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | removing {{ application }} master VM in {{ specified_resourcegroup.name }}
azure_rm_virtualmachine:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}-{{ application }}-{{ item }}-master"
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | removing {{ application }} replica VM in {{ specified_resourcegroup.name }}
azure_rm_virtualmachine:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}-{{ application }}-{{ item }}-replica"
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | standalone NIC removal if we haven't booted a machine
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_internal"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | standalone NIC removal if we haven't booted a machine
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_external"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | {{ project }}_{{ application }}_{{ ec2_tag_Role | default('master') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_master_internal removal
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ ec2_tag_Role | default('master') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_{{ item }}_master_internal"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | {{ project }}_{{ application }}_{{ ec2_tag_Role | default('master') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_master_external removal
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ ec2_tag_Role | default('master') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_{{ item }}_master_external"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | {{ project }}_{{ application }}_{{ ec2_tag_Role | default('replica') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_replica_internal removal
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ ec2_tag_Role | default('replica') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_{{ item }}_replica_internal"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | {{ project }}_{{ application }}_{{ ec2_tag_Role | default('replica') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_replica_external removal
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ ec2_tag_Role | default('replica') }}_{{ cluster | default('a') }}_{{ dataflow | default('none') }}_{{ item }}_replica_external"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | ACL removal if we haven't booted a machine
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_master_internal"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | ACL removal if we haven't booted a machine
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_master_external"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | ACL removal if we haven't booted a machine
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_replica_internal"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | ACL removal if we haven't booted a machine
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}_{{ application }}_{{ item }}_replica_external"
state: absent
with_sequence: count={{ count }}
- name: POSTGRESQL CLEANUP | dnsg_{{ project }}_{{ application }}_public_internal removal
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "dnsg_{{ project }}_{{ application }}_public_internal"
state: absent
when: cloud == 'azure'