Class: Cookbooks

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

Instance Attribute Summary collapse

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 Attribute Details

#cookbook_idObject (readonly)

Returns the value of attribute cookbook_id.



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

def cookbook_id
  @cookbook_id
end

Instance Method Details



124
125
126
127
128
129
130
# File 'app/controllers/cookbooks.rb', line 124

def all_versions_link(cookbook)
  link_to("show all versions...", "JavaScript:void(0);",
          :class => "show_all",
          :id => "#{cookbook}_show_all",
          :data => cookbook,
          :title => "show all versions of #{cookbook}")
end

#attribute_filesObject



99
100
101
102
103
# File 'app/controllers/cookbooks.rb', line 99

def attribute_files
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  @recipe_files = r.get_rest("cookbooks/#{params[:id]}/attributes")
  display @attribute_files
end

#cb_versionsObject

GET /cookbooks/cookbook_id provides :json, for the javascript on the environments web form.



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

def cb_versions
  provides :json
  use_envs = session[:environment] && !params[:ignore_environments]
  num_versions = params[:num_versions] || "all"
  all_books = fetch_cookbook_versions(num_versions, :cookbook => cookbook_id,
                                      :use_envs => use_envs)
  display({ cookbook_id => all_books[cookbook_id] })
end


132
133
134
135
# File 'app/controllers/cookbooks.rb', line 132

def cookbook_link(version)
  url(:show_specific_version_cookbook,
      :cookbook_id => @cookbook_id, :cb_version => version)
end

#cookbook_partsObject



137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/cookbooks.rb', line 137

def cookbook_parts
  Chef::CookbookVersion::COOKBOOK_SEGMENTS.map do |p|
    part = p.to_s
    case part
    when "files"
      [part, "plain"]
    else
      [part, "ruby"]
    end
  end.sort { |a, b| a[0] <=> b[0] }
end

#definition_filesObject



105
106
107
108
109
# File 'app/controllers/cookbooks.rb', line 105

def definition_files
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  @recipe_files = r.get_rest("cookbooks/#{params[:id]}/definitions")
  display @definition_files
end

#highlight_content(url, type) ⇒ Object



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

def highlight_content(url, type)
  case type
  when "plain"
    show_plain_file(url)
  else
    begin
      syntax_highlight(url)
    rescue
      Chef::Log.error("Error while parsing file #{url}")
      show_plain_file(url)
    end
  end
end

#indexObject



36
37
38
39
# File 'app/controllers/cookbooks.rb', line 36

def index
  @cl = fetch_cookbook_versions(6)
  display @cl
end

#library_filesObject



111
112
113
114
115
# File 'app/controllers/cookbooks.rb', line 111

def library_files
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  @recipe_files = r.get_rest("cookbooks/#{params[:id]}/libraries")
  display @lib_files
end


117
118
119
120
121
122
# File 'app/controllers/cookbooks.rb', line 117

def more_versions_link(cookbook)
  link_to("+", "JavaScript:void(0);",
          :title => "show other versions of #{cookbook}",
          :data => cookbook,
          :class => "cookbook_version_toggle")
end

#params_helperObject



32
33
34
# File 'app/controllers/cookbooks.rb', line 32

def params_helper
  @cookbook_id = params[:id] || params[:cookbook_id]
end

#recipe_filesObject


Helpers

TODO: move these to a cookbooks helper module




91
92
93
94
95
96
97
# File 'app/controllers/cookbooks.rb', line 91

def recipe_files
  # node = params.has_key?('node') ? params[:node] : nil
  # @recipe_files = load_all_files(:recipes, node)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  @recipe_files = r.get_rest("cookbooks/#{params[:id]}/recipes")
  display @recipe_files
end

#showObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/cookbooks.rb', line 41

def show
  begin
    all_books = fetch_cookbook_versions("all", :cookbook => cookbook_id)
    @versions = all_books[cookbook_id].map { |v| v["version"] }
    if params[:cb_version] == "_latest"
      redirect(url(:show_specific_version_cookbook,
                   :cookbook_id => cookbook_id,
                   :cb_version => @versions.first))
      return
    end
    @version = params[:cb_version]
    if !@versions.include?(@version)
      msg = { :warning => ["Cookbook #{cookbook_id} (#{params[:cb_version]})",
                           "is not available in the #{session[:environment]}",
                           "environment."
                          ].join(" ") }
      redirect(url(:cookbooks), :message => msg)
      return
    end
    cookbook_url = "cookbooks/#{cookbook_id}/#{@version}"
    rest = Chef::REST.new(Chef::Config[:chef_server_url])
    @cookbook = rest.get_rest(cookbook_url)
    raise NotFound unless @cookbook
    @manifest = @cookbook.manifest
    display @cookbook
  rescue => e
    Chef::Log.error("#{e}\n#{e.backtrace.join("\n")}")
    @_message = {:error => $!}
    @cl = {}
    render :index
  end
end