Commit b179e82a authored by Carlos Sanchez's avatar Carlos Sanchez

If no Puppetfile is present default to use the metadata.json or Modulefile

parent 1347f0a8
......@@ -37,17 +37,20 @@ See the [Changelog](Changelog.md) for more details.
## The Puppetfile
Every Puppet repository that uses Librarian-puppet will have a file named
`Puppetfile` in the root directory of that repository. The full specification
for which modules your puppet infrastructure repository depends goes in here.
Every Puppet repository that uses Librarian-puppet may have a file named
`Puppetfile`, `metadata.json` or `Modulefile` in the root directory of that repository.
The full specification
for which modules your puppet infrastructure repository depends goes in here.
### Simple Puppetfile
### Simple usage
This Puppetfile will download all the dependencies listed in your Modulefile from the Puppet Forge
If no Puppetfile is present, `librarian-puppet` will download all the dependencies
listed in your `metadata.json` or `Modulefile` from the Puppet Forge,
as if the Puppetfile contained
forge "https://forgeapi.puppetlabs.com"
modulefile
metadata
### Example Puppetfile
......
......@@ -2,10 +2,10 @@ Feature: cli/install
In order to be worth anything
Puppet librarian needs to install modules properly
Scenario: Running install with no Puppetfile
Scenario: Running install with no Puppetfile nor metadata.json
Given there is no Puppetfile
When I run `librarian-puppet install`
Then the output should contain "Could not find Puppetfile"
Then the output should match /^Metadata file does not exist: .*metadata.json$/
And the exit status should be 1
Scenario: Install a module dependency from git and forge should be deterministic
......
......@@ -13,6 +13,35 @@ Feature: cli/install/forge
And the file "modules/ntp/metadata.json" should match /"name": "puppetlabs-ntp"/
And the file "modules/stdlib/metadata.json" should match /"name": "puppetlabs-stdlib"/
Scenario: Running install with no Puppetfile and metadata.json
Given there is no Puppetfile
And a file named "metadata.json" with:
"""
{
"name": "random name",
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": "4.1.0"
}
]
}
"""
When I run `librarian-puppet install`
Then the exit status should be 0
And the file "modules/stdlib/metadata.json" should match /"name": "puppetlabs-stdlib"/
Scenario: Running install with no Puppetfile and Modulefile
Given there is no Puppetfile
And a file named "Modulefile" with:
"""
name "random name"
dependency "puppetlabs/stdlib", "4.1.0"
"""
When I run `librarian-puppet install`
Then the exit status should be 0
And the file "modules/stdlib/metadata.json" should match /"name": "puppetlabs-stdlib"/
Scenario: Installing an exact version of a module
Given a file named "Puppetfile" with:
"""
......
Feature: cli/update
Puppet librarian needs to update modules properly
Scenario: Updating a module with no Puppetfile and with metadata.json
Given a file named "metadata.json" with:
"""
{
"name": "random name",
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": "3.1.x"
}
]
}
"""
And a file named "Puppetfile.lock" with:
"""
FORGE
remote: http://forge.puppetlabs.com
specs:
puppetlabs/stdlib (3.1.0)
DEPENDENCIES
puppetlabs/stdlib (~> 3.0)
"""
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\)/
And the file "modules/stdlib/Modulefile" should match /name *'puppetlabs-stdlib'/
And the file "modules/stdlib/Modulefile" should match /version *'3\.1\.1'/
Scenario: Updating a module with no Puppetfile and with Modulefile
Given a file named "Modulefile" with:
"""
name "random name"
dependency "puppetlabs/stdlib", "3.1.x"
"""
And a file named "Puppetfile.lock" with:
"""
FORGE
remote: http://forge.puppetlabs.com
specs:
puppetlabs/stdlib (3.1.0)
DEPENDENCIES
puppetlabs/stdlib (~> 3.0)
"""
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\)/
And the file "modules/stdlib/Modulefile" should match /name *'puppetlabs-stdlib'/
And the file "modules/stdlib/Modulefile" should match /version *'3\.1\.1'/
Scenario: Updating a module
Given a file named "Puppetfile" with:
"""
......
require "librarian/puppet/action/install"
module Librarian
module Puppet
module Action
class Install < Librarian::Action::Install
private
def create_install_path
install_path.rmtree if install_path.exist? && destructive?
install_path.mkpath
end
def destructive?
environment.config_db.local['destructive'] == 'true'
end
def check_specfile
# don't fail if Puppetfile doesn't exist as we'll use the Modulefile or metadata.json
end
end
end
end
end
......@@ -2,6 +2,7 @@ require 'librarian/helpers'
require 'librarian/cli'
require 'librarian/puppet'
require 'librarian/puppet/action'
module Librarian
module Puppet
......@@ -47,11 +48,6 @@ module Librarian
option "use-v1-api", :type => :boolean, :default => true
def install
unless File.exist?('Puppetfile')
say "Could not find Puppetfile in #{Dir.pwd}", :red
exit 1
end
ensure!
clean! if options["clean"]
unless options["destructive"].nil?
......@@ -89,6 +85,14 @@ module Librarian
def version
say "librarian-puppet v#{Librarian::Puppet::VERSION}"
end
private
# override the actions to use our own
def install!(options = { })
Action::Install.new(environment, options).run
end
end
end
end
......@@ -17,6 +17,14 @@ module Librarian
def run(specfile = nil, sources = [])
specfile, sources = nil, specfile if specfile.kind_of?(Array) && sources.empty?
if specfile.kind_of?(Pathname) and !File.exists?(specfile)
debug { "Specfile not found, using defaults: #{specfile}" }
specfile = Proc.new do
forge "https://forgeapi.puppetlabs.com"
metadata
end
end
Target.new(self).tap do |target|
target.precache_sources(sources)
debug_named_source_cache("Pre-Cached Sources", target)
......@@ -30,17 +38,20 @@ module Librarian
end
class Receiver < Librarian::Dsl::Receiver
attr_reader :specfile
attr_reader :specfile, :working_path
# save the specfile and call librarian
def run(specfile = nil)
@working_path = specfile.kind_of?(Pathname) ? specfile.parent : Pathname.new(Dir.pwd)
@specfile = specfile
super
end
# implement the 'modulefile' syntax for Puppetfile
def modulefile
File.read(Pathname.new(specfile).parent.join('Modulefile')).lines.each do |line|
f = modulefile_path
raise Error, "Modulefile file does not exist: #{f}" unless File.exists?(f)
File.read(f).lines.each do |line|
regexp = /\s*dependency\s+('|")([^'"]+)\1\s*(?:,\s*('|")([^'"]+)\3)?/
regexp =~ line && mod($2, $4)
end
......@@ -48,11 +59,28 @@ module Librarian
# implement the 'metadata' syntax for Puppetfile
def metadata
dependencyList = JSON.parse(File.read(Pathname.new(specfile).parent.join('metadata.json')))['dependencies']
f = working_path.join('metadata.json')
unless File.exists?(f)
msg = "Metadata file does not exist: #{f}"
# try modulefile, in case we don't have a Puppetfile and we are using the default template
if File.exists?(modulefile_path)
modulefile
return
else
raise Error, msg
end
end
dependencyList = JSON.parse(File.read(f))['dependencies']
dependencyList.each do |d|
mod(d['name'], d['version_requirement'])
end
end
private
def modulefile_path
working_path.join('Modulefile')
end
end
end
end
......
......@@ -80,22 +80,6 @@ module Librarian
end
end
module Action
class Install < Base
private
def create_install_path
install_path.rmtree if install_path.exist? && destructive?
install_path.mkpath
end
def destructive?
environment.config_db.local['destructive'] == 'true'
end
end
end
class ManifestSet
include Librarian::Puppet::Util
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment