Skip to content

Commit 453aa6e

Browse files
authored
[sonic_installer]: add command to cleanup installed images (#134)
1 parent 4110092 commit 453aa6e

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

sonic_installer/main.py

+54-31
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ def get_next_image():
114114
next_image = images[next_image_index]
115115
return next_image
116116

117+
def remove_image(image):
118+
if get_image_type() == IMAGE_TYPE_ABOOT:
119+
nextimage = get_next_image()
120+
current = get_current_image()
121+
if image == nextimage:
122+
image_dir = current.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
123+
command = "echo \"SWI=flash:%s/.sonic-boot.swi\" > %s/%s" % (image_dir, HOST_PATH, ABOOT_BOOT_CONFIG)
124+
run_command(command)
125+
click.echo("Set next boot to current image %s" % current)
126+
127+
image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
128+
click.echo('Removing image root filesystem...')
129+
shutil.rmtree(HOST_PATH + '/' + image_dir)
130+
click.echo('Image removed')
131+
else:
132+
click.echo('Updating GRUB...')
133+
config = open(HOST_PATH + '/grub/grub.cfg', 'r')
134+
old_config = config.read()
135+
menuentry = re.search("menuentry '" + image + "[^}]*}", old_config).group()
136+
config.close()
137+
config = open(HOST_PATH + '/grub/grub.cfg', 'w')
138+
# remove menuentry of the image in grub.cfg
139+
config.write(old_config.replace(menuentry, ""))
140+
config.close()
141+
click.echo('Done')
142+
143+
image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
144+
click.echo('Removing image root filesystem...')
145+
shutil.rmtree(HOST_PATH + '/' + image_dir)
146+
click.echo('Done')
147+
148+
run_command('grub-set-default --boot-directory=' + HOST_PATH + ' 0')
149+
click.echo('Image removed')
150+
117151
# Callback for confirmation prompt. Aborts if user enters "n"
118152
def abort_if_false(ctx, param, value):
119153
if not value:
@@ -216,44 +250,14 @@ def remove(image):
216250
""" Uninstall image """
217251
images = get_installed_images()
218252
current = get_current_image()
219-
nextimage = get_next_image()
220253
if image not in images:
221254
click.echo('Image does not exist')
222255
sys.exit(1)
223256
if image == current:
224257
click.echo('Cannot remove current image')
225258
sys.exit(1)
226259

227-
if get_image_type() == IMAGE_TYPE_ABOOT:
228-
if image == nextimage:
229-
image_dir = current.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
230-
command = "echo \"SWI=flash:%s/.sonic-boot.swi\" > %s/%s" % (image_dir, HOST_PATH, ABOOT_BOOT_CONFIG)
231-
run_command(command)
232-
click.echo("Set next boot to current image %s" % current)
233-
234-
image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
235-
click.echo('Removing image root filesystem...')
236-
shutil.rmtree(HOST_PATH + '/' + image_dir)
237-
click.echo('Image removed')
238-
else:
239-
click.echo('Updating GRUB...')
240-
config = open(HOST_PATH + '/grub/grub.cfg', 'r')
241-
old_config = config.read()
242-
menuentry = re.search("menuentry '" + image + "[^}]*}", old_config).group()
243-
config.close()
244-
config = open(HOST_PATH + '/grub/grub.cfg', 'w')
245-
# remove menuentry of the image in grub.cfg
246-
config.write(old_config.replace(menuentry, ""))
247-
config.close()
248-
click.echo('Done')
249-
250-
image_dir = image.replace(IMAGE_PREFIX, IMAGE_DIR_PREFIX)
251-
click.echo('Removing image root filesystem...')
252-
shutil.rmtree(HOST_PATH + '/' + image_dir)
253-
click.echo('Done')
254-
255-
run_command('grub-set-default --boot-directory=' + HOST_PATH + ' 0')
256-
click.echo('Image removed')
260+
remove_image(image)
257261

258262
# Retrieve version from binary image file and print to screen
259263
@cli.command()
@@ -291,5 +295,24 @@ def binary_version(binary_image_path):
291295

292296
click.echo(IMAGE_PREFIX + version_num)
293297

298+
# Remove installed images which are not current and next
299+
@cli.command()
300+
@click.option('-y', '--yes', is_flag=True, callback=abort_if_false,
301+
expose_value=False, prompt='Remove images which are not current and next, continue?')
302+
def cleanup():
303+
""" Remove installed images which are not current and next """
304+
images = get_installed_images()
305+
curimage = get_current_image()
306+
nextimage = get_next_image()
307+
image_removed = 0
308+
for image in get_installed_images():
309+
if image != curimage and image != nextimage:
310+
click.echo("Removing image %s" % image)
311+
remove_image(image)
312+
image_removed += 1
313+
314+
if image_removed == 0:
315+
click.echo("No image(s) to remove")
316+
294317
if __name__ == '__main__':
295318
cli()

0 commit comments

Comments
 (0)