2013-12-17 15:13:30 -08:00
|
|
|
"""
|
|
|
|
Copyright 2013 Rackspace, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
"""
|
|
|
|
|
2013-12-19 16:59:16 -08:00
|
|
|
import collections
|
2013-12-17 15:13:30 -08:00
|
|
|
import time
|
2014-01-02 13:17:15 -08:00
|
|
|
import uuid
|
2013-12-17 15:13:30 -08:00
|
|
|
|
2013-12-18 15:47:48 -08:00
|
|
|
import pkg_resources
|
|
|
|
from teeth_rest import encoding
|
2014-01-02 13:17:15 -08:00
|
|
|
from teeth_rest import errors as rest_errors
|
2013-12-18 15:47:48 -08:00
|
|
|
from werkzeug import serving
|
2013-12-17 15:13:30 -08:00
|
|
|
|
2013-12-18 15:47:48 -08:00
|
|
|
from teeth_agent import api
|
2013-12-21 17:22:09 -08:00
|
|
|
from teeth_agent import errors
|
2013-12-17 15:13:30 -08:00
|
|
|
|
|
|
|
|
2013-12-18 15:47:48 -08:00
|
|
|
class TeethAgentStatus(encoding.Serializable):
|
2013-12-17 15:13:30 -08:00
|
|
|
def __init__(self, mode, started_at, version):
|
|
|
|
self.mode = mode
|
|
|
|
self.started_at = started_at
|
|
|
|
self.version = version
|
|
|
|
|
|
|
|
def serialize(self, view):
|
2013-12-19 16:59:16 -08:00
|
|
|
"""Turn the status into a dict."""
|
|
|
|
return collections.OrderedDict([
|
2013-12-17 15:13:30 -08:00
|
|
|
('mode', self.mode),
|
|
|
|
('started_at', self.started_at),
|
|
|
|
('version', self.version),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2014-01-02 13:17:15 -08:00
|
|
|
class AgentCommandStatus(object):
|
|
|
|
RUNNING = 'RUNNING'
|
|
|
|
SUCCEEDED = 'SUCCEEDED'
|
|
|
|
FAILED = 'FAILED'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseCommandResult(encoding.Serializable):
|
|
|
|
def __init__(self, command_name, command_params):
|
|
|
|
self.id = str(uuid.uuid4())
|
|
|
|
self.command_name = command_name
|
|
|
|
self.command_params = command_params
|
|
|
|
self.command_status = AgentCommandStatus.RUNNING
|
|
|
|
self.command_error = None
|
|
|
|
self.command_result = None
|
|
|
|
|
|
|
|
def serialize(self, view):
|
|
|
|
return collections.OrderedDict([
|
|
|
|
('id', self.id),
|
|
|
|
('command_name', self.command_name),
|
|
|
|
('command_params', self.command_params),
|
|
|
|
('command_status', self.command_status),
|
|
|
|
('command_error', self.command_error),
|
|
|
|
('command_result', self.command_result),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class SyncCommandResult(BaseCommandResult):
|
|
|
|
def __init__(self, command_name, command_params, success, result_or_error):
|
|
|
|
super(SyncCommandResult, self).__init__(command_name,
|
|
|
|
command_params)
|
|
|
|
if success:
|
|
|
|
self.command_status = AgentCommandStatus.SUCCEEDED
|
|
|
|
self.command_result = result_or_error
|
|
|
|
else:
|
|
|
|
self.command_status = AgentCommandStatus.FAILED
|
|
|
|
self.command_error = result_or_error
|
|
|
|
|
|
|
|
|
2013-12-20 12:57:38 -08:00
|
|
|
class BaseTeethAgent(object):
|
2013-12-17 15:13:30 -08:00
|
|
|
def __init__(self, listen_host, listen_port, mode):
|
|
|
|
self.listen_host = listen_host
|
|
|
|
self.listen_port = listen_port
|
|
|
|
self.started_at = None
|
|
|
|
self.mode = mode
|
2013-12-18 15:47:48 -08:00
|
|
|
self.api = api.TeethAgentAPIServer(self)
|
2014-01-02 13:17:15 -08:00
|
|
|
self.command_results = {}
|
2013-12-21 17:22:09 -08:00
|
|
|
self.command_map = {}
|
2013-12-17 15:13:30 -08:00
|
|
|
|
|
|
|
def get_status(self):
|
2013-12-19 16:59:16 -08:00
|
|
|
"""Retrieve a serializable status."""
|
2013-12-17 15:13:30 -08:00
|
|
|
return TeethAgentStatus(
|
|
|
|
mode=self.mode,
|
|
|
|
started_at=self.started_at,
|
2013-12-18 15:47:48 -08:00
|
|
|
version=pkg_resources.get_distribution('teeth-agent').version
|
2013-12-17 15:13:30 -08:00
|
|
|
)
|
|
|
|
|
2013-12-20 16:03:30 -08:00
|
|
|
def execute_command(self, command_name, **kwargs):
|
2013-12-19 16:59:16 -08:00
|
|
|
"""Execute an agent command."""
|
2013-12-21 17:22:09 -08:00
|
|
|
if command_name not in self.command_map:
|
|
|
|
raise errors.InvalidCommandError(command_name)
|
|
|
|
|
2014-01-02 13:17:15 -08:00
|
|
|
try:
|
|
|
|
result = self.command_map[command_name](command_name, **kwargs)
|
|
|
|
if not isinstance(result, BaseCommandResult):
|
|
|
|
result = SyncCommandResult(command_name, kwargs, True, result)
|
|
|
|
except rest_errors.ValidationError as e:
|
|
|
|
# Any command may raise a ValidationError which will be returned
|
|
|
|
# to the caller directly.
|
|
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
# Other errors are considered command execution errors, and are
|
|
|
|
# recorded as an
|
|
|
|
result = SyncCommandResult(command_name, kwargs, False, e)
|
|
|
|
|
|
|
|
self.command_results[result.id] = result
|
2013-12-27 15:06:29 -08:00
|
|
|
return result
|
|
|
|
|
2013-12-17 15:13:30 -08:00
|
|
|
def run(self):
|
2013-12-19 16:59:16 -08:00
|
|
|
"""Run the Teeth Agent."""
|
2013-12-17 15:13:30 -08:00
|
|
|
if self.started_at:
|
|
|
|
raise RuntimeError('Agent was already started')
|
|
|
|
|
|
|
|
self.started_at = time.time()
|
2013-12-18 15:47:48 -08:00
|
|
|
serving.run_simple(self.listen_host, self.listen_port, self.api)
|