Class ApplicationSpecification
In: app/models/application_specification.rb
Parent: PseudoModel

Methods

delete   get_hash   obj_to_hash   update   validate   write  

Public Class methods

[Source]

     # File app/models/application_specification.rb, line 273
273:     def delete(app_id)
274:       info = YAML::load_file(CONFIG_FILE_PATH) rescue nil
275:       app_name = info['Application Specification'][app_id]["name"]
276:       info['Application Specification'].delete_at(app_id)
277:       if(info['Application Specification'].length == 0)
278:         info.delete('Application Specification')
279:       end       
280:       YAMLWriter.write(info,CONFIG_FILE_PATH,"config")
281:       return app_name
282:     end

This method is to get application specifications for a specific application from WebROaR config file.

[Source]

     # File app/models/application_specification.rb, line 285
285:     def get_hash(application_id = 1000)
286:       info = YAML::load_file(CONFIG_FILE_PATH) rescue nil
287:       name = ""    
288:       path = ""
289:       run_as_user = ""
290:       type = "Rails"
291:       analytics = "Disabled"
292:       environment = "Production"
293:       port, min_worker, max_worker, log_level = ServerSpecification.get_fields
294:       if !application_id.nil?
295:         if info and info['Application Specification'] and info['Application Specification'][application_id] 
296:           name = info['Application Specification'][application_id]['name'] if info['Application Specification'][application_id]['name']
297:           baseuri = info['Application Specification'][application_id]['baseuri'] if info['Application Specification'][application_id]['baseuri']        
298:           host_names = info['Application Specification'][application_id]['host_names'] if info['Application Specification'][application_id]['host_names']       
299:           path = info['Application Specification'][application_id]['path'] if info['Application Specification'][application_id]['path'] 
300:           run_as_user = info['Application Specification'][application_id]['run_as_user'] if info['Application Specification'][application_id]['run_as_user']    
301:           type = info['Application Specification'][application_id]['type'].capitalize if info['Application Specification'][application_id]['type']
302:           analytics = info['Application Specification'][application_id]['analytics'].capitalize if info['Application Specification'][application_id]['analytics']
303:           environment = info['Application Specification'][application_id]['environment'].capitalize if info['Application Specification'][application_id]['environment']
304:           min_worker = info['Application Specification'][application_id]['min_worker'] if info['Application Specification'][application_id]['min_worker']
305:           max_worker = info['Application Specification'][application_id]['max_worker'] if info['Application Specification'][application_id]['max_worker']
306:         end
307:       end
308:       app_hash = Hash[:app_id => application_id.to_i,
309:         :name => name,                
310:         :path => path,
311:         :run_as_user => run_as_user,
312:         :type1 => type,
313:         :analytics => analytics,
314:         :environment => environment,
315:         :min_worker => min_worker,
316:         :max_worker => max_worker]
317:       app_hash[:resolver] = baseuri if baseuri
318:       app_hash[:resolver] = host_names if host_names
319: 
320:       return app_hash  
321:     end

Public Instance methods

Converting ApplicationSpecification obeject into a Hash.

[Source]

    # File app/models/application_specification.rb, line 62
62:   def obj_to_hash
63:     app = Hash["name"=>name,"path"=>path,"run_as_user"=>run_as_user.downcase,"type"=>type1.downcase,
64:       "analytics"=>analytics.to_s.downcase,"environment"=>environment.downcase,"min_worker"=>min_worker.to_i,
65:       "max_worker"=>max_worker.to_i]
66:     app["baseuri"] = baseuri.strip if baseuri
67:     app["host_names"] = host_names if host_names
68:     return app
69:   end

[Source]

    # File app/models/application_specification.rb, line 71
71:   def update(app_id)
72:     info=YAML::load_file(CONFIG_FILE_PATH) rescue nil
73:     info['Application Specification'][app_id] = obj_to_hash     
74:     YAMLWriter.write(info, CONFIG_FILE_PATH, "config")
75:   end

this method is used to validate the various fields of the apps model.

[Source]

     # File app/models/application_specification.rb, line 78
 78:   def validate     
 79:     errors.add_to_base MIN_WORKERS_VALIDATION if min_worker.to_i>20
 80:     if max_worker.to_i < min_worker.to_i
 81:       errors.add_to_base MAX_WORKERS_VALIDATION_1
 82:     elsif max_worker.to_i>20
 83:       errors.add_to_base MAX_WORKERS_VALIDATION_2
 84:     end
 85:     errors.add_to_base APPLICATION_PATH_EXISTANCE_VALIDATION if !File.directory?(path)
 86:     errors.add_to_base ANALYTICS_VALIDATION if type1=="Rails" and !(analytics.downcase == "enabled" or analytics.downcase == "disabled")
 87:     errors.add_to_base ENVIRONMENT_VALIDATION if !(environment.downcase == "production" or environment.downcase == "development" or environment.downcase == "test")
 88:     errors.add_to_base TYPE_VALIDATION if !(type1.downcase == "rails" or type1.downcase == "rack")
 89:       
 90:     # Resolver take either baseuri or host_names, not both. 
 91:     # baseuri starts with '/'      
 92:     tokens = resolver.split(/ /)
 93:       
 94:     curr_host_names = []
 95:     host_names_flag = 0
 96:     if tokens.size == 1 and tokens[0].start_with?('/')
 97:       write_attribute(:baseuri,resolver)
 98:       errors.add_to_base "BaseURI must start with '/' and contains characters A-Z, a-z, 0-9 , _ , -  and /." if !(baseuri.strip =~ /^\/[A-Za-z0-9_\-\/]*$/i)
 99:     else
100:       write_attribute(:host_names,resolver)
101:       host_name_flag = 1
102:     end
103:     # More than one token would come only in case of host_names
104:     if host_name_flag == 1 and tokens.size > 0
105:       tokens.each do |token|
106:         next if token == ""
107:         len = token.size
108:         # Whole name can not exceed total length of 253
109:         if len > 253
110:           errors.add_to_base HOSTNAME_LENGTH_EXCEEDS
111:           next
112:         end          
113:         # Subdivision can go down to maximum 127 level.
114:         labels = token.split(/\./)
115:         if labels.size > 127
116:           errors.add_to_base SUBDIVISION_EXCEEDS_127
117:           next
118:         end
119:         # Start with '/' indicates BaseURI
120:         if token.start_with?('/')
121:           errors.add_to_base BASEURI_AND_HOSTNAMES_EXIST
122:           break
123:         end
124:         # If wildcard '*' presents, Hostname should prefix with '~'
125:         if token.include?("*") and !(token =~ /^~/)
126:           errors.add_to_base "#{token} - #{START_WTIH_TILD}"   
127:           next         
128:         end
129:         # Wildcard '*' can come either at start or at end, not inbetween
130:         pos = token.index('*',1)
131:         if pos and pos != 1 and pos != len-1
132:           errors.add_to_base "#{token} - #{WILDCARD_AT_START_OR_END}"
133:           next
134:         end
135:         pos = token.index('*',2)
136:         if pos and pos != len-1
137:           errors.add_to_base "#{token} - #{WILDCARD_AT_START_OR_END}"
138:           next
139:         end
140:         if token =~ /[.][.]/
141:           errors.add_to_base "#{token} - #{CONSECUTIVE_DOTS}"
142:           next
143:         end
144:         # Check for Letters, Digits and Hyphen
145:         err_flag = 0
146:         #first label can have '~' as first char and '*' as second char, last label can have '*' as last character
147:         first_label = labels[0]              
148:         last_label = labels[labels.size-1]
149:         labels = labels[1..labels.size-2]
150:         labels.each do |label|
151:           if label.size > 63
152:             errors.add_to_base LABEL_LENGTH_EXCEEDS
153:             next
154:           end
155:           label.each_char do |c|
156:             if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='-' 
157:               next
158:             else
159:               errors.add_to_base HOSTNAME_LDH 
160:               err_flag = 1
161:               break;
162:             end
163:           end
164:         end
165:         char_array = []
166:         first_label.each_char do |c|
167:           char_array << c
168:         end
169:         #p char_array
170:         # checking first character of first label against LDH and ~
171:         if err_flag == 0            
172:           c = char_array[0]
173:           if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='~'             
174:           else
175:             errors.add_to_base HOSTNAME_LDH
176:             err_flag = 1
177:           end
178:         end
179:         #checking second char of first label against LDH and *
180:         if err_flag == 0
181:           c = char_array[1]
182:           if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='*'             
183:           else
184:             errors.add_to_base HOSTNAME_LDH
185:             err_flag = 1
186:           end
187:         end
188:         #checking remaining character of first label against LDH
189:         char_array = char_array[2..char_array.size-1]
190:         if err_flag == 0
191:           char_array.each do |c| 
192:             if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='-' 
193:               next
194:             else
195:               errors.add_to_base HOSTNAME_LDH 
196:               err_flag = 1
197:               break;
198:             end
199:           end            
200:         end
201:         #checking last character of last label against LDH and *
202:         char_array = []
203:         last_label.each_char do |c|
204:           char_array << c
205:         end
206:         if err_flag == 0
207:           c = char_array[char_array.size-1]
208:           if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='*'             
209:           else
210:             errors.add_to_base HOSTNAME_LDH
211:             err_flag = 1
212:           end
213:         end
214:         #checking remaining characters of the last label against LDH
215:         if err_flag == 0
216:           char_array = char_array[1..char_array.size-2]
217:           char_array.each do |c|
218:             if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') or c=='*'             
219:             else
220:               errors.add_to_base HOSTNAME_LDH
221:               err_flag = 1
222:             end
223:           end            
224:         end
225:         if err_flag == 0
226:           curr_host_names << token
227:         end
228:       end
229:     end
230:     
231:     #BaseURI can not be /admin-panel
232:     errors.add_to_base BASEURI_AS_ADMIN_PANEL_BASEURI_VALIDATION if baseuri and baseuri.strip == ADMIN_PANEL_BASE_URI
233:     all_host_names = []
234:     curr_host_names_size = curr_host_names.size
235:       
236:     #checking uniqueness of BaseURI
237:     info= YAML::load_file(CONFIG_FILE_PATH)
238:     if info and info['Application Specification']
239:       i=0
240:       flag=0
241:       while(info['Application Specification'][i])
242:         flag=1 if baseuri and baseuri.strip and info['Application Specification'][i]['baseuri'] == baseuri.strip and app_id.to_i != i.to_i
243:         errors.add_to_base "#{name} - #{APPLICATION_NAME_REPEATED}" if info['Application Specification'][i]['name'] == name and app_id.to_i != i.to_i
244:         all_host_names << info['Application Specification'][i]['host_names'].split(/ /) if info['Application Specification'][i]['host_names'] and curr_host_names_size > 0 and app_id.to_i != i.to_i
245:         i+=1
246:       end        
247:       errors.add_to_base BASEURI_EXISTANCE_VALIDATION if flag==1
248:     end
249:     all_host_names.flatten!
250:     #puts "all=#{all_host_names}"
251:     #puts "curr=#{curr_host_names}"
252:     # checking uniqueness of Hostnames
253:     if curr_host_names_size > 0
254:       errors.add_to_base HOSTNAME_REPEATED  if curr_host_names_size != curr_host_names.uniq.size
255:       if all_host_names.size > 0
256:         curr_host_names.each do |c_host_name|
257:           #p c_host_name
258:           #p c_host_name.length
259:           all_host_names.each do |host_name|
260:             #p host_name
261:             #p host_name.length
262:             if c_host_name.eql?(host_name)
263:               errors.add_to_base "#{c_host_name}-#{HOSTNAME_REPEATED }"
264:               break
265:             end
266:           end
267:         end
268:       end
269:     end      
270:   end

validates_format_of :baseuri, :with => /^\/[A-Za-z0-9_\-\/]*$/i,:message=>"must start with ’/’ and contains characters A-Z, a-z, 0-9 , _ , - and /."

[Source]

    # File app/models/application_specification.rb, line 43
43:   def write
44:     server_specification = ServerSpecification.get_hash
45:     info= YAML::load_file(CONFIG_FILE_PATH) rescue nil
46:     if info 
47:       if info['Application Specification']
48:         i=info['Application Specification'].size
49:         info['Application Specification'][i] = obj_to_hash
50:         data=info['Application Specification']
51:       else
52:         data = Array[obj_to_hash]
53:       end
54:     else
55:       data = Array[obj_to_hash]
56:     end
57:     info = Hash['Server Specification' => server_specification, 'Application Specification' => data]
58:     YAMLWriter.write(info, CONFIG_FILE_PATH, "config")
59:   end

[Validate]