Class: Nodes

Inherits:
Application show all
Defined in:
app/controllers/nodes.rb

Instance Method Summary collapse

Methods inherited from Application

#access_denied, #admin_or_requesting_node, #authenticate_every, #display, #get_available_recipes, #is_admin, #is_admin_or_validator, #redirect_back_or_default, #store_location

Instance Method Details

#cookbooksObject

Return a hash, cookbook_name => cookbook manifest, of the cookbooks appropriate for this node, using its run_list and environment.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/nodes.rb', line 87

def cookbooks
  begin
    @node = Chef::Node.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end

  # Get the mapping of cookbook_name => CookbookVersion applicable to
  # this node's run_list and its environment.
  begin
    included_cookbooks = Chef::CookbookVersionSelector.expand_to_cookbook_versions(@node.run_list, @node.chef_environment)
  rescue Chef::Exceptions::CookbookVersionSelection::InvalidRunListItems => e
    raise PreconditionFailed, e.to_json
  rescue Chef::Exceptions::CookbookVersionSelection::UnsatisfiableRunListItem => e
    raise PreconditionFailed, e.to_json
  end

  # Convert from
  #  name => CookbookVersion
  # to
  #  name => cookbook manifest
  # and display.
  display(included_cookbooks.inject({}) do |acc, (cookbook_name, cookbook_version)|
            acc[cookbook_name.to_s] = cookbook_version.generate_manifest_with_urls{|opts| absolute_url(:cookbook_file, opts) }
            acc
          end)
end

#createObject



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/nodes.rb', line 49

def create
  @node = params["inflated_object"]
  begin
    Chef::Node.cdb_load(@node.name)
    raise Conflict, "Node already exists"
  rescue Chef::Exceptions::CouchDBNotFound
  end
  self.status = 201
  @node.cdb_save
  display({ :uri => absolute_url(:node, @node.name) })
end

#destroyObject



74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/nodes.rb', line 74

def destroy
  begin
    @node = Chef::Node.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end
  @node.cdb_destroy
  @node.couchdb_rev = nil
  display @node
end

#indexObject



32
33
34
35
36
37
# File 'app/controllers/nodes.rb', line 32

def index
  @node_list = Chef::Node.cdb_list
  display(@node_list.inject({}) do |r,n|
    r[n] = absolute_url(:node, n); r
  end)
end

#showObject



39
40
41
42
43
44
45
46
47
# File 'app/controllers/nodes.rb', line 39

def show
  begin
    @node = Chef::Node.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end
  @node.couchdb_rev = nil
  display @node
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/nodes.rb', line 61

def update
  begin
    @node = Chef::Node.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end

  @node.update_from!(params['inflated_object'])
  @node.cdb_save
  @node.couchdb_rev = nil
  display(@node)
end