Skip to content

Commit 17c4b16

Browse files
author
gitlab
committed
Merge branch 'fix-50588' into '4.6.11'
<improvement>[zstackbuild]: add agent_version file generator See merge request zstackio/zstack-utility!2648
2 parents 346532b + 3693af5 commit 17c4b16

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

zstackbuild/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ zstacknetwork.source=${zstack_build_root}/zstack-network
111111
zsnansibleplaybook.source=${zstack_build_root}/zstack-utility/zstacknetwork
112112

113113
zstackzwatch.source=${zstack_build_root}/zstack-zwatch
114-
checkmd5.bin.script=${zstack_build_root}/zstack-utility/zstackbuild/scripts/check_md5.sh
114+
agent.version.generator=${zstack_build_root}/zstack-utility/zstackbuild/scripts/agnet_version_generator_py2.py
115115

116116
zstacksharedblock.source=${zstack_build_root}/zstack-sharedblock
117117
zsblkansibleplaybook.source=${zstack_build_root}/zstack-utility/zstacksharedblock

zstackbuild/projects/zstack-zwatch.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
<include name="**" />
3232
</fileset>
3333
</copy>
34-
<exec executable="/bin/bash" dir="${zsw.ansible.dir}" failonerror="true">
35-
<arg value="${checkmd5.bin.script}" />
34+
<exec executable="/bin/python" dir="${zsw.ansible.dir}" failonerror="true">
35+
<arg value="${agent.version.generator}" />
36+
<arg value="-s ${zstackzwatch.source}" />
37+
<arg value="-o agent_version" />
3638
</exec>
3739
</target>
3840
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
# for python 2.x
4+
5+
from argparse import ArgumentParser, RawTextHelpFormatter
6+
7+
import os
8+
import hashlib
9+
10+
class AgentVersionItem(object):
11+
def __init__(self, name):
12+
self.name = str(name)
13+
self.md5 = ''
14+
self.filepath = ''
15+
16+
@property
17+
def compatible_text(self):
18+
return 'compatible-version-of-zwatch-vm-agent.%s=4.2.0' % self.name
19+
20+
@property
21+
def md5_text(self):
22+
return 'md5-zwatch-vm-agent.%s=%s' % (self.name, self.md5)
23+
24+
def with_filepath(self, filepath):
25+
self.filepath = filepath
26+
return self
27+
28+
def calculate_md5(self):
29+
if not os.path.exists(self.filepath):
30+
return
31+
with open(self.filepath, 'r') as f:
32+
# file size < 15M
33+
m = hashlib.md5(f.read())
34+
self.md5 = m.hexdigest()
35+
36+
def is_valid(self):
37+
return bool(self.md5)
38+
39+
class AgentVersionBuilder(object):
40+
def __init__(self, sourcepath, outputpath):
41+
self.sourcepath = str(sourcepath).strip()
42+
self.outputpath = str(outputpath).strip()
43+
self.items = [
44+
AgentVersionItem('linux-amd64').with_filepath('zwatch-vm-agent'),
45+
AgentVersionItem('linux-aarch64').with_filepath('zwatch-vm-agent_aarch64'),
46+
AgentVersionItem('linux-mips64el').with_filepath('zwatch-vm-agent_mips64el'),
47+
AgentVersionItem('linux-loongarch64').with_filepath('zwatch-vm-agent_loongarch64'),
48+
AgentVersionItem('freebsd-amd64').with_filepath('zwatch-vm-agent-freebsd')
49+
]
50+
51+
def _read_version_from_source(self):
52+
version_line = ''
53+
path = os.path.join(self.sourcepath, 'src', 'zwatch-vm-agent', 'utils', 'config.go')
54+
with open(path, 'r') as f:
55+
for l in f.readlines():
56+
index = l.find('VERSION')
57+
if index == -1 or l.find('=', index + len('VERSION')) == -1:
58+
continue
59+
version_line = l
60+
break
61+
62+
if version_line == '':
63+
raise Exception('failed to find version in file %s' % path)
64+
text = version_line.split()[-1]
65+
if text.startswith('"'):
66+
text = text[1:-1]
67+
return text
68+
69+
def write(self):
70+
lines_for_compatible = ''
71+
lines_for_md5 = ''
72+
73+
for item in self.items:
74+
item.calculate_md5()
75+
if not item.is_valid():
76+
continue
77+
lines_for_compatible += ('# %s\n' % item.compatible_text) # must start with '#'
78+
lines_for_md5 += ('%s\n' % item.md5_text)
79+
80+
with open(self.outputpath, 'w') as f:
81+
f.write('''zwatch-vm-agent=%s
82+
83+
# This agent version text file is generated by agent_version_generator_py2 script,
84+
# and the comments below are due to compatibility (version <= 4.2).
85+
# Please DO NOT DELETE or MODIFY them!
86+
# See more in ZSTAC-41487
87+
#
88+
%s
89+
%s''' % (self._read_version_from_source(), lines_for_compatible, lines_for_md5))
90+
91+
if __name__ == '__main__':
92+
parser = ArgumentParser("agent_version_generator_py2", description=__doc__, formatter_class=RawTextHelpFormatter)
93+
parser.add_argument('-s', '--sourcepath', required=True, default="", help='the source path for zstack-zwatch project')
94+
parser.add_argument('-o', '--outputpath', required=True, default="", help='the agent version file path for output')
95+
args = parser.parse_args()
96+
97+
builder = AgentVersionBuilder(args.sourcepath, args.outputpath)
98+
builder.write()

zstackbuild/scripts/check_md5.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)