/
dockstrap.py
186 lines (167 loc) · 6.28 KB
/
dockstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import requests
import click
import os
import os.path
from subprocess import call
def setup_dir(path):
if not os.path.isdir(path):
os.mkdir(path)
def is_gzip(path):
with open(path, 'rb') as f:
gzip_header = f.read(2)
if gzip_header[0] != 0x1F:
return False
if gzip_header[1] != 0x8B:
return False
return True
def get_tags(baseurl, image):
r = requests.get('{0}/v1/repositories/{1}/tags'.format(baseurl, image))
if r.status_code != 200:
raise click.ClickException("repository {0} not found".format(image))
return r.json()
def get_images(baseurl, image):
r = requests.get('{0}/v1/repositories/{1}/images'.format(baseurl, image),
headers={'X-Docker-Token': 'true'})
return (r.headers['x-docker-token'],
r.headers['x-docker-endpoints'],
r.json())
def get_checksum(image_id, images):
for image in images:
if image['id'] == image_id:
if not image['checksum']:
return None
return image['checksum']
return None
def download_layers(endpoint, token, image_id, cachedir, checksums):
layers = []
while True:
r = requests.get('https://{0}/v1/images/{1}/json'.format(endpoint,
image_id),
headers={'Authorization': 'Token {0}'.format(token)})
image_json = r.json()
if 'parent' in image_json:
if not image_json['parent']:
break
image_id = image_json['parent']
layers.insert(0, image_id)
else:
break
# start downloading layers (if needed)
for layer in layers:
r = requests.get('https://{0}/v1/images/{1}/layer'.format(endpoint,
layer),
headers={'Authorization': 'Token {0}'.format(token)},
stream=True)
destination = os.path.join(cachedir, layer)
content_length = int(r.headers['content-length'])
# first of all check if the file exists
if os.path.isfile(destination):
# does the filesize matches ?
if os.path.getsize(destination) == content_length:
# do we need to compute the checksum ?
checksum = get_checksum(layer, checksums)
# TODO do something with checksum
if checksum:
pass
click.echo("reusing cached {0}".format(layer))
continue
remains = content_length
with open(destination, 'w') as targz:
while remains > 0:
chunk = r.raw.read(32768)
if not chunk:
break
remains -= len(chunk)
downloaded = content_length - remains
click.echo("\rdownloading layer {0}"
" {1}/{2}".format(layer,
downloaded,
content_length),
nl=False)
targz.write(chunk)
click.echo('')
return layers
@click.command()
@click.option('--baseurl', default='https://index.docker.io',
help='set the base url for registry api access')
@click.option('--cachedir', default=os.path.expanduser('~/.dockstrap_cache'),
help='set the directory on which to store/cache image files')
@click.option('--verbose', default=False,
is_flag = True,
help='set verbose mode')
@click.argument('image')
@click.argument('path')
def dockstrap_run(baseurl, cachedir, image, path, verbose):
setup_dir(cachedir)
setup_dir(path)
click.echo("using baseurl: {0}".format(baseurl))
click.echo("using cachedir: {0}".format(cachedir))
tag = 'latest'
force_tag = False
# does the user specified a tag ?
if ':' in image:
force_tag = True
image, tag = image.split(':', 1)
# get the tags list
tags = get_tags(baseurl, image)
# check the requested tag exists:
layer = None
for tag_item in tags:
if tag_item['name'] == tag:
layer = tag_item['layer']
break
# if no tag is found, get the first one
if not layer:
if not force_tag:
layer = tags[0]['layer']
tag = tags[0]['name']
else:
raise click.ClickException("unable to find tag {0}"
" for repository {1}".format(tag,
image))
click.echo("using image: {0}".format(image))
click.echo("using tag: {0}".format(tag))
"""
get the list of repository images
this list will be used for managing
checksums.
the function returns the authentication token
end the registry endpoints too
"""
token, endpoints, images = get_images(baseurl, image)
image_id = None
for image_item in images:
if image_item['id'].startswith(layer):
image_id = image_item['id']
break
if not image_id:
raise click.ClickException("unable to find image id"
" starting with '{0}'".format(layer))
endpoint = endpoints.split(' ')[0]
click.echo("using endpoint: {0}".format(endpoint))
# now start iterating layers
# and download them
layers = download_layers(endpoint,
token,
image_id,
cachedir,
images)
am_i_root = False
if os.getuid() == 0:
am_i_root = True
for layer in layers:
source = os.path.join(cachedir, layer)
flags = ['-', 'x', 'f']
if is_gzip(source):
flags.insert(1, 'z')
if verbose:
flags.insert(1, 'v')
click.echo("extracting {0} to {1}".format(layer, path))
if not am_i_root:
ret = call(['tar',
'--exclude=dev', '-C', path, ''.join(flags), source])
else:
ret = call(['tar', '-C', path, ''.join(flags), source])
if ret != 0:
raise click.ClickException("tar failed")
click.echo("your filesystem is ready at {0}".format(path))