-
Notifications
You must be signed in to change notification settings - Fork 23
/
mkp_packer
executable file
·231 lines (197 loc) · 7.31 KB
/
mkp_packer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
# +-----------------------------------------------------------------+
# | |
# | ( ___ \ | \ /\|\ /||\ /|( ( /| |
# | | ( ) ) | \ / /| ) ( || ) ( || \ ( | |
# | | (__/ / | (_/ / | | | || (___) || \ | | |
# | | __ ( | _ ( | | | || ___ || (\ \) | |
# | | ( \ \ | ( \ \ | | | || ( ) || | \ | |
# | | )___) )_ | / \ \| (___) || ) ( || ) \ | |
# | |/ \___/(_) |_/ \/(_______)|/ \||/ )_) |
# | |
# | Copyright Bastian Kuhn 2024 [email protected] |
# +-----------------------------------------------------------------+
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# pylint: disable=consider-using-with
"""
Simple Packer and (later) unpacker for MKP Files
MKP is the Package format for Check_MK
"""
import sys
import os
import io
import json
import tarfile
import ast
import shutil
from datetime import datetime
def usage():
"""
Print Usage and Exit
"""
print("Pack and unpack Check_MK MKP Files")
print("./mkp_packer OPERATION PACKAGENAME")
print()
print("OPERATIONS:")
print(" - pack (Create MKP File in cmk 2.3 Style)")
print(" - pack_legacy (Create MKP File in cmk 2.2 Style)")
print(" - get_files (Get files from info file and move to repo")
print()
print("PACKAGENAME:")
print(" - e.g. namexy is ./namexy/src")
def get_tar_info(name, obj):
"""
Helper for Tarinfo Creation
"""
tar_info = tarfile.TarInfo(name)
tar_info.size = obj.getbuffer().nbytes
tar_info.mtime = datetime.now().timestamp()
tar_info.uid = 0
tar_info.gid = 0
tar_info.mode = 0o644
return tar_info
def _file_filter(tarinfo):
"""
Exclude Files from Tar
"""
excludes = ['__pycache__', '__init__']
for exclude in excludes:
if exclude in tarinfo.name:
return None
return tarinfo
def pack_suptar_23(package_name, sub_name, sub_path):
"""
Helper to create the suptar
"""
sub_filename = sub_name + ".tar"
sub_obj = io.BytesIO()
with tarfile.open(sub_filename, "w:", fileobj=sub_obj) as subtar:
subtar.add(f"{package_name}/src/{sub_path}", sub_path, filter=_file_filter)
sub_obj.seek(0)
tar_info = get_tar_info(sub_filename, sub_obj)
return sub_obj, tar_info
def pack_23(package_name):
"""
Pack a MKP File for Checkmk Version 2.3
"""
path = f"{package_name}/src/"
#Create info:
info_dict = ast.literal_eval(open(path+"/info").read())
info_json = json.dumps(info_dict).encode('utf-8')
#Pack Subfolders
mkp_filename = package_name+"/{name}-{version}.mkp".format(**info_dict)
dirs = {
'cmk_addons_plugins': f"{package_name}",
}
with tarfile.open(mkp_filename, "w:gz") as tar:
tar.add(path+'/info', 'info')
info_obj = io.BytesIO()
info_obj.write(info_json)
info_obj.seek(0)
tar_info = get_tar_info('info.json', info_obj)
tar.addfile(tar_info, info_obj)
# New part:
for folder_name, sub_path in dirs.items():
if (folder_name in info_dict['files'] and info_dict['files'][folder_name]):
sub_obj, tar_info = pack_suptar_23(package_name, folder_name, sub_path)
tar.addfile(tar_info, sub_obj)
def pack_suptar(package_name, sub_name, sub_path):
"""
Helper to create the suptar
"""
sub_filename = sub_name + ".tar"
sub_obj = io.BytesIO()
with tarfile.open(sub_filename, "w:", fileobj=sub_obj) as subtar:
subtar.add(package_name+"/src/"+sub_path, "")
sub_obj.seek(0)
tar_info = get_tar_info(sub_filename, sub_obj)
return sub_obj, tar_info
def pack(package_name):
"""
DEPRECATED: Pack a MKP File
"""
special_dirs = {
'agent_based': 'base/plugins/agent_based',
}
path = package_name+"/src"
#Create info:
info_dict = ast.literal_eval(open(path+"/info").read())
info_json = json.dumps(info_dict).encode('utf-8')
#Pack Subfolders
mkp_filename = package_name+"/{name}-{version}.mkp".format(**info_dict)
found_legacy_folders = []
with tarfile.open(mkp_filename, "w:gz") as tar:
tar.add(path+'/info', 'info')
info_obj = io.BytesIO()
info_obj.write(info_json)
info_obj.seek(0)
tar_info = get_tar_info('info.json', info_obj)
tar.addfile(tar_info, info_obj)
# With Checkmk 2.0 the structure changed.
# In order to stay compatible this packer first
# Handels the old way, and will be improved step by step
# for parts in the new way
for sub_path in os.listdir(path):
if sub_path in ['info', 'info.json', 'base']:
continue
sub_obj, tar_info = pack_suptar(package_name, sub_path, sub_path)
found_legacy_folders.append(sub_path)
tar.addfile(tar_info, sub_obj)
# New part:
for folder_name, path in special_dirs.items():
if (folder_name in info_dict['files'] and info_dict['files'][folder_name]) and folder_name not in found_legacy_folders:
sub_obj, tar_info = pack_suptar(package_name, folder_name, path)
tar.addfile(tar_info, sub_obj)
def _copy_category(cmk_type, info_dict, base_path, search_path):
"""
Copy File from Local to GIT
"""
for file in info_dict['files'][cmk_type]:
if special_dir := special_dirs.get(cmk_type):
src_file = f"{search_path}/lib/check_mk/{special_dir}/{file}"
else:
src_file = f"{search_path}/share/check_mk/{cmk_type}/{file}"
dest_file = f"{base_path}/{cmk_type}/{file}"
try:
os.makedirs((os.path.dirname((dest_file))))
except FileExistsError:
pass
shutil.copy(src_file, dest_file)
print(f"Copied {file}")
def get_files(package_name, search_path):
"""
Search files in packages infos file
in the given (local) path and copy them
into the local repo structure
"""
base_path = package_name+"/src"
info_dict = ast.literal_eval(open(base_path+"/info", encoding="utf-8").read())
for file_type in info_dict['files']:
_copy_category(file_type, info_dict, base_path, search_path)
if __name__ == "__main__":
if len(sys.argv) < 3:
usage()
sys.exit(1)
OPERATION = sys.argv[1].lower()
# beware of empty name, since we add /src as path
NAME = sys.argv[2].lower()
if OPERATION == "pack":
pack_23(NAME)
elif OPERATION == "pack_legacy":
pack(NAME)
elif OPERATION == "unpack":
print("NOT YET IMPLEMENTED")
elif OPERATION == "get_files":
get_files(NAME, sys.argv[3])
else:
usage()
sys.exit(1)