Class: Puppet::Provider::Sensuctl
- Inherits:
-
Puppet::Provider
- Object
- Puppet::Provider
- Puppet::Provider::Sensuctl
- Defined in:
- lib/puppet/provider/sensuctl.rb
Class Attribute Summary collapse
-
.chunk_size ⇒ Object
Returns the value of attribute chunk_size.
-
.path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
- .config ⇒ Object
- .config_path ⇒ Object
- .convert_boolean_property_value(value) ⇒ Object
- .dump(resource_type) ⇒ Object
- .namespaces ⇒ Object
- .resource_file(type, metadata, spec, api_version = 'core/v2') ⇒ Object
- .sensuctl(args) ⇒ Object
- .sensuctl_auth_types ⇒ Object
- .sensuctl_config(path = nil) ⇒ Object
- .sensuctl_create(type, metadata, spec, api_version = 'core/v2') ⇒ Object
- .sensuctl_delete(command, name, namespace = nil, metadata = nil, spec = nil, api_version = 'core/v2') ⇒ Object
- .sensuctl_list(command, namespaces = true) ⇒ Object
- .type_properties ⇒ Object
- .valid_json?(json) ⇒ Boolean
Instance Method Summary collapse
- #config_path ⇒ Object
- #convert_boolean_property_value(value) ⇒ Object
- #dump(*args) ⇒ Object
- #namespaces ⇒ Object
- #sensuctl(*args) ⇒ Object
- #sensuctl_config(*args) ⇒ Object
- #sensuctl_create(*args) ⇒ Object
- #sensuctl_delete(*args) ⇒ Object
- #type_properties ⇒ Object
- #valid_json?(json) ⇒ Boolean
Class Attribute Details
.chunk_size ⇒ Object
Returns the value of attribute chunk_size.
11 12 13 |
# File 'lib/puppet/provider/sensuctl.rb', line 11 def chunk_size @chunk_size end |
.path ⇒ Object
Returns the value of attribute path.
12 13 14 |
# File 'lib/puppet/provider/sensuctl.rb', line 12 def path @path end |
Class Method Details
.config ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/puppet/provider/sensuctl.rb', line 188 def self.config output = sensuctl(['config','view']) data = JSON.parse(output) return data rescue Exception => e Puppet.info("Error executing 'sensuctl config view': #{e}") return {} end |
.config_path ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/puppet/provider/sensuctl.rb', line 15 def self.config_path begin home = Dir.home rescue ArgumentError, NoMethodError # https://github.com/sensu/sensu-puppet/issues/1072 # since $HOME is not set in systemd service File.expand_path('~') won't work home = Etc.getpwuid(Process.uid).dir end File.join(home, '.config/sensu/sensuctl/cluster') end |
.convert_boolean_property_value(value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet/provider/sensuctl.rb', line 48 def self.convert_boolean_property_value(value) case value when :true true when :false false else value end end |
.dump(resource_type) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/puppet/provider/sensuctl.rb', line 156 def self.dump(resource_type) # Dump YAML because 'sensuctl dump' does not yet support '--format json' # https://github.com/sensu/sensu-go/issues/3424 output = sensuctl(['dump',resource_type,'--format','yaml','--all-namespaces']) Puppet.debug("YAML dump of #{resource_type}:\n#{output}") resources = [] dumps = output.split('---') dumps.each do |d| resources << YAML.load(d) end resources end |
.namespaces ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/puppet/provider/sensuctl.rb', line 172 def self.namespaces() begin data = self.sensuctl_list('namespace', false) namespaces = [] data.each do |d| namespaces << d['name'] end rescue Exception return [] end namespaces end |
.resource_file(type, metadata, spec, api_version = 'core/v2') ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puppet/provider/sensuctl.rb', line 100 def self.resource_file(type, , spec, api_version = 'core/v2') data = {} data['type'] = type data['api_version'] = api_version data['metadata'] = data['spec'] = spec f = Tempfile.new('sensuctl') f.write(JSON.pretty_generate(data)) f.close Puppet.debug(IO.read(f.path)) f end |
.sensuctl(args) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/puppet/provider/sensuctl.rb', line 62 def self.sensuctl(args) sensuctl_cmd = which('sensuctl') if ! path.nil? cmd = [path] + args else cmd = [sensuctl_cmd] + args end execute(cmd) end |
.sensuctl_auth_types ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/puppet/provider/sensuctl.rb', line 142 def self.sensuctl_auth_types() output = sensuctl(['auth','list','--format','yaml']) Puppet.debug("YAML auth list: #{output}") auth_types = {} auths = output.split('---') Puppet.debug("auths: #{auths}") auths.each do |auth| a = YAML.load(auth) auth_types[a['metadata']['name']] = a['type'] end Puppet.debug("auth_types: #{auth_types}") auth_types end |
.sensuctl_config(path = nil) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/puppet/provider/sensuctl.rb', line 29 def self.sensuctl_config(path = nil) path ||= config_path return {} unless File.file?(path) file = File.read(path) config = JSON.parse(file) Puppet.debug("CONFIG: #{config}") config end |
.sensuctl_create(type, metadata, spec, api_version = 'core/v2') ⇒ Object
113 114 115 116 |
# File 'lib/puppet/provider/sensuctl.rb', line 113 def self.sensuctl_create(type, , spec, api_version = 'core/v2') f = resource_file(type, , spec, api_version) sensuctl(['create', '--file', f.path]) end |
.sensuctl_delete(command, name, namespace = nil, metadata = nil, spec = nil, api_version = 'core/v2') ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/puppet/provider/sensuctl.rb', line 121 def self.sensuctl_delete(command, name, namespace = nil, = nil, spec = nil, api_version = 'core/v2') f = nil if spec && f = resource_file(command, , spec, api_version) args = ['delete','--file',f.path] else args = [command] args << 'delete' args << name args << '--skip-confirm' if namespace args << '--namespace' args << namespace end end sensuctl(args) end |
.sensuctl_list(command, namespaces = true) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/puppet/provider/sensuctl.rb', line 75 def self.sensuctl_list(command, namespaces = true) args = [command] args << 'list' if namespaces args << '--all-namespaces' end args << '--format' args << 'json' if ! chunk_size.nil? args << '--chunk-size' args << chunk_size.to_s end data = [] output = sensuctl(args) Puppet.debug("sensuctl #{args.join(' ')}: #{output}") begin data = JSON.parse(output) rescue JSON::ParserError => e Puppet.debug("Unable to parse output from sensuctl #{args.join(' ')}") return [] end return [] if data.nil? data end |
.type_properties ⇒ Object
41 42 43 |
# File 'lib/puppet/provider/sensuctl.rb', line 41 def self.type_properties resource_type.validproperties.reject { |p| p.to_sym == :ensure } end |
.valid_json?(json) ⇒ Boolean
197 198 199 200 201 202 203 |
# File 'lib/puppet/provider/sensuctl.rb', line 197 def self.valid_json?(json) return false if json.nil? JSON.parse(json) return true rescue JSON::ParserError => e return false end |
Instance Method Details
#config_path ⇒ Object
25 26 27 |
# File 'lib/puppet/provider/sensuctl.rb', line 25 def config_path self.class.config_path end |
#convert_boolean_property_value(value) ⇒ Object
58 59 60 |
# File 'lib/puppet/provider/sensuctl.rb', line 58 def convert_boolean_property_value(value) self.class.convert_boolean_property_value(value) end |
#dump(*args) ⇒ Object
168 169 170 |
# File 'lib/puppet/provider/sensuctl.rb', line 168 def dump(*args) self.class.dump(*args) end |
#namespaces ⇒ Object
184 185 186 |
# File 'lib/puppet/provider/sensuctl.rb', line 184 def namespaces() self.class.namespaces() end |
#sensuctl(*args) ⇒ Object
71 72 73 |
# File 'lib/puppet/provider/sensuctl.rb', line 71 def sensuctl(*args) self.class.sensuctl(*args) end |
#sensuctl_config(*args) ⇒ Object
37 38 39 |
# File 'lib/puppet/provider/sensuctl.rb', line 37 def sensuctl_config(*args) self.class.sensuctl_config(*args) end |
#sensuctl_create(*args) ⇒ Object
117 118 119 |
# File 'lib/puppet/provider/sensuctl.rb', line 117 def sensuctl_create(*args) self.class.sensuctl_create(*args) end |
#sensuctl_delete(*args) ⇒ Object
138 139 140 |
# File 'lib/puppet/provider/sensuctl.rb', line 138 def sensuctl_delete(*args) self.class.sensuctl_delete(*args) end |
#type_properties ⇒ Object
44 45 46 |
# File 'lib/puppet/provider/sensuctl.rb', line 44 def type_properties self.class.type_properties end |
#valid_json?(json) ⇒ Boolean
204 205 206 |
# File 'lib/puppet/provider/sensuctl.rb', line 204 def valid_json?(json) self.class.valid_json?(json) end |