Class: Puppet::Provider::Sensuctl

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/sensuctl.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.chunk_sizeObject

Returns the value of attribute chunk_size.



11
12
13
# File 'lib/puppet/provider/sensuctl.rb', line 11

def chunk_size
  @chunk_size
end

.pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/puppet/provider/sensuctl.rb', line 12

def path
  @path
end

.validate_namespacesObject

Returns the value of attribute validate_namespaces.



13
14
15
# File 'lib/puppet/provider/sensuctl.rb', line 13

def validate_namespaces
  @validate_namespaces
end

Class Method Details

.configObject



206
207
208
209
210
211
212
213
# File 'lib/puppet/provider/sensuctl.rb', line 206

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_pathObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/provider/sensuctl.rb', line 20

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/provider/sensuctl.rb', line 53

def self.convert_boolean_property_value(value)
  case value
  when :true
    true
  when :false
    false
  else
    value
  end
end

.dump(resource_type) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/puppet/provider/sensuctl.rb', line 166

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

.namespacesObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet/provider/sensuctl.rb', line 182

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/provider/sensuctl.rb', line 110

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, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/puppet/provider/sensuctl.rb', line 67

def self.sensuctl(args, opts = {})
  sensuctl_cmd = which('sensuctl')
  if ! path.nil?
    cmd = [path] + args
  else
    cmd = [sensuctl_cmd] + args
  end
  execute(cmd, opts)
end

.sensuctl_auth_typesObject



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet/provider/sensuctl.rb', line 152

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



34
35
36
37
38
39
40
41
# File 'lib/puppet/provider/sensuctl.rb', line 34

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



123
124
125
126
# File 'lib/puppet/provider/sensuctl.rb', line 123

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet/provider/sensuctl.rb', line 131

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet/provider/sensuctl.rb', line 80

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 = []
  begin
    output = sensuctl(args, {:failonfail => false})
    Puppet.debug("sensuctl #{args.join(' ')}: #{output}")
  rescue Exception => e
    Puppet.notice("Failed to list resources with sensuctl: #{e}")
    return []
  end
  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_propertiesObject



46
47
48
# File 'lib/puppet/provider/sensuctl.rb', line 46

def self.type_properties
  resource_type.validproperties.reject { |p| p.to_sym == :ensure }
end

.valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
# File 'lib/puppet/provider/sensuctl.rb', line 215

def self.valid_json?(json)
  return false if json.nil?
  JSON.parse(json)
  return true
rescue JSON::ParserError => e
  return false
end

.versionObject



226
227
228
229
230
231
232
233
# File 'lib/puppet/provider/sensuctl.rb', line 226

def self.version
  output = sensuctl(['version'], {:failonfail => false})
  version = output[%r{version ([0-9.]+)}, 1]
  return version
rescue Exception => e
  Puppet.notice "Unable to query Sensu API version: #{e.message}"
  return nil
end

.version_cmp(v) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/puppet/provider/sensuctl.rb', line 238

def self.version_cmp(v)
  if @current_version.nil?
    @current_version = version
  end
  return true if @current_version.nil?
  Gem::Version.new(@current_version) >= Gem::Version.new(v)
rescue ArgumentError => e
  Puppet.debug "Unable to compare version #{@current_version} with needed version #{v}: #{e}"
  return true
end

Instance Method Details

#config_pathObject



30
31
32
# File 'lib/puppet/provider/sensuctl.rb', line 30

def config_path
  self.class.config_path
end

#convert_boolean_property_value(value) ⇒ Object



63
64
65
# File 'lib/puppet/provider/sensuctl.rb', line 63

def convert_boolean_property_value(value)
  self.class.convert_boolean_property_value(value)
end

#dump(*args) ⇒ Object



178
179
180
# File 'lib/puppet/provider/sensuctl.rb', line 178

def dump(*args)
  self.class.dump(*args)
end

#get_entity(entity, namespace) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/puppet/provider/sensuctl.rb', line 198

def get_entity(entity, namespace)
  output = sensuctl(['entity', 'info', entity, '--namespace', namespace, '--format', 'json'])
  data = JSON.parse(output)
  return data
rescue Exception => e
  raise Puppet::Error "Failed to get entity #{entity}: #{e}"
end

#namespacesObject



194
195
196
# File 'lib/puppet/provider/sensuctl.rb', line 194

def namespaces()
  self.class.namespaces()
end

#sensuctl(*args) ⇒ Object



76
77
78
# File 'lib/puppet/provider/sensuctl.rb', line 76

def sensuctl(*args)
  self.class.sensuctl(*args)
end

#sensuctl_config(*args) ⇒ Object



42
43
44
# File 'lib/puppet/provider/sensuctl.rb', line 42

def sensuctl_config(*args)
  self.class.sensuctl_config(*args)
end

#sensuctl_create(*args) ⇒ Object



127
128
129
# File 'lib/puppet/provider/sensuctl.rb', line 127

def sensuctl_create(*args)
  self.class.sensuctl_create(*args)
end

#sensuctl_delete(*args) ⇒ Object



148
149
150
# File 'lib/puppet/provider/sensuctl.rb', line 148

def sensuctl_delete(*args)
  self.class.sensuctl_delete(*args)
end

#type_propertiesObject



49
50
51
# File 'lib/puppet/provider/sensuctl.rb', line 49

def type_properties
  self.class.type_properties
end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/puppet/provider/sensuctl.rb', line 222

def valid_json?(json)
  self.class.valid_json?(json)
end

#validate_namespacesObject



16
17
18
# File 'lib/puppet/provider/sensuctl.rb', line 16

def validate_namespaces
  self.class.validate_namespaces
end

#versionObject



234
235
236
# File 'lib/puppet/provider/sensuctl.rb', line 234

def version
  self.class.version
end

#version_cmp(*args) ⇒ Object



248
249
250
# File 'lib/puppet/provider/sensuctl.rb', line 248

def version_cmp(*args)
  self.class.version_cmp(*args)
end