Class: Topo::Provision::ResourceGenerator

Inherits:
Object
  • Object
show all
Includes:
Topo::ParseGen
Defined in:
lib/topo/provision/generators/resource.rb

Constant Summary collapse

@@templates =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Topo::ParseGen

#convert_keys_to_sym, #convert_keys_to_sym_deep, #expand_ref, #lazy_attribute_to_s, #topo_refs, #value_from_path

Constructor Details

#initialize(data) ⇒ ResourceGenerator

Returns a new instance of ResourceGenerator.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/topo/provision/generators/resource.rb', line 34

def initialize(data)
  @resource_type||= "resource"  # define in each class
  @template_base_name = @resource_type
  @undeploy_action = "destroy"
  @resource_attributes = {}  # define in each class
  @name = data['name']
  provisioning = data['provisioning']
  %w[ driver chef_server].each do |key|
    @resource_attributes[key] = provisioning[key] if provisioning && provisioning.key?(key)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/topo/provision/generators/resource.rb', line 31

def name
  @name
end

#resource_attributesObject (readonly)

Returns the value of attribute resource_attributes.



31
32
33
# File 'lib/topo/provision/generators/resource.rb', line 31

def resource_attributes
  @resource_attributes
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



31
32
33
# File 'lib/topo/provision/generators/resource.rb', line 31

def resource_type
  @resource_type
end

#undeploy_actionObject (readonly)

Returns the value of attribute undeploy_action.



31
32
33
# File 'lib/topo/provision/generators/resource.rb', line 31

def undeploy_action
  @undeploy_action
end

Instance Method Details

#default_action(action) ⇒ Object



62
63
# File 'lib/topo/provision/generators/resource.rb', line 62

def default_action(action)
end

#default_resource_template(action) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/topo/provision/generators/resource.rb', line 69

def default_resource_template(action)
  default = "resource_#{action}"
  if @@templates[default] == nil
    path = File.join(File.expand_path("../templates", __FILE__), "#{default}.erb")
    @@templates[default] = ERB.new(File.new(path).read, nil, '>')
  end        
  @@templates[default]
end

#deployObject



54
55
56
# File 'lib/topo/provision/generators/resource.rb', line 54

def deploy()
  puts(template("deploy").result(binding))
end

#do_action(action) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/topo/provision/generators/resource.rb', line 46

def do_action(action)
  if (self.respond_to? action)
    self.send(action) 
  else
    self.send("default_action", action) 
  end
end

#template(action) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/topo/provision/generators/resource.rb', line 79

def template(action)
 name = "#{@template_base_name}_#{action}"
 if (@@templates[name] == nil)
    path = File.join(template_root_dir, "#{name}.erb")
    @@templates[name] = File.exists?(path) ? ERB.new(File.new(path).read, nil, '>') :
      default_resource_template(action)       
  end
  @@templates[name]
end

#template_root_dirObject



65
66
67
# File 'lib/topo/provision/generators/resource.rb', line 65

def template_root_dir
  File.expand_path("../templates", __FILE__)
end

#undeployObject



58
59
60
# File 'lib/topo/provision/generators/resource.rb', line 58

def undeploy()
  puts(template("undeploy").result(binding))
end