| 
									
										
										
										
											2015-06-17 22:37:08 +03:00
										 |  |  | require 'lib/string_helper' | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  | require 'db/mongo/models/stack_template/stack_template_factory' | 
					
						
							| 
									
										
										
										
											2015-06-17 22:37:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Devops | 
					
						
							|  |  |  |   module StackTemplatePresets | 
					
						
							|  |  |  |     class Base | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def id | 
					
						
							|  |  |  |         StringHelper.underscore_class(self.class) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def to_hash | 
					
						
							|  |  |  |         {id: id, template_preset_body: template_preset_body} | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  |       def build_template_from_preset(provider, stack_template_id, template_attrs) | 
					
						
							|  |  |  |         stack_template_attrs = { | 
					
						
							| 
									
										
										
										
											2015-06-22 13:51:04 +03:00
										 |  |  |           'id' => stack_template_id, | 
					
						
							|  |  |  |           'provider' => provider, | 
					
						
							|  |  |  |           'template_body' => serialize(actual_template_hash(template_attrs)) | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |         template_model = Model::StackTemplateFactory.create(provider, stack_template_attrs) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 22:37:08 +03:00
										 |  |  |       def template_preset_body | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  |         @template_preset_body ||= begin | 
					
						
							|  |  |  |           file_name = File.join("lib/stack_template_presets/#{id}.#{template_file_extension}") | 
					
						
							|  |  |  |           File.read(file_name) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def template_preset_hash | 
					
						
							|  |  |  |         unserialize(template_preset_body) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 11:59:31 +03:00
										 |  |  |       def actual_template_hash(template_attrs) | 
					
						
							| 
									
										
										
										
											2015-06-22 13:51:04 +03:00
										 |  |  |         actual = template_preset_hash.dup | 
					
						
							| 
									
										
										
										
											2015-06-22 11:59:31 +03:00
										 |  |  |         template_attrs.each do |key, value| | 
					
						
							| 
									
										
										
										
											2015-06-22 13:51:04 +03:00
										 |  |  |           path = param_attrs_pathes_in_preset[key.to_s] | 
					
						
							| 
									
										
										
										
											2015-06-22 11:59:31 +03:00
										 |  |  |           next unless path | 
					
						
							|  |  |  |           insert_value_at(actual, value, path) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         actual | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 11:59:31 +03:00
										 |  |  |       # returns hash with keys lists. Better to show an example: | 
					
						
							|  |  |  |       # Imagine tha t template is the following: | 
					
						
							|  |  |  |       # { | 
					
						
							|  |  |  |       #   template: { | 
					
						
							|  |  |  |       #     users_count: %users_count% | 
					
						
							|  |  |  |       #   } | 
					
						
							|  |  |  |       # } | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # So param_attrs_pathes_in_preset will be | 
					
						
							|  |  |  |       # {users_count: [:template, :users_count]} | 
					
						
							|  |  |  |       def param_attrs_pathes_in_preset | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # given hash = {t: {a: nil}}, value = 1, path = [:t, :a] returns | 
					
						
							|  |  |  |       # {t: {a: 1}} | 
					
						
							|  |  |  |       def insert_value_at(hash, value, path) | 
					
						
							|  |  |  |         current = hash | 
					
						
							|  |  |  |         path.each_with_index do |key, i| | 
					
						
							|  |  |  |           if i == path.size - 1
 | 
					
						
							|  |  |  |             current[key] = value | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             current = current[key] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         hash | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # some templates may be YAML files | 
					
						
							|  |  |  |       def template_file_extension | 
					
						
							|  |  |  |         serialization_strategy.to_s | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # some templates may be YAML files | 
					
						
							|  |  |  |       def serialize(hash) | 
					
						
							|  |  |  |         case serialization_strategy | 
					
						
							|  |  |  |         when :json | 
					
						
							|  |  |  |           hash.to_json | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def unserialize(text) | 
					
						
							|  |  |  |         case serialization_strategy | 
					
						
							|  |  |  |         when :json | 
					
						
							|  |  |  |           JSON.parse(text) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-06-17 22:37:08 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 17:53:03 +03:00
										 |  |  |       def serialization_strategy | 
					
						
							|  |  |  |         :json | 
					
						
							| 
									
										
										
										
											2015-06-17 22:37:08 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |