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
1=== modified file 'utah/provisioning/baremetal/bamboofeeder.py'
2--- utah/provisioning/baremetal/bamboofeeder.py 2013-04-25 16:05:56 +0000
3+++ utah/provisioning/baremetal/bamboofeeder.py 2013-04-26 17:14:27 +0000
4@@ -40,8 +40,8 @@
5
6 # TODO: raise more exceptions if ProcessRunner fails
7 # maybe get some easy way to do that like failok
8- def __init__(self, inventory=None, machineinfo=None, name=None,
9- preboot=None, *args, **kw):
10+ def __init__(self, machineinfo=None, name=None, preboot=None, *args,
11+ **kw):
12 # TODO: respect rewrite setting
13 if name is None:
14 raise UTAHBMProvisioningException(
15@@ -50,7 +50,6 @@
16 self.macaddress = machineinfo['mac-address']
17 except AttributeError:
18 raise UTAHBMProvisioningException('No MAC address specified')
19- self.inventory = inventory
20 super(BambooFeederMachine, self).__init__(*args,
21 machineinfo=machineinfo,
22 name=name, **kw)
23
24=== modified file 'utah/provisioning/baremetal/cobbler.py'
25--- utah/provisioning/baremetal/cobbler.py 2013-04-25 16:05:56 +0000
26+++ utah/provisioning/baremetal/cobbler.py 2013-04-26 17:14:27 +0000
27@@ -39,8 +39,7 @@
28
29 # TODO: raise more exceptions if ProcessRunner fails
30 # maybe get some easy way to do that like failok
31- def __init__(self, inventory=None, machineinfo=None,
32- name=None, *args, **kw):
33+ def __init__(self, machineinfo=None, name=None, *args, **kw):
34 # TODO: support for reusing existing machines
35 if name is None:
36 raise UTAHBMProvisioningException(
37@@ -51,7 +50,6 @@
38 else:
39 self.machineinfo = machineinfo
40 self.power = {}
41- self.inventory = inventory
42 super(CobblerMachine, self).__init__(*args, machineinfo=machineinfo,
43 name=name, **kw)
44 if self.inventory is not None:
45
46=== modified file 'utah/provisioning/provisioning.py'
47--- utah/provisioning/provisioning.py 2013-04-25 16:05:56 +0000
48+++ utah/provisioning/provisioning.py 2013-04-26 17:14:27 +0000
49@@ -66,10 +66,10 @@
50
51 def __init__(self, arch=None, boot=None, clean=True, debug=False,
52 directory=None, image=None, dlpercentincrement=1,
53- initrd=None, installtype=None, kernel=None, machineid=None,
54- machineuuid=None, name=None, new=False, prefix='utah',
55- preseed=None, rewrite=None, series=None, template=None,
56- xml=None):
57+ initrd=None, installtype=None, inventory=None, kernel=None,
58+ machineid=None, machineuuid=None, name=None, new=False,
59+ prefix='utah', preseed=None, rewrite=None, series=None,
60+ template=None, xml=None):
61 """Initialize the object representing the machine.
62
63 One of these groups of arguments should be included:
64@@ -90,6 +90,8 @@
65 used to install a physical machine using another method.
66 dlpercentincrement: How often to log download updates to INFO
67 (All updates logged to DEBUG)
68+ inventory: Inventory object managing the machine; used for cleanup
69+ purposes.
70 machineid: Should be passed by the request method of an Inventory
71 class. Often used to name virtual machines.
72 machineuuid: Unique identifier. Will be randomly generated if not
73@@ -117,6 +119,7 @@
74 # TODO: Consider a global temp file creator, maybe as part of install.
75 self.debug = debug
76 self.dlpercentincrement = dlpercentincrement
77+ self.inventory = inventory
78 self.machineid = machineid
79 self.new = new
80 self.prefix = prefix
81
82=== modified file 'utah/provisioning/ssh.py'
83--- utah/provisioning/ssh.py 2013-04-24 11:37:21 +0000
84+++ utah/provisioning/ssh.py 2013-04-26 17:14:27 +0000
85@@ -26,11 +26,10 @@
86
87 from stat import S_ISDIR
88
89-from jinja2 import Environment, FileSystemLoader
90-
91 import utah.timeout
92
93 from utah import config
94+from utah.cleanup import cleanup
95 from utah.provisioning.exceptions import UTAHProvisioningException
96 from utah.provisioning.provisioning import Machine
97 from utah.retry import retry
98@@ -56,6 +55,7 @@
99 ssh_client = paramiko.SSHClient()
100 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
101 self.ssh_client = ssh_client
102+ cleanup.add_function(10, self.ssh_client.close)
103 self.ssh_logger = logging.getLogger('ssh')
104
105 def run(self, command, quiet=False, root=False, command_timeout=None):
106@@ -292,37 +292,6 @@
107 else:
108 self.downloadfiles(myfile, newtarget)
109
110- def destroy(self, *args, **kw):
111- """Clean up known hosts for machine."""
112- # TODO: evaluate value of known_hosts with paramiko
113- # TODO: make this use standard cleanup if we do leave it in
114- self.ssh_logger.info('Removing machine addresses '
115- 'from ssh known_hosts file')
116- addresses = [self.name]
117- try:
118- addresses.append(socket.gethostbyname(self.name))
119- except socket.gaierror as err:
120- if err.errno in [-2, -5]:
121- self.ssh_logger.debug('{} is not resolvable, '
122- 'so not removing from known_hosts'
123- .format(self.name))
124- else:
125- raise err
126-
127- old_host_keys = self.ssh_client.get_host_keys()
128- new_host_keys = paramiko.HostKeys()
129- addresses = set(addresses)
130- for address, key in old_host_keys.iteritems():
131- # Skip keys so that they don't get added
132- # into the new keys (i.e. they're removed)
133- if address in addresses:
134- continue
135- new_host_keys[address] = key
136- new_host_keys.save(config.sshknownhosts)
137- self.ssh_client.close()
138-
139- super(SSHMixin, self).destroy(*args, **kw)
140-
141 def sshcheck(self):
142 """Check if the machine is available via ssh.
143
144
145=== modified file 'utah/provisioning/vm/vm.py'
146--- utah/provisioning/vm/vm.py 2013-04-18 17:17:45 +0000
147+++ utah/provisioning/vm/vm.py 2013-04-26 17:14:27 +0000
148@@ -90,6 +90,7 @@
149 self.vm.create()
150 else:
151 raise UTAHVMProvisioningException('Failed to provision VM')
152+ self.cleanfunction(self.stop, force=True)
153 self.active = True
154
155 def stop(self, force=False):
156@@ -163,6 +164,9 @@
157 autoname = False
158 super(CustomVM, self).__init__(machineid=machineid, name=name, *args,
159 **kw)
160+ if self.inventory is not None:
161+ self.cleanfunction(self.inventory.destroy,
162+ machineid=self.machineid)
163 # TODO: do a better job of separating installation
164 # into _create rather than __init__
165 if self.image is None:
166@@ -202,6 +206,7 @@
167 self.directory = os.path.join(config.vmpath, self.name)
168 else:
169 self.directory = directory
170+ self.cleanfile(self.directory)
171 self.logger.info('Checking if machine directory {} exists'
172 .format(self.directory))
173 if not os.path.isdir(self.directory):
174@@ -234,6 +239,7 @@
175 'size': disksize,
176 'type': 'qcow2'}
177 self.disks.append(disk)
178+ self.cleanfile(diskfile)
179 self.logger.debug('Adding disk to list')
180
181 def _supportsdomaintype(self, domaintype):
182@@ -516,6 +522,7 @@
183
184 self.logger.info('Setting up final VM')
185 self.vm = self.lv.defineXML(ElementTree.tostring(xml.getroot()))
186+ self.cleanfunction(self.vm.undefine)
187
188 def _start(self):
189 """Start the VM."""
190@@ -538,19 +545,6 @@
191 self.sshpoll(timeout=self.boottimeout)
192 self.active = True
193
194- def destroy(self, *args, **kw):
195- """Remove the machine from libvirt and remove all the disk files."""
196- # TODO: make this use standard cleanup
197- super(CustomVM, self).destroy(*args, **kw)
198- self.stop(force=True)
199- if self.vm is not None:
200- self.vm.undefine()
201- else:
202- self.logger.info('VM not created')
203- for disk in self.disks:
204- os.unlink(disk['file'])
205- shutil.rmtree(self.directory)
206-
207
208 # See http://kennethreitz.com/blog/generate-a-random-mac-address-in-python/
209 def random_mac_address():

Subscribers

People subscribed via source and target branches