lib/itamae-mitsurin/mitsurin/serverspec_task.rb in itamae-mitsurin-0.50 vs lib/itamae-mitsurin/mitsurin/serverspec_task.rb in itamae-mitsurin-1.0.0

- old
+ new

@@ -1,106 +1,81 @@ -require 'itamae-mitsurin/mitsurin/task_base' -include Rake::DSL if defined? Rake::DSL +require 'itamae-mitsurin/mitsurin/base_task' -module Itamae +module ItamaeMitsurin module Mitsurin - class ServerspecTask + class ServerspecTask < BaseTask + LoadSpecError = Class.new(StandardError) - TaskBase = ItamaeMitsurin::Mitsurin::TaskBase + def list_recipe_filepath(run_list) + recipes = [] + run_list.each do |recipe| + target_list = Dir.glob("site-cookbooks/**/#{recipe.keys.join}/spec/#{recipe.values.join}_spec.rb") + raise LoadSpecError, "#{recipe.to_a.join('::')} cookbook or spec does not exist." if target_list.empty? + + target_list.each do |target| + recipes << " #{target}" + end + end + + recipes + end + + ItamaeMitsurin.logger.formatter.colored = true + task = ServerspecTask.new + namespace :spec do all = [] - Dir.glob("tmp-nodes/**/*.json").each do |node_file| - file_name = File.basename(node_file, '.json') + Dir.glob('nodes/**/*.json').each do |node_file| begin - node_attr = JSON.parse(File.read(node_file), symbolize_names: true) - rescue JSON::ParserError => e - puts e.class.to_s + ", " + e.backtrace[0].to_s - puts "Node error, nodefile:#{node_file}, reason:#{e.message}" + node_name = File.basename(node_file, '.json') + node = task.load_node_attributes(node_file) + node_short = node[:environments][:hostname].split('.')[0] + rescue => e + ItamaeMitsurin.logger.error e.inspect + ItamaeMitsurin.logger.info "From node file: #{node_file}" + exit 2 end - node_short = node_attr[:environments][:hostname].split(".")[0] all << node_short - desc "Serverspec to all nodes" - task :all => all + desc 'Serverspec to all nodes' + task 'all' => all - desc "Spec to #{file_name}" - task node_attr[:environments][:hostname].split(".")[0] do - + desc "Serverspec to #{node_name}" + task node_short do begin - recipes = [] - TaskBase.get_roles(node_file).each do |role| - recipes << TaskBase.get_recipes(role) - end - TaskBase.get_node_recipes(node_file).each do |recipe| - recipes << recipe - end - rescue Exception => e - puts e.class.to_s + ", " + e.backtrace[0].to_s - puts "Node or role error, nodefile:#{node_file} reason:#{e.message}" - exit 1 - else - recipes << {'_base' => 'default'} - recipes.flatten! - end + run_list = task.load_run_list(node_file) + environments = task.load_environments(node) + recipe_attributes_list = task.load_recipe_attributes(run_list) - node_name = node_attr[:environments][:hostname] - ssh_user = node_attr[:environments][:ssh_user] - ssh_password = node_attr[:environments][:ssh_password] - sudo_password = node_attr[:environments][:sudo_password] - ssh_port = node_attr[:environments][:ssh_port] - ssh_key = node_attr[:environments][:ssh_key] + merged_recipe = task.merge_attributes(recipe_attributes_list) + merged_environments = task.merge_attributes(merged_recipe, environments) + attributes = task.merge_attributes(merged_environments, node) + task.create_tmp_nodes(node_name, attributes) - desc "Run spec to #{file_name}" - ENV['TARGET_HOST'] = node_name - ENV['NODE_FILE'] = node_file - ENV['SSH_PASSWORD'] = ssh_password - ENV['SUDO_PASSWORD'] = sudo_password - ENV['SSH_KEY'] = "keys/#{ssh_key}" - ENV['SSH_PORT'] = ssh_port - ENV['SSH_USER'] = ssh_user + command = task.create_spec_command(node_name, attributes) + command_recipe = task.list_recipe_filepath(run_list) + command_recipe.sort_by! {|item| File.dirname(item) } + command << command_recipe.join - specs = "bundle exec rspec" - - # Pass to read the spec command - command_recipe = [] - recipes.each do |recipe_h| - target_recipe = "site-cookbooks/**/#{recipe_h.keys.join}/spec/#{recipe_h[recipe_h.keys.join]}_spec.rb" - if Dir.glob(target_recipe).empty? - raise "Spec load error, nodefile: #{node_file}, reason: Does not exist " + - recipe_h.keys.join + '::' + recipe_h.values.join - end - Dir.glob(target_recipe).join("\s").split.each do |target| - unless File.exists?(target) - ex_recipe = recipe_h.to_s.gsub('=>', '::').gsub('"', '') - raise "Spec load error, nodefile: #{node_file}, reason: Does not exist #{ex_recipe}" + task.runner_display(attributes[:run_list], run_list, command) + st = system command + if st + ItamaeMitsurin.logger.color(:green) do + ItamaeMitsurin.logger.info 'serverspec_task is completed.' end - command_recipe << " #{target}" - end - end - - command_recipe.sort_by! {|item| File.dirname(item)} - command << command_recipe.join - - puts TaskBase.hl.color(%!Run Serverspec to "#{bname}"!, :red) - run_list_noti = [] - command_recipe.each { |c_recipe| - unless c_recipe.split('/')[4].split('.')[0] == 'default_spec' - subspec = c_recipe.split('/')[4].split('.')[0].split('_')[0..-2].join('_') - run_list_noti << c_recipe.split('/')[2] + "::#{subspec}" else - run_list_noti << c_recipe.split('/')[2] + ItamaeMitsurin.logger.error 'serverspec_task is failed.' + exit 1 end - } - - puts TaskBase.hl.color(%!Run List to \"#{run_list_noti.uniq.join(", ")}\"!, :green) - puts TaskBase.hl.color(%!#{command}!, :white) - st = system command - exit 1 unless st + rescue => e + ItamaeMitsurin.logger.error e.inspect + ItamaeMitsurin.logger.info "From node file: #{node_file}" + exit 2 + end end end end - end end end