| 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 sys |
| 20 |
import time |
| 21 |
from abc import ABCMeta, abstractmethod |
| 22 |
|
| 23 |
from baculak8s.io.log import Log |
| 24 |
|
| 25 |
INVALID_START_TEMPLATE = "Invalid start packet. Expected packet: {}" |
| 26 |
KUBERNETES_CODE_INFO = "Connected to Kubernetes {major}.{minor} - {git_version}." |
| 27 |
|
| 28 |
|
| 29 |
class Job(metaclass=ABCMeta): |
| 30 |
""" |
| 31 |
Abstract Base Class for all the Backend Jobs |
| 32 |
""" |
| 33 |
|
| 34 |
def __init__(self, plugin, io, params): |
| 35 |
self._plugin = plugin |
| 36 |
self._io = io |
| 37 |
self._params = params |
| 38 |
|
| 39 |
@abstractmethod |
| 40 |
def execute(self): |
| 41 |
""" |
| 42 |
Executes the Job |
| 43 |
""" |
| 44 |
raise NotImplementedError |
| 45 |
|
| 46 |
@abstractmethod |
| 47 |
def execution_loop(self): |
| 48 |
""" |
| 49 |
Execution loop subroutine |
| 50 |
""" |
| 51 |
raise NotImplementedError |
| 52 |
|
| 53 |
def _start(self, expected_start_packet): |
| 54 |
self._read_start(expected_start_packet, onError=self._abort) |
| 55 |
if self._params.get('level', 'F') != 'F': |
| 56 |
logging.error("Unsupported backup level. Doing FULL backup.") |
| 57 |
self._io.send_warning("Unsupported backup level. Doing FULL backup.") |
| 58 |
self._connect() |
| 59 |
self._io.send_eod() |
| 60 |
|
| 61 |
def _read_start(self, start_packet, onError): |
| 62 |
_, packet = self._io.read_packet() |
| 63 |
if packet != start_packet: |
| 64 |
self._io.send_abort(INVALID_START_TEMPLATE.format(start_packet)) |
| 65 |
onError() |
| 66 |
|
| 67 |
def _connect(self): |
| 68 |
response = self._plugin.connect() |
| 69 |
|
| 70 |
if 'error' in response: |
| 71 |
logging.debug("response data:{}".format(response)) |
| 72 |
if 'error_code' in response: |
| 73 |
self._io.send_connection_error(response['error_code']) |
| 74 |
else: |
| 75 |
self._io.send_connection_error(0, strerror=response['error']) |
| 76 |
# Reads termination packet |
| 77 |
packet_header = self._io.read_line() |
| 78 |
Log.save_received_termination(packet_header) |
| 79 |
sys.exit(0) |
| 80 |
else: |
| 81 |
if self._params.get("type", None) == 'b': |
| 82 |
# display some info to user |
| 83 |
data = response.get('response') |
| 84 |
if data is not None: |
| 85 |
self._io.send_info(KUBERNETES_CODE_INFO.format( |
| 86 |
major=data.major, |
| 87 |
minor=data.minor, |
| 88 |
git_version=data.git_version, |
| 89 |
)) |
| 90 |
|
| 91 |
def _handle_non_fatal_error(self, error_message): |
| 92 |
if self._params.get("abort_on_error", None) == "1": |
| 93 |
self._io.send_abort(error_message) |
| 94 |
self._abort() |
| 95 |
else: |
| 96 |
self._io.send_non_fatal_error(error_message) |
| 97 |
|
| 98 |
def _handle_error(self, error_message): |
| 99 |
if self._params.get("abort_on_error", None) == "1": |
| 100 |
self._io.send_abort(error_message) |
| 101 |
self._abort() |
| 102 |
else: |
| 103 |
self._io.send_error(error_message) |
| 104 |
|
| 105 |
def _abort(self): |
| 106 |
self._plugin.disconnect() |
| 107 |
sys.exit(0) |