-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.yml
170 lines (158 loc) · 6.12 KB
/
restore.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
169
170
---
- name: Restore volume backups
hosts: vm
tags: reset, vm-reset, backup, restore-backup, always
tasks:
- name: Wait for all pods in longhorn-system to be running
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
kind: Pod
namespace: longhorn-system
register: longhorn_pods
retries: 60
delay: 15
until: |
longhorn_pods.resources is defined and
longhorn_pods.resources | length > 0 and
longhorn_pods.resources | map(attribute='status.phase') | list | difference(['Running']) | length == 0
- name: Check if Longhorn backup names are stored
ansible.builtin.stat:
path: "{{ longhorn_volume_map_local_dir }}/longhorn_volumes_map_{{ inventory_hostname }}.json"
register: longhorn_volumes_map_file
delegate_to: localhost
- name: Read from file per host
ansible.builtin.slurp:
src: "{{ longhorn_volume_map_local_dir }}/longhorn_volumes_map_{{ inventory_hostname }}.json"
register: longhorn_volumes_map_file_content
delegate_to: localhost
when: longhorn_volumes_map_file.stat.exists
- name: Set list of longhorn backups
ansible.builtin.set_fact:
longhorn_volumes_map: "{{ longhorn_volumes_map_file_content['content'] | b64decode | from_json }}"
when: longhorn_volumes_map_file.stat.exists
- name: Set empty fact if file does not exist per host
ansible.builtin.set_fact:
longhorn_volumes_map: []
when: not longhorn_volumes_map_file.stat.exists
- name: Get Longhorn volumes
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
kind: Volume
api_version: longhorn.io/v1beta2
namespace: longhorn-system
register: longhorn_volumes
- name: Restore volumes
become: true
kubernetes.core.k8s:
state: present
kubeconfig: "{{ kubeconfig_path }}"
definition:
apiVersion: longhorn.io/v1beta2
kind: Volume
metadata:
labels:
longhornvolume: vol-01
name: "{{ item.volumeName }}"
namespace: longhorn-system
spec:
fromBackup: "s3://longhorn-backups-{{ rancher_cluster_name }}@minio?volume={{ item.volumeName }}&backup={{ item.backupName }}"
numberOfReplicas: 1
frontend: blockdev
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined and
item.volumeName not in longhorn_volumes.resources | map(attribute='metadata.name') | list
register: restore_vols
retries: 60
delay: 5
until: restore_vols is not failed
loop_control:
pause: 5
- name: Wait for volume to have status.restoreRequired false
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
api_version: longhorn.io/v1beta2
kind: Volume
name: "{{ item.volumeName }}"
namespace: longhorn-system
register: longhorn_volume
retries: 60
delay: 5
until: |
longhorn_volume.resources is defined and
longhorn_volume.resources | length > 0 and
longhorn_volume.resources[0].status is defined and
longhorn_volume.resources[0].status.restoreRequired is defined and
not longhorn_volume.resources[0].status.restoreRequired
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined and
item.volumeName not in longhorn_volumes.resources | map(attribute='metadata.name') | list
- name: Get a running longhorn-ui pod
become: true
kubernetes.core.k8s_info:
kubeconfig: "{{ kubeconfig_path }}"
kind: "Pod"
namespace: "longhorn-system"
label_selectors:
- "app=longhorn-ui"
field_selectors:
- "status.phase=Running"
register: longhorn_ui_pod
retries: 360
delay: 5
until: |
longhorn_ui_pod.resources is defined and
longhorn_ui_pod.resources | length > 0 and
longhorn_ui_pod.resources[0].metadata is defined and
longhorn_ui_pod.resources[0].metadata.name is defined
when:
- restore_vols is defined
- not restore_vols is skipped
- name: Call Longhorn to create PV
become: true
kubernetes.core.k8s_exec:
kubeconfig: "{{ kubeconfig_path }}"
namespace: longhorn-system
pod: "{{ longhorn_ui_pod.resources[0].metadata.name }}"
container: longhorn-ui
command: >-
curl -X POST -d '{"pvName":"{{ item.pvName }}","fsType":"xfs"}'
"http://longhorn-frontend.longhorn-system.svc.cluster.local/v1/volumes/{{ item.volumeName }}?action=pvCreate"
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined and
item.volumeName not in longhorn_volumes.resources | map(attribute='metadata.name') | list
loop_control:
pause: 1
- name: Create namespaces for PVCs
become: true
kubernetes.core.k8s:
state: present
kubeconfig: "{{ kubeconfig_path }}"
definition:
apiVersion: v1
kind: Namespace
metadata:
name: "{{ item.namespace }}"
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined and
item.volumeName not in longhorn_volumes.resources | map(attribute='metadata.name') | list
loop_control:
pause: 1
- name: Call Longhorn to create PVC
become: true
kubernetes.core.k8s_exec:
kubeconfig: "{{ kubeconfig_path }}"
namespace: longhorn-system
pod: "{{ longhorn_ui_pod.resources[0].metadata.name }}"
container: longhorn-ui
command: >-
curl -X POST -d '{"namespace":"{{ item.namespace }}","pvcName":"{{ item.pvcName }}"}'
"http://longhorn-frontend.longhorn-system.svc.cluster.local/v1/volumes/{{ item.volumeName }}?action=pvcCreate"
loop: "{{ longhorn_volumes_map }}"
when: longhorn_volumes_map is defined and
item.volumeName not in longhorn_volumes.resources | map(attribute='metadata.name') | list
loop_control:
pause: 1
...