Class: Roles

Inherits:
Application show all
Defined in:
app/controllers/roles.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 /roles



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/roles.rb', line 97

def create
  begin
    @role = Chef::Role.new
    @role.name(params[:name])
    @role.env_run_lists(params[:env_run_lists])
    @role.description(params[:description]) if params[:description] != ''
    @role.default_attributes(Chef::JSONCompat.from_json(params[:default_attributes])) if params[:default_attributes] != ''
    @role.override_attributes(Chef::JSONCompat.from_json(params[:override_attributes])) if params[:override_attributes] != ''
    @role.create
    redirect(url(:roles), :message => { :notice => "Created Role #{@role.name}" })
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    redirect(url(:new_role), :message => { :error => "Could not create role. #{e.message}" })
  end
end

#destroyObject

DELETE /roles/:id



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

def destroy
  begin
    @role = Chef::Role.load(params[:id])
    @role.destroy
    redirect(absolute_url(:roles), :message => { :notice => "Role #{@role.name} deleted successfully." }, :permanent => true)
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    redirect url(:roles), :message => {:error => "Could not delete role #{params[:id]}"}
  end
end

#editObject

GET /roles/:id/edit



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/roles.rb', line 77

def edit
  begin
    @role = Chef::Role.load(params[:id])
    @available_roles = Chef::Role.list.keys.sort
    @environments = Chef::Environment.list.keys.sort
    @current_env = session[:environment] || "_default"
    @run_list = @role.run_list
    @run_lists = @environments.inject({}) { |run_lists, env| run_lists[env] = @role.env_run_lists[env]; run_lists}
    @existing_run_list_environments = @role.env_run_lists.keys
    # merb select helper has no :include_blank => true, so fix the view in the controller.
    @existing_run_list_environments.unshift('')
    @available_recipes = list_available_recipes_for(@current_env)
    render
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    redirect url(:roles), :message => {:error => "Could not load role #{params[:id]}. #{e.message}"}
  end
end

#indexObject

GET /roles



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

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

#newObject

GET /roles/new



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/roles.rb', line 58

def new
  begin
    @role = Chef::Role.new
    @available_roles = Chef::Role.list.keys.sort
    @environments = Chef::Environment.list.keys.sort
    @run_lists = @environments.inject({}) { |run_lists, env| run_lists[env] = @role.env_run_lists[env]; run_lists}
    @current_env = "_default"
    @available_recipes = list_available_recipes_for(@current_env)
    @existing_run_list_environments = @role.env_run_lists.keys
    # merb select helper has no :include_blank => true, so fix the view in the controller.
    @existing_run_list_environments.unshift('')
    render
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    redirect url(:roles), :message => {:error => "Could not load available recipes, roles, or the run list."}
  end
end

#showObject

GET /roles/:id



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/roles.rb', line 41

def show
  @role = begin
            Chef::Role.load(params[:id])
          rescue => e
            Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
            @_message = {:error => "Could not load role #{params[:id]}."}
            Chef::Role.new
          end

  @current_env = session[:environment] || "_default"
  @env_run_list_exists = @role.env_run_lists.has_key?(@current_env)
  @run_list = @role.run_list_for(@current_env)
  @recipes = @run_list.expand(@current_env, 'server').recipes
  render
end

#updateObject

PUT /roles/:id



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

def update
  begin
    @role = Chef::Role.load(params[:id])
    @role.env_run_lists(params[:env_run_lists])
    @role.description(params[:description]) if params[:description] != ''
    @role.default_attributes(Chef::JSONCompat.from_json(params[:default_attributes])) if params[:default_attributes] != ''
    @role.override_attributes(Chef::JSONCompat.from_json(params[:override_attributes])) if params[:override_attributes] != ''
    @role.save
    redirect(url(:role, params[:id]), :message => { :notice => "Updated Role" })
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    redirect url(:edit_role, params[:id]), :message => {:error => "Could not update role #{params[:id]}. #{e.message}"}
  end
end