Project

General

Profile

Revision b747db40

Added by Partha Aji over 6 years ago

Refs #8409 - Partially addressing pull before create container

Inital code to do a pull of an image before creating a container.
Note likely this part is going to change once we add foreman tasks but
this is the start point.

View differences:

app/models/foreman_docker/docker.rb
71 71
    def create_container(args = {})
72 72
      options = vm_instance_defaults.merge(args)
73 73
      logger.debug("Creating container with the following options: #{options.inspect}")
74
      ::Docker::Container.create(options)
75
    rescue Excon::Errors::Error, ::Docker::Error::DockerError => e
76
      logger.debug "Fog error: #{e.message}\n " + e.backtrace.join("\n ")
77
      errors.add(:base, _("Error creating container. Check the Foreman logs: %s") % e.message.to_s)
78
      false
74
      docker_command do
75
        ::Docker::Container.create(options)
76
      end
77
    end
78

  
79
    def create_image(args = {})
80
      logger.debug("Creating docker image with the following options: #{args.inspect}")
81
      docker_command do
82
        ::Docker::Image.create(args)
83
      end
79 84
    end
80 85

  
81 86
    def vm_instance_defaults
82 87
      ActiveSupport::HashWithIndifferentAccess.new('name' => "foreman_#{Time.now.to_i}",
83
                                                   'cmd' => ['/bin/bash'])
88
                                                   'Cmd' => ['/bin/bash'])
84 89
    end
85 90

  
86 91
    def console(uuid)
......
104 109

  
105 110
    protected
106 111

  
112
    def docker_command
113
      yield
114
    rescue Excon::Errors::Error, ::Docker::Error::DockerError => e
115
      logger.debug "Fog error: #{e.message}\n " + e.backtrace.join("\n ")
116
      errors.add(:base,
117
                 _("Error creating communicating with Docker. Check the Foreman logs: %s") %
118
                 e.message.to_s)
119
      false
120
    end
121

  
107 122
    def bootstrap(args)
108 123
      client.servers.bootstrap vm_instance_defaults.merge(args.to_hash)
109 124
    rescue Fog::Errors::Error => e
app/models/service/containers.rb
16 16
          container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
17 17
        end
18 18

  
19
        fail ActiveRecord::Rollback unless start_container(container)
19
        fail ActiveRecord::Rollback unless pull_image(container) && start_container(container)
20 20

  
21 21
        container.save!
22 22
        destroy_wizard_state(wizard_state)
......
24 24
      end
25 25
    end
26 26

  
27
    def self.pull_image(container)
28
      container.compute_resource.create_image(:fromImage => container.repository_pull_url)
29
    end
30

  
27 31
    def self.start_container(container)
28 32
      started = container.compute_resource.create_container(container.parametrize)
29 33
      container.uuid = started.id if started
test/units/containers_service_test.rb
13 13
  end
14 14

  
15 15
  test 'removes current state after successful container creation' do
16
    ret = OpenStruct.new(:id => 1)
17
    ForemanDocker::Docker.any_instance.expects(:create_image).returns(ret).with do |subject|
18
      subject.must_equal(:fromImage => "test:test")
19
    end
16 20
    ForemanDocker::Docker.any_instance.expects(:create_container)
17 21
                                      .returns(OpenStruct.new(:uuid => 1))
18 22
    Service::Containers.start_container!(@state)

Also available in: Unified diff