VirtualBox

Changeset 28606 in vbox


Ignore:
Timestamp:
Apr 22, 2010 4:12:35 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
60453
Message:

Python shell: refactoring, more commands

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/vboxshell.py

    r28478 r28606  
    233233    mgr = ctx['mgr']
    234234    vb = ctx['vb']
    235     mach = vb.createMachine(name, kind, base, "")
     235    mach = vb.createMachine(name, kind, base, "", False)
    236236    mach.saveSettings()
    237237    print "created machine with UUID",mach.id
     
    245245    id = mach.id
    246246    print "removing machine ",mach.name,"with UUID",id
    247     session = ctx['global'].openMachineSession(id)
    248     try:
    249        mach = session.machine
    250        for d in ctx['global'].getArray(mach, 'mediumAttachments'):
    251           mach.detachDevice(d.controller, d.port, d.device)
    252     except:
    253        traceback.print_exc()
    254     mach.saveSettings()
    255     ctx['global'].closeMachineSession(session)
     247    cmdClosedVm(ctx, mach, detachVmDevice, ["ALL"])
    256248    mach = vb.unregisterMachine(id)
    257249    if mach:
     
    502494    session.close()
    503495
     496
     497def cmdClosedVm(ctx,mach,cmd,args=[],save=True):
     498    session = ctx['global'].openMachineSession(mach.id)
     499    mach = session.machine
     500    try:
     501         cmd(ctx, mach, args)
     502    except Exception, e:
     503        print 'failed: ',e
     504        if g_verbose:
     505            traceback.print_exc()
     506    if save:
     507         mach.saveSettings()
     508    session.close()
     509
    504510def machById(ctx,id):
    505511    mach = None
     
    556562    return "<unknown>"
    557563
     564def enumFromString(ctx,enum,str):
     565    all = ctx['ifaces'].all_values(enum)
     566    return all.get(str, None)
     567
    558568def listCmd(ctx, args):
    559569    for m in getMachines(ctx, True):
     
    575585    print " One can use setvar <mach> <var> <value> to change variable, using name in []."
    576586    print "  Name [name]: %s" %(mach.name)
     587    print "  Description [description]: %s" %(mach.description)
    577588    print "  ID [n/a]: %s" %(mach.id)
    578589    print "  OS Type [via OSTypeId]: %s" %(os.description)
     
    618629        print "  Controllers:"
    619630    for controller in controllers:
    620         print "    '%s' %s bus: %d" % (controller.name, asEnumElem(ctx,"StorageControllerType", controller.controllerType), controller.bus)
     631        print "    '%s': bus %s type %s" % (controller.name, asEnumElem(ctx,"StorageBus", controller.bus), asEnumElem(ctx,"StorageControllerType", controller.controllerType))
    621632
    622633    attaches = ctx['global'].getArray(mach, 'mediumAttachments')
     
    671682    return 0
    672683
    673 def createCmd(ctx, args):
     684def createVmCmd(ctx, args):
    674685    if (len(args) < 3 or len(args) > 4):
    675         print "usage: create name ostype <basefolder>"
     686        print "usage: createvm name ostype <basefolder>"
    676687        return 0
    677688    name = args[1]
     
    722733    return 0
    723734
    724 def removeCmd(ctx, args):
     735def removeVmCmd(ctx, args):
    725736    mach = argsToMach(ctx,args)
    726737    if mach == None:
     
    801812    return 0
    802813
     814def portalsettings(ctx,mach,args):
     815    enabled = args[0]
     816    mach.teleporterEnabled = enabled
     817    if enabled:
     818        port = args[1]
     819        passwd = args[2]
     820        mach.teleporterPort = port
     821        mach.teleporterPassword = passwd
     822
    803823def openportalCmd(ctx, args):
    804824    if (len(args) < 3):
     
    814834        passwd = ""
    815835    if not mach.teleporterEnabled or mach.teleporterPort != port or passwd:
    816         session = ctx['global'].openMachineSession(mach.id)
    817         mach1 = session.machine
    818         mach1.teleporterEnabled = True
    819         mach1.teleporterPort = port
    820         mach1.teleporterPassword = passwd
    821         mach1.saveSettings()
    822         session.close()
     836        cmdClosedVm(ctx, mach, portalsettings, [True, port, passwd])
    823837    startVm(ctx, mach, "gui")
    824838    return 0
     
    832846        return 0
    833847    if mach.teleporterEnabled:
    834         session = ctx['global'].openMachineSession(mach.id)
    835         mach1 = session.machine
    836         mach1.teleporterEnabled = False
    837         mach1.saveSettings()
    838         session.close()
     848        cmdClosedVm(ctx, mach, portalsettings, [False])
    839849    return 0
    840850
     
    849859    return 0
    850860
     861def plugcpu(ctx,mach,args):
     862    plug = args[0]
     863    cpu = args[1]
     864    if plug:
     865        print "Adding CPU %d..." %(cpu)
     866        mach.hotPlugCPU(cpu)
     867    else:
     868        print "Removing CPU %d..." %(cpu)
     869        mach.hotUnplugCPU(cpu)
     870
    851871def plugcpuCmd(ctx, args):
    852872    if (len(args) < 2):
     
    858878    if str(mach.sessionState) != str(ctx['ifaces'].SessionState_Open):
    859879        if mach.CPUHotPlugEnabled:
    860             session = ctx['global'].openMachineSession(mach.id)
    861             try:
    862                 mach1 = session.machine
    863                 cpu = int(args[2])
    864                 print "Adding CPU %d..." %(cpu)
    865                 mach1.hotPlugCPU(cpu)
    866                 mach1.saveSettings()
    867             except:
    868                 session.close()
    869                 raise
    870             session.close()
     880            cmdClosedVm(ctx, mach, plugcpu, [True, int(args[2])])
    871881    else:
    872882        cmdExistingVm(ctx, mach, 'plugcpu', args[2])
     
    882892    if str(mach.sessionState) != str(ctx['ifaces'].SessionState_Open):
    883893        if mach.CPUHotPlugEnabled:
    884             session = ctx['global'].openMachineSession(mach.id)
    885             try:
    886                 mach1 = session.machine
    887                 cpu = int(args[2])
    888                 print "Removing CPU %d..." %(cpu)
    889                 mach1.hotUnplugCPU(cpu)
    890                 mach1.saveSettings()
    891             except:
    892                 session.close()
    893                 raise
    894             session.close()
     894            cmdClosedVm(ctx, mach, plugcpu, [False, int(args[2])])
    895895    else:
    896896        cmdExistingVm(ctx, mach, 'unplugcpu', args[2])
    897897    return 0
     898
     899def setvar(ctx,mach,args):
     900    expr = 'mach.'+args[0]+' = '+args[1]
     901    print "Executing",expr
     902    exec expr
    898903
    899904def setvarCmd(ctx, args):
     
    904909    if mach == None:
    905910        return 0
    906     session = ctx['global'].openMachineSession(mach.id)
    907     mach = session.machine
    908     expr = 'mach.'+args[2]+' = '+args[3]
    909     print "Executing",expr
    910     try:
    911         exec expr
    912     except Exception, e:
    913         print 'failed: ',e
    914         if g_verbose:
    915             traceback.print_exc()
    916     mach.saveSettings()
    917     session.close()
    918     return 0
    919 
     911    cmdClosedVm(ctx, mach, setvar, args[2:])
     912    return 0
     913
     914def setvmextra(ctx,mach,args):
     915    key = args[0]
     916    value = args[1]
     917    print "%s: setting %s to %s" %(mach.name, key, value)
     918    mach.setExtraData(key, value)
    920919
    921920def setExtraDataCmd(ctx, args):
     
    935934    if mach == None:
    936935        return 0
    937     session = ctx['global'].openMachineSession(mach.id)
    938     mach = session.machine
    939     mach.setExtraData(key, value)
    940     mach.saveSettings()
    941     session.close()
     936    cmdClosedVm(ctx, mach, setvmextra, [key, value])
    942937    return 0
    943938
     
    966961        keys = [ key ]
    967962    for k in keys:
    968         printExtraKey(args[1], k, ctx['vb'].getExtraData(k))
     963        printExtraKey(args[1], k, obj.getExtraData(k))
    969964
    970965    return 0
     
    15371532   return 0
    15381533
     1534def controldevice(ctx,mach,args):
     1535    [ctr,port,slot,type,id] = args
     1536    mach.attachDevice(ctr, port, slot,type,id)
     1537
    15391538def attachHddCmd(ctx,args):
    15401539   if (len(args) < 4):
     
    15541553   ctr = args[3]
    15551554   (port,slot) = args[4].split(":")
    1556 
    1557    session = ctx['global'].openMachineSession(mach.id)
    1558    mach = session.machine
    1559    try:
    1560       mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_HardDisk, hdd.id)
    1561    except Exception, e:
    1562       print 'failed: ',e
    1563    mach.saveSettings()
    1564    session.close()
     1555   cmdClosedVm(ctx, mach, lambda ctx,mach,args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_HardDisk,hdd.id))
    15651556   return 0
    15661557
     1558def detachVmDevice(ctx,mach,args):
     1559    atts = ctx['global'].getArray(mach, 'mediumAttachments')
     1560    hid = args[0]
     1561    for a in atts:
     1562        if a.medium:
     1563            if hid == "ALL" or a.medium.id == hid:
     1564                mach.detachDevice(a.controller, a.port, a.device)
     1565
    15671566def detachMedium(ctx,mid,medium):
    1568    session = ctx['global'].openMachineSession(mid)
    1569    mach = session.machine
    1570    try:
    1571       atts = ctx['global'].getArray(mach, 'mediumAttachments')
    1572       hid = medium.id
    1573       for a in atts:
    1574          if a.medium and a.medium.id == hid:
    1575             mach.detachDevice(a.controller, a.port, a.device)
    1576    except Exception, e:
    1577       session.close()
    1578       raise e
    1579    mach.saveSettings()
    1580    session.close()
     1567    cmdClosedVm(ctx, mach, detachVmDevice, [medium.id])
    15811568
    15821569def detachHddCmd(ctx,args):
     
    16571644   return 0
    16581645
    1659 
    16601646def unregisterIsoCmd(ctx,args):
    16611647   if (len(args) != 2):
     
    17121698   ctr = args[3]
    17131699   (port,slot) = args[4].split(":")
    1714 
    1715    session = ctx['global'].openMachineSession(mach.id)
    1716    mach = session.machine
    1717    try:
    1718       mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_DVD, dvd.id)
    1719    except Exception, e:
    1720       print 'failed: ',e
    1721    mach.saveSettings()
    1722    session.close()
     1700   cmdClosedVm(ctx, mach, lambda ctx,mach,args: mach.attachDevice(ctr, port, slot, ctx['global'].constants.DeviceType_DVD,dvd.id))
    17231701   return 0
    17241702
     
    17421720   return 0
    17431721
    1744 
    17451722def mountIsoCmd(ctx,args):
    17461723   if (len(args) < 5):
     
    17821759
    17831760   return 0
     1761
     1762def attachCtr(ctx,mach,args):
     1763    [name, bus, type] = args
     1764    ctr = mach.addStorageController(name, bus)
     1765    if type != None:
     1766        ctr.controllerType = type
     1767
     1768def attachCtrCmd(ctx,args):
     1769   if (len(args) < 4):
     1770      print "usage: attachCtr vm cname bus <type>"
     1771      return 0
     1772
     1773   if len(args) > 4:
     1774       type = enumFromString(ctx,'StorageControllerType', args[4])
     1775       if type == None:
     1776           print "Controller type %s unknown" %(args[4])
     1777           return 0
     1778   else:
     1779       type = None
     1780
     1781   mach = ctx['machById'](args[1])
     1782   if mach is None:
     1783        return 0
     1784   bus = enumFromString(ctx,'StorageBus', args[3])
     1785   if bus is None:
     1786       print "Bus type %s unknown" %(args[3])
     1787       return 0
     1788   name = args[2]
     1789   cmdClosedVm(ctx, mach, attachCtr, [name, bus, type])
     1790   return 0
     1791
     1792def detachCtrCmd(ctx,args):
     1793   if (len(args) < 3):
     1794      print "usage: detachCtr vm name"
     1795      return 0
     1796
     1797   mach = ctx['machById'](args[1])
     1798   if mach is None:
     1799        return 0
     1800   ctr = args[2]
     1801   cmdClosedVm(ctx, mach, lambda ctx,mach,args: mach.removeStorageController(ctr))
     1802   return 0
     1803
    17841804
    17851805aliases = {'s':'start',
     
    17941814commands = {'help':['Prints help information', helpCmd, 0],
    17951815            'start':['Start virtual machine by name or uuid: start Linux', startCmd, 0],
    1796             'create':['Create virtual machine', createCmd, 0],
    1797             'remove':['Remove virtual machine', removeCmd, 0],
     1816            'createVm':['Create virtual machine: createVm macvm MacOS', createVmCmd, 0],
     1817            'removeVm':['Remove virtual machine', removeVmCmd, 0],
    17981818            'pause':['Pause virtual machine', pauseCmd, 0],
    17991819            'resume':['Resume virtual machine', resumeCmd, 0],
     
    18451865            'mountIso': ['Mount CD/DVD to the running VM: mountIso win /os.iso "IDE Controller" 0:1', mountIsoCmd, 0],
    18461866            'unmountIso': ['Unmount CD/DVD from running VM: unmountIso win "IDE Controller" 0:1', unmountIsoCmd, 0],
     1867            'attachCtr': ['Attach storage controller to the VM: attachCtr win Ctr0 IDE ICH6', attachCtrCmd, 0],
     1868            'detachCtr': ['Detach HDD from the VM: detachCtr win Ctr0', detachCtrCmd, 0],
    18471869            'listMediums': ['List mediums known to this VBox instance', listMediumsCmd, 0]
    18481870            }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette