Use httplib instead of requests_unixsocket

This commit is contained in:
Hiroaki Nakamura 2016-06-26 08:30:38 +09:00
parent 82302e80d4
commit 2ee5c04335

View file

@ -89,8 +89,6 @@ options:
- The unix domain socket path for the LXD server. - The unix domain socket path for the LXD server.
required: false required: false
default: /var/lib/lxd/unix.socket default: /var/lib/lxd/unix.socket
requirements:
- 'requests_unixsocket'
notes: notes:
- Containers must have a unique name. If you attempt to create a container - Containers must have a unique name. If you attempt to create a container
with a name that already existed in the users namespace the module will with a name that already existed in the users namespace the module will
@ -198,15 +196,43 @@ lxd_container:
sample: ["create", "start"] sample: ["create", "start"]
""" """
from ansible.compat.six.moves.urllib.parse import quote
try: try:
import requests_unixsocket import json
from requests.exceptions import ConnectionError
except ImportError: except ImportError:
HAS_REQUETS_UNIXSOCKET = False import simplejson as json
else:
HAS_REQUETS_UNIXSOCKET = True # httplib/http.client connection using unix domain socket
import socket
try:
import httplib
class UnixHTTPConnection(httplib.HTTPConnection):
def __init__(self, path, host='localhost', port=None, strict=None,
timeout=None):
httplib.HTTPConnection.__init__(self, host, port=port, strict=strict,
timeout=timeout)
self.path = path
def connect(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(self.path)
self.sock = sock
except ImportError:
# Python 3
import http.client
class UnixHTTPConnection(http.client.HTTPConnection):
def __init__(self, path, host='localhost', port=None,
timeout=None):
http.client.HTTPConnection.__init__(self, host, port=port,
timeout=timeout)
self.path = path
def connect(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(self.path)
self.sock = sock
# LXD_ANSIBLE_STATES is a map of states that contain values of methods used # LXD_ANSIBLE_STATES is a map of states that contain values of methods used
# when a particular state is evoked. # when a particular state is evoked.
@ -253,22 +279,19 @@ class LxdContainerManagement(object):
self.force_stop = self.module.params['force_stop'] self.force_stop = self.module.params['force_stop']
self.addresses = None self.addresses = None
self.unix_socket_path = self.module.params['unix_socket_path'] self.unix_socket_path = self.module.params['unix_socket_path']
self.base_url = 'http+unix://{0}'.format(quote(self.unix_socket_path, safe='')) self.connection = UnixHTTPConnection(self.unix_socket_path, timeout=self.timeout)
self.session = requests_unixsocket.Session()
self.logs = [] self.logs = []
self.actions = [] self.actions = []
def _path_to_url(self, path): def _send_request(self, method, url, body_json=None, ok_error_codes=None):
return self.base_url + path
def _send_request(self, method, path, json=None, ok_error_codes=None):
try: try:
url = self._path_to_url(path) body = json.dumps(body_json)
resp = self.session.request(method, url, json=json, timeout=self.timeout) self.connection.request(method, url, body=body)
resp_json = resp.json() resp = self.connection.getresponse()
resp_json = json.loads(resp.read())
self.logs.append({ self.logs.append({
'type': 'sent request', 'type': 'sent request',
'request': {'method': method, 'url': url, 'json': json, 'timeout': self.timeout}, 'request': {'method': method, 'url': url, 'json': body_json, 'timeout': self.timeout},
'response': {'json': resp_json} 'response': {'json': resp_json}
}) })
resp_type = resp_json.get('type', None) resp_type = resp_json.get('type', None)
@ -277,16 +300,16 @@ class LxdContainerManagement(object):
return resp_json return resp_json
self.module.fail_json( self.module.fail_json(
msg='error response', msg='error response',
request={'method': method, 'url': url, 'json': json, 'timeout': self.timeout}, request={'method': method, 'url': url, 'json': body_json, 'timeout': self.timeout},
response={'json': resp_json}, response={'json': resp_json},
logs=self.logs logs=self.logs
) )
return resp_json return resp_json
except ConnectionError: except socket.error:
self.module.fail_json(msg='cannot connect to the LXD server', unix_socket_path=self.unix_socket_path) self.module.fail_json(msg='cannot connect to the LXD server', unix_socket_path=self.unix_socket_path)
def _operate_and_wait(self, method, path, json=None): def _operate_and_wait(self, method, path, body_json=None):
resp_json = self._send_request(method, path, json=json) resp_json = self._send_request(method, path, body_json=body_json)
if resp_json['type'] == 'async': if resp_json['type'] == 'async':
path = '{0}/wait?timeout={1}'.format(resp_json['operation'], self.timeout) path = '{0}/wait?timeout={1}'.format(resp_json['operation'], self.timeout)
resp_json = self._send_request('GET', path) resp_json = self._send_request('GET', path)
@ -294,7 +317,7 @@ class LxdContainerManagement(object):
url = self._path_to_url(path) url = self._path_to_url(path)
self.module.fail_json( self.module.fail_json(
msg='error response for waiting opearation', msg='error response for waiting opearation',
request={'method': method, 'url': url, 'json': json, 'timeout': self.timeout}, request={'method': method, 'url': url, 'json': body_json, 'timeout': self.timeout},
response={'json': resp_json}, response={'json': resp_json},
logs=self.logs logs=self.logs
) )
@ -319,10 +342,10 @@ class LxdContainerManagement(object):
return ANSIBLE_LXD_STATES[resp_json['metadata']['status']] return ANSIBLE_LXD_STATES[resp_json['metadata']['status']]
def _change_state(self, action, force_stop=False): def _change_state(self, action, force_stop=False):
json={'action': action, 'timeout': self.timeout} body_json={'action': action, 'timeout': self.timeout}
if force_stop: if force_stop:
json['force'] = True body_json['force'] = True
return self._operate_and_wait('PUT', '/1.0/containers/{0}/state'.format(self.container_name), json) return self._operate_and_wait('PUT', '/1.0/containers/{0}/state'.format(self.container_name), body_json=body_json)
def _create_container(self): def _create_container(self):
config = self.config.copy() config = self.config.copy()
@ -464,24 +487,24 @@ class LxdContainerManagement(object):
def _apply_configs(self): def _apply_configs(self):
old_metadata = self.old_container_json['metadata'] old_metadata = self.old_container_json['metadata']
json = { body_json = {
'architecture': old_metadata['architecture'], 'architecture': old_metadata['architecture'],
'config': old_metadata['config'], 'config': old_metadata['config'],
'devices': old_metadata['devices'], 'devices': old_metadata['devices'],
'profiles': old_metadata['profiles'] 'profiles': old_metadata['profiles']
} }
if self._needs_to_change_config('architecture'): if self._needs_to_change_config('architecture'):
json['architecture'] = self.config['architecture'] body_json['architecture'] = self.config['architecture']
if self._needs_to_change_config('config'): if self._needs_to_change_config('config'):
for k, v in self.config['config'].items(): for k, v in self.config['config'].items():
json['config'][k] = v body_json['config'][k] = v
if self._needs_to_change_config('ephemeral'): if self._needs_to_change_config('ephemeral'):
json['ephemeral'] = self.config['ephemeral'] body_json['ephemeral'] = self.config['ephemeral']
if self._needs_to_change_config('devices'): if self._needs_to_change_config('devices'):
json['devices'] = self.config['devices'] body_json['devices'] = self.config['devices']
if self._needs_to_change_config('profiles'): if self._needs_to_change_config('profiles'):
json['profiles'] = self.config['profiles'] body_json['profiles'] = self.config['profiles']
self._operate_and_wait('PUT', '/1.0/containers/{0}'.format(self.container_name), json) self._operate_and_wait('PUT', '/1.0/containers/{0}'.format(self.container_name), body_json=body_json)
self.actions.append('apply_configs') self.actions.append('apply_configs')
def run(self): def run(self):
@ -541,11 +564,6 @@ def main():
supports_check_mode=False, supports_check_mode=False,
) )
if not HAS_REQUETS_UNIXSOCKET:
module.fail_json(
msg='The `requests_unixsocket` module is not importable. Check the requirements.'
)
lxd_manage = LxdContainerManagement(module=module) lxd_manage = LxdContainerManagement(module=module)
lxd_manage.run() lxd_manage.run()