Skip to content

Commit 4c17901

Browse files
yejianquanmssonicbld
authored andcommitted
[Feat] Enhance upgrade image script to support chassis devices (sonic-net#12871)
Description of PR Summary: Enhance upgrade image script to support chassis device. For chassis device, we need to firstly upgrade the image for supervisor cards, then upgrade the image for line cards. Approach What is the motivation for this PR? Enhance the upgrade_image script to support chassis devices. How did you do it? Upgrade image on the supervisor cards, then wait 900s for the supervisor card to be ready. Upgrade image on the line cards, then wait 300s for the line cards to be ready. The sonichosts defautly run commands on all supervisor cards and line cards at the same time, enhance the framework to be able to upgrade specific hosts. How did you verify/test it? Run upgrade image script on the chassis device and pizzbox device, both of them works well co-authorized by: [email protected]
1 parent 78beab7 commit 4c17901

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

ansible/devutil/devices/ansible_hosts.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ def _run_ansible_module(self, *args, **kwargs):
701701
}
702702
"""
703703
caller_info = kwargs.pop("caller_info", None)
704+
target_hosts = kwargs.pop("target_hosts", None)
704705
if not caller_info:
705706
previous_frame = inspect.currentframe().f_back
706707
caller_info = inspect.getframeinfo(previous_frame)
@@ -723,7 +724,10 @@ def _run_ansible_module(self, *args, **kwargs):
723724
self._log_modules(caller_info, module_info, verbosity)
724725

725726
task = self.build_task(**module_info)
726-
results = self.run_tasks(self.host_pattern, self.loader, self.im, self.vm, self.options, tasks=[task])
727+
host_pattern = self.host_pattern
728+
if target_hosts:
729+
host_pattern = target_hosts
730+
results = self.run_tasks(host_pattern, self.loader, self.im, self.vm, self.options, tasks=[task])
727731

728732
self._log_results(caller_info, module_info, results, verbosity)
729733
self._check_results(caller_info, module_info, results, module_ignore_errors, verbosity)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import enum
2+
3+
4+
class ChassisCardType(str, enum.Enum):
5+
# Sample: lab-1111-sup-1
6+
SUPERVISOR_CARD = "-sup-"
7+
# Sample: lab-1111-lc1-1
8+
LINE_CARD = "-lc"
9+
10+
11+
def is_chassis(sonichosts):
12+
supervisor_card_exists, line_card_exists = False, False
13+
for hostname in sonichosts.hostnames:
14+
if ChassisCardType.SUPERVISOR_CARD.value in hostname:
15+
supervisor_card_exists = True
16+
if ChassisCardType.LINE_CARD.value in hostname:
17+
line_card_exists = True
18+
return supervisor_card_exists and line_card_exists
19+
20+
21+
def get_chassis_hostnames(sonichosts, chassis_card_type: ChassisCardType):
22+
res = []
23+
for hostname in sonichosts.hostnames:
24+
if chassis_card_type.value in hostname:
25+
res.append(hostname)
26+
return res

ansible/devutil/devices/sonic.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
import time
3+
24
import yaml
35

46
from .ansible_hosts import AnsibleHosts
57
from .ansible_hosts import RunAnsibleModuleFailed
8+
from .chassis_utils import is_chassis, get_chassis_hostnames, ChassisCardType
69

710
logger = logging.getLogger(__name__)
811

@@ -26,14 +29,40 @@ def sonic_version(self):
2629
return {}
2730

2831

29-
def upgrade_by_sonic(sonichosts, image_url, disk_used_percent):
32+
def upgrade_by_sonic(sonichosts, localhost, image_url, disk_used_percent):
3033
try:
3134
sonichosts.reduce_and_add_sonic_images(
3235
disk_used_pcent=disk_used_percent,
3336
new_image_url=image_url,
3437
module_attrs={"become": True}
3538
)
36-
sonichosts.shell("reboot", module_attrs={"become": True, "async": 300, "poll": 0})
39+
if is_chassis(sonichosts):
40+
logger.info("Upgrading image on chassis device...")
41+
# Chassis DUT need to firstly upgrade and reboot supervisor cards.
42+
# Until supervisor cards back online, then upgrade and reboot line cards.
43+
rp_hostnames = get_chassis_hostnames(sonichosts, ChassisCardType.SUPERVISOR_CARD)
44+
lc_hostnames = get_chassis_hostnames(sonichosts, ChassisCardType.LINE_CARD)
45+
sonichosts.shell("reboot", target_hosts=rp_hostnames,
46+
module_attrs={"become": True, "async": 300, "poll": 0})
47+
logger.info("Sleep 900s to wait for supervisor card to be ready...")
48+
time.sleep(900)
49+
for i in range(len(sonichosts.ips)):
50+
localhost.wait_for(
51+
host=sonichosts.ips[i],
52+
port=22,
53+
state="started",
54+
search_regex="OpenSSH",
55+
delay=0,
56+
timeout=600,
57+
module_attrs={"changed_when": False}
58+
)
59+
sonichosts.shell("reboot", target_hosts=lc_hostnames,
60+
module_attrs={"become": True, "async": 300, "poll": 0})
61+
logger.info("Sleep 300s to wait for line cards to be ready...")
62+
time.sleep(300)
63+
else:
64+
sonichosts.shell("reboot", module_attrs={"become": True, "async": 300, "poll": 0})
65+
3766
return True
3867
except RunAnsibleModuleFailed as e:
3968
logger.error(
@@ -205,7 +234,7 @@ def upgrade_image(sonichosts, localhost, image_url, upgrade_type="sonic", disk_u
205234
return False
206235

207236
if upgrade_type == "sonic":
208-
upgrade_result = upgrade_by_sonic(sonichosts, image_url, disk_used_percent)
237+
upgrade_result = upgrade_by_sonic(sonichosts, localhost, image_url, disk_used_percent)
209238
elif upgrade_type == "onie":
210239
upgrade_result = upgrade_by_onie(sonichosts, localhost, image_url, onie_pause_time)
211240
if not upgrade_result:

0 commit comments

Comments
 (0)