Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/io/services/job_info_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 import datetime
18
19 from baculak8s.io.default_io import DefaultIO
20 from baculak8s.io.log import Log
21 from baculak8s.io.packet_definitions import EOD_PACKET
22
23 JOB_START_PACKET = "Job"
24 INVALID_JOB_START_PACKET = "Invalid Job Start Packet"
25 INVALID_JOB_PARAMETER_BLOCK = "Invalid Job Parameter Block"
26 INVALID_JOB_TYPE = "Invalid Job Type. The supported types are:" \
27 "B - Backup, R - Restore or E - Estimation"
28 INVALID_REPLACE_PARAM = "Invalid Replace Parameter. The supported values are:" \
29 "a - Replace always, w - Replace if newer, n - Never replace" \
30 "or o - Replace if older"
31 JOB_NAME_NOT_FOUND = "Parameter Job Name Not Found"
32 JOB_ID_NOT_FOUND = "Parameter Job ID Not Found"
33 JOB_TYPE_NOT_FOUND = "Parameter Job Type Not Found"
34
35
36 class JobInfoIO(DefaultIO):
37
38 def read_job_info(self):
39 """
40 Reads blocks of parameters:
41
42 "C000111\n" (Command packet header)
43 key1=value1\n (Parameter)
44 "C000222\n"
45 key2=value2\n
46 "C000333\n"
47 key3=value3\n
48 ...
49
50 until an EOD packet (F000000) is found
51
52 :return: A dictionary containing the parameters
53
54 """
55
56 block = {}
57 while True:
58 packet_header = self.read_line()
59 if not packet_header:
60 raise ValueError("Packet Header not found")
61
62 if packet_header == EOD_PACKET:
63 Log.save_received_eod(packet_header)
64 return block
65 else:
66 packet_content = self.read_line().decode()
67 param = packet_content.split("=", 1)
68 # converts key to lowercase
69 param[0] = param[0].lower()
70
71 if param[0] == "since":
72 # converts the provided timestamp to a utc timestamp
73 parsed_param = int(param[1])
74 parsed_param = int(datetime.datetime \
75 .utcfromtimestamp(parsed_param) \
76 .replace(tzinfo=datetime.timezone.utc) \
77 .timestamp())
78 block[param[0]] = parsed_param
79 else:
80 block[param[0]] = param[1]
81
82 Log.save_received_packet(packet_header, packet_content)