begin on command execution

This commit is contained in:
Russell Haering 2013-12-18 15:47:48 -08:00
parent a58e837e59
commit 2f5dcb896b
2 changed files with 54 additions and 15 deletions
teeth_agent

@ -17,11 +17,11 @@ limitations under the License.
from collections import OrderedDict from collections import OrderedDict
import time import time
from pkg_resources import get_distribution import pkg_resources
from teeth_rest.encoding import Serializable from teeth_rest import encoding
from werkzeug.serving import run_simple from werkzeug import serving
from teeth_agent.api import TeethAgentAPIServer from teeth_agent import api
class TeethAgentOperationModes(object): class TeethAgentOperationModes(object):
@ -34,7 +34,7 @@ class TeethAgentOperationModes(object):
raise RuntimeError('Invalid mode: {}'.format(mode)) raise RuntimeError('Invalid mode: {}'.format(mode))
class TeethAgentStatus(Serializable): class TeethAgentStatus(encoding.Serializable):
def __init__(self, mode, started_at, version): def __init__(self, mode, started_at, version):
self.mode = mode self.mode = mode
self.started_at = started_at self.started_at = started_at
@ -58,7 +58,7 @@ class TeethAgent(object):
self.listen_port = listen_port self.listen_port = listen_port
self.started_at = None self.started_at = None
self.mode = mode self.mode = mode
self.api = TeethAgentAPIServer(self) self.api = api.TeethAgentAPIServer(self)
def get_status(self): def get_status(self):
""" """
@ -67,9 +67,15 @@ class TeethAgent(object):
return TeethAgentStatus( return TeethAgentStatus(
mode=self.mode, mode=self.mode,
started_at=self.started_at, started_at=self.started_at,
version=get_distribution('teeth-agent').version version=pkg_resources.get_distribution('teeth-agent').version
) )
def execute_command(self, command):
"""
Execute an agent command.
"""
pass
def run(self): def run(self):
""" """
Run the Teeth Agent. Run the Teeth Agent.
@ -78,4 +84,4 @@ class TeethAgent(object):
raise RuntimeError('Agent was already started') raise RuntimeError('Agent was already started')
self.started_at = time.time() self.started_at = time.time()
run_simple(self.listen_host, self.listen_port, self.api) serving.run_simple(self.listen_host, self.listen_port, self.api)

@ -14,13 +14,36 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from teeth_rest.component import APIComponent, APIServer from collections import OrderedDict
from teeth_rest.responses import (
ItemResponse from teeth_rest import component, encoding, errors, responses
)
class TeethAgentAPI(APIComponent): class AgentCommand(encoding.Serializable):
def __init__(self, name, params):
self.name = name
self.params = params
@classmethod
def deserialize(cls, obj):
if 'name' not in obj:
raise errors.InvalidContentError('Missing command \'name\' field.')
if 'params' not in obj:
raise errors.InvalidContentError('Missing command \'params\' field.')
return cls(obj['name'], obj['params'])
def serialize(self, view):
"""
Turn a command into a dictionary.
"""
return OrderedDict([
('name', self.name),
('params', self.params),
])
class TeethAgentAPI(component.APIComponent):
""" """
The primary Teeth Agent API. The primary Teeth Agent API.
""" """
@ -34,15 +57,25 @@ class TeethAgentAPI(APIComponent):
Called during initialization. Override to map relative routes to methods. Called during initialization. Override to map relative routes to methods.
""" """
self.route('GET', '/status', self.get_agent_status) self.route('GET', '/status', self.get_agent_status)
self.route('POST', '/command', self.execute_agent_command)
def get_agent_status(self, request): def get_agent_status(self, request):
""" """
Get the status of the agent. Get the status of the agent.
""" """
return ItemResponse(self.agent.get_status()) return responses.ItemResponse(self.agent.get_status())
def execute_agent_command(self, request):
"""
Execute a command on the agent.
"""
command = AgentCommand.deserialize(self.parse_content(request))
self.agent.execute_command(command)
# TODO(russellhaering): implement actual responses
return responses.ItemResponse({'result': 'success'})
class TeethAgentAPIServer(APIServer): class TeethAgentAPIServer(component.APIServer):
""" """
Server for the teeth agent API. Server for the teeth agent API.
""" """