summaryrefslogtreecommitdiffstats
path: root/playbooks/common/openshift-cluster/upgrades/etcd/backup.yml
blob: 9d0333ca836067817fae56db59dfb480bbb6efdb (plain)
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
---
- name: Backup etcd
  hosts: oo_etcd_hosts_to_backup
  vars:
    embedded_etcd: "{{ groups.oo_etcd_to_config | default([]) | length == 0 }}"
    etcdctl_command: "{{ 'etcdctl' if not openshift.common.is_containerized or embedded_etcd else 'docker exec etcd_container etcdctl' if not openshift.common.is_etcd_system_container else 'runc exec etcd etcdctl' }}"
    timestamp: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
  roles:
  - openshift_facts
  tasks:
  # Ensure we persist the etcd role for this host in openshift_facts
  - openshift_facts:
      role: etcd
      local_facts: {}
    when: "'etcd' not in openshift"
  - set_fact:
      etcd_backup_dir: "{{ openshift.etcd.etcd_data_dir }}/openshift-backup-{{ backup_tag | default('') }}{{ timestamp }}"

  # TODO: replace shell module with command and update later checks
  - name: Check available disk space for etcd backup
    shell: df --output=avail -k {{ openshift.etcd.etcd_data_dir }} | tail -n 1
    register: avail_disk
    # AUDIT:changed_when: `false` because we are only inspecting
    # state, not manipulating anything
    changed_when: false

  # TODO: replace shell module with command and update later checks
  - name: Check current etcd disk usage
    shell: du --exclude='*openshift-backup*' -k {{ openshift.etcd.etcd_data_dir }} | tail -n 1 | cut -f1
    register: etcd_disk_usage
    when: embedded_etcd | bool
    # AUDIT:changed_when: `false` because we are only inspecting
    # state, not manipulating anything
    changed_when: false

  - name: Abort if insufficient disk space for etcd backup
    fail:
      msg: >
        {{ etcd_disk_usage.stdout }} Kb disk space required for etcd backup,
        {{ avail_disk.stdout }} Kb available.
    when: (embedded_etcd | bool) and (etcd_disk_usage.stdout|int > avail_disk.stdout|int)

  # For non containerized and non embedded we should have the correct version of
  # etcd installed already. So don't do anything.
  #
  # For containerized installs we now exec into etcd_container
  #
  # For embedded non containerized we need to ensure we have the latest version
  # etcd on the host.
  - name: Install latest etcd for embedded
    package:
      name: etcd
      state: latest
    when:
    - embedded_etcd | bool
    - not openshift.common.is_atomic | bool

  - name: Generate etcd backup
    command: >
      {{ etcdctl_command }} backup --data-dir={{ openshift.etcd.etcd_data_dir }}
      --backup-dir={{ etcd_backup_dir }}

  # According to the docs change you can simply copy snap/db
  # https://github.com/openshift/openshift-docs/commit/b38042de02d9780842dce95cfa0ef45d53b58bc6
  - name: Check for v3 data store
    stat:
      path: "{{ openshift.etcd.etcd_data_dir }}/member/snap/db"
    register: v3_db

  - name: Copy etcd v3 data store
    command: >
      cp -a {{ openshift.etcd.etcd_data_dir }}/member/snap/db
      {{ etcd_backup_dir }}/member/snap/
    when: v3_db.stat.exists

  - set_fact:
      etcd_backup_complete: True

  - name: Display location of etcd backup
    debug:
      msg: "Etcd backup created in {{ etcd_backup_dir }}"

- name: Gate on etcd backup
  hosts: localhost
  connection: local
  become: no
  tasks:
  - set_fact:
      etcd_backup_completed: "{{ hostvars
                                 | oo_select_keys(groups.oo_etcd_hosts_to_backup)
                                 | oo_collect('inventory_hostname', {'etcd_backup_complete': true}) }}"
  - set_fact:
      etcd_backup_failed: "{{ groups.oo_etcd_hosts_to_backup | difference(etcd_backup_completed) }}"
  - fail:
      msg: "Upgrade cannot continue. The following hosts did not complete etcd backup: {{ etcd_backup_failed | join(',') }}"
    when: etcd_backup_failed | length > 0