Class: Bootscript::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/bootscript/script.rb

Overview

Main functional class. Models and builds a self-extracting Bash/TAR file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Script

constructor - configures the AWS S3 connection and logging

Parameters:

  • logger (::Logger) (defaults to: nil)
    • a standard Ruby logger



25
26
27
28
29
# File 'lib/bootscript/script.rb', line 25

def initialize(logger = nil)
  @log      ||= logger || Bootscript.default_logger
  @data_map = Hash.new
  @vars     = Hash.new
end

Instance Attribute Details

#data_mapObject

A Hash of data sources to be written onto the boot target’s filesystem. Each (String) key is a path to the desired file on the boot target. Each value can be a String (treated as ERB), or Object with a read method. Any Ruby File objects with extension “.ERB” are also processed as ERB.



18
19
20
# File 'lib/bootscript/script.rb', line 18

def data_map
  @data_map
end

#logObject (readonly)

Standard Ruby Logger, overridden by passing :logger to #initialize



21
22
23
# File 'lib/bootscript/script.rb', line 21

def log
  @log
end

Instance Method Details

#generate(erb_vars = {}, destination = nil) ⇒ Fixnum, String

Generates the BootScript contents by interpreting the @data_map based on erb_vars. If destination has a write() method, the data is streamed there line-by-line, and the number of bytes written is returned. Otherwise, the BootScript contents are returned as a String. In the case of streaming output, the destination must be already opened.

Parameters:

  • erb_vars (Hash) (defaults to: {})

    Ruby variables to interpolate into all templates

  • destination (IO) (defaults to: nil)

    a Ruby object that responds to write(String)

Returns:

  • (Fixnum)

    the number of bytes written to the destination, or

  • (String)

    the text of the rendered script, if destination is nil



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bootscript/script.rb', line 40

def generate(erb_vars = {}, destination = nil)
  # Set state / instance variables, used by publish() and helper methods
  @vars           = Bootscript.merge_platform_defaults(erb_vars)
  output          = destination || StringIO.open(@script_data = "")
  @bytes_written  = 0
  if Bootscript.windows?(@vars)
    @bytes_written += output.write(render_erb_text(File.read(
      "#{File.dirname(__FILE__)}/../templates/windows_header.bat.erb"
    )))
  end
  write_bootscript(output)          # streams the script part line-by-line
  write_uuencoded_archive(output)   # streams the archive line-by-line
  if Bootscript.windows?(@vars)
    @bytes_written += output.write(render_erb_text(File.read(
      "#{File.dirname(__FILE__)}/../templates/windows_footer.bat.erb"
    )))
  end
  output.close unless destination   # (close StringIO if it was opened)
  return (destination ? @bytes_written : @script_data)
end