Repository: ansible/ansible-modules-core

Issue:
Docker pull failing with Docker 1.8.3 and legacy docker registries.  
Docker: 1.8.3
Ansible: 1.9.4-1
Docker-py: 1.5.0

When trying to pull images from a legacy docker registry (quay.io), I get this error:

```
Status: Image is up to date for quay.io/mycompany/jenkins:latest\"}\r\n", "{\"status\":\"quay.io/3dsim/jenkins: this image was pulled from a legacy registry.  Important: This registry version will not be supported in future versions of docker.\"}\r\n"], "failed": true, "status": "quay.io/mycompany/jenkins: this image was pulled from a legacy registry.  Important: This registry version will not be supported in future versions of docker."}
msg: Unrecognized status from pull.
```



Discussion:
I'm seeing this when using images uploaded to the official Docker Hub, but only for really old images that haven't been updated in quite some time. The fix for me was to pull the images in question with `1.8.3` and re-push them back up to the registry. This ensures the images are put into the `/v2/` registry endpoint instead of remaining in the old `/v1/` endpoint.

TBH I'm not sure if Ansible should be working around this issue though. It is a legacy registry issue, not really a docker or Ansible issue. I totally understand that it's a pain in the backside though, especially if you use a 3rd-party registry to host your Docker images (like quay.io or ECR).

The other option to workaround this is to pin docker at `1.8.2` for the time being.

Thanks @daviddyball.  Yeah, I worked around it for now by using Docker Hub instead of Quay.io.  

As far as the fix, I guess my thinking is that it shouldn't treat the warning as an error.  But probably a low priority problem.

I've also hit on this while using tutum.co private registry. I think I've fixed this, I'll PR it.


Resolving patch:
diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py
--- a/cloud/docker/docker.py
+++ b/cloud/docker/docker.py
@@ -117,7 +117,7 @@
     version_added: "2.0"
   log_opt:
     description:
-      - Additional options to pass to the logging driver selected above. See Docker `log-driver 
+      - Additional options to pass to the logging driver selected above. See Docker `log-driver
         <https://docs.docker.com/reference/logging/overview/>` documentation for more information.
         Requires docker >=1.7.0.
     required: false
@@ -1390,24 +1390,20 @@ def pull_image(self):
                 self.module.fail_json(msg="failed to login to the remote registry, check your username/password.", error=repr(e))
         try:
             changes = list(self.client.pull(image, tag=tag, stream=True, **extra_params))
-            try:
-                last = changes[-1]
-                # seems Docker 1.8 puts an empty dict at the end of the
-                # stream; catch that and get the previous instead
-                # https://github.com/ansible/ansible-modules-core/issues/2043
-                if last.strip() == '{}':
-                    last = changes[-2]
-            except IndexError:
-                last = '{}'
-            status = json.loads(last).get('status', '')
-            if status.startswith('Status: Image is up to date for'):
-                # Image is already up to date. Don't increment the counter.
-                pass
-            elif (status.startswith('Status: Downloaded newer image for') or
-                    status.startswith('Download complete')):
-                # Image was updated. Increment the pull counter.
-                self.increment_counter('pulled')
-            else:
+            pull_success = False
+            for change in changes:
+                status = json.loads(change).get('status', '')
+                if status.startswith('Status: Image is up to date for'):
+                    # Image is already up to date. Don't increment the counter.
+                    pull_success = True
+                    break
+                elif (status.startswith('Status: Downloaded newer image for') or
+                        status.startswith('Download complete')):
+                    # Image was updated. Increment the pull counter.
+                    self.increment_counter('pulled')
+                    pull_success = True
+                    break
+            if not pull_success:
                 # Unrecognized status string.
                 self.module.fail_json(msg="Unrecognized status from pull.", status=status, changes=changes)
         except Exception as e:


Test patch: