Module: KnifeTopo::Loader

Included in:
TopoBootstrap, TopoCookbookCreate, TopoCookbookUpload, TopoCreate, TopoDelete, TopoExport, TopoImport, TopoList
Defined in:
lib/chef/knife/topo/loader.rb

Overview

Topology loaders

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loaderObject (readonly)

Loader to get data bag items from file



31
32
33
# File 'lib/chef/knife/topo/loader.rb', line 31

def loader
  @loader
end

#uiObject (readonly)

Returns the value of attribute ui.



28
29
30
# File 'lib/chef/knife/topo/loader.rb', line 28

def ui
  @ui
end

Instance Method Details

#auto_detect_format(data) ⇒ Object



59
60
61
62
# 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) ⇒ Object



52
53
54
55
56
57
# 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_bagObject



101
102
103
104
105
106
107
# 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) ⇒ Object



64
65
66
67
68
69
70
71
# 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_bagObject



109
110
111
112
113
114
# 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) ⇒ Object



35
36
37
38
39
40
41
# 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') ⇒ Object



116
117
118
119
120
121
122
123
# 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) ⇒ Object



43
44
45
46
47
48
49
50
# 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) ⇒ Object



73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
# 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') ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# 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_nameObject

Name of the topology bag



90
91
92
93
# 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_pathObject

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



97
98
99
# File 'lib/chef/knife/topo/loader.rb', line 97

def topologies_path
  @topologies_path ||= 'data_bags'
end