~psivaa/uci-engine/lander-jenkins-with-proxy

« back to all changes in this revision

Viewing changes to image-builder/imagebuilder/cloud_image.py

  • Committer: Evan Dandrea
  • Date: 2014-03-14 10:37:28 UTC
  • mfrom: (387 ubuntu-ci-services-itself)
  • mto: This revision was merged to the branch mainline in revision 389.
  • Revision ID: evan.dandrea@canonical.com-20140314103728-i9wqk4r17gsya24h
Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import shutil
23
23
import string
24
24
import subprocess
 
25
import textwrap
25
26
import time
26
27
import urllib
27
28
import urlparse
62
63
        #This will throw an exception for malformed ppas, intentional for now
63
64
        (team, repo) = ppamatch.groups()
64
65
        aptlines.append(
 
66
            "deb http://ppa.launchpad.net/{team}/{repo}/ubuntu "
 
67
            "{series} main".format(team=team, repo=repo, series=series))
 
68
        aptlines.append(
65
69
            "deb-src http://ppa.launchpad.net/{team}/{repo}/ubuntu "
66
70
            "{series} main".format(team=team, repo=repo, series=series))
67
71
    return aptlines
74
78
        log.info(output)
75
79
 
76
80
 
 
81
def _apt_get_update(chroot):
 
82
    # apt-get update isn't always reliable give it 3 tries:
 
83
    for x in (2, 1, 0):
 
84
        try:
 
85
            _chrooted(chroot, "/usr/bin/apt-get update")
 
86
            break
 
87
        except subprocess.CalledProcessError as e:
 
88
            if x == 0:
 
89
                raise e
 
90
            else:
 
91
                log.info('apt-get update failed, retrying %d more times', x)
 
92
 
 
93
 
77
94
def _run_chroot_cmds(chroot, ppalist, pkglist):
78
95
    '''Run commands in the chroot to do update the sources and add packages
79
96
    '''
 
97
    # TEMP HACK while getting proper support for private ppa keys
80
98
    #Build the list of PPAs for the sources.list file
81
 
    with open('%s/etc/apt/sources.list.d/ci.list' % chroot, 'a+') as f:
82
 
        for debline in ppalist:
83
 
            f.write('\n%s' % debline)
 
99
    #with open('%s/etc/apt/sources.list.d/ci.list' % chroot, 'a+') as f:
 
100
    #    for debline in ppalist:
 
101
    #        f.write('\n%s' % debline)
 
102
    for ppa in ppalist:
 
103
        _chrooted(chroot, 'add-apt-repository -y %s' % ppa)
84
104
 
85
 
    _chrooted(chroot, "/usr/bin/apt-get update")
 
105
    _apt_get_update(chroot)
86
106
    _chrooted(chroot, "/usr/bin/apt-get install -y {pkglist}".format(
87
107
              pkglist=string.join(pkglist, " ")))
88
108
    _chrooted(chroot, "/usr/bin/apt-get clean")
164
184
            shutil.move('%s/etc/resolv.conf' % mountpoint,
165
185
                        '%s/etc/resolv.conf.orig' % mountpoint)
166
186
        shutil.copy('/etc/resolv.conf', '%s/etc/resolv.conf' % mountpoint)
 
187
        policyfile = textwrap.dedent("""\
 
188
            #!/bin/sh
 
189
            while true; do
 
190
            case "$1" in
 
191
                -*) shift ;;
 
192
                makedev) exit 0;;
 
193
                x11-common) exit 0;;
 
194
                *) exit 101;;
 
195
            esac
 
196
            done""")
 
197
        with open('%s/usr/sbin/policy-rc.d' % mountpoint, 'w') as f:
 
198
            f.write(policyfile)
 
199
        os.chmod('%s/usr/sbin/policy-rc.d' % mountpoint, 0o755)
 
200
 
167
201
        return mountpoint
168
202
 
169
203
    def _umount_image(self):
172
206
        if os.path.lexists('%s/etc/resolv.conf.orig' % self.mountpoint):
173
207
            shutil.move('%s/etc/resolv.conf.orig' % self.mountpoint,
174
208
                        '%s/etc/resolv.conf' % self.mountpoint)
 
209
        os.unlink('%s/usr/sbin/policy-rc.d' % self.mountpoint)
175
210
        runcmd('umount %s' % self.mountpoint)
176
211
        runcmd('qemu-nbd -d /dev/%s' % self.nbd_dev)
177
212
 
188
223
        image_path = _download(image, tmpdir)
189
224
        with CloudImage(image_path, tmpdir) as mountpoint:
190
225
            status_cb('updating image %s...' % image)
191
 
            ppalist = _parse_ppas(mountpoint, repos, series)
 
226
            # TEMP HACK
 
227
            # ppalist = _parse_ppas(mountpoint, repos, series)
 
228
            ppalist = repos
192
229
            status_cb('Adding requested packages to the image...')
193
230
            _run_chroot_cmds(mountpoint, ppalist, packages)
194
231
        auth_config = get_auth_config()