Merge lp:~nuclearbob/utah/cleanup-cleanup into lp:utah

Proposed by Max Brustkern
Status: Merged
Approved by: Javier Collado
Approved revision: 883
Merged at revision: 879
Proposed branch: lp:~nuclearbob/utah/cleanup-cleanup
Merge into: lp:utah
Diff against target: 209 lines (+19/-56)
5 files modified
utah/provisioning/baremetal/bamboofeeder.py (+2/-3)
utah/provisioning/baremetal/cobbler.py (+1/-3)
utah/provisioning/provisioning.py (+7/-4)
utah/provisioning/ssh.py (+2/-33)
utah/provisioning/vm/vm.py (+7/-13)
To merge this branch: bzr merge lp:~nuclearbob/utah/cleanup-cleanup
Reviewer Review Type Date Requested Status
Javier Collado (community) Approve
Max Brustkern (community) Needs Resubmitting
Andy Doan (community) Approve
Review via email: mp+160674@code.launchpad.net

Description of the change

This branch moves all cleanup operations to the standard cleanup methods. I think this should allow us to remove the destroy function of the machine class once the scripts are modified appropriately. I'm working on that over in lp:~nuclearbob/utah/consolidate-scripts but I'd like to get this merged first.

To post a comment you must log in.
Revision history for this message
Andy Doan (doanac) wrote :

nice idea - i like it.

review: Approve
Revision history for this message
Javier Collado (javier.collado) wrote :

A backtrace is printed when trying to run the code:
  File "utah/provisioning/ssh.py", line 58, in initialize
    cleanup.add_function(self.ssh_client.close)
TypeError: add_function() takes at least 3 arguments (2 given)

The problem is that the `cleanup.add_function` requires a timeout parameter
that is missing in the call above.

review: Needs Fixing
Revision history for this message
Max Brustkern (nuclearbob) wrote :

Fixed that. Running some testing now.

Revision history for this message
Max Brustkern (nuclearbob) wrote :

I fixed that and ran a successful test on a VM. Bare metal is broken in the current dev branch, so I can't test there yet, but I don't think this introduces and new problems there.

review: Needs Resubmitting
lp:~nuclearbob/utah/cleanup-cleanup updated
876. By Andy Doan

uuid_check requires ssh so "self.provisioned" must be True

This call was moved into provisioning.py where its a litle
easier to manage it in conjunction with the state the machine
is in. This now means vm's will do the check as well, but that
should be harmless.

877. By Javier Collado

Merged changes to fix help output to use just one runlist (LP: #1112597)

Source branch: lp:~nuclearbob/utah/bug1112597

883. By Max Brustkern

Added timeout to cleanup call

Revision history for this message
Javier Collado (javier.collado) wrote :

This worked fine for me with a server image with pass.run runlist.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'utah/provisioning/baremetal/bamboofeeder.py'
--- utah/provisioning/baremetal/bamboofeeder.py 2013-04-25 16:05:56 +0000
+++ utah/provisioning/baremetal/bamboofeeder.py 2013-04-26 17:14:27 +0000
@@ -40,8 +40,8 @@
4040
41 # TODO: raise more exceptions if ProcessRunner fails41 # TODO: raise more exceptions if ProcessRunner fails
42 # maybe get some easy way to do that like failok42 # maybe get some easy way to do that like failok
43 def __init__(self, inventory=None, machineinfo=None, name=None,43 def __init__(self, machineinfo=None, name=None, preboot=None, *args,
44 preboot=None, *args, **kw):44 **kw):
45 # TODO: respect rewrite setting45 # TODO: respect rewrite setting
46 if name is None:46 if name is None:
47 raise UTAHBMProvisioningException(47 raise UTAHBMProvisioningException(
@@ -50,7 +50,6 @@
50 self.macaddress = machineinfo['mac-address']50 self.macaddress = machineinfo['mac-address']
51 except AttributeError:51 except AttributeError:
52 raise UTAHBMProvisioningException('No MAC address specified')52 raise UTAHBMProvisioningException('No MAC address specified')
53 self.inventory = inventory
54 super(BambooFeederMachine, self).__init__(*args,53 super(BambooFeederMachine, self).__init__(*args,
55 machineinfo=machineinfo,54 machineinfo=machineinfo,
56 name=name, **kw)55 name=name, **kw)
5756
=== modified file 'utah/provisioning/baremetal/cobbler.py'
--- utah/provisioning/baremetal/cobbler.py 2013-04-25 16:05:56 +0000
+++ utah/provisioning/baremetal/cobbler.py 2013-04-26 17:14:27 +0000
@@ -39,8 +39,7 @@
3939
40 # TODO: raise more exceptions if ProcessRunner fails40 # TODO: raise more exceptions if ProcessRunner fails
41 # maybe get some easy way to do that like failok41 # maybe get some easy way to do that like failok
42 def __init__(self, inventory=None, machineinfo=None,42 def __init__(self, machineinfo=None, name=None, *args, **kw):
43 name=None, *args, **kw):
44 # TODO: support for reusing existing machines43 # TODO: support for reusing existing machines
45 if name is None:44 if name is None:
46 raise UTAHBMProvisioningException(45 raise UTAHBMProvisioningException(
@@ -51,7 +50,6 @@
51 else:50 else:
52 self.machineinfo = machineinfo51 self.machineinfo = machineinfo
53 self.power = {}52 self.power = {}
54 self.inventory = inventory
55 super(CobblerMachine, self).__init__(*args, machineinfo=machineinfo,53 super(CobblerMachine, self).__init__(*args, machineinfo=machineinfo,
56 name=name, **kw)54 name=name, **kw)
57 if self.inventory is not None:55 if self.inventory is not None:
5856
=== modified file 'utah/provisioning/provisioning.py'
--- utah/provisioning/provisioning.py 2013-04-25 16:05:56 +0000
+++ utah/provisioning/provisioning.py 2013-04-26 17:14:27 +0000
@@ -66,10 +66,10 @@
6666
67 def __init__(self, arch=None, boot=None, clean=True, debug=False,67 def __init__(self, arch=None, boot=None, clean=True, debug=False,
68 directory=None, image=None, dlpercentincrement=1,68 directory=None, image=None, dlpercentincrement=1,
69 initrd=None, installtype=None, kernel=None, machineid=None,69 initrd=None, installtype=None, inventory=None, kernel=None,
70 machineuuid=None, name=None, new=False, prefix='utah',70 machineid=None, machineuuid=None, name=None, new=False,
71 preseed=None, rewrite=None, series=None, template=None,71 prefix='utah', preseed=None, rewrite=None, series=None,
72 xml=None):72 template=None, xml=None):
73 """Initialize the object representing the machine.73 """Initialize the object representing the machine.
7474
75 One of these groups of arguments should be included:75 One of these groups of arguments should be included:
@@ -90,6 +90,8 @@
90 used to install a physical machine using another method.90 used to install a physical machine using another method.
91 dlpercentincrement: How often to log download updates to INFO91 dlpercentincrement: How often to log download updates to INFO
92 (All updates logged to DEBUG)92 (All updates logged to DEBUG)
93 inventory: Inventory object managing the machine; used for cleanup
94 purposes.
93 machineid: Should be passed by the request method of an Inventory95 machineid: Should be passed by the request method of an Inventory
94 class. Often used to name virtual machines.96 class. Often used to name virtual machines.
95 machineuuid: Unique identifier. Will be randomly generated if not97 machineuuid: Unique identifier. Will be randomly generated if not
@@ -117,6 +119,7 @@
117 # TODO: Consider a global temp file creator, maybe as part of install.119 # TODO: Consider a global temp file creator, maybe as part of install.
118 self.debug = debug120 self.debug = debug
119 self.dlpercentincrement = dlpercentincrement121 self.dlpercentincrement = dlpercentincrement
122 self.inventory = inventory
120 self.machineid = machineid123 self.machineid = machineid
121 self.new = new124 self.new = new
122 self.prefix = prefix125 self.prefix = prefix
123126
=== modified file 'utah/provisioning/ssh.py'
--- utah/provisioning/ssh.py 2013-04-24 11:37:21 +0000
+++ utah/provisioning/ssh.py 2013-04-26 17:14:27 +0000
@@ -26,11 +26,10 @@
2626
27from stat import S_ISDIR27from stat import S_ISDIR
2828
29from jinja2 import Environment, FileSystemLoader
30
31import utah.timeout29import utah.timeout
3230
33from utah import config31from utah import config
32from utah.cleanup import cleanup
34from utah.provisioning.exceptions import UTAHProvisioningException33from utah.provisioning.exceptions import UTAHProvisioningException
35from utah.provisioning.provisioning import Machine34from utah.provisioning.provisioning import Machine
36from utah.retry import retry35from utah.retry import retry
@@ -56,6 +55,7 @@
56 ssh_client = paramiko.SSHClient()55 ssh_client = paramiko.SSHClient()
57 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())56 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
58 self.ssh_client = ssh_client57 self.ssh_client = ssh_client
58 cleanup.add_function(10, self.ssh_client.close)
59 self.ssh_logger = logging.getLogger('ssh')59 self.ssh_logger = logging.getLogger('ssh')
6060
61 def run(self, command, quiet=False, root=False, command_timeout=None):61 def run(self, command, quiet=False, root=False, command_timeout=None):
@@ -292,37 +292,6 @@
292 else:292 else:
293 self.downloadfiles(myfile, newtarget)293 self.downloadfiles(myfile, newtarget)
294294
295 def destroy(self, *args, **kw):
296 """Clean up known hosts for machine."""
297 # TODO: evaluate value of known_hosts with paramiko
298 # TODO: make this use standard cleanup if we do leave it in
299 self.ssh_logger.info('Removing machine addresses '
300 'from ssh known_hosts file')
301 addresses = [self.name]
302 try:
303 addresses.append(socket.gethostbyname(self.name))
304 except socket.gaierror as err:
305 if err.errno in [-2, -5]:
306 self.ssh_logger.debug('{} is not resolvable, '
307 'so not removing from known_hosts'
308 .format(self.name))
309 else:
310 raise err
311
312 old_host_keys = self.ssh_client.get_host_keys()
313 new_host_keys = paramiko.HostKeys()
314 addresses = set(addresses)
315 for address, key in old_host_keys.iteritems():
316 # Skip keys so that they don't get added
317 # into the new keys (i.e. they're removed)
318 if address in addresses:
319 continue
320 new_host_keys[address] = key
321 new_host_keys.save(config.sshknownhosts)
322 self.ssh_client.close()
323
324 super(SSHMixin, self).destroy(*args, **kw)
325
326 def sshcheck(self):295 def sshcheck(self):
327 """Check if the machine is available via ssh.296 """Check if the machine is available via ssh.
328297
329298
=== modified file 'utah/provisioning/vm/vm.py'
--- utah/provisioning/vm/vm.py 2013-04-18 17:17:45 +0000
+++ utah/provisioning/vm/vm.py 2013-04-26 17:14:27 +0000
@@ -90,6 +90,7 @@
90 self.vm.create()90 self.vm.create()
91 else:91 else:
92 raise UTAHVMProvisioningException('Failed to provision VM')92 raise UTAHVMProvisioningException('Failed to provision VM')
93 self.cleanfunction(self.stop, force=True)
93 self.active = True94 self.active = True
9495
95 def stop(self, force=False):96 def stop(self, force=False):
@@ -163,6 +164,9 @@
163 autoname = False164 autoname = False
164 super(CustomVM, self).__init__(machineid=machineid, name=name, *args,165 super(CustomVM, self).__init__(machineid=machineid, name=name, *args,
165 **kw)166 **kw)
167 if self.inventory is not None:
168 self.cleanfunction(self.inventory.destroy,
169 machineid=self.machineid)
166 # TODO: do a better job of separating installation170 # TODO: do a better job of separating installation
167 # into _create rather than __init__171 # into _create rather than __init__
168 if self.image is None:172 if self.image is None:
@@ -202,6 +206,7 @@
202 self.directory = os.path.join(config.vmpath, self.name)206 self.directory = os.path.join(config.vmpath, self.name)
203 else:207 else:
204 self.directory = directory208 self.directory = directory
209 self.cleanfile(self.directory)
205 self.logger.info('Checking if machine directory {} exists'210 self.logger.info('Checking if machine directory {} exists'
206 .format(self.directory))211 .format(self.directory))
207 if not os.path.isdir(self.directory):212 if not os.path.isdir(self.directory):
@@ -234,6 +239,7 @@
234 'size': disksize,239 'size': disksize,
235 'type': 'qcow2'}240 'type': 'qcow2'}
236 self.disks.append(disk)241 self.disks.append(disk)
242 self.cleanfile(diskfile)
237 self.logger.debug('Adding disk to list')243 self.logger.debug('Adding disk to list')
238244
239 def _supportsdomaintype(self, domaintype):245 def _supportsdomaintype(self, domaintype):
@@ -516,6 +522,7 @@
516522
517 self.logger.info('Setting up final VM')523 self.logger.info('Setting up final VM')
518 self.vm = self.lv.defineXML(ElementTree.tostring(xml.getroot()))524 self.vm = self.lv.defineXML(ElementTree.tostring(xml.getroot()))
525 self.cleanfunction(self.vm.undefine)
519526
520 def _start(self):527 def _start(self):
521 """Start the VM."""528 """Start the VM."""
@@ -538,19 +545,6 @@
538 self.sshpoll(timeout=self.boottimeout)545 self.sshpoll(timeout=self.boottimeout)
539 self.active = True546 self.active = True
540547
541 def destroy(self, *args, **kw):
542 """Remove the machine from libvirt and remove all the disk files."""
543 # TODO: make this use standard cleanup
544 super(CustomVM, self).destroy(*args, **kw)
545 self.stop(force=True)
546 if self.vm is not None:
547 self.vm.undefine()
548 else:
549 self.logger.info('VM not created')
550 for disk in self.disks:
551 os.unlink(disk['file'])
552 shutil.rmtree(self.directory)
553
554548
555# See http://kennethreitz.com/blog/generate-a-random-mac-address-in-python/549# See http://kennethreitz.com/blog/generate-a-random-mac-address-in-python/
556def random_mac_address():550def random_mac_address():

Subscribers

People subscribed via source and target branches