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"
|
2015-10-06 13:50:26 +03:00
|
|
|
require "./templates/generators/path_scenarios_generator.rb"
|
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
|
|
|
|
2018-04-04 22:44:39 +03:00
|
|
|
include PathScenariosGenerator
|
|
|
|
|
|
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()
|
2018-04-04 22:44:39 +03:00
|
|
|
super(:config => @config, :formatter => @formatter, :fixtures => @fixtures)
|
2014-07-14 13:25:10 +04:00
|
|
|
end
|
|
|
|
|
|
2014-11-21 14:22:46 +03:00
|
|
|
def configure!
|
2014-07-14 13:25:10 +04:00
|
|
|
c = {}
|
2018-04-04 22:44:39 +03:00
|
|
|
%w{host port username password path_prefix username_without_privileges password_without_privileges}.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-10-06 13:50:26 +03:00
|
|
|
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}"
|
2015-02-19 13:26:14 +03:00
|
|
|
end
|
2014-11-21 14:22:46 +03:00
|
|
|
else
|
|
|
|
|
puts "WARN: file '#{input}' does not exist"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-04 22:44:39 +03:00
|
|
|
|
2015-10-06 13:50:26 +03:00
|
|
|
def self.clean!
|
2018-04-04 22:44:39 +03:00
|
|
|
dir = "features/api_v3/"
|
2015-10-06 13:50:26 +03:00
|
|
|
if File.exists?(dir)
|
|
|
|
|
puts "Removing directory '#{dir}'"
|
|
|
|
|
FileUtils.rm_r(dir)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
=begin
|
2014-11-21 14:22:46 +03:00
|
|
|
feature_files.each do |feature_file|
|
|
|
|
|
if File.exists?(feature_file)
|
2015-10-06 13:50:26 +03:00
|
|
|
FileUtils.rm_f(feature_file)
|
2014-11-21 14:22:46 +03:00
|
|
|
else
|
|
|
|
|
puts "WARN: file '#{feature_file}' does not exist"
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-06 13:50:26 +03:00
|
|
|
=end
|
2014-11-21 14:22:46 +03:00
|
|
|
end
|
|
|
|
|
|
2018-04-04 22:44:39 +03:00
|
|
|
def concat(string)
|
|
|
|
|
@output.concat(string)
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-21 14:22:46 +03:00
|
|
|
private
|
|
|
|
|
|
2014-07-14 12:51:34 +04:00
|
|
|
def render(template)
|
2018-04-04 22:44:39 +03:00
|
|
|
@output = ""
|
|
|
|
|
ERB.new(template, 0, "%<>", "@output").result(binding)
|
2014-07-14 12:51:34 +04:00
|
|
|
end
|
2014-11-27 18:45:35 +03:00
|
|
|
|
|
|
|
|
def load_fixtures
|
2015-10-06 13:50:26 +03:00
|
|
|
puts "Load fixures:"
|
2014-11-27 18:45:35 +03:00
|
|
|
@fixtures = {}
|
2015-07-08 16:48:19 +03:00
|
|
|
Dir["templates/fixtures/*.yml"].each do |fixture_path|
|
|
|
|
|
fixture_name = File.basename(fixture_path, '.yml')
|
2015-10-06 13:50:26 +03:00
|
|
|
print "\t#{fixture_path}..."
|
2015-07-08 16:48:19 +03:00
|
|
|
@fixtures[fixture_name] = YAML.load_file(fixture_path)
|
2015-10-06 13:50:26 +03:00
|
|
|
puts " ok"
|
2015-07-08 16:48:19 +03:00
|
|
|
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
|
|
|
|
2018-04-04 22:44:39 +03:00
|
|
|
#prepare
|
|
|
|
|
"templates/api_v3/00_prepare/00_create_role.feature.erb" => "features/api_v3/00_prepare/00_create_role.feature",
|
|
|
|
|
"templates/api_v3/00_prepare/10_create_user.feature.erb" => "features/api_v3/00_prepare/10_create_user.feature",
|
2014-07-15 11:34:42 +04:00
|
|
|
#list
|
2018-04-04 22:44:39 +03:00
|
|
|
"templates/api_v3/01_list/00_chef.feature.erb" => "features/api_v3/01_list/00_chef.feature",
|
|
|
|
|
"templates/api_v3/01_list/10_filter.feature.erb" => "features/api_v3/01_list/10_filter.feature",
|
|
|
|
|
"templates/api_v3/01_list/10_key.feature.erb" => "features/api_v3/01_list/10_key.feature",
|
|
|
|
|
"templates/api_v3/01_list/11_provider.feature.erb" => "features/api_v3/01_list/11_provider.feature",
|
|
|
|
|
"templates/api_v3/01_list/10_user.feature.erb" => "features/api_v3/01_list/10_user.feature",
|
|
|
|
|
"templates/api_v3/01_list/20_image.feature.erb" => "features/api_v3/01_list/20_image.feature",
|
|
|
|
|
"templates/api_v3/01_list/30_project.feature.erb" => "features/api_v3/01_list/30_project.feature",
|
2014-07-15 11:34:42 +04:00
|
|
|
|
|
|
|
|
#create
|
2018-04-04 22:44:39 +03:00
|
|
|
"templates/api_v3/10_create/00_filter.feature.erb" => "features/api_v3/10_create/00_filter.feature",
|
|
|
|
|
"templates/api_v3/10_create/00_key.feature.erb" => "features/api_v3/10_create/00_key.feature",
|
|
|
|
|
"templates/api_v3/10_create/01_provider_account.feature.erb" => "features/api_v3/10_create/01_provider_account.feature",
|
|
|
|
|
"templates/api_v3/10_create/10_image.feature.erb" => "features/api_v3/10_create/10_image.feature",
|
|
|
|
|
"templates/api_v3/10_create/20_project.feature.erb" => "features/api_v3/10_create/20_project.feature",
|
|
|
|
|
"templates/api_v3/10_create/21_deploy_env.feature.erb" => "features/api_v3/10_create/21_deploy_env.feature",
|
|
|
|
|
"templates/api_v3/10_create/40_server.feature.erb" => "features/api_v3/10_create/40_server.feature",
|
|
|
|
|
=begin
|
|
|
|
|
"templates/api_v3/10_create/00_stack_template.feature.erb" => "features/api_v3/10_create/00_stack_template.feature",
|
|
|
|
|
"templates/api_v3/10_create/50_stack.feature.erb" => "features/api_v3/10_create/50_stack.feature",
|
|
|
|
|
"templates/api_v3/10_create/30_script.feature.erb" => "features/api_v3/10_create/30_script.feature",
|
|
|
|
|
"templates/api_v3/10_create/40_deploy_env.feature.erb" => "features/api_v3/10_create/40_deploy_env.feature",
|
|
|
|
|
"templates/api_v3/10_create/00_user.feature.erb" => "features/api_v3/10_create/00_user.feature",
|
|
|
|
|
=end
|
2014-07-15 12:15:42 +04:00
|
|
|
|
|
|
|
|
#update
|
2018-04-04 22:44:39 +03:00
|
|
|
"templates/api_v3/20_update/10_image.feature.erb" => "features/api_v3/20_update/10_image.feature",
|
|
|
|
|
"templates/api_v3/20_update/20_project.feature.erb" => "features/api_v3/20_update/20_project.feature",
|
|
|
|
|
=begin
|
|
|
|
|
"templates/api_v3/20_update/00_user.feature.erb" => "features/api_v3/20_update/00_user.feature",
|
|
|
|
|
=end
|
2014-07-15 12:22:51 +04:00
|
|
|
|
|
|
|
|
#delete
|
2018-04-04 22:44:39 +03:00
|
|
|
=begin
|
|
|
|
|
"templates/api_v3/90_delete/00_stack_template.feature.erb" => "features/api_v3/90_delete/00_stack_template.feature",
|
|
|
|
|
"templates/api_v3/90_delete/10_script.feature.erb" => "features/api_v3/90_delete/10_script.feature",
|
|
|
|
|
"templates/api_v3/90_delete/10_stack.feature.erb" => "features/api_v3/90_delete/10_stack.feature",
|
|
|
|
|
"templates/api_v3/90_delete/20_deploy_env.feature.erb" => "features/api_v3/90_delete/20_deploy_env.feature",
|
|
|
|
|
=end
|
|
|
|
|
"templates/api_v3/90_delete/60_server.feature.erb" => "features/api_v3/90_delete/60_server.feature",
|
|
|
|
|
"templates/api_v3/90_delete/80_project.feature.erb" => "features/api_v3/90_delete/80_project.feature",
|
|
|
|
|
"templates/api_v3/90_delete/90_image.feature.erb" => "features/api_v3/90_delete/90_image.feature",
|
|
|
|
|
"templates/api_v3/90_delete/98_key.feature.erb" => "features/api_v3/90_delete/98_key.feature",
|
|
|
|
|
"templates/api_v3/90_delete/98_filter.feature.erb" => "features/api_v3/90_delete/98_filter.feature",
|
|
|
|
|
"templates/api_v3/90_delete/99_finalize.feature.erb" => "features/api_v3/90_delete/99_finalize.feature"
|
2014-07-14 12:51:34 +04:00
|
|
|
}
|
2014-11-21 14:22:46 +03:00
|
|
|
|
|
|
|
|
if ARGV.first != 'clean'
|
2015-10-06 13:50:26 +03:00
|
|
|
generator = Generator.new.configure!
|
2014-11-21 14:22:46 +03:00
|
|
|
generator.generate!(templates)
|
|
|
|
|
else
|
2015-10-06 13:50:26 +03:00
|
|
|
Generator.clean!
|
2014-07-14 12:51:34 +04:00
|
|
|
end
|
2014-11-21 14:22:46 +03:00
|
|
|
|