Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/io/log.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 import logging
19 import os
20 import baculak8s
21
22 PLUGIN_WORKING = os.getenv("PLUGIN_WORKING", "/opt/bacula/working/kubernetes")
23 PRE_JOB_LOG_NAME_TEMPLATE = "pre_job_{}.log"
24 LOG_NAME_TEMPLATE = "{pid}_{job_id}_{job_name}.log"
25
26
27 class LogConfig(object):
28 """
29 Class used in order to configure the execution Debug Log File
30 It determines whether the Debug Log File should be created, and
31 where it should be created
32 """
33
34 @staticmethod
35 def start():
36 # The Log File should be at the PLUGIN_WORKING path
37 if not os.path.exists(PLUGIN_WORKING):
38 try:
39 os.makedirs(PLUGIN_WORKING)
40 except:
41 # fallback to /tmp
42 baculak8s.io.log.PLUGIN_WORKING = '/tmp/backendplugin'
43 if not os.path.exists(PLUGIN_WORKING):
44 os.makedirs(PLUGIN_WORKING)
45
46 # The Log File starts with a "Pre Job" name
47 file_name = PRE_JOB_LOG_NAME_TEMPLATE.format(os.getpid())
48 file_name = os.path.join(PLUGIN_WORKING, file_name)
49 logging.basicConfig(filename=file_name, level=logging.DEBUG, filemode='w+', format='%(levelname)s:[%(pathname)s:%(lineno)d in %(funcName)s] %(message)s')
50
51 @staticmethod
52 def handle_params(job_info, plugin_params):
53 if "debug" in plugin_params and plugin_params["debug"]:
54 LogConfig._create(job_info)
55 Log.debug_level = int(plugin_params["debug"])
56 else:
57 LogConfig._delete_pre_job_log()
58
59 @staticmethod
60 def _create(job_info):
61 pid = os.getpid()
62 old_name = PRE_JOB_LOG_NAME_TEMPLATE.format(pid)
63 new_name = LOG_NAME_TEMPLATE.format(pid=pid, job_id=job_info["jobid"], job_name=job_info["name"])
64 old_name = os.path.join(PLUGIN_WORKING, old_name)
65 new_name = os.path.join(PLUGIN_WORKING, new_name)
66 if os.path.isfile(old_name):
67 os.rename(old_name, new_name)
68
69 @staticmethod
70 def _delete_pre_job_log():
71 pid = os.getpid()
72 pre_job_log_file = PRE_JOB_LOG_NAME_TEMPLATE.format(pid)
73 pre_job_log_file = os.path.join(PLUGIN_WORKING, pre_job_log_file)
74 if os.path.isfile(pre_job_log_file):
75 os.remove(pre_job_log_file)
76
77
78 class Log:
79 """
80 Class with helper methods to send data to the Debug Log
81 """
82
83 debug_level = 0
84
85 @staticmethod
86 def save_received_termination(packet_header):
87 Log.save_received_packet(packet_header, "(TERMINATION PACKET)")
88
89 @staticmethod
90 def save_received_eod(packet_header):
91 Log.save_received_packet(packet_header, "(EOD PACKET)")
92
93 @staticmethod
94 def save_received_data(packet_header):
95 Log.save_received_packet(packet_header, "(DATA PACKET)")
96
97 @staticmethod
98 def save_received_packet(packet_header, packet_content):
99 if Log.debug_level <= 1:
100 return
101 if Log.debug_level == 2 and packet_header.decode()[0] == 'D':
102 return
103 message = "Received Packet\n{}\n{}\n".format(packet_header.decode(), packet_content)
104 logging.debug(message)
105
106 @staticmethod
107 def save_sent_eod(packet_header):
108 Log.save_sent_packet(packet_header, "(EOD PACKET)\n")
109
110 @staticmethod
111 def save_sent_data(packet_header):
112 Log.save_sent_packet(packet_header, "(DATA PACKET)\n")
113
114 @staticmethod
115 def save_sent_packet(packet_header, packet_content):
116 if Log.debug_level <= 1:
117 return
118 if Log.debug_level == 2 and packet_header[0] == 'D':
119 return
120 message = "Sent Packet\n{}{}".format(packet_header, packet_content)
121 logging.debug(message)
122
123 @staticmethod
124 def save_exit_code(exit_code):
125 message = "Backend finished with Exit Code: {}".format(exit_code)
126 logging.debug(message)
127
128 @staticmethod
129 def save_exception(e):
130 logging.exception(e)