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
13 14 15 16 |
# File 'app/models/foreman_docker/docker.rb', line 13 def self.get_container(container) conn = container.compute_resource.docker_connection ::Docker::Container.get(container.uuid, {}, conn) end |
.model_name ⇒ Object
9 10 11 |
# File 'app/models/foreman_docker/docker.rb', line 9 def self.model_name ComputeResource.model_name end |
Instance Method Details
#api_version ⇒ Object
111 112 113 |
# File 'app/models/foreman_docker/docker.rb', line 111 def api_version ::Docker.version(docker_connection) end |
#authenticate! ⇒ Object
115 116 117 |
# File 'app/models/foreman_docker/docker.rb', line 115 def authenticate! ::Docker.authenticate!(credentials, docker_connection) end |
#available_images ⇒ Object
38 39 40 |
# File 'app/models/foreman_docker/docker.rb', line 38 def available_images ::Docker::Image.all({}, docker_connection) end |
#capabilities ⇒ Object
18 19 20 |
# File 'app/models/foreman_docker/docker.rb', line 18 def capabilities [:image] end |
#console(uuid) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'app/models/foreman_docker/docker.rb', line 101 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
81 82 83 84 85 86 87 |
# File 'app/models/foreman_docker/docker.rb', line 81 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
89 90 91 92 93 94 |
# File 'app/models/foreman_docker/docker.rb', line 89 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
129 130 131 |
# File 'app/models/foreman_docker/docker.rb', line 129 def docker_connection @docker_connection ||= ::Docker::Connection.new(url, credentials) end |
#exist?(name) ⇒ Boolean
53 54 55 |
# File 'app/models/foreman_docker/docker.rb', line 53 def exist?(name) ::Docker::Image.exist?(name, {}, docker_connection) end |
#image(id) ⇒ Object
57 58 59 |
# File 'app/models/foreman_docker/docker.rb', line 57 def image(id) ::Docker::Image.get(id, {}, docker_connection) end |
#local_images(filter = '') ⇒ Object
42 43 44 |
# File 'app/models/foreman_docker/docker.rb', line 42 def local_images(filter = '') ::Docker::Image.all({ 'filter' => filter }, docker_connection) end |
#max_cpu_count ⇒ Object
34 35 36 |
# File 'app/models/foreman_docker/docker.rb', line 34 def max_cpu_count ::Docker.info['NCPU'] || 1 end |
#max_memory ⇒ Object
30 31 32 |
# File 'app/models/foreman_docker/docker.rb', line 30 def max_memory 16 * 1024 * 1024 * 1024 end |
#provided_attributes ⇒ Object
26 27 28 |
# File 'app/models/foreman_docker/docker.rb', line 26 def provided_attributes super.merge(:mac => :mac) end |
#provider_friendly_name ⇒ Object
77 78 79 |
# File 'app/models/foreman_docker/docker.rb', line 77 def provider_friendly_name 'Docker' end |
#search(term = '') ⇒ Object
73 74 75 |
# File 'app/models/foreman_docker/docker.rb', line 73 def search(term = '') client.images.image_search(:term => term) end |
#supports_update? ⇒ Boolean
22 23 24 |
# File 'app/models/foreman_docker/docker.rb', line 22 def supports_update? false end |
#tags(image_name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/foreman_docker/docker.rb', line 61 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
46 47 48 49 50 51 |
# File 'app/models/foreman_docker/docker.rb', line 46 def (image) image.info['RepoTags'].map do |image_tag| _, tag = image_tag.split(':') tag end end |
#test_connection(options = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'app/models/foreman_docker/docker.rb', line 119 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
96 97 98 99 |
# File 'app/models/foreman_docker/docker.rb', line 96 def vm_instance_defaults ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}", 'Cmd' => ['/bin/bash']) end |