Skip to content
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

Feature/download #321

Open
wants to merge 6 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
5 changes: 1 addition & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ CLOBBER.include('Gemfile.lock')

RSpec::Core::RakeTask.new
Cucumber::Rake::Task.new(:features) do |t|
require 'puppet'
puppet_version = Puppet::version.gsub("~>","").split(".").first.to_i
tags = (2..4).select {|i| i != puppet_version}.map{|i| "--tags @puppet#{puppet_version},~@puppet#{i}"}
# don't run githubtarball scenarios in Travis, they easily fail with rate limit exceeded
tags = []
tags << "--tags ~@github" if ENV['TRAVIS']=='true'
t.cucumber_opts = tags.join(" ")
end
Expand Down
6 changes: 3 additions & 3 deletions features/update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Feature: cli/update
And a file named "Puppetfile.lock" with:
"""
FORGE
remote: http://forge.puppetlabs.com
remote: https://forgeapi.puppetlabs.com
specs:
puppetlabs/stdlib (3.1.0)

DEPENDENCIES
puppetlabs/stdlib (~> 3.0)
"""
When I run `librarian-puppet update puppetlabs/stdlib`
When I run `librarian-puppet update puppetlabs-stdlib`
Then the exit status should be 0
And the file "Puppetfile" should not exist
And the file "Puppetfile.lock" should match /puppetlabs.stdlib \(3\.1\.1\)/
Expand All @@ -40,7 +40,7 @@ Feature: cli/update
And a file named "Puppetfile.lock" with:
"""
FORGE
remote: http://forge.puppetlabs.com
remote: https://forgeapi.puppetlabs.com
specs:
puppetlabs/stdlib (3.1.0)

Expand Down
23 changes: 0 additions & 23 deletions lib/librarian/puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@

module Librarian
module Puppet
@@puppet_version = nil

# Output of puppet --version, typically x.y.z
# For Puppet Enterprise it contains the PE version too, ie. 3.4.3 (Puppet Enterprise 3.2.1)
def puppet_version
return @@puppet_version unless @@puppet_version.nil?

begin
@@puppet_version = Librarian::Posix.run!(%W{puppet --version}).strip
rescue Errno::ENOENT, Librarian::Posix::CommandFailure => error
msg = "Unable to load puppet. Please install it using native packages for your platform (eg .deb, .rpm, .dmg, etc)."
msg += "\npuppet --version returned #{error.status}" if error.respond_to? :status
msg += "\n#{error.message}" unless error.message.nil?
$stderr.puts msg
exit 1
end
return @@puppet_version
end

# Puppet version x.y.z translated as a Gem version
def puppet_gem_version
Gem::Version.create(puppet_version.split(' ').first.strip.gsub('-', '.'))
end

end
end
19 changes: 12 additions & 7 deletions lib/librarian/puppet/source/forge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ def client_api_version()
def initialize(environment, uri, options = {})
self.environment = environment

if uri =~ %r{^http(s)?://forge\.puppetlabs\.com}
uri = "https://forgeapi.puppetlabs.com"
warn { "Replacing Puppet Forge API URL to use v3 #{uri}. You should update your Puppetfile" }
end

@uri = URI::parse(uri)
@cache_path = nil
end
Expand Down Expand Up @@ -158,8 +153,18 @@ def repo(name)
@repo ||= {}

unless @repo[name]
# if we are using the official Forge then use API v3, otherwise stick to v1 for now
if uri.hostname =~ /\.puppetlabs\.com$/ || !environment.use_v1_api
use_version_3 = true
# Use v3 of the api unless the url is the old one or v1 is explicitly set
if uri.hostname =~ /forge\.puppetlabs\.com$/ || environment.use_v1_api
use_version_3 = false
end

#Override the above if they have specified the forgeapi (v3) endpoint
if uri.hostname =~ /forgeapi\.puppetlabs\.com$/
use_version_3 = true
end

if use_version_3
@repo[name] = RepoV3.new(self, name)
else
@repo[name] = RepoV1.new(self, name)
Expand Down
57 changes: 7 additions & 50 deletions lib/librarian/puppet/source/forge/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,13 @@ def cache_version_unpacked!(version)
path = version_unpacked_cache_path(version)
return if path.directory?

# The puppet module command is only available from puppet versions >= 2.7.13
#
# Specifying the version in the gemspec would force people to upgrade puppet while it's still usable for git
# So we do some more clever checking
#
# Executing older versions or via puppet-module tool gives an exit status = 0 .
#
check_puppet_module_options

path.mkpath

target = vendored?(name, version) ? vendored_path(name, version).to_s : name

# can't pass the default v3 forge url (http://forgeapi.puppetlabs.com)
# to clients that use the v1 API (https://forge.puppetlabs.com)
# nor the other way around
module_repository = source.to_s

if Forge.client_api_version() > 1 and module_repository =~ %r{^http(s)?://forge\.puppetlabs\.com}
module_repository = "https://forgeapi.puppetlabs.com"
warn { "Replacing Puppet Forge API URL to use v3 #{module_repository} as required by your client version #{Librarian::Puppet.puppet_version}" }
end

m = module_repository.match(%r{^http(s)?://forge(api)?\.puppetlabs\.com})
if Forge.client_api_version() == 1 and m
ssl = m[1]
# Puppet 2.7 can't handle the 302 returned by the https url, so stick to http
if ssl and Librarian::Puppet::puppet_gem_version < Gem::Version.create('3.0.0')
warn { "Using plain http as your version of Puppet #{Librarian::Puppet::puppet_gem_version} can't download from forge.puppetlabs.com using https" }
ssl = nil
end
module_repository = "http#{ssl}://forge.puppetlabs.com"
end

command = %W{puppet module install --version #{version} --target-dir}
command.push(*[path.to_s, "--module_repository", module_repository, "--modulepath", path.to_s, "--module_working_dir", path.to_s, "--ignore-dependencies", target])
debug { "Executing puppet module install for #{name} #{version}: #{command.join(" ")}" }
download_file(url(name, version), "#{path.to_s}/#{name}-#{version}.tar.gz")

begin
Librarian::Posix.run!(command)
Librarian::Posix.run!(%W{tar -xf #{path.to_s}/#{name}-#{version}.tar.gz -C #{path.to_s}})
FileUtils::mv "#{path.to_s}/#{name}-#{version}", "#{path.to_s}/#{name.split('-')[1]}"
rescue Posix::CommandFailure => e
# Rollback the directory if the puppet module had an error
begin
Expand All @@ -126,29 +93,19 @@ def cache_version_unpacked!(version)
file = tar.first
msg = " (looks like an incomplete download of #{file})"
end
raise Error, "Error executing puppet module install#{msg}. Check that this command succeeds:\n#{command.join(" ")}\nError:\n#{e.message}"
raise Error, "Error extracting module #{msg}."
end

end

def check_puppet_module_options
min_version = Gem::Version.create('2.7.13')

if Librarian::Puppet.puppet_gem_version < min_version
raise Error, "To get modules from the forge, we use the puppet faces module command. For this you need at least puppet version 2.7.13 and you have #{Librarian::Puppet.puppet_version}"
end
end

def vendor_cache(name, version)
url = url(name, version)
path = vendored_path(name, version).to_s
debug { "Downloading #{url} into #{path}"}
environment.vendor!
File.open(path, 'wb') do |f|
open(url, "rb") do |input|
f.write(input.read)
end
end

download_file(url, path)

end

end
Expand Down
14 changes: 1 addition & 13 deletions lib/librarian/puppet/source/githubtarball/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,7 @@ def vendor_cache(name, version)
add_api_token_to_url(url)

environment.vendor!
File.open(vendored_path(vendored_name(name), version).to_s, 'wb') do |f|
begin
debug { "Downloading <#{url}> to <#{f.path}>" }
open(url,
"User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}") do |res|
while buffer = res.read(8192)
f.write(buffer)
end
end
rescue OpenURI::HTTPError => e
raise e, "Error requesting <#{url}>: #{e.to_s}"
end
end
download_file(url, vendored_path(vendored_name(name), version).to_s)
end

def clean_up_old_cached_versions(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/librarian/puppet/source/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def evaluate_modulefile(modulefile)

# Puppet 4 does not have the class
unless defined? ::Puppet::ModuleTool::ModulefileReader
warn { "Can't parse Modulefile in Puppet >= 4.0 and you are using #{Librarian::Puppet::puppet_version}. Ignoring dependencies in #{modulefile}" }
warn { "Can't parse Modulefile in Puppet >= 4.0. Ignoring dependencies in #{modulefile}" }
return metadata
end

Expand Down
16 changes: 16 additions & 0 deletions lib/librarian/puppet/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ def module_name(name)
name.rpartition('-').last
end

def download_file(url, path)
File.open(path, 'wb') do |f|
begin
debug { "Downloading <#{url}> to <#{f.path}>" }
open(url,
"User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}") do |res|
while buffer = res.read(8192)
f.write(buffer)
end
end
rescue OpenURI::HTTPError => e
raise e, "Error requesting <#{url}>: #{e.to_s}"
end
end
end

# deprecated
alias :organization_name :module_name
end
Expand Down
14 changes: 0 additions & 14 deletions spec/librarian_puppet_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/source/forge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe Forge do

let(:environment) { Librarian::Puppet::Environment.new }
let(:uri) { "https://forge.puppetlabs.com" }
let(:uri) { "https://forgeapi.puppetlabs.com" }
let(:puppet_version) { "3.6.0" }
subject { Forge.new(environment, uri) }

Expand Down