Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/services/plugin_params_service.py

raw
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 os
19
20 from baculak8s.io.services.plugin_params_io import PluginParamsIO, INVALID_PLUGIN_PARAMETERS_BLOCK, URL_NOT_FOUND, \
21 USER_NOT_FOUND, PWD_NOT_FOUND, PASSFILE_NOT_FOUND, PWD_INSIDE_PASSFILE_NOT_FOUND, RESTORE_LOCAL_WITHOUT_WHERE, \
22 PLUGIN_PARAMETERS_START, INVALID_PLUGIN_PARAMETERS_START
23 from baculak8s.services.service import Service
24
25
26 class PluginParamsService(Service):
27 """
28 Service that contains the business logic
29 related to reading and parsing the information about the Plugin
30 that will be used by the Job to fulfill it's purpose
31 """
32
33 def __init__(self, job_info):
34 self.io = PluginParamsIO()
35 self.job_info = job_info
36
37 def execute(self, params=None):
38 self.__read_start()
39 params_block = self.__read_params_block()
40 self.io.send_eod()
41 return params_block
42
43 def __read_start(self):
44 _, packet = self.io.read_packet()
45
46 if packet != PLUGIN_PARAMETERS_START:
47 self.io.send_abort(INVALID_PLUGIN_PARAMETERS_START)
48 sys.exit(0)
49
50 def __read_params_block(self):
51 params_block = self.io.read_plugin_params()
52
53 # Parameters validation
54 if not params_block:
55 self.io.send_abort(INVALID_PLUGIN_PARAMETERS_BLOCK)
56 sys.exit(0)
57
58 if "restore_local" in params_block and ("where" not in self.job_info or self.job_info["where"] == ""):
59 self.io.send_abort(RESTORE_LOCAL_WITHOUT_WHERE)
60 sys.exit(0)
61
62 if 'pv' in params_block:
63 # 'pv' is an alias for 'persistentvolume'
64 params_block.get('persistentvolume').append(params_block.get('pv'))
65 del params_block['pv']
66
67 if 'ns' in params_block:
68 # 'ns' is an alias for 'namespace'
69 params_block.get('namespace').append(params_block.get('ns'))
70 del params_block['ns']
71
72 return params_block
73
74 def __get_password(self, params_block):
75 """
76 Reads the password from the passfile and puts it into the Plugin Params
77 """
78 if not os.path.isfile(params_block["passfile"]):
79 self.io.send_abort(PASSFILE_NOT_FOUND)
80 sys.exit(0)
81
82 with open(params_block["passfile"], "r") as f:
83 params_block["password"] = f.readline()
84 f.close()
85
86 if not params_block["password"]:
87 self.io.send_abort(PWD_INSIDE_PASSFILE_NOT_FOUND)
88 sys.exit(0)
89
90 return params_block