| 1 |
# Bacula(R) - The Network Backup Solution |
| 2 |
# |
| 3 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 4 |
# |
| 5 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 6 |
# from many others, a complete list can be found in the file AUTHORS. |
| 7 |
# |
| 8 |
# You may use this file and others of this release according to the |
| 9 |
# license defined in the LICENSE file, which includes the Affero General |
| 10 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 11 |
# terms pursuant to its AGPLv3 Section 7. |
| 12 |
# |
| 13 |
# This notice must be preserved when any source code is |
| 14 |
# conveyed and/or propagated. |
| 15 |
# |
| 16 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 17 |
|
| 18 |
import logging |
| 19 |
import re |
| 20 |
import time |
| 21 |
|
| 22 |
from baculak8s.entities.file_info import DIRECTORY, EMPTY_FILE, NOT_EMPTY_FILE |
| 23 |
from baculak8s.entities.k8sobjtype import K8SObjType |
| 24 |
from baculak8s.io.jobs.restore_io import (COMMA_SEPARATOR_NOT_SUPPORTED, |
| 25 |
FILE_ERROR_TEMPLATE, |
| 26 |
FILE_TRANSFER_START, |
| 27 |
RESTORE_LOOP_ERROR, RESTORE_START, |
| 28 |
SKIP_PACKET, SUCCESS_PACKET, |
| 29 |
FileContentReader, RestoreIO, |
| 30 |
RestorePacket) |
| 31 |
from baculak8s.jobs.job_pod_bacula import JobPodBacula |
| 32 |
from baculak8s.plugins.k8sbackend.k8sfileinfo import k8sfile2objname |
| 33 |
from baculak8s.services.job_info_service import (REPLACE_ALWAYS, |
| 34 |
REPLACE_NEVER) |
| 35 |
from baculak8s.util.respbody import parse_json_descr |
| 36 |
from baculak8s.util.sslserver import DEFAULTTIMEOUT |
| 37 |
|
| 38 |
NS_DOESNOT_EXIST_ERR = "Namespace '{namespace}' does not exist! You should restore namespace too!" |
| 39 |
PVC_DOESNOT_EXIST_ERR = "PVC: '{pvcname}' does not exist or is not mounted! " \ |
| 40 |
"You should restore PVC config or mount it in any Pod!" |
| 41 |
PVC_ISNOTREADY_ERR = "PVC: '{pvcname}' is not ready in expected time! " \ |
| 42 |
"You should check PVC config or logs!" |
| 43 |
SERVICE_ACCOUNT_TOKEN_RESTORE_INFO = "Unable to restore service-account-token {token}. Regenerated new one." |
| 44 |
RESTORE_RES_ERR = "Cannot restore resource object. Err={}" |
| 45 |
PVC_STATUS_ERR = "PVC: '{}' invalid status! Err={}" |
| 46 |
|
| 47 |
|
| 48 |
class RestoreJob(JobPodBacula): |
| 49 |
""" |
| 50 |
Job that contains the business logic |
| 51 |
related to the restore mode of the Backend. |
| 52 |
It depends upon a Plugin Class implementation |
| 53 |
that sends data to the Plugins Data Source. |
| 54 |
""" |
| 55 |
|
| 56 |
def __init__(self, plugin, params): |
| 57 |
super().__init__(plugin, RestoreIO(), params) |
| 58 |
self.ns = {} |
| 59 |
self.localrestore = "where" in self._params and self._params["where"] != "" |
| 60 |
|
| 61 |
def execution_loop(self): |
| 62 |
return self.__restore_loop() |
| 63 |
|
| 64 |
def execute(self): |
| 65 |
self._start(RESTORE_START), |
| 66 |
self.execution_loop() |
| 67 |
self._io.send_eod() |
| 68 |
|
| 69 |
def __restore_loop(self): |
| 70 |
file_info = None |
| 71 |
while True: |
| 72 |
packet_type, packet = self._io.next_loop_packet(onError=self._abort) |
| 73 |
if packet_type == RestorePacket.RESTORE_END: |
| 74 |
break |
| 75 |
elif packet_type == RestorePacket.FILE_INFO: |
| 76 |
file_info = packet |
| 77 |
self.restore_object(file_info) |
| 78 |
else: |
| 79 |
self._io.send_abort(RESTORE_LOOP_ERROR) |
| 80 |
self._abort() |
| 81 |
|
| 82 |
def restore_object(self, file_info): |
| 83 |
dorestore = self.__handle_file_info(file_info) |
| 84 |
logging.debug('restore_object:dorestore:'+str(dorestore)) |
| 85 |
if dorestore: |
| 86 |
# everything is ok, so do a restore |
| 87 |
reader = FileContentReader() |
| 88 |
self._io.send_command(SUCCESS_PACKET) |
| 89 |
self._read_start(FILE_TRANSFER_START, onError=self._abort) |
| 90 |
|
| 91 |
if self.localrestore or file_info.objtype != K8SObjType.K8SOBJ_PVCDATA: |
| 92 |
self.__restore_file(file_info, reader) |
| 93 |
else: |
| 94 |
namespace = file_info.namespace |
| 95 |
self.__restore_pvcdata(namespace) |
| 96 |
|
| 97 |
elif dorestore is not None: |
| 98 |
self._io.send_command(SKIP_PACKET) |
| 99 |
|
| 100 |
def __handle_file_info(self, file_info): |
| 101 |
logging.debug('handle_file_info:fileinfo: {}'.format(file_info)) |
| 102 |
dorestore = True |
| 103 |
if file_info.type in [EMPTY_FILE, NOT_EMPTY_FILE]: |
| 104 |
if "replace" in self._params and not self.__should_replace(file_info): |
| 105 |
dorestore = False |
| 106 |
|
| 107 |
else: |
| 108 |
if self.localrestore: |
| 109 |
file_info = self._plugin.apply_where_parameter(file_info, self._params["where"]) |
| 110 |
if "regexwhere" in self._params and self._params["regexwhere"] != '': |
| 111 |
self.__apply_regexwhere_param(file_info) |
| 112 |
|
| 113 |
else: |
| 114 |
if file_info.objtype == K8SObjType.K8SOBJ_PVCDATA: |
| 115 |
dorestore = self.__prepare_pvcdata_restore(file_info) |
| 116 |
|
| 117 |
elif file_info.type == DIRECTORY: |
| 118 |
# We just ignore it for the current implementation |
| 119 |
dorestore = False |
| 120 |
|
| 121 |
logging.debug('handle_file_info:dorestore: {}'.format(dorestore)) |
| 122 |
return dorestore |
| 123 |
|
| 124 |
def __prepare_pvcdata_restore(self, file_info): |
| 125 |
""" |
| 126 |
Successful pvcdata restore require the following prerequisites: |
| 127 |
- namespace has to exist so at least restored with this job |
| 128 |
- pvcs has to exist so at least restored with this job |
| 129 |
:param file_info: pvcdata FileInfo for restore |
| 130 |
:return: |
| 131 |
""" |
| 132 |
|
| 133 |
namespace = file_info.namespace |
| 134 |
nsdata = self.ns.get(namespace) |
| 135 |
if nsdata is None: |
| 136 |
# no information about namespace, get it |
| 137 |
logging.debug('prepare_pvcdata_restore:no previous ns check. do it!') |
| 138 |
nsexist = self._plugin.check_namespace(namespace) |
| 139 |
nsdata = { |
| 140 |
'exist': nsexist, |
| 141 |
} |
| 142 |
self.ns[namespace] = nsdata |
| 143 |
if nsexist is None: |
| 144 |
logging.debug('prepare_pvcdata_restore ns:{} not exist!'.format(namespace)) |
| 145 |
self._handle_error(NS_DOESNOT_EXIST_ERR.format(namespace=namespace)) |
| 146 |
return None |
| 147 |
else: |
| 148 |
if nsdata.get('exist', None) is None: |
| 149 |
logging.debug('prepare_pvcdata_restore ns:cached:{} not exist!'.format(namespace)) |
| 150 |
return False |
| 151 |
logging.debug('prepare_pvcdata_restore ns: {} found.'.format(namespace)) |
| 152 |
|
| 153 |
pvcdatalist = nsdata.get('pvcs') |
| 154 |
if pvcdatalist is None: |
| 155 |
# grab pvcs |
| 156 |
pvcdatalist = self._plugin.list_pvcdata_for_namespace(namespace, allpvcs=True) |
| 157 |
logging.debug('prepare_pvcdata_restore pvcdatalist:{}'.format(pvcdatalist)) |
| 158 |
self.ns[namespace].update({ |
| 159 |
'pvcs': pvcdatalist, |
| 160 |
}) |
| 161 |
|
| 162 |
pvcname = k8sfile2objname(file_info.name) |
| 163 |
pvcdata = pvcdatalist.get(pvcname) |
| 164 |
if pvcdata is None: |
| 165 |
logging.debug('prepare_pvcdata_restore pvc:{} not exist!'.format(pvcname)) |
| 166 |
self._handle_error(PVC_DOESNOT_EXIST_ERR.format(pvcname=pvcname)) |
| 167 |
return None |
| 168 |
|
| 169 |
pvcisready = False |
| 170 |
for _ in range(DEFAULTTIMEOUT): |
| 171 |
time.sleep(1) |
| 172 |
isready = self._plugin.pvc_isready(namespace, pvcname) |
| 173 |
iswaiting = self._plugin.is_pvc_waiting_first_consumer(namespace,pvcname) |
| 174 |
if isinstance(isready, dict) and 'error' in isready: |
| 175 |
# cannot check pvc status |
| 176 |
self._handle_error(PVC_STATUS_ERR.format(pvcname, parse_json_descr(isready))) |
| 177 |
elif isready or iswaiting: |
| 178 |
pvcisready = True |
| 179 |
break |
| 180 |
# well, we have to wait for pvc to be ready, so restart procedure |
| 181 |
logging.debug('Waiting for pvc to become ready...') |
| 182 |
|
| 183 |
if not pvcisready: |
| 184 |
logging.debug('prepare_pvcdata_restore pvc:{} is not ready in expected time!'.format(pvcname)) |
| 185 |
self._handle_error(PVC_ISNOTREADY_ERR.format(pvcname=pvcname)) |
| 186 |
return None |
| 187 |
|
| 188 |
self.ns[namespace].update({ |
| 189 |
'pvcdata': pvcdata, |
| 190 |
}) |
| 191 |
logging.debug('prepare_pvcdata_restore PVCDATA:{}'.format(pvcdata)) |
| 192 |
|
| 193 |
if self.prepare_bacula_pod(pvcdata, namespace=namespace, mode='restore'): |
| 194 |
return True |
| 195 |
|
| 196 |
return False |
| 197 |
|
| 198 |
def __should_replace(self, file_info): |
| 199 |
""" |
| 200 |
Checks if restored object is already available as we have to distinguish between create and patch. |
| 201 |
:param file_info: |
| 202 |
:return: |
| 203 |
""" |
| 204 |
if file_info.objtype == K8SObjType.K8SOBJ_PVCDATA: |
| 205 |
# always replace file_info |
| 206 |
return True |
| 207 |
curent_file = self._plugin.check_file(file_info) |
| 208 |
# cache get object for future use |
| 209 |
if isinstance(curent_file, dict) and 'error' in curent_file: |
| 210 |
file_info.objcache = None |
| 211 |
logging.error("check_file: {}".format(curent_file['error'])) |
| 212 |
else: |
| 213 |
file_info.objcache = curent_file |
| 214 |
if self._params["replace"] == REPLACE_ALWAYS: |
| 215 |
return True |
| 216 |
if self._params["replace"] == REPLACE_NEVER: |
| 217 |
if curent_file is not None: |
| 218 |
return False |
| 219 |
# XXX: we cannot support ifnewer or ifolder because k8s does not provide modification time |
| 220 |
return True |
| 221 |
|
| 222 |
def __apply_regexwhere_param(self, file_info): |
| 223 |
if re.match(r",(.+?),(.+?),", self._params["regexwhere"]): |
| 224 |
self._io.send_abort(COMMA_SEPARATOR_NOT_SUPPORTED) |
| 225 |
self._abort() |
| 226 |
|
| 227 |
file_info.apply_regexwhere_param(self._params["regexwhere"]) |
| 228 |
|
| 229 |
def __handle_pvcdata_connections(self): |
| 230 |
logging.debug('restore_pvcdata:data send') |
| 231 |
response = self.connsrv.handle_connection(self.handle_pod_data_send) |
| 232 |
if 'error' in response: |
| 233 |
self._handle_error(response['error']) |
| 234 |
return False |
| 235 |
logging.debug('backup_pvcdata:logs recv') |
| 236 |
response = self.connsrv.handle_connection(self.handle_pod_logs) |
| 237 |
if 'error' in response: |
| 238 |
self._handle_error(response['error']) |
| 239 |
return False |
| 240 |
return True |
| 241 |
|
| 242 |
def __restore_pvcdata(self, namespace): |
| 243 |
self.__handle_pvcdata_connections() |
| 244 |
self.handle_tarstderr() |
| 245 |
self.handle_delete_pod(namespace=namespace) |
| 246 |
self._io.send_command(SUCCESS_PACKET) |
| 247 |
|
| 248 |
def __restore_file(self, file_info, reader): |
| 249 |
if file_info.size == 0 and file_info.objtype != K8SObjType.K8SOBJ_PVCDATA: |
| 250 |
logging.debug('file_info.size == 0') |
| 251 |
response = self._plugin.restore_file(file_info) |
| 252 |
else: |
| 253 |
reader.finished = False |
| 254 |
response = self._plugin.restore_file(file_info, reader) |
| 255 |
if isinstance(response, dict) and 'error' in response: |
| 256 |
if file_info.objtype == K8SObjType.K8SOBJ_SECRET: |
| 257 |
self._io.send_warning(SERVICE_ACCOUNT_TOKEN_RESTORE_INFO.format( |
| 258 |
token=file_info.name.split('/')[-1][:-5])) |
| 259 |
else: |
| 260 |
if 'exception' in response: |
| 261 |
error_msg = RESTORE_RES_ERR.format(parse_json_descr(response)) |
| 262 |
self._handle_error(error_msg) |
| 263 |
else: |
| 264 |
error_msg = FILE_ERROR_TEMPLATE.format(file_info.name, file_info.namespace, response['error']) |
| 265 |
self._handle_error(error_msg) |
| 266 |
else: |
| 267 |
if file_info.size != 0 or file_info.objtype == K8SObjType.K8SOBJ_PVCDATA: |
| 268 |
self._io.send_command(SUCCESS_PACKET) |