Class: Environments

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

Instance Method Summary collapse

Methods included from Merb::CookbookVersionHelper

#expand_cookbook_urls, #num_versions!

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

#cookbookObject

GET /environments/:environment_id/cookbooks/:cookbook_id returns data in the format of: => {

  :url => "http://url",
  :versions => [{:url => "http://url/1.0.0", :version => "1.0.0", => "http://url/0.0.1", :version=>"0.0.1"]
}

}

Raises:

  • (NotFound)


121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/environments.rb', line 121

def cookbook
  cookbook_name = params[:cookbook_id]
  begin
    filtered_cookbooks = Chef::Environment.cdb_load_filtered_cookbook_versions(params[:environment_id])
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load environment #{params[:environment_id]}"
  end
  raise NotFound, "Cannot load cookbook #{cookbook_name}" unless filtered_cookbooks.has_key?(cookbook_name)
  versions = filtered_cookbooks[cookbook_name].map{|v| v.version.to_s}
  num_versions = num_versions!("all")
  display({ cookbook_name => expand_cookbook_urls(cookbook_name, versions, num_versions) })
end

#cookbook_versions_for_run_listObject

POST /environments/:environment_id/cookbook_versions

Take the given run_list and return the versions of cookbooks that would be used after applying the constraints of the given environment.

INPUT:

:run_list = an Array of String's, e.g.,
["recipe[apache2]", "recipe[runit]"]

OUT:

Hash of cookbook names cookbook manifest

NOTE: This method is a POST, not because it’s a mutator (it’s idempotent), but the run_list can likely exceed Merb’s query string limit for GET of 1024 characters.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/environments.rb', line 170

def cookbook_versions_for_run_list
  begin
    # not possible to be nil due to the route to get us to this API
    # endpoint
    environment_input = params[:environment_id]

    run_list_input = params[:run_list]
    raise BadRequest, "Missing param: run_list" unless run_list_input
    raise BadRequest, "Param run_list is not an Array: #{run_list_input.class}" unless run_list_input.is_a?(Array)

    # Convert the input array of strings to a RunList containing
    # RunListItem's.
    run_list = Chef::RunList.new
    run_list_input.each do |run_list_item_string|
      run_list << run_list_item_string
    end

    # Expand the run list in the scope of the specified environment.
    names_to_cookbook_version = Chef::CookbookVersionSelector.expand_to_cookbook_versions(run_list, environment_input)
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load environment #{params[:environment_id]}"
  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(names_to_cookbook_version.inject({}) do |res, (cookbook_name, cookbook_version)|
            res[cookbook_name] = cookbook_version.generate_manifest_with_urls {|opts| absolute_url(:cookbook_file, opts) }
            res
          end)
end

#createObject

POST /environments

Raises:

  • (Conflict)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/environments.rb', line 50

def create
  env = params["inflated_object"]
  exists = true
  begin
    Chef::Environment.cdb_load(env.name)
  rescue Chef::Exceptions::CouchDBNotFound
    exists = false
  end
  raise Conflict, "Environment already exists" if exists

  env.cdb_save
  self.status = 201
  display({:uri => absolute_url(:environment, env.name)})
end

#destroyObject

DELETE /environments/:id

Raises:

  • (MethodNotAllowed)


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

def destroy
  raise MethodNotAllowed if params[:id] == "_default"
  begin
    env = Chef::Environment.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load environment #{params[:id]}"
  end
  env.cdb_destroy
  display(env)
end

#indexObject

GET /environments



33
34
35
36
# File 'app/controllers/environments.rb', line 33

def index
  environment_list = Chef::Environment.cdb_list(true)
  display(environment_list.inject({}) { |res, env| res[env.name] = absolute_url(:environment, env.name); res })
end

#list_cookbooksObject

GET /environments/:environment_id/cookbooks returns data in the format of: => {

  :url => "http://url",
  :versions => [{:url => "http://url/1.0.0", :version => "1.0.0", => "http://url/0.0.1", :version=>"0.0.1"]
}

}



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/environments.rb', line 100

def list_cookbooks
  begin
    filtered_cookbooks = Chef::Environment.cdb_load_filtered_cookbook_versions(params[:environment_id])
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load environment #{params[:environment_id]}"
  end
  num_versions = num_versions!
  display(filtered_cookbooks.inject({}) {|res, (cookbook_name,versions)|
    versions.map!{|v| v.version.to_s}
    res[cookbook_name] = expand_cookbook_urls(cookbook_name, versions, num_versions)
    res
  })
end

#list_nodesObject

GET /environments/:environment_id/nodes



140
141
142
143
# File 'app/controllers/environments.rb', line 140

def list_nodes
  node_list = Chef::Node.cdb_list_by_environment(params[:environment_id])
  display(node_list.inject({}) {|r,n| r[n] = absolute_url(:node, n); r})
end

#list_recipesObject

GET /environments/:environment/recipes



135
136
137
# File 'app/controllers/environments.rb', line 135

def list_recipes
  display(Chef::Environment.cdb_load_filtered_recipe_list(params[:environment_id]))
end

#roleObject

GET /environments/:environment_id/roles/:role_id



146
147
148
149
150
151
152
153
# File 'app/controllers/environments.rb', line 146

def role
  begin
    role = Chef::Role.cdb_load(params[:role_id])
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load role #{params[:role_id]}"
  end
  display("run_list" => role.env_run_lists[params[:environment_id]])
end

#showObject

GET /environments/:id



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

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

#updateObject

PUT /environments/:id

Raises:

  • (MethodNotAllowed)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/environments.rb', line 66

def update
  raise MethodNotAllowed if params[:id] == "_default"
  begin
    env = Chef::Environment.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound
    raise NotFound, "Cannot load environment #{params[:id]}"
  end

  env.update_from!(params["inflated_object"])
  env.cdb_save
  env.couchdb_rev = nil
  self.status = 200
  display(env)
end