Class: Environments

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

Instance Method Summary collapse

Methods inherited from Application

#access_denied, #append_tree, #bad_request?, #binary?, #build_tree, #can_edit_admin?, #cleanup_session, #conflict?, #convert_newline_to_br, #determine_name, #forbidden?, #format_exception, #is_admin?, #is_last_admin?, #list_available_recipes_for, #load_cookbook_segment, #load_environments, #load_session_user, #login_required, #logout_and_redirect_to_login, #not_found?, #redirect_back_or_default, #require_admin, #segment_files, #show_plain_file, #store_location, #str_to_bool, #syntax_highlight

Instance Method Details

#createObject

POST /environments



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/environments.rb', line 53

def create
  @environment = Chef::Environment.new
  if @environment.update_from_params(processed_params=process_params)
    begin
      @environment.create
      redirect(url(:environments), :message => { :notice => "Created Environment #{@environment.name}" })
    rescue Net::HTTPServerException => e
      if conflict?(e)
        Chef::Log.debug("Got 409 conflict creating environment #{params[:name]}\n#{format_exception(e)}")
        redirect(url(:new_environment), :message => { :error => "An environment with that name already exists"})
      elsif forbidden?(e)
        # Currently it's not possible to get 403 here. I leave the code here for completeness and may be useful in the future.[nuo]
        Chef::Log.debug("Got 403 forbidden creating environment #{params[:name]}\n#{format_exception(e)}")
        redirect(url(:new_environment), :message => { :error => "Permission Denied. You do not have permission to create an environment."})
      else
        Chef::Log.error("Error communicating with the API server\n#{format_exception(e)}")
        raise
      end
    end
  else
    load_cookbooks
    # By rendering :new, the view shows errors from @environment.invalid_fields
    render :new
  end
end

#destroyObject

DELETE /environments/:id



116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/environments.rb', line 116

def destroy
  begin
    @environment = Chef::Environment.load(params[:id])
    @environment.destroy
    redirect(absolute_url(:environments), :message => { :notice => "Environment #{@environment.name} deleted successfully." }, :permanent => true)
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    @environment_list = Chef::Environment.list()
    @_message = {:error => "Could not delete environment #{params[:id]}: #{e.message}"}
    render :index
  end
end

#editObject

GET /environments/:id/edit



80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/environments.rb', line 80

def edit
  load_environment
  if @environment.name == "_default"
    msg = { :warning => "The '_default' environment cannot be edited." }
    redirect(url(:environments), :message => msg)
    return
  end
  load_cookbooks
  render
end

#indexObject

GET /environments



28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/environments.rb', line 28

def index
  @environment_list = begin
                        Chef::Environment.list
                      rescue => e
                        Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
                        @_message = "Could not list environments"
                        {}
                      end
  render
end

#list_cookbooksObject

GET /environments/:environment_id/cookbooks



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/environments.rb', line 130

def list_cookbooks
  # TODO: rescue loading the environment
  @environment = Chef::Environment.load(params[:environment_id])
  @cookbooks = begin
                 r = Chef::REST.new(Chef::Config[:chef_server_url])
                 r.get_rest("/environments/#{params[:environment_id]}/cookbooks").inject({}) do |res, (cookbook, url)|
                   # we just want the cookbook name and the version
                   res[cookbook] = url.split('/').last
                   res
                 end
               rescue => e
                 Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
                 @_message = "Could not load cookbooks for environment #{params[:environment_id]}"
                 {}
               end
  render
end

#list_nodesObject

GET /environments/:environment_id/nodes



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/environments.rb', line 149

def list_nodes
  # TODO: rescue loading the environment
  @environment = Chef::Environment.load(params[:environment_id])
  @nodes = begin
             r = Chef::REST.new(Chef::Config[:chef_server_url])
             r.get_rest("/environments/#{params[:environment_id]}/nodes").keys.sort
           rescue => e
             Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
             @_message = "Could not load nodes for environment #{params[:environment_id]}"
             []
           end
  render
end

#list_recipesObject

GET /environments/:environment/recipes



164
165
166
167
# File 'app/controllers/environments.rb', line 164

def list_recipes
  provides :json
  display(:recipes => list_available_recipes_for(params[:environment_id]))
end

#newObject

GET /environemnts/new



46
47
48
49
50
# File 'app/controllers/environments.rb', line 46

def new
  @environment = Chef::Environment.new
  load_cookbooks
  render :new
end

#select_environmentObject

GET /environments/:environment_id/set



170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/environments.rb', line 170

def select_environment
  name = params[:environment_id]
  referer = request.referer || "/nodes"
  if name == '_none'
    session[:environment] = nil
  else
    # TODO: check if environment exists
    session[:environment] = name
  end
  redirect referer
end

#showObject

GET /environments/:id



40
41
42
43
# File 'app/controllers/environments.rb', line 40

def show
  load_environment
  render
end

#updateObject

PUT /environments/:id



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/environments.rb', line 92

def update
  load_environment
  if @environment.update_from_params(process_params(params[:id]))
    begin
      @environment.save
      redirect(url(:environment, @environment.name), :message => { :notice => "Updated Environment #{@environment.name}" })
    rescue Net::HTTPServerException => e
      if forbidden?(e)
        # Currently it's not possible to get 403 here. I leave the code here for completeness and may be useful in the future.[nuo]
        Chef::Log.debug("Got 403 forbidden updating environment #{params[:name]}\n#{format_exception(e)}")
        redirect(url(:edit_environment), :message => { :error => "Permission Denied. You do not have permission to update an environment."})
      else
        Chef::Log.error("Error communicating with the API server\n#{format_exception(e)}")
        raise
      end
    end
  else
    load_cookbooks
    # By rendering :new, the view shows errors from @environment.invalid_fields
    render :edit
  end
end