Sha256: f88fd420651c3e918dd3f236ab06df13b0569723627f1db6bfa385d534aa44e3

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

require_relative 'service'
require_relative 'services/postgresql'
require_relative 'services/redis'
require_relative 'services/mysql'

module VagrantPlugins
  module Ventriloquist
    class ServicesBuilder
      MAPPING = {
        'pg'    => Services::PostgreSQL,
        'mysql' => Services::MySql,
        'redis' => Services::Redis
      }

      def initialize(services, mapping = MAPPING)
        @services = services.flatten
        @mapping  = mapping
      end

      def self.build(services, docker_client)
        new(services).build(docker_client)
      end

      def build(docker_client)
        @services.each_with_object([]) do |cfg, built_services|
          case cfg
            when Hash
              built_services.concat build_services(cfg, docker_client)
            when String, Symbol
              built_services << create_service_provisioner(cfg, {}, docker_client)
            else
              raise "Unknown cfg type: #{cfg.class}"
          end
        end
      end

      private

      def build_services(cfg_hash, docker_client)
        cfg_hash.map do |name, config|
          create_service_provisioner(name, config, docker_client)
        end
      end

      def create_service_provisioner(name, config, docker_client)
        name, tag = name.to_s.split(':')

        # REFACTOR: This is a bit confusing...
        config[:tag]   ||= (tag || 'latest')
        config[:image] ||= extract_image_name(name)
        config[:image] << ":#{config[:tag]}"

        klass = @mapping.fetch(name, Service)
        klass.new(name, config, docker_client)
      end

      def extract_image_name(name)
        if name =~ /(\w+\/\w+)/
          $1
        else
          "fgrehm/ventriloquist-#{name}"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ventriloquist-0.3.2 lib/ventriloquist/services_builder.rb
ventriloquist-0.3.1 lib/ventriloquist/services_builder.rb
ventriloquist-0.3.0 lib/ventriloquist/services_builder.rb
ventriloquist-0.2.1 lib/ventriloquist/services_builder.rb
ventriloquist-0.2.0 lib/ventriloquist/services_builder.rb
ventriloquist-0.1.0 lib/ventriloquist/services_builder.rb