Blame cloudinit/config/cc_apt_pipelining.py

Packit Service a04d08
# Copyright (C) 2011 Canonical Ltd.
Packit Service a04d08
#
Packit Service a04d08
# Author: Ben Howard <ben.howard@canonical.com>
Packit Service a04d08
#
Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
"""
Packit Service a04d08
Apt Pipelining
Packit Service a04d08
--------------
Packit Service a04d08
**Summary:** configure apt pipelining
Packit Service a04d08
Packit Service 751c4a
This module configures apt's ``Acquite::http::Pipeline-Depth`` option, which
Packit Service a04d08
controls how apt handles HTTP pipelining. It may be useful for pipelining to be
Packit Service a04d08
disabled, because some web servers, such as S3 do not pipeline properly (LP:
Packit Service a04d08
#948461). The ``apt_pipelining`` config key may be set to ``false`` to disable
Packit Service a04d08
pipelining altogether. This is the default behavior. If it is set to ``none``,
Packit Service a04d08
``unchanged``, or ``os``, no change will be made to apt configuration and the
Packit Service a04d08
default setting for the distro will be used. The pipeline depth can also be
Packit Service a04d08
manually specified by setting ``apt_pipelining`` to a number. However, this is
Packit Service a04d08
not recommended.
Packit Service a04d08
Packit Service a04d08
**Internal name:** ``cc_apt_pipelining``
Packit Service a04d08
Packit Service a04d08
**Module frequency:** per instance
Packit Service a04d08
Packit Service a04d08
**Supported distros:** ubuntu, debian
Packit Service a04d08
Packit Service a04d08
**Config keys**::
Packit Service a04d08
    apt_pipelining: <false/none/unchanged/os/number>
Packit Service a04d08
"""
Packit Service a04d08
Packit Service a04d08
from cloudinit.settings import PER_INSTANCE
Packit Service a04d08
from cloudinit import util
Packit Service a04d08
Packit Service a04d08
frequency = PER_INSTANCE
Packit Service a04d08
Packit Service a04d08
distros = ['ubuntu', 'debian']
Packit Service a04d08
Packit Service a04d08
DEFAULT_FILE = "/etc/apt/apt.conf.d/90cloud-init-pipelining"
Packit Service a04d08
Packit Service a04d08
APT_PIPE_TPL = ("//Written by cloud-init per 'apt_pipelining'\n"
Packit Service a04d08
                'Acquire::http::Pipeline-Depth "%s";\n')
Packit Service a04d08
Packit Service a04d08
# Acquire::http::Pipeline-Depth can be a value
Packit Service a04d08
# from 0 to 5 indicating how many outstanding requests APT should send.
Packit Service a04d08
# A value of zero MUST be specified if the remote host does not properly linger
Packit Service a04d08
# on TCP connections - otherwise data corruption will occur.
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def handle(_name, cfg, _cloud, log, _args):
Packit Service a04d08
Packit Service a04d08
    apt_pipe_value = util.get_cfg_option_str(cfg, "apt_pipelining", 'os')
Packit Service a04d08
    apt_pipe_value_s = str(apt_pipe_value).lower().strip()
Packit Service a04d08
Packit Service a04d08
    if apt_pipe_value_s == "false":
Packit Service a04d08
        write_apt_snippet("0", log, DEFAULT_FILE)
Packit Service a04d08
    elif apt_pipe_value_s in ("none", "unchanged", "os"):
Packit Service a04d08
        return
Packit Service a04d08
    elif apt_pipe_value_s in [str(b) for b in range(0, 6)]:
Packit Service a04d08
        write_apt_snippet(apt_pipe_value_s, log, DEFAULT_FILE)
Packit Service a04d08
    else:
Packit Service a04d08
        log.warning("Invalid option for apt_pipelining: %s", apt_pipe_value)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def write_apt_snippet(setting, log, f_name):
Packit Service a04d08
    """Writes f_name with apt pipeline depth 'setting'."""
Packit Service a04d08
Packit Service a04d08
    file_contents = APT_PIPE_TPL % (setting)
Packit Service a04d08
    util.write_file(f_name, file_contents)
Packit Service a04d08
    log.debug("Wrote %s with apt pipeline depth setting %s", f_name, setting)
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab