/
virtualbox.py
276 lines (224 loc) · 7.36 KB
/
virtualbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
A salt cloud provider that lets you use virtualbox on your machine
and act as a cloud.
For now this will only clone existing VMs. It's best to create a template
from which we will clone.
Followed
https://docs.saltstack.com/en/latest/topics/cloud/cloud.html#non-libcloud-based-modules
to create this.
Dicts provided by salt:
__opts__ : contains the options used to run Salt Cloud,
as well as a set of configuration and environment variables
"""
# Import python libs
import logging
# Import salt libs
import salt.config as config
import salt.utils.cloud
log = logging.getLogger(__name__)
# Import virtualbox libs
HAS_LIBS = False
try:
# This code assumes vboxapi.py from VirtualBox distribution
# being in PYTHONPATH, or installed system-wide
from vboxapi import VirtualBoxManager
HAS_LIBS = True
except ImportError:
VirtualBoxManager = None
log.error("Couldn't import VirtualBox API")
"""
The name salt will identify the lib by
"""
__virtualname__ = 'virtualbox'
_virtualboxManager = None
def __virtual__():
"""
This function determines whether or not
to make this cloud module available upon execution.
Most often, it uses get_configured_provider() to determine
if the necessary configuration has been set up.
It may also check for necessary imports decide whether to load the module.
In most cases, it will return a True or False value.
If the name of the driver used does not match the filename,
then that name should be returned instead of True.
@return True|False|str
"""
if not HAS_LIBS:
return False
if get_configured_provider() is False:
return False
# If the name of the driver used does not match the filename,
# then that name should be returned instead of True.
# return __virtualname__
return True
def get_configured_provider():
"""
Return the first configured instance.
"""
configured = config.is_provider_configured(
__opts__,
__active_provider_name__ or __virtualname__,
() # keys we need from the provider configuration
)
log.debug("First virtualbox configuration %s" % configured)
return configured
def create(vm_info):
"""
Creates a virtual machine from the given VM information.
This is what is used to request a virtual machine to be created by the
cloud provider, wait for it to become available,
and then (optionally) log in and install Salt on it.
Fires:
"starting create" : This event is tagged salt/cloud/<vm name>/creating.
The payload contains the names of the VM, profile and provider.
@param vm_info {dict}
{
name: <str>
profile: <dict>
provider: <provider>
clone_from: <vm_name>
}
@return dict of resulting vm. !!!Passwords can and should be included!!!
"""
log.debug("Creating virtualbox with %s" % vm_info)
try:
# Check for required profile parameters before sending any API calls.
# TODO should this be a call to config.is_provider_configured ?
if vm_info['profile'] and config.is_profile_configured(
__opts__,
__active_provider_name__ or 'virtualbox',
vm_info['profile']
) is False:
return False
except AttributeError:
pass
# For now we can only clone
if 'clone_from' not in vm_info:
log.error('"clone_from" not in profile configuration!')
return False
salt.utils.cloud.fire_event(
'event',
'starting create',
'salt/cloud/{0}/creating'.format(vm_info['name']),
{
'name': vm_info['name'],
'profile': vm_info['profile'],
'provider': vm_info['provider'],
},
transport=__opts__['transport']
)
# TODO Calculate kwargs with parameters required by virtualbox
# to create the virtual machine.
request_kwargs = {
'name': vm_info['name'],
'clone_from': vm_info['clone_from']
}
salt.utils.cloud.fire_event(
'event',
'requesting instance',
'salt/cloud/{0}/requesting'.format(vm_info['name']),
request_kwargs,
transport=__opts__['transport']
)
# TODO request a new VM!
vm_result = vb_clone_vm(**request_kwargs)
# TODO Prepare deployment of salt on the vm
# Any private data, including passwords and keys (including public keys)
# should be stripped from the deploy kwargs before the event is fired.
deploy_kwargs = {
}
salt.utils.cloud.fire_event(
'event',
'deploying salt',
'salt/cloud/{0}/deploying'.format(vm_info['name']),
deploy_kwargs,
transport=__opts__['transport']
)
deploy_kwargs.update({
# TODO Add private data
})
# TODO wait for target machine to become available
# TODO deploy!
# Do we have to call this?
salt.utils.cloud.deploy_script(None, **deploy_kwargs)
salt.utils.cloud.fire_event(
'event',
'created machine',
'salt/cloud/{0}/created'.format(vm_info['name']),
vm_result,
transport=__opts__['transport']
)
# Passwords should be included in this object!!
return vm_result
# -----------------------------
# Virtualbox methods
# -----------------------------
def vb_get_manager():
# This code initializes VirtualBox manager with default style
# and parameters
global _virtualboxManager
if _virtualboxManager is None:
_virtualboxManager = VirtualBoxManager(None, None)
vbox = _virtualboxManager.vbox
return vbox
def vb_create_machine(name=None):
vbox = vb_get_manager()
log.info("Create virtualbox machine %s " % (name,))
groups = None
os_type_id = "Other"
new_machine = vbox.createMachine(
None, # Settings file
name,
groups,
os_type_id,
None # flags
)
vbox.registerMachine(new_machine)
log.info("Finished creating %s" % name)
def vb_clone_vm(
name=None,
clone_from=None,
timeout=10000):
"""
Tells virtualbox to create a VM
@return dict of resulting VM
"""
vbox = vb_get_manager()
log.info("Clone virtualbox machine %s from %s" % (name, clone_from))
source_machine = vbox.findMachine(clone_from)
groups = None
osTypeId = "Other"
new_machine = vbox.createMachine(
None, # Settings file
name,
groups,
osTypeId,
None # flags
)
progress = source_machine.cloneTo(
new_machine,
0, # CloneMode
None # CloneOptions : None = Full?
)
progress.waitForCompletion(timeout)
log.info("Finished cloning %s from %s" % (name, clone_from))
vbox.registerMachine(new_machine)
# TODO return a struct/class that describes a virtual machine
def vb_start_vm(**kwargs):
"""
Tells Virtualbox to start up a VM.
Blocking function!
@return dict of started VM, contains IP addresses and what not
"""
pass
def vb_destroy_machine(name=None, timeout=10000):
"""
@param timeout int timeout in milliseconds
"""
vbox = vb_get_manager()
log.info("Destroying machine %s" % name)
machine = vbox.findMachine(name)
files = machine.unregister(2)
progress = machine.deleteConfig(files)
progress.waitForCompletion(timeout)
log.info("Finished destroying machine %s" % name)