#!/usr/bin/env ruby # require "erb" require "yaml" require "ostruct" require "fileutils" require "./templates/fixtures/fixture_formatter" require "./templates/generators/path_scenarios_generator.rb" class Generator < OpenStruct CONFIG = "params.yml" TESTS_CONFIG = "features/support/config.yml" def initialize config_file = ENV["DEVOPS_FEATURES_GENERATOR_CONFIG"] || ENV["CONFIG"] || CONFIG @config = YAML.load_file(File.new(config_file)) load_fixtures() @generator = PathScenariosGenerator.new super(:config => @config, :formatter => @formatter, :fixtures => @fixtures, :generator => @generator) end def configure! c = {} %w{host port username password path_prefix username_without_privileges password_without_privileges openstack ec2}.each do |key| c[key] = @config[key] end File.open(TESTS_CONFIG, "w") {|f| f.write(c.to_yaml) } self end def generate!(templates) templates.each do |input, output| puts "Input: #{input}" if File.exists?(input) begin 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 rescue => e puts "\tError: #{e.message}" end else puts "WARN: file '#{input}' does not exist" end end end def self.clean! dir = "features/api_v2/" if File.exists?(dir) puts "Removing directory '#{dir}'" FileUtils.rm_r(dir) end =begin feature_files.each do |feature_file| if File.exists?(feature_file) FileUtils.rm_f(feature_file) else puts "WARN: file '#{feature_file}' does not exist" end end =end end private def render(template) ERB.new(template).result(binding) end def load_fixtures puts "Load fixures:" @fixtures = {} Dir["templates/fixtures/*.yml"].each do |fixture_path| fixture_name = File.basename(fixture_path, '.yml') print "\t#{fixture_path}..." @fixtures[fixture_name] = YAML.load_file(fixture_path) puts " ok" end @formatter = FixtureFormatter.new(@fixtures) end end templates = { #list "templates/api_v2/00_list/flavor.feature.erb" => "features/api_v2/00_list/flavor.feature", "templates/api_v2/00_list/00_network.feature.erb" => "features/api_v2/00_list/00_network.feature", "templates/api_v2/00_list/10_user.feature.erb" => "features/api_v2/00_list/10_user.feature", "templates/api_v2/00_list/10_group.feature.erb" => "features/api_v2/00_list/10_group.feature", "templates/api_v2/00_list/10_bootstrap_template.feature.erb" => "features/api_v2/00_list/10_bootstrap_template.feature", "templates/api_v2/00_list/10_filter.feature.erb" => "features/api_v2/00_list/10_filter.feature", "templates/api_v2/00_list/20_image.feature.erb" => "features/api_v2/00_list/20_image.feature", "templates/api_v2/00_list/20_key.feature.erb" => "features/api_v2/00_list/20_key.feature", "templates/api_v2/00_list/30_project.feature.erb" => "features/api_v2/00_list/30_project.feature", #create "templates/api_v2/10_create/00_filter.feature.erb" => "features/api_v2/10_create/00_filter.feature", "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", "templates/api_v2/10_create/10_image.feature.erb" => "features/api_v2/10_create/10_image.feature", "templates/api_v2/10_create/50_stack.feature.erb" => "features/api_v2/10_create/50_stack.feature", "templates/api_v2/10_create/20_project.feature.erb" => "features/api_v2/10_create/20_project.feature", "templates/api_v2/10_create/21_deploy_env.feature.erb" => "features/api_v2/10_create/21_deploy_env.feature", "templates/api_v2/10_create/30_script.feature.erb" => "features/api_v2/10_create/30_script.feature", "templates/api_v2/10_create/40_deploy_env.feature.erb" => "features/api_v2/10_create/40_deploy_env.feature", "templates/api_v2/10_create/00_user.feature.erb" => "features/api_v2/10_create/00_user.feature", #update "templates/api_v2/20_update/10_image.feature.erb" => "features/api_v2/20_update/10_image.feature", "templates/api_v2/20_update/00_user.feature.erb" => "features/api_v2/20_update/00_user.feature", #delete "templates/api_v2/90_delete/00_stack_template.feature.erb" => "features/api_v2/90_delete/00_stack_template.feature", "templates/api_v2/90_delete/10_script.feature.erb" => "features/api_v2/90_delete/10_script.feature", "templates/api_v2/90_delete/10_stack.feature.erb" => "features/api_v2/90_delete/10_stack.feature", "templates/api_v2/90_delete/20_deploy_env.feature.erb" => "features/api_v2/90_delete/20_deploy_env.feature", "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", "templates/api_v2/90_delete/90_user.feature.erb" => "features/api_v2/90_delete/90_user.feature", "templates/api_v2/90_delete/99_filter.feature.erb" => "features/api_v2/90_delete/99_filter.feature", "templates/api_v2/90_delete/99_key.feature.erb" => "features/api_v2/90_delete/99_key.feature" } if ARGV.first != 'clean' generator = Generator.new.configure! generator.generate!(templates) else Generator.clean! end