Class: ForemanDocker::Docker
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanDocker::Docker
- Defined in:
- app/models/foreman_docker/docker.rb
Class Method Summary collapse
Instance Method Summary collapse
- #available_images ⇒ Object
- #capabilities ⇒ Object
- #console(uuid) ⇒ Object
- #create_container(args = {}) ⇒ Object
- #exist?(name) ⇒ Boolean
- #image(id) ⇒ Object
- #local_images(filter = '') ⇒ Object
- #max_memory ⇒ Object
- #provided_attributes ⇒ Object
- #provider_friendly_name ⇒ Object
- #search(term = '') ⇒ Object
- #supports_update? ⇒ Boolean
- #tags(image_name) ⇒ Object
- #tags_for_local_image(image) ⇒ Object
- #test_connection(options = {}) ⇒ Object
- #vm_instance_defaults ⇒ Object
Class Method Details
.model_name ⇒ Object
7 8 9 |
# File 'app/models/foreman_docker/docker.rb', line 7 def self.model_name ComputeResource.model_name end |
Instance Method Details
#available_images ⇒ Object
27 28 29 |
# File 'app/models/foreman_docker/docker.rb', line 27 def available_images client.images.all end |
#capabilities ⇒ Object
11 12 13 |
# File 'app/models/foreman_docker/docker.rb', line 11 def capabilities [:image] end |
#console(uuid) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'app/models/foreman_docker/docker.rb', line 86 def console(uuid) test_connection container = ::Docker::Container.get(uuid) { :name => container.info['Name'], 'timestamp' => Time.now.utc, 'output' => container.logs(:stdout => true, :tail => 100) } end |
#create_container(args = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'app/models/foreman_docker/docker.rb', line 71 def create_container(args = {}) = vm_instance_defaults.merge(args) logger.debug("Creating container with the following options: #{.inspect}") ::Docker::Container.create() rescue Excon::Errors::Error, ::Docker::Error::DockerError => e logger.debug "Fog error: #{e.}\n " + e.backtrace.join("\n ") errors.add(:base, _("Error creating container. Check the Foreman logs: %s") % e..to_s) false end |
#exist?(name) ⇒ Boolean
43 44 45 |
# File 'app/models/foreman_docker/docker.rb', line 43 def exist?(name) ::Docker::Image.exist?(name) end |
#image(id) ⇒ Object
47 48 49 |
# File 'app/models/foreman_docker/docker.rb', line 47 def image(id) client.image_get(id) end |
#local_images(filter = '') ⇒ Object
31 32 33 34 |
# File 'app/models/foreman_docker/docker.rb', line 31 def local_images(filter = '') client # initialize Docker-Api ::Docker::Image.all('filter' => filter) end |
#max_memory ⇒ Object
23 24 25 |
# File 'app/models/foreman_docker/docker.rb', line 23 def max_memory 16 * 1024 * 1024 * 1024 end |
#provided_attributes ⇒ Object
19 20 21 |
# File 'app/models/foreman_docker/docker.rb', line 19 def provided_attributes super.merge(:mac => :mac) end |
#provider_friendly_name ⇒ Object
67 68 69 |
# File 'app/models/foreman_docker/docker.rb', line 67 def provider_friendly_name 'Docker' end |
#search(term = '') ⇒ Object
63 64 65 |
# File 'app/models/foreman_docker/docker.rb', line 63 def search(term = '') client.images.image_search(:term => term) end |
#supports_update? ⇒ Boolean
15 16 17 |
# File 'app/models/foreman_docker/docker.rb', line 15 def supports_update? false end |
#tags(image_name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/foreman_docker/docker.rb', line 51 def (image_name) if exist?(image_name) (local_images(image_name).first) else # If image is not found in the compute resource, get the tags from the Hub hub_api_url = "https://index.docker.io/v1/repositories/#{image_name}/tags" JSON.parse(URI.parse(hub_api_url).read).map do |tag| tag['name'] end end end |
#tags_for_local_image(image) ⇒ Object
36 37 38 39 40 41 |
# File 'app/models/foreman_docker/docker.rb', line 36 def (image) image.info['RepoTags'].map do |image_tag| _, tag = image_tag.split(':') tag end end |
#test_connection(options = {}) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'app/models/foreman_docker/docker.rb', line 96 def test_connection( = {}) super client.present? # This should only rescue Fog::Errors, but Fog returns all kinds of errors... rescue => e errors[:base] << e. false end |
#vm_instance_defaults ⇒ Object
81 82 83 84 |
# File 'app/models/foreman_docker/docker.rb', line 81 def vm_instance_defaults ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}", 'cmd' => ['/bin/bash']) end |