| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | #!/usr/bin/env ruby | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | require "erb" | 
					
						
							|  |  |  | require "yaml" | 
					
						
							|  |  |  | require "ostruct" | 
					
						
							| 
									
										
										
										
											2014-07-14 13:05:14 +04:00
										 |  |  | require "fileutils" | 
					
						
							| 
									
										
										
										
											2014-11-27 18:45:35 +03:00
										 |  |  | require "./templates/fixtures/fixture_formatter" | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-14 13:05:14 +04:00
										 |  |  | class Generator < OpenStruct | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-14 13:25:10 +04:00
										 |  |  |   CONFIG = "params.yml" | 
					
						
							|  |  |  |   TESTS_CONFIG = "features/support/config.yml" | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def initialize | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |     config_file = ENV["DEVOPS_FEATURES_GENERATOR_CONFIG"] || ENV["CONFIG"] || CONFIG | 
					
						
							|  |  |  |     @config = YAML.load_file(File.new(config_file)) | 
					
						
							| 
									
										
										
										
											2014-11-27 18:45:35 +03:00
										 |  |  |     load_fixtures() | 
					
						
							| 
									
										
										
										
											2014-07-14 13:25:10 +04:00
										 |  |  |     super(:config => @config) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  |   def configure! | 
					
						
							| 
									
										
										
										
											2014-07-14 13:25:10 +04:00
										 |  |  |     c = {} | 
					
						
							| 
									
										
										
										
											2015-02-19 13:26:14 +03:00
										 |  |  |     %w{host port username password path_prefix username_without_privileges password_without_privileges openstack ec2}.each do |key| | 
					
						
							| 
									
										
										
										
											2014-07-14 13:25:10 +04:00
										 |  |  |       c[key] = @config[key] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     File.open(TESTS_CONFIG, "w") {|f| f.write(c.to_yaml) } | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  |     self | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  |   def generate!(templates) | 
					
						
							|  |  |  |     templates.each do |input, output| | 
					
						
							| 
									
										
										
										
											2015-02-19 13:26:14 +03:00
										 |  |  |       puts "Input: #{input}" | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  |       if File.exists?(input) | 
					
						
							| 
									
										
										
										
											2015-02-19 13:26:14 +03:00
										 |  |  |         file_data = File.read(input) | 
					
						
							|  |  |  |         if file_data.nil? | 
					
						
							|  |  |  |           puts "Data of file '#{input}' is nil" | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           data = render(file_data) | 
					
						
							|  |  |  |           dir = File.dirname(output) | 
					
						
							|  |  |  |           FileUtils.mkdir_p(dir) unless File.exists?(dir) | 
					
						
							|  |  |  |           File.open(output, "w") {|f| f.write(data)} | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  |       else | 
					
						
							|  |  |  |         puts "WARN: file '#{input}' does not exist" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def clean!(feature_files) | 
					
						
							|  |  |  |     feature_files.each do |feature_file| | 
					
						
							|  |  |  |       if File.exists?(feature_file) | 
					
						
							|  |  |  |         FileUtils.rm(feature_file) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         puts "WARN: file '#{feature_file}' does not exist" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  |   def render(template) | 
					
						
							| 
									
										
										
										
											2015-02-25 13:57:50 +03:00
										 |  |  |     ERB.new(template).result(binding) | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-11-27 18:45:35 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def load_fixtures | 
					
						
							|  |  |  |     @fixtures = {} | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |     Dir["templates/fixtures/*.yml"].each do |fixture_path| | 
					
						
							|  |  |  |       fixture_name = File.basename(fixture_path, '.yml') | 
					
						
							|  |  |  |       @fixtures[fixture_name] = YAML.load_file(fixture_path) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 18:45:35 +03:00
										 |  |  |     @formatter = FixtureFormatter.new(@fixtures) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | templates = { | 
					
						
							| 
									
										
										
										
											2014-07-15 11:34:42 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #list | 
					
						
							| 
									
										
										
										
											2014-07-15 01:36:31 +04:00
										 |  |  |   "templates/api_v2/00_list/flavor.feature.erb" => "features/api_v2/00_list/flavor.feature", | 
					
						
							| 
									
										
										
										
											2015-07-15 19:21:23 +03:00
										 |  |  |   "templates/api_v2/00_list/stack_preset.feature.erb" => "features/api_v2/00_list/stack_preset.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 13:08:00 +04:00
										 |  |  |   "templates/api_v2/00_list/10_user.feature.erb" => "features/api_v2/00_list/10_user.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 11:34:42 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #create | 
					
						
							| 
									
										
										
										
											2014-07-15 01:36:31 +04:00
										 |  |  |   "templates/api_v2/10_create/00_filter.feature.erb" => "features/api_v2/10_create/00_filter.feature", | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/10_create/00_stack_template.feature.erb" => "features/api_v2/10_create/00_stack_template.feature", | 
					
						
							|  |  |  |   "templates/api_v2/10_create/00_key.feature.erb" => "features/api_v2/10_create/00_key.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 01:36:31 +04:00
										 |  |  |   "templates/api_v2/10_create/10_image.feature.erb" => "features/api_v2/10_create/10_image.feature", | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/10_create/50_stack.feature.erb" => "features/api_v2/10_create/50_stack.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 01:36:31 +04:00
										 |  |  |   "templates/api_v2/10_create/20_project.feature.erb" => "features/api_v2/10_create/20_project.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:15:42 +04:00
										 |  |  |   "templates/api_v2/10_create/30_script.feature.erb" => "features/api_v2/10_create/30_script.feature", | 
					
						
							| 
									
										
										
										
											2014-11-26 15:45:33 +03:00
										 |  |  |   "templates/api_v2/10_create/40_deploy_env.feature.erb" => "features/api_v2/10_create/40_deploy_env.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:57:21 +04:00
										 |  |  |   "templates/api_v2/10_create/00_user.feature.erb" => "features/api_v2/10_create/00_user.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:15:42 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #update | 
					
						
							| 
									
										
										
										
											2014-07-15 12:22:51 +04:00
										 |  |  |   "templates/api_v2/20_update/10_image.feature.erb" => "features/api_v2/20_update/10_image.feature", | 
					
						
							| 
									
										
										
										
											2014-07-16 13:32:59 +04:00
										 |  |  |   "templates/api_v2/20_update/00_user.feature.erb" => "features/api_v2/20_update/00_user.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:22:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   #delete | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/90_delete/00_stack_template.feature.erb" => "features/api_v2/90_delete/00_stack_template.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:49:41 +04:00
										 |  |  |   "templates/api_v2/90_delete/10_script.feature.erb" => "features/api_v2/90_delete/10_script.feature", | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/90_delete/10_stack.feature.erb" => "features/api_v2/90_delete/10_stack.feature", | 
					
						
							| 
									
										
										
										
											2014-11-26 15:45:33 +03:00
										 |  |  |   "templates/api_v2/90_delete/20_deploy_env.feature.erb" => "features/api_v2/90_delete/20_deploy_env.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 12:49:41 +04:00
										 |  |  |   "templates/api_v2/90_delete/80_project.feature.erb" => "features/api_v2/90_delete/80_project.feature", | 
					
						
							|  |  |  |   "templates/api_v2/90_delete/90_image.feature.erb" => "features/api_v2/90_delete/90_image.feature", | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/90_delete/90_user.feature.erb" => "features/api_v2/90_delete/90_user.feature", | 
					
						
							| 
									
										
										
										
											2014-07-15 13:26:24 +04:00
										 |  |  |   "templates/api_v2/90_delete/99_filter.feature.erb" => "features/api_v2/90_delete/99_filter.feature", | 
					
						
							| 
									
										
										
										
											2015-07-08 16:48:19 +03:00
										 |  |  |   "templates/api_v2/90_delete/99_key.feature.erb" => "features/api_v2/90_delete/99_key.feature" | 
					
						
							| 
									
										
										
										
											2014-07-15 12:15:42 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | generator = Generator.new.configure! | 
					
						
							|  |  |  | if ARGV.first != 'clean' | 
					
						
							|  |  |  |   generator.generate!(templates) | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   generator.clean!(templates.values) | 
					
						
							| 
									
										
										
										
											2014-07-14 12:51:34 +04:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-11-21 14:22:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |