From 9218cc5e7046da43547d694a411f0ca67ac9cb7e Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Tue, 6 Feb 2024 13:49:06 -0800 Subject: [PATCH] fb_apt: Add ability to log output of `apt-get update` (#232) Test Plan: Imported from GitHub, without a `Test Plan:` line. Tested with a basic regression test on a twshared host, though this cookbook isn't used internally and this should be a no-op Differential Revision: D52880450 fbshipit-source-id: 1c4a1cc20837cb0cafd95f432f73639fce3a4eb6 --- cookbooks/fb_apt/README.md | 6 ++++++ cookbooks/fb_apt/attributes/default.rb | 1 + cookbooks/fb_apt/recipes/default.rb | 8 +++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cookbooks/fb_apt/README.md b/cookbooks/fb_apt/README.md index 040549ade..560f2732f 100644 --- a/cookbooks/fb_apt/README.md +++ b/cookbooks/fb_apt/README.md @@ -22,6 +22,7 @@ Attributes * node['fb_apt']['want_source'] * node['fb_apt']['preserve_unknown_keyrings'] * node['fb_apt']['allow_modified_pkg_keyrings'] +* node['fb_apt']['apt_update_log_path'] Usage ----- @@ -107,3 +108,8 @@ As mentioned above, `fb_apt` can assemble the basic sources for you. It uses the LSB "codename" of the current systemd to build the URLs. In the event you want to use Chef to upgrade across distros, however, you can set `node['fb_apt']['distro']` to the appropriate name and it will be used instead. + +### Logging `apt-get update` +Set `node['fb_apt']['apt_update_log_path']` to log stdout and stderr of the +`apt-get update` command invoked by this cookbook. This may be useful for +debugging purposes. The caller must handle log rotation. diff --git a/cookbooks/fb_apt/attributes/default.rb b/cookbooks/fb_apt/attributes/default.rb index 21364dbc1..f4f21781f 100644 --- a/cookbooks/fb_apt/attributes/default.rb +++ b/cookbooks/fb_apt/attributes/default.rb @@ -38,6 +38,7 @@ 'want_source' => false, 'preserve_unknown_keyrings' => false, 'allow_modified_pkg_keyrings' => false, + 'apt_update_log_path' => nil, } # fb_apt must be defined for this to work... keys = FB::Apt.get_official_keyids(node).map { |id| [id, nil] }.to_h diff --git a/cookbooks/fb_apt/recipes/default.rb b/cookbooks/fb_apt/recipes/default.rb index 40be50fbb..56c0c6a44 100644 --- a/cookbooks/fb_apt/recipes/default.rb +++ b/cookbooks/fb_apt/recipes/default.rb @@ -18,6 +18,8 @@ # limitations under the License. # +require 'shellwords' + unless node.debian? || node.ubuntu? fail 'fb_apt is only supported on Debian and Ubuntu.' end @@ -94,7 +96,11 @@ end execute 'apt-get update' do - command 'apt-get update' + command lazy do + log_path = node['fb_apt']['apt_update_log_path'] + cmd_suffix = " >>#{Shellwords.shellescape(log_path)} 2>&1" if log_path + "apt-get update#{cmd_suffix}" + end action :nothing end