Module: Stem::Userdata

Extended by:
Userdata
Included in:
Userdata
Defined in:
lib/stem/userdata.rb

Constant Summary collapse

CREATE_ONLY =
File::CREAT|File::EXCL|File::WRONLY

Instance Method Summary collapse

Instance Method Details

#compile(path, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stem/userdata.rb', line 10

def compile(path, opts = {})
  raise "No absolute paths please" if path.index("/") == 0
  raise "must be a directory" unless File.directory?(path)
  Dir.mktmpdir do |tmp_path|
    # trailing dot copies directory contents to match actual cp -r semantics... go figure
    FileUtils.cp_r("#{path}/.", tmp_path)
    Dir.chdir tmp_path do
      process_erb(opts[:erb_binding])
      process_mustache(opts[:mustache_vars])
      raise "must contain a userdata.sh" unless File.exists?("userdata.sh")
      # Set constant timestamps for all files to guarantee hashing consistency
      make_zip_shell
    end
  end
end

#make_zip_shellObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stem/userdata.rb', line 49

def make_zip_shell
  # We'll comment outside here, to keep from wasting valuable userdata bytes.
  # we decompress into /root/userdata, then run userdata.sh
  header = <<-SHELL
#!/bin/bash
exec >> /var/log/userdata.log 2>&1
date --utc '+BOOTING %FT%TZ'
UD=~/userdata
mkdir -p $UD
sed '1,/^#### THE END$/ d' "$0" | tar -jx -C $UD
cd $UD
exec bash userdata.sh
#### THE END
  SHELL
  header + %x{find . -print0 | sort -z | tar -cv --no-recursion --null --owner=0 --group=0 --mtime='2002-10-28 04:27:00Z' --exclude \\*.stem -T - | bzip2 --best -}
end

#process_erb(binding) ⇒ Object

todo: make this & process_mustache both use binding



27
28
29
30
31
32
33
34
35
36
# File 'lib/stem/userdata.rb', line 27

def process_erb(binding)
  Dir["**/*.erb.stem"].each do |file|
    raise "must pass :erb_binding when using .erb.stem files" unless binding
    require 'erb'
    puts "erb ... #{file}"
    File.open(file.gsub(/.erb.stem$/,""), CREATE_ONLY) do |fff|
      fff.write ERB.new(File.read(file), 0, '<>').result(binding)
    end
  end
end

#process_mustache(vars) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/stem/userdata.rb', line 38

def process_mustache(vars)
  Dir["**/*.mustache.stem"].each do |file|
    raise "must pass :mustache_vars when using .mustache.stem files" unless vars
    require 'mustache'
    puts "mustache ... #{file}"
    File.open(file.gsub(/.mustache.stem$/,""), CREATE_ONLY) do |fff|
      fff.write Mustache.render(File.read(file), vars)
    end
  end
end