2014-04-09 14:31:31 -07: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.
|
2014-01-07 14:14:58 -08:00
|
|
|
|
2014-01-16 18:32:44 -08:00
|
|
|
import json
|
2014-04-07 10:59:47 -07:00
|
|
|
|
2014-01-16 18:32:44 -08:00
|
|
|
import requests
|
2014-01-07 14:14:58 -08:00
|
|
|
|
2014-04-07 10:59:47 -07:00
|
|
|
from ironic_python_agent import backoff
|
2014-03-19 16:19:52 -07:00
|
|
|
from ironic_python_agent import encoding
|
|
|
|
from ironic_python_agent import errors
|
2014-03-26 10:57:25 -07:00
|
|
|
from ironic_python_agent.openstack.common import log
|
|
|
|
from ironic_python_agent.openstack.common import loopingcall
|
2014-01-07 14:14:58 -08:00
|
|
|
|
|
|
|
|
2014-04-07 10:59:47 -07:00
|
|
|
LOG = log.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-01-07 14:14:58 -08:00
|
|
|
class APIClient(object):
|
|
|
|
api_version = 'v1'
|
2014-04-09 17:09:51 -07:00
|
|
|
payload_version = '2'
|
2014-01-07 14:14:58 -08:00
|
|
|
|
2014-04-08 17:52:07 -07:00
|
|
|
def __init__(self, api_url, driver_name):
|
2014-01-07 14:14:58 -08:00
|
|
|
self.api_url = api_url.rstrip('/')
|
2014-04-08 17:52:07 -07:00
|
|
|
self.driver_name = driver_name
|
2014-06-16 15:46:01 -07:00
|
|
|
|
|
|
|
# Only keep alive a maximum of 2 connections to the API. More will be
|
|
|
|
# opened if they are needed, but they will be closed immediately after
|
|
|
|
# use.
|
|
|
|
adapter = requests.adapters.HTTPAdapter(pool_connections=2,
|
|
|
|
pool_maxsize=2)
|
2014-01-07 14:14:58 -08:00
|
|
|
self.session = requests.Session()
|
2014-06-16 15:46:01 -07:00
|
|
|
self.session.mount(self.api_url, adapter)
|
|
|
|
|
2014-03-17 10:58:39 -07:00
|
|
|
self.encoder = encoding.RESTJSONEncoder()
|
2014-03-26 10:57:25 -07:00
|
|
|
self.log = log.getLogger(__name__)
|
2014-01-07 14:14:58 -08:00
|
|
|
|
|
|
|
def _request(self, method, path, data=None):
|
|
|
|
request_url = '{api_url}{path}'.format(api_url=self.api_url, path=path)
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
data = self.encoder.encode(data)
|
|
|
|
|
|
|
|
request_headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
|
2014-01-08 14:32:44 -08:00
|
|
|
return self.session.request(method,
|
|
|
|
request_url,
|
|
|
|
headers=request_headers,
|
|
|
|
data=data)
|
2014-01-07 14:14:58 -08:00
|
|
|
|
2014-03-19 13:55:56 -07:00
|
|
|
def heartbeat(self, uuid, advertise_address):
|
2014-03-13 16:52:45 -07:00
|
|
|
path = '/{api_version}/nodes/{uuid}/vendor_passthru/heartbeat'.format(
|
|
|
|
api_version=self.api_version,
|
|
|
|
uuid=uuid
|
|
|
|
)
|
2014-03-18 15:24:20 -07:00
|
|
|
data = {
|
2014-03-19 13:55:56 -07:00
|
|
|
'agent_url': self._get_agent_url(advertise_address)
|
2014-03-18 15:24:20 -07:00
|
|
|
}
|
2014-01-07 14:14:58 -08:00
|
|
|
try:
|
2014-03-18 15:24:20 -07:00
|
|
|
response = self._request('POST', path, data=data)
|
2014-01-07 14:14:58 -08:00
|
|
|
except Exception as e:
|
|
|
|
raise errors.HeartbeatError(str(e))
|
|
|
|
|
2014-04-24 11:03:30 -07:00
|
|
|
if response.status_code != requests.codes.ACCEPTED:
|
2014-03-11 13:31:19 -07:00
|
|
|
msg = 'Invalid status code: {0}'.format(response.status_code)
|
2014-01-07 14:14:58 -08:00
|
|
|
raise errors.HeartbeatError(msg)
|
2014-01-07 17:37:38 -08:00
|
|
|
|
2014-03-26 10:57:25 -07:00
|
|
|
def lookup_node(self, hardware_info, timeout, starting_interval):
|
2014-04-07 10:59:47 -07:00
|
|
|
timer = backoff.BackOffLoopingCall(
|
2014-03-26 10:57:25 -07:00
|
|
|
self._do_lookup,
|
2014-04-07 10:59:47 -07:00
|
|
|
hardware_info=hardware_info)
|
|
|
|
try:
|
|
|
|
node_content = timer.start(starting_interval=starting_interval,
|
|
|
|
timeout=timeout).wait()
|
|
|
|
except backoff.LoopingCallTimeOut:
|
2014-03-26 10:57:25 -07:00
|
|
|
raise errors.LookupNodeError('Could not look up node info. Check '
|
|
|
|
'logs for details.')
|
|
|
|
return node_content
|
|
|
|
|
2014-04-07 10:59:47 -07:00
|
|
|
def _do_lookup(self, hardware_info):
|
2014-03-26 10:57:25 -07:00
|
|
|
"""The actual call to lookup a node. Should be called inside
|
2014-04-07 10:59:47 -07:00
|
|
|
loopingcall.BackOffLoopingCall.
|
2014-03-26 10:57:25 -07:00
|
|
|
"""
|
2014-04-08 17:52:07 -07:00
|
|
|
path = '/{api_version}/drivers/{driver}/vendor_passthru/lookup'.format(
|
|
|
|
api_version=self.api_version,
|
|
|
|
driver=self.driver_name
|
2014-03-18 12:45:36 -07:00
|
|
|
)
|
2014-03-26 10:57:25 -07:00
|
|
|
# This hardware won't be saved on the node currently, because of
|
|
|
|
# how driver_vendor_passthru is implemented (no node saving).
|
2014-03-13 16:52:45 -07:00
|
|
|
data = {
|
2014-04-08 13:16:46 -07:00
|
|
|
'version': self.payload_version,
|
|
|
|
'inventory': hardware_info
|
2014-03-13 16:52:45 -07:00
|
|
|
}
|
2014-01-16 18:32:44 -08:00
|
|
|
|
2014-03-26 10:57:25 -07:00
|
|
|
# Make the POST, make sure we get back normal data/status codes and
|
|
|
|
# content
|
2014-03-13 16:52:45 -07:00
|
|
|
try:
|
|
|
|
response = self._request('POST', path, data=data)
|
|
|
|
except Exception as e:
|
2014-03-26 10:57:25 -07:00
|
|
|
self.log.warning('POST failed: %s' % str(e))
|
2014-04-07 10:59:47 -07:00
|
|
|
return False
|
2014-01-16 18:32:44 -08:00
|
|
|
|
2014-04-24 11:00:28 -07:00
|
|
|
if response.status_code != requests.codes.OK:
|
2014-04-07 10:59:47 -07:00
|
|
|
self.log.warning('Invalid status code: %s' % response.status_code)
|
|
|
|
return False
|
2014-01-16 18:32:44 -08:00
|
|
|
|
|
|
|
try:
|
2014-03-13 16:52:45 -07:00
|
|
|
content = json.loads(response.content)
|
2014-01-16 18:32:44 -08:00
|
|
|
except Exception as e:
|
2014-03-26 10:57:25 -07:00
|
|
|
self.log.warning('Error decoding response: %s' % str(e))
|
2014-04-07 10:59:47 -07:00
|
|
|
return False
|
2014-03-13 16:52:45 -07:00
|
|
|
|
2014-03-26 10:57:25 -07:00
|
|
|
# Check for valid response data
|
2014-03-13 16:52:45 -07:00
|
|
|
if 'node' not in content or 'uuid' not in content['node']:
|
2014-03-26 10:57:25 -07:00
|
|
|
self.log.warning('Got invalid node data from the API: %s' %
|
|
|
|
content)
|
2014-04-07 10:59:47 -07:00
|
|
|
return False
|
2014-03-26 10:57:25 -07:00
|
|
|
|
2014-03-20 15:18:48 -07:00
|
|
|
if 'heartbeat_timeout' not in content:
|
2014-03-26 10:57:25 -07:00
|
|
|
self.log.warning('Got invalid heartbeat from the API: %s' %
|
|
|
|
content)
|
2014-04-07 10:59:47 -07:00
|
|
|
return False
|
2014-03-26 10:57:25 -07:00
|
|
|
|
|
|
|
# Got valid content
|
|
|
|
raise loopingcall.LoopingCallDone(retvalue=content)
|
2014-03-14 16:40:51 -07:00
|
|
|
|
2014-03-19 13:55:56 -07:00
|
|
|
def _get_agent_url(self, advertise_address):
|
|
|
|
return 'http://{0}:{1}'.format(advertise_address[0],
|
|
|
|
advertise_address[1])
|