-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(MODULES-6697) Implement packageprovider type and provider #9
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,82 @@ | ||
require 'json' | ||
|
||
Puppet::Type.type(:pspackageprovider).provide :powershellcore do | ||
confine operatingsystem: :windows | ||
commands pwsh: 'pwsh' | ||
|
||
mk_resource_methods | ||
|
||
def self.invoke_ps_command(command) | ||
# override_locale is necessary otherwise the Install-Module commands silently fails on Linux | ||
result = Puppet::Util::Execution.execute(['pwsh', '-NoProfile', '-NonInteractive', '-NoLogo', '-Command', | ||
"$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; #{command}"], | ||
override_locale: false) | ||
result.lines | ||
end | ||
|
||
def initialize(value = {}) | ||
super(value) | ||
@property_flush = {} | ||
end | ||
|
||
def self.prefetch(resources) | ||
instances.each do |prov| | ||
if resource = resources[prov.name] | ||
resource.provider = prov | ||
end | ||
end | ||
end | ||
|
||
def self.instances | ||
result = invoke_ps_command instances_command | ||
result.each.collect do |line| | ||
p = JSON.parse(line.strip, symbolize_names: true) | ||
p[:ensure] = :present | ||
new(p) | ||
end | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present | ||
end | ||
|
||
def create | ||
self.class.invoke_ps_command install_command | ||
@property_hash[:ensure] = :present | ||
end | ||
|
||
def flush | ||
unless @property_flush.empty? | ||
flush_command = "PackageManagement\\Install-PackageProvider -Name #{@resource[:name]}" | ||
@property_flush.each do |key, value| | ||
if @property_flush[:version] | ||
flush_command << " -RequiredVersion '#{value}'" | ||
else | ||
flush_command << " -#{key} '#{value}'" | ||
end | ||
end | ||
flush_command < " -Force" | ||
self.class.invoke_ps_command flush_command | ||
end | ||
@property_hash = @resource.to_hash | ||
end | ||
|
||
def self.instances_command | ||
<<-COMMAND | ||
@(Get-PackageProvider).foreach({ | ||
[ordered]@{ | ||
'name' = $_.Name.ToLower() | ||
'version' = $_.Version.ToString() | ||
} | ConvertTo-Json -Depth 99 -Compress | ||
}) | ||
COMMAND | ||
end | ||
|
||
def install_command | ||
command = [] | ||
command << "PackageManagement\\Install-PackageProvider -Name #{@resource[:name]}" | ||
command << " -Force" | ||
command.join | ||
end | ||
|
||
end |
10 changes: 10 additions & 0 deletions
10
lib/puppet/provider/pspackageprovider/windowspowershell.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,10 @@ | ||
Puppet::Type.type(:pspackageprovider).provide(:windowspowershell, parent: :powershellcore) do | ||
confine operatingsystem: :windows | ||
commands powershell: 'powershell' | ||
|
||
def self.invoke_ps_command(command) | ||
result = powershell(['-NoProfile', '-ExecutionPolicy', 'Bypass', '-NonInteractive', '-NoLogo', '-Command', | ||
"$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; #{command}"]) | ||
result.lines | ||
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,32 @@ | ||
Puppet::Type.newtype(:pspackageprovider) do | ||
@doc = 'Manage PowerShell Package providers for PowerShell modules' | ||
|
||
newproperty(:ensure) do | ||
newvalue(:present) do | ||
provider.create | ||
end | ||
end | ||
|
||
newparam(:name, :namevar => true) do | ||
desc 'The name of the package provider' | ||
validate do |value| | ||
if value.nil? or value.empty? | ||
raise ArgumentError, "A non-empty #{self.name.to_s} must be specified." | ||
end | ||
fail "#{self.name.to_s} should be a String" unless value.is_a? ::String | ||
fail("#{value} is not a valid #{self.name.to_s}") unless value =~ /^[a-zA-Z0-9\.\-\_\'\s]+$/ | ||
end | ||
munge(&:downcase) | ||
end | ||
|
||
newproperty(:version) do | ||
desc 'The version for a PowerShell Package Provider' | ||
validate do |value| | ||
if value.nil? or value.empty? | ||
raise ArgumentError, "A non-empty #{self.name.to_s} must be specified." | ||
end | ||
fail "#{self.name.to_s} should be a String" unless value.is_a? ::String | ||
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,76 @@ | ||
require 'spec_helper' | ||
|
||
provider_class = Puppet::Type.type(:pspackageprovider).provider(:windowspowershell) | ||
|
||
describe provider_class do | ||
|
||
before(:each) do | ||
type = Puppet::Type.type(:pspackageprovider).new( | ||
name: 'repo' | ||
) | ||
@provider_instance = provider_class.new(type) | ||
allow(provider_class).to receive(:invoke_ps_command).and_return(nil) | ||
allow(provider_class).to receive(:invoke_ps_command).with( | ||
provider_class.instances_command | ||
).and_return( | ||
[ | ||
'{"name":"Repo1"}', | ||
'{"name":"Repo2"}' | ||
] | ||
) | ||
end | ||
|
||
describe :instances do | ||
specify 'returns an array of :windowspowershell providers' do | ||
instances = provider_class.instances | ||
expect(instances.count).to eq(2) | ||
expect(instances).to all(be_instance_of(provider_class)) | ||
end | ||
specify 'sets the property hash for each provider' do | ||
instances = provider_class.instances | ||
expect(instances[0].instance_variable_get('@property_hash')).to eq( | ||
name: 'Repo1', ensure: :present | ||
) | ||
expect(instances[1].instance_variable_get('@property_hash')).to eq( | ||
name: 'Repo2', ensure: :present | ||
) | ||
end | ||
end | ||
|
||
describe :prefetch do | ||
specify 'sets the provider instance of the managed resource to a provider with the fetched state' do | ||
repo_resource1 = spy('pspackageprovider', name: 'Repo1') | ||
repo_resource2 = spy('pspackageprovider', name: 'Repo2') | ||
provider_class.prefetch( | ||
'Repo1' => repo_resource1, | ||
'Repo2' => repo_resource2 | ||
) | ||
expect(repo_resource1).to have_received(:provider=).with( | ||
provider_class.instances[0] | ||
) | ||
expect(repo_resource2).to have_received(:provider=).with( | ||
provider_class.instances[1] | ||
) | ||
end | ||
end | ||
|
||
describe :exists? do | ||
specify 'returns true if the resource already exists' do | ||
existing_instance = provider_class.instances[0] | ||
expect(existing_instance.exists?).to be true | ||
end | ||
specify 'returns false if the resource does not exist' do | ||
expect(@provider_instance.exists?).to be false | ||
end | ||
end | ||
|
||
describe :create do | ||
specify 'calls install-packageprovider with parameters' do | ||
@provider_instance.create | ||
expect(provider_class).to have_received(:invoke_ps_command).with( | ||
"PackageManagement\\Install-PackageProvider -Name repo -Force" | ||
) | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the name is always ToLower I think you need a corresponding munge(&:downcase) in the type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Munge added