Changeset 2418 for package_branches


Ignore:
Timestamp:
Aug 9, 2009, 9:07:28 PM (15 years ago)
Author:
quentin
Message:

Make commands work from list and info pages

Location:
package_branches/invirt-web/cherrypy/code
Files:
1 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • package_branches/invirt-web/cherrypy/code/controls.py

    r2295 r2418  
    205205        raise
    206206
    207 def commandResult(username, state, fields):
     207def commandResult(username, state, command_name, machine_id, fields):
    208208    start_time = 0
    209     machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
    210     action = fields.getfirst('action')
    211     cdrom = fields.getfirst('cdrom')
     209    machine = validation.Validate(username, state, machine_id=machine_id).machine
     210    action = command_name
     211    cdrom = fields.get('cdrom')
     212    if not cdrom:
     213        cdrom = None
    212214    if cdrom is not None and not CDROM.query().filter_by(cdrom_id=cdrom).one():
    213215        raise CodeError("Invalid cdrom type '%s'" % cdrom)   
    214     if action not in ('Reboot', 'Power on', 'Power off', 'Shutdown',
    215                       'Delete VM'):
     216    if action not in "reboot create destroy shutdown delete".split(" "):
    216217        raise CodeError("Invalid action '%s'" % action)
    217     if action == 'Reboot':
     218    if action == 'reboot':
    218219        if cdrom is not None:
    219220            out, err = remctl('control', machine.name, 'reboot', cdrom,
     
    231232                raise CodeError('ERROR on remctl')
    232233               
    233     elif action == 'Power on':
     234    elif action == 'create':
    234235        if validation.maxMemory(username, state, machine) < machine.memory:
    235236            raise InvalidInput('action', 'Power on',
     
    237238                               "to turn on this machine.")
    238239        bootMachine(machine, cdrom)
    239     elif action == 'Power off':
     240    elif action == 'destroy':
    240241        out, err = remctl('control', machine.name, 'destroy', err=True)
    241242        if err:
     
    247248                print >> sys.stderr, err
    248249                raise CodeError('ERROR on remctl')
    249     elif action == 'Shutdown':
     250    elif action == 'shutdown':
    250251        out, err = remctl('control', machine.name, 'shutdown', err=True)
    251252        if err:
     
    257258                print >> sys.stderr, err
    258259                raise CodeError('ERROR on remctl')
    259     elif action == 'Delete VM':
     260    elif action == 'delete':
    260261        deleteVM(machine)
    261262
  • package_branches/invirt-web/cherrypy/code/main.py

    r2414 r2418  
    5959    @cherrypy.expose
    6060    @cherrypy.tools.mako(filename="/list.mako")
    61     def list(self):
     61    def list(self, result=None):
    6262        """Handler for list requests."""
    6363        checkpoint.checkpoint('Getting list dict')
    6464        d = getListDict(cherrypy.request.login, cherrypy.request.state)
     65        if result is not None:
     66            d['result'] = result
    6567        checkpoint.checkpoint('Got list dict')
    6668        return d
     
    215217                     authtoken=token)
    216218            return d
     219        @cherrypy.expose
     220        @cherrypy.tools.mako(filename="/command.mako")
     221        def command(self, command_name, machine_id, **kwargs):
     222            """Handler for running commands like boot and delete on a VM."""
     223            if cherrypy.request.method != "POST":
     224                raise InvalidInput("command_name", command_name, "That's not a valid command")
     225            back = kwargs.get('back', None)
     226            try:
     227                d = controls.commandResult(cherrypy.request.login, cherrypy.request.state, command_name, machine_id, kwargs)
     228                if d['command'] == 'Delete VM':
     229                    back = 'list'
     230            except InvalidInput, err:
     231                if not back:
     232                    raise
     233                print >> sys.stderr, err
     234                result = err
     235            else:
     236                result = 'Success!'
     237                if not back:
     238                    return d
     239            if back == 'list':
     240                cherrypy.request.state.clear() #Changed global state
     241                raise cherrypy.InternalRedirect('/list?result=%s' % urllib.quote(result))
     242            elif back == 'info':
     243                raise cherrypy.HTTPRedirect(cherrypy.request.base + '/machine/%d/' % machine_id, status=303)
     244            else:
     245                raise InvalidInput('back', back, 'Not a known back page.')
    217246
    218247    machine = MachineView()
     
    432461    return disk_fields
    433462
    434 def command(username, state, path, fields):
    435     """Handler for running commands like boot and delete on a VM."""
    436     back = fields.getfirst('back')
    437     try:
    438         d = controls.commandResult(username, state, fields)
    439         if d['command'] == 'Delete VM':
    440             back = 'list'
    441     except InvalidInput, err:
    442         if not back:
    443             raise
    444         print >> sys.stderr, err
    445         result = err
    446     else:
    447         result = 'Success!'
    448         if not back:
    449             return templates.command(searchList=[d])
    450     if back == 'list':
    451         state.clear() #Changed global state
    452         d = getListDict(username, state)
    453         d['result'] = result
    454         return templates.list(searchList=[d])
    455     elif back == 'info':
    456         machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
    457         return ({'Status': '303 See Other',
    458                  'Location': 'info?machine_id=%d' % machine.machine_id},
    459                 "You shouldn't see this message.")
    460     else:
    461         raise InvalidInput('back', back, 'Not a known back page.')
    462 
    463463def modifyDict(username, state, fields):
    464464    """Modify a machine as specified by CGI arguments.
     
    652652
    653653mapping = dict(
    654                command=command,
    655654               modify=modify,
    656655               create=create,
  • package_branches/invirt-web/cherrypy/code/templates/info.mako

    r2417 r2418  
    2929<%def name="command_button(title, value, cdrom=False, extra='')">
    3030<form action="machine/${machine.machine_id}/command/${value}" method="POST">
    31   <input type="hidden" name="back" value="machine/${machine.machine_id}/info" />
     31  <input type="hidden" name="back" value="info" />
    3232  <input type="submit" class="button" name="action" value="${title}" ${extra | n}/>
    3333% if cdrom:
  • package_branches/invirt-web/cherrypy/code/templates/list.mako

    r2416 r2418  
    8585      <tr>
    8686        <td rowspan="2">
    87           <form action="command" method="post">
     87          <form action="machine/${machine.machine_id}/command/${'shutdown' if machine.uptime else 'create'}" method="post">
    8888            <input type="hidden" name="back" value="list"/>
    8989            <input type="hidden" name="machine_id"
    9090                   value="${machine.machine_id}"/>
    91 <input type="submit" class="power ${'on' if machine.uptime else 'off'}" name="action" value="${'Power off' if machine.uptime else 'Power on'}"\
     91<input type="submit" class="power ${'on' if machine.uptime else 'off'}" name="action" value="${'Shutdown' if machine.uptime else 'Power on'}"\
    9292% if machine.uptime:
    9393 onclick="return confirm('Are you sure you want to power off this VM?');"
Note: See TracChangeset for help on using the changeset viewer.