Razique Mahroua
2020-03-18 b85c91a8192593f6b62f93e11c971868964343a9
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
---
- import_playbook: ../../setup_runtime.yml
 
- name: Backup event log of the user
  hosts: localhost
  connection: local
  gather_facts: False
  become: no
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  tasks:
    - name: Create Infra Key
      include_role:
        name: infra-ec2-ssh-key
      when: install_infra_ssh_key | default(false) | bool
 
    - name: Get fact for stack
      cloudformation_facts:
        stack_name: "{{ project_tag }}"
      register: stack_facts
 
    - when: project_tag in stack_facts.ansible_facts.cloudformation
      block:
        - name: Grab and set stack creation_time
          set_fact:
            stack_creation_time: >-
              {{ stack_facts.ansible_facts.cloudformation[project_tag].stack_description.creation_time }}
            stack_status: >-
              {{ stack_facts.ansible_facts.cloudformation[project_tag].stack_description.stack_status }}
 
        - when: stack_status == "CREATE_COMPLETE"
          block:
            - name: Grab student user
              set_fact:
                student_stack_user: >-
                  {{ stack_facts.ansible_facts.cloudformation[project_tag].stack_outputs.StudentUser }}
 
            - name: Backup event log for user
              shell: >-
                aws cloudtrail lookup-events
                --lookup-attributes AttributeKey=Username,AttributeValue={{ student_stack_user }}
                --start-time {{ stack_creation_time }}
                --max-items 10000
                > {{ output_dir }}/{{ env_type }}_{{ guid }}_cloudtrail_event_log.json
              when:
                - email is defined or owner is defined
                - run_cloudtrail | default(true) | bool
              failed_when: false
 
- name: Build inventory
  hosts: localhost
  connection: local
  gather_facts: False
  become: no
  tasks:
    - when: cloud_provider == 'ec2'
      block:
      - name: Run infra-ec2-create-inventory Role
        include_role:
          name: infra-ec2-create-inventory
 
      - name: Run Common SSH Config Generator Role
        include_role:
          name: infra-common-ssh-config-generate
        when: "'bastions' in groups"
 
- import_playbook: ../../setup_runtime.yml
 
- name: Start clientVM and cluster instances if they are stopped
  hosts: localhost
  connection: local
  gather_facts: False
  become: no
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  tasks:
    - when:
        - stack_status is defined
        - stack_status == 'CREATE_COMPLETE'
      block:
        - set_fact:
            clientvm_id: "{{ hostvars[groups.bastions[0]].instance_id }}"
          when:
            - "'bastions' in groups"
            - groups.bastions | length > 0
 
        - fail:
            msg: "No clientVM present"
          when: >-
            'bastions' not in groups or groups.bastions | length == 0
 
        - when:
            - stack_status is defined
            - stack_status == 'CREATE_COMPLETE'
          block:
            - name: Start clientVM instance
              command: "aws ec2 start-instances --instance-ids '{{clientvm_id}}'"
              register: _startclientvm
              until: _startclientvm is succeeded
              retries: 10
              delay: "{{ 60|random(start=3, step=1) }}"
 
            - name: Get cluster instance Ids
              command: >-
                aws ec2 describe-instances
                --filters
                "Name=tag:clusterid,Values={{ cluster_name }}"
                "Name=instance-state-name,Values=pending,running,shutting-down,stopping,stopped"
                --query 'Reservations[*].Instances[*].InstanceId'
                --output text
              changed_when: false
              # Sometimes deployment fails before any OCP instances is created, so comment this line:
              # failed_when: instanceids.stdout | trim | length == 0
              register: instanceids
              until: instanceids is succeeded
              retries: 10
              delay: "{{ 60|random(start=3, step=1) }}"
 
            - name: Start cluster instances
              when: instanceids.stdout | trim | length > 0
              command: >-
                    aws ec2 start-instances --instance-ids {{ instanceids.stdout | trim }}
              register: _startinstances
              until: _startinstances is succeeded
              retries: 10
              delay: "{{ 60|random(start=3, step=1) }}"
 
            - name: Wait for clientVM instance
              command: "aws ec2 wait instance-running --instance-ids '{{clientvm_id}}'"
 
            - name: Wait for cluster instances
              when: instanceids.stdout | trim | length > 0
              ignore_errors: true
              command: >-
                    aws ec2 wait instance-running
                    --filters "Name=tag:clusterid,Values={{ cluster_name }}"
 
- name: Remove workloads
  hosts: bastions
  gather_facts: false
  run_once: true
  become: false
  tasks:
  - name: Set Ansible Python interpreter to k8s virtualenv
    set_fact:
      ansible_python_interpreter: /opt/virtualenvs/k8s/bin/python
  - name: Remove ocp workloads
    when:
    - remove_workloads | d("") | length > 0
    tags:
    - remove_workloads
    block:
      - name: Set facts for remote access
        set_fact:
          ansible_ssh_extra_args: >-
            {{ ansible_ssh_extra_args|d() }}
            -F {{hostvars.localhost.output_dir}}/{{ env_type }}_{{ guid }}_ssh_conf
      - name: Invoke roles to remove ocp workloads
        include_role:
          name: "{{ workload_loop_var }}"
        vars:
          ocp_username: "system:admin"
          ACTION: "remove"
        loop: "{{ remove_workloads }}"
        loop_control:
          loop_var: workload_loop_var
 
- name: Destroy OCP 4 resources using the installer
  hosts: bastions
  gather_facts: false
  become: no
  run_once: yes
  tasks:
    - when:
        - hostvars.localhost.stack_status is defined
        - hostvars.localhost.stack_status == 'CREATE_COMPLETE'
      block:
        - name: Set facts for remote access
          set_fact:
            ansible_ssh_extra_args: >-
              {{ ansible_ssh_extra_args|d() }}
              -F {{hostvars.localhost.output_dir}}/{{ env_type }}_{{ guid }}_ssh_conf
 
        - name: Wait for linux host to be available
          wait_for_connection:
            timeout: 20
 
        - name: Pack an archive of everything in case something goes wrong
          become: yes
          archive:
            path:
              - /home
              - /root
            dest: /tmp/home.tar.gz
          # TODO: remove ignore_errors when https://github.com/ansible/ansible/issues/42090 is fixed
          ignore_errors: yes
 
        - name: Fetch the archive
          fetch:
            flat: yes
            src: /tmp/home.tar.gz
            dest: "{{ hostvars.localhost.output_dir }}/{{ env_type }}_{{ guid }}_user_home.tar.gz"
 
        - stat:
            path: /home/{{ ansible_user }}/{{ cluster_name }}/metadata.json
          register: statclusterdir
 
        - name: Wait until the file /tmp/deployinprogress lockfile is absent
          wait_for:
            path: /tmp/deployinprogress
            state: absent
            timeout: 1800
            sleep: 10
 
        - when: statclusterdir.stat.exists
          block:
            - name: destroy terraform resources (openshift-install destroy cluster)
              command: openshift-install destroy cluster --dir=/home/{{ ansible_user }}/{{ cluster_name }}/
              register: destroyr
              async: 2400
              poll: 30
              retries: 3
              delay: 10
              until: destroyr is succeeded
 
            - set_fact:
                oktodelete: yes
 
          always:
            - name: pack an archive of everything
              archive:
                path: /home/{{ansible_user}}/{{ cluster_name }}
                dest: /tmp/{{ cluster_name }}.tar.gz
 
            - name: get archive of environment target dir
              fetch:
                flat: yes
                src: /tmp/{{ cluster_name }}.tar.gz
                dest: "{{ hostvars.localhost.output_dir }}/{{ env_type }}_{{ guid }}_{{ cluster_name }}.tar.gz"
        - when: not statclusterdir.stat.exists
          block:
            - name: Detect cluster dir using the terraform file (/root)
              find:
                file_type: file
                paths: /root
                pattern: terraform.tfstate
                recurse: yes
                depth: 5
              become: yes
              register: findr
 
            - name: Destroy all clusters
              loop: "{{ findr.files }}"
              include_tasks: destroy_cluster.yml
              vars:
                cluster_dir: "{{ item.path | dirname }}"
                username: root
 
            - name: Detect cluster dir using the terraform file (/home)
              find:
                file_type: file
                paths: /home
                pattern: terraform.tfstate
                recurse: yes
                depth: 5
              become: yes
              register: findr
 
            - name: Destroy all clusters
              loop: "{{ findr.files }}"
              include_tasks: destroy_cluster.yml
              vars:
                cluster_dir: "{{ item.path | dirname }}"
                username: "{{ item.path | dirname | regex_replace('/home/([^/]+).*', '\\1') }}"
 
            - set_fact:
                oktodelete: yes
      always:
        - name: Test if janitor is present
          shell: command -v janitor
          register: commandjanitor
          delegate_to: localhost
          failed_when: false
          changed_when: false
 
        - when:
            - commandjanitor.rc == 0
            - run_janitor | default(false) | bool
          block:
            - name: Run janitor to get existing resources
              environment:
                AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
                AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
                AWS_REGION: "{{aws_region_final|d(aws_region)}}"
              command: >-
                janitor
                -u={{ hostvars.localhost.student_stack_user }}
                -t='{{ hostvars.localhost.stack_creation_time }}'
                -r
                -quiet
              register: janitorreg
              delegate_to: localhost
 
            - debug:
                var: janitorreg.stdout
 
            - name: Send email about remaining resources
              when:
                - janitorreg.stdout_lines | length > 0
                - report_email is defined
              mail:
                subject: "OCP4 lab: Remaining resources found for user {{ hostvars.localhost.student_stack_user }}"
                to: "{{ report_email }}"
                subtype: html
                body: |-
                  <pre>
                  {{ janitorreg.stdout }}
                  </pre>
                from: "{{ report_from_email | d(report_email) }}"
                headers:
                  - Reply-To={{ report_reply_to_email | d(report_email) }}
              delegate_to: localhost
 
            - name: Output janitor logs
              when: report_email is not defined
              debug:
                var: janitorreg.stdout
 
- name: Delete ocp4 provisioner stack
  hosts: localhost
  connection: local
  gather_facts: False
  become: no
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  tasks:
    - set_fact:
        aws_public_zone: "{{ guid }}.{{ subdomain_base_suffix | regex_replace('^\\.', '') }}."
 
    - name: Grab zone ID
      # use CLI here because the route53_zone module is not graceful and asks for all zones
      # then iterates in python to find the one. This causes Throttling errors.
      # https://github.com/ansible/ansible/blob/05c6ff79f9860dbd6b43cb4914ee749baf65b9f7/lib/ansible/modules/cloud/amazon/route53_zone.py#L145
      command: >-
        aws route53 list-hosted-zones-by-name
        --dns-name {{ aws_public_zone }}
        --max-items 4
        --output json
      register: awsroute53zone
      changed_when: false
      retries: 5
      delay: "{{ 60|random(start=10, step=1) }}"
      until: awsroute53zone is succeeded
 
    - name: delete zones
      include_tasks: delete_zone.yml
      vars:
        _zone: "{{ loopzone }}"
        _hostedzoneid: "{{ loopzone.Id | regex_replace('/hostedzone/', '') }}"
      when:
        - loopzone.Name == aws_public_zone
      loop: "{{ awsroute53zone.stdout|from_json|json_query('HostedZones') }}"
      loop_control:
        loop_var: loopzone
 
    - name: Run infra-ec2-template-destroy
      include_role:
        name: infra-ec2-template-destroy
      when:
        # Before deleting the provisioner, make sure destroy terraform successfully run.
        - stack_status is defined
        - stack_status == 'CREATE_COMPLETE'
        - "'bastions' in groups"
        - groups.bastions | length > 0
        - hostvars[groups.bastions[0]].oktodelete
 
    - name: Run infra-ec2-template-destroy
      include_role:
        name: infra-ec2-template-destroy
      when:
        # delete stack if stack creation failed
        - stack_status is defined
        - stack_status != 'CREATE_COMPLETE'