Skip to content

Commit ebd03e7

Browse files
committed
<fix>[baremetal_v2_gateway_agent]: improve local deploy and bm2-agent
1. replace smartctl to filter /dev/disk/by-id for getting diskinfo 2. pass back ks err msg to gateway 3. improve bm2-instance-agent build scripts Resolves: ZSTAC-45956 Change-Id: I6962787467706d767378796e666f636577677963
1 parent 26a039a commit ebd03e7

File tree

11 files changed

+97
-64
lines changed

11 files changed

+97
-64
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '4.3.18'
1+
__version__ = "2.0.0"

bm-instance-agent/bm_instance_agent/api/utils.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from concurrent import futures
22
import json
33
import sys
4-
import time
54

65
from oslo_concurrency import processutils
76
from oslo_log import log as logging
@@ -11,6 +10,7 @@
1110

1211
from bm_instance_agent.conf import CONF
1312
from bm_instance_agent.objects import HeaderObj
13+
from bm_instance_agent.common.utils import transcantion
1414

1515

1616
THREAD_POOL = None
@@ -123,35 +123,3 @@ def format_exception():
123123
del exception_info
124124

125125
return data
126-
127-
128-
class transcantion(object):
129-
""" A tool class for retry
130-
"""
131-
132-
def __init__(self, retries, sleep_time=0):
133-
self.retries = retries
134-
self.sleep_time = sleep_time
135-
136-
def __enter__(self):
137-
return self
138-
139-
def __exit__(self, exc_type, exc_val, exc_tb):
140-
if exc_type is not None:
141-
return False
142-
return True
143-
144-
def execute(self, func, *args, **kwargs):
145-
err = None
146-
for i in range(self.retries):
147-
try:
148-
if i > 0:
149-
msg = 'Attempt rerun {name}: {i}'.format(
150-
name=func.__name__, i=i)
151-
LOG.warning(msg)
152-
return func(*args, **kwargs)
153-
except Exception as e:
154-
LOG.exception(e)
155-
err = e
156-
time.sleep(self.sleep_time)
157-
raise err

bm-instance-agent/bm_instance_agent/common/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,46 @@
77
from netaddr import IPAddress
88
import netifaces
99
from oslo_concurrency import processutils
10+
from oslo_log import log as logging
1011
import psutil
1112
import pyroute2
13+
import time
1214

1315
from bm_instance_agent import exception
1416

17+
LOG = logging.getLogger(__name__)
18+
19+
class transcantion(object):
20+
""" A tool class for retry
21+
"""
22+
23+
def __init__(self, retries, sleep_time=0):
24+
self.retries = retries
25+
self.sleep_time = sleep_time
26+
27+
def __enter__(self):
28+
return self
29+
30+
def __exit__(self, exc_type, exc_val, exc_tb):
31+
if exc_type is not None:
32+
return False
33+
return True
34+
35+
def execute(self, func, *args, **kwargs):
36+
err = None
37+
for i in range(self.retries):
38+
try:
39+
if i > 0:
40+
msg = 'Attempt rerun {name}: {i}'.format(
41+
name=func.__name__, i=i)
42+
LOG.warning(msg)
43+
return func(*args, **kwargs)
44+
except Exception as e:
45+
LOG.exception(e)
46+
err = e
47+
time.sleep(self.sleep_time)
48+
raise err
49+
1550

1651
class IpLink:
1752
def __init__(self, chunk):

bm-instance-agent/bm_instance_agent/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from oslo_log import log as logging
44
from stevedore import driver
55

6+
from __init__ import __version__
67
from bm_instance_agent.common import utils as bm_utils
78
from bm_instance_agent import exception
89
from bm_instance_agent.objects import BmInstanceObj
@@ -16,7 +17,6 @@
1617
BM_INSTANCE_UUID = None
1718
DRIVER = None
1819
ZWATCH_AGENT_CONF_PATH = "/usr/local/zstack/zwatch-vm-agent/conf.yaml"
19-
VERSION = '2.0.0'
2020

2121

2222
class AgentManager(object):
@@ -73,7 +73,7 @@ def ping(self, bm_instance):
7373
self.driver.ping(instance_obj)
7474
self.driver.discovery_target(instance_obj)
7575
self._check_gateway_ip(instance_obj)
76-
return {'version': VERSION, 'ping': {'bmInstanceUuid': BM_INSTANCE_UUID}}
76+
return {'version': __version__, 'ping': {'bmInstanceUuid': BM_INSTANCE_UUID}}
7777

7878
def reboot(self, bm_instance):
7979
instance_obj = BmInstanceObj.from_json(bm_instance)

bm-instance-agent/bm_instance_agent/systems/linux/driver.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,26 @@ def attach_volume(self, instance_obj, volume_obj):
9494
conf_path = '/etc/iscsi/initiatorname.iscsi'
9595
initiator = ('InitiatorName=iqn.2015-01.io.zstack:initiator.'
9696
'instance.{uuid}').format(uuid=instance_obj.uuid)
97-
with open(conf_path, 'w') as f:
98-
f.write(initiator)
9997

100-
cmd = 'systemctl daemon-reload && systemctl restart iscsid'
101-
processutils.execute(cmd, shell=True)
98+
update_conf = False
99+
if os.path.exists(conf_path):
100+
with open(conf_path, 'r') as f:
101+
if f.read().strip() != initiator:
102+
update_conf = True
103+
else:
104+
update_conf = True
105+
106+
if update_conf:
107+
with open(conf_path, 'w') as f:
108+
f.write(initiator)
109+
110+
cmd = 'systemctl daemon-reload && systemctl restart iscsid'
111+
processutils.execute(cmd, shell=True)
102112

103113
cmd = ['iscsiadm', '-m', 'session', '--rescan']
104-
processutils.execute(*cmd)
114+
# parameter[delay_on_retry] of func[processutils.execute] will not verify exit_code
115+
with bm_utils.transcantion(retries=5, sleep_time=10) as cursor:
116+
cursor.execute(processutils.execute, *cmd)
105117

106118
def detach_volume(self, instance_obj, volume_obj):
107119
""" Detach a given iSCSI lun

bm-instance-agent/tools/package.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

3+
version=`awk -F' = ' '{gsub(/"/,"",$2);print $2}' bm_instance_agent/__init__.py`
34
bin_name='bm-instance-agent.tar.gz'
5+
agent_name=zstack-bm-agent-`uname -m`-${version}.bin
46

57
# Get the shell scirpt's dir
68
shell_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
79
pushd ${shell_dir}
810

9-
# Get the git repo's root dir
10-
project_dir=`git rev-parse --show-toplevel`/bm-instance-agent
11-
pushd ${project_dir}
11+
pushd ${shell_dir}/../
1212

1313
temp=`mktemp -d`
1414
pex . -v \
@@ -28,7 +28,7 @@ popd
2828
popd
2929
popd
3030

31-
cat ./tools/install.sh ${temp}/${bin_name} > ./install.bin
32-
sed -i "s/MD5_SUM/${md5}/g" ./install.bin
33-
chmod +x ./install.bin
31+
cat ./tools/install.sh ${temp}/${bin_name} > ${agent_name}
32+
sed -i "s/MD5_SUM/${md5}/g" ${agent_name}
33+
chmod +x ${agent_name}
3434
rm -rf ${temp}
-950 KB
Binary file not shown.

kvmagent/kvmagent/plugins/baremetal_v2_gateway_agent.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BaremetalV2GatewayAgentPlugin(kvmagent.KvmAgent):
7272
DEFAULT_PXE_PATH = os.path.join(PXELINUX_CFG_DIR, 'default')
7373
GRUB_CFG_DIR = os.path.join(TFTPBOOT_DIR, 'EFI/centos/')
7474
GRUB_CFG_PATH = os.path.join(GRUB_CFG_DIR, 'grub.cfg')
75+
KS_ERR_LOG_PATH = os.path.join(TFTPBOOT_DIR, 'ks.errlogs/')
7576
X86_64_BOOTIMG_DIR = os.path.join(TFTPBOOT_DIR, 'x86_64')
7677
AARCH64_BOOTIMG_DIR = os.path.join(TFTPBOOT_DIR, 'aarch64')
7778

@@ -179,6 +180,9 @@ def _ensure_env(self, network_obj):
179180
if not os.path.exists(self.BM_IMGS_DIR):
180181
linux.mkdir(self.BM_IMGS_DIR)
181182

183+
if not os.path.exists(self.KS_ERR_LOG_PATH):
184+
linux.mkdir(self.KS_ERR_LOG_PATH)
185+
182186
# download pxe images from management node
183187
# static repo url like: http://10.10.0.1:8080/zstack/static/zstack-repo/x86_64/c76
184188
bmtempdir = tempfile.mkdtemp()
@@ -283,17 +287,16 @@ def _prepare_tftp(self):
283287
with open(self.MAPFILE_PATH, 'w') as f:
284288
f.write(map_file)
285289

286-
if not os.path.exists(self.TFTPD_SYSTEMD_SERVICE_PATH):
287-
template_systemd_service = self._load_template(
288-
'tftp_systemd_service')
289-
systemd_service = template_systemd_service.render(
290-
bm_gateway_tftpd_mapfile=self.MAPFILE_PATH,
291-
bm_gateway_tftpboot_dir=self.TFTPBOOT_DIR)
292-
with open(self.TFTPD_SYSTEMD_SERVICE_PATH, 'w') as f:
293-
f.write(systemd_service)
290+
template_systemd_service = self._load_template(
291+
'tftp_systemd_service')
292+
systemd_service = template_systemd_service.render(
293+
bm_gateway_tftpd_mapfile=self.MAPFILE_PATH,
294+
bm_gateway_tftpboot_dir=self.TFTPBOOT_DIR)
295+
with open(self.TFTPD_SYSTEMD_SERVICE_PATH, 'w') as f:
296+
f.write(systemd_service)
294297

295298
cmd = ('systemctl daemon-reload && '
296-
'systemctl start zstack-baremetal-tftpd')
299+
'systemctl restart zstack-baremetal-tftpd')
297300
shell.call(cmd)
298301

299302
def _destroy_tftp(self):
@@ -916,7 +919,8 @@ def _create_import_data_config(self, volume_driver, cmd):
916919
chassis_address=cmd.chassisInfo.address,
917920
chassis_port=cmd.chassisInfo.port,
918921
api_id=cmd.threadContext.api,
919-
task_name=cmd.threadContext["task-name"]
922+
task_name=cmd.threadContext["task-name"],
923+
provision_mac=instance_obj.provision_mac
920924
)
921925

922926
with open(ks_config_path, 'w') as f:

kvmagent/kvmagent/plugins/bmv2_gateway_agent/templates/import-data.ks.j2

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import multiprocessing
1919
import os
2020
import sys
2121
import time
22+
import traceback
23+
24+
provision_mac = '{{provision_mac}}'
2225

2326
def shell_cmd(cmd, exception=True, workdir=None):
2427
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
@@ -63,7 +66,7 @@ def get_src_dev():
6366
time.sleep(1)
6467
cmd = "iscsiadm --mode node --targetname {{iqn_name}} -p {{gateway_ip}}:3260 --login"
6568
shell_cmd(cmd)
66-
time.sleep(1)
69+
time.sleep(3)
6770
cmd = "systemctl daemon-reload && systemctl restart iscsid"
6871
shell_cmd(cmd)
6972
time.sleep(1)
@@ -145,9 +148,13 @@ def main():
145148

146149
try:
147150
main()
148-
except Exception as e:
149-
shell_cmd("touch /root/ks_error_log")
150-
shell_cmd("echo %s >> /root/ks_error_log" % str(e))
151+
except:
152+
# pass back the ks err to tftp server on gateway
153+
err_file_name = '/root/ks.{}'.format(provision_mac.replace(':', '-'))
154+
traceback.print_exc(file=open(err_file_name,'w+'))
155+
shell_cmd("curl -T {} tftp://{}/ks.errlogs/".format(err_file_name, "{{gateway_ip}}"))
156+
157+
# report fail and update ipmi info
151158
convert_info = {}
152159
convert_info['status'] = 'ConvertFailed'
153160
convert_info['progress'] = 0

kvmagent/kvmagent/plugins/bmv2_gateway_agent/templates/inspector.ks.j2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import multiprocessing
1919
import os
2020
import sys
2121
import platform
22+
import traceback
2223

2324
PROVISION_NET = "{{ provision_net }}"
25+
pxe_iface_mac = ''
2426

2527
units_mapping = {
2628
'kb': 1024,
@@ -123,7 +125,6 @@ def get_nic_info():
123125
# Get the pxe interface from /proc/cmdline
124126
# NOTE: Need to point that the mac addr should start with '01', because
125127
# the arp type of ethernet is 1.
126-
pxe_iface_mac = ''
127128
with open('/proc/cmdline', 'r') as f:
128129
for param in f.read().strip().split():
129130
if 'BOOTIF' in param:
@@ -247,7 +248,13 @@ def main():
247248
shell_cmd("poweroff")
248249

249250

250-
main()
251+
try:
252+
main()
253+
except:
254+
# pass back the ks err to tftp server on gateway
255+
err_file_name = '/root/ks.{}'.format(pxe_iface_mac.replace(':','-'))
256+
traceback.print_exc(file=open(err_file_name,'w+'))
257+
shell_cmd("curl -T {} tftp://{}/ks.errlogs/".format(err_file_name, PROVISION_NET))
251258

252259

253260
%end

0 commit comments

Comments
 (0)