Skip to content

Commit 162762d

Browse files
author
Ben Howard
committed
Simplified proposed patch
- Changed values to be more simplistic and intuitive - Only allow pipelining values up to 5 - Changed to per_instance over per_always to remove need for tracking the values - Fixed Python style
1 parent acf41ea commit 162762d

File tree

2 files changed

+20
-62
lines changed

2 files changed

+20
-62
lines changed

cloudinit/CloudConfig/cc_apt_pipelining.py

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,29 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import cloudinit.util as util
20-
import cloudinit.SshUtil as sshutil
2120
import re
2221
import os
23-
from cloudinit.CloudConfig import per_always
22+
from cloudinit.CloudConfig import per_instance
2423

25-
frequency = per_always
26-
default_file = "/etc/apt/apt.conf.d/90cloud-init-tweaks"
24+
frequency = per_instance
25+
default_file = "/etc/apt/apt.conf.d/90cloud-init-pipeling"
2726

2827
def handle(_name, cfg, cloud, log, _args):
2928

30-
apt_pipelining_enabled = util.get_cfg_option_str(cfg, "apt-pipelining", False)
29+
apt_pipe_value = util.get_cfg_option_str(cfg, "apt_pipelining", False)
30+
apt_pipe_value = str(apt_pipe_value).lower()
3131

32-
if apt_pipelining_enabled in ("False", "false", False):
32+
if apt_pipe_value in ("false", "default", False):
3333
write_apt_snippet(0, log)
3434

35-
elif apt_pipelining_enabled in ("Default", "default", "True", "true", True):
36-
revert_os_default(log)
35+
elif apt_pipe_value in ("none", "unchanged", "os"):
36+
return
3737

38-
else:
39-
write_apt_snippet(apt_pipelining_enabled, log)
40-
41-
def revert_os_default(log, f_name=default_file):
42-
try:
43-
44-
if os.path.exists(f_name):
45-
os.unlink(f_name)
46-
47-
except OSError:
48-
log.debug("Unable to remove %s" % f_name)
38+
elif apt_pipe_value in str(range(1, 5)):
39+
write_apt_snippet(apt_pipe_value, log)
4940

41+
else:
42+
log.warn("Invalid option for apt_pipeling")
5043

5144
def write_apt_snippet(setting, log, f_name=default_file):
5245
"""
@@ -57,54 +50,19 @@ def write_apt_snippet(setting, log, f_name=default_file):
5750
acquire_pipeline_depth = 'Acquire::http::Pipeline-Depth "%s";\n'
5851
try:
5952
if os.path.exists(f_name):
60-
update_file = False
6153
skip_re = re.compile('^//CLOUD-INIT-IGNORE.*')
62-
enabled_re = re.compile('Acquire::http::Pipeline-Depth.*')
63-
64-
local_override = False
65-
tweak = open(f_name, 'r')
66-
new_file = []
6754

6855
for line in tweak.readlines():
6956
if skip_re.match(line):
70-
local_override = True
71-
continue
72-
73-
if enabled_re.match(line):
74-
75-
try:
76-
value = line.replace('"','')
77-
value = value.replace(';','')
78-
enabled = value.split()[1]
79-
80-
if enabled != setting:
81-
update_file = True
82-
line = acquire_pipeline_depth % setting
83-
84-
except IndexError:
85-
log.debug("Unable to determine current setting of 'Acquire::http::Pipeline-Depth'\n%s" % e)
86-
return
87-
88-
new_file.append(line)
57+
tweak.close()
58+
return
8959

9060
tweak.close()
9161

92-
if local_override:
93-
log.debug("Not updating apt pipelining settings due to local override in %s" % f_name)
94-
return
95-
96-
if update_file:
97-
tweak = open(f_name, 'w')
98-
for line in new_file:
99-
tweak.write(line)
100-
tweak.close()
101-
102-
return
62+
file_contents = ("//Cloud-init Tweaks\n//Disables APT HTTP pipelining"\
63+
"\n" + (acquire_pipeline_depth % setting))
10364

104-
tweak = open(f_name, 'w')
105-
tweak.write("""//Cloud-init Tweaks\n//Disables APT HTTP pipelining\n""")
106-
tweak.write(acquire_pipeline_depth % setting)
107-
tweak.close()
65+
util.write_file(f_name, file_contents)
10866

10967
log.debug("Wrote %s with APT pipeline setting" % f_name )
11068

doc/examples/cloud-config.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ apt_mirror_search:
4545
# apt_proxy (configure Acquire::HTTP::Proxy)
4646
apt_proxy: http://my.apt.proxy:3128
4747

48-
# apt_pipelining (confiugure Acquire::http::Pipeline-Depth)
48+
# apt_pipelining (configure Acquire::http::Pipeline-Depth)
4949
# Default: disables HTTP pipelining. Certain web servers, such
5050
# as S3 do not pipeline properly.
5151
# Valid options:
52-
# True/Default: Enables OS default
53-
# False: Disables pipelining all-together
52+
# False/default: Disables pipelining for APT
53+
# None/Unchanged: Use OS default
5454
# Number: Set pipelining to some number (not recommended)
5555
apt_pipelining: False
5656

0 commit comments

Comments
 (0)