class Anjou::InstanceUserData

Constants

APPENDS
OPTIONALS
PREPENDS

Public Class Methods

new(scripts=[]) click to toggle source
# File lib/anjou/instance_user_data.rb, line 11
def initialize(scripts=[])
  @scripts = scripts
  @script_sequence = 0
end
render_mime(*scripts) click to toggle source
# File lib/anjou/instance_user_data.rb, line 7
def self.render_mime(*scripts)
  self.new(scripts.flatten).render_mime
end

Public Instance Methods

render_mime() click to toggle source
# File lib/anjou/instance_user_data.rb, line 16
def render_mime
  generate_mime
  mime_sections.join("\n")
end
to_s() click to toggle source
# File lib/anjou/instance_user_data.rb, line 21
def to_s
  render_mime
end

Private Instance Methods

add_to(path, scripts) click to toggle source
# File lib/anjou/instance_user_data.rb, line 33
def add_to(path, scripts)
  scripts.each do |script|
    filename  = "#{script}.sh"
    filepath  = "#{path}/#{filename}"
    mime_sections << file_part(filename, File.read(filepath))
  end
end
boundary() click to toggle source
# File lib/anjou/instance_user_data.rb, line 41
def boundary
  @boundary ||= "~~Anjou::UserData~~"
end
file_part(filename, file_contents) click to toggle source
# File lib/anjou/instance_user_data.rb, line 57
def file_part(filename, file_contents)
  [ part_header(filename), file_contents ].join
end
file_sequence() click to toggle source
# File lib/anjou/instance_user_data.rb, line 61
def file_sequence
  "%03d" % ( @script_sequence += 1 )
end
generate_mime() click to toggle source
# File lib/anjou/instance_user_data.rb, line 27
def generate_mime
  add_to PREPENDS,  [ 'install-ubuntu-updates', 'install-git' ]
  add_to OPTIONALS, @scripts
  add_to APPENDS,   [ 'install-motd' ]
end
message_header() click to toggle source
# File lib/anjou/instance_user_data.rb, line 49
    def message_header
      <<-HEADER
Content-Type: multipart/mixed; boundary=#{boundary}
MIME-Version: 1.0

      HEADER
    end
mime_sections() click to toggle source
# File lib/anjou/instance_user_data.rb, line 45
def mime_sections
  @mime_sections ||= [ message_header ]
end
part_header(filename, content_type: 'text/x-shellscript') click to toggle source
# File lib/anjou/instance_user_data.rb, line 65
    def part_header(filename, content_type: 'text/x-shellscript')
      <<-PART
--#{boundary}
Content-Type: #{content_type}
MIME-Version: 1.0
Content-Disposition: attachment; filename="#{file_sequence}-#{filename}"

      PART
    end