diff --git a/src/rouse/web/__init__.py b/src/rouse/web/__init__.py index 8d61ae3..d2d3c32 100644 --- a/src/rouse/web/__init__.py +++ b/src/rouse/web/__init__.py @@ -83,6 +83,7 @@ class Application(milla.Application): r.add_route('/', hosts.HostListController()) r.add_route('/api-doc', swagger.SwaggerController()) + r.add_route('/{host_id}/wake', hosts.WakeController()) r.add_route('/{host_id}', hosts.HostController()) def make_request(self, environ): diff --git a/src/rouse/web/hosts.py b/src/rouse/web/hosts.py index 96b6e95..8d2aab2 100644 --- a/src/rouse/web/hosts.py +++ b/src/rouse/web/hosts.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals from . import controllers, model +from .. import host import logging import milla import six @@ -61,7 +62,7 @@ class HostListController(controllers.BaseController): return response -class HostController(controllers.BaseController): +class HostMixin(object): @staticmethod def get_host(db, host_id): @@ -74,6 +75,9 @@ class HostController(controllers.BaseController): raise milla.HTTPNotFound return host + +class HostController(controllers.BaseController, HostMixin): + def GET(self, request, host_id): host = self.get_host(request.db, host_id) @@ -110,3 +114,17 @@ class HostController(controllers.BaseController): response = request.ResponseClass() response.status_int = milla.HTTPNoContent.code 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 diff --git a/src/rouse/web/swagger.yml b/src/rouse/web/swagger.yml index dbfae5a..48be5e0 100644 --- a/src/rouse/web/swagger.yml +++ b/src/rouse/web/swagger.yml @@ -100,6 +100,17 @@ paths: description: Host not found tags: - 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: host_id: name: host_id