Module: Stem::Userdata
Constant Summary collapse
- CREATE_ONLY =
File::CREAT|File::EXCL|File::WRONLY
Instance Method Summary collapse
- #compile(path, opts = {}) ⇒ Object
- #make_zip_shell ⇒ Object
-
#process_erb(binding) ⇒ Object
todo: make this & process_mustache both use binding.
- #process_mustache(vars) ⇒ Object
- #set_all_timestamps(file_or_dir, time) ⇒ Object
Instance Method Details
#compile(path, opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 (tmp_path, "200210272016") make_zip_shell end end end |
#make_zip_shell ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stem/userdata.rb', line 50 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{tar --exclude \\*.stem -cv . | bzip2 --best -} end |
#process_erb(binding) ⇒ Object
todo: make this & process_mustache both use binding
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/stem/userdata.rb', line 28 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
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/stem/userdata.rb', line 39 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 |
#set_all_timestamps(file_or_dir, time) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/stem/userdata.rb', line 67 def (file_or_dir, time) Dir.foreach(file_or_dir) do |item| path = file_or_dir + '/' + item if File.directory?(path) && item != '.' next if item == '..' (path, time) else `touch -t #{time} #{path}` end end end |