Project

General

Profile

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

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

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 do |r|
28
        r.attributes = wizard_state.container_attributes
29
        # eagerly load environment variables and exposed ports configuration
30
        state = DockerContainerWizardState.includes(
31
          :environment => [:environment_variables, :exposed_ports]).find(wizard_state.id)
32

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

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

    
42
      pull_image(container)
43
      start_container(container)
44
      unless container.valid?
45
        @errors = errors + container.errors.full_messages
46
      end
47

    
48
      if @errors.present?
49
        @errors = @errors.flatten.uniq
50
        fail ActiveRecord::Rollback
51
      end
52

    
53
      container.name = container.in_fog.name[1..-1] unless container.name.present?
54

    
55
      container
56
    end
57

    
58
    def pull_image(container)
59
      success = container.compute_resource.
60
        create_image(:fromImage => container.repository_pull_url)
61
      return true if success
62
      @errors = errors + container.compute_resource.errors.full_messages
63
    end
64

    
65
    def start_container(container)
66
      started = container.compute_resource.create_container(container.parametrize)
67
      if started
68
        container.uuid = started.id
69
      else
70
        @errors = errors + container.compute_resource.errors.full_messages
71
      end
72
      started
73
    end
74

    
75
    def destroy_wizard_state(wizard_state)
76
      wizard_state.destroy
77
      DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now.utc - 24.hours)])
78
    end
79

    
80
    def load_environment_variables(state, r)
81
      state.environment_variables.each do |environment_variable|
82
        var = r.environment_variables.build
83
        var.key = environment_variable.key
84
        var.value = environment_variable.value
85
      end
86
    end
87

    
88
    def load_exposed_ports(state, r)
89
      state.exposed_ports.each do |e|
90
        port = r.exposed_ports.build
91
        port.key = e.key
92
        port.value = e.value
93
      end
94
    end
95

    
96
    def load_dns(state, r)
97
      state.dns.each do |e|
98
        dns = r.dns.build
99
        dns.key = e.key
100
      end
101
    end
102

    
103
    def full_messages
104
      return errors.full_messages if errors.respond_to?(:full_messages)
105
      @errors
106
    end
107

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