Merge pull request #41 from rackerlabs/new_agent_hearbeat
RFR: New-Style Agent Heartbeat
This commit is contained in:
commit
c821fd7129
@ -67,6 +67,7 @@ class TeethAgentHeartbeater(threading.Thread):
|
|||||||
def __init__(self, agent):
|
def __init__(self, agent):
|
||||||
super(TeethAgentHeartbeater, self).__init__()
|
super(TeethAgentHeartbeater, self).__init__()
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
|
self.hardware = hardware.get_manager()
|
||||||
self.api = overlord_agent_api.APIClient(agent.api_url)
|
self.api = overlord_agent_api.APIClient(agent.api_url)
|
||||||
self.log = structlog.get_logger(api_url=agent.api_url)
|
self.log = structlog.get_logger(api_url=agent.api_url)
|
||||||
self.stop_event = threading.Event()
|
self.stop_event = threading.Event()
|
||||||
@ -87,8 +88,7 @@ class TeethAgentHeartbeater(threading.Thread):
|
|||||||
def do_heartbeat(self):
|
def do_heartbeat(self):
|
||||||
try:
|
try:
|
||||||
deadline = self.api.heartbeat(
|
deadline = self.api.heartbeat(
|
||||||
mac_addr=self.agent.get_agent_mac_addr(),
|
hardware_info=self.hardware.list_hardware_info(),
|
||||||
url=self.agent.get_agent_url(),
|
|
||||||
version=self.agent.version,
|
version=self.agent.version,
|
||||||
mode=self.agent.get_mode_name())
|
mode=self.agent.get_mode_name())
|
||||||
self.error_delay = self.initial_delay
|
self.error_delay = self.initial_delay
|
||||||
|
@ -15,12 +15,15 @@ limitations under the License.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
import collections
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import plumbum
|
import plumbum
|
||||||
import stevedore
|
import stevedore
|
||||||
import structlog
|
import structlog
|
||||||
|
|
||||||
|
from teeth_rest import encoding
|
||||||
|
|
||||||
_global_manager = None
|
_global_manager = None
|
||||||
|
|
||||||
|
|
||||||
@ -37,6 +40,22 @@ class HardwareSupport(object):
|
|||||||
SERVICE_PROVIDER = 3
|
SERVICE_PROVIDER = 3
|
||||||
|
|
||||||
|
|
||||||
|
class HardwareType(object):
|
||||||
|
MAC_ADDRESS = 'mac_address'
|
||||||
|
|
||||||
|
|
||||||
|
class HardwareInfo(encoding.Serializable):
|
||||||
|
def __init__(self, type, id):
|
||||||
|
self.type = type
|
||||||
|
self.id = id
|
||||||
|
|
||||||
|
def serialize(self, view):
|
||||||
|
return collections.OrderedDict([
|
||||||
|
('type', self.type),
|
||||||
|
('id', self.id),
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
class BlockDevice(object):
|
class BlockDevice(object):
|
||||||
def __init__(self, name, size, start_sector):
|
def __init__(self, name, size, start_sector):
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -66,6 +85,13 @@ class HardwareManager(object):
|
|||||||
def get_os_install_device(self):
|
def get_os_install_device(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def list_hardware_info(self):
|
||||||
|
hardware_info = []
|
||||||
|
for interface in self.list_network_interfaces():
|
||||||
|
hardware_info.append(HardwareInfo(HardwareType.MAC_ADDRESS,
|
||||||
|
interface.mac_address))
|
||||||
|
return hardware_info
|
||||||
|
|
||||||
|
|
||||||
class GenericHardwareManager(HardwareManager):
|
class GenericHardwareManager(HardwareManager):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -47,13 +47,11 @@ class APIClient(object):
|
|||||||
headers=request_headers,
|
headers=request_headers,
|
||||||
data=data)
|
data=data)
|
||||||
|
|
||||||
def heartbeat(self, mac_addr, url, mode, version):
|
def heartbeat(self, hardware_info, mode, version):
|
||||||
path = '/{api_version}/agents/{mac_addr}'.format(
|
path = '/{api_version}/agents'.format(api_version=self.api_version)
|
||||||
api_version=self.api_version,
|
|
||||||
mac_addr=mac_addr)
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'url': url,
|
'hardware': hardware_info,
|
||||||
'mode': mode,
|
'mode': mode,
|
||||||
'version': version,
|
'version': version,
|
||||||
}
|
}
|
||||||
|
@ -53,3 +53,17 @@ class TestGenericHardwareManager(unittest.TestCase):
|
|||||||
self.assertEqual(self.hardware.get_os_install_device(), '/dev/sdb')
|
self.assertEqual(self.hardware.get_os_install_device(), '/dev/sdb')
|
||||||
self.hardware._cmd.assert_called_once_with('blockdev')
|
self.hardware._cmd.assert_called_once_with('blockdev')
|
||||||
blockdev.assert_called_once_with('--report')
|
blockdev.assert_called_once_with('--report')
|
||||||
|
|
||||||
|
def test_list_hardwre_info(self):
|
||||||
|
self.hardware.list_network_interfaces = mock.Mock()
|
||||||
|
self.hardware.list_network_interfaces.return_value = [
|
||||||
|
hardware.NetworkInterface('eth0', '00:0c:29:8c:11:b1'),
|
||||||
|
hardware.NetworkInterface('eth1', '00:0c:29:8c:11:b2'),
|
||||||
|
]
|
||||||
|
|
||||||
|
hardware_info = self.hardware.list_hardware_info()
|
||||||
|
self.assertEqual(len(hardware_info), 2)
|
||||||
|
self.assertEqual(hardware_info[0].type, 'mac_address')
|
||||||
|
self.assertEqual(hardware_info[1].type, 'mac_address')
|
||||||
|
self.assertEqual(hardware_info[0].id, '00:0c:29:8c:11:b1')
|
||||||
|
self.assertEqual(hardware_info[1].id, '00:0c:29:8c:11:b2')
|
||||||
|
@ -21,6 +21,7 @@ import time
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from teeth_agent import errors
|
from teeth_agent import errors
|
||||||
|
from teeth_agent import hardware
|
||||||
from teeth_agent import overlord_agent_api
|
from teeth_agent import overlord_agent_api
|
||||||
|
|
||||||
API_URL = 'http://agent-api.overlord.example.org/'
|
API_URL = 'http://agent-api.overlord.example.org/'
|
||||||
@ -29,6 +30,12 @@ API_URL = 'http://agent-api.overlord.example.org/'
|
|||||||
class TestBaseTeethAgent(unittest.TestCase):
|
class TestBaseTeethAgent(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.api_client = overlord_agent_api.APIClient(API_URL)
|
self.api_client = overlord_agent_api.APIClient(API_URL)
|
||||||
|
self.hardware_info = [
|
||||||
|
hardware.HardwareInfo(hardware.HardwareType.MAC_ADDRESS,
|
||||||
|
'a:b:c:d'),
|
||||||
|
hardware.HardwareInfo(hardware.HardwareType.MAC_ADDRESS,
|
||||||
|
'0:1:2:3'),
|
||||||
|
]
|
||||||
|
|
||||||
def test_successful_heartbeat(self):
|
def test_successful_heartbeat(self):
|
||||||
expected_heartbeat_before = time.time() + 120
|
expected_heartbeat_before = time.time() + 120
|
||||||
@ -40,8 +47,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
self.api_client.session.request.return_value = response
|
self.api_client.session.request.return_value = response
|
||||||
|
|
||||||
heartbeat_before = self.api_client.heartbeat(
|
heartbeat_before = self.api_client.heartbeat(
|
||||||
url='http://1.2.3.4:9999/',
|
hardware_info=self.hardware_info,
|
||||||
mac_addr='a:b:c:d',
|
|
||||||
version='15',
|
version='15',
|
||||||
mode='STANDBY')
|
mode='STANDBY')
|
||||||
|
|
||||||
@ -49,13 +55,22 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
|
|
||||||
request_args = self.api_client.session.request.call_args[0]
|
request_args = self.api_client.session.request.call_args[0]
|
||||||
self.assertEqual(request_args[0], 'PUT')
|
self.assertEqual(request_args[0], 'PUT')
|
||||||
self.assertEqual(request_args[1], API_URL + 'v1/agents/a:b:c:d')
|
self.assertEqual(request_args[1], API_URL + 'v1/agents')
|
||||||
|
|
||||||
data = self.api_client.session.request.call_args[1]['data']
|
data = self.api_client.session.request.call_args[1]['data']
|
||||||
content = json.loads(data)
|
content = json.loads(data)
|
||||||
self.assertEqual(content['url'], 'http://1.2.3.4:9999/')
|
|
||||||
self.assertEqual(content['mode'], 'STANDBY')
|
self.assertEqual(content['mode'], 'STANDBY')
|
||||||
self.assertEqual(content['version'], '15')
|
self.assertEqual(content['version'], '15')
|
||||||
|
self.assertEqual(content['hardware'], [
|
||||||
|
{
|
||||||
|
'type': 'mac_address',
|
||||||
|
'id': 'a:b:c:d',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'mac_address',
|
||||||
|
'id': '0:1:2:3',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
def test_heartbeat_requests_exception(self):
|
def test_heartbeat_requests_exception(self):
|
||||||
self.api_client.session.request = mock.Mock()
|
self.api_client.session.request = mock.Mock()
|
||||||
@ -63,8 +78,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(errors.HeartbeatError,
|
self.assertRaises(errors.HeartbeatError,
|
||||||
self.api_client.heartbeat,
|
self.api_client.heartbeat,
|
||||||
url='http://1.2.3.4:9999/',
|
hardware_info=self.hardware_info,
|
||||||
mac_addr='a:b:c:d',
|
|
||||||
version='15',
|
version='15',
|
||||||
mode='STANDBY')
|
mode='STANDBY')
|
||||||
|
|
||||||
@ -75,8 +89,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(errors.HeartbeatError,
|
self.assertRaises(errors.HeartbeatError,
|
||||||
self.api_client.heartbeat,
|
self.api_client.heartbeat,
|
||||||
url='http://1.2.3.4:9999/',
|
hardware_info=self.hardware_info,
|
||||||
mac_addr='a:b:c:d',
|
|
||||||
version='15',
|
version='15',
|
||||||
mode='STANDBY')
|
mode='STANDBY')
|
||||||
|
|
||||||
@ -87,8 +100,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(errors.HeartbeatError,
|
self.assertRaises(errors.HeartbeatError,
|
||||||
self.api_client.heartbeat,
|
self.api_client.heartbeat,
|
||||||
url='http://1.2.3.4:9999/',
|
hardware_info=self.hardware_info,
|
||||||
mac_addr='a:b:c:d',
|
|
||||||
version='15',
|
version='15',
|
||||||
mode='STANDBY')
|
mode='STANDBY')
|
||||||
|
|
||||||
@ -101,7 +113,6 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(errors.HeartbeatError,
|
self.assertRaises(errors.HeartbeatError,
|
||||||
self.api_client.heartbeat,
|
self.api_client.heartbeat,
|
||||||
url='http://1.2.3.4:9999/',
|
hardware_info=self.hardware_info,
|
||||||
mac_addr='a:b:c:d',
|
|
||||||
version='15',
|
version='15',
|
||||||
mode='STANDBY')
|
mode='STANDBY')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user