web: hosts: Add WOL sender endpoint

master
Dustin 2016-01-03 10:14:45 -06:00
parent 491b8ed491
commit e5315a6792
3 changed files with 31 additions and 1 deletions

View File

@ -83,6 +83,7 @@ class Application(milla.Application):
r.add_route('/', hosts.HostListController()) r.add_route('/', hosts.HostListController())
r.add_route('/api-doc', swagger.SwaggerController()) r.add_route('/api-doc', swagger.SwaggerController())
r.add_route('/{host_id}/wake', hosts.WakeController())
r.add_route('/{host_id}', hosts.HostController()) r.add_route('/{host_id}', hosts.HostController())
def make_request(self, environ): def make_request(self, environ):

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from . import controllers, model from . import controllers, model
from .. import host
import logging import logging
import milla import milla
import six import six
@ -61,7 +62,7 @@ class HostListController(controllers.BaseController):
return response return response
class HostController(controllers.BaseController): class HostMixin(object):
@staticmethod @staticmethod
def get_host(db, host_id): def get_host(db, host_id):
@ -74,6 +75,9 @@ class HostController(controllers.BaseController):
raise milla.HTTPNotFound raise milla.HTTPNotFound
return host return host
class HostController(controllers.BaseController, HostMixin):
def GET(self, request, host_id): def GET(self, request, host_id):
host = self.get_host(request.db, host_id) host = self.get_host(request.db, host_id)
@ -110,3 +114,17 @@ class HostController(controllers.BaseController):
response = request.ResponseClass() response = request.ResponseClass()
response.status_int = milla.HTTPNoContent.code response.status_int = milla.HTTPNoContent.code
return response return response
class WakeController(controllers.BaseController, HostMixin):
def POST(self, request, host_id):
h = self.get_host(request.db, host_id)
m = host.MagicPacket(h.macaddr)
log.debug('Sending WOL magic packet to {}'.format(m.macaddr))
m.send()
response = request.ResponseClass()
response.status_int = milla.HTTPNoContent.code
return response

View File

@ -100,6 +100,17 @@ paths:
description: Host not found description: Host not found
tags: tags:
- hosts - hosts
/{host_id}/wake:
post:
description: >-
Send Wake-on-LAN magic packet to a host
parameters:
- $ref: '#/parameters/host_id'
responses:
204:
description: 'Magic packet sent'
tags:
- hosts
parameters: parameters:
host_id: host_id:
name: host_id name: host_id