92
92
print "%s: onShowWindow: %d" %(self.mach.name, winId)
95
def __init__(self, vbox):
95
def __init__(self, params):
97
self.isMscom = params[1]
99
100
def onMachineStateChange(self, id, state):
105
106
def onExtraDataCanChange(self, id, key, value):
106
107
print "onExtraDataCanChange: %s %s=>%s" %(id, key, value)
108
# Witty COM bridge thinks if someone wishes to return tuple, hresult
109
# is one of values we want to return
109
115
def onExtraDataChange(self, id, key, value):
110
116
print "onExtraDataChange: %s %s=>%s" %(id, key, value)
112
def onMediaRegistred(self, id, type, registred):
113
print "onMediaRegistred: %s" %(id)
118
def onMediaRegistered(self, id, type, registered):
119
print "onMediaRegistered: %s" %(id)
115
def onMachineRegistred(self, id, registred):
116
print "onMachineRegistred: %s" %(id)
121
def onMachineRegistered(self, id, registred):
122
print "onMachineRegistered: %s" %(id)
118
124
def onSessionStateChange(self, id, state):
119
125
print "onSessionStateChange: %s %d" %(id, state)
205
211
def split_no_quotes(s):
206
212
return shlex.split(s)
214
def progressBar(ctx,p,wait=1000):
216
while not p.completed:
217
print "%d %%\r" %(p.percent),
219
p.waitForCompletion(wait)
220
except KeyboardInterrupt:
208
223
def createVm(ctx,name,kind,base):
222
237
print "removing machine ",mach.name,"with UUID",id
223
238
session = ctx['global'].openMachineSession(id)
225
mach = session.Machine
240
mach = session.machine
226
241
for d in ctx['global'].getArray(mach, 'hardDiskAttachments'):
227
242
mach.detachHardDisk(d.controller, d.port, d.device)
229
244
traceback.print_exc()
230
246
ctx['global'].closeMachineSession(session)
231
247
mach = vb.unregisterMachine(id)
241
257
session = mgr.getSessionObject(vb)
243
259
progress = vb.openRemoteSession(session, uuid, type, "")
244
progress.waitForCompletion(-1)
260
progressBar(ctx, progress, 100)
245
261
completed = progress.completed
246
262
rc = int(progress.resultCode)
247
263
print "Completed:", completed, "rc:",hex(rc&0xffffffff)
302
318
console.unregisterCallback(cb)
305
def monitorVbox(ctx, dur):
321
def monitorVBox(ctx, dur):
307
cb = ctx['global'].createCallback('IVirtualBoxCallback', VBoxMonitor, vbox)
323
isMscom = (ctx['global'].type == 'MSCOM')
324
cb = ctx['global'].createCallback('IVirtualBoxCallback', VBoxMonitor, [vbox, isMscom])
308
325
vbox.registerCallback(cb)
310
327
# not infinity, but close enough
315
332
ctx['global'].waitForEvents(500)
316
333
# We need to catch all exceptions here, otherwise callback will never be unregistered
319
traceback.print_exc()
320
336
vbox.unregisterCallback(cb)
322
338
def cmdExistingVm(ctx,mach,cmd,args):
347
363
'stats': lambda: guestStats(ctx, mach),
348
364
'guest': lambda: guestExec(ctx, mach, console, args),
349
365
'monitorGuest': lambda: monitorGuest(ctx, mach, console, args),
350
'save': lambda: console.saveState().waitForCompletion(-1)
366
'save': lambda: progressBar(ctx,console.saveState())
382
398
print "Machine '%s' is unknown, use list command to find available machines" %(id)
401
def helpSingleCmd(cmd,h,sp):
403
spec = " [ext from "+sp+"]"
406
print " %s: %s%s" %(cmd,h,spec)
385
408
def helpCmd(ctx, args):
386
409
if len(args) == 1:
387
410
print "Help page:"
388
411
names = commands.keys()
391
print " ",i,":", commands[i][0]
414
helpSingleCmd(i, commands[i][0], commands[i][2])
393
c = commands.get(args[1], None)
417
c = commands.get(cmd)
395
print "Command '%s' not known" %(args[1])
419
print "Command '%s' not known" %(cmd)
397
print " ",args[1],":", c[0]
421
helpSingleCmd(cmd, c[0], c[2])
400
424
def listCmd(ctx, args):
689
714
cmdExistingVm(ctx, mach, 'monitorGuest', dur)
692
def monitorVboxCmd(ctx, args):
717
def monitorVBoxCmd(ctx, args):
693
718
if (len(args) > 2):
694
print "usage: monitorVbox (duration)"
719
print "usage: monitorVBox (duration)"
697
722
if len(args) > 1:
698
723
dur = float(args[1])
699
monitorVbox(ctx, dur)
724
monitorVBox(ctx, dur)
702
727
def getAdapterType(ctx, type):
782
807
def reloadExtCmd(ctx, args):
783
808
# maybe will want more args smartness
784
checkUserExtensions(ctx, commands, ctx['vb'].homeFolder)
809
checkUserExtensions(ctx, commands, getHomeFolder(ctx))
785
810
autoCompletion(commands, ctx)
909
def exportVMCmd(ctx, args):
913
print "usage: exportVm <machine> <path> <format> <license>"
915
mach = ctx['machById'](args[1])
928
app = ctx['vb'].createAppliance()
929
desc = mach.export(app)
930
desc.addDescription(ctx['global'].constants.VirtualSystemDescriptionType_License, license, "")
931
p = app.write(format, path)
933
print "Exported to %s in format %s" %(path, format)
884
936
aliases = {'s':'start',
889
941
'q':'quit', 'exit':'quit',
893
944
commands = {'help':['Prints help information', helpCmd, 0],
894
945
'start':['Start virtual machine by name or uuid', startCmd, 0],
910
961
'host':['Show host information', hostCmd, 0],
911
962
'guest':['Execute command for guest: guest Win32 \'console.mouse.putMouseEvent(20, 20, 0, 0)\'', guestCmd, 0],
912
963
'monitorGuest':['Monitor what happens with the guest for some time: monitorGuest Win32 10', monitorGuestCmd, 0],
913
'monitorVbox':['Monitor what happens with Virtual Box for some time: monitorVbox 10', monitorVboxCmd, 0],
964
'monitorVBox':['Monitor what happens with Virtual Box for some time: monitorVBox 10', monitorVBoxCmd, 0],
914
965
'portForward':['Setup permanent port forwarding for a VM, takes adapter number host port and guest port: portForward Win32 0 8080 80', portForwardCmd, 0],
915
966
'showLog':['Show log file of the VM, : showLog Win32', showLogCmd, 0],
916
967
'reloadExt':['Reload custom extensions: reloadExt', reloadExtCmd, 0],
917
968
'runScript':['Run VBox script: runScript script.vbox', runScriptCmd, 0],
918
969
'sleep':['Sleep for specified number of seconds: sleep 3.14159', sleepCmd, 0],
919
'shell':['Execute external shell command: shell "ls /etc/rc*"', shellCmd, 0]
970
'shell':['Execute external shell command: shell "ls /etc/rc*"', shellCmd, 0],
971
'exportVm':['Export VM in OVF format: export Win /tmp/win.ovf', exportVMCmd, 0]
922
974
def runCommandArgs(ctx, args):
948
1000
# 'test': ['Test help', runTestCmd]
950
1002
# and issue reloadExt shell command.
951
# This file also will be read automatically on startup.
1003
# This file also will be read automatically on startup or 'reloadExt'.
953
def checkUserExtensions(ctx, cmds, folder):
954
name = os.path.join(str(folder), "shellext.py")
955
if not os.path.isfile(name):
1005
# Also one can put shell extensions into ~/.VirtualBox/shexts and
1006
# they will also be picked up, so this way one can exchange
1007
# shell extensions easily.
1008
def addExtsFromFile(ctx, cmds, file):
1009
if not os.path.isfile(file):
1013
execfile(file, d, d)
960
1014
for (k,v) in d['commands'].items():
962
1016
print "customize: adding \"%s\" - %s" %(k, v[0])
963
cmds[k] = [v[0], v[1], 1]
1017
cmds[k] = [v[0], v[1], file]
965
print "Error loading user extensions:"
1019
print "Error loading user extensions from %s" %(file)
966
1020
traceback.print_exc()
1023
def checkUserExtensions(ctx, cmds, folder):
1024
folder = str(folder)
1025
name = os.path.join(folder, "shellext.py")
1026
addExtsFromFile(ctx, cmds, name)
1027
# also check 'exts' directory for all files
1028
shextdir = os.path.join(folder, "shexts")
1029
if not os.path.isdir(shextdir):
1031
exts = os.listdir(shextdir)
1033
addExtsFromFile(ctx, cmds, os.path.join(shextdir,e))
1035
def getHomeFolder(ctx):
1036
if ctx['remote'] or ctx['vb'] is None:
1037
return os.path.join(os.path.expanduser("~"), ".VirtualBox")
1039
return ctx['vb'].homeFolder
968
1041
def interpret(ctx):
969
1042
if ctx['remote']:
970
1043
commands['connect'] = ["Connect to remote VBox instance", connectCmd, 0]
975
1048
if vbox is not None:
976
1049
print "Running VirtualBox version %s" %(vbox.version)
977
ctx['perf'] = ctx['global'].getPerfCollector(ctx['vb'])
978
home = vbox.homeFolder
1050
ctx['perf'] = ctx['global'].getPerfCollector(vbox)
980
1052
ctx['perf'] = None
981
home = os.path.join(os.path.expanduser("~"), ".VirtualBox")
1054
home = getHomeFolder(ctx)
983
1055
checkUserExtensions(ctx, commands, home)
985
1057
autoCompletion(commands, ctx)
1048
1120
'type':g_virtualBoxManager.type,
1049
1121
'run': lambda cmd,args: runCommandCb(ctx, cmd, args),
1050
1122
'machById': lambda id: machById(ctx,id),
1123
'progressBar': lambda p: progressBar(ctx,p),
1124
'progressBar': lambda p: progressBar(ctx,p),
1051
1125
'_machlist':None