Class: District

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDistrict

Returns a new instance of District.



6
7
# File 'app/models/district.rb', line 6

def initialize()
end

Instance Attribute Details

#active_server_identities_sizeObject

Returns the value of attribute active_server_identities_size.



3
4
5
# File 'app/models/district.rb', line 3

def active_server_identities_size
  @active_server_identities_size
end

#available_capacityObject

Returns the value of attribute available_capacity.



3
4
5
# File 'app/models/district.rb', line 3

def available_capacity
  @available_capacity
end

#available_uidsObject

Returns the value of attribute available_uids.



3
4
5
# File 'app/models/district.rb', line 3

def available_uids
  @available_uids
end

#creation_timeObject

Returns the value of attribute creation_time.



3
4
5
# File 'app/models/district.rb', line 3

def creation_time
  @creation_time
end

#externally_reserved_uids_sizeObject

Returns the value of attribute externally_reserved_uids_size.



3
4
5
# File 'app/models/district.rb', line 3

def externally_reserved_uids_size
  @externally_reserved_uids_size
end

#max_capacityObject

Returns the value of attribute max_capacity.



3
4
5
# File 'app/models/district.rb', line 3

def max_capacity
  @max_capacity
end

#max_uidObject

Returns the value of attribute max_uid.



3
4
5
# File 'app/models/district.rb', line 3

def max_uid
  @max_uid
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'app/models/district.rb', line 3

def name
  @name
end

#node_profileObject

Returns the value of attribute node_profile.



3
4
5
# File 'app/models/district.rb', line 3

def node_profile
  @node_profile
end

#server_identitiesObject

Returns the value of attribute server_identities.



3
4
5
# File 'app/models/district.rb', line 3

def server_identities
  @server_identities
end

#uuidObject

Returns the value of attribute uuid.



3
4
5
# File 'app/models/district.rb', line 3

def uuid
  @uuid
end

Class Method Details

.find(uuid) ⇒ Object



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

def self.find(uuid)
  hash = OpenShift::DataStore.instance.find_district(uuid, {:read => :primary})
  return nil unless hash
  hash_to_district(hash)
end

.find_allObject



36
37
38
39
40
41
42
43
# File 'app/models/district.rb', line 36

def self.find_all()
  data = OpenShift::DataStore.instance.find_all_districts()
  return [] unless data
  districts = data.map do |hash|
    hash_to_district(hash)
  end
  districts
end

.find_available(node_profile = nil) ⇒ Object



45
46
47
48
49
# File 'app/models/district.rb', line 45

def self.find_available(node_profile=nil)
  hash = OpenShift::DataStore.instance.find_available_district(node_profile)
  return nil unless hash
  hash_to_district(hash)
end

.find_by_name(name) ⇒ Object



30
31
32
33
34
# File 'app/models/district.rb', line 30

def self.find_by_name(name)
  hash = OpenShift::DataStore.instance.find_district_by_name(name, {:read => :primary})
  return nil unless hash
  hash_to_district(hash)
end

Instance Method Details

#activate_node(server_identity) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/district.rb', line 141

def activate_node(server_identity)
  if server_identities.has_key?(server_identity)
    unless server_identities[server_identity]["active"]
      OpenShift::DataStore.instance.activate_district_node(@uuid, server_identity)
      container = OpenShift::ApplicationContainerProxy.instance(server_identity)
      container.set_district(@uuid, true)
      server_identities[server_identity] = {"active" => true}
    else
      raise OpenShift::OOException.new("Node with server identity: #{server_identity} is already active")
    end
  else
    raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
  end
end

#add_capacity(num_uids) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/district.rb', line 156

def add_capacity(num_uids)
  if num_uids > 0
    additions = []
    additions.fill(0, num_uids) {|i| i+max_uid+1}
    OpenShift::DataStore.instance.add_district_uids(uuid, additions)
    @available_capacity += num_uids
    @max_uid += num_uids
    @max_capacity += num_uids
    @available_uids += additions
  else
    raise OpenShift::OOException.new("You must supply a positive number of uids to remove")
  end
end

#add_node(server_identity) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/district.rb', line 69

def add_node(server_identity)
  if server_identity
    hash = OpenShift::DataStore.instance.find_district_with_node(server_identity)
    unless hash
      unless server_identities.has_key?(server_identity)
        container = OpenShift::ApplicationContainerProxy.instance(server_identity)
        begin
          capacity = container.get_capacity
          if capacity == 0
            container_node_profile = container.get_node_profile
            if container_node_profile == node_profile 
              container.set_district(@uuid, true)
              server_identities[server_identity] = {"active" => true}
              OpenShift::DataStore.instance.add_district_node(@uuid, server_identity)
            else
              raise OpenShift::OOException.new("Node with server identity: #{server_identity} is of node profile '#{container_node_profile}' and needs to be '#{node_profile}' to add to district '#{name}'")  
            end
          else
            raise OpenShift::OOException.new("Node with server identity: #{server_identity} already has apps on it")
          end
        rescue OpenShift::NodeException => e
          raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be found")
        end
      else
        raise OpenShift::OOException.new("Node with server identity: #{server_identity} already belongs to district: #{@uuid}")
      end
    else
      raise OpenShift::OOException.new("Node with server identity: #{server_identity} already belongs to another district: #{hash["uuid"]}")
    end
  else
    raise OpenShift::UserException.new("server_identity is required")
  end
end

#construct(name, node_profile = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/district.rb', line 9

def construct(name, node_profile=nil)
  self.uuid = OpenShift::Model.gen_uuid
  self.creation_time = DateTime::now().strftime
  self.server_identities = {}
  self.available_capacity = Rails.configuration.msg_broker[:districts][:max_capacity]
  self.available_uids = []
  self.available_uids.fill(0, Rails.configuration.msg_broker[:districts][:max_capacity]) {|i| i+Rails.configuration.msg_broker[:districts][:first_uid]}
  self.max_uid = Rails.configuration.msg_broker[:districts][:max_capacity] + Rails.configuration.msg_broker[:districts][:first_uid] - 1
  self.max_capacity = Rails.configuration.msg_broker[:districts][:max_capacity]
  self.externally_reserved_uids_size = 0
  self.active_server_identities_size = 0
  self.name = name
  self.node_profile = node_profile ? node_profile : "small"
end

#deactivate_node(server_identity) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/district.rb', line 126

def deactivate_node(server_identity)
  if server_identities.has_key?(server_identity)
    if server_identities[server_identity]["active"]
      OpenShift::DataStore.instance.deactivate_district_node(@uuid, server_identity)
      container = OpenShift::ApplicationContainerProxy.instance(server_identity)
      container.set_district(@uuid, false)
      server_identities[server_identity] = {"active" => false}
    else
      raise OpenShift::OOException.new("Node with server identity: #{server_identity} is already deactivated")
    end
  else
    raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
  end
end

#deleteObject



51
52
53
54
55
56
57
# File 'app/models/district.rb', line 51

def delete()
  if server_identities.empty?
    OpenShift::DataStore.instance.delete_district(@uuid)
  else
    raise OpenShift::OOException.new("Couldn't destroy district '#{uuid}' because it still contains nodes")
  end
end

#remove_capacity(num_uids) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/district.rb', line 170

def remove_capacity(num_uids)
  if num_uids > 0
    subtractions = []
    subtractions.fill(0, num_uids) {|i| i+max_uid-num_uids+1}
    pos = 0
    found_first_pos = false
    available_uids.each do |available_uid|
      if !found_first_pos && available_uid == subtractions[pos]
        found_first_pos = true
      elsif found_first_pos
        unless available_uid == subtractions[pos]
          raise OpenShift::OOException.new("Uid: #{subtractions[pos]} not found in order in available_uids.  Can not continue!")
        end
      end
      pos += 1 if found_first_pos
      break if pos == subtractions.length
    end
    if !found_first_pos
      raise OpenShift::OOException.new("Missing uid: #{subtractions[0]} in existing available_uids.  Can not continue!")
    end
    OpenShift::DataStore.instance.remove_district_uids(uuid, subtractions)
    @available_capacity -= num_uids
    @max_uid -= num_uids
    @max_capacity -= num_uids
    @available_uids -= subtractions
  else
    raise OpenShift::OOException.new("You must supply a positive number of uids to remove")
  end
end

#remove_node(server_identity) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/district.rb', line 103

def remove_node(server_identity)
  if server_identities.has_key?(server_identity)
    unless server_identities[server_identity]["active"]
      container = OpenShift::ApplicationContainerProxy.instance(server_identity)
      capacity = container.get_capacity
      if capacity == 0
        if OpenShift::DataStore.instance.remove_district_node(@uuid, server_identity)
          container.set_district('NONE', false)
          server_identities.delete(server_identity)
        else
          raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be removed from district: #{@uuid}")
        end
      else
        raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be removed from district: #{@uuid} because it still has apps on it")
      end
    else
      raise OpenShift::OOException.new("Node with server identity: #{server_identity} from district: #{@uuid} must be deactivated before it can be removed")
    end
  else
    raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
  end
end

#saveObject



59
60
61
62
63
64
65
66
67
# File 'app/models/district.rb', line 59

def save()
  OpenShift::DataStore.instance.save_district(@uuid, self.attributes)
  @previously_changed = changes
  @changed_attributes.clear
  @new_record = false
  @persisted = true
  @deleted = false
  self
end