Class: ChefServerSlice::Nodes

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

Instance Method Summary collapse

Methods inherited from Application

#absolute_slice_url, #access_denied, #authorized_node, #escape_node_id, #expand_cookbook_deps, #fix_up_node_id, #get_available_recipes, #load_all_files, #load_cookbook_segment, #login_required, #redirect_back_or_default, #segment_files, #specific_cookbooks, #store_location

Instance Method Details

#createObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/nodes.rb', line 68

def create
  if params.has_key?("inflated_object")
    @node = params["inflated_object"]
    exists = true
    begin
      Chef::Node.load(@node.name)
    rescue Net::HTTPServerException
      exists = false
    end
    raise Forbidden, "Node already exists" if exists
    self.status = 201
    @node.save
    display({ :uri => absolute_slice_url(:node, escape_node_id(@node.name)) })
  else
    begin
      @node = Chef::Node.new
      @node.name params[:name]
      @node.attribute = JSON.parse(params[:attributes])
      @node.run_list params[:for_node]
      @node.save
      redirect(slice_url(:nodes), :message => { :notice => "Created Node #{@node.name}" })
    rescue
      @node.attribute = JSON.parse(params[:attributes])
      @available_recipes = get_available_recipes 
      @available_roles = Chef::Role.list.sort
      @run_list = params[:for_node] 
      @_message = { :error => $! }
      render :new
    end
  end
end

#destroyObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/nodes.rb', line 130

def destroy
  begin
    @node = Chef::Node.load(params[:id])
  rescue Net::HTTPServerException => e 
    raise NotFound, "Cannot load node #{params[:id]}"
  end
  @node.destroy
  if request.accept == 'application/json'
    display @node
  else
    redirect(absolute_slice_url(:nodes), {:message => { :notice => "Node #{params[:id]} deleted successfully" }, :permanent => true})
  end
end

#editObject



56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/nodes.rb', line 56

def edit
  begin
    @node = Chef::Node.load(params[:id])
  rescue Net::HTTPServerException => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end
  @available_recipes = get_available_recipes 
  @available_roles = Chef::Role.list.sort
  @run_list = @node.run_list
  render
end

#indexObject



30
31
32
33
# File 'app/controllers/nodes.rb', line 30

def index
  @node_list = Chef::Node.list 
  display(@node_list.collect { |n| absolute_slice_url(:node, escape_node_id(n)) })
end

#newObject



48
49
50
51
52
53
54
# File 'app/controllers/nodes.rb', line 48

def new
  @node = Chef::Node.new
  @available_recipes = get_available_recipes 
  @available_roles = Chef::Role.list.sort
  @run_list = @node.run_list
  render
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/nodes.rb', line 35

def show
  begin
    @node = Chef::Node.load(params[:id])
  rescue Net::HTTPServerException => e
    raise NotFound, "Cannot load node #{params[:id]}"
  end
  # TODO - might as well expand the run list here, too, rather than take multiple round trips.
  recipes, defaults, overrides = @node.run_list.expand("couchdb")
  @node.default = defaults
  @node.override = overrides
  display @node
end

#updateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/nodes.rb', line 100

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

  if params.has_key?("inflated_object")
    updated = params['inflated_object']
    @node.run_list.reset(updated.run_list)
    @node.attribute = updated.attribute
    @node.save
    display(@node)
  else
    begin
      @node.run_list.reset(params[:for_node] ? params[:for_node] : [])
      @node.attribute = JSON.parse(params[:attributes])
      @node.save
      @_message = { :notice => "Updated Node" }
      render :show
    rescue
      @available_recipes = get_available_recipes 
      @available_roles = Chef::Role.list.sort
      @run_list = Chef::RunList.new
      @run_list.reset(params[:for_node])
      render :edit
    end
  end
end