Class: Pupistry::Packer
- Inherits:
-
Object
- Object
- Pupistry::Packer
- Defined in:
- lib/pupistry/packer.rb
Overview
Pupistry::Packer
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#template_dir ⇒ Object
Returns the value of attribute template_dir.
Instance Method Summary collapse
- #build(template) ⇒ Object
-
#initialize ⇒ Packer
constructor
A new instance of Packer.
- #list ⇒ Object
- #output_file(filename) ⇒ Object
- #output_plain ⇒ Object
Constructor Details
#initialize ⇒ Packer
Returns a new instance of Packer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pupistry/packer.rb', line 12 def initialize # We need to find where the templates are located - either it should be # in the current working directory, or if we are an installed gem, we # can try the gem's installed path. if Dir.exist?('resources/packer/') # Use local PWD version first if possible @template_dir = Dir.pwd else # Check for GEM installed location begin @template_dir = Gem::Specification.find_by_name('pupistry').gem_dir rescue Gem::LoadError $logger.error "Unable to find packer templates directory, doesn't appear we are running from project dir nor as a Gem" return false end end @template_dir = @template_dir.chomp('/') + '/resources/packer/' if Dir.exist?(@template_dir) $logger.debug "Using directory #{@template_dir} for packer templates" else $logger.error "Unable to find packer templates dir at #{@template_dir}, unable to proceed." return false end end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
10 11 12 |
# File 'lib/pupistry/packer.rb', line 10 def contents @contents end |
#template_dir ⇒ Object
Returns the value of attribute template_dir.
9 10 11 |
# File 'lib/pupistry/packer.rb', line 9 def template_dir @template_dir end |
Instance Method Details
#build(template) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pupistry/packer.rb', line 49 def build(template) # Build a template with the configured parameters already to go and save # into the object, so it can be outputted in the desired format. $logger.debug "Generating a packer template using #{template}" unless File.exist?("#{@template_dir}/#{template}.json.erb") $logger.error 'The requested template does not exist, unable to build' return 0 end # Extract the OS bootstrap name from the template filename, we can then # generate the bootstrap commands to be inserted inline into the packer # configuration. matches = template.match(/^\S*_(\S*)$/) if matches[1] $logger.debug "Fetching bootstrap data for #{matches[1]}..." else $logger.error 'Unable to parse the packer filename properly' return 0 end bootstrap = Pupistry::Bootstrap.new unless bootstrap.build matches[1] $logger.error 'An unexpected error occured when building the bootstrap data to go inside Packer' end # Pass the values we care about to the template template_values = { bootstrap_commands: bootstrap.output_array } # Generate template using ERB begin @contents = Erubis::Eruby.new(File.read("#{@template_dir}/#{template}.json.erb")).result(template_values) rescue StandardError => e $logger.error 'An unexpected error occured when trying to generate the packer template' raise e end end |
#list ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/pupistry/packer.rb', line 40 def list # Simply glob the templates directory and list their names. $logger.debug 'Finding all available templates' Dir.glob("#{@template_dir}/*.erb").each do |file| puts "- #{File.basename(file, '.json.erb')}" end end |
#output_file(filename) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pupistry/packer.rb', line 100 def output_file(filename) # Write the template to the specified file begin File.open(filename, 'w') do |f| f.puts @contents end rescue StandardError => e $logger.error "An unexpected erorr occured when attempting to write the template to #{filename}" raise e else $logger.info "Wrote template into file #{filename} successfully." end end |
#output_plain ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/pupistry/packer.rb', line 92 def output_plain # Do nothing clever, just output the template data. puts '-- Packer Start --' puts @contents puts '-- Packer End --' puts 'Tip: add --file output.json to write out the packer file directly and then run with `packer build output.json`' end |