Skip to content

Commit

Permalink
fb_apt: Add ability to log output of apt-get update (#232)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adsr authored and facebook-github-bot committed Feb 6, 2024
1 parent 65ec06e commit 9218cc5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cookbooks/fb_apt/README.md
Expand Up @@ -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
-----
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions cookbooks/fb_apt/attributes/default.rb
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion cookbooks/fb_apt/recipes/default.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

1 comment on commit 9218cc5

@taylorific
Copy link

@taylorific taylorific commented on 9218cc5 Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to cause a regression if node['fb_apt']['apt_update_log_path'] is not set.

It seems like the code should have curly braces instead:

execute 'apt-get update' do
  command lazy {
    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}"
  }
  action :nothing
end

I'm getting the following error with this code when I try to use chef 18:
https://github.com/boxcutter/boxcutter-chef-cookbooks/actions/runs/7862940576/job/21452971460

================================================================================
       Recipe Compile Error in /opt/kitchen/cache/cookbooks/boxcutter_init/recipes/default.rb
       ================================================================================
       
       ArgumentError
       -------------
       tried to create Proc object without a block
       
       Cookbook Trace: (most recent call first)
       ----------------------------------------
         /opt/kitchen/cache/cookbooks/fb_apt/recipes/default.rb:99:in `block in from_file'
         /opt/kitchen/cache/cookbooks/fb_apt/recipes/default.rb:98:in `from_file'
         /opt/kitchen/cache/cookbooks/boxcutter_init/recipes/default.rb:35:in `from_file'
       
       Relevant File Content:
       ----------------------
       /opt/kitchen/cache/cookbooks/fb_apt/recipes/default.rb:
       
        92:  end
        93:  
        94:  fb_apt_sources_list 'populate sources list' do
        95:    notifies :run, 'execute[apt-get update]', :immediately
        96:  end
        97:  
        98:  execute 'apt-get update' do
        99>>   command lazy do
       100:      log_path = node['fb_apt']['apt_update_log_path']
       101:      cmd_suffix = " >>#{Shellwords.shellescape(log_path)} 2>&1" if log_path
       102:      "apt-get update#{cmd_suffix}"
       103:    end
       104:    action :nothing
       105:  end
       106:  
       107:  if Chef::VERSION.to_i >= 16
       108:    notify_group 'periodic package cache update' do
       
       System Info:
       ------------
       chef_version=18.2.7
       platform=ubuntu
       platform_version=20.04
       ruby=ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
       program_name=/opt/cinc/bin/cinc-client
       executable=/opt/cinc/bin/cinc-client

Please sign in to comment.