1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
1919import cloudinit .util as util
20- import cloudinit .SshUtil as sshutil
2120import re
2221import 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
2827def 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
5144def 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
0 commit comments