63
Perform deployment of the VM.
65
If True is returned, no further deployment will be done.
67
def install_file(self, path, contents=None, source=None, mode=None):
68
fullpath = '%s%s' % (self.chroot_dir, path)
69
if not os.path.isdir(os.path.dirname(fullpath)):
70
os.makedirs(os.path.dirname(fullpath))
71
if source and not contents:
72
shutil.copy(source, fullpath)
74
fp = open(fullpath, 'w')
78
os.chmod(fullpath, mode)
69
81
def install_from_template(self, path, tmplname, context=None, mode=None):
70
if not self.vm.fsmounted:
71
raise VMBuilderException('install_from_template called while file system is not mounted')
72
return self.vm.install_file(path, VMBuilder.util.render_template(self.__module__.split('.')[2], self.vm, tmplname, context), mode=mode)
82
return self.install_file(path, VMBuilder.util.render_template(self.__module__.split('.')[2], self.context, tmplname, context), mode=mode)
74
84
def run_in_target(self, *args, **kwargs):
75
if not self.vm.fsmounted:
76
raise VMBuilderException('install_from_template called while file system is not mounted')
77
return run_cmd('chroot', self.vm.installdir, *args, **kwargs)
85
return util.run_cmd('chroot', self.chroot_dir, *args, **kwargs)
87
def call_hooks(self, *args, **kwargs):
88
return util.call_hooks(self.context, *args, **kwargs)
91
class SettingGroup(object):
92
def __init__(self, plugin, context, name):
93
# The plugin that owns this setting
96
self.context = context
97
# Name of the Setting Group
99
# A list of Setting objects
102
def add_setting(self, *args, **kwargs):
103
# kwarg['type'] is used to determine which type of Setting object to create
104
# but we don't want to pass it on to its __init__.
106
type = kwargs['type']
112
setting = self.plugin.StringSetting(self, *args, **kwargs)
114
setting = self.plugin.BooleanSetting(self, *args, **kwargs)
116
setting = self.plugin.ListSetting(self, *args, **kwargs)
118
setting = self.plugin.IntSetting(self, *args, **kwargs)
120
raise VMBuilderException("Unknown setting type: '%s' (Plugin: '%s', Setting group: '%s', Setting: '%s')" %
122
self.plugin.__module__,
125
self._settings.append(setting)
127
class Setting(object):
130
def __init__(self, setting_group, name, metavar=None, help=None, extra_args=None, valid_options=None, action=None, **kwargs):
131
# The Setting Group object that owns this Setting
132
self.setting_group = setting_group
133
# The name if the setting
134
name_regex = '[a-z0-9-]+$'
135
if not re.match(name_regex, name):
136
raise VMBuilderException('Invalid name for Setting: %s. Must match regex: %s' % (name, name_regex))
140
self.default = kwargs.get('default', self.default)
142
# Alternate names (for the CLI)
143
self.extra_args = extra_args or []
144
self.metavar = metavar
146
self.value_set = False
147
self.valid_options = valid_options
149
if self.name in self.setting_group.context._config:
150
raise VMBuilderException("Setting named %s already exists. Previous definition in %s/%s/%s." %
152
self.setting_group.plugin.__name__,
153
self.setting_group.plugin._config[self.name].setting_group.name,
154
self.setting_group.plugin._config[self.name].name))
156
self.setting_group.context._config[self.name] = self
160
If a value has previously been set, return it.
161
If not, return the default value.
169
def do_check_value(self, value):
171
Checks the value's validity.
173
if self.valid_options is not None:
174
if value not in self.valid_options:
175
raise VMBuilderException('%r is not a valid option for %s. Valid options are: %s' % (value, self.name, ' '.join(self.valid_options)))
177
self.check_value(value)
179
def get_valid_options(self):
180
return self.valid_options
182
def set_valid_options(self, valid_options):
184
Set the list of valid options for this setting.
186
if not type(valid_options) == list and valid_options is not None:
187
raise VMBuilderException('set_valid_options only accepts lists or None')
189
for option in valid_options:
190
self.check_value(option)
191
self.valid_options = valid_options
193
def get_default(self):
195
Return the default value.
199
def set_default(self, value):
201
Set a new default value.
203
self.do_check_value(value)
206
def set_value(self, value):
210
self.do_check_value(value)
212
self.value_set = True
214
class ListSetting(Setting):
215
def __init__(self, *args, **kwargs):
217
super(Plugin.ListSetting, self).__init__(*args, **kwargs)
219
def check_value(self, value):
220
if not type(value) == list:
221
raise VMBuilderException('%r is type %s, expected list.' % (value, type(value)))
223
class IntSetting(Setting):
224
def check_value(self, value):
225
if not type(value) == int:
226
raise VMBuilderException('%r is type %s, expected int.' % (value, type(value)))
228
class BooleanSetting(Setting):
229
def check_value(self, value):
230
if not type(value) == bool:
231
raise VMBuilderException('%r is type %s, expected bool.' % (value, type(value)))
233
class StringSetting(Setting):
234
def check_value(self, value):
235
if not type(value) == str:
236
raise VMBuilderException('%r is type %s, expected str.' % (value, type(value)))
238
def setting_group(self, name):
239
setting_group = self.SettingGroup(self, self.context, name)
240
self._setting_groups.append(setting_group)
243
def has_setting(self, name):
244
return name in self.context._config
246
def get_setting(self, name):
247
if not name in self.context._config:
248
raise VMBuilderException('Unknown config key: %s' % name)
249
return self.context._config[name].get_value()
251
def set_setting(self, name, value):
252
if not name in self.context._config:
253
raise VMBuilderException('Unknown config key: %s' % name)
254
self.context._config[name].set_value(value)
256
def set_setting_default(self, name, value):
257
if not name in self.context._config:
258
raise VMBuilderException('Unknown config key: %s' % name)
259
self.context._config[name].set_default(value)
261
def get_setting_default(self, name):
262
if not name in self.context._config:
263
raise VMBuilderException('Unknown config key: %s' % name)
264
return self.context._config[name].get_default()
266
def get_setting_valid_options(self, name):
267
if not name in self.context._config:
268
raise VMBuilderException('Unknown config key: %s' % name)
269
return self.context._config[name].get_valid_options()
271
def set_setting_valid_options(self, name, valid_options):
272
if not name in self.context._config:
273
raise VMBuilderException('Unknown config key: %s' % name)
274
self.context._config[name].set_valid_options(valid_options)