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
|
|
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
|
errors << container.errors unless container.valid?
|
45
|
|
46
|
fail ActiveRecord::Rollback if @errors.present?
|
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
|
success = container.compute_resource.
|
55
|
create_image(:fromImage => container.repository_pull_url)
|
56
|
errors << container.compute_resource.errors[:base] unless success
|
57
|
end
|
58
|
|
59
|
def start_container(container)
|
60
|
started = container.compute_resource.create_container(container.parametrize)
|
61
|
if started
|
62
|
container.uuid = started.id
|
63
|
else
|
64
|
errors << container.compute_resource.errors[:base]
|
65
|
end
|
66
|
started
|
67
|
end
|
68
|
|
69
|
def destroy_wizard_state(wizard_state)
|
70
|
wizard_state.destroy
|
71
|
DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now.utc - 24.hours)])
|
72
|
end
|
73
|
|
74
|
def load_environment_variables(state, r)
|
75
|
state.environment_variables.each do |environment_variable|
|
76
|
var = r.environment_variables.build
|
77
|
var.name = environment_variable.name
|
78
|
var.value = environment_variable.value
|
79
|
var.priority = environment_variable.priority
|
80
|
end
|
81
|
end
|
82
|
|
83
|
def load_exposed_ports(state, r)
|
84
|
state.exposed_ports.each do |e|
|
85
|
port = r.exposed_ports.build
|
86
|
port.name = e.name
|
87
|
port.value = e.value
|
88
|
port.priority = e.priority
|
89
|
end
|
90
|
end
|
91
|
|
92
|
def load_dns(state, r)
|
93
|
state.dns.each do |e|
|
94
|
dns = r.dns.build
|
95
|
dns.name = e.name
|
96
|
dns.priority = e.priority
|
97
|
end
|
98
|
end
|
99
|
|
100
|
def full_messages
|
101
|
errors.respond_to?(:full_messages) ? errors.full_messages : errors
|
102
|
end
|
103
|
|
104
|
def run_container(container)
|
105
|
docker_container = container.compute_resource.find_vm_by_uuid(container.uuid)
|
106
|
error(_('Could not start container')) unless docker_container.send(:start)
|
107
|
end
|
108
|
end
|
109
|
end
|