Class: ComponentInstance

Inherits:
OpenShift::Model
  • Object
show all
Defined in:
app/models/component_instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cartname = nil, profname = nil, groupname = nil, compname = nil, pathname = nil, gi = nil) ⇒ ComponentInstance

Returns a new instance of ComponentInstance.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/component_instance.rb', line 5

def initialize (cartname=nil, profname=nil, groupname=nil, compname=nil, pathname=nil, gi=nil)
  self.name = pathname
  self.parent_cart_name = cartname
  self.parent_cart_profile = profname
  self.parent_cart_group = groupname
  self.group_instance_name = gi.name unless gi.nil?
  self.parent_component_name = compname
  self.dependencies = []
  self.exec_order = []
  self.cart_data = []
  self.cart_properties = {}
  self.addtl_fs_gb = 0
end

Instance Attribute Details

#addtl_fs_gbObject

Returns the value of attribute addtl_fs_gb.



2
3
4
# File 'app/models/component_instance.rb', line 2

def addtl_fs_gb
  @addtl_fs_gb
end

#cart_dataObject

Returns the value of attribute cart_data.



2
3
4
# File 'app/models/component_instance.rb', line 2

def cart_data
  @cart_data
end

#cart_propertiesObject

Returns the value of attribute cart_properties.



2
3
4
# File 'app/models/component_instance.rb', line 2

def cart_properties
  @cart_properties
end

#dependenciesObject

Returns the value of attribute dependencies.



2
3
4
# File 'app/models/component_instance.rb', line 2

def dependencies
  @dependencies
end

#exec_orderObject

Returns the value of attribute exec_order.



2
3
4
# File 'app/models/component_instance.rb', line 2

def exec_order
  @exec_order
end

#group_instance_nameObject

Returns the value of attribute group_instance_name.



2
3
4
# File 'app/models/component_instance.rb', line 2

def group_instance_name
  @group_instance_name
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'app/models/component_instance.rb', line 2

def name
  @name
end

#parent_cart_groupObject

Returns the value of attribute parent_cart_group.



2
3
4
# File 'app/models/component_instance.rb', line 2

def parent_cart_group
  @parent_cart_group
end

#parent_cart_nameObject

Returns the value of attribute parent_cart_name.



2
3
4
# File 'app/models/component_instance.rb', line 2

def parent_cart_name
  @parent_cart_name
end

#parent_cart_profileObject

Returns the value of attribute parent_cart_profile.



2
3
4
# File 'app/models/component_instance.rb', line 2

def parent_cart_profile
  @parent_cart_profile
end

#parent_component_nameObject

Returns the value of attribute parent_component_name.



2
3
4
# File 'app/models/component_instance.rb', line 2

def parent_component_name
  @parent_component_name
end

#stateObject

Returns the value of attribute state.



2
3
4
# File 'app/models/component_instance.rb', line 2

def state
  @state
end

Class Method Details

.collect_exec_order(app, cinst, return_list) ⇒ Object



199
200
201
202
203
204
205
# File 'app/models/component_instance.rb', line 199

def self.collect_exec_order(app, cinst, return_list)
  cinst.exec_order.each do |dep|
    depinst = app.comp_instance_map[dep]
    collect_exec_order(app, depinst, return_list)
    return_list << dep if not return_list.include? dep
  end
end

.establish_connections(inst1, inst2, app) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/component_instance.rb', line 93

def self.establish_connections(inst1, inst2, app)
  comp1,prof1,cart1 = inst1.get_component_definition(app)
  comp2,prof2,cart2 = inst2.get_component_definition(app)

  new_connections = []
  
  comp1.publishes.each do |pub|
    comp2.subscribes.each do |sub|
      next if not pub.type==sub.type
      ce = ConnectionEndpoint.new(inst1, inst2, pub, sub) 
      app.conn_endpoints_list << ce
      new_connections << ce
    end
  end
  return if inst1==inst2
  comp1.subscribes.each do |sub|
    comp2.publishes.each do |pub|
      next if not pub.type==sub.type
      ce = ConnectionEndpoint.new(inst2, inst1, pub, sub) 
      app.conn_endpoints_list << ce
      new_connections << ce
    end
  end
  new_connections
end

.find_component_in_cart(profile, app, comp_name, parent_path) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/component_instance.rb', line 158

def self.find_component_in_cart(profile, app, comp_name, parent_path) 
  # FIXME : comp_name could be a component_name only, feature_name, cartridge_name that 
  #         one of the components depend upon, or a hierarchical name

  comp_name_list = comp_name.split("/")
  comp_name = comp_name_list.first
  rest = comp_name_list[1..-1]

  group_comp_refs = profile.groups.map { |g| g.component_refs }
  comp_refs = group_comp_refs.flatten
  comp_inst = nil
  comp_refs.each { |comp_ref|
    name_prefix = comp_ref.get_name_prefix(profile)
    comp_inst = app.comp_instance_map[parent_path + name_prefix]
    next if comp_inst.nil?
    if name_prefix!=""
      next if comp_ref.name!=comp_name
      return comp_inst if rest.length==0
      comp_name = rest.first
      rest = rest[1..-1]
    end
    comp_inst.dependencies.each { |dep|
      dep_inst = app.comp_instance_map[dep]
      next if dep_inst.parent_cart_name != comp_name
      return dep_inst if rest.length==0
      c, dep_profile, ca = dep_inst.get_component_definition(app)
      found_inst = self.find_component_in_cart(dep_profile, app, rest.join('/'), dep_inst.name)
      return found_inst if not found_inst.nil?
    }
  }

  return comp_inst
end

Instance Method Details

#elaborate(app) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/component_instance.rb', line 44

def elaborate(app)
  comp,profile,cart = get_component_definition(app)

  self.dependencies = []
  
  # cart map has all the sub-cartridges that will get instantiated through this component instance
  cart_map, cart_map_keys = get_cartridges_for_dependencies(comp, cart)

  group_list = cart_map_keys.map { |key| 
    cartprofile = cart_map[key]
    elaborate_cartridge(cartprofile[0], cartprofile[1], app) 
  }.flatten

  self.dependencies.each do |dep|
    cinst = app.comp_instance_map[dep]
    new_conns = ComponentInstance.establish_connections(cinst, self, app)
    new_conns.each { |conn|
      if conn.from_connector.type.match(/^FILESYSTEM/) or conn.from_connector.type.match(/^AFUNIX/)
        self.exec_order << dep if not self.exec_order.include? dep
      end
    }
  end

  ComponentInstance.establish_connections(self, self, app)

  resolve_exec_order(app)

  return group_list
end

#elaborate_cartridge(cart, profile, app) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/component_instance.rb', line 119

def elaborate_cartridge(cart, profile, app)
  group_list = profile.groups.map do |g|
     gpath = self.name + cart.get_name_prefix + g.get_name_prefix
     gi = app.working_group_inst_hash[gpath]
     if gi.nil?
       gi = app.group_instance_map[gpath]
       if gi.nil?
         gi = GroupInstance.new(app, cart.name, profile.name, g.name, gpath) 
       else
         gi.merge(cart.name, profile.name, g.name, gpath)
       end
     else
       gi.merge(cart.name, profile.name, g.name, gpath)
     end
     app.group_instance_map[gpath] = gi
     app.working_group_inst_hash[gpath] = gi
     sub_components = gi.elaborate(profile, g, self.name, app)
     self.dependencies += sub_components
     gi
  end

  # make connection_endpoints out of provided connections
  profile.connections.each do |conn|
    inst1 = ComponentInstance::find_component_in_cart(profile, app, conn.components[0], self.name)
    inst2 = ComponentInstance::find_component_in_cart(profile, app, conn.components[1], self.name)
    ComponentInstance::establish_connections(inst1, inst2, app)
  end
  
  profile.group_overrides.each do |n, v|
    from_cinst = ComponentInstance::find_component_in_cart(profile, app, n, self.name)
    to_cinst = ComponentInstance::find_component_in_cart(profile, app, v, self.name)
    next if from_cinst.nil? or to_cinst.nil?
    app.group_override_map[from_cinst.group_instance_name] = to_cinst.group_instance_name
    app.group_override_map[to_cinst.group_instance_name] = from_cinst.group_instance_name
  end

  return group_list
end

#get_cartridges_for_dependencies(comp, cart) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/models/component_instance.rb', line 207

def get_cartridges_for_dependencies(comp, cart)
  # resolve features into cartridges - two features may resolve
  # into one cartridge only, e.g. depends = [db,db-failover] 
  # will resolve into one mysql cartridge being instantiated with (master/slave) profile
  cart_map = {}
  cart_map_keys = []
  depends = comp.depends + cart.requires_feature
  
  depends.each do |feature| 
    cart = CartridgeCache::find_cartridge(feature)
    raise OpenShift::UserException.new("Invalid cartridge specified: #{feature}",1) if cart.nil?
    capability = feature
    capability = nil if feature==cart.name
    profile = cart.find_profile(capability)
    key = cart.name + profile.name
    cart_map_keys << key if not cart_map_keys.include? key
    cart_map[key] = [cart, profile]
  end
  return cart_map, cart_map_keys
end

#get_component_definition(app) ⇒ Object

Raises:

  • (Exception)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/component_instance.rb', line 30

def get_component_definition(app)
  if self.parent_cart_name == app.name
    cart = app
  else
    cart = CartridgeCache::find_cartridge(self.parent_cart_name)
  end
  raise Exception.new("Cartridge #{self.parent_cart_name} not found") if cart.nil?
  profile = cart.profiles(self.parent_cart_profile)
  group = profile.groups(self.parent_cart_group)
  comp_name = group.component_refs(self.parent_component_name).component
  comp = profile.components(comp_name)
  return comp,profile,cart
end

#process_cart_data(data) ⇒ Object



19
20
21
22
# File 'app/models/component_instance.rb', line 19

def process_cart_data(data)
  data.chomp! if data
  self.cart_data.push data
end

#process_cart_properties(properties) ⇒ Object



24
25
26
27
28
# File 'app/models/component_instance.rb', line 24

def process_cart_properties(properties)
  properties.each do |key, value|
    self.cart_properties[key] = value
  end
end

#resolve_exec_order(app) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/component_instance.rb', line 74

def resolve_exec_order(app)
  deps = self.dependencies.dup
  self.dependencies.each { |dep| 
    depinst = app.comp_instance_map[dep]
    comp,p,c = depinst.get_component_definition(app)
    if comp.depends_service and !comp.depends_service.empty?
      comp.depends_service.each { |dependent_cart|
        deps.each { |parent_dep| 
          if parent_dep.include? dependent_cart 
            # add parent_dep to exec_order
            self.exec_order << parent_dep if not self.exec_order.include? parent_dep
          end
        }
      }
    end
    self.exec_order << dep if not self.exec_order.include? dep 
  }
end

#set_additional_quota(app, fs_gb, inodes = nil) ⇒ Object



192
193
194
195
196
197
# File 'app/models/component_instance.rb', line 192

def set_additional_quota(app, fs_gb, inodes=nil)
  changed_gbs = fs_gb - @addtl_fs_gb
  gi = app.group_instance_map[self.group_instance_name]
  gi.update_quota(changed_gbs, inodes)
  @addtl_fs_gb = fs_gb
end