Project

General

Profile

Download (3.15 KB) Statistics
| Branch: | Tag: | Revision:

foreman-docker / app / models / service / containers.rb @ c69bcf98

1 6d101d7a Dmitri Dolguikh
module Service
2
  class Containers
3 63ae5d99 Dmitri Dolguikh
    def errors
4
      @errors ||= []
5
    end
6
7
    def start_container!(wizard_state)
8 6d101d7a Dmitri Dolguikh
      ActiveRecord::Base.transaction do
9 d5d9d39e Shlomi Zadok
        container = create_container_object(wizard_state)
10
        container.save!
11
        run_container(container)
12
        destroy_wizard_state(wizard_state)
13
        container
14
      end
15
    end
16 8e2848d5 Vanya Jauhal
17 d5d9d39e Shlomi Zadok
    def create_container!(wizard_state)
18
      ActiveRecord::Base.transaction do
19
        container = create_container_object(wizard_state)
20
        container.save!
21
        destroy_wizard_state(wizard_state)
22
        container
23
      end
24
    end
25 8e2848d5 Vanya Jauhal
26 d5d9d39e Shlomi Zadok
    def create_container_object(wizard_state)
27 cc6d0d73 David Davis
      container = Container.new do |r|
28
        r.attributes = wizard_state.container_attributes
29 d5d9d39e Shlomi Zadok
        # eagerly load environment variables and exposed ports configuration
30
        state = DockerContainerWizardState.includes(
31 7ff105d1 David Davis
          :environment => [:environment_variables, :exposed_ports]).find(wizard_state.id)
32 6d101d7a Dmitri Dolguikh
33 d5d9d39e Shlomi Zadok
        load_environment_variables(state, r)
34
        load_exposed_ports(state, r)
35
        load_dns(state, r)
36
      end
37 2a0cf95b David Davis
38 d5d9d39e Shlomi Zadok
      Taxonomy.enabled_taxonomies.each do |taxonomy|
39
        container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
40
      end
41 6d101d7a Dmitri Dolguikh
42 ef3feba4 Daniel Lobato
      pull_image(container)
43
      start_container(container)
44 97cb04b2 Daniel Lobato García
      unless container.valid?
45
        @errors = errors + container.errors.full_messages
46
      end
47 d5d9d39e Shlomi Zadok
48 97cb04b2 Daniel Lobato García
      if @errors.present?
49
        @errors = @errors.flatten.uniq
50
        fail ActiveRecord::Rollback
51
      end
52 d5d9d39e Shlomi Zadok
53 f9547788 Adam Ruzicka
      container.name = container.in_fog.name[1..-1] unless container.name.present?
54
55 d5d9d39e Shlomi Zadok
      container
56 6d101d7a Dmitri Dolguikh
    end
57
58 63ae5d99 Dmitri Dolguikh
    def pull_image(container)
59 ef3feba4 Daniel Lobato
      success = container.compute_resource.
60
        create_image(:fromImage => container.repository_pull_url)
61 97cb04b2 Daniel Lobato García
      return true if success
62
      @errors = errors + container.compute_resource.errors.full_messages
63 b747db40 Partha Aji
    end
64
65 63ae5d99 Dmitri Dolguikh
    def start_container(container)
66 6d101d7a Dmitri Dolguikh
      started = container.compute_resource.create_container(container.parametrize)
67 63ae5d99 Dmitri Dolguikh
      if started
68
        container.uuid = started.id
69
      else
70 97cb04b2 Daniel Lobato García
        @errors = errors + container.compute_resource.errors.full_messages
71 63ae5d99 Dmitri Dolguikh
      end
72 6d101d7a Dmitri Dolguikh
      started
73
    end
74
75 63ae5d99 Dmitri Dolguikh
    def destroy_wizard_state(wizard_state)
76 6d101d7a Dmitri Dolguikh
      wizard_state.destroy
77 7ff105d1 David Davis
      DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now.utc - 24.hours)])
78 6d101d7a Dmitri Dolguikh
    end
79 8e2848d5 Vanya Jauhal
80
    def load_environment_variables(state, r)
81
      state.environment_variables.each do |environment_variable|
82 cc6d0d73 David Davis
        var = r.environment_variables.build
83 2b80fdd9 orrabin
        var.key = environment_variable.key
84 cc6d0d73 David Davis
        var.value = environment_variable.value
85 8e2848d5 Vanya Jauhal
      end
86
    end
87
88
    def load_exposed_ports(state, r)
89
      state.exposed_ports.each do |e|
90 cc6d0d73 David Davis
        port = r.exposed_ports.build
91 2b80fdd9 orrabin
        port.key = e.key
92 cc6d0d73 David Davis
        port.value = e.value
93 8e2848d5 Vanya Jauhal
      end
94
    end
95 bc82d5d5 Vanya Jauhal
96
    def load_dns(state, r)
97
      state.dns.each do |e|
98 cc6d0d73 David Davis
        dns = r.dns.build
99 2b80fdd9 orrabin
        dns.key = e.key
100 bc82d5d5 Vanya Jauhal
      end
101
    end
102 2a0cf95b David Davis
103
    def full_messages
104 97cb04b2 Daniel Lobato García
      return errors.full_messages if errors.respond_to?(:full_messages)
105
      @errors
106 2a0cf95b David Davis
    end
107 d5d9d39e Shlomi Zadok
108
    def run_container(container)
109
      docker_container = container.compute_resource.find_vm_by_uuid(container.uuid)
110
      error(_('Could not start container')) unless docker_container.send(:start)
111
    end
112 6d101d7a Dmitri Dolguikh
  end
113
end