| 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 |
import sys |
| 18 |
import logging |
| 19 |
import baculak8s |
| 20 |
from baculak8s.io.services.job_info_io import JobInfoIO, INVALID_JOB_PARAMETER_BLOCK, INVALID_JOB_TYPE, \ |
| 21 |
JOB_NAME_NOT_FOUND, JOB_ID_NOT_FOUND, JOB_TYPE_NOT_FOUND, INVALID_REPLACE_PARAM, JOB_START_PACKET, \ |
| 22 |
INVALID_JOB_START_PACKET |
| 23 |
from baculak8s.services.service import Service |
| 24 |
|
| 25 |
TYPE_BACKUP = "b" |
| 26 |
TYPE_RESTORE = "r" |
| 27 |
TYPE_ESTIMATION = "e" |
| 28 |
|
| 29 |
REPLACE_ALWAYS = 'a' |
| 30 |
REPLACE_IFNEWER = 'w' |
| 31 |
REPLACE_NEVER = 'n' |
| 32 |
REPLACE_IFOLDER = 'o' |
| 33 |
|
| 34 |
|
| 35 |
class JobInfoService(Service): |
| 36 |
""" |
| 37 |
Service that contains the business logic |
| 38 |
related to reading and parsing the information about the Job |
| 39 |
that should be created and executed by the Backend. |
| 40 |
""" |
| 41 |
|
| 42 |
def __init__(self): |
| 43 |
self.io = JobInfoIO() |
| 44 |
|
| 45 |
def execute(self, params=None): |
| 46 |
self.__read_start() |
| 47 |
params_block = self.__read_params_block() |
| 48 |
self.io.send_eod() |
| 49 |
return params_block |
| 50 |
|
| 51 |
def __read_start(self): |
| 52 |
_, packet = self.io.read_packet() |
| 53 |
|
| 54 |
if packet != JOB_START_PACKET: |
| 55 |
self.io.send_abort(INVALID_JOB_START_PACKET) |
| 56 |
sys.exit(0) |
| 57 |
|
| 58 |
def __read_params_block(self): |
| 59 |
job_info = self.io.read_job_info() |
| 60 |
|
| 61 |
# Parameters validation |
| 62 |
if not job_info: |
| 63 |
self.io.send_abort(INVALID_JOB_PARAMETER_BLOCK) |
| 64 |
sys.exit(0) |
| 65 |
if "name" not in job_info: |
| 66 |
self.io.send_abort(JOB_NAME_NOT_FOUND) |
| 67 |
sys.exit(0) |
| 68 |
if "jobid" not in job_info: |
| 69 |
self.io.send_abort(JOB_ID_NOT_FOUND) |
| 70 |
sys.exit(0) |
| 71 |
if "replace" in job_info: |
| 72 |
job_info["replace"] = job_info["replace"].lower() |
| 73 |
if job_info["replace"] not in [REPLACE_ALWAYS, REPLACE_IFNEWER, REPLACE_NEVER, REPLACE_IFOLDER]: |
| 74 |
self.io.send_abort(INVALID_REPLACE_PARAM) |
| 75 |
sys.exit(0) |
| 76 |
if "namespace" in job_info: |
| 77 |
logging.debug("FILE NAMESPACE: {}".format(job_info.get("namespace"))) |
| 78 |
baculak8s.plugins.k8sbackend.k8sfileinfo.defaultk8spath = job_info.get("namespace") |
| 79 |
job_info.pop("namespace") |
| 80 |
if "type" not in job_info: |
| 81 |
self.io.send_abort(JOB_TYPE_NOT_FOUND) |
| 82 |
sys.exit(0) |
| 83 |
else: |
| 84 |
job_info["type"] = job_info["type"].lower() |
| 85 |
if job_info["type"] not in [TYPE_BACKUP, TYPE_RESTORE, TYPE_ESTIMATION]: |
| 86 |
self.io.send_abort(INVALID_JOB_TYPE) |
| 87 |
sys.exit(0) |
| 88 |
|
| 89 |
return job_info |