forked from kuhn-ruess/Checkmk-Checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkp_packer
executable file
·138 lines (118 loc) · 4.39 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
#!/usr/bin/env python3
# +-----------------------------------------------------------------+
# | |
# | ( ___ \ | \ /\|\ /||\ /|( ( /| |
# | | ( ) ) | \ / /| ) ( || ) ( || \ ( | |
# | | (__/ / | (_/ / | | | || (___) || \ | | |
# | | __ ( | _ ( | | | || ___ || (\ \) | |
# | | ( \ \ | ( \ \ | | | || ( ) || | \ | |
# | | )___) )_ | / \ \| (___) || ) ( || ) \ | |
# | |/ \___/(_) |_/ \/(_______)|/ \||/ )_) |
# | |
# | Copyright Bastian Kuhn 2018 [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.
"""
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
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)")
#print(" - unpack (Get MKP Source")
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 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):
"""
Pack a MKP File
"""
path = package_name+"/src"
#Create info:
info_dict = eval(open(path+"/info").read())
info_json = json.dumps(info_dict).encode('utf-8')
special_dirs = {
'agent_based': 'base/plugins/agent_based',
}
#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)
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(NAME)
elif OPERATION == "unpack":
print("NOT YET IMPLEMENTED")
else:
usage()
sys.exit(1)