Class: Pupistry::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/pupistry/bootstrap.rb

Overview

Pupistry::Bootstrap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBootstrap

Returns a new instance of Bootstrap.



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
39
# File 'lib/pupistry/bootstrap.rb', line 13

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/bootstrap/')
    # 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 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/bootstrap/'

  if Dir.exist?(@template_dir)
    $logger.debug "Using directory #{@template_dir} for bootstrap templates"
  else
    $logger.error "Unable to find templates dir at #{@template_dir}, unable to proceed."
    return false
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



11
12
13
# File 'lib/pupistry/bootstrap.rb', line 11

def contents
  @contents
end

#template_dirObject

Returns the value of attribute template_dir.



10
11
12
# File 'lib/pupistry/bootstrap.rb', line 10

def template_dir
  @template_dir
end

Instance Method Details

#build(template, environment) ⇒ Object



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
# File 'lib/pupistry/bootstrap.rb', line 50

def build(template, environment)
  # 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.info "Generating a bootstrap script for #{template} with environment #{environment}"

  unless File.exist?("#{@template_dir}/#{template}.erb")
    $logger.error 'The requested template does not exist, unable to build'
    return 0
  end

  # Assume values we care about
  template_values = {
    s3_bucket: $config['general']['s3_bucket'],
    s3_prefix: $config['general']['s3_prefix'],
    gpg_disable: $config['general']['gpg_disable'],
    gpg_signing_key: $config['general']['gpg_signing_key'],
    puppetcode: $config['agent']['puppetcode'],
    access_key_id: $config['agent']['access_key_id'],
    secret_access_key: $config['agent']['secret_access_key'],
    region: $config['agent']['region'],
    proxy_uri: $config['agent']['proxy_uri'],
    daemon_frequency: $config['agent']['daemon_frequency'],
    daemon_minimal: $config['agent']['daemon_minimal'],
    environment: environment
  }

  # Generate template using ERB
  begin
    @contents = Erubis::Eruby.new(File.read("#{@template_dir}/#{template}.erb")).result(template_values)
  rescue StandardError => e
    $logger.error 'An unexpected error occured when trying to generate the bootstrap template'
    raise e
  end
end

#listObject



41
42
43
44
45
46
47
48
# File 'lib/pupistry/bootstrap.rb', line 41

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, '.erb')}"
  end
end

#output_arrayObject



86
87
88
89
90
# File 'lib/pupistry/bootstrap.rb', line 86

def output_array
  # Return the output as an array of lines. Useful by other internal
  # methods such as Packer templates.
  @contents.split(/\n/)
end

#output_base64Object



99
100
101
102
103
104
105
106
# File 'lib/pupistry/bootstrap.rb', line 99

def output_base64
  # Some providers like AWS can accept the data in Base64 version which is
  # smaller and less likely to get messed up by copy and paste or weird
  # formatting issues.
  puts '-- Bootstrap Start --'
  puts Base64.encode64(@contents)
  puts '-- Bootstrap End --'
end

#output_plainObject



92
93
94
95
96
97
# File 'lib/pupistry/bootstrap.rb', line 92

def output_plain
  # Do nothing clever, just output the template data.
  puts '-- Bootstrap Start --'
  puts @contents
  puts '-- Bootstrap End --'
end