Project

General

Profile

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

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

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 = 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

    
17
    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

    
26
    def create_container_object(wizard_state)
27
      container = Container.new(wizard_state.container_attributes) do |r|
28
        # eagerly load environment variables and exposed ports configuration
29
        state = DockerContainerWizardState.includes(
30
          :environment => [:environment_variables, :exposed_ports]).find(wizard_state.id)
31

    
32
        load_environment_variables(state, r)
33
        load_exposed_ports(state, r)
34
        load_dns(state, r)
35
      end
36

    
37
      Taxonomy.enabled_taxonomies.each do |taxonomy|
38
        container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}"))
39
      end
40

    
41
      pull_image(container)
42
      start_container(container)
43
      errors << container.errors unless container.valid?
44

    
45
      fail ActiveRecord::Rollback if @errors.present?
46

    
47
      container.name = container.in_fog.name[1..-1] unless container.name.present?
48

    
49
      container
50
    end
51

    
52
    def pull_image(container)
53
      success = container.compute_resource.
54
        create_image(:fromImage => container.repository_pull_url)
55
      errors << container.compute_resource.errors[:base] unless success
56
    end
57

    
58
    def start_container(container)
59
      started = container.compute_resource.create_container(container.parametrize)
60
      if started
61
        container.uuid = started.id
62
      else
63
        errors << container.compute_resource.errors[:base]
64
      end
65
      started
66
    end
67

    
68
    def destroy_wizard_state(wizard_state)
69
      wizard_state.destroy
70
      DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now.utc - 24.hours)])
71
    end
72

    
73
    def load_environment_variables(state, r)
74
      state.environment_variables.each do |environment_variable|
75
        r.environment_variables.build :name     => environment_variable.name,
76
                                      :value    => environment_variable.value,
77
                                      :priority => environment_variable.priority
78
      end
79
    end
80

    
81
    def load_exposed_ports(state, r)
82
      state.exposed_ports.each do |e|
83
        r.exposed_ports.build :name => e.name,
84
                              :value => e.value,
85
                              :priority => e.priority
86
      end
87
    end
88

    
89
    def load_dns(state, r)
90
      state.dns.each do |e|
91
        r.dns.build :name => e.name,
92
                    :priority => e.priority
93
      end
94
    end
95

    
96
    def full_messages
97
      errors.respond_to?(:full_messages) ? errors.full_messages : errors
98
    end
99

    
100
    def run_container(container)
101
      docker_container = container.compute_resource.find_vm_by_uuid(container.uuid)
102
      error(_('Could not start container')) unless docker_container.send(:start)
103
    end
104
  end
105
end