From cf06a973e3494428c36725d649a06a4664421976 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 6 May 2020 17:22:10 -0700 Subject: fix launchd provider for chef-16 the refactoring to use the DSL (orthogonal to setting this up as a unified_mode resource) meant we have to care about lexical scoping now. Signed-off-by: Lamont Granquist --- lib/chef/provider/launchd.rb | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index fbb9307712..0eabd821e2 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -36,7 +36,6 @@ class Chef label mode owner - path source session_type type @@ -44,7 +43,6 @@ class Chef def load_current_resource current_resource = Chef::Resource::Launchd.new(new_resource.name) - @path = path ? path : gen_path_from_type end def gen_path_from_type @@ -89,26 +87,20 @@ class Chef end def manage_plist(action) + path = @path if source - cookbook_file @path do - cookbook_name = cookbook if cookbook - name(@path) if @path - backup(backup) if backup - group(group) if group - mode(mode) if mode - owner(owner) if owner - source(source) if source + cookbook_file path do + cookbook_name = new_resource.cookbook if new_resource.cookbook + name(path) if path + copy_properties_from(new_resource, :backup, :group, :mode, :owner, :source) action(action) only_if { manage_agent?(action) } end else - file @path do - name(@path) if @path - backup(backup) if backup + file path do + name(path) if path + copy_properties_from(new_resource, :backup, :group, :mode, :owner) content(content) if content? - group(group) if group - mode(mode) if mode - owner(owner) if owner action(action) only_if { manage_agent?(action) } end @@ -116,11 +108,12 @@ class Chef end def manage_service(action) + path = @path macosx_service label do - name(label) if label - service_name(label) if label - plist(@path) if @path - session_type(session_type) if session_type + name(new_resource.label) if new_resource.label + service_name(new_resource.label) if new_resource.label + plist(path) if path + copy_properties_from(new_resource, :session_type) action(action) only_if { manage_agent?(action) } end @@ -215,6 +208,11 @@ class Chef memo[val] = new_resource.send(key) if new_resource.send(key) end end + + # @api private + def path + @path = new_resource.path ? new_resource.path : gen_path_from_type + end end end end -- cgit v1.2.1 From 1c2b0bf2c01dd8543efceb6384f41852a342c7b4 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 6 May 2020 17:24:34 -0700 Subject: make it real obvious we're doing lexical scoping Signed-off-by: Lamont Granquist --- lib/chef/provider/launchd.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index 0eabd821e2..3be2f2995d 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -87,7 +87,7 @@ class Chef end def manage_plist(action) - path = @path + path = path if source cookbook_file path do cookbook_name = new_resource.cookbook if new_resource.cookbook @@ -108,7 +108,7 @@ class Chef end def manage_service(action) - path = @path + path = path macosx_service label do name(new_resource.label) if new_resource.label service_name(new_resource.label) if new_resource.label -- cgit v1.2.1 From 4e973b1db3bc5b2e50bdcf90c72f6446ca448a2b Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 6 May 2020 17:26:25 -0700 Subject: fix the scoping problems with file_content Signed-off-by: Lamont Granquist --- lib/chef/provider/launchd.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index 3be2f2995d..a79bfdb1e8 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -100,7 +100,7 @@ class Chef file path do name(path) if path copy_properties_from(new_resource, :backup, :group, :mode, :owner) - content(content) if content? + content(file_content) if file_content? action(action) only_if { manage_agent?(action) } end @@ -148,11 +148,11 @@ class Chef end end - def content? - !!content + def file_content? + !!file_content end - def content + def file_content plist_hash = new_resource.plist_hash || gen_hash ::Plist::Emit.dump(plist_hash) unless plist_hash.nil? end -- cgit v1.2.1 From 4cd444e53f9ff745dcd349f7255c31f99da02070 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 6 May 2020 17:37:15 -0700 Subject: remove name properties (pointless) Signed-off-by: Lamont Granquist --- lib/chef/provider/launchd.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index a79bfdb1e8..644ced9b3d 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -91,14 +91,12 @@ class Chef if source cookbook_file path do cookbook_name = new_resource.cookbook if new_resource.cookbook - name(path) if path copy_properties_from(new_resource, :backup, :group, :mode, :owner, :source) action(action) only_if { manage_agent?(action) } end else file path do - name(path) if path copy_properties_from(new_resource, :backup, :group, :mode, :owner) content(file_content) if file_content? action(action) @@ -110,7 +108,6 @@ class Chef def manage_service(action) path = path macosx_service label do - name(new_resource.label) if new_resource.label service_name(new_resource.label) if new_resource.label plist(path) if path copy_properties_from(new_resource, :session_type) -- cgit v1.2.1 From dc6aea2908cf8274320898337722f702113f3cd5 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Wed, 6 May 2020 17:49:13 -0700 Subject: more lexical scoping fixes Signed-off-by: Lamont Granquist --- lib/chef/provider/launchd.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/chef/provider/launchd.rb b/lib/chef/provider/launchd.rb index 644ced9b3d..579a53fe03 100644 --- a/lib/chef/provider/launchd.rb +++ b/lib/chef/provider/launchd.rb @@ -87,7 +87,6 @@ class Chef end def manage_plist(action) - path = path if source cookbook_file path do cookbook_name = new_resource.cookbook if new_resource.cookbook @@ -106,10 +105,10 @@ class Chef end def manage_service(action) - path = path + plist_path = path macosx_service label do service_name(new_resource.label) if new_resource.label - plist(path) if path + plist(plist_path) if plist_path copy_properties_from(new_resource, :session_type) action(action) only_if { manage_agent?(action) } -- cgit v1.2.1 From 2d01023f5ef084449906aa75d16ed876220615fd Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Wed, 6 May 2020 18:06:58 -0700 Subject: Fix macOS sudo command. Signed-off-by: Pete Higgins --- lib/chef/provider/service/macosx.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb index 6e8663b7d4..ecd0e9e455 100644 --- a/lib/chef/provider/service/macosx.rb +++ b/lib/chef/provider/service/macosx.rb @@ -56,8 +56,10 @@ class Chef if @console_user @console_user = Etc.getpwuid(::File.stat("/dev/console").uid).name logger.trace("#{new_resource} console_user: '#{@console_user}'") - cmd = "su -l" - @base_user_cmd = cmd + "#{@console_user} -c" + + @base_user_cmd = "su -l #{@console_user} -c" + logger.trace("#{new_resource} base_user_cmd: '#{@base_user_cmd}'") + # Default LaunchAgent session should be Aqua @session_type = "Aqua" if @session_type.nil? end -- cgit v1.2.1 From 011eb4b1826d1f0cda4bddac526f938cc0cb6cef Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Wed, 6 May 2020 18:14:47 -0700 Subject: Update method name in launchd provider unit tests. Signed-off-by: Pete Higgins --- spec/unit/provider/launchd_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/unit/provider/launchd_spec.rb b/spec/unit/provider/launchd_spec.rb index 53b56b4960..69f0f315d0 100644 --- a/spec/unit/provider/launchd_spec.rb +++ b/spec/unit/provider/launchd_spec.rb @@ -131,8 +131,8 @@ describe Chef::Provider::Launchd do new_resource.program "/Library/scripts/call_mom.sh" new_resource.time_out 300 new_resource.start_calendar_interval "Hour" => 10, "Weekday" => 7 - expect(provider.content?).to be_truthy - expect(provider.content).to eql(test_plist) + expect(provider.file_content?).to be_truthy + expect(provider.file_content).to eql(test_plist) end end @@ -147,8 +147,8 @@ describe Chef::Provider::Launchd do new_resource.program "/Library/scripts/call_mom.sh" new_resource.time_out 300 new_resource.start_calendar_interval allowed - expect(provider.content?).to be_truthy - expect(provider.content).to eql(test_plist_multiple_intervals) + expect(provider.file_content?).to be_truthy + expect(provider.file_content).to eql(test_plist_multiple_intervals) end it "should allow all StartCalendarInterval keys" do @@ -162,9 +162,9 @@ describe Chef::Provider::Launchd do new_resource.program "/Library/scripts/call_mom.sh" new_resource.time_out 300 new_resource.start_calendar_interval allowed - expect(provider.content?).to be_truthy + expect(provider.file_content?).to be_truthy %w{Minute Hour Day Weekday Month}.each do |key| - expect(provider.content).to include("#{key}") + expect(provider.file_content).to include("#{key}") end end @@ -188,8 +188,8 @@ describe Chef::Provider::Launchd do describe "hash is passed" do it "should produce the test_plist content from the plist_hash property" do new_resource.plist_hash test_hash - expect(provider.content?).to be_truthy - expect(provider.content).to eql(test_plist) + expect(provider.file_content?).to be_truthy + expect(provider.file_content).to eql(test_plist) end end end -- cgit v1.2.1