Module: KnifeTopo::NodeUpdateHelper

Included in:
TopoCreate
Defined in:
lib/chef/knife/topo/node_update_helper.rb

Overview

Node update helper for knife topo

Instance Method Summary collapse

Instance Method Details

#do_node_updates(node, node_updates, merge = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/knife/topo/node_update_helper.rb', line 47

def do_node_updates(node, node_updates, merge = false)
  updated = update_node_with_values(node, node_updates, merge)
  if updated
    ui.info "Updating #{updated.join(', ')} on node #{node.name}"
    node.save
    ui.output(format_for_display(node)) if config[:print_after]
  else
    ui.info "No updates found for node #{node.name}"
  end
end

#update_attrs(node, attrs, merge = false) ⇒ Object

Update methods all return true if an actual update is made



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/knife/topo/node_update_helper.rb', line 81

def update_attrs(node, attrs, merge = false)
  return false unless attrs
  # keep the current tags
  attrs['tags'] = node.normal.tags
  original = Marshal.load(Marshal.dump(node.normal))
  node.normal = if merge
                  Chef::Mixin::DeepMerge.merge(node.normal, attrs)
                else
                  attrs
                end
  original != node.normal
end

#update_chef_env(node, env) ⇒ Object



103
104
105
106
107
# File 'lib/chef/knife/topo/node_update_helper.rb', line 103

def update_chef_env(node, env)
  return false unless env && env != node.chef_environment
  node.chef_environment(env)
  true
end

#update_node(node_updates, merge = false) ⇒ Object

Update an existing node



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/knife/topo/node_update_helper.rb', line 28

def update_node(node_updates, merge = false)
  config[:disable_editing] = true

  begin
    # load then update and save the node
    node = Chef::Node.load(node_updates['name'])

    env = node_updates['chef_environment']
    check_chef_env(env) unless env == node['chef_environment']
    do_node_updates(node, node_updates, merge)

  rescue Net::HTTPServerException => e
    raise unless e.to_s =~ /^404/
    # Node has not been created
  end

  node
end

#update_node_with_values(node, updates, merge = false) ⇒ Object

Update original node, return list of updated properties.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/topo/node_update_helper.rb', line 59

def update_node_with_values(node, updates, merge = false)
  updated = []

  # merge the normal attributes (but not tags)
  updated << 'normal' if update_attrs(node, updates['normal'], merge)

  # update runlist
  updated << 'run_list' if update_runlist(node, updates['run_list'])

  # update chef env
  if update_chef_env(node, updates['chef_environment'])
    updated << 'chef_environment'
  end

  # merge tags
  updated << 'tags' if update_tags(node, updates['tags'])

  # return false if no updates, else return array of property names
  !updated.empty? && updated
end

#update_runlist(node, runlist) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/chef/knife/topo/node_update_helper.rb', line 94

def update_runlist(node, runlist)
  # node.run_list MUST be lhs of != to use override operator
  return false unless runlist && node.run_list != runlist
  updated_run_list = Chef::RunList.new
  runlist.each { |e| updated_run_list << e }
  node.run_list(*updated_run_list)
  true
end

#update_tags(node, tags) ⇒ Object



109
110
111
112
113
114
# File 'lib/chef/knife/topo/node_update_helper.rb', line 109

def update_tags(node, tags)
  return false unless tags
  orig_num_tags = node.tags.length
  node.tag(*tags)
  node.tags.length > orig_num_tags
end