Class: Vagabond::Vagabondfile

Inherits:
Object
  • Object
show all
Defined in:
lib/vagabond/vagabondfile.rb

Constant Summary collapse

DEFAULT_KEYS =
%w(defaults definitions nodes clusters specs server callbacks)
ALIASES =
Mash.new(
  :boxes => :nodes,
  :nodes => :boxes,
  :local_chef_server => :server,
  :server => :local_chef_server
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, *args) ⇒ Vagabondfile

Returns a new instance of Vagabondfile.



31
32
33
34
35
# File 'lib/vagabond/vagabondfile.rb', line 31

def initialize(path=nil, *args)
  path = discover_path(Dir.pwd) unless path
  @path = path
  load_configuration!(args.include?(:allow_missing))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/vagabond/vagabondfile.rb', line 21

def config
  @config
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/vagabond/vagabondfile.rb', line 20

def path
  @path
end

Class Method Details

.describe(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/vagabond/vagabondfile.rb', line 9

def describe(&block)
  inst = AttributeStruct.new
  if(block.arity != 1)
    inst.instance_exec(&block)
  else
    inst.instance_exec(inst, &block)
  end
  inst
end

Instance Method Details

#[](k) ⇒ Object



66
67
68
69
70
71
# File 'lib/vagabond/vagabondfile.rb', line 66

def [](k)
  if(DEFAULT_KEYS.include?(k.to_s))
    @config[k] ||= Mash.new
  end
  aliased(k) || @config[k]
end

#aliased(k) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vagabond/vagabondfile.rb', line 73

def aliased(k)
  if(ALIASES.has_key?(k))
    v = [@config[k], @config[ALIASES[k]]].compact
    if(v.size > 1)
      case v.first
      when Array
        m = :|
      when Hash, Mash
        m = :merge
      else
        m = :+
      end
      v.inject(&m)
    else
      v.first
    end
  end
end

#build_private_storeObject



113
114
115
116
117
118
119
120
121
# File 'lib/vagabond/vagabondfile.rb', line 113

def build_private_store
  unless(@_private_store_path)
    @_private_store_path = File.join('/tmp/vagabond-solos', directory.gsub(%r{[^0-9a-zA-Z]}, '-'), 'Vagabondfile')
    @_private_store_path = File.expand_path(@_private_store_path.gsub('-', '/'))
    FileUtils.mkdir_p(File.dirname(@_private_store_path))
    File.dirname(@_private_store_path)
  end
  File.dirname(@_private_store_path)
end

#callbacks_for(name) ⇒ Object



37
38
39
40
41
# File 'lib/vagabond/vagabondfile.rb', line 37

def callbacks_for(name)
  callbacks = self[:callbacks] || Mash.new
  callbacks = Chef::Mixin::DeepMerge.merge(callbacks, for_node(name, :allow_missing)[:callbacks])
  callbacks
end

#directoryObject



136
137
138
# File 'lib/vagabond/vagabondfile.rb', line 136

def directory
  File.dirname(@path)
end

#discover_path(path) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/vagabond/vagabondfile.rb', line 144

def discover_path(path)
  d_path = Dir.glob(File.join(path, 'Vagabondfile')).first
  unless(d_path)
    cut_path = path.split(File::SEPARATOR)
    cut_path.pop
    d_path = discover_path(cut_path.join(File::SEPARATOR)) unless cut_path.empty?
  end
  d_path
end

#for_definition(name) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/vagabond/vagabondfile.rb', line 57

def for_definition(name)
  base = self[:defaults]
  unless(self[:definitions][name])
    raise VagabondError::InvalidName.new("Requested name is not a valid definition name: #{name}")
  end
  base = Chef::Mixin::DeepMerge.merge(base, self[:definitions][name])
  base
end

#for_node(name, *args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagabond/vagabondfile.rb', line 43

def for_node(name, *args)
  unless(self[:nodes][name])
    return Mash.new if args.include?(:allow_missing)
    raise VagabondError::InvalidName.new("Requested name is not a valid node name: #{name}")
  end
  if(self[:nodes][name][:definition])
    base = for_definition(self[:nodes][name][:definition])
  else
    base = self[:defaults]
  end
  base = Chef::Mixin::DeepMerge.merge(base, self[:nodes][name])
  base
end

#generate_store_pathObject



123
124
125
126
127
128
129
130
# File 'lib/vagabond/vagabondfile.rb', line 123

def generate_store_path
  @path ||= File.expand_path(File.join(Dir.pwd, 'Vagabondfile'))
  unless(@store_path)
    build_private_store
    @store_path = @_private_store_path
  end
  File.dirname(@store_path)
end

#load_configuration!(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vagabond/vagabondfile.rb', line 92

def load_configuration!(*args)
  unless(args.empty?)
    no_raise = args.first == true
    force_store = args.include?(:force_store_path)
    no_raise ||= force_store
  end
  if(@path && File.exists?(@path))
    thing = self.instance_eval(IO.read(@path), @path, 1)
    if(thing.is_a?(AttributeStruct))
      @config = Mash.new(thing._dump)
    else
      @config = Mash.new(thing)
    end
  end
  if(!@config || force_store)
    raise 'No Vagabondfile file found!' unless no_raise
    generate_store_path
    @config = Mash[*DEFAULT_KEYS.map{|k| [k, Mash.new]}.flatten]
  end
end

#local_chef_server?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/vagabond/vagabondfile.rb', line 154

def local_chef_server?
  self[:local_chef_server] && self[:local_chef_server][:enabled]
end

#store_directoryObject



140
141
142
# File 'lib/vagabond/vagabondfile.rb', line 140

def store_directory
  File.dirname(@store_path || @path)
end

#store_pathObject



132
133
134
# File 'lib/vagabond/vagabondfile.rb', line 132

def store_path
  @store_path || @path
end