chef / chef

Chef Infra, a powerful automation platform that transforms infrastructure into code automating how infrastructure is configured, deployed and managed across any environment, at any scale
http://www.chef.io/chef/
Apache License 2.0
7.59k stars 2.51k forks source link

homebrew_update TypeError when running under launchd #14033

Open brandonfriess-stripe opened 12 months ago

brandonfriess-stripe commented 12 months ago

Description

I've been seeing an increase of failures in the homebrew_update resource when Chef is being ran under launchd on macOS.

The error is this:

      ================================================================================
      Error executing action `periodic` on resource 'homebrew_update[macOS]'
      ================================================================================

      TypeError
      ---------
      no implicit conversion of false into String

      Cookbook Trace: (most recent call first)
      ----------------------------------------
      /var/chef/cache/cookbooks/cpe_homebrew/resources/cpe_homebrew_manage.rb:40:in `block in class_from_file'

      Resource Declaration:
      ---------------------
      # In /var/chef/cache/cookbooks/cpe_homebrew/resources/cpe_homebrew_manage.rb

       40:     homebrew_update 'macOS' do
       41:       frequency 86400
       42:       action :periodic
       43:       ignore_failure true
       44:       retries 1
       45:     end
       46:   end

Chef Version

18.3.0

Platform Version

macOS

Replication Case

Add the homebrew_update resource to a recipe and run Chef job via launchd.

(i.e. sudo launchctl unload /Library/LaunchDaemon/com.chef.chef-client.plist && sudo launchctl load /Library/LaunchDaemon/com.chef.chef-client.plist)

Client Output

[2023-10-19T10:46:37-05:00] ERROR: homebrew_update[macOS] (cpe_homebrew::default line 40) had an error: TypeError: no implicit conversion of false into String; ignore_failure is set, continuing

      ================================================================================
      Error executing action `periodic` on resource 'homebrew_update[macOS]'
      ================================================================================

      TypeError
      ---------
      no implicit conversion of false into String

      Cookbook Trace: (most recent call first)
      ----------------------------------------
      /var/chef/cache/cookbooks/cpe_homebrew/resources/cpe_homebrew_manage.rb:40:in `block in class_from_file'

      Resource Declaration:
      ---------------------
      # In /var/chef/cache/cookbooks/cpe_homebrew/resources/cpe_homebrew_manage.rb

       40:     homebrew_update 'macOS' do
       41:       frequency 86400
       42:       action :periodic
       43:       ignore_failure true
       44:       retries 1
       45:     end
       46:   end

      Compiled Resource:
      ------------------
      # Declared in /var/chef/cache/cookbooks/cpe_homebrew/resources/cpe_homebrew_manage.rb:40:in `block in class_from_file'

      homebrew_update("macOS") do
        action [:periodic]
        default_guard_interpreter :default
        declared_type :homebrew_update
        cookbook_name "cpe_homebrew"
        recipe_name "default"
        frequency 86400
        ignore_failure true
        retries 1
      end

      System Info:
      ------------
      chef_version=18.3.0
      platform=mac_os_x
      platform_version=14.0
      ruby=ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin20]
      program_name=/opt/chef/bin/chef-client
      executable=/opt/chef/bin/chef-client

Stacktrace

brandonfriess-stripe commented 12 months ago

Possibly related to changes in https://github.com/chef/chef/pull/13669 ?

I'm puzzled how false is being surfaced here when I clearly have the right types for the resource.

jazaval commented 5 months ago

@brandonfriess-stripe are you still seeing this on newer client versions like 18.3.x, 18.4.x? I ran into a similar TypeError which I believe is caused by the Chef::Mixin::HomebrewUser.find_homebrew_username. I can see why running under the launchctl/root context might also make a repro occur here.

https://github.com/chef/chef/issues/14369

brandonfriess-stripe commented 5 months ago

@jazaval I ended up working around it by adding the brew binary path, user, and home dir to the env.

For whatever reason, Chef doesn't expose a property to set the homebrew user or binary path. However...

Deep in the homebrew_update resource they are calling the execute resource and I'm hi-jacking that process to set the ENV to the right things so that this resource will fire correctly under launchd.

Ex:

action :manage do
  if node['cpe_homebrew']['auto-update']
    # Set PATH for Homebrew auto-update so it works in launchd
    ENV['PATH'] = if node.arm64?
                    "/opt/homebrew/bin:/opt/homebrew/sbin:#{ENV['PATH']}"
                  else
                    "/usr/local/bin:/usr/local/sbin:#{ENV['PATH']}"
                  end
    ENV['HOME'] = ::Dir.home(node['cpe_homebrew']['user'])
    ENV['USER'] = node['cpe_homebrew']['user']
    homebrew_update 'macOS' do
      frequency 86_400
      action :periodic
      ignore_failure true
      retries 1
    end
  end
end

For your case, I wonder if you set the owner property to the primary user, it would probably work for you.