Class: Fog::OpenStack::OrchestrationUtil::RecursiveHotFileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb

Overview

Resolve get_file resources found in a HOT template populating

a files Hash conforming to Heat Specs
https://developer.openstack.org/api-ref/orchestration/v1/index.html?expanded=create-stack-detail#stacks

Files present in :files are not processed further. The others

are added to the Hash. This behavior is the same implemented in openstack-infra/shade
see https://github.com/openstack-infra/shade/blob/1d16f64fbf376a956cafed1b3edd8e51ccc16f2c/shade/openstackcloud.py#L1200

This implementation just process nested templates but not resource

registries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, files = nil) ⇒ RecursiveHotFileLoader

Returns a new instance of RecursiveHotFileLoader.



25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 25

def initialize(template, files = nil)
  # According to https://github.com/fog/fog-openstack/blame/master/docs/orchestration.md#L122
  #  templates can be either String or Hash.
  #  If it's an Hash, we deep_copy it so the passed argument
  #  is not modified by get_file_contents.
  template = deep_copy(template)
  @visited = Set.new
  @files = files || {}
  @template = get_template_contents(template)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



22
23
24
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 22

def files
  @files
end

#templateObject (readonly)

Returns the value of attribute template.



23
24
25
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 23

def template
  @template
end

Instance Method Details

#get_file_contents(from_data, base_url) ⇒ Object

Traverse the template tree looking for get_file and type

and populating the @files attribute with their content.
Resource referenced by get_file and type are eventually
replaced with their absolute URI as done in heatclient
and shade.


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 95

def get_file_contents(from_data, base_url)
  Fog::Logger.debug("Processing #{from_data} with base_url #{base_url}")

  # Recursively traverse the tree
  #   if recurse_data is Array or Hash
  recurse_data = from_data.kind_of?(Hash) ? from_data.values : from_data
  recurse_data.each { |value| get_file_contents(value, base_url) } if recurse_data.kind_of?(Array)

  # I'm on a Hash, process it.
  return unless from_data.kind_of?(Hash)
  from_data.each do |key, value|
    next if ignore_if(key, value)

    # Resolve relative paths.
    str_url = url_join(base_url, value)

    next if @files.key?(str_url)

    file_content = read_uri(str_url)

    # get_file should not recurse hot templates.
    if key == "type" && template_is_raw?(file_content) && !@visited.include?(str_url)
      template = get_template_contents(str_url)
      file_content = YAML.dump(template)
    end

    @files[str_url] = file_content
    # replace the data value with the normalised absolute URL as required
    #  by https://docs.openstack.org/heat/pike/template_guide/hot_spec.html#get-file
    from_data[key] = str_url
  end
end

#get_template_contents(template_file) ⇒ Object

Retrieve a template content.

XXX: we could use named parameters and better mimic heatclient implementation.

Parameters:

  • template_file

    can be either:

    • a raw_template string

    • an URI string

    • an Hash containing the parsed template.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 58

def get_template_contents(template_file)
  Fog::Logger.debug("get_template_contents [#{template_file}]")

  raise "template_file should be Hash or String, not #{template_file.class.name}" unless
    template_file.kind_of?(String) || template_file.kind_of?(Hash)

  local_base_url = url_join("file:/", File.absolute_path(Dir.pwd))

  if template_file.kind_of?(Hash)
    template_base_url = local_base_url
    template = template_file
  elsif template_is_raw?(template_file)
    template_base_url = local_base_url
    template = YAML.safe_load(template_file, [Date])
  elsif template_is_url?(template_file)
    template_file = normalise_file_path_to_url(template_file)
    template_base_url = base_url_for_url(template_file)
    raw_template = read_uri(template_file)
    template = YAML.safe_load(raw_template, [Date])

    Fog::Logger.debug("Template visited: #{@visited}")
    @visited.add(template_file)
  else
    raise "template_file is not a string of the expected form"
  end

  get_file_contents(template, template_base_url)

  template
end

#url_join(prefix, suffix) ⇒ Object

Return string



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/openstack/orchestration/util/recursive_hot_file_loader.rb', line 37

def url_join(prefix, suffix)
  if prefix
    # URI.join replaces prefix parts before a
    #  trailing slash. See https://docs.ruby-lang.org/en/2.3.0/URI.html.
    prefix += '/' unless prefix.to_s.end_with?("/")
    suffix = URI.join(prefix, suffix)
    # Force URI to use traditional file scheme representation.
    suffix.host = "" if suffix.scheme == "file"
  end
  suffix.to_s
end