Project

General

Profile

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

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

1
module Service
2
  class Containers
3
    def errors
4
      @errors ||= []
5
    end
6

    
7
    def start_container!(wizard_state)
8
      ActiveRecord::Base.transaction do
9
        container = Container.new(wizard_state.container_attributes) do |r|
10
          # 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
          load_dns(state, r)
17
        end
18

    
19
        Taxonomy.enabled_taxonomies.each do |taxonomy|
20
          container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
21
        end
22

    
23
        fail ActiveRecord::Rollback unless pull_image(container) && start_container(container)
24

    
25
        container.save!
26
        destroy_wizard_state(wizard_state)
27
        container
28
      end
29
    end
30

    
31
    def pull_image(container)
32
      container.compute_resource.create_image(:fromImage => container.repository_pull_url)
33
    end
34

    
35
    def start_container(container)
36
      started = container.compute_resource.create_container(container.parametrize)
37
      if started
38
        container.uuid = started.id
39
      else
40
        errors << container.compute_resource.errors[:base]
41
      end
42
      started
43
    end
44

    
45
    def destroy_wizard_state(wizard_state)
46
      wizard_state.destroy
47
      DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now - 24.hours)])
48
    end
49

    
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

    
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
  end
73
end