From 54b3b8487a8f2c7bffb6289f2c5f1612898e0846 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Mon, 29 Dec 2014 13:48:56 +0100 Subject: [PATCH] Import first type & provider for gnocchi_config gnocchi_config will be used to configure /etc/gnocchi/gnocchi.conf file. --- lib/puppet/provider/gnocchi.rb | 113 ++++++++++++++++++ .../provider/gnocchi_config/ini_setting.rb | 27 +++++ lib/puppet/type/gnocchi_config.rb | 42 +++++++ 3 files changed, 182 insertions(+) create mode 100644 lib/puppet/provider/gnocchi.rb create mode 100644 lib/puppet/provider/gnocchi_config/ini_setting.rb create mode 100644 lib/puppet/type/gnocchi_config.rb diff --git a/lib/puppet/provider/gnocchi.rb b/lib/puppet/provider/gnocchi.rb new file mode 100644 index 00000000..f96982b1 --- /dev/null +++ b/lib/puppet/provider/gnocchi.rb @@ -0,0 +1,113 @@ +require 'json' +require 'puppet/util/inifile' + +class Puppet::Provider::Gnocchi < Puppet::Provider + + def self.conf_filename + '/etc/gnocchi/gnocchi.conf' + end + + def self.withenv(hash, &block) + saved = ENV.to_hash + hash.each do |name, val| + ENV[name.to_s] = val + end + + yield + ensure + ENV.clear + saved.each do |name, val| + ENV[name] = val + end + end + + def self.gnocchi_credentials + @gnocchi_credentials ||= get_gnocchi_credentials + end + + def self.get_gnocchi_credentials + auth_keys = ['auth_host', 'auth_port', 'auth_protocol', + 'admin_tenant_name', 'admin_user', 'admin_password'] + conf = gnocchi_conf + if conf and conf['keystone_authtoken'] and + auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?} + return Hash[ auth_keys.map \ + { |k| [k, conf['keystone_authtoken'][k].strip] } ] + else + raise(Puppet::Error, "File: #{conf_filename} does not contain all \ +required sections. Gnocchi types will not work if gnocchi is not \ +correctly configured.") + end + end + + def gnocchi_credentials + self.class.gnocchi_credentials + end + + def self.auth_endpoint + @auth_endpoint ||= get_auth_endpoint + end + + def self.get_auth_endpoint + q = gnocchi_credentials + "#{q['auth_protocol']}://#{q['auth_host']}:#{q['auth_port']}/v2.0/" + end + + def self.gnocchi_conf + return @gnocchi_conf if @gnocchi_conf + @gnocchi_conf = Puppet::Util::IniConfig::File.new + @gnocchi_conf.read(conf_filename) + @gnocchi_conf + end + + def self.auth_gnocchi(*args) + q = gnocchi_credentials + authenv = { + :OS_AUTH_URL => self.auth_endpoint, + :OS_USERNAME => q['admin_user'], + :OS_TENANT_NAME => q['admin_tenant_name'], + :OS_PASSWORD => q['admin_password'] + } + begin + withenv authenv do + gnocchi(args) + end + rescue Exception => e + if (e.message =~ /\[Errno 111\] Connection refused/) or + (e.message =~ /\(HTTP 400\)/) + sleep 10 + withenv authenv do + gnocchi(args) + end + else + raise(e) + end + end + end + + def auth_gnocchi(*args) + self.class.auth_gnocchi(args) + end + + def gnocchi_manage(*args) + cmd = args.join(" ") + output = `#{cmd}` + $?.exitstatus + end + + def self.reset + @gnocchi_conf = nil + @gnocchi_credentials = nil + end + + def self.list_gnocchi_resources(type, *args) + json = auth_gnocchi("--json", "#{type}-list", *args) + return JSON.parse(json) + end + + def self.get_gnocchi_resource_attrs(type, id) + json = auth_gnocchi("--json", "#{type}-show", id) + return JSON.parse(json) + end + +end diff --git a/lib/puppet/provider/gnocchi_config/ini_setting.rb b/lib/puppet/provider/gnocchi_config/ini_setting.rb new file mode 100644 index 00000000..0fee4b12 --- /dev/null +++ b/lib/puppet/provider/gnocchi_config/ini_setting.rb @@ -0,0 +1,27 @@ +Puppet::Type.type(:gnocchi_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) do + + def section + resource[:name].split('/', 2).first + end + + def setting + resource[:name].split('/', 2).last + end + + def separator + '=' + end + + def self.file_path + '/etc/gnocchi/gnocchi.conf' + end + + # added for backwards compatibility with older versions of inifile + def file_path + self.class.file_path + end + +end diff --git a/lib/puppet/type/gnocchi_config.rb b/lib/puppet/type/gnocchi_config.rb new file mode 100644 index 00000000..9fbf10c0 --- /dev/null +++ b/lib/puppet/type/gnocchi_config.rb @@ -0,0 +1,42 @@ +Puppet::Type.newtype(:gnocchi_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from /etc/gnocchi/gnocchi.conf' + newvalues(/\S+\/\S+/) + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + + def is_to_s( currentvalue ) + if resource.secret? + return '[old secret redacted]' + else + return currentvalue + end + end + + def should_to_s( newvalue ) + if resource.secret? + return '[new secret redacted]' + else + return newvalue + end + end + end + + newparam(:secret, :boolean => true) do + desc 'Whether to hide the value from Puppet logs. Defaults to `false`.' + + newvalues(:true, :false) + + defaultto false + end +end