Class: R10kResolve

Inherits:
Object
  • Object
show all
Defined in:
lib/r10k-resolve.rb,
lib/r10k-resolve/version.rb

Constant Summary collapse

VERSION =
'0.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ R10kResolve

Returns a new instance of R10kResolve.



8
9
10
11
12
13
14
# File 'lib/r10k-resolve.rb', line 8

def initialize(options = {})
  [:source, :output, :puppetversion, :force, :dryrun, :logger].each do |key|
    send("#{key}=", options[key])
  end

  @output = File.open(@output, 'w+') if @output.is_a? String
end

Instance Attribute Details

#dryrunObject

Returns the value of attribute dryrun.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def dryrun
  @dryrun
end

#forceObject

Returns the value of attribute force.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def force
  @force
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def logger
  @logger
end

#outputObject

Returns the value of attribute output.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def output
  @output
end

#puppetversionObject

Returns the value of attribute puppetversion.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def puppetversion
  @puppetversion
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/r10k-resolve.rb', line 6

def source
  @source
end

Instance Method Details

#resolveObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/r10k-resolve.rb', line 16

def resolve
  content = File.read(@source)
  puppetfile = PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(content)

  # Make sure the Puppetfile is valid
  unless puppetfile.valid?
    logger.error("Puppetfile source is not valid")
    puppetfile.validation_errors.each { |err| logger.error(err) }
    return false
  end

  resolver = PuppetfileResolver::Resolver.new(puppetfile, puppetversion)
  result = resolver.resolve(strict_mode: true)

  # Output resolution validation information
  result.validation_errors.each { |err| logger.info(err) }

  logger.info('Generating Puppetfile...')

  # copy over the existing Puppetfile, then add resolved dependencies below
  @output.write(puppetfile.content)
  @output.write("\n####### resolved dependencies #######\n")

  result.dependency_graph.each do |dep|
    # ignore the original modules, they're already in the output
    next if puppetfile.modules.find { |mod| mod.name == dep.name }

    mod = dep.payload
    next unless mod.is_a?(PuppetfileResolver::Models::ModuleSpecification)

    @output.write("mod '#{dep.payload.owner}-#{dep.payload.name}', '#{dep.payload.version}'\n")
  end

  @output.write("\n# Generated with r10k-resolve v#{R10kResolve::VERSION}")
  @output.write(" for Puppet v#{puppetversion}") if puppetversion
  @output.write("\n\n")

  logger.warn(
    "Please inspect the output to ensure you know what you are deploying in your infrastructure."
  )
end