Project

General

Profile

Revision 29812b3a

Added by Dominic Cleal over 8 years ago

Update to pass CR class to plugin registration

View differences:

app/models/compute_resources/foreman/model/docker.rb
1
require 'uri'
2

  
3
module Foreman::Model
4
  class Docker < ::ComputeResource
5

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

  
8
    def self.model_name
9
      ComputeResource.model_name
10
    end
11

  
12
    def capabilities
13
      [:image]
14
    end
15

  
16
    def supports_update?
17
      false
18
    end
19

  
20
    def provided_attributes
21
      super.merge({:mac => :mac})
22
    end
23

  
24
    #FIXME
25
    def max_cpu_count
26
      8
27
    end
28

  
29
    def max_memory
30
      16*1024*1024*1024
31
    end
32

  
33
    def available_images
34
      client.images
35
    end
36

  
37
    def image(id)
38
      client.image.get(id) || raise(ActiveRecord::RecordNotFound)
39
    end
40

  
41
    def provider_friendly_name
42
      "Docker"
43
    end
44

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

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

  
60
    protected
61

  
62
    def bootstrap(args)
63
      client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
64
    rescue Fog::Errors::Error => e
65
      errors.add(:base, e.to_s)
66
      false
67
    end
68

  
69

  
70
    def client
71
      @client ||= ::Fog::Compute.new(
72
          :provider         => "fogdocker",
73
          :docker_username   => user,
74
          :docker_password   => password,
75
          :docker_url        => url
76
      )
77
    end
78

  
79
    def api_version
80
      @api_version ||= client.send(:client).api_version
81
    end
82

  
83
  end
84
end
app/models/foreman_docker/docker.rb
1
require 'uri'
2

  
3
module ForemanDocker
4
  class Docker < ::ComputeResource
5

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

  
8
    def self.model_name
9
      ComputeResource.model_name
10
    end
11

  
12
    def capabilities
13
      [:image]
14
    end
15

  
16
    def supports_update?
17
      false
18
    end
19

  
20
    def provided_attributes
21
      super.merge({:mac => :mac})
22
    end
23

  
24
    #FIXME
25
    def max_cpu_count
26
      8
27
    end
28

  
29
    def max_memory
30
      16*1024*1024*1024
31
    end
32

  
33
    def available_images
34
      client.images
35
    end
36

  
37
    def image(id)
38
      client.image.get(id) || raise(ActiveRecord::RecordNotFound)
39
    end
40

  
41
    def provider_friendly_name
42
      "Docker"
43
    end
44

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

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

  
60
    protected
61

  
62
    def bootstrap(args)
63
      client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
64
    rescue Fog::Errors::Error => e
65
      errors.add(:base, e.to_s)
66
      false
67
    end
68

  
69

  
70
    def client
71
      @client ||= ::Fog::Compute.new(
72
          :provider         => "fogdocker",
73
          :docker_username   => user,
74
          :docker_password   => password,
75
          :docker_url        => url
76
      )
77
    end
78

  
79
    def api_version
80
      @api_version ||= client.send(:client).api_version
81
    end
82

  
83
  end
84
end
lib/foreman_docker/engine.rb
16 16
    end
17 17

  
18 18
    initializer 'foreman_docker.register_plugin', :after=> :finisher_hook do |app|
19
      require File.expand_path('../../../app/models/compute_resources/foreman/model/docker', __FILE__)
20 19
      Foreman::Plugin.register :foreman_docker do
21 20
        requires_foreman '> 1.4'
22 21
        # Register docker compute resource in foreman
23
        compute_resource 'Docker'
22
        compute_resource ForemanDocker::Docker
24 23
      end
25 24

  
26 25
    end
......
34 33
  require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/image', __FILE__)
35 34
  Fog::Compute::Fogdocker::Server.send(:include, ::FogExtensions::Fogdocker::Server)
36 35
  Fog::Compute::Fogdocker::Image.send(:include, ::FogExtensions::Fogdocker::Image)
37
end
36
end

Also available in: Unified diff