Sha256: 872370a996c6743b4f051ffca880348582331ca584803e160c193bdcdadd55d6

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'uri'

module ForemanDocker
  class Docker < ::ComputeResource

    validates :url, :format => { :with => URI.regexp }

    def self.model_name
      ComputeResource.model_name
    end

    def capabilities
      [:image]
    end

    def supports_update?
      false
    end

    def provided_attributes
      super.merge({:mac => :mac})
    end

    #FIXME
    def max_cpu_count
      8
    end

    def max_memory
      16*1024*1024*1024
    end

    def available_images
      client.images
    end

    def image(id)
      client.image.get(id) || raise(ActiveRecord::RecordNotFound)
    end

    def provider_friendly_name
      "Docker"
    end

    def create_vm args = {}
      args['cmd'] = Array.wrap( args.delete('cmd') )
      options = vm_instance_defaults.merge(args)
      logger.debug("creating Docker with the following options: #{options.inspect}")
      client.servers.create options
    rescue Fog::Errors::Error => e
      logger.debug "Fog error: #{e.message}\n " + e.backtrace.join("\n ")
      errors.add(:base, e.message.to_s)
      false
    end

    def vm_instance_defaults
      ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}", 'cmd' => ['/bin/bash'])
    end

    protected

    def bootstrap(args)
      client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
    rescue Fog::Errors::Error => e
      errors.add(:base, e.to_s)
      false
    end


    def client
      @client ||= ::Fog::Compute.new(
          :provider         => "fogdocker",
          :docker_username   => user,
          :docker_password   => password,
          :docker_url        => url
      )
    end

    def api_version
      @api_version ||= client.send(:client).api_version
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_docker-0.0.3 app/models/foreman_docker/docker.rb
foreman_docker-0.0.2 app/models/foreman_docker/docker.rb