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
52
53
54
55
56
# 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)
  unless container.valid?
    @errors = errors + container.errors.full_messages
  end

  if @errors.present?
    @errors = @errors.flatten.uniq
    fail ActiveRecord::Rollback
  end

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

  container
end

#destroy_wizard_state(wizard_state) ⇒ Object



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

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

#errorsObject



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

def errors
  @errors ||= []
end

#full_messagesObject



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

def full_messages
  return errors.full_messages if errors.respond_to?(:full_messages)
  @errors
end

#load_dns(state, r) ⇒ Object



96
97
98
99
100
101
# File 'app/models/service/containers.rb', line 96

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

#load_environment_variables(state, r) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/service/containers.rb', line 80

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

#load_exposed_ports(state, r) ⇒ Object



88
89
90
91
92
93
94
# File 'app/models/service/containers.rb', line 88

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

#pull_image(container) ⇒ Object



58
59
60
61
62
63
# File 'app/models/service/containers.rb', line 58

def pull_image(container)
  success = container.compute_resource.
    create_image(:fromImage => container.repository_pull_url)
  return true if success
  @errors = errors + container.compute_resource.errors.full_messages
end

#run_container(container) ⇒ Object



108
109
110
111
# File 'app/models/service/containers.rb', line 108

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



65
66
67
68
69
70
71
72
73
# File 'app/models/service/containers.rb', line 65

def start_container(container)
  started = container.compute_resource.create_container(container.parametrize)
  if started
    container.uuid = started.id
  else
    @errors = errors + container.compute_resource.errors.full_messages
  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