Project

General

Profile

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

foreman-docker / app / models / service / containers.rb @ 7ff105d1

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
      unless container.valid?
42
        @errors = container.errors
43
        fail ActiveRecord::Rollback
44
      end
45

    
46
      fail ActiveRecord::Rollback unless pull_image(container) && start_container(container)
47

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

    
50
      container
51
    end
52

    
53
    def pull_image(container)
54
      container.compute_resource.create_image(:fromImage => container.repository_pull_url)
55
    end
56

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

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

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

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

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

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

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