Project

General

Profile

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

foreman_docker / app / models / service / containers.rb @ bc82d5d5

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
        container = Container.new(wizard_state.container_attributes) do |r|
10 8e2848d5 Vanya Jauhal
          # eagerly load environment variables and exposed ports configuration
11
          state = DockerContainerWizardState.includes(
12
            :environment => [:environment_variables, :exposed_ports]).find(wizard_state.id)
13
14
          load_environment_variables(state, r)
15
          load_exposed_ports(state, r)
16 bc82d5d5 Vanya Jauhal
          load_dns(state, r)
17 6d101d7a Dmitri Dolguikh
        end
18 8e2848d5 Vanya Jauhal
19 30891c0a Daniel Lobato
        Taxonomy.enabled_taxonomies.each do |taxonomy|
20
          container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
21
        end
22 6d101d7a Dmitri Dolguikh
23 b747db40 Partha Aji
        fail ActiveRecord::Rollback unless pull_image(container) && start_container(container)
24 6d101d7a Dmitri Dolguikh
25
        container.save!
26
        destroy_wizard_state(wizard_state)
27
        container
28
      end
29
    end
30
31 63ae5d99 Dmitri Dolguikh
    def pull_image(container)
32
      container.compute_resource.create_image(:fromImage => container.repository_pull_url)
33 b747db40 Partha Aji
    end
34
35 63ae5d99 Dmitri Dolguikh
    def start_container(container)
36 6d101d7a Dmitri Dolguikh
      started = container.compute_resource.create_container(container.parametrize)
37 63ae5d99 Dmitri Dolguikh
      if started
38
        container.uuid = started.id
39
      else
40
        errors << container.compute_resource.errors[:base]
41
      end
42 6d101d7a Dmitri Dolguikh
      started
43
    end
44
45 63ae5d99 Dmitri Dolguikh
    def destroy_wizard_state(wizard_state)
46 6d101d7a Dmitri Dolguikh
      wizard_state.destroy
47
      DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now - 24.hours)])
48
    end
49 8e2848d5 Vanya Jauhal
50
    def load_environment_variables(state, r)
51
      state.environment_variables.each do |environment_variable|
52
        r.environment_variables.build :name     => environment_variable.name,
53
                                      :value    => environment_variable.value,
54
                                      :priority => environment_variable.priority
55
      end
56
    end
57
58
    def load_exposed_ports(state, r)
59
      state.exposed_ports.each do |e|
60
        r.exposed_ports.build :name => e.name,
61
                              :value => e.value,
62
                              :priority => e.priority
63
      end
64
    end
65 bc82d5d5 Vanya Jauhal
66
    def load_dns(state, r)
67
      state.dns.each do |e|
68
        r.dns.build :name => e.name,
69
                    :priority => e.priority
70
      end
71
    end
72 6d101d7a Dmitri Dolguikh
  end
73
end