forked from CERIT-SC/puppet-yum
-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from EmersonPrado/issue_310
Support dnf module management - Fix #310
- Loading branch information
Showing
7 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
lib/puppet/provider/dnf_module_stream/dnf_module_stream.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
Puppet::Type.type(:dnf_module_stream).provide(:dnf_module_stream) do | ||
desc 'Unique provider' | ||
|
||
confine package_provider: 'dnf' | ||
|
||
commands dnf: 'dnf' | ||
|
||
# Converts plain output from 'dnf module list <Module>' to an array formatted as: | ||
# { | ||
# default_stream: "<Default stream> (if there's one)", | ||
# enabled_stream: "<Enabled stream> (if there's one)", | ||
# available_streams: ["<Stream>", "<Stream>", ...,] | ||
# } | ||
def dnf_output_2_hash(dnf_output) | ||
module_hash = { available_streams: [] } | ||
dnf_output.lines.each do |line| | ||
line.chomp! | ||
break if line.empty? | ||
|
||
# @stream_start and @stream_length: chunk of dnf output line with stream info | ||
# Determined in elsif block below from dnf output header | ||
if !@stream_start.nil? | ||
# Stream string is '<Stream>', '<Stream> [d][e]', or the like | ||
stream_string = line[@stream_start, @stream_length].rstrip | ||
stream = stream_string.split[0] | ||
module_hash[:default_stream] = stream if stream_string.include?('[d]') | ||
module_hash[:enabled_stream] = stream if stream_string.include?('[e]') | ||
module_hash[:available_streams] << stream | ||
elsif line.split[0] == 'Name' | ||
# 'dnf module list' output header is 'Name<Spaces>Stream<Spaces>Profiles<Spaces>...' | ||
# Each field has same position of data that follows | ||
@stream_start = line[%r{Name\s+}].length | ||
@stream_length = line[%r{Stream\s+}].length | ||
end | ||
end | ||
module_hash | ||
end | ||
|
||
# Gets module default, enabled and available streams | ||
# Output formatted by function dnf_output_2_hash | ||
def streams_state(module_name) | ||
# This function can be called multiple times in the same resource call | ||
return unless @streams_current_state.nil? | ||
|
||
dnf_output = dnf('-q', 'module', 'list', module_name) | ||
rescue Puppet::ExecutionFailure | ||
# Assumes any execution error happens because module doesn't exist | ||
raise ArgumentError, "Module \"#{module_name}\" not found" | ||
else | ||
@streams_current_state = dnf_output_2_hash(dnf_output) | ||
end | ||
|
||
def disable_stream(module_name) | ||
dnf('-y', 'module', 'reset', module_name) | ||
end | ||
|
||
def enable_stream(module_name, target_stream) | ||
action = @streams_current_state.key?(:enabled_stream) ? 'switch-to' : 'enable' | ||
dnf('-y', 'module', action, "#{module_name}:#{target_stream}") | ||
end | ||
|
||
def stream | ||
streams_state(resource[:module]) | ||
case resource[:stream] | ||
when :absent | ||
# Act if any stream is enabled | ||
@streams_current_state.key?(:enabled_stream) ? @streams_current_state[:enabled_stream] : :absent | ||
when :default | ||
# Act if default stream isn't enabled | ||
# Specified stream = :default requires an existing default stream | ||
raise ArgumentError, "No default stream to enable in module \"#{resource[:module]}\"" unless | ||
@streams_current_state.key?(:default_stream) | ||
|
||
@streams_current_state[:enabled_stream] == @streams_current_state[:default_stream] ? :default : @streams_current_state[:enabled_stream] | ||
when :present | ||
# Act if no stream is enabled | ||
# Specified stream = :default requires an existing default or enabled stream | ||
raise ArgumentError, "No default stream to enable in module \"#{resource[:module]}\"" unless | ||
@streams_current_state.key?(:default_stream) || @streams_current_state.key?(:enabled_stream) | ||
|
||
@streams_current_state.key?(:enabled_stream) ? :present : :absent | ||
else | ||
# Act if specified stream isn't enabled | ||
@streams_current_state[:enabled_stream] | ||
end | ||
end | ||
|
||
def stream=(target_stream) | ||
case target_stream | ||
when :absent | ||
disable_stream(resource[:module]) | ||
when :default, :present | ||
enable_stream(resource[:module], @streams_current_state[:default_stream]) | ||
else | ||
enable_stream(resource[:module], target_stream) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
Puppet::Type.newtype(:dnf_module_stream) do | ||
@doc = <<-TYPE_DOC | ||
@summary Manage DNF module streams | ||
@example Enable MariaDB default stream | ||
dnf_module_stream { 'mariadb': | ||
stream => default, | ||
} | ||
@example Enable MariaDB 10.5 stream | ||
dnf_module_stream { 'mariadb': | ||
stream => '10.5', | ||
} | ||
@example Disable MariaDB streams | ||
dnf_module_stream { 'mariadb': | ||
stream => absent, | ||
} | ||
@param module | ||
Module to be managed - Defaults to title | ||
@param stream | ||
Module stream to be enabled | ||
This type allows Puppet to enable/disable streams via DNF modules | ||
TYPE_DOC | ||
|
||
newparam(:title, namevar: true) do | ||
desc 'Resource title' | ||
newvalues(%r{.+}) | ||
end | ||
|
||
newparam(:module) do | ||
desc 'DNF module to be managed' | ||
newvalues(%r{.+}) | ||
end | ||
|
||
newproperty(:stream) do | ||
desc <<-EOS | ||
Module stream that should be enabled | ||
String - Specify stream | ||
present - Keep current enabled stream if any, otherwise enable default one | ||
default - Enable default stream | ||
absent - No stream (resets module) | ||
EOS | ||
newvalues(:present, :default, :absent, %r{.+}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
spec/unit/puppet/provider/dnf_module_stream/dnf_module_stream_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe 'the dnf_module_stream provider' do | ||
it 'loads' do | ||
expect(Puppet::Type.type(:dnf_module_stream).provide(:dnf_module_stream)).not_to be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
dnf_module_stream = Puppet::Type.type(:dnf_module_stream) | ||
RSpec.describe 'the dnf_module_stream type' do | ||
it 'loads' do | ||
expect(dnf_module_stream).not_to be_nil | ||
end | ||
|
||
it 'has parameter module' do | ||
expect(dnf_module_stream.parameters).to be_include(:module) | ||
end | ||
|
||
it 'has property stream' do | ||
expect(dnf_module_stream.properties.map(&:name)).to be_include(:stream) | ||
end | ||
end |