@@ -59,10 +59,26 @@ def _put_first_image(repository_path):
5959 _commit (repository_path , image , _empty_tar_archive (), 'v0' )
6060
6161
62+ def _api_call (endpoint , data = None , headers = {}, cookies = None , request_type = 'GET' ):
63+ # FIXME: update API calls for docker 0.10.0+
64+ base_headers = {'user-agent' : 'docker/0.9.0' }
65+ r = None
66+ if len (headers ) > 0 :
67+ for header , value in headers .iteritems ():
68+ base_headers [header ] = value
69+ if request_type == 'GET' :
70+ r = requests .get (endpoint , headers = base_headers )
71+ elif request_type == 'PUT' :
72+ r = requests .put (endpoint , data = data , headers = base_headers , cookies = cookies )
73+ else :
74+ raise AttributeError ("request type not supported: {}" .format (request_type ))
75+ return r
76+
77+
6278def _get_tag (repository , tag ):
6379 path = "/v1/repositories/{repository}/tags/{tag}" .format (** locals ())
6480 url = urlparse .urljoin (settings .REGISTRY_URL , path )
65- r = requests . get (url )
81+ r = _api_call (url )
6682 if not r .status_code == 200 :
6783 raise RuntimeError ("GET Image Error ({}: {})" .format (r .status_code , r .text ))
6884 print r .text
@@ -72,7 +88,7 @@ def _get_tag(repository, tag):
7288def _get_image (image_id ):
7389 path = "/v1/images/{image_id}/json" .format (** locals ())
7490 url = urlparse .urljoin (settings .REGISTRY_URL , path )
75- r = requests . get (url )
91+ r = _api_call (url )
7692 if not r .status_code == 200 :
7793 raise RuntimeError ("GET Image Error ({}: {})" .format (r .status_code , r .text ))
7894 return r .json ()
@@ -81,7 +97,7 @@ def _get_image(image_id):
8197def _put_image (image ):
8298 path = "/v1/images/{id}/json" .format (** image )
8399 url = urlparse .urljoin (settings .REGISTRY_URL , path )
84- r = requests . put (url , data = json .dumps (image ))
100+ r = _api_call (url , data = json .dumps (image ), request_type = 'PUT' )
85101 if not r .status_code == 200 :
86102 raise RuntimeError ("PUT Image Error ({}: {})" .format (r .status_code , r .text ))
87103 return r .json ()
@@ -90,7 +106,7 @@ def _put_image(image):
90106def _put_layer (image_id , layer_fileobj ):
91107 path = "/v1/images/{image_id}/layer" .format (** locals ())
92108 url = urlparse .urljoin (settings .REGISTRY_URL , path )
93- r = requests . put (url , data = layer_fileobj .read ())
109+ r = _api_call (url , data = layer_fileobj .read (), request_type = 'PUT' )
94110 if not r .status_code == 200 :
95111 raise RuntimeError ("PUT Layer Error ({}: {})" .format (r .status_code , r .text ))
96112 return r .cookies
@@ -101,7 +117,7 @@ def _put_checksum(image, cookies):
101117 url = urlparse .urljoin (settings .REGISTRY_URL , path )
102118 tarsum = TarSum (json .dumps (image )).compute ()
103119 headers = {'X-Docker-Checksum' : tarsum }
104- r = requests . put (url , headers = headers , cookies = cookies )
120+ r = _api_call (url , headers = headers , cookies = cookies , request_type = 'PUT' )
105121 if not r .status_code == 200 :
106122 raise RuntimeError ("PUT Checksum Error ({}: {})" .format (r .status_code , r .text ))
107123 print r .json ()
@@ -110,7 +126,7 @@ def _put_checksum(image, cookies):
110126def _put_tag (image_id , repository_path , tag ):
111127 path = "/v1/repositories/{repository_path}/tags/{tag}" .format (** locals ())
112128 url = urlparse .urljoin (settings .REGISTRY_URL , path )
113- r = requests . put (url , data = json .dumps (image_id ))
129+ r = _api_call (url , data = json .dumps (image_id ), request_type = 'PUT' )
114130 if not r .status_code == 200 :
115131 raise RuntimeError ("PUT Tag Error ({}: {})" .format (r .status_code , r .text ))
116132 print r .json ()
0 commit comments