Several cleanups to many modules:

* Fix docs to specify when python2.6+ is required (due to a library
  dep).  This helps us know when it is okay to use python2.6+ syntax in
  the file.
* remove BabyJson returns.  See #1211  This commit fixes all but the
  openstack modules.
* Use if __name__ == '__main__' to only run the main part of the module
  if the module is run as a program.  This allows for the potential to
  unittest the code later.
This commit is contained in:
Toshio Kuratomi
2015-05-11 12:15:53 -07:00
committed by Matt Clay
parent 0567404c03
commit 5336217649
17 changed files with 186 additions and 114 deletions

View File

@@ -261,7 +261,10 @@ options:
version_added: "1.9"
author: Cove Schneider, Joshua Conner, Pavel Antonov, Ash Wilson
requirements: [ "docker-py >= 0.3.0", "docker >= 0.10.0" ]
requirements:
- "python >= 2.6"
- "docker-py >= 0.3.0"
- "The docker server >= 0.10.0"
'''
EXAMPLES = '''
@@ -400,8 +403,7 @@ def _human_to_bytes(number):
return int(number[:-len(each)]) * (1024 ** i)
i = i + 1
print "failed=True msg='Could not convert %s to integer'" % (number)
sys.exit(1)
raise ValueError('Could not convert %s to integer' % (number,))
def _ansible_facts(container_list):
@@ -912,7 +914,11 @@ class DockerManager(object):
# MEM_LIMIT
expected_mem = _human_to_bytes(self.module.params.get('memory_limit'))
try:
expected_mem = _human_to_bytes(self.module.params.get('memory_limit'))
except ValueError as e:
self.module.fail_json(msg=str(e))
actual_mem = container['Config']['Memory']
if expected_mem and actual_mem != expected_mem:
@@ -1205,11 +1211,16 @@ class DockerManager(object):
self.module.fail_json(msg="Failed to pull the specified image: %s" % resource, error=repr(e))
def create_containers(self, count=1):
try:
mem_limit = _human_to_bytes(self.module.params.get('memory_limit'))
except ValueError as e:
self.module.fail_json(msg=str(e))
params = {'image': self.module.params.get('image'),
'command': self.module.params.get('command'),
'ports': self.exposed_ports,
'volumes': self.volumes,
'mem_limit': _human_to_bytes(self.module.params.get('memory_limit')),
'mem_limit': mem_limit,
'environment': self.env,
'hostname': self.module.params.get('hostname'),
'domainname': self.module.params.get('domainname'),

View File

@@ -72,7 +72,10 @@ options:
required: false
default: 600
aliases: []
requirements: [ "docker-py" ]
requirements:
- "python >= 2.6"
- "docker-py"
- "requests"
'''
EXAMPLES = '''
@@ -102,21 +105,31 @@ Remove image from local docker storage:
'''
try:
import sys
import re
import json
import docker.client
from requests.exceptions import *
from urlparse import urlparse
except ImportError, e:
print "failed=True msg='failed to import python module: %s'" % e
sys.exit(1)
import re
from urlparse import urlparse
try:
from docker.errors import APIError as DockerAPIError
import json
except ImportError:
from docker.client import APIError as DockerAPIError
import simplejson as json
try:
from requests.exceptions import *
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
try:
import docker.client
HAS_DOCKER_CLIENT = True
except ImportError:
HAS_DOCKER_CLIENT = False
if HAS_DOCKER_CLIENT:
try:
from docker.errors import APIError as DockerAPIError
except ImportError:
from docker.client import APIError as DockerAPIError
class DockerImageManager:
@@ -209,6 +222,10 @@ def main():
timeout = dict(default=600, type='int'),
)
)
if not HAS_DOCKER_CLIENT:
module.fail_json(msg='docker-py is needed for this module')
if not HAS_REQUESTS:
module.fail_json(msg='requests is needed for this module')
try:
manager = DockerImageManager(module)
@@ -245,8 +262,8 @@ def main():
except RequestException as e:
module.exit_json(failed=True, changed=manager.has_changed(), msg=repr(e))
# import module snippets
from ansible.module_utils.basic import *
main()
if __name__ == '__main__':
main()