Class: Service::Containers

Inherits:
Object
  • Object
show all
Defined in:
app/models/service/containers.rb

Instance Method Summary collapse

Instance Method Details

#create_container!(wizard_state) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/models/service/containers.rb', line 17

def create_container!(wizard_state)
  ActiveRecord::Base.transaction do
    container = create_container_object(wizard_state)
    container.save!
    destroy_wizard_state(wizard_state)
    container
  end
end

#create_container_object(wizard_state) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/service/containers.rb', line 26

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

    load_environment_variables(state, r)
    load_exposed_ports(state, r)
    load_dns(state, r)
  end

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

  pull_image(container)
  start_container(container)
  errors << container.errors unless container.valid?

  fail ActiveRecord::Rollback if @errors.present?

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

  container
end

#destroy_wizard_state(wizard_state) ⇒ Object



69
70
71
72
# File 'app/models/service/containers.rb', line 69

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

#errorsObject



3
4
5
# File 'app/models/service/containers.rb', line 3

def errors
  @errors ||= []
end

#full_messagesObject



100
101
102
# File 'app/models/service/containers.rb', line 100

def full_messages
  errors.respond_to?(:full_messages) ? errors.full_messages : errors
end

#load_dns(state, r) ⇒ Object



92
93
94
95
96
97
98
# File 'app/models/service/containers.rb', line 92

def load_dns(state, r)
  state.dns.each do |e|
    dns = r.dns.build
    dns.name = e.name
    dns.priority = e.priority
  end
end

#load_environment_variables(state, r) ⇒ Object



74
75
76
77
78
79
80
81
# File 'app/models/service/containers.rb', line 74

def load_environment_variables(state, r)
  state.environment_variables.each do |environment_variable|
    var = r.environment_variables.build
    var.name = environment_variable.name
    var.value = environment_variable.value
    var.priority = environment_variable.priority
  end
end

#load_exposed_ports(state, r) ⇒ Object



83
84
85
86
87
88
89
90
# File 'app/models/service/containers.rb', line 83

def load_exposed_ports(state, r)
  state.exposed_ports.each do |e|
    port = r.exposed_ports.build
    port.name = e.name
    port.value = e.value
    port.priority = e.priority
  end
end

#pull_image(container) ⇒ Object



53
54
55
56
57
# File 'app/models/service/containers.rb', line 53

def pull_image(container)
  success = container.compute_resource.
    create_image(:fromImage => container.repository_pull_url)
  errors << container.compute_resource.errors[:base] unless success
end

#run_container(container) ⇒ Object



104
105
106
107
# File 'app/models/service/containers.rb', line 104

def run_container(container)
  docker_container = container.compute_resource.find_vm_by_uuid(container.uuid)
  error(_('Could not start container')) unless docker_container.send(:start)
end

#start_container(container) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'app/models/service/containers.rb', line 59

def start_container(container)
  started = container.compute_resource.create_container(container.parametrize)
  if started
    container.uuid = started.id
  else
    errors << container.compute_resource.errors[:base]
  end
  started
end

#start_container!(wizard_state) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/models/service/containers.rb', line 7

def start_container!(wizard_state)
  ActiveRecord::Base.transaction do
    container = create_container_object(wizard_state)
    container.save!
    run_container(container)
    destroy_wizard_state(wizard_state)
    container
  end
end