~canonical-launchpad-branches/launchpad-buildd/trunk

« back to all changes in this revision

Viewing changes to lpbuildd/target/apt.py

  • Committer: Colin Watson
  • Date: 2017-05-11 08:34:07 UTC
  • mfrom: (215.1.1 extended-snap-status)
  • Revision ID: cjwatson@canonical.com-20170511083407-2jvw6phrd50strdk
[r=wgrant] Record the branch revision used to build a snap and return it along with other XML-RPC status information (LP: #1679157).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2019 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
from __future__ import print_function
5
 
 
6
 
__metaclass__ = type
7
 
 
8
 
import logging
9
 
import os
10
 
import subprocess
11
 
import sys
12
 
import tempfile
13
 
import time
14
 
 
15
 
from lpbuildd.target.operation import Operation
16
 
 
17
 
 
18
 
logger = logging.getLogger(__name__)
19
 
 
20
 
 
21
 
class OverrideSourcesList(Operation):
22
 
 
23
 
    description = "Override sources.list in the target environment."
24
 
 
25
 
    @classmethod
26
 
    def add_arguments(cls, parser):
27
 
        super(OverrideSourcesList, cls).add_arguments(parser)
28
 
        parser.add_argument(
29
 
            "--apt-proxy-url", metavar="URL", help="APT proxy URL")
30
 
        parser.add_argument(
31
 
            "archives", metavar="ARCHIVE", nargs="+",
32
 
            help="sources.list lines")
33
 
 
34
 
    def run(self):
35
 
        logger.info("Overriding sources.list in build-%s", self.args.build_id)
36
 
        with tempfile.NamedTemporaryFile() as sources_list:
37
 
            for archive in self.args.archives:
38
 
                print(archive, file=sources_list)
39
 
            sources_list.flush()
40
 
            os.fchmod(sources_list.fileno(), 0o644)
41
 
            self.backend.copy_in(sources_list.name, "/etc/apt/sources.list")
42
 
        if self.args.apt_proxy_url is not None:
43
 
            with tempfile.NamedTemporaryFile() as apt_proxy_conf:
44
 
                print(
45
 
                    'Acquire::http::Proxy "{}";'.format(
46
 
                        self.args.apt_proxy_url),
47
 
                    file=apt_proxy_conf)
48
 
                apt_proxy_conf.flush()
49
 
                os.fchmod(apt_proxy_conf.fileno(), 0o644)
50
 
                self.backend.copy_in(
51
 
                    apt_proxy_conf.name, "/etc/apt/apt.conf.d/99proxy")
52
 
        return 0
53
 
 
54
 
 
55
 
class AddTrustedKeys(Operation):
56
 
 
57
 
    description = "Write out new trusted keys."
58
 
 
59
 
    def __init__(self, args, parser):
60
 
        super(AddTrustedKeys, self).__init__(args, parser)
61
 
        self.input_file = sys.stdin
62
 
 
63
 
    def run(self):
64
 
        """Add trusted keys from an input file."""
65
 
        logger.info("Adding trusted keys to build-%s", self.args.build_id)
66
 
        self.backend.run(["apt-key", "add", "-"], stdin=self.input_file)
67
 
        self.backend.run(["apt-key", "list"])
68
 
        return 0
69
 
 
70
 
 
71
 
class Update(Operation):
72
 
 
73
 
    description = "Update the target environment."
74
 
 
75
 
    def run(self):
76
 
        logger.info("Updating target for build %s", self.args.build_id)
77
 
        with open("/dev/null", "r") as devnull:
78
 
            env = {
79
 
                "LANG": "C",
80
 
                "DEBIAN_FRONTEND": "noninteractive",
81
 
                "TTY": "unknown",
82
 
                }
83
 
            apt_get = "/usr/bin/apt-get"
84
 
            update_args = [apt_get, "-uy", "update"]
85
 
            try:
86
 
                self.backend.run(update_args, env=env, stdin=devnull)
87
 
            except subprocess.CalledProcessError:
88
 
                logger.warning("Waiting 15 seconds and trying again ...")
89
 
                time.sleep(15)
90
 
                self.backend.run(update_args, env=env, stdin=devnull)
91
 
            upgrade_args = [
92
 
                apt_get, "-o", "DPkg::Options::=--force-confold", "-uy",
93
 
                "--purge", "dist-upgrade",
94
 
                ]
95
 
            self.backend.run(upgrade_args, env=env, stdin=devnull)
96
 
        return 0