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 self.overwrite = False
39 40 # Cleanup
41 - def cleanup(self):
42 logging.info("Cleaning up") 43 while len(self._cleanup_cbs) > 0: 44 self._cleanup_cbs.pop(0)()
45
46 - def add_clean_cb(self, cb):
47 self._cleanup_cbs.insert(0, cb)
48
49 - def add_clean_cmd(self, *argv, **kwargs):
50 cb = lambda : run_cmd(*argv, **kwargs) 51 self.add_clean_cb(cb) 52 return cb
53
54 - def cancel_cleanup(self, cb):
55 try: 56 self._cleanup_cbs.remove(cb) 57 except ValueError: 58 # Wasn't in there. No worries. 59 pass
60 61 # Hooks
62 - def register_hook(self, hook_name, func):
63 self.hooks[hook_name] = self.hooks.get(hook_name, []) + [func]
64
65 - def call_hooks(self, *args, **kwargs):
66 try: 67 call_hooks(self, *args, **kwargs) 68 except Exception: 69 self.cleanup() 70 raise
71
72 -class Distro(Context):
73 - def __init__(self):
74 self.plugin_classes = VMBuilder._distro_plugins 75 super(Distro, self).__init__()
76
77 - def set_chroot_dir(self, chroot_dir):
78 self.chroot_dir = chroot_dir
79
80 - def build_chroot(self):
81 self.call_hooks('preflight_check') 82 self.call_hooks('set_defaults') 83 self.call_hooks('bootstrap') 84 self.call_hooks('configure_os') 85 self.cleanup()
86
87 - def has_xen_support(self):
88 """Install the distro into destdir""" 89 raise NotImplemented('Distro subclasses need to implement the has_xen_support method')
90
91 - def install(self, destdir):
92 """Install the distro into destdir""" 93 raise NotImplemented('Distro subclasses need to implement the install method')
94
95 - def post_mount(self, fs):
96 """Called each time a filesystem is mounted to let the distro add things to the filesystem"""
97
98 - def install_vmbuilder_log(self, logfile):
99 """Let the distro copy the install logfile to the guest"""
100