Sha256: e271c45f742667b4b59bac7cb61aa79ab01f6b2eb4af95417586e28fcd6f7e64

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'yaml'

module Rundock
  module Builder
    class ScenarioBuilder < Base
      CommandArgNotFoundError = Class.new(StandardError)

      def initialize(options, scenario_file_data)
        super(options)
        @scenario_file = scenario_file_data
        @default_ssh_builder = DefaultSshBuilder.new(@options)
      end

      def build
        # parse default ssh file
        @options.merge!(@default_ssh_builder.build)

        # use host specified
        return build_scenario_with_cli if @options[:host]

        # use scenario file
        build_scenario_with_file
      end

      def build_task(tasks, backend, target_attributes)
        OperationBuilder.new(@options).build_task(tasks, backend, target_attributes)
      end

      private

      def build_scenario_with_cli
        raise CommandArgNotFoundError, %("--command or -c" option is not specified.) unless @options[:command]
        ope = OperationBuilder.new(@options)
        ope.build_cli
      end

      def build_scenario_with_file
        if @scenario_file

          type = [:main, :target_info, :tasks, :hooks]
          scenario_data = {}

          YAML.load_documents(@scenario_file).each_with_index do |data, idx|
            if idx == 0
              scenario_data[type[idx]] = data
            else
              scenario_data[type[idx]] = data.deep_symbolize_keys unless data.nil?
            end
          end
        end

        ope = OperationBuilder.new(@options)
        ope.build_first(
          scenario_data[:main],
          scenario_data[:target_info],
          scenario_data[:tasks],
          scenario_data[:hooks])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rundock-0.5.2 lib/rundock/builder/scenario_builder.rb
rundock-0.5.0 lib/rundock/builder/scenario_builder.rb