-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.py
224 lines (178 loc) · 7.12 KB
/
core.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement
import json
import logging
from datetime import datetime
from functools import partial
import socket
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import get_object_or_404
from dbs.models import Task, TaskData, TaskLint, Dockerfile, Image
from dbs.task_api import TaskApi
from dbs.utils import chain_dict_get
logger = logging.getLogger(__name__)
builder_api = TaskApi()
class ErrorDuringRequest(Exception):
""" indicate that there was an error during processing request; e.g. 404, invalid sth... """
def lint_output_callback(task_id, lint, **kwargs):
t = Task.objects.get(id=task_id)
tl = TaskLint(lint=lint["html_markup"])
tl.save()
t.task_lint = tl
t.save()
def new_image_callback(task_id, response_tuple):
try:
response_hash, df, build_log = response_tuple
except (TypeError, ValueError):
response_hash, df, build_log = None, None, None
t = get_object_or_404(Task, id=task_id)
t.date_finished = datetime.now()
if build_log:
t.log = '\n'.join(build_log)
if response_hash:
image_id = chain_dict_get(response_hash, ['built_img_info', 'Id'])
logger.debug("image_id = %s", image_id)
parent_image_id = chain_dict_get(response_hash, ['base_img_info', 'Id'])
logger.debug("parent_image_id = %s", parent_image_id)
image_tags = chain_dict_get(response_hash, ['built_img_info', 'RepoTags'])
logger.debug("image_tags = %s", image_tags)
parent_tags = chain_dict_get(response_hash, ['base_img_info', 'RepoTags'])
logger.debug("parent_tags = %s", parent_tags)
if image_id and parent_image_id:
parent_image = Image.create(parent_image_id, Image.STATUS_BASE, tags=parent_tags)
image = Image.create(image_id, Image.STATUS_BUILD, tags=image_tags,
task=t, parent=parent_image)
if df:
df_model = Dockerfile(content=df)
df_model.save()
image.dockerfile = df_model
image.save()
rpm_list = chain_dict_get(response_hash, ['built_img_plugins_output', 'all_packages'])
base_rpm_list = chain_dict_get(response_hash, ['base_plugins_output', 'all_packages'])
if rpm_list:
image.add_rpms_list(rpm_list)
if base_rpm_list:
image.add_rpms_list(base_rpm_list)
else:
t.status = Task.STATUS_FAILED
t.status = Task.STATUS_SUCCESS
else:
logger.debug("task failed: %s" % repr (response_tuple))
t.status = Task.STATUS_FAILED
t.save()
def build(post_args, **kwargs):
""" initiate a new build """
owner = "testuser" # XXX: hardcoded
logger.debug("post_args = %s", post_args)
local_tag = "%s/%s" % (owner, post_args['tag'])
td = TaskData(json=json.dumps(post_args))
td.save()
t = Task(builddev_id="buildroot-fedora", status=Task.STATUS_PENDING,
type=Task.TYPE_BUILD, owner=owner, task_data=td)
t.save()
lint_callback = partial(lint_output_callback, t.id)
callback = partial(new_image_callback, t.id)
post_args.update({'build_image': "buildroot-fedora", 'local_tag': local_tag,
'callback': callback,
'lint_callback': lint_callback})
task_id = builder_api.build_docker_image(**post_args)
t.celery_id = task_id
t.save()
return t.id
def rebuild(post_args, image_id, **kwargs):
try:
data = Image.objects.taskdata_for_imageid(image_id)
except (ObjectDoesNotExist, AttributeError) as ex:
logger.error("%s", repr(ex))
raise ErrorDuringRequest("Image does not exist or was not built from task.")
else:
if post_args:
data.update(post_args)
return build(data)
def move_image_callback(task_id, response):
logger.debug("move callback: %s %s", task_id, response)
t = get_object_or_404(Task, id=task_id)
t.date_finished = datetime.now()
if response and response.get("error", False):
t.status = Task.STATUS_FAILED
else:
t.status = Task.STATUS_SUCCESS
t.save()
def move_image(post_args, image_id, **kwargs):
post_args['image_name'] = image_id
td = TaskData(json=json.dumps(post_args))
td.save()
owner = "testuser" # XXX: hardcoded
t = Task(status=Task.STATUS_PENDING, type=Task.TYPE_MOVE, owner=owner, task_data=td)
t.save()
callback = partial(move_image_callback, t.id)
post_args['callback'] = callback
task_id = builder_api.push_docker_image(**post_args)
t.celery_id = task_id
t.save()
return t.id
def invalidate(post_args, image_id, **kwargs):
response = Image.objects.invalidate(image_id)
return response
def task_status(args, task_id, request, **kwargs):
task = get_object_or_404(Task, id=task_id)
response = {
"task_id": task_id,
"status": task.get_status_display(),
"type": task.get_type_display(),
"owner": task.owner,
"started": str(task.date_started),
"finished": str(task.date_finished),
"builddev-id": task.builddev_id,
}
if hasattr(task, 'image'):
response['image_id'] = task.image.hash
task_data = json.loads(task.task_data.json)
# domain = request.get_host()
domain = socket.gethostbyname(request.META['SERVER_NAME'])
response['message'] = "You can pull your image with command: 'docker pull %s:5000/%s'" % \
(domain, task_data['tag'])
return response
def image_info(args, image_id, **kwargs):
img = get_object_or_404(Image, hash=image_id)
#rpms = []
#for rpm in img.rpms.all():
# rpms.append({"nvr": rpm.nvr,
# "component": rpm.component,
# })
#registries = []
#for reg in img.registries.all():
# registries.append({"url": reg.url})
response = {
"hash": img.hash,
"status": img.get_status_display(),
"is_invalidated": img.is_invalidated,
"rpms": img.ordered_rpms_list(),
"tags": img.tags,
# "registries": copy.copy(registries),
"parent": getattr(img.parent, 'hash', None)
}
if img.task:
response['built_on'] = str(img.task.date_finished)
return response
def list_images(args, **kwargs):
response = []
for img in Image.objects.all():
response.append(image_info(args, img.hash))
return response
def list_tasks(args, request, **kwargs):
response = []
for task in Task.objects.all():
response.append(task_status(args, task.id, request))
return response
def image_status(args, image_id, **kwargs):
img = get_object_or_404(Image, hash=image_id)
response = {"image_id": image_id,
"status": img.get_status_display()}
return response
def image_deps(args, image_id, **kwargs):
deps = []
response = {"image_id": image_id,
"deps": deps}
for child in Image.objects.children(image_id):
deps.append(image_deps(args, child.hash))
return response