Package VMBuilder :: Module distro
[frames] | no frames]

Source Code for Module VMBuilder.distro

  1  # 
  2  #    Uncomplicated VM Builder 
  3  #    Copyright (C) 2007-2009 Canonical Ltd. 
  4  #     
  5  #    See AUTHORS for list of contributors 
  6  # 
  7  #    This program is free software: you can redistribute it and/or modify 
  8  #    it under the terms of the GNU General Public License version 3, as 
  9  #    published by the Free Software Foundation. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19  #    Distro super class 
 20   
 21  import logging 
 22  import os 
 23   
 24  from   VMBuilder.util    import run_cmd, call_hooks 
 25  import VMBuilder.plugins 
 26   
27 -class Context(VMBuilder.plugins.Plugin):
28 - def __init__(self):
29 self._config = {} 30 super(Context, self).__init__(self) 31 self.plugins = [plugin_class(self) for plugin_class in self.plugin_classes] 32 self.plugins.sort(key=lambda x:x.priority) 33 self._cleanup_cbs = [] 34 self.hooks = {} 35 self.template_dirs = [os.path.expanduser('~/.vmbuilder/%s'), 36 os.path.dirname(__file__) + '/plugins/%s/templates', 37 '/etc/vmbuilder/%s']
38 39 # Cleanup
40 - def cleanup(self):
41 logging.info("Cleaning up") 42 while len(self._cleanup_cbs) > 0: 43 self._cleanup_cbs.pop(0)()
44
45 - def add_clean_cb(self, cb):
46 self._cleanup_cbs.insert(0, cb)
47
48 - def add_clean_cmd(self, *argv, **kwargs):
49 cb = lambda : run_cmd(*argv, **kwargs) 50 self.add_clean_cb(cb) 51 return cb
52
53 - def cancel_cleanup(self, cb):
54 try: 55 self._cleanup_cbs.remove(cb) 56 except ValueError: 57 # Wasn't in there. No worries. 58 pass
59 60 # Hooks
61 - def register_hook(self, hook_name, func):
62 self.hooks[hook_name] = self.hooks.get(hook_name, []) + [func]
63
64 - def call_hooks(self, *args, **kwargs):
65 try: 66 call_hooks(self, *args, **kwargs) 67 except Exception: 68 #self.cleanup() 69 raise
70
71 -class Distro(Context):
72 - def __init__(self):
73 self.plugin_classes = VMBuilder._distro_plugins 74 super(Distro, self).__init__()
75
76 - def set_chroot_dir(self, chroot_dir):
77 self.chroot_dir = chroot_dir
78
79 - def build_chroot(self):
80 self.call_hooks('preflight_check') 81 self.call_hooks('set_defaults') 82 self.call_hooks('bootstrap') 83 self.call_hooks('configure_os') 84 self.call_hooks('post_install')
85
86 - def has_xen_support(self):
87 """Install the distro into destdir""" 88 raise NotImplemented('Distro subclasses need to implement the has_xen_support method')
89
90 - def install(self, destdir):
91 """Install the distro into destdir""" 92 raise NotImplemented('Distro subclasses need to implement the install method')
93
94 - def post_mount(self, fs):
95 """Called each time a filesystem is mounted to let the distro add things to the filesystem"""
96
97 - def install_vmbuilder_log(self, logfile):
98 """Let the distro copy the install logfile to the guest"""
99