Skip to content

Download packed and standalone modules #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/puppet_library/app/views/pack.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-# -*- encoding: utf-8 -*-
-# Puppet Library
-# Copyright (C) 2014 Javier Palacios
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.

%h1= "Module #{this["full_name"]}"
%div= "Description: #{this["desc"]}"
%div
Version: #{this["version"]}
%a(href="download")
[Download]
%div= "Dependencies:"
%ul
- metadata.each do |name,mod|
%li= " #{name} - #{mod["version"]}"
57 changes: 57 additions & 0 deletions lib/puppet_library/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,63 @@ def initialize(forge)
end
end

get "/:author/:module/pack" do
author = params[:author]
module_name = params[:module]

begin
this = @forge.get_module_metadata(author, module_name)
this.update( this["releases"].last )
all_results = @forge.get_module_metadata_with_dependencies(author, module_name, this["version"])
deplist = all_results[this["full_name"]].first["dependencies"].inject({}){ |h,i| h.update( i.first => i.last ) }
deplist[this["full_name"]] = this["version"]
all_results.reject!{ |k| k == this["full_name"] }
metadata = all_results.inject({}) do |h,(k,v)|
s = v.sort{ |x,y| x["version"] <=> y["version"] }
item = s.find{ |i| i["version"] == deplist[k] }
h.update( k => item || s.last )
end
haml :pack, { :locals => { "this" => this, "metadata" => metadata } }
rescue Forge::ModuleNotFound
halt 404, haml(:module_not_found, { :locals => { "author" => author, "name" => module_name } })
end
end

get "/:author/:module/download" do
author = params[:author]
module_name = params[:module]

tempdir = Dir.mktmpdir
begin
this = @forge.get_module_metadata(author, module_name)
this.update( this["releases"].last )
all_results = @forge.get_module_metadata_with_dependencies(author, module_name, this["version"])
deplist = all_results[this["full_name"]].first["dependencies"].inject({}){ |h,i| h.update( i.first => i.last ) }

all_results.each do |k,v|
s = v.sort{ |x,y| x["version"] <=> y["version"] }
item = s.find{ |i| i["version"] == deplist[k] } || s.last
parts = item["file"].split('-',3)
parts[0].slice! "/modules/"
parts[2].slice! ".tar.gz"
FileUtils.cp @forge.get_module_buffer(*parts), tempdir
end

require 'puppet_library/archive/archiver'
buffer = Archive::Archiver.archive_dir(tempdir, ".").tap do
attachment "pack-#{author}-#{module_name}-#{this["version"]}.tar.gz"
end

content_type "application/x-tar"
download buffer

rescue Forge::ModuleNotFound
halt 404, haml(:module_not_found, { :locals => { "author" => author, "name" => module_name } })
ensure
FileUtils.rm_rf tempdir
end
end

post "/api/forge/clear-cache" do
@forge.clear_cache
end
Expand Down