Skip to content

Commit

Permalink
auto download image if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed May 7, 2020
1 parent 8c4075f commit 6f538cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 4 additions & 3 deletions kvirt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ def delete_image(args):
common.confirm("Are you sure?")
codes = []
for image in images:
clientprofile = "%s_%s" % (cli, image)
common.pprint("Deleting image %s on %s" % (image, cli))
if image in config.profiles and len(config.profiles[image]) == 1 and 'image' in config.profiles[image]:
profileimage = config.profiles[image]['image']
config.delete_profile(image, quiet=True)
if clientprofile in config.profiles and 'image' in config.profiles[clientprofile]:
profileimage = config.profiles[clientprofile]['image']
config.delete_profile(clientprofile, quiet=True)
result = k.delete_image(profileimage)
else:
result = k.delete_image(image)
Expand Down
23 changes: 21 additions & 2 deletions kvirt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,28 @@ def create_vm(self, name, profile, overrides={}, customprofile={}, k=None,
vmprofiles = {k: v for k, v in self.profiles.items() if 'type' not in v or v['type'] == 'vm'}
if customprofile:
vmprofiles[profile] = customprofile
customprofileimage = customprofile.get('image')
if customprofileimage is not None:
clientprofile = "%s_%s" % (self.client, customprofileimage)
if clientprofile in vmprofiles and 'image' in vmprofiles[clientprofile]:
vmprofiles[profile]['image'] = vmprofiles[clientprofile]['image']
elif customprofileimage in IMAGES and\
IMAGES[customprofileimage] not in [os.path.basename(v) for v in self.k.volumes()]:
common.pprint("Image %s not found. Downloading" % customprofileimage, color='blue')
self.handle_host(pool=self.pool, image=customprofileimage, download=True, update_profile=True)
vmprofiles[profile]['image'] = os.path.basename(IMAGES[customprofileimage])
else:
common.pprint("Deploying vm %s from profile %s..." % (name, profile))
if profile not in vmprofiles:
clientprofile = "%s_%s" % (self.client, profile)
if clientprofile in vmprofiles and 'image' in vmprofiles[clientprofile]:
# profile = clientprofile
vmprofiles[profile] = {'image': vmprofiles[clientprofile]['image']}
elif profile in IMAGES and IMAGES[profile] not in [os.path.basename(v) for v in self.k.volumes()]:
common.pprint("Image %s not found. Downloading" % profile, color='blue')
self.handle_host(pool=self.pool, image=profile, download=True, update_profile=True)
vmprofiles[profile] = {'image': os.path.basename(IMAGES[profile])}
else:
common.pprint("profile %s not found. Using the image as profile..." % profile, color='blue')
common.pprint("Profile %s not found. Using the image as profile..." % profile, color='blue')
vmprofiles[profile] = {'image': profile}
profilename = profile
profile = vmprofiles[profile]
Expand Down Expand Up @@ -1479,6 +1492,12 @@ def plan(self, plan, ansible=False, url=None, path=None, autostart=False, contai
if profilename == currentimage or profilename == clientprofile:
profile['image'] = entry[4]
break
imageprofile = profile['image']
if imageprofile in IMAGES and\
IMAGES[imageprofile] not in [os.path.basename(v) for v in self.k.volumes()]:
common.pprint("Image %s not found. Downloading" % imageprofile, color='blue')
self.handle_host(pool=self.pool, image=imageprofile, download=True, update_profile=True)
profile['image'] = os.path.basename(IMAGES[imageprofile])
result = self.create_vm(name, profilename, overrides=overrides, customprofile=profile, k=z,
plan=plan, basedir=currentplandir, client=vmclient, onfly=onfly, planmode=True)
common.handle_response(result, name, client=vmclient)
Expand Down

0 comments on commit 6f538cd

Please sign in to comment.