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
- #api_version ⇒ Object
- #authenticate! ⇒ Object
- #available_images ⇒ Object
- #capabilities ⇒ Object
- #console(uuid) ⇒ Object
- #create_container(args = {}) ⇒ Object
- #create_image(args = {}) ⇒ Object
- #docker_connection ⇒ Object
- #exist?(name) ⇒ Boolean
- #image(id) ⇒ Object
- #local_images(filter = '') ⇒ Object
- #max_cpu_count ⇒ 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
.get_container(container) ⇒ Object
11 12 13 14 |
# File 'app/models/foreman_docker/docker.rb', line 11 def self.get_container(container) conn = container.compute_resource.docker_connection ::Docker::Container.get(container.uuid, {}, conn) end |
.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
#api_version ⇒ Object
109 110 111 |
# File 'app/models/foreman_docker/docker.rb', line 109 def api_version ::Docker.version(docker_connection) end |
#authenticate! ⇒ Object
113 114 115 |
# File 'app/models/foreman_docker/docker.rb', line 113 def authenticate! ::Docker.authenticate!(credentials, docker_connection) end |
#available_images ⇒ Object
36 37 38 |
# File 'app/models/foreman_docker/docker.rb', line 36 def available_images ::Docker::Image.all({}, docker_connection) end |
#capabilities ⇒ Object
16 17 18 |
# File 'app/models/foreman_docker/docker.rb', line 16 def capabilities [:image] end |
#console(uuid) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'app/models/foreman_docker/docker.rb', line 99 def console(uuid) test_connection container = ::Docker::Container.get(uuid, {}, docker_connection) { :name => container.info['Name'], 'timestamp' => Time.now.utc, 'output' => container.logs(:stdout => true, :tail => 100) } end |
#create_container(args = {}) ⇒ Object
79 80 81 82 83 84 85 |
# File 'app/models/foreman_docker/docker.rb', line 79 def create_container(args = {}) = vm_instance_defaults.merge(args) logger.debug("Creating container with the following options: #{options.inspect}") docker_command do ::Docker::Container.create(, docker_connection) end end |
#create_image(args = {}) ⇒ Object
87 88 89 90 91 92 |
# File 'app/models/foreman_docker/docker.rb', line 87 def create_image(args = {}) logger.debug("Creating docker image with the following options: #{args.inspect}") docker_command do ::Docker::Image.create(args, credentials, docker_connection) end end |
#docker_connection ⇒ Object
127 128 129 |
# File 'app/models/foreman_docker/docker.rb', line 127 def docker_connection @docker_connection ||= ::Docker::Connection.new(url, credentials) end |
#exist?(name) ⇒ Boolean
51 52 53 |
# File 'app/models/foreman_docker/docker.rb', line 51 def exist?(name) ::Docker::Image.exist?(name, {}, docker_connection) end |
#image(id) ⇒ Object
55 56 57 |
# File 'app/models/foreman_docker/docker.rb', line 55 def image(id) ::Docker::Image.get(id, {}, docker_connection) end |
#local_images(filter = '') ⇒ Object
40 41 42 |
# File 'app/models/foreman_docker/docker.rb', line 40 def local_images(filter = '') ::Docker::Image.all({ 'filter' => filter }, docker_connection) end |
#max_cpu_count ⇒ Object
32 33 34 |
# File 'app/models/foreman_docker/docker.rb', line 32 def max_cpu_count ::Docker.info['NCPU'] || 1 end |
#max_memory ⇒ Object
28 29 30 |
# File 'app/models/foreman_docker/docker.rb', line 28 def max_memory 16 * 1024 * 1024 * 1024 end |
#provided_attributes ⇒ Object
24 25 26 |
# File 'app/models/foreman_docker/docker.rb', line 24 def provided_attributes super.merge(:mac => :mac) end |
#provider_friendly_name ⇒ Object
75 76 77 |
# File 'app/models/foreman_docker/docker.rb', line 75 def provider_friendly_name 'Docker' end |
#search(term = '') ⇒ Object
71 72 73 |
# File 'app/models/foreman_docker/docker.rb', line 71 def search(term = '') client.images.image_search(:term => term) end |
#supports_update? ⇒ Boolean
20 21 22 |
# File 'app/models/foreman_docker/docker.rb', line 20 def supports_update? false end |
#tags(image_name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/foreman_docker/docker.rb', line 59 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
44 45 46 47 48 49 |
# File 'app/models/foreman_docker/docker.rb', line 44 def (image) image.info['RepoTags'].map do |image_tag| _, tag = image_tag.split(':') tag end end |
#test_connection(options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'app/models/foreman_docker/docker.rb', line 117 def test_connection( = {}) super api_version credentials.empty? ? true : authenticate! # This should only rescue Fog::Errors, but Fog returns all kinds of errors... rescue => e errors[:base] << e. false end |
#vm_instance_defaults ⇒ Object
94 95 96 97 |
# File 'app/models/foreman_docker/docker.rb', line 94 def vm_instance_defaults ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}", 'Cmd' => ['/bin/bash']) end |