Skip to content

Instantly share code, notes, and snippets.

@ioggstream
Last active September 18, 2017 14:21
Show Gist options
  • Save ioggstream/bedea98dc53aa61b0ebd48cd1bb73486 to your computer and use it in GitHub Desktop.
Save ioggstream/bedea98dc53aa61b0ebd48cd1bb73486 to your computer and use it in GitHub Desktop.
Stub for gathering heat files
require 'yaml'
require 'open-uri'
def traverse(obj, parent, &blk)
case obj
when Hash
# Forget keys because I don't know what to do with them
obj.each {|k,v| traverse(v, k, &blk) }
when Array
obj.each {|v| traverse(v, parent, &blk) }
else
blk.call(obj, parent)
end
end
def get_content(uri_or_filename)
puts "Downloading #{uri_or_filename}"
if uri_or_filename.start_with?("http")
return open(uri_or_filename).read
else
return File.read(uri_or_filename)
end
end
def is_hot(filename)
# don't parse not-heat filename
if filename.end_with?("yaml")
content = get_content(filename)
htv = content.index("heat_template_version:")
if htv and htv < 5
return true
end
end
return false
end
def is_hot2(content)
htv = content.index("heat_template_version:")
return (htv and htv < 5)
end
def process_file(filename, h, files)
# No infinite loops.
return if h.key?(filename) or files.key?(filename)
# Process only HOT files
content = get_content(filename)
return if not is_hot2(content)
puts "New heat file #{filename}"
ret = YAML.load(content)
# I can parse this node, mark it as visited!
h[filename] = 1
traverse(ret, nil) do |node, parent|
# Files and templates are always strings
next if not node.kind_of?(String)
if parent == 'type'
# Process nested templates (it's a kludge but works)
# we should actually check that 'type' is the resource
# type. Probably Heat is able to resolve nested resources
# without adding them.
if File.exists?(node) or node.start_with?("http")
files[filename] = content
process_file(node, h, files)
end
elsif parent == 'get_file'
# Process plain files
puts "match #{parent}: #{node}"
files[filename] = get_content(node)
else
# Debug unmatching entries
puts "Unmatch #{parent}: #{node}"
end
end
end
filename = 'openshift.yaml'
h = {}
files = {}
process_file(filename, h, files)
puts files
@seanhandley
Copy link

Using open-uri, you can pass the open method a file path or a URI. Calling .read on the result will get the contents for both so you don't need the conditional logic in your get_content method.

@ioggstream
Copy link
Author

+1 for open-uri!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment