Project

General

Profile

Revision 5397ee73

Added by David Davis over 7 years ago

Fixes #9382 - Use connection info when connecting to docker

View differences:

app/controllers/containers_controller.rb
16 16

  
17 17
  def new
18 18
    redirect_to wizard_state_step_path(:wizard_state_id => DockerContainerWizardState.create.id,
19
                                       :id              => :preliminary)
19
                                       :id => :preliminary)
20 20
  end
21 21

  
22 22
  def destroy
23 23
    if container_deletion
24 24
      process_success(:success_redirect => containers_path,
25
                      :success_msg      => (_("Container %s is being deleted.") %
25
                      :success_msg => (_("Container %s is being deleted.") %
26 26
                                            @deleted_identifier))
27 27
    else
28 28
      process_error(:redirect => containers_path)
......
35 35
  end
36 36

  
37 37
  def commit
38
    Docker::Container.get(@container.uuid).commit(:author  => params[:commit][:author],
39
                                                  :repo    => params[:commit][:repo],
40
                                                  :tag     => params[:commit][:tag],
41
                                                  :comment => params[:commit][:comment])
38
    ForemanDocker::Docker.get_container(@container).commit(:author  => params[:commit][:author],
39
                                                           :repo => params[:commit][:repo],
40
                                                           :tag => params[:commit][:tag],
41
                                                           :comment => params[:commit][:comment])
42 42

  
43 43
    process_success :success_redirect => :back,
44 44
                    :success_msg      => _("%{container} commit was successful") %
app/helpers/containers_helper.rb
91 91
      trunc(text, length)
92 92
    end
93 93
  end
94

  
95
  def processes(container)
96
    ForemanDocker::Docker.get_container(container).top
97
  end
98

  
99
  def logs(container, opts = {})
100
    ForemanDocker::Docker.get_container(container).log(opts)
101
  end
94 102
end
app/models/foreman_docker/docker.rb
8 8
      ComputeResource.model_name
9 9
    end
10 10

  
11
    def self.get_container(container)
12
      conn = container.compute_resource.docker_connection
13
      ::Docker::Container.get(container.uuid, {}, conn)
14
    end
15

  
11 16
    def capabilities
12 17
      [:image]
13 18
    end
......
29 34
    end
30 35

  
31 36
    def available_images
32
      client.images.all
37
      ::Docker::Image.all({}, docker_connection)
33 38
    end
34 39

  
35 40
    def local_images(filter = '')
36
      client # initialize Docker-Api
37
      ::Docker::Image.all('filter' => filter)
41
      ::Docker::Image.all({ 'filter' => filter }, docker_connection)
38 42
    end
39 43

  
40 44
    def tags_for_local_image(image)
......
45 49
    end
46 50

  
47 51
    def exist?(name)
48
      ::Docker::Image.exist?(name)
52
      ::Docker::Image.exist?(name, {}, docker_connection)
49 53
    end
50 54

  
51 55
    def image(id)
52
      client.image_get(id)
56
      ::Docker::Image.get(id, {}, docker_connection)
53 57
    end
54 58

  
55 59
    def tags(image_name)
......
65 69
    end
66 70

  
67 71
    def search(term = '')
68
      client.images.image_search(:term => term)
72
      ::Docker::Image.search({ :term => term }, docker_connection)
69 73
    end
70 74

  
71 75
    def provider_friendly_name
......
76 80
      options = vm_instance_defaults.merge(args)
77 81
      logger.debug("Creating container with the following options: #{options.inspect}")
78 82
      docker_command do
79
        ::Docker::Container.create(options)
83
        ::Docker::Container.create(options, docker_connection)
80 84
      end
81 85
    end
82 86

  
83 87
    def create_image(args = {})
84 88
      logger.debug("Creating docker image with the following options: #{args.inspect}")
85 89
      docker_command do
86
        ::Docker::Image.create(args)
90
        ::Docker::Image.create(args, credentials, docker_connection)
87 91
      end
88 92
    end
89 93

  
......
94 98

  
95 99
    def console(uuid)
96 100
      test_connection
97
      container = ::Docker::Container.get(uuid)
101
      container = ::Docker::Container.get(uuid, {}, docker_connection)
98 102
      {
99 103
        :name       => container.info['Name'],
100 104
        'timestamp' => Time.now.utc,
......
102 106
      }
103 107
    end
104 108

  
109
    def api_version
110
      ::Docker.version(docker_connection)
111
    end
112

  
113
    def authenticate!
114
      ::Docker.authenticate!(credentials, docker_connection)
115
    end
116

  
105 117
    def test_connection(options = {})
106 118
      super
107
      client.present?
119
      api_version
120
      credentials.empty? ? true : authenticate!
108 121
    # This should only rescue Fog::Errors, but Fog returns all kinds of errors...
109 122
    rescue => e
110 123
      errors[:base] << e.message
111 124
      false
112 125
    end
113 126

  
127
    def docker_connection
128
      @docker_connection ||= ::Docker::Connection.new(url, credentials)
129
    end
130

  
114 131
    protected
115 132

  
116 133
    def docker_command
......
141 158
      @client ||= ::Fog::Compute.new(opts)
142 159
    end
143 160

  
144
    def api_version
145
      @api_version ||= client.send(:client).api_version
161
    def credentials
162
      @credentials ||= {}.tap do |options|
163
        options[:username] = user if user.present?
164
        options[:password] = password if password.present?
165
        options[:email] = email if email.present?
166
      end
146 167
    end
147 168
  end
148 169
end
app/views/containers/show.html.erb
87 87
    </ul>
88 88
    <div id="myTabContent" class="tab-content">
89 89
      <div class="tab-pane active in" id="processes">
90
        <% processes = Docker::Container.get(@container.uuid).top %>
91 90
        <div class='accordion' id='accordion'>
92
          <% processes.each do |process| %>
91
          <% processes(@container).each do |process| %>
93 92
          <div>
94 93
            <h2>
95 94
              <a href='#<%= process['PID'] %>-index'
......
108 107
        </div>
109 108
      </div>
110 109
      <div class="tab-pane" id="logs">
111
        <pre class="terminal terminal-output pre-scrollable"><code><%= Docker::Container.get(@container.uuid).logs(:stdout => true, :tail => 100) %></code></pre>
110
        <pre class="terminal terminal-output pre-scrollable"><code>
111
          <%= logs(@container, :stdout => true, :tail => 100) %>
112
        </code></pre>
112 113
      </div>
113 114
    </div>
114 115
    <% else %>
test/functionals/container_controller_test.rb
1
require 'test_plugin_helper'
2

  
3
class ContainersControllerTest < ActionController::TestCase
4
  test 'redirect if Docker provider is not available' do
5
    get :index, {}, set_session_user
6
    assert_redirected_to new_compute_resource_path
7
  end
8

  
9
  test 'index if Docker resource is available' do
10
    Fog.mock!
11
    # Avoid rendering errors by not retrieving any container
12
    ComputeResource.any_instance.stubs(:vms).returns([])
13
    FactoryGirl.create(:docker_cr)
14
    get :index, {}, set_session_user
15
    assert_template 'index'
16
  end
17

  
18
  test 'deleting a container in compute resource redirects to containers index' do
19
    Fog.mock!
20
    container_resource = FactoryGirl.create(:docker_cr)
21
    container          = container_resource.vms.first
22
    container.class.any_instance.expects(:destroy).returns(true)
23
    delete :destroy, { :compute_resource_id => container_resource,
24
                       :id                  => container.id }, set_session_user
25
    assert_redirected_to containers_path
26
  end
27

  
28
  test 'committing a managed container' do
29
    container = FactoryGirl.create(:container)
30
    request.env['HTTP_REFERER'] = container_path(:id => container.id)
31
    commit_hash = { :author => 'a', :repo => 'b', :tag => 'c', :comment => 'd' }
32

  
33
    mock_container = mock
34
    ::Docker::Container.expects(:get).with(container.uuid).returns(mock_container)
35
    mock_container.expects(:commit).with(commit_hash)
36

  
37
    post :commit, { :commit => commit_hash,
38
                    :id     => container.id }, set_session_user
39
  end
40
end
test/functionals/containers_controller_test.rb
1
require 'test_plugin_helper'
2

  
3
class ContainersControllerTest < ActionController::TestCase
4
  test 'redirect if Docker provider is not available' do
5
    get :index, {}, set_session_user
6
    assert_redirected_to new_compute_resource_path
7
  end
8

  
9
  test 'index if Docker resource is available' do
10
    Fog.mock!
11
    # Avoid rendering errors by not retrieving any container
12
    ComputeResource.any_instance.stubs(:vms).returns([])
13
    FactoryGirl.create(:docker_cr)
14
    get :index, {}, set_session_user
15
    assert_template 'index'
16
  end
17

  
18
  test 'deleting a container in compute resource redirects to containers index' do
19
    Fog.mock!
20
    container_resource = FactoryGirl.create(:docker_cr)
21
    container          = container_resource.vms.first
22
    container.class.any_instance.expects(:destroy).returns(true)
23
    delete :destroy, { :compute_resource_id => container_resource,
24
                       :id                  => container.id }, set_session_user
25
    assert_redirected_to containers_path
26
  end
27

  
28
  test 'committing a managed container' do
29
    container = FactoryGirl.create(:container)
30
    request.env['HTTP_REFERER'] = container_path(:id => container.id)
31
    commit_hash = { :author => 'a', :repo => 'b', :tag => 'c', :comment => 'd' }
32

  
33
    mock_container = mock
34
    ::Docker::Container.expects(:get).with(container.uuid, anything, anything)
35
      .returns(mock_container)
36
    mock_container.expects(:commit).with(commit_hash)
37

  
38
    post :commit, { :commit => commit_hash,
39
                    :id     => container.id }, set_session_user
40
  end
41
end

Also available in: Unified diff