Class: Oxidized::Output::File

Inherits:
Oxidized::Output show all
Defined in:
lib/oxidized/output/file.rb

Overview

ruby’s File class must be accessed with ::File to avoid name conflicts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFile

Returns a new instance of File.



9
10
11
12
# File 'lib/oxidized/output/file.rb', line 9

def initialize
  super
  @cfg = Oxidized.config.output.file
end

Instance Attribute Details

#commitrefObject (readonly)

Returns the value of attribute commitref.



7
8
9
# File 'lib/oxidized/output/file.rb', line 7

def commitref
  @commitref
end

Class Method Details

.clean_obsolete_nodes(active_nodes) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oxidized/output/file.rb', line 70

def self.clean_obsolete_nodes(active_nodes)
  cfg_dir = ::File.expand_path Oxidized.config.output.file.directory
  dir_base = ::File.dirname(cfg_dir)
  default_dir = ::File.basename(cfg_dir)

  keep_files = active_nodes.map { |n| node_path(n.name, n.group) }
  active_groups = active_nodes.map(&:group).compact.uniq

  [default_dir, *active_groups].each do |group|
    Dir.glob(::File.join(dir_base, group, "*")).each do |file|
      ::File.delete(file) if ::File.file?(file) && !keep_files.include?(file)
    end
  end
end

.node_path(node_name, group_name = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/oxidized/output/file.rb', line 60

def self.node_path(node_name, group_name = nil)
  cfg_dir = ::File.expand_path Oxidized.config.output.file.directory

  if group_name
    ::File.join ::File.dirname(cfg_dir), group_name, node_name
  else
    ::File.join cfg_dir, node_name
  end
end

Instance Method Details

#fetch(node, group) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oxidized/output/file.rb', line 34

def fetch(node, group)
  cfg_dir   = ::File.expand_path @cfg.directory
  node_name = node.name

  if group # group is explicitly defined by user
    cfg_dir = ::File.join ::File.dirname(cfg_dir), group
    ::File.read ::File.join(cfg_dir, node_name)
  elsif ::File.exist? ::File.join(cfg_dir, node_name) # node configuration file is stored on base directory
    ::File.read ::File.join(cfg_dir, node_name)
  else
    path = Dir.glob(::File.join(::File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups
    ::File.read path
  end
rescue Errno::ENOENT
  nil
end

#get_version(_node, _group, _oid) ⇒ Object



56
57
58
# File 'lib/oxidized/output/file.rb', line 56

def get_version(_node, _group, _oid)
  'not supported'
end

#setupObject

Raises:



14
15
16
17
18
19
20
# File 'lib/oxidized/output/file.rb', line 14

def setup
  return unless @cfg.empty?

  Oxidized.asetus.user.output.file.directory = ::File.join(Config::ROOT, 'configs')
  Oxidized.asetus.save :user
  raise NoConfig, "no output file config, edit #{Oxidized::Config.configfile}"
end

#store(node, outputs, opt = {}) ⇒ Object

node: node name (String) outputs: Oxidized::Models::Outputs opts: hash of node vars



25
26
27
28
29
30
31
32
# File 'lib/oxidized/output/file.rb', line 25

def store(node, outputs, opt = {})
  file = ::File.expand_path @cfg.directory
  file = ::File.join ::File.dirname(file), opt[:group] if opt[:group]
  FileUtils.mkdir_p file
  file = ::File.join file, node
  ::File.write(file, outputs.to_cfg)
  @commitref = file
end

#version(_node, _group) ⇒ Object



51
52
53
54
# File 'lib/oxidized/output/file.rb', line 51

def version(_node, _group)
  # not supported
  []
end