Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/io/services/plugin_params_io.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
18 from baculak8s.io.default_io import DefaultIO
19 from baculak8s.io.log import Log
20 from baculak8s.io.packet_definitions import EOD_PACKET
21
22 PLUGIN_PARAMETERS_START = "Params"
23 INVALID_PLUGIN_PARAMETERS_START = "Invalid Plugin Parameters Start Packet"
24 INVALID_PLUGIN_PARAMETERS_BLOCK = "Invalid Plugin Parameters Block"
25 URL_NOT_FOUND = "Parameter URL not found on Plugin Parameters"
26 USER_NOT_FOUND = "Parameter User not found on Plugin Parameters"
27 PWD_NOT_FOUND = "Parameter Password not found on Plugin Parameters"
28 PASSFILE_NOT_FOUND = "Passfile not found. ERR=No such file or directory"
29 PWD_INSIDE_PASSFILE_NOT_FOUND = "Password inside passfile not found"
30 RESTORE_LOCAL_WITHOUT_WHERE = "Restore Local plugin parameter without Where Plugin Parameter"
31
32
33 class PluginParamsIO(DefaultIO):
34
35 def read_plugin_params(self):
36 """
37 Reads blocks of parameters:
38
39 "C000111\n" (Command packet header)
40 key1=value1\n (Parameter)
41 "C000222\n"
42 key2=value2\n
43 "C000333\n"
44 key3=value3\n
45 ...
46
47 until an EOD packet (F000000) is found
48
49 :return: A dictionary containing the parameters
50
51 """
52
53 block = {
54 "includes": [],
55 "regex_includes": [],
56 "excludes": [],
57 "regex_excludes": [],
58 "namespace": [],
59 "persistentvolume": [],
60 "storageclass": [],
61 }
62 while True:
63 packet_header = self.read_line()
64 if not packet_header:
65 raise ValueError("Packet Header not found")
66
67 if packet_header == EOD_PACKET:
68 Log.save_received_eod(packet_header)
69 return block
70 else:
71 packet_content = self.read_line().decode()
72 param = packet_content.split("=", 1)
73
74 # converts key to lowercase
75 param[0] = param[0].lower()
76
77 # handle single word param without equal sign
78 if len(param) < 2:
79 param.append(True)
80
81 # Allow simple quote to identify string parameters.
82 if isinstance(param[1], str):
83 if param[1][0] == "'":
84 param[1] = ''.join(param[1].split("'"))
85
86 # handle array parameters automatically
87 if param[0] in block.keys():
88 if isinstance(block[param[0]], list):
89 block[param[0]].append(param[1])
90 else:
91 _b = block[param[0]]
92 block[param[0]] = [_b, param[1]]
93 else:
94 block[param[0]] = param[1]
95
96 Log.save_received_packet(packet_header, packet_content)