module KnifeTopo::Loader

Topology loaders

Attributes

loader[R]
ui[R]

Public Instance Methods

auto_detect_format(data) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 59
def auto_detect_format(data)
  return 'topo_v1' if data['cookbook_attributes']
  'default'
end
check_file(filepath, msg = nil) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 52
def check_file(filepath, msg = nil)
  return if loader.file_exists_and_is_readable?(filepath)
  msg ||= "Topology file #{filepath} not found"
  ui.fatal(msg)
  exit(1)
end
create_topo_bag() click to toggle source
# File lib/chef/knife/topo/loader.rb, line 101
def create_topo_bag
  data_bag = Chef::DataBag.new
  data_bag.name(topo_bag_name)
  data_bag.create
rescue Net::HTTPServerException => e
  raise unless e.to_s =~ /^409/
end
get_local_topo_path(topo_name) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 64
def get_local_topo_path(topo_name)
  File.join(
    Dir.pwd,
    topologies_path,
    topo_bag_name,
    topo_name + '.json'
  )
end
list_topo_bag() click to toggle source
# File lib/chef/knife/topo/loader.rb, line 109
def list_topo_bag
  Chef::DataBag.load(topo_bag_name)
rescue Net::HTTPServerException => e
  raise unless e.to_s =~ /^404/
  {}
end
load_local_topo_or_exit(topo_name) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 35
def load_local_topo_or_exit(topo_name)
  filepath = get_local_topo_path(topo_name)
  msg = "Topology file #{filepath} not found - use " \
    "'knife topo import' first"
  check_file(filepath, msg)
  load_topo_from_file_or_exit(filepath)
end
load_node_data(node_name, min_priority = 'default') click to toggle source
# File lib/chef/knife/topo/loader.rb, line 116
def load_node_data(node_name, min_priority = 'default')
  node_data = {}
  node = Chef::Node.load(node_name)
  %w(name tags chef_environment run_list).each do |key|
    node_data[key] = node.send(key)
  end
  node_data = node_data.merge(priority_attrs(node, min_priority))
end
load_topo_from_file_or_exit(filepath, format = nil) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 43
def load_topo_from_file_or_exit(filepath, format = nil)
  check_file(filepath)
  data = loader.object_from_file(filepath)
  format ||= auto_detect_format(data)
  topo = Chef::Topology.convert_from(format, data)
  topo.data_bag(topo_bag_name)
  topo
end
load_topo_from_server(topo_name) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 73
def load_topo_from_server(topo_name)
  Chef::Topology.load(topo_bag_name, topo_name)
rescue Net::HTTPServerException => e
  raise unless e.to_s =~ /^404/
end
load_topo_from_server_or_exit(topo_name) click to toggle source
# File lib/chef/knife/topo/loader.rb, line 79
def load_topo_from_server_or_exit(topo_name)
  topo = load_topo_from_server(topo_name)
  unless topo
    ui.fatal("Topology #{topo_bag_name}/#{@topo_name} does not exist " \
      "on the server - use 'knife topo create' first")
    exit(1)
  end
  topo
end
priority_attrs(node, min_priority = 'default') click to toggle source
# File lib/chef/knife/topo/loader.rb, line 125
def priority_attrs(node, min_priority = 'default')
  attrs = {}
  p = KnifeTopo::PRIORITIES
  min_index = p.index(min_priority)
  p.each_index do |index|
    next if index < min_index
    key = p[index]
    attrs[key] = node.send(key)
    attrs.delete(key) if attrs[key].empty?
  end
  attrs
end
topo_bag_name() click to toggle source

Name of the topology bag

# File lib/chef/knife/topo/loader.rb, line 90
def topo_bag_name
  @topo_bag_name ||= config[:data_bag]
  @topo_bag_name ||= 'topologies'
end
topologies_path() click to toggle source

Path for the topologies data bags. For now, use the standard data_bags path for our topologies bags

# File lib/chef/knife/topo/loader.rb, line 97
def topologies_path
  @topologies_path ||= 'data_bags'
end