|
19 | 19 |
|
20 | 20 | action :set do |
21 | 21 | if new_resource.bin_cmds |
| 22 | + # I couldn't find a way to cleanly avoid repeating this variable declaration in both :set and :unset |
| 23 | + alternatives_cmd = node['platform_family'] == 'rhel' ? 'alternatives' : 'update-alternatives' |
22 | 24 | new_resource.bin_cmds.each do |cmd| |
23 | 25 |
|
24 | 26 | bin_path = "/usr/bin/#{cmd}" |
|
31 | 33 | end |
32 | 34 |
|
33 | 35 | # install the alternative if needed |
34 | | - alternative_exists = shell_out("update-alternatives --display #{cmd} | grep #{alt_path}").exitstatus == 0 |
| 36 | + alternative_exists = shell_out("#{alternatives_cmd} --display #{cmd} | grep #{alt_path}").exitstatus == 0 |
35 | 37 | unless alternative_exists |
36 | 38 | description = "Add alternative for #{cmd}" |
37 | 39 | converge_by(description) do |
38 | 40 | Chef::Log.debug "Adding alternative for #{cmd}" |
39 | | - install_cmd = shell_out("update-alternatives --install #{bin_path} #{cmd} #{alt_path} #{priority}") |
| 41 | + install_cmd = shell_out("#{alternatives_cmd} --install #{bin_path} #{cmd} #{alt_path} #{priority}") |
40 | 42 | unless install_cmd.exitstatus == 0 |
41 | 43 | Chef::Application.fatal!(%Q[ set alternative failed ]) |
42 | 44 | end |
|
46 | 48 |
|
47 | 49 | # set the alternative if default |
48 | 50 | if new_resource.default |
49 | | - alternative_is_set = shell_out("update-alternatives --display #{cmd} | grep \"link currently points to #{alt_path}\"").exitstatus == 0 |
| 51 | + alternative_is_set = shell_out("#{alternatives_cmd} --display #{cmd} | grep \"link currently points to #{alt_path}\"").exitstatus == 0 |
50 | 52 | unless alternative_is_set |
51 | 53 | description = "Set alternative for #{cmd}" |
52 | 54 | converge_by(description) do |
53 | 55 | Chef::Log.debug "Setting alternative for #{cmd}" |
54 | | - set_cmd = shell_out("update-alternatives --set #{cmd} #{alt_path}") |
| 56 | + set_cmd = shell_out("#{alternatives_cmd} --set #{cmd} #{alt_path}") |
55 | 57 | unless set_cmd.exitstatus == 0 |
56 | 58 | Chef::Application.fatal!(%Q[ set alternative failed ]) |
57 | 59 | end |
|
64 | 66 | end |
65 | 67 |
|
66 | 68 | action :unset do |
| 69 | + # I couldn't find a way to cleanly avoid repeating this variable declaration in both :set and :unset |
| 70 | + alternatives_cmd = node['platform_family'] == 'rhel' ? 'alternatives' : 'update-alternatives' |
67 | 71 | new_resource.bin_cmds.each do |cmd| |
68 | 72 | alt_path = "#{new_resource.java_location}/bin/#{cmd}" |
69 | | - cmd = shell_out("update-alternatives --remove #{cmd} #{alt_path}") |
| 73 | + cmd = shell_out("#{alternatives_cmd} --remove #{cmd} #{alt_path}") |
70 | 74 | if cmd.exitstatus == 0 |
71 | 75 | new_resource.updated_by_last_action(true) |
72 | 76 | end |
|
0 commit comments