forked from chocolatey-archive/puppet-chocolatey
-
Notifications
You must be signed in to change notification settings - Fork 69
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 #17 from ferventcoder/ticket/master/MODULES-3035-c…
…onfig (MODULES-3035) Manage configuration settings
- Loading branch information
Showing
14 changed files
with
951 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
require 'puppet/type' | ||
require 'pathname' | ||
require 'rexml/document' | ||
|
||
Puppet::Type.type(:chocolateyconfig).provide(:windows) do | ||
confine :operatingsystem => :windows | ||
defaultfor :operatingsystem => :windows | ||
|
||
require Pathname.new(__FILE__).dirname + '../../../' + 'puppet_x/chocolatey/chocolatey_common' | ||
include PuppetX::Chocolatey::ChocolateyCommon | ||
|
||
CONFIG_MINIMUM_SUPPORTED_CHOCO_VERSION = '0.9.10.0' | ||
|
||
commands :chocolatey => PuppetX::Chocolatey::ChocolateyCommon.chocolatey_command | ||
|
||
def initialize(value={}) | ||
super(value) | ||
@property_flush = {} | ||
end | ||
|
||
def properties | ||
if @property_hash.empty? | ||
@property_hash = query || { :ensure => ( :absent )} | ||
@property_hash[:ensure] = :absent if @property_hash.empty? | ||
end | ||
@property_hash.dup | ||
end | ||
|
||
def query | ||
self.class.configs.each do |config| | ||
return config.properties if @resource[:name][/\A\S*/].downcase == config.name.downcase | ||
end | ||
|
||
return {} | ||
end | ||
|
||
def self.get_configs | ||
PuppetX::Chocolatey::ChocolateyCommon.set_env_chocolateyinstall | ||
|
||
choco_config = PuppetX::Chocolatey::ChocolateyCommon.choco_config_file | ||
raise Puppet::ResourceError, "Config file not found for Chocolatey. Please make sure you have Chocolatey installed." if choco_config.nil? | ||
raise Puppet::ResourceError, "An install was detected, but was unable to locate config file at #{choco_config}." unless PuppetX::Chocolatey::ChocolateyCommon.file_exists?(choco_config) | ||
|
||
Puppet.debug("Gathering sources from '#{choco_config}'.") | ||
config = REXML::Document.new File.new(choco_config, 'r') | ||
|
||
config.elements.to_a( '//add' ) | ||
end | ||
|
||
def self.get_config(element) | ||
config = {} | ||
return config if element.nil? | ||
|
||
config[:name] = element.attributes['key'] if element.attributes['key'] | ||
config[:value] = element.attributes['value'] if element.attributes['value'] | ||
config[:description] = element.attributes['description'] if element.attributes['description'] | ||
|
||
config[:ensure] = :present | ||
|
||
Puppet.debug("Loaded config '#{config.inspect}'.") | ||
|
||
config | ||
end | ||
|
||
def self.configs | ||
@configs ||= get_configs.collect do |item| | ||
config = get_config(item) | ||
new(config) | ||
end | ||
end | ||
|
||
def self.refresh_configs | ||
@configs = nil | ||
self.configs | ||
end | ||
|
||
def self.instances | ||
configs | ||
end | ||
|
||
def self.prefetch(resources) | ||
instances.each do |provider| | ||
if (resource = resources[provider.name]) | ||
resource.provider = provider | ||
end | ||
end | ||
end | ||
|
||
def create | ||
@property_flush[:ensure] = :present | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present | ||
end | ||
|
||
def destroy | ||
@property_flush[:ensure] = :absent | ||
end | ||
|
||
def validate | ||
choco_version = Gem::Version.new(PuppetX::Chocolatey::ChocolateyCommon.choco_version) | ||
if choco_version < Gem::Version.new(CONFIG_MINIMUM_SUPPORTED_CHOCO_VERSION) | ||
raise Puppet::ResourceError, "Chocolatey version must be '#{CONFIG_MINIMUM_SUPPORTED_CHOCO_VERSION}' to manage configuration values. Detected '#{choco_version}' as your version. Please upgrade Chocolatey." | ||
end | ||
end | ||
|
||
mk_resource_methods | ||
|
||
def flush | ||
choco_version = Gem::Version.new(PuppetX::Chocolatey::ChocolateyCommon.choco_version) | ||
|
||
args = [] | ||
args << 'config' | ||
|
||
command = 'set' | ||
command = 'unset' if @property_flush[:ensure] == :absent | ||
|
||
args << command | ||
args << '--name' << resource[:name] | ||
|
||
if @property_flush[:ensure] != :absent | ||
args << '--value' << resource[:value] | ||
end | ||
|
||
begin | ||
Puppet::Util::Execution.execute([command(:chocolatey), *args]) | ||
rescue Puppet::ExecutionFailure => e | ||
raise Puppet::Error, "An error occurred running choco. Unable to set Chocolateyconfig[#{self.name}]: #{e}" | ||
end | ||
|
||
@property_hash.clear | ||
@property_hash = { :ensure => ( @property_flush[:ensure] )} | ||
|
||
@property_flush.clear | ||
|
||
self.class.refresh_configs | ||
@property_hash = query | ||
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,85 @@ | ||
require 'puppet/type' | ||
require 'pathname' | ||
|
||
Puppet::Type.newtype(:chocolateyconfig) do | ||
|
||
@doc = <<-'EOT' | ||
Allows managing config settings for Chocolatey. | ||
Configuration values provide settings for users | ||
to configure aspects of Chocolatey and the way it | ||
functions. Similar to features, except allow for user | ||
configured values. Requires 0.9.10+. Learn more about | ||
config at https://chocolatey.org/docs/commands-config | ||
EOT | ||
|
||
ensurable do | ||
newvalue(:present) { provider.create } | ||
newvalue(:absent) { provider.destroy } | ||
defaultto :present | ||
|
||
def retrieve | ||
provider.properties[:ensure] | ||
end | ||
|
||
end | ||
|
||
newparam(:name) do | ||
desc "The name of the config setting. Used for uniqueness. | ||
Puppet is not able to easily manage any values that | ||
include Password in the key name in them as they | ||
will be encrypted in the configuration file." | ||
|
||
validate do |value| | ||
if value.nil? or value.empty? | ||
raise ArgumentError, "A non-empty name must be specified." | ||
end | ||
end | ||
|
||
isnamevar | ||
|
||
munge do |value| | ||
value.downcase | ||
end | ||
|
||
def insync?(is) | ||
is.downcase == should.downcase | ||
end | ||
end | ||
|
||
newproperty(:value) do | ||
desc "The value of the config setting. If the | ||
name includes 'password', then the value is | ||
not ensurable due to being encrypted in the | ||
configuration file." | ||
|
||
validate do |value| | ||
if value.nil? or value.empty? | ||
raise ArgumentError, "A non-empty value must be specified. To unset value, use ensure => absent" | ||
end | ||
end | ||
|
||
def insync?(is) | ||
if (resource[:name] =~ /password/i) | ||
# If name contains password, it is | ||
# always in sync if there is a value | ||
return (is.nil? || is.empty?) == (should.nil? || should.empty?) | ||
else | ||
return is.downcase == should.downcase | ||
end | ||
end | ||
end | ||
|
||
validate do | ||
if self[:ensure] != :absent | ||
raise ArgumentError, "Unless ensure => absent, value is required." if self[:value].nil? || self[:value].empty? | ||
end | ||
|
||
if provider.respond_to?(:validate) | ||
provider.validate | ||
end | ||
end | ||
|
||
autorequire(:exec) do | ||
['install_chocolatey_official'] | ||
end | ||
end |
Oops, something went wrong.