Update PR based on review from @resmo

This commit is contained in:
Olaf Kilian 2015-05-06 22:28:36 +02:00 committed by Matt Clay
parent d23f78c799
commit 9d69e78f18

View file

@ -24,7 +24,7 @@ DOCUMENTATION = '''
--- ---
module: docker_login module: docker_login
author: Olaf Kilian author: Olaf Kilian
version_added: "1.9" version_added: "2.0"
short_description: Manage Docker registry logins short_description: Manage Docker registry logins
description: description:
- Ansible version of the "docker login" CLI command. - Ansible version of the "docker login" CLI command.
@ -35,50 +35,38 @@ options:
description: description:
- URL of the registry, for example: https://index.docker.io/v1/ - URL of the registry, for example: https://index.docker.io/v1/
required: true required: true
default: null
aliases: []
username: username:
description: description:
- The username for the registry account - The username for the registry account
required: true required: true
default: null
aliases: []
password: password:
description: description:
- The plaintext password for the registry account - The plaintext password for the registry account
required: true required: true
default: null
aliases: []
email: email:
description: description:
- The email address for the registry account - The email address for the registry account
required: false required: false
default: None
aliases: []
reauth: reauth:
description: description:
- Whether refresh existing authentication on the Docker server (boolean) - Whether refresh existing authentication on the Docker server (boolean)
required: false required: false
default: false default: false
aliases: []
dockercfg_path: dockercfg_path:
description: description:
- Use a custom path for the .dockercfg file - Use a custom path for the .dockercfg file
required: false required: false
default: ~/.dockercfg default: ~/.dockercfg
aliases: []
docker_url: docker_url:
descriptions: descriptions:
- Refers to the protocol+hostname+port where the Docker server is hosted - Refers to the protocol+hostname+port where the Docker server is hosted
required: false required: false
default: unix://var/run/docker.sock default: unix://var/run/docker.sock
aliases: []
timeout: timeout:
description: description:
- The HTTP request timeout in seconds - The HTTP request timeout in seconds
required: false required: false
default: 600 default: 600
aliases: []
requirements: [ "docker-py" ] requirements: [ "docker-py" ]
''' '''
@ -108,22 +96,24 @@ Login to a Docker registry without performing any other action. Make sure that t
''' '''
try:
import os.path import os.path
import sys import sys
import json import json
import base64 import base64
import docker.client
from requests.exceptions import *
from urlparse import urlparse from urlparse import urlparse
except ImportError, e:
print "failed=True msg='failed to import python module: %s'" % e
sys.exit(1)
try: try:
import docker.client
from docker.errors import APIError as DockerAPIError from docker.errors import APIError as DockerAPIError
except ImportError: has_lib_docker = True
from docker.client import APIError as DockerAPIError except ImportError, e:
has_lib_docker = False
try:
from requests.exceptions import *
has_lib_requests_execeptions = True
except ImportError, e:
has_lib_requests_execeptions = False
class DockerLoginManager: class DockerLoginManager:
@ -171,7 +161,7 @@ class DockerLoginManager:
self.log.append("Already Authentificated") self.log.append("Already Authentificated")
# Update the dockercfg if changed but not failed. # Update the dockercfg if changed but not failed.
if self.has_changed(): if self.has_changed() and not self.module.check_mode:
self.update_dockercfg() self.update_dockercfg()
# This is what the underlaying docker-py unfortunately doesn't do (yet). # This is what the underlaying docker-py unfortunately doesn't do (yet).
@ -218,16 +208,23 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
registry = dict(required=True, default=None), registry = dict(required=True),
username = dict(required=True, default=None), username = dict(required=True),
password = dict(required=True, default=None), password = dict(required=True),
email = dict(required=False, default=None), email = dict(required=False, default=None),
reauth = dict(required=False, default=False, type='bool'), reauth = dict(required=False, default=False, type='bool'),
dockercfg_path = dict(required=False, default='~/.dockercfg'), dockercfg_path = dict(required=False, default='~/.dockercfg'),
docker_url = dict(default='unix://var/run/docker.sock'), docker_url = dict(default='unix://var/run/docker.sock'),
timeout = dict(default=10, type='int') timeout = dict(default=10, type='int')
),
supports_check_mode=True
) )
)
if not has_lib_docker:
module.fail_json(msg="python library docker-py required: pip install docker-py==1.1.0")
if not has_lib_requests_execeptions:
module.fail_json(msg="python library requests required: pip install requests")
try: try:
manager = DockerLoginManager(module) manager = DockerLoginManager(module)