~joetalbott/snappy-proposed-image-builder/add_versions

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# snappy-proposed-image-builder
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Business logic or the service lives here."""

import contextlib
import glob
import logging
import os
import subprocess
import tempfile

from uservice_utils.queue import MessageActions

from snappy_proposed_image_builder.constants import LOGGING_EXTRA
from snappy_proposed_image_builder.cloud import get_glance_client
from snappy_proposed_image_builder.utils import (
    check_call,
    BetterCalledProcessError,
)


logger = logging.getLogger(__name__)


class RootFSError(Exception):
    pass


class ImageBuilderWorker(object):

    """A worker callable that contains all our main logic."""

    def __init__(self, config, builder):
        self._config = config
        self.publish_results = builder

    def __call__(self, payload):
        extra = LOGGING_EXTRA.copy()
        extra.update(payload)
        try:
            release = payload['release']
            channel = payload['channel']
            device = payload['device']
            image_binaries = payload['image_binaries']
            source_version = payload['source_version']
        except KeyError as e:
            logger.error(
                "Unable to deserialize message payload - "
                "rejecting message: %s",
                e,
                extra=extra
            )
            return MessageActions.Retry

        (image_version, rootfs_version, device_version) = _get_image_versions(
            release, channel, device)

        payload['image_version'] = image_version
        payload['rootfs_version'] = rootfs_version
        payload['device_version'] = device_version

        with tempfile.TemporaryDirectory() as tmpdir:
            logger.info("Beginning rootfs download.", extra=extra)
            try:
                rootfs_path = download_rootfs(
                    image_version,
                    release,
                    channel,
                    device,
                    tmpdir
                )
            except subprocess.CalledProcessError as e:
                logger.error(
                    "Unable to download core image: %s",
                    e,
                    extra=extra
                )
                return MessageActions.Retry

            logger.info("Adding packages from proposed")
            MAX_RETRIES = 3
            count = MAX_RETRIES
            while count > 0:
                new_rootfs_path = add_proposed_packages(rootfs_path,
                                                        image_binaries,
                                                        source_version)
                if new_rootfs_path is not None:
                    break

                # try removing the rootfs as it sometimes is corrupted.
                # XXX: find out why it's getting corrupted.
                logger.info("Retrying rootfs download.", extra=extra)
                os.remove(rootfs_path)
                try:
                    rootfs_path = download_rootfs(
                        image_version,
                        channel,
                        device,
                        tmpdir
                    )
                except subprocess.CalledProcessError as e:
                    logger.error(
                        "Unable to download core image: %s",
                        e,
                        extra=extra
                    )
                    return MessageActions.Retry

                count -= 1
            if count == 0:
                logger.error(
                    "Unable to add packages after %d tries: %s",
                    MAX_RETRIES,
                    image_binaries,
                    source_version,
                )
                return MessageActions.Retry

            try:
                logger.info("Building the image")
                image_path = build_image(
                    image_version,
                    new_rootfs_path,
                    release,
                    channel,
                    device,
                    tmpdir
                )
            except subprocess.CalledProcessError as e:
                logger.error(
                    "Unable to build the image: %s",
                    e,
                    extra=extra
                )
                return MessageActions.Retry
            logger.info("Ubuntu Core image build OK.", extra=extra)

            try:
                nova_image_path = convert_nova_image(image_path)
            except subprocess.CalledProcessError as e:
                logger.error(
                    "Unable to convert core image to qcow2 image: %s",
                    e,
                    extra=extra)
                return MessageActions.Retry
            logger.info("Image converted to qcow2 OK.", extra=extra)

            try:
                logger.info("Beginning image upload to glance...",
                            extra=extra)
                glance_image_id = upload_image_to_glance(
                    nova_image_path,
                    self._config
                )
            except Exception as e:
                logger.error(
                    "Unable to upload image to glance: %s",
                    e,
                    extra=extra
                )
                return MessageActions.Retry
            logger.info("Image uploaded to glance OK.", extra=extra)

        payload['nova_image_id'] = glance_image_id
        self.publish_results(payload)
        logger.info("Processing completed.", extra=extra)
        return MessageActions.Acknowledge


def download_rootfs(name, release, channel, device, tmpdir):
    """Download the ubuntu code image, return a path to it on disk."""
    image_path = os.path.join(tmpdir, 'snappy-proposed-{}.img'.format(name))

    cmd = ['sudo',
           './ubuntu-device-flash',
           '--revision', name,
           '--download-only',
           'core',
           '--device', device,
           '--channel', channel,
           '--size', '3',
           '-o', image_path,
           '--developer-mode',
           '--cloud',
           release]
    check_call(cmd)

    cmd = ['sudo',
           'rm',
           '-rf',
           os.path.join(tmpdir, '.gnupg')]
    check_call(cmd)
    output = glob.glob('ubuntu-*.tar.xz')
    return output[0]


@contextlib.contextmanager
def mount_proc(chroot_base):
    check_call(['sudo', 'mount', '-t', 'proc', 'none',
                os.path.join(chroot_base, 'proc')])
    try:
        yield
    finally:
        check_call(['sudo', 'umount', os.path.join(chroot_base, 'proc')])


@contextlib.contextmanager
def mount_dev_pts(chroot_base):
    check_call(['sudo', 'mount', '-t', 'devpts', '-o', 'gid=5,mode=620',
                'none', os.path.join(chroot_base, 'dev', 'pts')])
    try:
        yield
    finally:
        check_call(['sudo', 'umount', os.path.join(chroot_base, 'dev', 'pts')])


@contextlib.contextmanager
def mount_sys(chroot_base):
    check_call(['sudo', 'mount', '-t', 'sysfs', 'none',
                os.path.join(chroot_base, 'sys')])
    try:
        yield
    finally:
        check_call(['sudo', 'umount', os.path.join(chroot_base, 'sys')])


@contextlib.contextmanager
def mount_dev_shm(chroot_base):
    check_call(['sudo', 'mount', '-t', 'tmpfs', 'none',
                os.path.join(chroot_base, 'dev', 'shm')])
    try:
        yield
    finally:
        check_call(['sudo', 'umount', os.path.join(chroot_base, 'dev', 'shm')])


def add_proposed_packages(rootfs_path, image_binaries, source_version):

    new_rootfs_path = rootfs_path.replace('.tar.xz', '-modified.tar.xz')

    ROOTFS_BASE_DIR = 'system'

    # untar the rootfs
    cmd = ['sudo', 'tar', 'xJf', rootfs_path]
    try:
        check_call(cmd)
    except BetterCalledProcessError as e:
        logger.error(e)
        return None

    # sanity check that the base directory exists
    if not os.path.exists('system'):
        logger.error("Failed to untar the rootfs: %s", rootfs_path)
        return None

    chroot_base = os.path.join(os.getcwd(), ROOTFS_BASE_DIR)

    # chroot into the rootfs and install packages
    cmd = ['sudo',  'chroot', chroot_base,
           'lsb_release', '-cs']
    output = subprocess.check_output(cmd)
    series = output.decode("utf-8").strip()
    proposed_pocket = '{}-proposed'.format(series)
    deb_url = "http://ftpmaster.internal/ubuntu/"
    proposed_line = ("deb {} {} restricted main universe".format(
        deb_url, proposed_pocket))
    proposed_line_2 = ""

    apt_sources_path = os.path.join(chroot_base, 'etc', 'apt', 'sources.list')
    with open(apt_sources_path, 'r') as fp:
        for line in fp.readlines():
            if line.startswith('deb '):
                data = line.split()
                if len(data) > 1:  # assumes a properly formatted source entry
                    proposed_line_2 = proposed_line.replace(deb_url, data[1])
                    break

    new_apt_sources_file = os.path.join(chroot_base, 'etc', 'apt',
                                        'sources.list.d',
                                        'add_proposed_pocket')
    with tempfile.NamedTemporaryFile(mode='wt') as fp:
        fp.write(proposed_line)
        fp.write('\n')
        fp.write(proposed_line_2)
        fp.flush()

        cmd = ['sudo', 'cp', fp.name, new_apt_sources_file]
        check_call(cmd)

        cmd = ['sudo', 'chmod', '+r',  new_apt_sources_file]
        check_call(cmd)

    with contextlib.ExitStack() as mount_stack:
        mount_stack.enter_context(mount_proc(chroot_base))
        mount_stack.enter_context(mount_sys(chroot_base))
        mount_stack.enter_context(mount_dev_pts(chroot_base))
        mount_stack.enter_context(mount_dev_shm(chroot_base))

        check_call(['sudo', 'cp', '/etc/hosts', '/etc/hostname',
                    '/etc/resolv.conf', os.path.join(chroot_base, 'etc')])

        cmd = ['sudo',  'chroot', chroot_base,
               '/usr/bin/apt-get', 'update']
        check_call(cmd)

        for package_name in image_binaries:
            pkg_version_cmd = ['sudo', 'chroot', chroot_base, 'dpkg-query',
                               '--show', package_name]

            old_version = subprocess.check_output(pkg_version_cmd)
            old_version = old_version.strip().decode('utf-8')

            if old_version == "":
                raise RootFSError(
                    "package not installed on original image: {}".format(
                        package_name))

            cmd = ['sudo',  'chroot', chroot_base,
                   '/usr/bin/apt-get', 'install', '--yes',
                   "{}={}".format(package_name, source_version)]
            try:
                check_call(cmd)
            except subprocess.CalledProcessError as e:
                raise RootFSError(
                    "Failed to install package: {}\n: {}".format(
                        package_name, e))

            new_version = subprocess.check_output(pkg_version_cmd)

            new_version = new_version.strip().decode('utf-8')
            expected_version = "{}\t{}".format(package_name,
                                               source_version)

            if new_version != expected_version:
                raise RootFSError(
                    "new version is not the same as "
                    "the expected version, {} == {}".format(
                        new_version, expected_version))

    # tar the rootfs back up.
    cmd = ['sudo', 'tar', 'cJf', new_rootfs_path, ROOTFS_BASE_DIR]
    check_call(cmd)

    return new_rootfs_path


def build_image(name, rootfs_path, release, channel, device, tmpdir):
    """Download the ubuntu code image, return a path to it on disk."""
    image_path = os.path.join(tmpdir, 'snappy-proposed-{}.img'.format(name))
    cmd = ['sudo',
           './ubuntu-device-flash',
           '--revision', name,
           'core',
           '--device', device,
           '--channel', channel,
           '--size', '3',
           '--image-part', rootfs_path,
           '-o', image_path,
           '--developer-mode',
           '--cloud',
           release]
    check_call(cmd)

    cmd = ['sudo',
           'rm',
           '-rf',
           os.path.join(tmpdir, '.gnupg')]
    check_call(cmd)

    return image_path


def convert_nova_image(image_path):
    """
    Convert a core image to a nova image, return path to the converted image.
    """
    converted_image_path = '{}-cloud.img'.format(image_path.strip('.img'))
    cmd = ['qemu-img',
           'convert',
           '-f', 'raw',
           '-O', 'qcow2',
           image_path,
           converted_image_path]
    check_call(cmd)

    return converted_image_path


def upload_image_to_glance(nova_image_path, config):
    """
    Upload the nova image to glance, returning the id of the image in glance.
    """
    extra = LOGGING_EXTRA.copy()
    glance = get_glance_client(config)
    image_name = os.path.basename(nova_image_path)
    image = glance.images.create(
        name=image_name,
        data=open(nova_image_path, 'rb'),
        disk_format='qcow2',
        container_format='bare',
    )
    logger.info("Uploaded %s with ID %s.", image_name, image.id, extra=extra)
    return image.id


def _get_version_string_output(release, channel, device):
    """Obtains a bytestring of the images info from the core image server"""
    extra = LOGGING_EXTRA.copy()
    cmd = [
        './ubuntu-device-flash',
        'query',
        '--show-image',
        '--channel=ubuntu-core/{}/{}'.format(release, channel),
        '--device={}'.format(device),
    ]
    latest_image_info = b''
    try:
        latest_image_info = subprocess.check_output(cmd)
    except subprocess.CalledProcessError as e:
        logger.error(e, exc_info=True, extra=extra)
    finally:
        return latest_image_info


def _get_image_versions(release, channel, device):
    latest_image_info = _get_version_string_output(release, channel, device)
    image_version = ''
    rootfs_version = ''
    dev_version = ''
    for info in latest_image_info.decode('utf-8').split("\n"):
        if 'Description' in info:
            for l in info.split(','):
                if 'version' in l:
                    image_version = l.split('version=')[1]
                    continue
                if 'ubuntu' in l:
                    rootfs_version = l.split('ubuntu=')[1]
                    continue
                if 'raw-device' in l:
                    dev_version = l.split('raw-device=')[1]
                    continue

    return (image_version, rootfs_version, dev_version)