-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.py
129 lines (109 loc) · 3.86 KB
/
prepare.py
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
#!/usr/bin/env python
"""Prepare setup files for Splunk Enterprise installation and save them into splunk-setup-all.tar.gz"""
import tarfile
from os import chdir, path
from pathlib import Path, PurePath
from re import search
from tempfile import TemporaryDirectory
from build import main as build_app
chdir_path = path.dirname(__file__)
chdir(chdir_path)
def exclusion(tarinfo: tarfile.TarInfo) -> tarfile.TarInfo | None:
"""Exclude self"""
# exclude certain folders/files
pathname = tarinfo.name
if search(
r"/\.|\\\.|__pycache__|pyproject\.toml|requirements|prepare\.py|splunk-setup-all\.tar\.gz",
pathname,
):
return None
# reset file stats
# based on https://splunkbase.splunk.com/app/833
tarinfo.uid = 1001
tarinfo.gid = 123
tarinfo.uname = tarinfo.gname = ""
if tarinfo.isfile():
# remove execution permission
tarinfo.mode = 0o644
# except for shell scripts
if tarinfo.name.endswith(".sh"):
tarinfo.mode = 0o744
if tarinfo.isdir():
# remove write permission from group & world
tarinfo.mode = 0o755
return tarinfo
def glob(pattern: str, out_path: str = "") -> Path | str:
filelist = list(Path(".").glob(pattern))
pattern_ext = PurePath(pattern).suffix
if len(filelist) >= 1:
return filelist[0]
if path.isfile(out_path) and (
len(pattern_ext) <= 0
or (len(pattern_ext) >= 1 and PurePath(out_path).suffix == pattern_ext)
):
return out_path
out_path = path.abspath(Path(input(f"Path to {pattern}: ")))
if not path.isfile(out_path):
print(f'"{out_path}" is not a file or does not exist.')
elif len(pattern_ext) >= 1 and PurePath(out_path).suffix != pattern_ext:
print(f'"{out_path}" is not a *{pattern_ext} file.')
return glob(pattern, out_path)
enterprise_gz = glob("splunk-*-Linux-x86_64.tgz")
output_gz = path.join(path.dirname(enterprise_gz), "splunk-setup-all.tar.gz")
host_key = glob("splunk_host_ed25519_key")
splunk_cert_key = glob("splunk-cert.key", "certs")
splunk_cert_pem = glob("splunk-cert.pem", "certs")
splunk_cert_web_pem = glob("splunk-cert-web.pem", "certs")
deployment_apps = ["1-deploymentserver", "1-indexserver", "100_splunkcloud"]
with TemporaryDirectory() as tmpdir:
built_apps = {}
for app in deployment_apps:
app_path = path.join("..", "deployment-apps", app)
if path.isdir(app_path):
built_apps.add(
build_app(
directory=app_path,
output=tmpdir,
)
)
with tarfile.open(output_gz, "w:gz") as tar:
tar.add(".", filter=exclusion)
tar.add(
path.join("..", "enterprise_root_cacert.crt"),
arcname="./enterprise_root_cacert.crt",
filter=exclusion,
)
tar.add(
path.join("..", "enterprise_intermediate_cacert.crt"),
arcname="./enterprise_intermediate_cacert.crt",
filter=exclusion,
)
tar.add(
path.join(
"..",
"deployment-apps",
"100_splunkcloud",
"default",
"100_splunkcloud_root_cacert.crt",
),
arcname="./100_splunkcloud_root_cacert.crt",
filter=exclusion,
)
tar.add(
path.join(
"..",
"deployment-apps",
"100_splunkcloud",
"default",
"100_splunkcloud_intermediate_cacert.crt",
),
arcname="./100_splunkcloud_intermediate_cacert.crt",
filter=exclusion,
)
for built_app in built_apps:
tar.add(
built_app,
arcname=path.basename(built_app),
filter=exclusion,
)
print(f'Created "{output_gz}"')